Skip to contentSkip to conversation

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

PATCH New Anatolij Gustschin 3 participants 3 messages Aug 1, 2019 – Sep 27, 2019 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

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

Hi Lokesh,

Quoted 58 lines · On Thu, 1 Aug 2019 09:43:39 +0530 Lokesh Vutla lokeshvutla at ti.com wrote:
Quoted 47 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 v3:
- remove 'power-domain' device to fix dm power-domain test (make qcheck)
- don't switch off the power domain for current console device

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

 drivers/core/device-remove.c | 18 ++++++++++++++++++
 include/dm/device.h          |  6 ++++++
 2 files changed, 24 insertions(+)
drivers/core/device-remove.c +2 -0
index 586fadee0a..5a0e48e182 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)
 {
@@ -155,6 +156,7 @@ static bool flags_remove(uint flags, uint drv_flags)
 int device_remove(struct udevice *dev, uint flags)
 {
 	const struct driver *drv;
+	struct power_domain pd;
 	int ret;
 
 	if (!dev)

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

Quoted 7 lines
drivers/core/device-remove.c +4 -0
@@ -192,6 +194,22 @@ 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 != gd->cur_serial_dev &&
+	    !(dev->flags & DM_FLAG_REMOVE_WITH_PD_ON)) {  

I did not realize before but this statement is too hard to read :). Can we drop the following two conditions:

Quoted 3 lines
	dev->parent && device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN

power_domain_get() will indirectly take care of above two cases.

I'll test it.

Quoted 67 lines

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

Quoted 9 lines
drivers/core/device-remove.c +9 -0
+		if (!power_domain_get(dev, &pd)) {
+			power_domain_off(&pd);
+			/*
+			 * power_domain_get() bound the device, thus
+			 * we must remove it again to prevent unbinding
+			 * active devices (which would result in unbind
+			 * error).
+			 */
+			device_remove(pd.dev, DM_REMOVE_NORMAL);  

Unless I am mistaken, this looks like this is an extra work. Why can't we store a PD in struct device, something like below:

drivers/core/device.c +3 -3
index cded457e2b..efa780a02f 100644
@@ -307,7 +307,6 @@ static void *alloc_priv(int size, uint flags)
 
 int device_probe(struct udevice *dev)
 {
-	struct power_domain pd;
 	const struct driver *drv;
 	int size = 0;
 	int ret;
@@ -388,10 +387,11 @@ int device_probe(struct udevice *dev)
 	if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
 		pinctrl_select_state(dev, "default");
 
+	dev->pd.dev = NULL;
 	if (dev->parent && device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN &&
 	    !(drv->flags & DM_FLAG_DEFAULT_PD_EN_OFF)) {
-		if (!power_domain_get(dev, &pd))
-			power_domain_on(&pd);
+		if (!power_domain_get(dev, &dev->pd))
+			power_domain_on(&dev->pd);
 	}
 
 	ret = uclass_pre_probe_device(dev);
include/dm/device.h +3 -0
index b892386dc4..26699ca5cc 100644
@@ -12,12 +12,14 @@
 
 #include <dm/ofnode.h>
 #include <dm/uclass-id.h>
+#include <errno.h>
 #include <fdtdec.h>
 #include <linker_lists.h>
 #include <linux/compat.h>
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/printk.h>
+#include <power-domain.h>
 
 struct driver_info;
 
@@ -126,6 +128,7 @@ enum {
 *		When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
 *		add to this list. Memory so-allocated will be freed
 *		automatically when the device is removed / unbound
+ * @pd: Pointer to power_domain controlling this device.
 */
 struct udevice {
 	const struct driver *driver;

include/dm/device.h struct udevice { · line 158

Quoted 7 lines
include/dm/device.h +1 -0
@@ -149,6 +152,7 @@ struct udevice {
 #ifdef CONFIG_DEVRES
 	struct list_head devres_head;
 #endif
+	struct power_domain pd;
 };
 
 /* Maximum sequence number supported */

I tried similar change, this didn't work yet. When booting Linux I see infinite recursion in device_remove() for 'lsio_gpio0' on i.mx8qxp.

--
Anatolij

#3 lore raw

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

Hi Anatolij,

Quoted 25 lines · Anatolij Gustschin

Hi Lokesh,

Quoted 19 lines · On Thu, 1 Aug 2019 09:43:39 +0530 Lokesh Vutla lokeshvutla at ti.com wrote:
Quoted 18 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 v3:
- remove 'power-domain' device to fix dm power-domain test (make qcheck)
- don't switch off the power domain for current console device

This still breaks 'make qcheck' for me. It doesn't build sandbox_spl:

crosfw -b sandbox_spl
cmd: 'make -j32 'ARCH=sandbox' 'CROSS_COMPILE=' --no-print-directory
'HOSTSTRIP=true' 'DEV_TREE_SRC=--default' 'QEMU_ARCH=' -s
'CROS_FULL=1' 'O=/tmp/b/sandbox_spl' 'BUILD_ROM=1'
'DEV_TREE_SEPARATE=1' all'
output: 'drivers/core/device-remove.o: In function `dev_power_domain_on':
/scratch/sglass/cosarm/src/third_party/u-boot/files/include/power-domain.h:172:
multiple definition of `dev_power_domain_on' drivers/core/device.o:/scratch/sglass/cosarm/src/third_party/u-boot/files/include/power-domain.h:172:
first defined here /scratch/sglass/cosarm/src/third_party/u-boot/files/scripts/Makefile.build:355:
recipe for target 'drivers/core/built-in.o' failed
make[3]: *** [drivers/core/built-in.o] Error 1 /scratch/sglass/cosarm/src/third_party/u-boot/files/scripts/Makefile.build:432:
recipe for target 'drivers/core' failed
make[2]: *** [drivers/core] Error 2
make[2]: *** Waiting for unfinished jobs.... /scratch/sglass/cosarm/src/third_party/u-boot/files/Makefile:1633:
recipe for target 'drivers' failed
make[1]: *** [drivers] Error 2
make[1]: *** Waiting for unfinished jobs....
Makefile:148: recipe for target 'sub-make' failed
make: *** [sub-make] Error 2

Regards,
SImon

#4 lore raw

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

Quoted 31 lines · Simon Glass

Hi Anatolij,

Quoted 25 lines · Anatolij Gustschin

Hi Lokesh,

Quoted 19 lines · On Thu, 1 Aug 2019 09:43:39 +0530 Lokesh Vutla lokeshvutla at ti.com wrote:
Quoted 18 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 v3:
- remove 'power-domain' device to fix dm power-domain test (make qcheck)
- don't switch off the power domain for current console device

This still breaks 'make qcheck' for me. It doesn't build sandbox_spl:

Quoted 8 lines

crosfw -b sandbox_spl
cmd: 'make -j32 'ARCH=sandbox' 'CROSS_COMPILE=' --no-print-directory 'HOSTSTRIP=true' 'DEV_TREE_SRC=--default' 'QEMU_ARCH=' -s 'CROS_FULL=1' 'O=/tmp/b/sandbox_spl' 'BUILD_ROM=1' 'DEV_TREE_SEPARATE=1' all'
output: 'drivers/core/device-remove.o: In function `dev_power_domain_on':
/scratch/sglass/cosarm/src/third_party/u-boot/files/include/power-domain.h:172:
multiple definition of `dev_power_domain_on'

dev_power_domain_on is not yet in the mainline. I guess this is caused by v2 of dev_power_domain_on() series. Looks like that is fixed by [0]. So, I rebased this patch on top of [0] and posted it[1]. Anatolij, I hope you don't mind :)

[0] https://patchwork.ozlabs.org/project/uboot/list/?series=132841
[1] https://patchwork.ozlabs.org/project/uboot/list/?series=132880

Thanks and regards,
Lokesh

Quoted 19 lines

drivers/core/device.o:/scratch/sglass/cosarm/src/third_party/u-boot/files/include/power-domain.h:172:
first defined here /scratch/sglass/cosarm/src/third_party/u-boot/files/scripts/Makefile.build:355:
recipe for target 'drivers/core/built-in.o' failed
make[3]: *** [drivers/core/built-in.o] Error 1 /scratch/sglass/cosarm/src/third_party/u-boot/files/scripts/Makefile.build:432:
recipe for target 'drivers/core' failed
make[2]: *** [drivers/core] Error 2
make[2]: *** Waiting for unfinished jobs.... /scratch/sglass/cosarm/src/third_party/u-boot/files/Makefile:1633:
recipe for target 'drivers' failed
make[1]: *** [drivers] Error 2
make[1]: *** Waiting for unfinished jobs....
Makefile:148: recipe for target 'sub-make' failed
make: *** [sub-make] Error 2

Regards,
SImon