Skip to contentSkip to conversation

driver core / pmdomain: Add support for fined grained sync_state

PATCH Superseded Ulf Hansson 3 participants 24 messages Apr 10 – May 5

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover driver core / pmdomain: Add support for fined grained sync_state

Since the introduction [1] of the common sync_state support for pmdomains (genpd), we have encountered a lot of various interesting problems. In most cases the new behaviour of genpd triggered some weird platform specific bugs.

That said, in LPC in Tokyo me and Saravana hosted a session to walk through the remaining limitations that we have found for genpd's sync state support. In particular, we discussed the problems we have for the so-called onecell power domain providers, where a single provider typically provides multiple independent power domains, all with their own set of consumers.

Note that, onecell power domain providers are very common. It's being used by many SoCs/platforms/technologies. To name a few:
SCMI, Qualcomm, NXP, Mediatek, Renesas, TI, etc.

Anyway, in these cases, the generic sync_state mechanism with fw_devlink isn't fine grained enough, as we end up waiting for all consumers for all power domains before the ->sync_callback gets called for the supplier/provider. In other words, we may end up keeping unused power domains powered-on, for no good reasons.

The series intends to fix this problem. Please have a look at the commit messages for more details and help review/test!

Kind regards
Ulf Hansson

[1]
https://lore.kernel.org/all/[email protected]/

Ulf Hansson (9):
driver core: Enable suppliers to implement fine grained sync_state
support
driver core: Add dev_set_drv_queue_sync_state()
pmdomain: core: Move genpd_get_from_provider()
pmdomain: core: Add initial fine grained sync_state support
pmdomain: core: Extend fine grained sync_state to more onecell
providers
pmdomain: core: Export a common function for ->queue_sync_state()
pmdomain: renesas: rcar-gen4-sysc: Drop GENPD_FLAG_NO_STAY_ON
pmdomain: renesas: rcar-sysc: Drop GENPD_FLAG_NO_STAY_ON
pmdomain: renesas: rmobile-sysc: Drop GENPD_FLAG_NO_STAY_ON

 drivers/base/core.c                       |   7 +-
 drivers/pmdomain/core.c                   | 205 ++++++++++++++++++----
 drivers/pmdomain/renesas/rcar-gen4-sysc.c |   1 -
 drivers/pmdomain/renesas/rcar-sysc.c      |   1 -
 drivers/pmdomain/renesas/rmobile-sysc.c   |   3 +-
 include/linux/device.h                    |  12 ++
 include/linux/device/driver.h             |   7 +
 include/linux/pm_domain.h                 |   3 +
 8 files changed, 197 insertions(+), 42 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/9 driver core: Enable suppliers to implement fine grained sync_state support

The common sync_state support isn't fine grained enough for some types of suppliers, like power domains for example. Especially when a supplier provides multiple independent power domains, each with their own set of consumers. In these cases we need to wait for all consumers for all the provided power domains before invoking the supplier's ->sync_state().

To allow a more fine grained sync_state support to be implemented on per supplier's driver basis, let's add a new optional callback. As soon as there is an update worth to consider in regards to managing sync_state for a supplier device, __device_links_queue_sync_state() invokes the callback.

---
 drivers/base/core.c           | 7 ++++++-
 include/linux/device/driver.h | 7 +++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/base/core.c +6 -1
index 09b98f02f559..4085a011d8ca 100644
@@ -1106,7 +1106,9 @@ int device_links_check_suppliers(struct device *dev)
  * Queues a device for a sync_state() callback when the device links write lock
  * isn't held. This allows the sync_state() execution flow to use device links
  * APIs.  The caller must ensure this function is called with
- * device_links_write_lock() held.
+ * device_links_write_lock() held.  Note, if the optional queue_sync_state()
+ * callback has been assigned too, it gets called for every update to allowing a
+ * more fine grained support to be implemented on per supplier basis.
  *
  * This function does a get_device() to make sure the device is not freed while
  * on this list.
@@ -1126,6 +1128,9 @@ static void __device_links_queue_sync_state(struct device *dev,
 	if (dev->state_synced)
 		return;
 
+	if (dev->driver && dev->driver->queue_sync_state)
+		dev->driver->queue_sync_state(dev);
+
 	list_for_each_entry(link, &dev->links.consumers, s_node) {
 		if (!device_link_test(link, DL_FLAG_MANAGED))
 			continue;
include/linux/device/driver.h +7 -0
index bbc67ec513ed..bc9ae1cbe03c 100644
@@ -68,6 +68,12 @@ enum probe_type {
  *		be called at late_initcall_sync level. If the device has
  *		consumers that are never bound to a driver, this function
  *		will never get called until they do.
+ * @queue_sync_state: Similar to the ->sync_state() callback, but called to
+ *		allow syncing device state to software state in a more fine
+ *		grained way. It is called when there is an updated state that
+ *		may be worth to consider for any of the consumers linked to
+ *		this device. If implemented, the ->sync_state() callback is
+ *		required too.
  * @remove:	Called when the device is removed from the system to
  *		unbind a device from this driver.
  * @shutdown:	Called at shut-down time to quiesce the device.
@@ -110,6 +116,7 @@ struct device_driver {
 
 	int (*probe) (struct device *dev);
 	void (*sync_state)(struct device *dev);
+	void (*queue_sync_state)(struct device *dev);
 	int (*remove) (struct device *dev);
 	void (*shutdown) (struct device *dev);
 	int (*suspend) (struct device *dev, pm_message_t state);
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH v2 1/9] driver core: Enable suppliers to implement fine grained sync_state support

Quoted 23 lines · Ulf Hansson

The common sync_state support isn't fine grained enough for some types of suppliers, like power domains for example. Especially when a supplier provides multiple independent power domains, each with their own set of consumers. In these cases we need to wait for all consumers for all the provided power domains before invoking the supplier's ->sync_state().

To allow a more fine grained sync_state support to be implemented on per supplier's driver basis, let's add a new optional callback. As soon as there is an update worth to consider in regards to managing sync_state for a supplier device, __device_links_queue_sync_state() invokes the callback.

---
 drivers/base/core.c           | 7 ++++++-
 include/linux/device/driver.h | 7 +++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/base/core.c +0 -0
index 09b98f02f559..4085a011d8ca 100644

drivers/base/core.c int device_links_check_suppliers(struct device *dev) · line 1110

Quoted 6 lines
drivers/base/core.c +2 -1
@@ -1106,7 +1106,9 @@ int device_links_check_suppliers(struct device *dev)
  * Queues a device for a sync_state() callback when the device links write lock
  * isn't held. This allows the sync_state() execution flow to use device links
  * APIs.  The caller must ensure this function is called with
- * device_links_write_lock() held.
+ * device_links_write_lock() held.  Note, if the optional queue_sync_state()
+ * callback has been assigned too, it gets called for every update to allowing a

s/allowing/allow/

drivers/base/core.c static void __device_links_queue_sync_state(struct device *dev, · line 1132

Quoted 10 lines
drivers/base/core.c +3 -0
+ * more fine grained support to be implemented on per supplier basis.
  *
  * This function does a get_device() to make sure the device is not freed while
  * on this list.
@@ -1126,6 +1128,9 @@ static void __device_links_queue_sync_state(struct device *dev,
 	if (dev->state_synced)
 		return;
 
+	if (dev->driver && dev->driver->queue_sync_state)
+		dev->driver->queue_sync_state(dev);

This seems to be called without the device lock being held, which seems to allow the queue_sync_state() callback to execute concurrently with remove(). This opens the door for all kinds of UAF conditions in drivers.

This also made me aware that the above dev_has_sync_state() is probably broken, as it also performs the following check without the device lock being held.

	dev->driver && dev->driver->sync_state

I think nothing prevents dev->driver to become NULL concurrently; in this case READ_ONCE() should be sufficient though as it doesn't execute the callback.

I will send a patch for this.

Quoted 9 lines
drivers/base/core.c +1 -0
+
 	list_for_each_entry(link, &dev->links.consumers, s_node) {
 		if (!device_link_test(link, DL_FLAG_MANAGED))
 			continue;
include/linux/device/driver.h +0 -0
index bbc67ec513ed..bc9ae1cbe03c 100644

include/linux/device/driver.h enum probe_type { · line 76

Quoted 9 lines
include/linux/device/driver.h +6 -0
@@ -68,6 +68,12 @@ enum probe_type {
  *		be called at late_initcall_sync level. If the device has
  *		consumers that are never bound to a driver, this function
  *		will never get called until they do.
+ * @queue_sync_state: Similar to the ->sync_state() callback, but called to
+ *		allow syncing device state to software state in a more fine
+ *		grained way. It is called when there is an updated state that
+ *		may be worth to consider for any of the consumers linked to
+ *		this device. If implemented, the ->sync_state() callback is
+ *		required too.

What happens if this is not the case? Maybe worth to check and warn about this in driver_register().

Quoted 13 lines
include/linux/device/driver.h +1 -0
  * @remove:	Called when the device is removed from the system to
  *		unbind a device from this driver.
  * @shutdown:	Called at shut-down time to quiesce the device.
@@ -110,6 +116,7 @@ struct device_driver {
 
 	int (*probe) (struct device *dev);
 	void (*sync_state)(struct device *dev);
+	void (*queue_sync_state)(struct device *dev);
 	int (*remove) (struct device *dev);
 	void (*shutdown) (struct device *dev);
 	int (*suspend) (struct device *dev, pm_message_t state);
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v2 1/9] driver core: Enable suppliers to implement fine grained sync_state support

Quoted 42 lines · Danilo Krummrich
Quoted 23 lines · Ulf Hansson

The common sync_state support isn't fine grained enough for some types of suppliers, like power domains for example. Especially when a supplier provides multiple independent power domains, each with their own set of consumers. In these cases we need to wait for all consumers for all the provided power domains before invoking the supplier's ->sync_state().

To allow a more fine grained sync_state support to be implemented on per supplier's driver basis, let's add a new optional callback. As soon as there is an update worth to consider in regards to managing sync_state for a supplier device, __device_links_queue_sync_state() invokes the callback.

---
 drivers/base/core.c           | 7 ++++++-
 include/linux/device/driver.h | 7 +++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/base/core.c +0 -0
index 09b98f02f559..4085a011d8ca 100644

drivers/base/core.c int device_links_check_suppliers(struct device *dev) · line 1110

Quoted 6 lines
drivers/base/core.c +2 -1
@@ -1106,7 +1106,9 @@ int device_links_check_suppliers(struct device *dev)
  * Queues a device for a sync_state() callback when the device links write lock
  * isn't held. This allows the sync_state() execution flow to use device links
  * APIs.  The caller must ensure this function is called with
- * device_links_write_lock() held.
+ * device_links_write_lock() held.  Note, if the optional queue_sync_state()
+ * callback has been assigned too, it gets called for every update to allowing a

s/allowing/allow/

Quoted 8 lines
drivers/base/core.c +1 -0
+ * more fine grained support to be implemented on per supplier basis.
  *
  * This function does a get_device() to make sure the device is not freed while
  * on this list.
@@ -1126,6 +1128,9 @@ static void __device_links_queue_sync_state(struct device *dev,
      if (dev->state_synced)
              return;
 
Quoted 6 lines

drivers/base/core.c static void __device_links_queue_sync_state(struct device *dev, · line 1132

Quoted 2 lines
drivers/base/core.c +2 -0
+     if (dev->driver && dev->driver->queue_sync_state)
+             dev->driver->queue_sync_state(dev);

This seems to be called without the device lock being held, which seems to allow the queue_sync_state() callback to execute concurrently with remove(). This opens the door for all kinds of UAF conditions in drivers.

If that were the case, this whole function would be unsafe even before this change. I assume this isn't because of how the function is being called, but I may be wrong.

Anyway, let me add a get/put_device() here somewhere, to ensure we prevent this from happening. I assume that is what you are proposing?

Quoted 10 lines

This also made me aware that the above dev_has_sync_state() is probably broken, as it also performs the following check without the device lock being held.

        dev->driver && dev->driver->sync_state

I think nothing prevents dev->driver to become NULL concurrently; in this case READ_ONCE() should be sufficient though as it doesn't execute the callback.

I will send a patch for this.

Okay, thanks!

Quoted 14 lines
Quoted 14 lines
drivers/base/core.c +1 -0
+
      list_for_each_entry(link, &dev->links.consumers, s_node) {
              if (!device_link_test(link, DL_FLAG_MANAGED))
                      continue;
include/linux/device/driver.h +1 -0
index bbc67ec513ed..bc9ae1cbe03c 100644
@@ -68,6 +68,12 @@ enum probe_type {
  *           be called at late_initcall_sync level. If the device has
  *           consumers that are never bound to a driver, this function
  *           will never get called until they do.
+ * @queue_sync_state: Similar to the ->sync_state() callback, but called to
Quoted 8 lines

include/linux/device/driver.h enum probe_type { · line 76

Quoted 5 lines
include/linux/device/driver.h +5 -0
+ *           allow syncing device state to software state in a more fine
+ *           grained way. It is called when there is an updated state that
+ *           may be worth to consider for any of the consumers linked to
+ *           this device. If implemented, the ->sync_state() callback is
+ *           required too.

What happens if this is not the case? Maybe worth to check and warn about this in driver_register().

Good point!

I believe I should also add a check in dev_set_drv_queue_sync_state() that is added in patch2.

Quoted 6 lines
Quoted 6 lines
include/linux/device/driver.h +0 -0
  * @remove:  Called when the device is removed from the system to
  *           unbind a device from this driver.
  * @shutdown:        Called at shut-down time to quiesce the device.
@@ -110,6 +116,7 @@ struct device_driver {
 
Quoted 9 lines
Quoted 8 lines
include/linux/device/driver.h +1 -1
      int (*probe) (struct device *dev);
      void (*sync_state)(struct device *dev);
+     void (*queue_sync_state)(struct device *dev);
      int (*remove) (struct device *dev);
      void (*shutdown) (struct device *dev);
      int (*suspend) (struct device *dev, pm_message_t state);
--

2.43.0

Thanks for reviewing!

Kind regards
Uffe

maintainer
#4 lore raw

Re: [PATCH v2 1/9] driver core: Enable suppliers to implement fine grained sync_state support

Quoted 13 lines · Ulf Hansson
Quoted 11 lines · Danilo Krummrich

drivers/base/core.c static void __device_links_queue_sync_state(struct device *dev, · line 1132

Quoted 6 lines · Ulf Hansson
drivers/base/core.c +2 -0
@@ -1126,6 +1128,9 @@ static void __device_links_queue_sync_state(struct device *dev,
      if (dev->state_synced)
              return;
 
+     if (dev->driver && dev->driver->queue_sync_state)
+             dev->driver->queue_sync_state(dev);

This seems to be called without the device lock being held, which seems to allow the queue_sync_state() callback to execute concurrently with remove(). This opens the door for all kinds of UAF conditions in drivers.

Quoted 3 lines

If that were the case, this whole function would be unsafe even before this change. I assume this isn't because of how the function is being called, but I may be wrong.

This function does not issue any driver callbacks intentionally; the existing sync_state() callback is deferred to device_links_flush_sync_list(), which is called without the device_links_write_lock() held, but takes the device_lock() to protect against other concurrent driver callbacks, such as remove().

I.e. we can't take the device_lock() when the device_links_write_lock() is held, as it would be prone to lock inversion.

The documentation of __device_links_queue_sync_state() actually slightly hints at this, but focuses more on the other reason for the deferred semantics -- the sync_state() callback may want to call device link APIs.

Quoted 2 lines

Anyway, let me add a get/put_device() here somewhere, to ensure we prevent this from happening. I assume that is what you are proposing?

No, an additional device reference count won't protect against other concurrent driver callbacks, such as remove().

#5 lore raw

Re: [PATCH v2 1/9] driver core: Enable suppliers to implement fine grained sync_state support

Quoted 19 lines · Danilo Krummrich
Quoted 13 lines · Ulf Hansson
Quoted 11 lines · Danilo Krummrich

drivers/base/core.c static void __device_links_queue_sync_state(struct device *dev, · line 1132

Quoted 6 lines · Ulf Hansson
drivers/base/core.c +2 -0
@@ -1126,6 +1128,9 @@ static void __device_links_queue_sync_state(struct device *dev,
      if (dev->state_synced)
              return;
 
+     if (dev->driver && dev->driver->queue_sync_state)
+             dev->driver->queue_sync_state(dev);

This seems to be called without the device lock being held, which seems to allow the queue_sync_state() callback to execute concurrently with remove(). This opens the door for all kinds of UAF conditions in drivers.

Quoted 3 lines

If that were the case, this whole function would be unsafe even before this change. I assume this isn't because of how the function is being called, but I may be wrong.

Quoted 7 lines

This function does not issue any driver callbacks intentionally; the existing sync_state() callback is deferred to device_links_flush_sync_list(), which is called without the device_links_write_lock() held, but takes the device_lock() to protect against other concurrent driver callbacks, such as remove().

I.e. we can't take the device_lock() when the device_links_write_lock() is held, as it would be prone to lock inversion.

You are right, the device_lock() is needed in device_links_flush_sync_list() to protect against concurrent driver callbacks from being invoked, such as the ->remove().

However, as part of the removal procedure in __device_release_driver(), we also need to account for device links. Hence we call device_links_busy() which takes the device_links_write_lock().

Quoted 10 lines

The documentation of __device_links_queue_sync_state() actually slightly hints at this, but focuses more on the other reason for the deferred semantics -- the sync_state() callback may want to call device link APIs.

Quoted 2 lines

Anyway, let me add a get/put_device() here somewhere, to ensure we prevent this from happening. I assume that is what you are proposing?

No, an additional device reference count won't protect against other concurrent driver callbacks, such as remove().

Because _device_links_queue_sync_state() is always called while holding the device_links_write_lock(), it I believe it should be perfectly safe to invoke the driver callbacks from there.

Kind regards
Uffe

maintainer
#6 lore raw

Re: [PATCH v2 1/9] driver core: Enable suppliers to implement fine grained sync_state support

Quoted 28 lines · Ulf Hansson
Quoted 19 lines · Danilo Krummrich
Quoted 13 lines · Ulf Hansson
Quoted 11 lines · Danilo Krummrich

drivers/base/core.c static void __device_links_queue_sync_state(struct device *dev, · line 1132

Quoted 6 lines · Ulf Hansson
drivers/base/core.c +2 -0
@@ -1126,6 +1128,9 @@ static void __device_links_queue_sync_state(struct device *dev,
      if (dev->state_synced)
              return;
 
+     if (dev->driver && dev->driver->queue_sync_state)
+             dev->driver->queue_sync_state(dev);

This seems to be called without the device lock being held, which seems to allow the queue_sync_state() callback to execute concurrently with remove(). This opens the door for all kinds of UAF conditions in drivers.

Quoted 3 lines

If that were the case, this whole function would be unsafe even before this change. I assume this isn't because of how the function is being called, but I may be wrong.

Quoted 7 lines

This function does not issue any driver callbacks intentionally; the existing sync_state() callback is deferred to device_links_flush_sync_list(), which is called without the device_links_write_lock() held, but takes the device_lock() to protect against other concurrent driver callbacks, such as remove().

I.e. we can't take the device_lock() when the device_links_write_lock() is held, as it would be prone to lock inversion.

Quoted 8 lines

You are right, the device_lock() is needed in device_links_flush_sync_list() to protect against concurrent driver callbacks from being invoked, such as the ->remove().

However, as part of the removal procedure in __device_release_driver(), we also need to account for device links. Hence we call device_links_busy() which takes the device_links_write_lock().

I don't think this is sufficient.

I wrapped my head around this about three weeks ago and I think there were multiple cases where this falls apart. I think one case was:

1. Consumer binds, supplier is added to deferred_sync. Consumer unbinds, link goes to AVAILABLE.

2. device_links_busy(supplier) takes and releases device_links_write_lock(),

     finds no ACTIVE consumers, returns false -- drv->remove() starts without
     device_links_write_lock() held.

3. device_links_supplier_sync_state_resume() takes device_links_write_lock(), finds supplier still on deferred_sync -- device_links_driver_cleanup() hasn't run yet, blocked on the lock. queue_sync_state() is called concurrently with drv->remove().

But again, I think that's what I concluded three weeks ago and this state machine is rather tricky.

That said, *even if* we could prove that this works in all cases, it would be very subtle, pretty fragile and not by design.

The whole state machine around sync state is already rather complicated. So, if we really want to make it an invariant that it is valid to call bus device callbacks without holding the device lock from __device_links_queue_sync_state(), I think this has to be carefully reflected by the overall design making it *very explicit* how this invariant holds up in all cases.

Otherwise, I'd be rather concerned that this becomes a source of subtle bugs.

#7 lore raw

Re: [PATCH v2 1/9] driver core: Enable suppliers to implement fine grained sync_state support

Quoted 47 lines · Danilo Krummrich
Quoted 28 lines · Ulf Hansson
Quoted 19 lines · Danilo Krummrich
Quoted 13 lines · Ulf Hansson
Quoted 11 lines · Danilo Krummrich

drivers/base/core.c static void __device_links_queue_sync_state(struct device *dev, · line 1132

Quoted 6 lines · Ulf Hansson
drivers/base/core.c +2 -0
@@ -1126,6 +1128,9 @@ static void __device_links_queue_sync_state(struct device *dev,
      if (dev->state_synced)
              return;
 
+     if (dev->driver && dev->driver->queue_sync_state)
+             dev->driver->queue_sync_state(dev);

This seems to be called without the device lock being held, which seems to allow the queue_sync_state() callback to execute concurrently with remove(). This opens the door for all kinds of UAF conditions in drivers.

Quoted 3 lines

If that were the case, this whole function would be unsafe even before this change. I assume this isn't because of how the function is being called, but I may be wrong.

Quoted 7 lines

This function does not issue any driver callbacks intentionally; the existing sync_state() callback is deferred to device_links_flush_sync_list(), which is called without the device_links_write_lock() held, but takes the device_lock() to protect against other concurrent driver callbacks, such as remove().

I.e. we can't take the device_lock() when the device_links_write_lock() is held, as it would be prone to lock inversion.

Quoted 8 lines

You are right, the device_lock() is needed in device_links_flush_sync_list() to protect against concurrent driver callbacks from being invoked, such as the ->remove().

However, as part of the removal procedure in __device_release_driver(), we also need to account for device links. Hence we call device_links_busy() which takes the device_links_write_lock().

I don't think this is sufficient.

I wrapped my head around this about three weeks ago and I think there were multiple cases where this falls apart. I think one case was:

1. Consumer binds, supplier is added to deferred_sync. Consumer unbinds, link goes to AVAILABLE.

Quoted 8 lines

2. device_links_busy(supplier) takes and releases device_links_write_lock(),

     finds no ACTIVE consumers, returns false -- drv->remove() starts without
     device_links_write_lock() held.

3. device_links_supplier_sync_state_resume() takes device_links_write_lock(), finds supplier still on deferred_sync -- device_links_driver_cleanup() hasn't run yet, blocked on the lock. queue_sync_state() is called concurrently with drv->remove().

Thanks for pointing this out! More problems seem to exist regarding the above.

What if drv->remove(supplier) completes before device_links_supplier_sync_state_resume() is called and the removed device is still in the deferred_sync list? Probably another get|put_device() is needed to protect us from UAF.

Quoted 3 lines

But again, I think that's what I concluded three weeks ago and this state machine is rather tricky.

Indeed it is.

Quoted 10 lines

That said, *even if* we could prove that this works in all cases, it would be very subtle, pretty fragile and not by design.

The whole state machine around sync state is already rather complicated. So, if we really want to make it an invariant that it is valid to call bus device callbacks without holding the device lock from __device_links_queue_sync_state(), I think this has to be carefully reflected by the overall design making it *very explicit* how this invariant holds up in all cases.

Right.

I experimented with adding the devices for ->queue_sync_state() into a separate list and then flushing that list similarly to how the sync_state list is flushed. It becomes messy, but I guess there is no other way.

Perhaps Saravana has another suggestion for how to implement this?

Quoted 2 lines

Otherwise, I'd be rather concerned that this becomes a source of subtle bugs.

Kind regards
Uffe

#8 lore raw

PATCH v2 2/9 driver core: Add dev_set_drv_queue_sync_state()

In reply to Ulf Hansson

Similar to the dev_set_drv_sync_state() helper, let's add another one to allow subsystem level code to set the ->queue_sync_state() callback for a driver that has not already set it.

---
 include/linux/device.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/device.h +12 -0
index e65d564f01cd..f812e70bdf22 100644
@@ -994,6 +994,18 @@ static inline int dev_set_drv_sync_state(struct device *dev,
 	return 0;
 }
 
+static inline int dev_set_drv_queue_sync_state(struct device *dev,
+					       void (*fn)(struct device *dev))
+{
+	if (!dev || !dev->driver)
+		return 0;
+	if (dev->driver->queue_sync_state && dev->driver->queue_sync_state != fn)
+		return -EBUSY;
+	if (!dev->driver->queue_sync_state)
+		dev->driver->queue_sync_state = fn;
+	return 0;
+}
+
 static inline void dev_set_removable(struct device *dev,
 				     enum device_removable removable)
 {
Signature · 1 line
2.43.0
maintainer
#9 lore raw

Re: [PATCH v2 2/9] driver core: Add dev_set_drv_queue_sync_state()

include/linux/device.h static inline int dev_set_drv_sync_state(struct device *dev, · line 998

Quoted 10 lines · Ulf Hansson
include/linux/device.h +2 -0
index e65d564f01cd..f812e70bdf22 100644
@@ -994,6 +994,18 @@ static inline int dev_set_drv_sync_state(struct device *dev,
 	return 0;
 }
 
+static inline int dev_set_drv_queue_sync_state(struct device *dev,
+					       void (*fn)(struct device *dev))

As this is a public function, please add some documentation.

include/linux/device.h static inline int dev_set_drv_sync_state(struct device *dev, · line 1005

Quoted 7 lines
include/linux/device.h +7 -0
+{
+	if (!dev || !dev->driver)
+		return 0;
+	if (dev->driver->queue_sync_state && dev->driver->queue_sync_state != fn)
+		return -EBUSY;
+	if (!dev->driver->queue_sync_state)
+		dev->driver->queue_sync_state = fn;

I think this follows dev_set_drv_sync_state(), but I think it is worth pointing out that it is yet another blocker for moving towards
const struct device_driver.

Quoted 8 lines
include/linux/device.h +3 -0
+	return 0;
+}
+
 static inline void dev_set_removable(struct device *dev,
 				     enum device_removable removable)
 {
Signature · 1 line
2.43.0
#10 lore raw

Re: [PATCH v2 2/9] driver core: Add dev_set_drv_queue_sync_state()

Quoted 10 lines · Danilo Krummrich
Quoted 8 lines · Ulf Hansson
include/linux/device.h +0 -0
index e65d564f01cd..f812e70bdf22 100644
@@ -994,6 +994,18 @@ static inline int dev_set_drv_sync_state(struct device *dev,
      return 0;
 }
 
Quoted 4 lines

include/linux/device.h static inline int dev_set_drv_sync_state(struct device *dev, · line 998

Quoted 2 lines
include/linux/device.h +2 -0
+static inline int dev_set_drv_queue_sync_state(struct device *dev,
+                                            void (*fn)(struct device *dev))

As this is a public function, please add some documentation.

Most of the static inline functions in device.h lacks documentation. Are you suggesting that we should move towards documenting all of them?

In this case, dev_set_drv_sync_state() also lacks documentation and to me, it doesn't make sense to add documentation only for dev_set_drv_queue_sync_state(). Do you want me to add it for both?

[...]

Kind regards
Uffe

maintainer
#11 lore raw

Re: [PATCH v2 2/9] driver core: Add dev_set_drv_queue_sync_state()

Quoted 3 lines · Ulf Hansson

Most of the static inline functions in device.h lacks documentation. Are you suggesting that we should move towards documenting all of them?

I'd prefer that, yes. After all, if they are defined in device.h, they are public APIs, so they should ideally have documentation.

(If a function is only used throughout the driver core, it should be in drivers/base/base.h instead.)

#12 lore raw

PATCH v2 3/9 pmdomain: core: Move genpd_get_from_provider()

In reply to Ulf Hansson

To prepare for subsequent changes and to avoid an unnecessary function declaration, let's move genpd_get_from_provider() a bit earlier in the code.

---
 drivers/pmdomain/core.c | 70 ++++++++++++++++++++---------------------
 1 file changed, 35 insertions(+), 35 deletions(-)
drivers/pmdomain/core.c +35 -35
index 4d32fc676aaf..ad57846f02a3 100644
@@ -2664,6 +2664,41 @@ static bool genpd_present(const struct generic_pm_domain *genpd)
 	return ret;
 }
 
+/**
+ * genpd_get_from_provider() - Look-up PM domain
+ * @genpdspec: OF phandle args to use for look-up
+ *
+ * Looks for a PM domain provider under the node specified by @genpdspec and if
+ * found, uses xlate function of the provider to map phandle args to a PM
+ * domain.
+ *
+ * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
+ * on failure.
+ */
+static struct generic_pm_domain *genpd_get_from_provider(
+					const struct of_phandle_args *genpdspec)
+{
+	struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
+	struct of_genpd_provider *provider;
+
+	if (!genpdspec)
+		return ERR_PTR(-EINVAL);
+
+	mutex_lock(&of_genpd_mutex);
+
+	/* Check if we have such a provider in our array */
+	list_for_each_entry(provider, &of_genpd_providers, link) {
+		if (provider->node == genpdspec->np)
+			genpd = provider->xlate(genpdspec, provider->data);
+		if (!IS_ERR(genpd))
+			break;
+	}
+
+	mutex_unlock(&of_genpd_mutex);
+
+	return genpd;
+}
+
 static void genpd_sync_state(struct device *dev)
 {
 	return of_genpd_sync_state(dev->of_node);
@@ -2889,41 +2924,6 @@ void of_genpd_del_provider(struct device_node *np)
 }
 EXPORT_SYMBOL_GPL(of_genpd_del_provider);
 
-/**
- * genpd_get_from_provider() - Look-up PM domain
- * @genpdspec: OF phandle args to use for look-up
- *
- * Looks for a PM domain provider under the node specified by @genpdspec and if
- * found, uses xlate function of the provider to map phandle args to a PM
- * domain.
- *
- * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
- * on failure.
- */
-static struct generic_pm_domain *genpd_get_from_provider(
-					const struct of_phandle_args *genpdspec)
-{
-	struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
-	struct of_genpd_provider *provider;
-
-	if (!genpdspec)
-		return ERR_PTR(-EINVAL);
-
-	mutex_lock(&of_genpd_mutex);
-
-	/* Check if we have such a provider in our array */
-	list_for_each_entry(provider, &of_genpd_providers, link) {
-		if (provider->node == genpdspec->np)
-			genpd = provider->xlate(genpdspec, provider->data);
-		if (!IS_ERR(genpd))
-			break;
-	}
-
-	mutex_unlock(&of_genpd_mutex);
-
-	return genpd;
-}
-
 /**
  * of_genpd_add_device() - Add a device to an I/O PM domain
  * @genpdspec: OF phandle args to use for look-up PM domain
Signature · 1 line
2.43.0
#13 lore raw

PATCH v2 4/9 pmdomain: core: Add initial fine grained sync_state support

In reply to Ulf Hansson

A onecell (#power-domain-cells = <1 or 2>; in DT) power domain provider typically provides multiple independent power domains, each with their own corresponding consumers. In these cases we have to wait for all consumers for all the provided power domains before the ->sync_state() callback gets called for the supplier.

In a first step to improve this, let's implement support for fine grained sync_state support a per genpd basis by using the ->queue_sync_state() callback. To take step by step, let's initially limit the improvement to the internal genpd provider driver and to its corresponding genpd devices for onecell providers.

---
 drivers/pmdomain/core.c   | 125 ++++++++++++++++++++++++++++++++++++++
 include/linux/pm_domain.h |   1 +
 2 files changed, 126 insertions(+)
drivers/pmdomain/core.c +125 -0
index ad57846f02a3..783d6f981708 100644
@@ -2699,6 +2699,120 @@ static struct generic_pm_domain *genpd_get_from_provider(
 	return genpd;
 }
 
+static bool genpd_should_wait_for_consumer(struct device_node *np)
+{
+	struct generic_pm_domain *genpd;
+	bool should_wait = false;
+
+	mutex_lock(&gpd_list_lock);
+	list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
+		if (genpd->provider == of_fwnode_handle(np)) {
+			genpd_lock(genpd);
+
+			/* Clear the previous state before reevaluating. */
+			genpd->wait_for_consumer = false;
+
+			/*
+			 * Unless there is at least one genpd for the provider
+			 * that is being kept powered-on, we don't have to care
+			 * about waiting for consumers.
+			 */
+			if (genpd->stay_on)
+				should_wait = true;
+
+			genpd_unlock(genpd);
+		}
+	}
+	mutex_unlock(&gpd_list_lock);
+
+	return should_wait;
+}
+
+static void genpd_parse_for_consumer(struct device_node *sup,
+				     struct device_node *con)
+{
+	struct generic_pm_domain *genpd;
+	int i;
+
+	for (i = 0; ; i++) {
+		struct of_phandle_args pd_args;
+
+		if (of_parse_phandle_with_args(con, "power-domains",
+					       "#power-domain-cells",
+					        i, &pd_args))
+			break;
+
+		/*
+		 * The phandle must correspond to the supplier's genpd provider
+		 * to be relevant else let's move to the next index.
+		 */
+		if (sup != pd_args.np) {
+			of_node_put(pd_args.np);
+			continue;
+		}
+
+		mutex_lock(&gpd_list_lock);
+		genpd = genpd_get_from_provider(&pd_args);
+		if (!IS_ERR(genpd)) {
+			genpd_lock(genpd);
+			genpd->wait_for_consumer = true;
+			genpd_unlock(genpd);
+		}
+		mutex_unlock(&gpd_list_lock);
+
+		of_node_put(pd_args.np);
+	}
+}
+
+static void _genpd_queue_sync_state(struct device_node *np)
+{
+	struct generic_pm_domain *genpd;
+
+	mutex_lock(&gpd_list_lock);
+	list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
+		if (genpd->provider == of_fwnode_handle(np)) {
+			genpd_lock(genpd);
+			if (genpd->stay_on && !genpd->wait_for_consumer) {
+				genpd->stay_on = false;
+				genpd_queue_power_off_work(genpd);
+			}
+			genpd_unlock(genpd);
+		}
+	}
+	mutex_unlock(&gpd_list_lock);
+}
+
+static void genpd_queue_sync_state(struct device *dev)
+{
+	struct device_node *np = dev->of_node;
+	struct device_link *link;
+
+	if (!genpd_should_wait_for_consumer(np))
+		return;
+
+	list_for_each_entry(link, &dev->links.consumers, s_node) {
+		struct device *consumer = link->consumer;
+
+		if (!device_link_test(link, DL_FLAG_MANAGED))
+			continue;
+
+		if (link->status == DL_STATE_ACTIVE)
+			continue;
+
+		if (!consumer->of_node)
+			continue;
+
+		/*
+		 * A consumer device has not been probed yet. Let's parse its
+		 * device node for the power-domains property, to find out the
+		 * genpds it may belong to and then prevent sync state for them.
+		 */
+		genpd_parse_for_consumer(np, consumer->of_node);
+	}
+
+	_genpd_queue_sync_state(np);
+}
+
 static void genpd_sync_state(struct device *dev)
 {
 	return of_genpd_sync_state(dev->of_node);
@@ -3531,6 +3645,16 @@ static int genpd_provider_probe(struct device *dev)
 	return 0;
 }
 
+static void genpd_provider_queue_sync_state(struct device *dev)
+{
+	struct generic_pm_domain *genpd = container_of(dev, struct generic_pm_domain, dev);
+
+	if (genpd->sync_state != GENPD_SYNC_STATE_ONECELL)
+		return;
+
+	genpd_queue_sync_state(dev);
+}
+
 static void genpd_provider_sync_state(struct device *dev)
 {
 	struct generic_pm_domain *genpd = container_of(dev, struct generic_pm_domain, dev);
@@ -3559,6 +3683,7 @@ static struct device_driver genpd_provider_drv = {
 	.name = "genpd_provider",
 	.bus = &genpd_provider_bus_type,
 	.probe = genpd_provider_probe,
+	.queue_sync_state = genpd_provider_queue_sync_state,
 	.sync_state = genpd_provider_sync_state,
 	.suppress_bind_attrs = true,
 };
include/linux/pm_domain.h +1 -0
index b299dc0128d6..7aa49721cde5 100644
@@ -215,6 +215,7 @@ struct generic_pm_domain {
 	cpumask_var_t cpus;		/* A cpumask of the attached CPUs */
 	bool synced_poweroff;		/* A consumer needs a synced poweroff */
 	bool stay_on;			/* Stay powered-on during boot. */
+	bool wait_for_consumer;		/* Consumers awaits to be probed. */
 	enum genpd_sync_state sync_state; /* How sync_state is managed. */
 	int (*power_off)(struct generic_pm_domain *domain);
 	int (*power_on)(struct generic_pm_domain *domain);
Signature · 1 line
2.43.0
#14 lore raw

Re: [PATCH v2 4/9] pmdomain: core: Add initial fine grained sync_state support

Hi Ulf,

Quoted 6 lines · Ulf Hansson

A onecell (#power-domain-cells = <1 or 2>; in DT) power domain provider typically provides multiple independent power domains, each with their own corresponding consumers. In these cases we have to wait for all consumers for all the provided power domains before the ->sync_state() callback gets called for the supplier.

Quoted 8 lines

In a first step to improve this, let's implement support for fine grained sync_state support a per genpd basis by using the ->queue_sync_state() callback. To take step by step, let's initially limit the improvement to the internal genpd provider driver and to its corresponding genpd devices for onecell providers.

Thanks for your patch!

drivers/pmdomain/core.c static struct generic_pm_domain *genpd_get_from_provider( · line 2735

Quoted 8 lines
drivers/pmdomain/core.c +5 -0
 
+static void genpd_parse_for_consumer(struct device_node *sup,
+                                    struct device_node *con)
+{
+       struct generic_pm_domain *genpd;
+       int i;

unsigned int?

drivers/pmdomain/core.c

Quoted 2 lines
drivers/pmdomain/core.c +2 -0
+
+       for (i = 0; ; i++) {

... and you could move it inside the for-statement.

drivers/pmdomain/core.c static struct generic_pm_domain *genpd_get_from_provider( · line 2742

Quoted 5 lines
drivers/pmdomain/core.c +5 -0
+               struct of_phandle_args pd_args;
+
+               if (of_parse_phandle_with_args(con, "power-domains",
+                                              "#power-domain-cells",
+                                               i, &pd_args))

Checkpatch reports a superfluous space before the "i".

Quoted 1 line

+ break;

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#15 lore raw

PATCH v2 5/9 pmdomain: core: Extend fine grained sync_state to more onecell providers

In reply to Ulf Hansson

A onecell power domain provider driver that we can assign a common ->sync_state() callback for, should be able to benefit from the improved fine grained sync_state support in genpd. Therefore, let's also assign the ->queue_sync_state() callback for these types of provider drivers.

---
 drivers/pmdomain/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pmdomain/core.c +4 -2
index 783d6f981708..f11dc2110737 100644
@@ -2918,10 +2918,12 @@ int of_genpd_add_provider_onecell(struct device_node *np,
 
 	fwnode = of_fwnode_handle(np);
 	dev = get_dev_from_fwnode(fwnode);
-	if (!dev)
+	if (!dev) {
 		sync_state = true;
-	else
+	} else if (!dev_has_sync_state(dev)) {
 		dev_set_drv_sync_state(dev, genpd_sync_state);
+		dev_set_drv_queue_sync_state(dev, genpd_queue_sync_state);
+	}
 
 	put_device(dev);
 
Signature · 1 line
2.43.0
#16 lore raw

PATCH v2 6/9 pmdomain: core: Export a common function for ->queue_sync_state()

In reply to Ulf Hansson

Along with of_genpd_sync_state() that genpd provider drivers may use to manage sync_state, let's add and export of_genpd_queue_sync_state() for those that may need it. It's expected that the genpd provider driver assigns it's own ->queue_sync_state() callback and invoke the new helper from there.

---
 drivers/pmdomain/core.c   | 14 +++++++++-----
 include/linux/pm_domain.h |  2 ++
 2 files changed, 11 insertions(+), 5 deletions(-)
drivers/pmdomain/core.c +9 -5
index f11dc2110737..49e60cb67b3e 100644
@@ -2764,7 +2764,7 @@ static void genpd_parse_for_consumer(struct device_node *sup,
 	}
 }
 
-static void _genpd_queue_sync_state(struct device_node *np)
+static void genpd_queue_sync_state(struct device_node *np)
 {
 	struct generic_pm_domain *genpd;
 
@@ -2782,11 +2782,14 @@ static void _genpd_queue_sync_state(struct device_node *np)
 	mutex_unlock(&gpd_list_lock);
 }
 
-static void genpd_queue_sync_state(struct device *dev)
+void of_genpd_queue_sync_state(struct device *dev)
 {
 	struct device_node *np = dev->of_node;
 	struct device_link *link;
 
+	if (!np)
+		return;
+
 	if (!genpd_should_wait_for_consumer(np))
 		return;
 
@@ -2810,8 +2813,9 @@ static void genpd_queue_sync_state(struct device *dev)
 		genpd_parse_for_consumer(np, consumer->of_node);
 	}
 
-	_genpd_queue_sync_state(np);
+	genpd_queue_sync_state(np);
 }
+EXPORT_SYMBOL_GPL(of_genpd_queue_sync_state);
 
 static void genpd_sync_state(struct device *dev)
 {
@@ -2922,7 +2926,7 @@ int of_genpd_add_provider_onecell(struct device_node *np,
 		sync_state = true;
 	} else if (!dev_has_sync_state(dev)) {
 		dev_set_drv_sync_state(dev, genpd_sync_state);
-		dev_set_drv_queue_sync_state(dev, genpd_queue_sync_state);
+		dev_set_drv_queue_sync_state(dev, of_genpd_queue_sync_state);
 	}
 
 	put_device(dev);
@@ -3654,7 +3658,7 @@ static void genpd_provider_queue_sync_state(struct device *dev)
 	if (genpd->sync_state != GENPD_SYNC_STATE_ONECELL)
 		return;
 
-	genpd_queue_sync_state(dev);
+	of_genpd_queue_sync_state(dev);
 }
 
 static void genpd_provider_sync_state(struct device *dev)
include/linux/pm_domain.h +2 -0
index 7aa49721cde5..d428dd805c46 100644
@@ -467,6 +467,7 @@ int of_genpd_remove_subdomain(const struct of_phandle_args *parent_spec,
 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np);
 int of_genpd_parse_idle_states(struct device_node *dn,
 			       struct genpd_power_state **states, int *n);
+void of_genpd_queue_sync_state(struct device *dev);
 void of_genpd_sync_state(struct device_node *np);
 
 int genpd_dev_pm_attach(struct device *dev);
@@ -513,6 +514,7 @@ static inline int of_genpd_parse_idle_states(struct device_node *dn,
 	return -ENODEV;
 }
 
+static inline void of_genpd_queue_sync_state(struct device *dev) {}
 static inline void of_genpd_sync_state(struct device_node *np) {}
 
 static inline int genpd_dev_pm_attach(struct device *dev)
Signature · 1 line
2.43.0
#17 lore raw

PATCH v2 7/9 pmdomain: renesas: rcar-gen4-sysc: Drop GENPD_FLAG_NO_STAY_ON

In reply to Ulf Hansson

Due to the new fine grained sync_state support for onecell genpd provider drivers, we should no longer need use the legacy behaviour. Therefore, let's drop GENPD_FLAG_NO_STAY_ON.

---
 drivers/pmdomain/renesas/rcar-gen4-sysc.c | 1 -
 1 file changed, 1 deletion(-)
drivers/pmdomain/renesas/rcar-gen4-sysc.c +0 -1
index 0c6c639a91d0..81b154da725f 100644
@@ -251,7 +251,6 @@ static int __init rcar_gen4_sysc_pd_setup(struct rcar_gen4_sysc_pd *pd)
 		genpd->detach_dev = cpg_mssr_detach_dev;
 	}
 
-	genpd->flags |= GENPD_FLAG_NO_STAY_ON;
 	genpd->power_off = rcar_gen4_sysc_pd_power_off;
 	genpd->power_on = rcar_gen4_sysc_pd_power_on;
 
Signature · 1 line
2.43.0
#18 lore raw

PATCH v2 8/9 pmdomain: renesas: rcar-sysc: Drop GENPD_FLAG_NO_STAY_ON

In reply to Ulf Hansson

Due to the new fine grained sync_state support for onecell genpd provider drivers, we should no longer need use the legacy behaviour. Therefore, let's drop GENPD_FLAG_NO_STAY_ON.

---
 drivers/pmdomain/renesas/rcar-sysc.c | 1 -
 1 file changed, 1 deletion(-)
drivers/pmdomain/renesas/rcar-sysc.c +0 -1
index bd7bb9cbd9da..e4608c657629 100644
@@ -241,7 +241,6 @@ static int __init rcar_sysc_pd_setup(struct rcar_sysc_pd *pd)
 		}
 	}
 
-	genpd->flags |= GENPD_FLAG_NO_STAY_ON;
 	genpd->power_off = rcar_sysc_pd_power_off;
 	genpd->power_on = rcar_sysc_pd_power_on;
 
Signature · 1 line
2.43.0
#19 lore raw

PATCH v2 9/9 pmdomain: renesas: rmobile-sysc: Drop GENPD_FLAG_NO_STAY_ON

In reply to Ulf Hansson

Rmobile-sysc is not a onecell provider and didn't really needed the GENPD_FLAG_NO_STAY_ON flag in the first place. Let's drop it.

---
 drivers/pmdomain/renesas/rmobile-sysc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/pmdomain/renesas/rmobile-sysc.c +1 -2
index 93103ff33d6e..e36f5d763c91 100644
@@ -100,8 +100,7 @@ static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd)
 	struct generic_pm_domain *genpd = &rmobile_pd->genpd;
 	struct dev_power_governor *gov = rmobile_pd->gov;
 
-	genpd->flags |= GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP |
-		GENPD_FLAG_NO_STAY_ON;
+	genpd->flags |= GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP;
 	genpd->attach_dev = cpg_mstp_attach_dev;
 	genpd->detach_dev = cpg_mstp_detach_dev;
 
Signature · 1 line
2.43.0
#20 lore raw
In reply to Ulf Hansson

Hi Ulf,

Quoted 14 lines · Ulf Hansson

Since the introduction [1] of the common sync_state support for pmdomains (genpd), we have encountered a lot of various interesting problems. In most cases the new behaviour of genpd triggered some weird platform specific bugs.

That said, in LPC in Tokyo me and Saravana hosted a session to walk through the remaining limitations that we have found for genpd's sync state support. In particular, we discussed the problems we have for the so-called onecell power domain providers, where a single provider typically provides multiple independent power domains, all with their own set of consumers.

Note that, onecell power domain providers are very common. It's being used by many SoCs/platforms/technologies. To name a few:
SCMI, Qualcomm, NXP, Mediatek, Renesas, TI, etc.

Quoted 8 lines

Anyway, in these cases, the generic sync_state mechanism with fw_devlink isn't fine grained enough, as we end up waiting for all consumers for all power domains before the ->sync_callback gets called for the supplier/provider. In other words, we may end up keeping unused power domains powered-on, for no good reasons.

The series intends to fix this problem. Please have a look at the commit messages for more details and help review/test!

Thanks for the update!

At first glance, the only real change compared to v1 seems to be the removal of printing

    pr_info("%s:%s con=%s\n", __func__, dev_name(dev),
            dev_name(consumer));

Right?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#21 lore raw
Quoted 29 lines · Geert Uytterhoeven

Hi Ulf,

Quoted 14 lines · Ulf Hansson

Since the introduction [1] of the common sync_state support for pmdomains (genpd), we have encountered a lot of various interesting problems. In most cases the new behaviour of genpd triggered some weird platform specific bugs.

That said, in LPC in Tokyo me and Saravana hosted a session to walk through the remaining limitations that we have found for genpd's sync state support. In particular, we discussed the problems we have for the so-called onecell power domain providers, where a single provider typically provides multiple independent power domains, all with their own set of consumers.

Note that, onecell power domain providers are very common. It's being used by many SoCs/platforms/technologies. To name a few:
SCMI, Qualcomm, NXP, Mediatek, Renesas, TI, etc.

Quoted 8 lines

Anyway, in these cases, the generic sync_state mechanism with fw_devlink isn't fine grained enough, as we end up waiting for all consumers for all power domains before the ->sync_callback gets called for the supplier/provider. In other words, we may end up keeping unused power domains powered-on, for no good reasons.

The series intends to fix this problem. Please have a look at the commit messages for more details and help review/test!

Thanks for the update!

Quoted 7 lines

At first glance, the only real change compared to v1 seems to be the removal of printing

    pr_info("%s:%s con=%s\n", __func__, dev_name(dev),
            dev_name(consumer));

Right?

Correct! I forgot to include a version history, sorry.

Besides the removed print, I have just added your tags and updated the commit message in patch9.

FYI, my plan is to queue this as soon as v7.1-rc1 is available.

Kind regards
Uffe

#22 lore raw
In reply to Ulf Hansson

+ Danilo (for the driver core changes)

Quoted 56 lines · Ulf Hansson

Since the introduction [1] of the common sync_state support for pmdomains (genpd), we have encountered a lot of various interesting problems. In most cases the new behaviour of genpd triggered some weird platform specific bugs.

That said, in LPC in Tokyo me and Saravana hosted a session to walk through the remaining limitations that we have found for genpd's sync state support. In particular, we discussed the problems we have for the so-called onecell power domain providers, where a single provider typically provides multiple independent power domains, all with their own set of consumers.

Note that, onecell power domain providers are very common. It's being used by many SoCs/platforms/technologies. To name a few:
SCMI, Qualcomm, NXP, Mediatek, Renesas, TI, etc.

Anyway, in these cases, the generic sync_state mechanism with fw_devlink isn't fine grained enough, as we end up waiting for all consumers for all power domains before the ->sync_callback gets called for the supplier/provider. In other words, we may end up keeping unused power domains powered-on, for no good reasons.

The series intends to fix this problem. Please have a look at the commit messages for more details and help review/test!

Kind regards
Ulf Hansson

[1]
https://lore.kernel.org/all/[email protected]/

Ulf Hansson (9):
driver core: Enable suppliers to implement fine grained sync_state
support
driver core: Add dev_set_drv_queue_sync_state()
pmdomain: core: Move genpd_get_from_provider()
pmdomain: core: Add initial fine grained sync_state support
pmdomain: core: Extend fine grained sync_state to more onecell
providers
pmdomain: core: Export a common function for ->queue_sync_state()
pmdomain: renesas: rcar-gen4-sysc: Drop GENPD_FLAG_NO_STAY_ON
pmdomain: renesas: rcar-sysc: Drop GENPD_FLAG_NO_STAY_ON
pmdomain: renesas: rmobile-sysc: Drop GENPD_FLAG_NO_STAY_ON

 drivers/base/core.c                       |   7 +-
 drivers/pmdomain/core.c                   | 205 ++++++++++++++++++----
 drivers/pmdomain/renesas/rcar-gen4-sysc.c |   1 -
 drivers/pmdomain/renesas/rcar-sysc.c      |   1 -
 drivers/pmdomain/renesas/rmobile-sysc.c   |   3 +-
 include/linux/device.h                    |  12 ++
 include/linux/device/driver.h             |   7 +
 include/linux/pm_domain.h                 |   3 +
 8 files changed, 197 insertions(+), 42 deletions(-)

--
2.43.0