Skip to contentSkip to conversation

dm: core: device: switch off power domain after device removal

PATCH Superseded Anatolij Gustschin 4 participants 11 messages Jul 14, 2019 – Jul 31, 2019

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 dm: core: device: switch off power domain after device removal

The power domain associated with a device is enabled when probing, but currently the domain remains enabled when the device is removed. Some boards started to disable power domains for selected devices via custom board_quiesce_devices(), but it doesn't work in many cases, i. e. because devices still can be accessed later in .remove() callback on behalf of dm_remove_devices_flags().

Utilize the DM core to power off the device power domain, but add a device flag to be able to selectively let the power domain enabled after device removal. This might be required for devices that must remain enabled when booting OS, i. e. serial console for debug output, etc.

---
Changes in v2:
- use CONFIG_IS_ENABLED(POWER_DOMAIN) to reduce code size

 drivers/core/device-remove.c | 9 +++++++++
 include/dm/device.h          | 6 ++++++
 2 files changed, 15 insertions(+)
drivers/core/device-remove.c +9 -0
index 586fadee0a..abed84a652 100644
@@ -16,6 +16,7 @@
 #include <dm/uclass.h>
 #include <dm/uclass-internal.h>
 #include <dm/util.h>
+#include <power-domain.h>
 
 int device_chld_unbind(struct udevice *dev, struct driver *drv)
 {
@@ -154,6 +155,7 @@ static bool flags_remove(uint flags, uint drv_flags)
 
 int device_remove(struct udevice *dev, uint flags)
 {
+	struct power_domain pd;
 	const struct driver *drv;
 	int ret;
 
@@ -192,6 +194,13 @@ int device_remove(struct udevice *dev, uint flags)
 		}
 	}
 
+	if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
+	    device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN &&
+	    !(dev->flags & DM_FLAG_REMOVE_WITH_PD_ON)) {
+		if (!power_domain_get(dev, &pd))
+			power_domain_off(&pd);
+	}
+
 	if (flags_remove(flags, drv->flags)) {
 		device_free(dev);
 
include/dm/device.h +6 -0
index 27a6d7b9fd..9a98a4a39e 100644
@@ -61,6 +61,12 @@ struct driver_info;
  */
 #define DM_FLAG_OS_PREPARE		(1 << 10)
 
+/*
+ * Device is removed without switching off its power domain. This might
+ * be required, i. e. for serial console (debug) output when booting OS.
+ */
+#define DM_FLAG_REMOVE_WITH_PD_ON	(1 << 11)
+
 /*
  * One or multiple of these flags are passed to device_remove() so that
  * a selective device removal as specified by the remove-stage and the
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 2/2 serial: lpuart: request dm device removal when booting OS

Extend the driver to allow dm device removal, but always let the console serial device power domain enabled, so that U-Boot doesn't crash when i. e. the serial output is enabled for debugging.

---
Changes in v2:
- None

 drivers/serial/serial_lpuart.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/serial/serial_lpuart.c +9 -0
index 57dd4a72c6..b028f4b35f 100644
@@ -489,6 +489,13 @@ static int lpuart_serial_probe(struct udevice *dev)
 		return _lpuart_serial_init(dev);
 }
 
+static int lpuart_serial_remove(struct udevice *dev)
+{
+	if (dev == gd->cur_serial_dev)
+		dev->flags |= DM_FLAG_REMOVE_WITH_PD_ON;
+	return 0;
+}
+
 static int lpuart_serial_ofdata_to_platdata(struct udevice *dev)
 {
 	struct lpuart_serial_platdata *plat = dev->platdata;
@@ -543,5 +550,7 @@ U_BOOT_DRIVER(serial_lpuart) = {
 	.ofdata_to_platdata = lpuart_serial_ofdata_to_platdata,
 	.platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata),
 	.probe = lpuart_serial_probe,
+	.remove = lpuart_serial_remove,
 	.ops	= &lpuart_serial_ops,
+	.flags	= DM_FLAG_OS_PREPARE,
 };
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/2 serial: lpuart: request dm device removal when booting OS

Hi Anatolij,

Quoted 20 lines

Subject: [PATCH v2 2/2] serial: lpuart: request dm device removal when booting OS

Extend the driver to allow dm device removal, but always let the console serial device power domain enabled, so that U-Boot doesn't crash when i. e. the serial output is enabled for debugging.

---
Changes in v2:
- None

 drivers/serial/serial_lpuart.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/serial/serial_lpuart.c index renamed from drivers/serial/serial_lpuart.c +0 -0

57dd4a72c6..b028f4b35f 100644

drivers/serial/serial_lpuart.c static int lpuart_serial_probe(struct udevice *dev) · line 494

Quoted 6 lines
drivers/serial/serial_lpuart.c +3 -0
@@ -489,6 +489,13 @@ static int lpuart_serial_probe(struct udevice *dev)
 		return _lpuart_serial_init(dev);
 }
 
+static int lpuart_serial_remove(struct udevice *dev) {
+	if (dev == gd->cur_serial_dev)
+		dev->flags |= DM_FLAG_REMOVE_WITH_PD_ON;

How about introduce a device tree property for DM? Then Set the flags in common code, not in specific driver.

Thanks,
Peng

Quoted 15 lines
drivers/serial/serial_lpuart.c +5 -1
+	return 0;
+}
+
 static int lpuart_serial_ofdata_to_platdata(struct udevice *dev)  {
 	struct lpuart_serial_platdata *plat = dev->platdata; @@ -543,5 +550,7
 @@ U_BOOT_DRIVER(serial_lpuart) = {
 	.ofdata_to_platdata = lpuart_serial_ofdata_to_platdata,
 	.platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata),
 	.probe = lpuart_serial_probe,
+	.remove = lpuart_serial_remove,
 	.ops	= &lpuart_serial_ops,
+	.flags	= DM_FLAG_OS_PREPARE,
 };
--

2.17.1

#3 lore raw

PATCH v2 2/2 serial: lpuart: request dm device removal when booting OS

Hi Peng,

On Mon, 15 Jul 2019 03:02:46 +0000
Peng Fan peng.fan at nxp.com wrote:
...

Quoted 6 lines

drivers/serial/serial_lpuart.c static int lpuart_serial_probe(struct udevice *dev) · line 495

Quoted 3 lines
drivers/serial/serial_lpuart.c +3 -0
+static int lpuart_serial_remove(struct udevice *dev) {
+	if (dev == gd->cur_serial_dev)
+		dev->flags |= DM_FLAG_REMOVE_WITH_PD_ON;  

How about introduce a device tree property for DM? Then Set the flags in common code, not in specific driver.

we should not add more DT properties which are not describing the hardware, I think. Current plan is to drop this patch and do additional check dev != gd->cur_serial_dev in device_remove(), as Lokesh suggested.

--
Anatolij

#4 lore raw

PATCH v2 1/2 dm: core: device: switch off power domain after device removal

In reply to Anatolij Gustschin
Quoted 63 lines

Subject: [PATCH v2 1/2] dm: core: device: switch off power domain after device removal

The power domain associated with a device is enabled when probing, but currently the domain remains enabled when the device is removed. Some boards started to disable power domains for selected devices via custom board_quiesce_devices(), but it doesn't work in many cases, i. e. because devices still can be accessed later in
.remove() callback on behalf of dm_remove_devices_flags().

Utilize the DM core to power off the device power domain, but add a device flag to be able to selectively let the power domain enabled after device removal. This might be required for devices that must remain enabled when booting OS, i. e. serial console for debug output, etc.

---
Changes in v2:
- use CONFIG_IS_ENABLED(POWER_DOMAIN) to reduce code size

 drivers/core/device-remove.c | 9 +++++++++
 include/dm/device.h          | 6 ++++++
 2 files changed, 15 insertions(+)
drivers/core/device-remove.c +9 -1
index 586fadee0a..abed84a652 100644
@@ -16,6 +16,7 @@
 #include <dm/uclass.h>
 #include <dm/uclass-internal.h>
 #include <dm/util.h>
+#include <power-domain.h>
 
 int device_chld_unbind(struct udevice *dev, struct driver *drv)  { @@
-154,6 +155,7 @@ static bool flags_remove(uint flags, uint drv_flags)
 
 int device_remove(struct udevice *dev, uint flags)  {
+	struct power_domain pd;
 	const struct driver *drv;
 	int ret;
@@ -192,6 +194,13 @@ int device_remove(struct udevice *dev, uint flags)
 		}
 	}
 
+	if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
+	    device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN &&
+	    !(dev->flags & DM_FLAG_REMOVE_WITH_PD_ON)) {
+		if (!power_domain_get(dev, &pd))
+			power_domain_off(&pd);
+	}
+
 	if (flags_remove(flags, drv->flags)) {
 		device_free(dev);
 
include/dm/device.h index renamed from include/dm/device.h +0 -0

27a6d7b9fd..9a98a4a39e 100644

include/dm/device.h +0 -0
@@ -61,6 +61,12 @@ struct driver_info;
  */
 #define DM_FLAG_OS_PREPARE		(1 << 10)

include/dm/device.h struct driver_info; · line 72

Quoted 10 lines
include/dm/device.h +6 -0
 
+/*
+ * Device is removed without switching off its power domain. This might
+ * be required, i. e. for serial console (debug) output when booting OS.
+ */
+#define DM_FLAG_REMOVE_WITH_PD_ON	(1 << 11)
+
 /*
  * One or multiple of these flags are passed to device_remove() so that
  * a selective device removal as specified by the remove-stage and the
Quoted 2 lines

--
2.17.1

#5 lore raw

PATCH v2 1/2 dm: core: device: switch off power domain after device removal

Hi Anatolij,

Quoted 16 lines · Peng Fan
Quoted 16 lines

Subject: [PATCH v2 1/2] dm: core: device: switch off power domain after device removal

The power domain associated with a device is enabled when probing, but currently the domain remains enabled when the device is removed. Some boards started to disable power domains for selected devices via custom board_quiesce_devices(), but it doesn't work in many cases, i. e. because devices still can be accessed later in
.remove() callback on behalf of dm_remove_devices_flags().

Utilize the DM core to power off the device power domain, but add a device flag to be able to selectively let the power domain enabled after device removal. This might be required for devices that must remain enabled when booting OS, i. e. serial console for debug output, etc.

Quoted 8 lines
Quoted 8 lines

---
Changes in v2:
- use CONFIG_IS_ENABLED(POWER_DOMAIN) to reduce code size

 drivers/core/device-remove.c | 9 +++++++++
 include/dm/device.h          | 6 ++++++
 2 files changed, 15 insertions(+)

Unfortunately this causes a test failure (make qcheck). Can you please take a look?

Regards,
Simon

#6 lore raw

PATCH v2 1/2 dm: core: device: switch off power domain after device removal

Hi Simon,

On Thu, 18 Jul 2019 09:22:20 -0600
Simon Glass sjg at chromium.org wrote:
...

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
 drivers/core/device-remove.c | 9 +++++++++
 include/dm/device.h          | 6 ++++++
 2 files changed, 15 insertions(+)  

Unfortunately this causes a test failure (make qcheck). Can you please take a look?

The dm power_domain test worked, but later when
dm_test_destroy()
-> uclass_destroy()
removes devices, first 'power-domain' device is removed, then the 'power-domain-test' device. When removing the latter, we run

if (!power_domain_get(dev, &pd))

       power_domain_off(&pd);

and this probes sandbox_power_domain driver for 'power-domain' device activating this device again. Then 'power-domain-test' device is removed, but 'power-domain' is active. When unbinding it later, we get this error in device_unbind():

        if (dev->flags & DM_FLAG_ACTIVATED)
                return -EINVAL;

Following will fix it:

drivers/core/device-remove.c +2 -1
index 586fadee0a..fadb05c944 100644
@@ -64,7 +64,8 @@ int device_unbind(struct udevice *dev)
        if (!dev)
                return -EINVAL;
 
-       if (dev->flags & DM_FLAG_ACTIVATED)
+       if (dev->flags & DM_FLAG_ACTIVATED &&
+           device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN)
                return -EINVAL;
 
        if (!(dev->flags & DM_FLAG_BOUND))

But I'm not sure if this it the correct approach. What do you think?

Thanks,
Anatolij

#7 lore raw

PATCH v2 1/2 dm: core: device: switch off power domain after device removal

Hi Anatolij,

Quoted 22 lines · Anatolij Gustschin

Hi Simon,

On Thu, 18 Jul 2019 09:22:20 -0600
Simon Glass sjg at chromium.org wrote:
...

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
 drivers/core/device-remove.c | 9 +++++++++
 include/dm/device.h          | 6 ++++++
 2 files changed, 15 insertions(+)

Unfortunately this causes a test failure (make qcheck). Can you please take a look?

The dm power_domain test worked, but later when
dm_test_destroy()
-> uclass_destroy()
removes devices, first 'power-domain' device is removed, then the 'power-domain-test' device. When removing the latter, we run

if (!power_domain_get(dev, &pd))

       power_domain_off(&pd);
Quoted 7 lines

and this probes sandbox_power_domain driver for 'power-domain' device activating this device again. Then 'power-domain-test' device is removed, but 'power-domain' is active. When unbinding it later, we get this error in device_unbind():

        if (dev->flags & DM_FLAG_ACTIVATED)
                return -EINVAL;

This is because you are not allowed to unbind an active device. You must deactivate it (device_remove()) first.

Quoted 18 lines

Following will fix it:

drivers/core/device-remove.c +2 -1
index 586fadee0a..fadb05c944 100644
@@ -64,7 +64,8 @@ int device_unbind(struct udevice *dev)
        if (!dev)
                return -EINVAL;
 
-       if (dev->flags & DM_FLAG_ACTIVATED)
+       if (dev->flags & DM_FLAG_ACTIVATED &&
+           device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN)
                return -EINVAL;
 
        if (!(dev->flags & DM_FLAG_BOUND))
Quoted 1 line

But I'm not sure if this it the correct approach. What do you think?

That doesn't look right to me. Power supplies should be removed before being unbound, just like any other device.

Regards,
Simon

#8 lore raw

PATCH v2 1/2 dm: core: device: switch off power domain after device removal

Hi Simon,

On Wed, 31 Jul 2019 10:29:50 -0600
Simon Glass sjg at chromium.org wrote:
...

Quoted 4 lines
Quoted 1 line

But I'm not sure if this it the correct approach. What do you think?

That doesn't look right to me. Power supplies should be removed before being unbound, just like any other device.

OK, I tried to remove the associated power domain device explicitly and this seems to work, at least with sandbox power domain driver. Will rework the patch and resend. Thanks!

--
Anatolij

#9 lore raw

PATCH v2 1/2 dm: core: device: switch off power domain after device removal

In reply to Anatolij Gustschin
Quoted 43 lines · Anatolij Gustschin

The power domain associated with a device is enabled when probing, but currently the domain remains enabled when the device is removed. Some boards started to disable power domains for selected devices via custom board_quiesce_devices(), but it doesn't work in many cases, i. e. because devices still can be accessed later in .remove() callback on behalf of dm_remove_devices_flags().

Utilize the DM core to power off the device power domain, but add a device flag to be able to selectively let the power domain enabled after device removal. This might be required for devices that must remain enabled when booting OS, i. e. serial console for debug output, etc.

---
Changes in v2:
- use CONFIG_IS_ENABLED(POWER_DOMAIN) to reduce code size

 drivers/core/device-remove.c | 9 +++++++++
 include/dm/device.h          | 6 ++++++
 2 files changed, 15 insertions(+)
drivers/core/device-remove.c +2 -0
index 586fadee0a..abed84a652 100644
@@ -16,6 +16,7 @@
 #include <dm/uclass.h>
 #include <dm/uclass-internal.h>
 #include <dm/util.h>
+#include <power-domain.h>
 
 int device_chld_unbind(struct udevice *dev, struct driver *drv)
 {
@@ -154,6 +155,7 @@ static bool flags_remove(uint flags, uint drv_flags)
 
 int device_remove(struct udevice *dev, uint flags)
 {
+	struct power_domain pd;
 	const struct driver *drv;
 	int ret;
 

drivers/core/device-remove.c int device_remove(struct udevice *dev, uint flags) · line 199

Quoted 6 lines
drivers/core/device-remove.c +3 -0
@@ -192,6 +194,13 @@ int device_remove(struct udevice *dev, uint flags)
 		}
 	}
 
+	if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
+	    device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN &&
+	    !(dev->flags & DM_FLAG_REMOVE_WITH_PD_ON)) {

This is going to hit every board and all serial drivers needs to be updated. Can we add an extra check here for (dev != gd->cur_serial_dev).

Thanks and regards,
Lokesh

Quoted 25 lines
drivers/core/device-remove.c +4 -0
+		if (!power_domain_get(dev, &pd))
+			power_domain_off(&pd);
+	}
+
 	if (flags_remove(flags, drv->flags)) {
 		device_free(dev);
 
include/dm/device.h +6 -0
index 27a6d7b9fd..9a98a4a39e 100644
@@ -61,6 +61,12 @@ struct driver_info;
  */
 #define DM_FLAG_OS_PREPARE		(1 << 10)
 
+/*
+ * Device is removed without switching off its power domain. This might
+ * be required, i. e. for serial console (debug) output when booting OS.
+ */
+#define DM_FLAG_REMOVE_WITH_PD_ON	(1 << 11)
+
 /*
  * One or multiple of these flags are passed to device_remove() so that
  * a selective device removal as specified by the remove-stage and the
#10 lore raw

PATCH v2 1/2 dm: core: device: switch off power domain after device removal

Hi Lokesh,

On Tue, 23 Jul 2019 19:35:43 +0530
Lokesh Vutla lokeshvutla at ti.com wrote:
...

Quoted 6 lines

drivers/core/device-remove.c int device_remove(struct udevice *dev, uint flags) · line 199

Quoted 3 lines
drivers/core/device-remove.c +3 -0
+	if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
+	    device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN &&
+	    !(dev->flags & DM_FLAG_REMOVE_WITH_PD_ON)) {  

This is going to hit every board and all serial drivers needs to be updated. Can we add an extra check here for (dev != gd->cur_serial_dev).

Will do it in v3 patch, thanks!

--
Anatolij