/** 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,
};
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);
}
}
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);
}
#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;
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);
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 */
}
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;
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);
struct dm_testdevres_priv {
void *ptr;
+ void *ptr_ofdata;
};
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);
.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),