Skip to contentSkip to conversation

soc: qcom: pmic_glink: Deliver notifications from a freezable workqueue

PATCH New Vladyslav Tymoshchuk 2 participants 9 messages Nov 29

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover soc: qcom: pmic_glink: Deliver notifications from a freezable workqueue

The GLINK interrupt is not suspended, so PMIC GLINK clients get notifications while devices are suspended, and the work they schedule can use those devices. This was hit on X Elite with a PS8830 retimer [1], on SM8850 with an FSA4480 switch [2] and on SM8750, where dwc3-qcom accesses registers with the clocks off and the SoC resets [3]. Commit c7c56b6ec077 ("soc: qcom: pmic_glink: Fix device access from worker during suspend") fixed the altmode client as a stop-gap, and left the queueing and filtering of the notifications in pmic_glink for later.

- Patch 1 makes ucsi_glink notice a restart of the remote that happened before its work ran; with patch 2, DOWN and UP can arrive back to back.
- Patch 2 passes the notifications and the changes of the service state on from system_freezable_wq, which is frozen while devices are suspended. RFC v1 queued them from ->prepare() to ->complete().
- Patch 3 reports wakeup events, for the clients whose device may wake up the system.
- Patch 4 enables wakeup for ucsi_glink, as proposed in [4].
- Patch 5 makes qcom_battmgr wakeup capable for charger changes, with wakeup disabled by default.

Patches 4 and 5 depend on patch 3, so the series would best go through the Qualcomm SoC tree. It is based on next-20261127.

Not covered: pmic_glink does not wait for the work that a client queues for a notification, so work queued right before a suspend can still overlap the suspend of devices.

Two observations of Joonhoe Kim on RFC v1 are not addressed, as they are the same without the series: a DisplayPort plug wakes up the system only after 26 s, and the DP controller resumes before the held back altmode notification reaches it.

The PMIC GIO [5] and RTC [6] series also touch pmic_glink.c. With this series, notifications for their clients are held back as well.

Tested on the OnePlus Pad 3 (SM8750), with the patches ported to a 7.3-rc5 based tree, debug prints added and the dwc3 fix [3] left out, pulling or plugging the cable while in s2idle:
- with the series: unplug 6 times, plug 4 times, no reset. No notification was passed on before the tasks were restarted. One of the unplugs was done with the UCSI wakeup disabled, and did not wake up the system.
- without it: unplug 3 times, plug 2 times, reset every time. No reset in 3 sleeps with the cable left alone.

A restart of the remote was only simulated, by queueing DOWN and UP back to back while awake: ucsi_glink registered anew 15 times out of 15, and missed 4 of 5 without patch 1. A real one could not be tested, stopping the ADSP resets the SoC in this setup, without the series as well.

Joonhoe, I dropped your Tested-by, as the queueing is new. Could you check this version?

Changes since RFC v1:
- Drop RFC, shorten the descriptions (Sebastian)
- Always deliver from system_freezable_wq, with the service state in the same queue, instead of switching the queueing on and off from the pm callbacks, which also covers the Sashiko findings on that patch
- New patch 1, for ucsi_glink
- Drop the Tested-by of Joonhoe Kim, as the queueing changed
- Report wakeup events through a wakeup source of pmic_glink, kept active until the queue has been passed on (Sashiko)
- qcom_battmgr: only for the SM8350 and SM8550 firmware (Sashiko)
- Rebase on the qcom_battmgr use-after-free fix
- Link to RFC v1: https://lore.kernel.org/all/[email protected]/

An AI assistant (Claude) helped with the analysis, the patches and the descriptions.

[1] https://lore.kernel.org/all/20250205-ucsi-schedule-conn-worker-on-freezable-wq-v1-1-107d1356b77b@linaro.org/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/20261118-gio-framework-rtc-driver-2026-10-12-v3-0-52d78a8f43ee@oss.qualcomm.com/
[6] https://lore.kernel.org/all/[email protected]/

Vladyslav Tymoshchuk (5):
usb: typec: ucsi: glink: Handle a restart of the remote before the
work runs
soc: qcom: pmic_glink: Deliver notifications from a freezable
workqueue
soc: qcom: pmic_glink: Report wakeup events on behalf of clients
usb: typec: ucsi: glink: Wake up the system on connector changes
power: supply: qcom_battmgr: Allow charger changes to wake up the
system

 drivers/power/supply/qcom_battmgr.c |  22 +++
 drivers/soc/qcom/pmic_glink.c       | 272 +++++++++++++++++++++++++---
 drivers/usb/typec/ucsi/ucsi_glink.c |  21 ++-
 include/linux/soc/qcom/pmic_glink.h |   2 +
 4 files changed, 290 insertions(+), 27 deletions(-)

base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d

#1 lore raw

PATCH v2 1/5 usb: typec: ucsi: glink: Handle a restart of the remote before the work runs

When the remote service goes down and comes back up before the register work runs, the work finds the service running and UCSI registered, and does nothing. The restarted firmware is then never initialized, and no longer reports connector changes.

Remember that the service went down, and register UCSI anew.

Fixes 62b5412b1f4a ("usb: typec: ucsi: add PMIC Glink UCSI driver")
---
 drivers/usb/typec/ucsi/ucsi_glink.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/usb/typec/ucsi/ucsi_glink.c +11 -4
index 2696e0b978f6..053571027182 100644
@@ -77,6 +77,7 @@ struct pmic_glink_ucsi {
 	spinlock_t state_lock;
 	bool ucsi_registered;
 	bool pd_running;
+	bool pd_restarted;
 
 	u8 read_buf[UCSI_BUF_V2_SIZE];
 };
@@ -314,18 +315,22 @@ static void pmic_glink_ucsi_register(struct work_struct *work)
 {
 	struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work);
 	unsigned long flags;
-	bool pd_running;
+	bool pd_running, pd_restarted;
 
 	spin_lock_irqsave(&ucsi->state_lock, flags);
 	pd_running = ucsi->pd_running;
+	pd_restarted = ucsi->pd_restarted;
+	ucsi->pd_restarted = false;
 	spin_unlock_irqrestore(&ucsi->state_lock, flags);
 
+	if (ucsi->ucsi_registered && (pd_restarted || !pd_running)) {
+		ucsi_unregister(ucsi->ucsi);
+		ucsi->ucsi_registered = false;
+	}
+
 	if (!ucsi->ucsi_registered && pd_running) {
 		ucsi_register(ucsi->ucsi);
 		ucsi->ucsi_registered = true;
-	} else if (ucsi->ucsi_registered && !pd_running) {
-		ucsi_unregister(ucsi->ucsi);
-		ucsi->ucsi_registered = false;
 	}
 }
 
@@ -354,6 +359,8 @@ static void pmic_glink_ucsi_pdr_notify(void *priv, int state)
 
 	spin_lock_irqsave(&ucsi->state_lock, flags);
 	ucsi->pd_running = (state == SERVREG_SERVICE_STATE_UP);
+	if (!ucsi->pd_running)
+		ucsi->pd_restarted = true;
 	spin_unlock_irqrestore(&ucsi->state_lock, flags);
 	schedule_work(&ucsi->register_work);
 }
#2 lore raw

Re: [PATCH v2 1/5] usb: typec: ucsi: glink: Handle a restart of the remote before the work runs

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] usb: typec: ucsi: glink: use-after-free on UCSI re-registration - [Medium] usb: typec: ucsi: glink…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] usb: typec: ucsi: glink: use-after-free on UCSI re-registration
- [Medium] usb: typec: ucsi: glink: stale pd_running used in pmic_glink_ucsi_register()
- [Low] usb: typec: ucsi: glink: wrong Fixes commit in changelog --

commit a32a3e07eca3f17ff775eaeede6e3c77bdeacb36
Author: Vladyslav Tymoshchuk <[email protected]>

Quoted 11 lines

usb: typec: ucsi: glink: Handle a restart of the remote before the work runs

When the remote service goes down and comes back up before the register work runs, the work finds the service running and UCSI registered, and does nothing. The restarted firmware is then never initialized, and no longer reports connector changes.

Remember that the service went down, and register UCSI anew.

Fixes 62b5412b1f4a ("usb: typec: ucsi: add PMIC Glink UCSI driver")

[Severity: Low]
Should the Fixes tag reference 11bb2ffb6793 ("usb: typec: ucsi: Move unregister out of atomic section") instead?

In 62b5412b1f4a ("usb: typec: ucsi: add PMIC Glink UCSI driver"), pmic_glink_ucsi_pdr_notify() called ucsi_unregister() directly when the service went down. 11bb2ffb6793 ("usb: typec: ucsi: Move unregister out of atomic section") deferred unregistration to pmic_glink_ucsi_register() and introduced the pd_running and ucsi_registered checks.

Quoted 4 lines
drivers/usb/typec/ucsi/ucsi_glink.c +0 -0
index 2696e0b978f66..0535710271822 100644

[ ... ]

Quoted 7 lines
drivers/usb/typec/ucsi/ucsi_glink.c +1 -1
@@ -314,18 +315,22 @@ static void pmic_glink_ucsi_register(struct work_struct *work)
 {
 	struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work);
 	unsigned long flags;
-	bool pd_running;
+	bool pd_running, pd_restarted;
 

drivers/usb/typec/ucsi/ucsi_glink.c static void pmic_glink_ucsi_register(struct work_struct *work) · line 329

Quoted 10 lines
drivers/usb/typec/ucsi/ucsi_glink.c +6 -0
 	spin_lock_irqsave(&ucsi->state_lock, flags);
 	pd_running = ucsi->pd_running;
+	pd_restarted = ucsi->pd_restarted;
+	ucsi->pd_restarted = false;
 	spin_unlock_irqrestore(&ucsi->state_lock, flags);
 
+	if (ucsi->ucsi_registered && (pd_restarted || !pd_running)) {
+		ucsi_unregister(ucsi->ucsi);
+		ucsi->ucsi_registered = false;
+	}

[Severity: Medium]
Can pd_running become stale while ucsi_unregister() sleeps?

ucsi_unregister() sleeps while cancelling work and waiting on GLINK commands. If the remote service goes down during that window, pmic_glink_ucsi_register() continues with the earlier pd_running == true snapshot, calls ucsi_register() against the down service, and sets ucsi->ucsi_registered = true.

drivers/usb/typec/ucsi/ucsi_glink.c static void pmic_glink_ucsi_register(struct work_struct *work) · line 335

Quoted 9 lines
drivers/usb/typec/ucsi/ucsi_glink.c +1 -3
+
 	if (!ucsi->ucsi_registered && pd_running) {
 		ucsi_register(ucsi->ucsi);
 		ucsi->ucsi_registered = true;
-	} else if (ucsi->ucsi_registered && !pd_running) {
-		ucsi_unregister(ucsi->ucsi);
-		ucsi->ucsi_registered = false;
 	}
 }

[Severity: High]
Can re-registering the same ucsi->ucsi instance after ucsi_unregister() cause a use-after-free or double free on ucsi->connector?

ucsi_unregister() frees ucsi->connector without clearing the pointer, ucsi->ntfy, or EVENT_PENDING in ucsi->flags:

drivers/usb/typec/ucsi/ucsi.c:ucsi_unregister() {
    ...
	for (i = 0; i < ucsi->cap.num_connectors; i++) {
		cancel_work_sync(&ucsi->connector[i].work);
		ucsi_unregister_port(&ucsi->connector[i]);
		lockdep_unregister_key(&ucsi->connector[i].lock_key);
	}

	kfree(ucsi->connector);
}

Because ucsi_register() queues ucsi->work asynchronously, ucsi->connector remains a dangling pointer and ucsi->ntfy retains UCSI_ENABLE_NTFY_CONNECTOR_CHANGE until ucsi_init() assigns a new connector array:

kworker (register_work / ucsi->work) kworker (notify_work)
pmic_glink_ucsi_register()
ucsi_unregister(ucsi->ucsi)

    kfree(ucsi->connector)
  ucsi_register(ucsi->ucsi)
    queue_delayed_work(..., &ucsi->work, 0)
                                       pmic_glink_ucsi_notify()
                                         ucsi_notify_common()
                                           ucsi_connector_change()
                                             con = &ucsi->connector[num - 1];
                                             schedule_work(&con->work)
                                               <-- use-after-free

In addition, if ucsi_register() fails (its return value is not checked), if ucsi_init() fails before reallocating ucsi->connector, or if another ucsi_unregister() runs before ucsi_init() finishes, the next ucsi_unregister() sees the non-NULL ucsi->connector pointer, dereferences the freed array, and calls kfree(ucsi->connector) a second time.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/5 soc: qcom: pmic_glink: Deliver notifications from a freezable workqueue

In reply to Vladyslav Tymoshchuk

The GLINK interrupt is not suspended, so notifications reach the clients during system suspend and resume. The clients handle them in work on the system workqueue, which can then use devices that are suspended. On SM8750, unplugging USB in suspend-to-idle makes dwc3-qcom access its registers with the clocks off, and the SoC resets.

Queue the notifications, and pass them on from work on system_freezable_wq, which is frozen before the devices suspend and thawed after they have resumed. Queue changes of the service state as well, so that the clients see both in order. Responses are passed on right away, as the sender of the request is waiting for them.

Clients treat a notification as the latest state or as a hint to read it, so keep only the latest of identical ones, and at most 64.

Fixes 58ef4ece1e41 ("soc: qcom: pmic_glink: Introduce base PMIC GLINK driver")
---
 drivers/soc/qcom/pmic_glink.c | 192 ++++++++++++++++++++++++++++++----
 1 file changed, 169 insertions(+), 23 deletions(-)
drivers/soc/qcom/pmic_glink.c +169 -23
index 0e354fcd87c9..805c4ee85bba 100644
@@ -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_NOTIFICATIONS 64
 
 enum {
 	PMIC_GLINK_CLIENT_BATT = 0,
@@ -41,15 +44,23 @@ struct pmic_glink {
 	struct auxiliary_device ps_aux;
 	struct auxiliary_device ucsi_aux;
 
-	/* serializing client_state and pdr_state updates */
+	/* serializing client_state, service_state and pdr_state updates */
 	struct mutex state_lock;
-	unsigned int client_state;
+	unsigned int client_state;	/* last state passed on to the clients */
+	unsigned int service_state;	/* last state queued for the clients */
 	unsigned int pdr_state;
 	bool pdr_available;
 
 	/* serializing clients list updates */
 	spinlock_t client_lock;
 	struct list_head clients;
+
+	/* serializing the queue of events for the clients */
+	spinlock_t event_lock;
+	struct list_head events;
+	unsigned int num_notifications;
+	unsigned int num_dropped;
+	struct work_struct deliver_work;
 };
 
 static struct pmic_glink *__pmic_glink;
@@ -66,6 +77,14 @@ struct pmic_glink_client {
 	void *priv;
 };
 
+/* A notification, or a change of the service state if @len is 0 */
+struct pmic_glink_event {
+	struct list_head node;
+	unsigned int state;
+	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 +168,62 @@ 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 void pmic_glink_add_notification(struct pmic_glink *pg, const void *data, size_t len)
+{
+	struct pmic_glink_event *event;
+
+	lockdep_assert_held(&pg->event_lock);
+
+	/* An identical notification brings nothing new, keep the latest one */
+	list_for_each_entry(event, &pg->events, node) {
+		if (event->len == len && !memcmp(event->data, data, len)) {
+			list_move_tail(&event->node, &pg->events);
+			return;
+		}
+	}
+
+	if (pg->num_notifications == PMIC_GLINK_MAX_NOTIFICATIONS) {
+		list_for_each_entry(event, &pg->events, node) {
+			if (event->len)
+				break;
+		}
+		list_del(&event->node);
+		kfree(event);
+		pg->num_notifications--;
+		pg->num_dropped++;
+	}
+
+	event = kmalloc_flex(*event, data, len, GFP_ATOMIC);
+	if (!event) {
+		pg->num_dropped++;
+		return;
+	}
+
+	event->len = len;
+	memcpy(event->data, data, len);
+	list_add_tail(&event->node, &pg->events);
+	pg->num_notifications++;
+}
+
 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 +232,16 @@ 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);
+	/* The sender of a request is waiting for the response */
+	if (le32_to_cpu(hdr->type) == PMIC_GLINK_REQ_RESP) {
+		pmic_glink_dispatch(pg, data, len);
+		return 0;
 	}
-	spin_unlock_irqrestore(&pg->client_lock, flags);
+
+	guard(spinlock_irqsave)(&pg->event_lock);
+
+	pmic_glink_add_notification(pg, data, len);
+	queue_work(system_freezable_wq, &pg->deliver_work);
 
 	return 0;
 }
@@ -210,13 +282,14 @@ static void pmic_glink_del_aux_device(struct pmic_glink *pg,
 	auxiliary_device_uninit(aux);
 }
 
-static void pmic_glink_state_notify_clients(struct pmic_glink *pg)
+static void pmic_glink_update_state(struct pmic_glink *pg)
 {
-	struct pmic_glink_client *client;
-	unsigned int new_state = pg->client_state;
-	unsigned long flags;
+	unsigned int new_state = pg->service_state;
+	struct pmic_glink_event *event;
+
+	lockdep_assert_held(&pg->state_lock);
 
-	if (pg->client_state != SERVREG_SERVICE_STATE_UP) {
+	if (pg->service_state != SERVREG_SERVICE_STATE_UP) {
 		if (pg->pdr_state == SERVREG_SERVICE_STATE_UP && pg->ept)
 			new_state = SERVREG_SERVICE_STATE_UP;
 	} else {
@@ -224,13 +297,78 @@ static void pmic_glink_state_notify_clients(struct pmic_glink *pg)
 			new_state = SERVREG_SERVICE_STATE_DOWN;
 	}
 
-	if (new_state != pg->client_state) {
-		spin_lock_irqsave(&pg->client_lock, flags);
-		list_for_each_entry(client, &pg->clients, node)
-			client->pdr_notify(client->priv, new_state);
-		spin_unlock_irqrestore(&pg->client_lock, flags);
-		pg->client_state = new_state;
+	if (new_state == pg->service_state)
+		return;
+
+	event = kzalloc_obj(*event);
+	if (!event)
+		return;
+
+	event->state = new_state;
+	pg->service_state = new_state;
+
+	guard(spinlock_irqsave)(&pg->event_lock);
+
+	list_add_tail(&event->node, &pg->events);
+	queue_work(system_freezable_wq, &pg->deliver_work);
+}
+
+static void pmic_glink_notify_state(struct pmic_glink *pg, unsigned int state)
+{
+	struct pmic_glink_client *client;
+	unsigned long flags;
+
+	guard(mutex)(&pg->state_lock);
+
+	spin_lock_irqsave(&pg->client_lock, flags);
+	list_for_each_entry(client, &pg->clients, node)
+		client->pdr_notify(client->priv, state);
+	spin_unlock_irqrestore(&pg->client_lock, flags);
+	pg->client_state = state;
+}
+
+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_event *event;
+	unsigned int dropped;
+	unsigned long flags;
+
+	for (;;) {
+		spin_lock_irqsave(&pg->event_lock, flags);
+		event = list_first_entry_or_null(&pg->events, struct pmic_glink_event, node);
+		if (!event) {
+			dropped = pg->num_dropped;
+			pg->num_dropped = 0;
+			spin_unlock_irqrestore(&pg->event_lock, flags);
+			break;
+		}
+
+		list_del(&event->node);
+		if (event->len)
+			pg->num_notifications--;
+		spin_unlock_irqrestore(&pg->event_lock, flags);
+
+		if (event->len)
+			pmic_glink_dispatch(pg, event->data, event->len);
+		else
+			pmic_glink_notify_state(pg, event->state);
+		kfree(event);
 	}
+
+	if (dropped)
+		dev_warn(pg->dev, "%u notifications lost\n", dropped);
+}
+
+static void pmic_glink_release_events(void *data)
+{
+	struct pmic_glink *pg = data;
+	struct pmic_glink_event *event, *tmp;
+
+	disable_work_sync(&pg->deliver_work);
+
+	list_for_each_entry_safe(event, tmp, &pg->events, node)
+		kfree(event);
 }
 
 static void pmic_glink_pdr_callback(int state, char *svc_path, void *priv)
@@ -240,7 +378,7 @@ static void pmic_glink_pdr_callback(int state, char *svc_path, void *priv)
 	guard(mutex)(&pg->state_lock);
 	pg->pdr_state = state;
 
-	pmic_glink_state_notify_clients(pg);
+	pmic_glink_update_state(pg);
 }
 
 static int pmic_glink_rpmsg_probe(struct rpmsg_device *rpdev)
@@ -260,7 +398,7 @@ static int pmic_glink_rpmsg_probe(struct rpmsg_device *rpdev)
 	pg->ept = rpdev->ept;
 	if (!pg->pdr_available)
 		pg->pdr_state = SERVREG_SERVICE_STATE_UP;
-	pmic_glink_state_notify_clients(pg);
+	pmic_glink_update_state(pg);
 
 	return 0;
 }
@@ -278,7 +416,7 @@ static void pmic_glink_rpmsg_remove(struct rpmsg_device *rpdev)
 	pg->ept = NULL;
 	if (!pg->pdr_available)
 		pg->pdr_state = SERVREG_SERVICE_STATE_DOWN;
-	pmic_glink_state_notify_clients(pg);
+	pmic_glink_update_state(pg);
 }
 
 static const struct rpmsg_device_id pmic_glink_rpmsg_id_match[] = {
@@ -315,6 +453,14 @@ static int pmic_glink_probe(struct platform_device *pdev)
 	spin_lock_init(&pg->client_lock);
 	mutex_init(&pg->state_lock);
 
+	INIT_LIST_HEAD(&pg->events);
+	spin_lock_init(&pg->event_lock);
+	INIT_WORK(&pg->deliver_work, pmic_glink_deliver_work);
+
+	ret = devm_add_action_or_reset(&pdev->dev, pmic_glink_release_events, pg);
+	if (ret)
+		return ret;
+
 	pg->data = of_device_get_match_data(&pdev->dev);
 	if (!pg->data)
 		return -EINVAL;
#4 lore raw

PATCH v2 3/5 soc: qcom: pmic_glink: Report wakeup events on behalf of clients

In reply to Vladyslav Tymoshchuk

The GLINK interrupt is not a wakeup interrupt, as most PMIC GLINK messages must not wake up the system, and the clients get notifications only after the system has resumed.

Report the wakeup events in pmic_glink, when a notification arrives: it is a wakeup event if the device of the client may wake up the system and the optional wakeup filter of the client accepts it. Use a wakeup source of pmic_glink, and keep it active until the queued notifications have been passed on, so that the system does not go back to sleep before that.

No client enables wakeup yet, so nothing changes for them.

---
 drivers/soc/qcom/pmic_glink.c       | 80 +++++++++++++++++++++++++++++
 include/linux/soc/qcom/pmic_glink.h |  2 +
 2 files changed, 82 insertions(+)
drivers/soc/qcom/pmic_glink.c +80 -0
index 805c4ee85bba..a25d7967629d 100644
@@ -9,12 +9,14 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/pm_wakeup.h>
 #include <linux/rpmsg.h>
 #include <linux/slab.h>
 #include <linux/soc/qcom/pdr.h>
 #include <linux/soc/qcom/pmic_glink.h>
 #include <linux/spinlock.h>
 #include <linux/string.h>
+#include <linux/suspend.h>
 #include <linux/workqueue.h>
 
 #define PMIC_GLINK_SEND_TIMEOUT (5 * HZ)
@@ -61,6 +63,8 @@ struct pmic_glink {
 	unsigned int num_notifications;
 	unsigned int num_dropped;
 	struct work_struct deliver_work;
+
+	struct wakeup_source *ws;
 };
 
 static struct pmic_glink *__pmic_glink;
@@ -70,10 +74,12 @@ struct pmic_glink_client {
 	struct list_head node;
 
 	struct pmic_glink *pg;
+	struct device *dev;
 	unsigned int id;
 
 	void (*cb)(const void *data, size_t len, void *priv);
 	void (*pdr_notify)(void *priv, int state);
+	bool (*wakeup_filter)(const void *data, size_t len, void *priv);
 	void *priv;
 };
 
@@ -96,6 +102,22 @@ static void _devm_pmic_glink_release_client(struct device *dev, void *res)
 	spin_unlock_irqrestore(&pg->client_lock, flags);
 }
 
+/**
+ * devm_pmic_glink_client_alloc() - allocate a client of the PMIC GLINK service
+ * @dev:	client device, a child of the pmic_glink device
+ * @id:		owner of the messages for the client
+ * @cb:		called, in atomic context, with the messages for the client
+ * @pdr:	called, in atomic context, with the state of the remote service
+ * @priv:	passed to @cb, @pdr and the wakeup filter
+ *
+ * A notification for a client whose @dev may wake up the system, see
+ * device_may_wakeup(), is a wakeup event, unless the wakeup filter of the
+ * client rejects it. Notifications that arrive during system sleep reach @cb
+ * once the system has resumed, only the latest one of identical notifications.
+ *
+ * Return: the client, to be registered with pmic_glink_client_register(), or
+ * an ERR_PTR() on failure.
+ */
 struct pmic_glink_client *devm_pmic_glink_client_alloc(struct device *dev,
 						       unsigned int id,
 						       void (*cb)(const void *, size_t, void *),
@@ -110,6 +132,7 @@ struct pmic_glink_client *devm_pmic_glink_client_alloc(struct device *dev,
 		return ERR_PTR(-ENOMEM);
 
 	client->pg = pg;
+	client->dev = dev;
 	client->id = id;
 	client->cb = cb;
 	client->pdr_notify = pdr;
@@ -122,6 +145,22 @@ struct pmic_glink_client *devm_pmic_glink_client_alloc(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_pmic_glink_client_alloc);
 
+/**
+ * pmic_glink_client_set_wakeup_filter() - select the wakeup notifications
+ * @client:	client from devm_pmic_glink_client_alloc(), not registered yet
+ * @filter:	called, in atomic context, with each notification for the client
+ *		while its device may wake up the system; returns true to wake
+ *		the system up
+ *
+ * Without a filter, every notification for the client is a wakeup event.
+ */
+void pmic_glink_client_set_wakeup_filter(struct pmic_glink_client *client,
+					 bool (*filter)(const void *, size_t, void *))
+{
+	client->wakeup_filter = filter;
+}
+EXPORT_SYMBOL_GPL(pmic_glink_client_set_wakeup_filter);
+
 void pmic_glink_client_register(struct pmic_glink_client *client)
 {
 	struct pmic_glink *pg = client->pg;
@@ -168,6 +207,24 @@ int pmic_glink_send(struct pmic_glink_client *client, void *data, size_t len)
 }
 EXPORT_SYMBOL_GPL(pmic_glink_send);
 
+static bool pmic_glink_is_wakeup_event(struct pmic_glink *pg, const void *data, size_t len)
+{
+	const struct pmic_glink_hdr *hdr = data;
+	struct pmic_glink_client *client;
+
+	guard(spinlock_irqsave)(&pg->client_lock);
+
+	list_for_each_entry(client, &pg->clients, node) {
+		if (client->id != le32_to_cpu(hdr->owner) || !device_may_wakeup(client->dev))
+			continue;
+
+		if (!client->wakeup_filter || client->wakeup_filter(data, len, client->priv))
+			return true;
+	}
+
+	return false;
+}
+
 static void pmic_glink_dispatch(struct pmic_glink *pg, const void *data, size_t len)
 {
 	const struct pmic_glink_hdr *hdr = data;
@@ -224,6 +281,7 @@ static int pmic_glink_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 {
 	struct pmic_glink_hdr *hdr;
 	struct pmic_glink *pg = dev_get_drvdata(&rpdev->dev);
+	bool wakeup;
 
 	if (len < sizeof(*hdr)) {
 		dev_warn(pg->dev, "ignoring truncated message\n");
@@ -238,8 +296,16 @@ static int pmic_glink_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 		return 0;
 	}
 
+	wakeup = pmic_glink_is_wakeup_event(pg, data, len);
+
 	guard(spinlock_irqsave)(&pg->event_lock);
 
+	/* Keep the system awake until the notification has been passed on */
+	if (wakeup) {
+		__pm_stay_awake(pg->ws);
+		pm_system_wakeup();
+	}
+
 	pmic_glink_add_notification(pg, data, len);
 	queue_work(system_freezable_wq, &pg->deliver_work);
 
@@ -340,6 +406,7 @@ static void pmic_glink_deliver_work(struct work_struct *work)
 		if (!event) {
 			dropped = pg->num_dropped;
 			pg->num_dropped = 0;
+			__pm_relax(pg->ws);
 			spin_unlock_irqrestore(&pg->event_lock, flags);
 			break;
 		}
@@ -435,6 +502,11 @@ static struct rpmsg_driver pmic_glink_rpmsg_driver = {
 	},
 };
 
+static void pmic_glink_unregister_wakeup_source(void *ws)
+{
+	wakeup_source_unregister(ws);
+}
+
 static int pmic_glink_probe(struct platform_device *pdev)
 {
 	struct pdr_service *service;
@@ -453,6 +525,14 @@ static int pmic_glink_probe(struct platform_device *pdev)
 	spin_lock_init(&pg->client_lock);
 	mutex_init(&pg->state_lock);
 
+	pg->ws = wakeup_source_register(&pdev->dev, dev_name(&pdev->dev));
+	if (!pg->ws && IS_ENABLED(CONFIG_PM_SLEEP))
+		return -ENOMEM;
+
+	ret = devm_add_action_or_reset(&pdev->dev, pmic_glink_unregister_wakeup_source, pg->ws);
+	if (ret)
+		return ret;
+
 	INIT_LIST_HEAD(&pg->events);
 	spin_lock_init(&pg->event_lock);
 	INIT_WORK(&pg->deliver_work, pmic_glink_deliver_work);
include/linux/soc/qcom/pmic_glink.h +2 -0
index 7cddf1027752..786d2f35a9a9 100644
@@ -28,6 +28,8 @@ struct pmic_glink_client *devm_pmic_glink_client_alloc(struct device *dev,
 						       void (*cb)(const void *, size_t, void *),
 						       void (*pdr)(void *, int),
 						       void *priv);
+void pmic_glink_client_set_wakeup_filter(struct pmic_glink_client *client,
+					 bool (*filter)(const void *, size_t, void *));
 void pmic_glink_client_register(struct pmic_glink_client *client);
 
 #endif
#5 lore raw

PATCH v2 4/5 usb: typec: ucsi: glink: Wake up the system on connector changes

In reply to Vladyslav Tymoshchuk

Enable wakeup on the device, so that pmic_glink reports the UCSI notifications as wakeup events: plugging a cable in or out brings the system out of suspend-to-idle. User space can change it through power/wakeup.

Jishnu Prakash proposed the same [1], with the event reported from the callback, which now runs only after the system has resumed.

---
 drivers/usb/typec/ucsi/ucsi_glink.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/usb/typec/ucsi/ucsi_glink.c +6 -0
index 053571027182..a0e28ec715bd 100644
@@ -7,6 +7,7 @@
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/of_device.h>
+#include <linux/pm_wakeup.h>
 #include <linux/property.h>
 #include <linux/soc/qcom/pdr.h>
 #include <linux/usb/typec_mux.h>
@@ -462,6 +463,11 @@ static int pmic_glink_ucsi_probe(struct auxiliary_device *adev,
 		ucsi->port_orientation[port] = desc;
 	}
 
+	/* pmic_glink reports the notifications as wakeup events */
+	ret = devm_device_init_wakeup(dev);
+	if (ret)
+		return ret;
+
 	ucsi->client = devm_pmic_glink_client_alloc(dev, PMIC_GLINK_OWNER_USBC,
 						    pmic_glink_ucsi_callback,
 						    pmic_glink_ucsi_pdr_notify,
#6 lore raw

PATCH v2 5/5 power: supply: qcom_battmgr: Allow charger changes to wake up the system

In reply to Vladyslav Tymoshchuk

Most notifications of the battery manager report battery status, which is not worth a wakeup. Changes of the USB and wireless charger, like plugging one in, can be.

Make the device wakeup capable, with a filter for the charger notifications. Leave wakeup disabled by default, it is up to user space through power/wakeup.

Do this only for the SM8350 and SM8550 firmware, as the SC8280XP and X1E80100 firmware sends no notification when a charger changes.

---
 drivers/power/supply/qcom_battmgr.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/power/supply/qcom_battmgr.c +22 -0
index 69473ea753f6..9902192b2921 100644
@@ -9,6 +9,7 @@
 #include <linux/mutex.h>
 #include <linux/nvmem-consumer.h>
 #include <linux/of_device.h>
+#include <linux/pm_wakeup.h>
 #include <linux/power_supply.h>
 #include <linux/property.h>
 #include <linux/soc/qcom/pdr.h>
@@ -1723,6 +1724,20 @@ static void qcom_battmgr_sm8350_callback(struct qcom_battmgr *battmgr,
 	complete(&battmgr->ack);
 }
 
+static bool qcom_battmgr_is_charger_notification(const void *data, size_t len, void *priv)
+{
+	const struct qcom_battmgr_message *msg = data;
+	unsigned int notification;
+
+	if (le32_to_cpu(msg->hdr.opcode) != BATTMGR_NOTIFICATION ||
+	    len != sizeof(msg->hdr) + sizeof(msg->notification))
+		return false;
+
+	notification = le32_to_cpu(msg->notification) & 0xff;
+
+	return notification == NOTIF_USB_PROPERTY || notification == NOTIF_WLS_PROPERTY;
+}
+
 static void qcom_battmgr_callback(const void *data, size_t len, void *priv)
 {
 	const struct pmic_glink_hdr *hdr = data;
@@ -1896,6 +1911,13 @@ static int qcom_battmgr_probe(struct auxiliary_device *adev,
 	if (ret)
 		return ret;
 
+	if (battmgr->variant == QCOM_BATTMGR_SM8350 ||
+	    battmgr->variant == QCOM_BATTMGR_SM8550) {
+		device_set_wakeup_capable(dev, true);
+		pmic_glink_client_set_wakeup_filter(battmgr->client,
+						    qcom_battmgr_is_charger_notification);
+	}
+
 	pmic_glink_client_register(battmgr->client);
 
 	return 0;
#7 lore raw

Re: [PATCH v2 5/5] power: supply: qcom_battmgr: Allow charger changes to wake up the system

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] power: supply: qcom_battmgr: missing TAOYAO variant in qcom_battmgr_probe() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] power: supply: qcom_battmgr: missing TAOYAO variant in qcom_battmgr_probe()
--

commit 986e08901fb06b02155aeb9b0186523faacf2862
Author: Vladyslav Tymoshchuk <[email protected]>

power: supply: qcom_battmgr: Allow charger changes to wake up the system

This commit marks the battery manager device as wakeup-capable on SM8350 and SM8550 variants and registers a PMIC GLINK wakeup filter for USB and wireless charger property notifications.

Quoted 4 lines
drivers/power/supply/qcom_battmgr.c +0 -0
index 69473ea753f66..9902192b29218 100644

[ ... ]

drivers/power/supply/qcom_battmgr.c static int qcom_battmgr_probe(struct auxiliary_device *adev, · line 1919

Quoted 10 lines
drivers/power/supply/qcom_battmgr.c +6 -0
@@ -1896,6 +1911,13 @@ static int qcom_battmgr_probe(struct auxiliary_device *adev,
 	if (ret)
 		return ret;
 
+	if (battmgr->variant == QCOM_BATTMGR_SM8350 ||
+	    battmgr->variant == QCOM_BATTMGR_SM8550) {
+		device_set_wakeup_capable(dev, true);
+		pmic_glink_client_set_wakeup_filter(battmgr->client,
+						    qcom_battmgr_is_charger_notification);
+	}

[Severity: Medium]
Should this condition also include QCOM_BATTMGR_TAOYAO (or check that battmgr->variant is neither QCOM_BATTMGR_SC8280XP nor QCOM_BATTMGR_X1E80100, matching the rest of the driver)?

QCOM_BATTMGR_TAOYAO uses the SM8350 firmware protocol, registers sm8350_usb_psy_desc and sm8350_wls_psy_desc, and receives NOTIF_USB_PROPERTY and NOTIF_WLS_PROPERTY notifications, so excluding it leaves charger wakeup unavailable on xiaomi,taoyao-pmic-glink platforms.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#8 lore raw
In reply to Vladyslav Tymoshchuk
Sashiko has finished reviewing this 5-patch series:

Sashiko has finished reviewing this 5-patch series:

- Patches 2-4: no issues found.
- Patch 1 ("usb: typec: ucsi: glink: Handle a restart of the rem..."): 3 potential issues (1 High, 1 Medium, 1 Low)
- Patch 5 ("power: supply: qcom_battmgr: Allow charger changes t..."): 1 potential issue (1 Medium)

Detailed reviews have been sent as separate replies to patches 1 and 5.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]