dm: core: Fully separate ofdata_to_platdata() from probe()
Subsystems (6)
cover dm: core: Fully separate ofdata_to_platdata() from probe()
At present ofdata_to_platdata() is done as part of device_probe(). Drivers are supposed to read their platdata in the ofdata_to_platdata method() but this requirement is not always followed.
Having these methods separate helps deal with of-platdata, where the probe() method is common to both DT/of-platdata operation, but the ofdata_to_platdata() method is implemented differently.
Another case has come up where this separate is useful. Generation of ACPI tables uses the of-platdata but does not want to probe the device. Probing would cause U-Boot to violate one of its design principles, viz that it should only probe devices that are used. For ACPI we want to generate a table for each device, even if U-Boot does not use it. In fact it may not even be possible to probe the device - e.g. an SD card which is not present will cause an error on probe, yet we still must tell Linux about the SD card connector in case it is used while Linux is running.
This series splits out the ofdata_to_platdata() part of device_probe() so that it can be used separately from device_probe(). Thus devices now go through two distinct states when probing: reading platform data and actually touching the hardware to bring the device up.
This should not break existing boards since the operations still happen in mostly the same order. The main change is that parents and uclasses are probed after ofdata_to_platdata() is called.
HOWEVER it is quite possible that some boards break the rules and due to a series of unfortunate events, something will break. Two boards were found in this category already. SO this series may require some tidying up from board maintainers, if problems arise.
Note that there are cases where devices must be probed in the ofdata_to_platdata() method. An example is where a GPIO is selected - this obviously requires that the GPIO device is probed.
One board was found to burst its size limit with this series, despite the very small size increase. The patches to remove use of BUG_ON() are to ensure that this series passes tests.
Simon Glass (19):
common: Add a noisy assert()
dm: core: Use assert_noisy() in devres
usb: Drop use of BUG_ON() and WARN_ON()
x86: apl: Avoid accessing the PCI bus before it is probed
pci: Print a warning if the bus is accessed before probing
aspeed: ast2500: Read clock ofdata in the correct method
dm: core: Don't clear active flag twice when probe() fails
dm: core: Move ofdata_to_platdata() call earlier
dm: core: Allocate parent data separate from probing parent
dm: core: Add a comment for DM_FLAG_OF_PLATDATA
dm: core: Export a new function to read platdata
dm: core: Add a new flag to track platform data
dm: devres: Create a new devres header file
test: Add functions to find the amount of allocated memory
dm: devres: Convert to use logging
dm: test: Add a test driver for devres
dm: devres: Add tests
dm: devres: Use an enum for the allocation phase
dm: devres: Add a new OFDATA phase
arch/sandbox/dts/test.dts | 4 + arch/x86/cpu/apollolake/p2sb.c | 20 +- arch/x86/cpu/apollolake/pmc.c | 20 +- drivers/clk/aspeed/clk_ast2500.c | 4 +- drivers/core/device-remove.c | 1 + drivers/core/device.c | 56 +++-- drivers/core/devres.c | 57 ++++- drivers/pci/pci-uclass.c | 13 ++ drivers/usb/gadget/composite.c | 4 + drivers/usb/gadget/f_mass_storage.c | 4 + drivers/usb/musb-new/musb_core.c | 4 + drivers/usb/musb-new/musb_gadget_ep0.c | 2 +- include/dm/device-internal.h | 16 ++ include/dm/device.h | 259 +--------------------- include/dm/devres.h | 289 +++++++++++++++++++++++++ include/dm/uclass-id.h | 1 + include/log.h | 15 ++ include/test/test.h | 10 + include/test/ut.h | 16 ++ test/dm/Makefile | 1 + test/dm/devres.c | 186 ++++++++++++++++ test/dm/test-fdt.c | 58 +++++ test/ut.c | 14 ++ 23 files changed, 760 insertions(+), 294 deletions(-) create mode 100644 include/dm/devres.h create mode 100644 test/dm/devres.c
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 1/19 common: Add a noisy assert()
Some U-Boot code uses BUG_ON() and WARN_ON() macros. These use __FILE__ which can include quite a large path, depending on how U-Boot is built.
The existing assert() is only checked if DEBUG is enabled. Add a new one which is always checked, and prints a (smaller) error in that case.
---
include/log.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/log.h +14 -0
@@ -218,6 +218,20 @@ void __assert_fail(const char *assertion, const char *file, unsigned int line, ({if (!(x) && _DEBUG) \ __assert_fail(#x, __FILE__, __LINE__, __func__); }) +/* +* This one actually gets compiled in even without DEBUG. It doesn't include the +* full pathname as it may be huge. Only use this when the user should be +* warning, similar to BUG_ON() in linux. +* +* @return true if assertion succeeded (condition is true), else false + */ +#define assert_noisy (x) \ + ({ bool _val = (x); \ + if (!_val) \ + __assert_fail(#x, "?", __LINE__, __func__); \ + _val; \ + }) +#if CONFIG_IS_ENABLED (LOG ) && defined(CONFIG_LOG_ERROR_RETURN )/* * Log an error return value, possibly with a message. Usage:
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 1/19 common: Add a noisy assert()
Some U-Boot code uses BUG_ON() and WARN_ON() macros. These use __FILE__ which can include quite a large path, depending on how U-Boot is built.
The existing assert() is only checked if DEBUG is enabled. Add a new one which is always checked, and prints a (smaller) error in that case.
---
include/log.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 2/19 dm: core: Use assert_noisy() in devres
In reply to Simon GlassUse this macros instead of the linux ones, as the output is smaller.
---
drivers/core/devres.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/core/devres.c +3 -3
@@ -80,7 +80,7 @@ void devres_free(void *res)if (res) { struct devres *dr = container_of(res, struct devres, data); -BUG_ON (!list_empty (&dr->entry)); +assert_noisy (list_empty (&dr->entry));kfree (dr); } }
@@ -90,7 +90,7 @@ void devres_add(struct udevice *dev, void *res) struct devres *dr = container_of(res, struct devres, data);devres_log (dev, dr,"ADD" ); -BUG_ON (!list_empty (&dr->entry)); +assert_noisy (list_empty (&dr->entry)); dr->probe = dev->flags &DM_FLAG_BOUND ? true : false;list_add_tail (&dr->entry, &dev->devres_head); }
@@ -254,5 +254,5 @@ void devm_kfree(struct udevice *dev, void *p)int rc; rc =devres_destroy (dev, devm_kmalloc_release, devm_kmalloc_match, p); -WARN_ON (rc); +assert_noisy (!rc); }
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 2/19 dm: core: Use assert_noisy() in devres
Use this macros instead of the linux ones, as the output is smaller.
---
drivers/core/devres.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 3/19 usb: Drop use of BUG_ON() and WARN_ON()
In reply to Simon GlassThese macros use __FILE__ which inserts the full path of the object file into U-Boot, thus increasing file size. Drop these usages.
An older version of this patch was submitted here:
http://patchwork.ozlabs.org/patch/1205784/
---
drivers/usb/gadget/composite.c | 4 ++++ drivers/usb/gadget/f_mass_storage.c | 4 ++++ drivers/usb/musb-new/musb_core.c | 4 ++++ drivers/usb/musb-new/musb_gadget_ep0.c | 2 +- 4 files changed, 13 insertions(+), 1 deletion(-)
drivers/usb/gadget/composite.c +4 -0
@@ -1003,7 +1003,11 @@ static void composite_unbind(struct usb_gadget *gadget)* so there's no i/o concurrency that could affect the * state protected by cdev->lock. */ +#ifdef __UBOOT__ +assert_noisy (!cdev->config); +#else BUG_ON (cdev->config); +#endif while (!list_empty (&cdev->configs)) { c = list_first_entry(&cdev->configs,
drivers/usb/gadget/f_mass_storage.c +4 -0
@@ -390,7 +390,11 @@ static inline int __fsg_is_set(struct fsg_common *common,if (common->fsg)return 1 ;ERROR (common,"common->fsg is NULL in %s at %u\n" , func, line); +#ifdef __UBOOT__ +assert_noisy (false); +#else WARN_ON (1 ); +#endif return 0 ; }
drivers/usb/musb-new/musb_core.c +4 -0
@@ -1859,7 +1859,11 @@ allocate_instance(struct device *dev, musb->ctrl_base = mbase; musb->nIrq = -ENODEV ; musb->config = config; +#ifdef __UBOOT__ +assert_noisy (musb->config->num_eps <=MUSB_C_NUM_EPS ); +#else BUG_ON (musb->config->num_eps >MUSB_C_NUM_EPS ); +#endif for (epnum =0 , ep = musb->endpoints; epnum < musb->config->num_eps; epnum++, ep++) {
drivers/usb/musb-new/musb_gadget_ep0.c +1 -1
@@ -882,7 +882,7 @@ finish:default :/* "can't happen" */ -WARN_ON (1 ); +assert_noisy (false);musb_writew (regs,MUSB_CSR0 ,MUSB_CSR0_P_SENDSTALL ); musb->ep0_state =MUSB_EP0_STAGE_IDLE ;break ;
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 3/19 usb: Drop use of BUG_ON() and WARN_ON()
These macros use __FILE__ which inserts the full path of the object file into U-Boot, thus increasing file size. Drop these usages.
An older version of this patch was submitted here:
http://patchwork.ozlabs.org/patch/1205784/
---
drivers/usb/gadget/composite.c | 4 ++++ drivers/usb/gadget/f_mass_storage.c | 4 ++++ drivers/usb/musb-new/musb_core.c | 4 ++++ drivers/usb/musb-new/musb_gadget_ep0.c | 2 +- 4 files changed, 13 insertions(+), 1 deletion(-)
Applied to u-boot-dm, thanks!
PATCH 4/19 x86: apl: Avoid accessing the PCI bus before it is probed
In reply to Simon GlassThe PCI bus is not actually probed by the time the ofdata_to_platdata() method is called since that happens in the uclass's post_probe() method. Update the PMC and P2SB drivers to access the bus in its probe() method.
---
arch/x86/cpu/apollolake/p2sb.c | 20 ++++++++++++-------- arch/x86/cpu/apollolake/pmc.c | 20 +++++++++++++++++--- 2 files changed, 29 insertions(+), 11 deletions(-)
arch/x86/cpu/apollolake/p2sb.c +12 -8
@@ -106,11 +106,6 @@ int apl_p2sb_ofdata_to_platdata(struct udevice *dev)if (plat->bdf <0 )return log_msg_ret ("Cannot get p2sb PCI address" , plat->bdf); - }else { - plat->mmio_base =dev_read_addr_pci (dev); -/* Don't set BDF since it should not be used */ -if (!plat->mmio_base || plat->mmio_base ==FDT_ADDR_T_NONE ) -return -EINVAL ; }#else plat->mmio_base = plat->dtplat.early_regs[0 ];
@@ -124,10 +119,19 @@ int apl_p2sb_ofdata_to_platdata(struct udevice *dev)static int apl_p2sb_probe (struct udevice *dev) { -if (spl_phase () ==PHASE_TPL ) +if (spl_phase () ==PHASE_TPL ) {return apl_p2sb_early_init (dev); -else if (spl_phase () ==PHASE_SPL ) -return apl_p2sb_spl_init (dev); + }else { +struct p2sb_platdata *plat =dev_get_platdata (dev); + + plat->mmio_base =dev_read_addr_pci (dev); +/* Don't set BDF since it should not be used */ +if (!plat->mmio_base || plat->mmio_base ==FDT_ADDR_T_NONE ) +return -EINVAL ; + +if (spl_phase () ==PHASE_SPL ) +return apl_p2sb_spl_init (dev); + }return 0 ; }
arch/x86/cpu/apollolake/pmc.c +17 -3
@@ -119,8 +119,16 @@ int apl_pmc_ofdata_to_uc_platdata(struct udevice *dev) ret =dev_read_u32_array (dev,"early-regs" , base,ARRAY_SIZE (base));if (ret)return log_msg_ret ("Missing/short early-regs" , ret); - upriv->pmc_bar0 = (void *)base[0 ]; - upriv->pmc_bar2 = (void *)base[2 ]; +if (spl_phase () ==PHASE_TPL ) { + upriv->pmc_bar0 = (void *)base[0 ]; + upriv->pmc_bar2 = (void *)base[2 ]; + +/* Since PCI is not enabled, we must get the BDF manually */ + plat->bdf =pci_get_devfn (dev); +if (plat->bdf <0 ) +return log_msg_ret ("Cannot get PMC PCI address" , + plat->bdf); + } upriv->acpi_base = base[4 ];/* Since PCI is not enabled, we must get the BDF manually */
@@ -187,8 +195,14 @@ static int enable_pmcbar(struct udevice *dev)static int apl_pmc_probe (struct udevice *dev) { -if (spl_phase () ==PHASE_TPL ) +if (spl_phase () ==PHASE_TPL ) {return enable_pmcbar (dev); + }else { +struct acpi_pmc_upriv *upriv =dev_get_uclass_priv (dev); + + upriv->pmc_bar0 = (void *)dm_pci_read_bar32 (dev,0 ); + upriv->pmc_bar2 = (void *)dm_pci_read_bar32 (dev,2 ); + }return 0 ; }
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 4/19 x86: apl: Avoid accessing the PCI bus before it is probed
The PCI bus is not actually probed by the time the ofdata_to_platdata() method is called since that happens in the uclass's post_probe() method. Update the PMC and P2SB drivers to access the bus in its probe() method.
---
arch/x86/cpu/apollolake/p2sb.c | 20 ++++++++++++-------- arch/x86/cpu/apollolake/pmc.c | 20 +++++++++++++++++--- 2 files changed, 29 insertions(+), 11 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 5/19 pci: Print a warning if the bus is accessed before probing
In reply to Simon GlassIt is not possible to access a device on a PCI bus that has not yet been probed, since the bus number is not known. Add a warning to catch this error.
---
drivers/pci/pci-uclass.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/pci/pci-uclass.c +13 -0
@@ -48,6 +48,19 @@ pci_dev_t dm_pci_get_bdf(struct udevice *dev)struct pci_child_platdata *pplat =dev_get_parent_platdata (dev);struct udevice *bus = dev->parent; +/* +* This error indicates that @dev is a device on an unprobed PCI bus. +* The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below +* will produce a bad BDF> +* +* A common cause of this problem is that this function is called in the +* ofdata_to_platdata() method of @dev. Accessing the PCI bus in that +* method is not allowed, since it has not yet been probed. To fix this, +* move that access to the probe() method of @dev instead. +*/ +if (!device_active (bus)) +log_err ("PCI: Device '%s' on unprobed bus '%s'\n" , dev->name, + bus->name);return PCI_ADD_BUS (bus->seq, pplat->devfn); }
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 5/19 pci: Print a warning if the bus is accessed before probing
It is not possible to access a device on a PCI bus that has not yet been probed, since the bus number is not known. Add a warning to catch this error.
---
drivers/pci/pci-uclass.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 6/19 aspeed: ast2500: Read clock ofdata in the correct method
In reply to Simon GlassAt present the clock driver reads its ofdata in the probe() method. This is not correct although it is often harmless.
However in this case it causes a problem, something like this:
- ast_get_scu() is called (from somewhere) to get the SCI address
- this probes the clock
- first sets up ofdata (which does nothing at present)
- DM marks clock device as active
- DM calls pinctrl
- pinctrl probes and calls ast_get_scu() in ast2500_pinctrl_probe()
- ast_get_scu() probes the clock, but sees it already marked as probed
- ast_get_scu() accesses the clock's private data, with scu as NULL
- DM calls clock probe function ast2500_clk_probe() which reads scu
By putting the read of scu into the correct method, scu is read as part of ofdata setup, and everything is OK.
Note: This problem did not matter until now since DM always probed all parents before reading a child's ofdata. The fact that pinctrl is a child of clock seems to trigger this strange bug.
---
drivers/clk/aspeed/clk_ast2500.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/aspeed/clk_ast2500.c +2 -2
@@ -490,7 +490,7 @@ struct clk_ops ast2500_clk_ops = { .enable = ast2500_clk_enable, }; -static int ast2500_clk_probe (struct udevice *dev) +static int ast2500_clk_ofdata_to_platdata (struct udevice *dev) {struct ast2500_clk_priv *priv =dev_get_priv (dev);
@@ -525,5 +525,5 @@ U_BOOT_DRIVER(aspeed_ast2500_scu) = { .priv_auto_alloc_size = sizeof(struct ast2500_clk_priv), .ops = &ast2500_clk_ops, .bind = ast2500_clk_bind, - .probe = ast2500_clk_probe, + .ofdata_to_platdata = ast2500_clk_ofdata_to_platdata, };
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 6/19 aspeed: ast2500: Read clock ofdata in the correct method
Quoted 16 lines · Simon Glass
At present the clock driver reads its ofdata in the
probe()method. This is not correct although it is often harmless.However in this case it causes a problem, something like this:
-
ast_get_scu()is called (from somewhere) to get the SCI address
- this probes the clock
- first sets up ofdata (which does nothing at present)
- DM marks clock device as active
- DM calls pinctrl
- pinctrl probes and callsast_get_scu()inast2500_pinctrl_probe()
-ast_get_scu()probes the clock, but sees it already marked as probed
-ast_get_scu()accesses the clock's private data, with scu as NULL
- DM calls clock probe functionast2500_clk_probe()which reads scu
Quoted 6 lines
By putting the read of scu into the correct method, scu is read as part of ofdata setup, and everything is OK.
Note: This problem did not matter until now since DM always probed all parents before reading a child's ofdata. The fact that pinctrl is a child of clock seems to trigger this strange bug.
Did you find it with QEMU ?
Quoted 2 lines
Signed-off-by Simon Glass <[email protected]>
C.
Quoted 26 lines
---
drivers/clk/aspeed/clk_ast2500.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/clk/aspeed/clk_ast2500.c +2
-2@@ -490,7 +490,7 @@ struct clk_ops ast2500_clk_ops = { .enable = ast2500_clk_enable, }; -static int ast2500_clk_probe (struct udevice *dev) +static int ast2500_clk_ofdata_to_platdata (struct udevice *dev) {struct ast2500_clk_priv *priv =dev_get_priv (dev);@@ -525,5 +525,5 @@ U_BOOT_DRIVER(aspeed_ast2500_scu) = { .priv_auto_alloc_size = sizeof(struct ast2500_clk_priv), .ops = &ast2500_clk_ops, .bind = ast2500_clk_bind, - .probe = ast2500_clk_probe, + .ofdata_to_platdata = ast2500_clk_ofdata_to_platdata, };
PATCH 6/19 aspeed: ast2500: Read clock ofdata in the correct method
Hi Cédric,
Quoted 18 lines · Cédric Le Goater
Quoted 16 lines · Simon Glass
At present the clock driver reads its ofdata in the
probe()method. This is not correct although it is often harmless.However in this case it causes a problem, something like this:
-
ast_get_scu()is called (from somewhere) to get the SCI address
- this probes the clock
- first sets up ofdata (which does nothing at present)
- DM marks clock device as active
- DM calls pinctrl
- pinctrl probes and callsast_get_scu()inast2500_pinctrl_probe()
-ast_get_scu()probes the clock, but sees it already marked as probed
-ast_get_scu()accesses the clock's private data, with scu as NULL
- DM calls clock probe functionast2500_clk_probe()which reads scu
Quoted 8 lines
Quoted 6 lines
By putting the read of scu into the correct method, scu is read as part of ofdata setup, and everything is OK.
Note: This problem did not matter until now since DM always probed all parents before reading a child's ofdata. The fact that pinctrl is a child of clock seems to trigger this strange bug.
Did you find it with QEMU ?
Yes that's right.
Quoted 26 lines
Quoted 3 lines
Signed-off-by Simon Glass <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>C.
Quoted 18 lines
---
drivers/clk/aspeed/clk_ast2500.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/clk/aspeed/clk_ast2500.c +1
-1@@ -490,7 +490,7 @@ struct clk_ops ast2500_clk_ops = { .enable = ast2500_clk_enable, }; -static int ast2500_clk_probe (struct udevice *dev) +static int ast2500_clk_ofdata_to_platdata (struct udevice *dev) {struct ast2500_clk_priv *priv =dev_get_priv (dev);
Quoted 9 lines
drivers/clk/aspeed/clk_ast2500.c U_BOOT_DRIVER(aspeed_ast2500_scu) = { · line 529
Quoted 8 lines
drivers/clk/aspeed/clk_ast2500.c +1
-1@@ -525,5 +525,5 @@ U_BOOT_DRIVER(aspeed_ast2500_scu) = { .priv_auto_alloc_size = sizeof(struct ast2500_clk_priv), .ops = &ast2500_clk_ops, .bind = ast2500_clk_bind, - .probe = ast2500_clk_probe, + .ofdata_to_platdata = ast2500_clk_ofdata_to_platdata, };
Regards,
Simon
PATCH 6/19 aspeed: ast2500: Read clock ofdata in the correct method
Hi Cédric,
Quoted 18 lines · Cédric Le Goater
Quoted 16 lines · Simon Glass
At present the clock driver reads its ofdata in the
probe()method. This is not correct although it is often harmless.However in this case it causes a problem, something like this:
-
ast_get_scu()is called (from somewhere) to get the SCI address
- this probes the clock
- first sets up ofdata (which does nothing at present)
- DM marks clock device as active
- DM calls pinctrl
- pinctrl probes and callsast_get_scu()inast2500_pinctrl_probe()
-ast_get_scu()probes the clock, but sees it already marked as probed
-ast_get_scu()accesses the clock's private data, with scu as NULL
- DM calls clock probe functionast2500_clk_probe()which reads scu
Quoted 8 lines
Quoted 6 lines
By putting the read of scu into the correct method, scu is read as part of ofdata setup, and everything is OK.
Note: This problem did not matter until now since DM always probed all parents before reading a child's ofdata. The fact that pinctrl is a child of clock seems to trigger this strange bug.
Did you find it with QEMU ?
Yes that's right.
Quoted 13 lines
Quoted 3 lines
Signed-off-by Simon Glass <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>C.
Quoted 5 lines
---
drivers/clk/aspeed/clk_ast2500.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 7/19 dm: core: Don't clear active flag twice when probe() fails
In reply to Simon GlassRemove this duplicated code, since the 'fail' label does this immediately.
---
drivers/core/device.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/core/device.c +1 -3
@@ -431,10 +431,8 @@ int device_probe(struct udevice *dev)if (drv->probe) { ret = drv->probe (dev); -if (ret) { - dev->flags &= ~DM_FLAG_ACTIVATED ; +if (ret) goto fail; - } } ret =uclass_post_probe_device (dev);
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 7/19 dm: core: Don't clear active flag twice when probe() fails
Remove this duplicated code, since the 'fail' label does this immediately.
---
drivers/core/device.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 8/19 dm: core: Move ofdata_to_platdata() call earlier
In reply to Simon GlassThis method is supposed to extract platform data from the device tree. It should be done before the device itself is probed. Move it earlier in the device_probe() function.
---
drivers/core/device.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/core/device.c +7 -7
@@ -375,6 +375,13 @@ int device_probe(struct udevice *dev)return 0 ; } +if (drv->ofdata_to_platdata && + (CONFIG_IS_ENABLED (OF_PLATDATA ) ||dev_has_of_node (dev))) { + ret = drv->ofdata_to_platdata (dev); +if (ret) + goto fail; + } + seq =uclass_resolve_seq (dev);if (seq <0 ) { ret = seq;
@@ -411,13 +418,6 @@ int device_probe(struct udevice *dev) goto fail; } -if (drv->ofdata_to_platdata && - (CONFIG_IS_ENABLED (OF_PLATDATA ) ||dev_has_of_node (dev))) { - ret = drv->ofdata_to_platdata (dev); -if (ret) - goto fail; - } -/* Only handle devices that have a valid ofnode */ if (dev_of_valid (dev)) {/*
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 8/19 dm: core: Move ofdata_to_platdata() call earlier
This method is supposed to extract platform data from the device tree. It should be done before the device itself is probed. Move it earlier in the device_probe() function.
---
drivers/core/device.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 9/19 dm: core: Allocate parent data separate from probing parent
In reply to Simon GlassAt present the parent is probed before the child's ofdata_to_platdata() method is called. Adjust the logic slightly so that probing parents is not done until afterwards.
---
drivers/core/device.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/core/device.c +11 -8
@@ -346,7 +346,7 @@ int device_probe(struct udevice *dev) } } -/* Ensure all parents are probed */ +/* Allocate parent data for this child */ if (dev->parent) { size = dev->parent->driver->per_child_auto_alloc_size;if (!size) {
@@ -360,7 +360,17 @@ int device_probe(struct udevice *dev) goto fail; } } + } + +if (drv->ofdata_to_platdata && + (CONFIG_IS_ENABLED (OF_PLATDATA ) ||dev_has_of_node (dev))) { + ret = drv->ofdata_to_platdata (dev); +if (ret) + goto fail; + } +/* Ensure all parents are probed */ +if (dev->parent) { ret =device_probe (dev->parent);if (ret) goto fail;
@@ -375,13 +385,6 @@ int device_probe(struct udevice *dev)return 0 ; } -if (drv->ofdata_to_platdata && - (CONFIG_IS_ENABLED (OF_PLATDATA ) ||dev_has_of_node (dev))) { - ret = drv->ofdata_to_platdata (dev); -if (ret) - goto fail; - } - seq =uclass_resolve_seq (dev);if (seq <0 ) { ret = seq;
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 9/19 dm: core: Allocate parent data separate from probing parent
At present the parent is probed before the child's ofdata_to_platdata() method is called. Adjust the logic slightly so that probing parents is not done until afterwards.
---
drivers/core/device.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 10/19 dm: core: Add a comment for DM_FLAG_OF_PLATDATA
In reply to Simon GlassThis flag is missing a comment. Add one.
---
include/dm/device.h | 1 + 1 file changed, 1 insertion(+)
include/dm/device.h +1 -0
@@ -45,6 +45,7 @@ struct driver_info;/* Device name is allocated and should be freed on unbind() */ #define DM_FLAG_NAME_ALLOCED (1 << 7) +/* Device has platform data provided by of-platdata */ #define DM_FLAG_OF_PLATDATA (1 << 8)/*
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 10/19 dm: core: Add a comment for DM_FLAG_OF_PLATDATA
This flag is missing a comment. Add one.
---
include/dm/device.h | 1 + 1 file changed, 1 insertion(+)
Applied to u-boot-dm, thanks!
PATCH 11/19 dm: core: Export a new function to read platdata
In reply to Simon GlassAdd a new internal function, device_ofdata_to_platdata() to handle allocating private space associated with each device and reading the platform data from the device tree.
Call this new function from device_probe().
---
drivers/core/device.c | 29 +++++++++++++++++++++++++++-- include/dm/device-internal.h | 16 ++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-)
drivers/core/device.c +27 -2
@@ -311,12 +311,11 @@ static void *alloc_priv(int size, uint flags)return priv; } -int device_probe (struct udevice *dev) +int device_ofdata_to_platdata (struct udevice *dev) {const struct driver *drv;int size =0 ;int ret; -int seq;if (!dev)return -EINVAL ;
@@ -369,6 +368,32 @@ int device_probe(struct udevice *dev) goto fail; } +return 0 ; +fail: +device_free (dev); + +return ret; +} + +int device_probe (struct udevice *dev) +{ +const struct driver *drv; +int ret; +int seq; + +if (!dev) +return -EINVAL ; + +if (dev->flags &DM_FLAG_ACTIVATED ) +return 0 ; + + drv = dev->driver; +assert (drv); + + ret =device_ofdata_to_platdata (dev); +if (ret) + goto fail; +/* Ensure all parents are probed */ if (dev->parent) { ret =device_probe (dev->parent);
include/dm/device-internal.h +16 -0
@@ -83,6 +83,22 @@ int device_bind_with_driver_data(struct udevice *parent,int device_bind_by_name (struct udevice *parent,bool pre_reloc_only,const struct driver_info *info,struct udevice **devp); +/** +* device_ofdata_to_platdata() - Read platform data for a device +* +* Read platform data for a device (typically from the device tree) so that +* the information needed to probe the device is present. +* +* This may cause some others devices to be probed if this one depends on them, +* e.g. a GPIO line will cause a GPIO device to be probed. +* +* All private data associated with the device is allocated. +* +* @dev: Pointer to device to process +* @return 0 if OK, -ve on error +*/ +int device_ofdata_to_platdata (struct udevice *dev); +/** * device_probe() - Probe a device, activating it *
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 11/19 dm: core: Export a new function to read platdata
Add a new internal function, device_ofdata_to_platdata() to handle allocating private space associated with each device and reading the platform data from the device tree.
Call this new function from device_probe().
---
drivers/core/device.c | 29 +++++++++++++++++++++++++++-- include/dm/device-internal.h | 16 ++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 12/19 dm: core: Add a new flag to track platform data
In reply to Simon GlassWe want to avoid allocating platform data twice. This could happen if device_probe() is called after device_ofdata_to_platdata() for the same device.
Add a flag to track whether device_ofdata_to_platdata() has been called on a device. Check the flag to make sure it doesn't happen twice, and clear the flag when the data is freed.
---
drivers/core/device-remove.c | 1 + drivers/core/device.c | 4 +++- include/dm/device.h | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-)
drivers/core/device-remove.c +1 -0
@@ -140,6 +140,7 @@ void device_free(struct udevice *dev) dev->parent_priv =NULL ; } } + dev->flags &= ~DM_FLAG_PLATDATA_VALID ;devres_release_probe (dev); }
drivers/core/device.c +3 -1
@@ -320,7 +320,7 @@ int device_ofdata_to_platdata(struct udevice *dev)if (!dev)return -EINVAL ; -if (dev->flags &DM_FLAG_ACTIVATED ) +if (dev->flags &DM_FLAG_PLATDATA_VALID )return 0 ; drv = dev->driver;
@@ -368,6 +368,8 @@ int device_ofdata_to_platdata(struct udevice *dev) goto fail; } + dev->flags |=DM_FLAG_PLATDATA_VALID ; +return 0 ; fail:device_free (dev);
include/dm/device.h +3 -0
@@ -65,6 +65,9 @@ struct driver_info;/* DM does not enable/disable the power domains corresponding to this device */ #define DM_FLAG_DEFAULT_PD_CTRL_OFF (1 << 11) +/* Driver platdata has been read. Cleared when the device is removed */ +#define DM_FLAG_PLATDATA_VALID (1 << 12) +/* * 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.24.1.735.g03f4e72817-goog
PATCH 12/19 dm: core: Add a new flag to track platform data
We want to avoid allocating platform data twice. This could happen if device_probe() is called after device_ofdata_to_platdata() for the same device.
Add a flag to track whether device_ofdata_to_platdata() has been called on a device. Check the flag to make sure it doesn't happen twice, and clear the flag when the data is freed.
---
drivers/core/device-remove.c | 1 + drivers/core/device.c | 4 +++- include/dm/device.h | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-)
Applied to u-boot-dm, thanks!
PATCH 13/19 dm: devres: Create a new devres header file
In reply to Simon GlassAt present these functions are lumped in with the core device functions. They have their own #ifdef to control their availability, so it seems better to split them out.
Move them into their own header file.
---
include/dm/device.h | 255 +---------------------------------------- include/dm/devres.h | 269 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+), 254 deletions(-) create mode 100644 include/dm/devres.h
include/dm/device.h +1 -254
@@ -720,260 +720,7 @@ static inline bool device_is_on_pci_bus(struct udevice *dev)*/ int dm_scan_fdt_dev (struct udevice *dev); -/* device resource management */ -typedef void (*dr_release_t )(struct udevice *dev,void *res); -typedef int (*dr_match_t )(struct udevice *dev,void *res,void *match_data); - -#ifdef CONFIG_DEVRES - -#ifdef CONFIG_DEBUG_DEVRES -void *__devres_alloc (dr_release_t release,size_t size,gfp_t gfp, -const char *name); -#define _devres_alloc (release, size, gfp) \ - __devres_alloc(release, size, gfp, #release) -#else -void *_devres_alloc (dr_release_t release,size_t size,gfp_t gfp); -#endif - -/** -* devres_alloc() - Allocate device resource data -* @release: Release function devres will be associated with -* @size: Allocation size -* @gfp: Allocation flags -* -* Allocate devres of @size bytes. The allocated area is associated -* with @release. The returned pointer can be passed to -* other devres_*() functions. -* -* RETURNS: -* Pointer to allocated devres on success, NULL on failure. -*/ -#define devres_alloc (release, size, gfp) \ - _devres_alloc(release, size, gfp | __GFP_ZERO) - -/** -* devres_free() - Free device resource data -* @res: Pointer to devres data to free -* -* Free devres created with devres_alloc(). -*/ -void devres_free (void *res); - -/** -* devres_add() - Register device resource -* @dev: Device to add resource to -* @res: Resource to register -* -* Register devres @res to @dev. @res should have been allocated -* using devres_alloc(). On driver detach, the associated release -* function will be invoked and devres will be freed automatically. -*/ -void devres_add (struct udevice *dev,void *res); - -/** -* devres_find() - Find device resource -* @dev: Device to lookup resource from -* @release: Look for resources associated with this release function -* @match: Match function (optional) -* @match_data: Data for the match function -* -* Find the latest devres of @dev which is associated with @release -* and for which @match returns 1. If @match is NULL, it's considered -* to match all. -* -* @return pointer to found devres, NULL if not found. -*/ -void *devres_find (struct udevice *dev,dr_release_t release, -dr_match_t match,void *match_data); - -/** -* devres_get() - Find devres, if non-existent, add one atomically -* @dev: Device to lookup or add devres for -* @new_res: Pointer to new initialized devres to add if not found -* @match: Match function (optional) -* @match_data: Data for the match function -* -* Find the latest devres of @dev which has the same release function -* as @new_res and for which @match return 1. If found, @new_res is -* freed; otherwise, @new_res is added atomically. -* -* @return ointer to found or added devres. -*/ -void *devres_get (struct udevice *dev,void *new_res, -dr_match_t match,void *match_data); - -/** -* devres_remove() - Find a device resource and remove it -* @dev: Device to find resource from -* @release: Look for resources associated with this release function -* @match: Match function (optional) -* @match_data: Data for the match function -* -* Find the latest devres of @dev associated with @release and for -* which @match returns 1. If @match is NULL, it's considered to -* match all. If found, the resource is removed atomically and -* returned. -* -* @return ointer to removed devres on success, NULL if not found. -*/ -void *devres_remove (struct udevice *dev,dr_release_t release, -dr_match_t match,void *match_data); - -/** -* devres_destroy() - Find a device resource and destroy it -* @dev: Device to find resource from -* @release: Look for resources associated with this release function -* @match: Match function (optional) -* @match_data: Data for the match function -* -* Find the latest devres of @dev associated with @release and for -* which @match returns 1. If @match is NULL, it's considered to -* match all. If found, the resource is removed atomically and freed. -* -* Note that the release function for the resource will not be called, -* only the devres-allocated data will be freed. The caller becomes -* responsible for freeing any other data. -* -* @return 0 if devres is found and freed, -ENOENT if not found. -*/ -int devres_destroy (struct udevice *dev,dr_release_t release, -dr_match_t match,void *match_data); - -/** -* devres_release() - Find a device resource and destroy it, calling release -* @dev: Device to find resource from -* @release: Look for resources associated with this release function -* @match: Match function (optional) -* @match_data: Data for the match function -* -* Find the latest devres of @dev associated with @release and for -* which @match returns 1. If @match is NULL, it's considered to -* match all. If found, the resource is removed atomically, the -* release function called and the resource freed. -* -* @return 0 if devres is found and freed, -ENOENT if not found. -*/ -int devres_release (struct udevice *dev,dr_release_t release, -dr_match_t match,void *match_data); - -/* managed devm_k.alloc/kfree for device drivers */ -/** -* devm_kmalloc() - Resource-managed kmalloc -* @dev: Device to allocate memory for -* @size: Allocation size -* @gfp: Allocation gfp flags -* -* Managed kmalloc. Memory allocated with this function is -* automatically freed on driver detach. Like all other devres -* resources, guaranteed alignment is unsigned long long. -* -* @return pointer to allocated memory on success, NULL on failure. -*/ -void *devm_kmalloc (struct udevice *dev,size_t size,gfp_t gfp); -static inline void *devm_kzalloc (struct udevice *dev,size_t size,gfp_t gfp) -{ -return devm_kmalloc (dev, size, gfp | __GFP_ZERO); -} -static inline void *devm_kmalloc_array (struct udevice *dev, -size_t n,size_t size,gfp_t flags) -{ -if (size !=0 && n >SIZE_MAX / size) -return NULL ; -return devm_kmalloc (dev, n * size, flags); -} -static inline void *devm_kcalloc (struct udevice *dev, -size_t n,size_t size,gfp_t flags) -{ -return devm_kmalloc_array (dev, n, size, flags | __GFP_ZERO); -} - -/** -* devm_kfree() - Resource-managed kfree -* @dev: Device this memory belongs to -* @ptr: Memory to free -* -* Free memory allocated with devm_kmalloc(). -*/ -void devm_kfree (struct udevice *dev,void *ptr); - -#else /* ! CONFIG_DEVRES */ - -static inline void *devres_alloc (dr_release_t release,size_t size,gfp_t gfp) -{ -return kzalloc (size, gfp); -} - -static inline void devres_free (void *res) -{ -kfree (res); -} - -static inline void devres_add (struct udevice *dev,void *res) -{ -} - -static inline void *devres_find (struct udevice *dev,dr_release_t release, -dr_match_t match,void *match_data) -{ -return NULL ; -} - -static inline void *devres_get (struct udevice *dev,void *new_res, -dr_match_t match,void *match_data) -{ -return NULL ; -} - -static inline void *devres_remove (struct udevice *dev,dr_release_t release, -dr_match_t match,void *match_data) -{ -return NULL ; -} - -static inline int devres_destroy (struct udevice *dev,dr_release_t release, -dr_match_t match,void *match_data) -{ -return 0 ; -} - -static inline int devres_release (struct udevice *dev,dr_release_t release, -dr_match_t match,void *match_data) -{ -return 0 ; -} - -static inline void *devm_kmalloc (struct udevice *dev,size_t size,gfp_t gfp) -{ -return kmalloc (size, gfp); -} - -static inline void *devm_kzalloc (struct udevice *dev,size_t size,gfp_t gfp) -{ -return kzalloc (size, gfp); -} - -static inline void *devm_kmalloc_array (struct udevice *dev, -size_t n,size_t size,gfp_t flags) -{ -/* TODO: add kmalloc_array() to linux/compat.h */ -if (size !=0 && n >SIZE_MAX / size) -return NULL ; -return kmalloc (n * size, flags); -} - -static inline void *devm_kcalloc (struct udevice *dev, -size_t n,size_t size,gfp_t flags) -{ -/* TODO: add kcalloc() to linux/compat.h */ -return kmalloc (n * size, flags | __GFP_ZERO); -} - -static inline void devm_kfree (struct udevice *dev,void *ptr) -{ -kfree (ptr); -} - -#endif /* ! CONFIG_DEVRES */ +#include <dm/devres.h> /* * REVISIT:
include/dm/devres.h new file +269 -0
269 lines not on this page · raw message
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 13/19 dm: devres: Create a new devres header file
At present these functions are lumped in with the core device functions. They have their own #ifdef to control their availability, so it seems better to split them out.
Move them into their own header file.
---
include/dm/device.h | 255 +---------------------------------------- include/dm/devres.h | 269 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+), 254 deletions(-) create mode 100644 include/dm/devres.h
Applied to u-boot-dm, thanks!
PATCH 14/19 test: Add functions to find the amount of allocated memory
In reply to Simon GlassThe malloc() implementations provides a way of finding out the approximate amount of memory that is allocated. Add helper functions to make it easier to access this and see changes over time. This is useful for tests that want to check if memory has been allocated or freed.
---
include/test/ut.h | 16 ++++++++++++++++ test/ut.c | 14 ++++++++++++++ 2 files changed, 30 insertions(+)
include/test/ut.h +16 -0
@@ -149,4 +149,20 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line,/* Assert that an operation succeeds (returns 0) */ #define ut_assertok (cond) ut_asserteq(0, cond) +/** +* ut_check_free() - Return the number of bytes free in the malloc() pool +* +* @return bytes free +*/ +ulong ut_check_free (void ); + +/** +* ut_check_delta() - Return the number of bytes allocated/freed +* +* @last: Last value from ut_check_free +* @return free memory delta from @last; positive means more memory has been +* allocated, negative means less has been allocated (i.e. some is freed) +*/ +long ut_check_delta (ulong last); +#endif
test/ut.c +14 -0
@@ -6,6 +6,7 @@*/ #include <common.h> +#include <malloc.h> #include <test/test.h> #include <test/ut.h>
@@ -32,3 +33,16 @@ void ut_failf(struct unit_test_state *uts, const char *fname, int line,putc ('\n' ); uts->fail_count++; } + +ulong ut_check_free (void ) +{ +struct mallinfo info =mallinfo (); + +return info.uordblks; +} + +long ut_check_delta (ulong last) +{ +return ut_check_free () - last; +} +
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 14/19 test: Add functions to find the amount of allocated memory
The malloc() implementations provides a way of finding out the approximate amount of memory that is allocated. Add helper functions to make it easier to access this and see changes over time. This is useful for tests that want to check if memory has been allocated or freed.
---
include/test/ut.h | 16 ++++++++++++++++ test/ut.c | 14 ++++++++++++++ 2 files changed, 30 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 15/19 dm: devres: Convert to use logging
In reply to Simon GlassAt present when CONFIG_DEBUG_DEVRES is enabled, U-Boot prints log messages to the console with every devres allocation/free event. This causes most tests to fail since the console output is not as expected.
In particular this prevents us from adding a device to sandbox which uses devres in its bind method.
Move devres over to use U-Boot's logging feature instead, and add a new category for devres.
---
drivers/core/devres.c | 6 ++++-- include/log.h | 1 + 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/core/devres.c +4 -2
@@ -7,6 +7,8 @@* Copyright (c) 2006 Tejun Heo <[email protected]> */ +#define LOG_CATEGORY LOGC_DEVRES +#include <common.h> #include <linux/compat.h> #include <linux/kernel.h>
@@ -46,8 +48,8 @@ static void set_node_dbginfo(struct devres *dr, const char *name, size_t size)static void devres_log (struct udevice *dev,struct devres *dr,const char *op) { -printf ("%s: DEVRES %3s %p %s (%lu bytes)\n" , - dev->name, op, dr, dr->name, (unsigned long )dr->size); +log_debug ("%s: DEVRES %3s %p %s (%lu bytes)\n" , dev->name, op, dr, + dr->name, (unsigned long )dr->size); }#else /* CONFIG_DEBUG_DEVRES */ #define set_node_dbginfo (dr, n, s) do {} while (0)
include/log.h +1 -0
@@ -49,6 +49,7 @@ enum log_category_t {LOGC_ALLOC ,/* Memory allocation */ LOGC_SANDBOX ,/* Related to the sandbox board */ LOGC_BLOBLIST ,/* Bloblist */ +LOGC_DEVRES ,/* Device resources (devres_... functions) */ LOGC_COUNT ,/* Number of log categories */ LOGC_END, /* Sentinel value for a list of log categories */
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 15/19 dm: devres: Convert to use logging
At present when CONFIG_DEBUG_DEVRES is enabled, U-Boot prints log messages to the console with every devres allocation/free event. This causes most tests to fail since the console output is not as expected.
In particular this prevents us from adding a device to sandbox which uses devres in its bind method.
Move devres over to use U-Boot's logging feature instead, and add a new category for devres.
---
drivers/core/devres.c | 6 ++++-- include/log.h | 1 + 2 files changed, 5 insertions(+), 2 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 16/19 dm: test: Add a test driver for devres
In reply to Simon GlassAdd a driver which does devres allocations so that we can write tests for devres.
---
arch/sandbox/dts/test.dts | 4 ++++ include/dm/uclass-id.h | 1 + include/test/test.h | 9 ++++++++ test/dm/test-fdt.c | 47 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+)
arch/sandbox/dts/test.dts +4 -0
@@ -201,6 +201,10 @@ compatible = "denx,u-boot-fdt-test1"; }; + devres-test { + compatible = "denx,u-boot-devres-test"; + }; + clocks { clk_fixed: clk-fixed { compatible = "fixed-clock";
include/dm/uclass-id.h +1 -0
@@ -19,6 +19,7 @@ enum uclass_id {UCLASS_TEST_BUS ,UCLASS_TEST_PROBE ,UCLASS_TEST_DUMMY , +UCLASS_TEST_DEVRES ,UCLASS_SPI_EMUL ,/* sandbox SPI device emulator */ UCLASS_I2C_EMUL ,/* sandbox I2C device emulator */ UCLASS_I2C_EMUL_PARENT, /* parent for I2C device emulators */
include/test/test.h +9 -0
@@ -46,5 +46,14 @@ struct unit_test { .func = _name, \ } +/* Sizes for devres tests */ +enum { +TEST_DEVRES_SIZE =100 , +TEST_DEVRES_COUNT =10 , +TEST_DEVRES_TOTAL =TEST_DEVRES_SIZE *TEST_DEVRES_COUNT , + +/* A different size */ +TEST_DEVRES_SIZE2 =15 , +};#endif /* __TEST_TEST_H */
test/dm/test-fdt.c +47 -0
53 lines not on this page · raw message
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 16/19 dm: test: Add a test driver for devres
Add a driver which does devres allocations so that we can write tests for devres.
---
arch/sandbox/dts/test.dts | 4 ++++ include/dm/uclass-id.h | 1 + include/test/test.h | 9 ++++++++ test/dm/test-fdt.c | 47 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 17/19 dm: devres: Add tests
In reply to Simon GlassThe devres functionality has very few users in U-Boot, but it still should have tests. Add a few basic tests of the main functions.
---
drivers/core/devres.c | 13 +++ include/dm/devres.h | 20 +++++ test/dm/Makefile | 1 + test/dm/devres.c | 178 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+) create mode 100644 test/dm/devres.c
drivers/core/devres.c +13 -0
@@ -223,6 +223,19 @@ void dm_dump_devres(void)if (root) dump_resources(root, 0); } + +void devres_get_stats (const struct udevice *dev,struct devres_stats *stats) +{ +struct devres *dr; + + stats->allocs =0 ; + stats->total_size =0 ; +list_for_each_entry (dr, &dev->devres_head, entry) { + stats->allocs++; + stats->total_size += dr->size; + } +} +#endif /*
include/dm/devres.h +20 -0
@@ -15,6 +15,17 @@typedef void (*dr_release_t )(struct udevice *dev,void *res);typedef int (*dr_match_t )(struct udevice *dev,void *res,void *match_data); +/** +* struct devres_stats - Information about devres allocations for a device +* +* @allocs: Number of allocations +* @total_size: Total size of allocations in bytes +*/ +struct devres_stats { +int allocs; +int total_size; +}; +#ifdef CONFIG_DEVRES #ifdef CONFIG_DEBUG_DEVRES
@@ -189,6 +200,9 @@ static inline void *devm_kcalloc(struct udevice *dev,*/ void devm_kfree (struct udevice *dev,void *ptr); +/* Get basic stats on allocations */ +void devres_get_stats (const struct udevice *dev,struct devres_stats *stats); +#else /* ! CONFIG_DEVRES */ static inline void *devres_alloc (dr_release_t release,size_t size,gfp_t gfp)
@@ -265,5 +279,11 @@ static inline void devm_kfree(struct udevice *dev, void *ptr) {kfree (ptr); } + +static inline void devres_get_stats (const struct udevice *dev, +struct devres_stats *stats) +{ +} +#endif /* DEVRES */ #endif /* _DM_DEVRES_H */
test/dm/Makefile +1 -0
@@ -18,6 +18,7 @@ obj-$(CONFIG_BLK) += blk.o obj-$(CONFIG_BOARD) += board.o obj-$(CONFIG_DM_BOOTCOUNT) += bootcount.o obj-$(CONFIG_CLK) += clk.o clk_ccf.o +obj-$(CONFIG_DEVRES) += devres.o obj-$(CONFIG_VIDEO_MIPI_DSI) += dsi_host.o obj-$(CONFIG_DM_ETH) += eth.o obj-$(CONFIG_FIRMWARE) += firmware.o
test/dm/devres.c new file +178 -0
178 lines not on this page · raw message
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 17/19 dm: devres: Add tests
The devres functionality has very few users in U-Boot, but it still should have tests. Add a few basic tests of the main functions.
---
drivers/core/devres.c | 13 +++ include/dm/devres.h | 20 +++++ test/dm/Makefile | 1 + test/dm/devres.c | 178 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+) create mode 100644 test/dm/devres.c
Applied to u-boot-dm, thanks!
PATCH 18/19 dm: devres: Use an enum for the allocation phase
In reply to Simon GlassAt present we only support two phases where devres can be used:
bind and probe. This is handled with a boolean. We want to add a new phase (platdata), so change this to an enum.
---
drivers/core/devres.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/core/devres.c +16 -6
@@ -17,12 +17,21 @@#include <dm/root.h> #include <dm/util.h> +/** enum devres_phase - Shows where resource was allocated +* +* DEVRES_PHASE_BIND: In the bind() method +* DEVRES_PHASE_PROBE: In the probe() method +*/ +enum devres_phase { +DEVRES_PHASE_BIND , +DEVRES_PHASE_PROBE , +}; +/** * struct devres - Bookkeeping info for managed device resource * @entry: List to associate this structure with a device * @release: Callback invoked when this resource is released -* @probe: Flag to show when this resource was allocated -(true = probe, false = bind) +* @probe: Show where this resource was allocated * @name: Name of release function * @size: Size of resource data * @data: Resource data
@@ -30,7 +39,7 @@struct devres {struct list_head entry;dr_release_t release; -bool probe; +enum devres_phase phase;#ifdef CONFIG_DEBUG_DEVRES const char *name;size_t size;
@@ -93,7 +102,8 @@ void devres_add(struct udevice *dev, void *res)devres_log (dev, dr,"ADD" );assert_noisy (list_empty (&dr->entry)); - dr->probe = dev->flags &DM_FLAG_BOUND ? true : false; + dr->phase = dev->flags &DM_FLAG_BOUND ?DEVRES_PHASE_PROBE : +DEVRES_PHASE_BIND ;list_add_tail (&dr->entry, &dev->devres_head); }
@@ -179,7 +189,7 @@ static void release_nodes(struct udevice *dev, struct list_head *head,struct devres *dr, *tmp;list_for_each_entry_safe_reverse (dr, tmp, head, entry) { -if (probe_only && !dr->probe) +if (probe_only && dr->phase !=DEVRES_PHASE_PROBE )break ;devres_log (dev, dr,"REL" ); dr->release (dev, dr->data);
@@ -209,7 +219,7 @@ static void dump_resources(struct udevice *dev, int depth)list_for_each_entry (dr, &dev->devres_head, entry)printf (" %p (%lu byte) %s %s\n" , dr, (unsigned long )dr->size, dr->name, - dr->probe ?"PROBE" :"BIND" ); + dr->phase ==DEVRES_PHASE_PROBE ?"PROBE" :"BIND" );list_for_each_entry (child, &dev->child_head, sibling_node)dump_resources (child, depth +1 );
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 18/19 dm: devres: Use an enum for the allocation phase
At present we only support two phases where devres can be used:
bind and probe. This is handled with a boolean. We want to add a new phase (platdata), so change this to an enum.
---
drivers/core/devres.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 19/19 dm: devres: Add a new OFDATA phase
In reply to Simon GlassSince the ofdata_to_platdata() method can allocate resources, add it as a new devres phase.
---
drivers/core/devres.c | 18 +++++++++++++----- include/test/test.h | 3 ++- test/dm/devres.c | 14 +++++++++++--- test/dm/test-fdt.c | 11 +++++++++++ 4 files changed, 37 insertions(+), 9 deletions(-)
drivers/core/devres.c +13 -5
@@ -20,10 +20,12 @@/** enum devres_phase - Shows where resource was allocated * * DEVRES_PHASE_BIND: In the bind() method +* DEVRES_PHASE_OFDATA: In the ofdata_to_platdata() method * DEVRES_PHASE_PROBE: In the probe() method */ enum devres_phase {DEVRES_PHASE_BIND , +DEVRES_PHASE_OFDATA ,DEVRES_PHASE_PROBE , };
@@ -102,8 +104,12 @@ void devres_add(struct udevice *dev, void *res)devres_log (dev, dr,"ADD" );assert_noisy (list_empty (&dr->entry)); - dr->phase = dev->flags &DM_FLAG_BOUND ?DEVRES_PHASE_PROBE : -DEVRES_PHASE_BIND ; +if (dev->flags &DM_FLAG_PLATDATA_VALID ) + dr->phase =DEVRES_PHASE_PROBE ; +else if (dev->flags &DM_FLAG_BOUND ) + dr->phase =DEVRES_PHASE_OFDATA ; +else + dr->phase =DEVRES_PHASE_BIND ;list_add_tail (&dr->entry, &dev->devres_head); }
@@ -184,12 +190,12 @@ int devres_release(struct udevice *dev, dr_release_t release, }static void release_nodes (struct udevice *dev,struct list_head *head, -bool probe_only) +bool probe_and_ofdata_only) {struct devres *dr, *tmp;list_for_each_entry_safe_reverse (dr, tmp, head, entry) { -if (probe_only && dr->phase !=DEVRES_PHASE_PROBE ) +if (probe_and_ofdata_only && dr->phase ==DEVRES_PHASE_BIND )break ;devres_log (dev, dr,"REL" ); dr->release (dev, dr->data);
@@ -209,6 +215,8 @@ void devres_release_all(struct udevice *dev) }#ifdef CONFIG_DEBUG_DEVRES +static char *const devres_phase_name[] = {"BIND" ,"OFDATA" ,"PROBE" }; +static void dump_resources (struct udevice *dev,int depth) {struct devres *dr;
@@ -219,7 +227,7 @@ static void dump_resources(struct udevice *dev, int depth)list_for_each_entry (dr, &dev->devres_head, entry)printf (" %p (%lu byte) %s %s\n" , dr, (unsigned long )dr->size, dr->name, - dr->phase ==DEVRES_PHASE_PROBE ?"PROBE" :"BIND" ); + devres_phase_name[dr->phase]);list_for_each_entry (child, &dev->child_head, sibling_node)dump_resources (child, depth +1 );
include/test/test.h +2 -1
@@ -52,8 +52,9 @@ enum {TEST_DEVRES_COUNT =10 ,TEST_DEVRES_TOTAL =TEST_DEVRES_SIZE *TEST_DEVRES_COUNT , -/* A different size */ +/* A few different sizes */ TEST_DEVRES_SIZE2 =15 , + TEST_DEVRES_SIZE3 = 37, };#endif /* __TEST_TEST_H */
test/dm/devres.c +11 -3
@@ -140,6 +140,7 @@ static int dm_test_devres_kcalloc(struct unit_test_state *uts) }DM_TEST (dm_test_devres_kcalloc,DM_TESTF_SCAN_PDATA ); +/* Test devres releases resources automatically as expected */ static int dm_test_devres_phase (struct unit_test_state *uts) {struct devres_stats stats;
@@ -154,14 +155,21 @@ static int dm_test_devres_phase(struct unit_test_state *uts)ut_asserteq (1 , stats.allocs);ut_asserteq (TEST_DEVRES_SIZE , stats.total_size); +/* Getting platdata should add one allocation */ +ut_assertok (device_ofdata_to_platdata (dev)); +devres_get_stats (dev, &stats); +ut_asserteq (2 , stats.allocs); +ut_asserteq (TEST_DEVRES_SIZE +TEST_DEVRES_SIZE3 , stats.total_size); +/* Probing the device should add one allocation */ ut_assertok (uclass_first_device (UCLASS_TEST_DEVRES , &dev));ut_assert (dev !=NULL );devres_get_stats (dev, &stats); -ut_asserteq (2 , stats.allocs); -ut_asserteq (TEST_DEVRES_SIZE +TEST_DEVRES_SIZE2 , stats.total_size); +ut_asserteq (3 , stats.allocs); +ut_asserteq (TEST_DEVRES_SIZE +TEST_DEVRES_SIZE2 +TEST_DEVRES_SIZE3 , + stats.total_size); -/* Removing the device should drop one allocation */ +/* Removing the device should drop both those allocations */ device_remove (dev,DM_REMOVE_NORMAL );devres_get_stats (dev, &stats);ut_asserteq (1 , stats.allocs);
test/dm/test-fdt.c +11 -0
@@ -159,6 +159,7 @@ struct dm_testdevres_pdata {struct dm_testdevres_priv {void *ptr; +void *ptr_ofdata; };static int testdevres_drv_bind (struct udevice *dev)
@@ -170,6 +171,15 @@ static int testdevres_drv_bind(struct udevice *dev)return 0 ; } +static int testdevres_drv_ofdata_to_platdata (struct udevice *dev) +{ +struct dm_testdevres_priv *priv =dev_get_priv (dev); + + priv->ptr_ofdata =devm_kmalloc (dev,TEST_DEVRES_SIZE3 ,0 ); + +return 0 ; +} +static int testdevres_drv_probe (struct udevice *dev) {struct dm_testdevres_priv *priv =dev_get_priv (dev);
@@ -189,6 +199,7 @@ U_BOOT_DRIVER(testdevres_drv) = { .of_match = testdevres_ids, .id = UCLASS_TEST_DEVRES, .bind = testdevres_drv_bind, + .ofdata_to_platdata = testdevres_drv_ofdata_to_platdata, .probe = testdevres_drv_probe, .platdata_auto_alloc_size = sizeof(struct dm_testdevres_pdata), .priv_auto_alloc_size = sizeof(struct dm_testdevres_priv),
Signature · 1 line
2.24.1.735.g03f4e72817-goog
PATCH 19/19 dm: devres: Add a new OFDATA phase
Since the ofdata_to_platdata() method can allocate resources, add it as a new devres phase.
---
drivers/core/devres.c | 18 +++++++++++++----- include/test/test.h | 3 ++- test/dm/devres.c | 14 +++++++++++--- test/dm/test-fdt.c | 11 +++++++++++ 4 files changed, 37 insertions(+), 9 deletions(-)
Applied to u-boot-dm, thanks!