debug("dm_init() failed: %d\n", ret);
return ret;
}
- ret = dm_scan_platdata();
+ ret = dm_scan_platdata(false);
if (ret) {
debug("dm_scan_platdata() failed: %d\n", ret);
return ret;
}
#ifdef CONFIG_OF_CONTROL
- ret = dm_scan_fdt(gd->fdt_blob);
+ ret = dm_scan_fdt(gd->fdt_blob, false);
if (ret) {
debug("dm_scan_fdt() failed: %d\n", ret);
return ret;
You should see something like this:
<...U-Boot banner...>
- Running 12 driver model tests
+ Running 14 driver model tests
Test: dm_test_autobind
Test: dm_test_autoprobe
Test: dm_test_children
Test: dm_test_fdt
+ Test: dm_test_fdt_pre_reloc
Test: dm_test_gpio
sandbox_gpio: sb_gpio_get_value: error: offset 4 not reserved
Test: dm_test_leak
- Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c
- Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c
Test: dm_test_lifecycle
Test: dm_test_operations
Test: dm_test_ordering
Test: dm_test_platdata
+ Test: dm_test_pre_reloc
Test: dm_test_remove
Test: dm_test_uclass
Failures: 0
-(You can add '#define DEBUG' as suggested to check for memory leaks)
-
What is going on?
-----------------
- Implemented a GPIO system, trying to keep it simple
+Pre-Relocation Support
+----------------------
+
+For pre-relocation we simply call the driver model init function. Only
+drivers marked with DM_FLAG_PRE_RELOC or the device tree
+'u-boot,dm-pre-reloc' flag are initialised prior to relocation. This helps
+to reduce the driver model overhead.
+
+Then post relocation we throw that away and re-init driver model again.
+For drivers which require some sort of continuity between pre- and
+post-relocation devices, we can provide access to the pre-relocation
+device pointers, but this is not currently implemented (the root device
+pointer is saved but not made available through the driver model API).
+
+
Things to punt for later
------------------------
- SPL support - this will have to be present before many drivers can be
converted, but it seems like we can add it once we are happy with the
core implementation.
-- Pre-relocation support - similar story
-That is not to say that no thinking has gone into these - in fact there
+That is not to say that no thinking has gone into this - in fact there
is quite a lot there. However, getting these right is non-trivial and
there is a high cost associated with going down the wrong path.
For SPL, it may be possible to fit in a simplified driver model with only
bind and probe methods, to reduce size.
-For pre-relocation we can simply call the driver model init function. Then
-post relocation we throw that away and re-init driver model again. For drivers
-which require some sort of continuity between pre- and post-relocation
-devices, we can provide access to the pre-relocation device pointers.
-
Uclasses are statically numbered at compile time. It would be possible to
change this to dynamic numbering, but then we would require some sort of
lookup service, perhaps searching by name. This is slightly less efficient
return ret;
}
-int device_bind_by_name(struct udevice *parent, const struct driver_info *info,
- struct udevice **devp)
+int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
+ const struct driver_info *info, struct udevice **devp)
{
struct driver *drv;
drv = lists_driver_lookup_name(info->name);
if (!drv)
return -ENOENT;
+ if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
+ return -EPERM;
return device_bind(parent, drv, info->name, (void *)info->platdata,
-1, devp);
return NULL;
}
-int lists_bind_drivers(struct udevice *parent)
+int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
{
struct driver_info *info =
ll_entry_start(struct driver_info, driver_info);
int ret;
for (entry = info; entry != info + n_ents; entry++) {
- ret = device_bind_by_name(parent, entry, &dev);
- if (ret) {
+ ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
+ if (ret && ret != -EPERM) {
dm_warn("No match for driver '%s'\n", entry->name);
if (!result || ret != -ENOENT)
result = ret;
}
INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
- ret = device_bind_by_name(NULL, &root_info, &DM_ROOT_NON_CONST);
+ ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
if (ret)
return ret;
ret = device_probe(DM_ROOT_NON_CONST);
return 0;
}
-int dm_scan_platdata(void)
+int dm_scan_platdata(bool pre_reloc_only)
{
int ret;
- ret = lists_bind_drivers(DM_ROOT_NON_CONST);
+ ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
if (ret == -ENOENT) {
dm_warn("Some drivers were not found\n");
ret = 0;
}
#ifdef CONFIG_OF_CONTROL
-int dm_scan_fdt(const void *blob)
+int dm_scan_fdt(const void *blob, bool pre_reloc_only)
{
int offset = 0;
int ret = 0, err;
do {
offset = fdt_next_node(blob, offset, &depth);
if (offset > 0 && depth == 1) {
+ if (pre_reloc_only &&
+ !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
+ continue;
err = lists_bind_fdt(gd->dm_root, blob, offset);
if (err && !ret)
ret = err;
* tree.
*
* @parent: Pointer to device's parent
+ * @pre_reloc_only: If true, bind the driver only if its DM_INIT_F flag is set.
+ * If false bind the driver always.
* @info: Name and platdata for this device
* @devp: Returns a pointer to the bound device
* @return 0 if OK, -ve on error
*/
-int device_bind_by_name(struct udevice *parent, const struct driver_info *info,
- struct udevice **devp);
+int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
+ const struct driver_info *info, struct udevice **devp);
/**
* device_probe() - Probe a device, activating it
/* DM is responsible for allocating and freeing platdata */
#define DM_FLAG_ALLOC_PDATA (1 << 1)
+/* DM should init this device prior to relocation */
+#define DM_FLAG_PRE_RELOC (1 << 2)
+
/**
* struct udevice - An instance of a driver
*
* ops: Driver-specific operations. This is typically a list of function
* pointers defined by the driver, to implement driver functions required by
* the uclass.
+ * @flags: driver flags - see DM_FLAGS_...
*/
struct driver {
char *name;
int priv_auto_alloc_size;
int platdata_auto_alloc_size;
const void *ops; /* driver-specific operations */
+ uint32_t flags;
};
/* Declare a new U-Boot driver */
* @early_only: If true, bind only drivers with the DM_INIT_F flag. If false
* bind all drivers.
*/
-int lists_bind_drivers(struct udevice *parent);
+int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only);
/**
* lists_bind_fdt() - bind a device tree node
*
* This scans all available platdata and creates drivers for each
*
+ * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
+ * flag. If false bind all drivers.
* @return 0 if OK, -ve on error
*/
-int dm_scan_platdata(void);
+int dm_scan_platdata(bool pre_reloc_only);
/**
* dm_scan_fdt() - Scan the device tree and bind drivers
* This scans the device tree and creates a driver for each node
*
* @blob: Pointer to device tree blob
+ * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
+ * flag. If false bind all drivers.
* @return 0 if OK, -ve on error
*/
-int dm_scan_fdt(const void *blob);
+int dm_scan_fdt(const void *blob, bool pre_reloc_only);
/**
* dm_init() - Initialise Driver Model structures
TEST_INTVAL2 = 3,
TEST_INTVAL3 = 6,
TEST_INTVAL_MANUAL = 101112,
+ TEST_INTVAL_PRE_RELOC = 7,
};
static const struct dm_test_pdata test_pdata[] = {
.ping_add = TEST_INTVAL_MANUAL,
};
+static const struct dm_test_pdata test_pdata_pre_reloc = {
+ .ping_add = TEST_INTVAL_PRE_RELOC,
+};
+
U_BOOT_DEVICE(dm_test_info1) = {
.name = "test_drv",
.platdata = &test_pdata[0],
.platdata = &test_pdata_manual,
};
+static struct driver_info driver_info_pre_reloc = {
+ .name = "test_pre_reloc_drv",
+ .platdata = &test_pdata_manual,
+};
+
/* Test that binding with platdata occurs correctly */
static int dm_test_autobind(struct dm_test_state *dms)
{
ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
- ut_assertok(dm_scan_platdata());
+ ut_assertok(dm_scan_platdata(false));
/* We should have our test class now at least, plus more children */
ut_assert(1 < list_count_items(&gd->uclass_root));
memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
- ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
+ ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
&dev));
ut_assert(dev);
ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
int pingret;
- ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
+ ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
&dev));
ut_assert(dev);
/* Bind two new devices (numbers 4 and 5) */
- ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
+ ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
&dev_penultimate));
ut_assert(dev_penultimate);
- ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
+ ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
&dev_last));
ut_assert(dev_last);
ut_assert(dev_last == test_dev);
/* Add back the original device 3, now in position 5 */
- ut_assertok(device_bind_by_name(dms->root, &driver_info_manual, &dev));
+ ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+ &dev));
ut_assert(dev);
/* Try ping */
if (!start.uordblks)
puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
- ut_assertok(dm_scan_platdata());
- ut_assertok(dm_scan_fdt(gd->fdt_blob));
+ ut_assertok(dm_scan_platdata(false));
+ ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
/* Scanning the uclass is enough to probe all the devices */
for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
for (i = 0; i < count; i++) {
struct dm_test_pdata *pdata;
- ut_assertok(device_bind_by_name(parent, &driver_info_manual,
- &dev));
+ ut_assertok(device_bind_by_name(parent, false,
+ &driver_info_manual, &dev));
pdata = calloc(1, sizeof(*pdata));
pdata->ping_add = key + i;
dev->platdata = pdata;
return 0;
}
DM_TEST(dm_test_children, 0);
+
+/* Test that pre-relocation devices work as expected */
+static int dm_test_pre_reloc(struct dm_test_state *dms)
+{
+ struct udevice *dev;
+
+ /* The normal driver should refuse to bind before relocation */
+ ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
+ &driver_info_manual, &dev));
+
+ /* But this one is marked pre-reloc */
+ ut_assertok(device_bind_by_name(dms->root, true,
+ &driver_info_pre_reloc, &dev));
+
+ return 0;
+}
+DM_TEST(dm_test_pre_reloc, 0);
.remove = test_manual_remove,
.unbind = test_manual_unbind,
};
+
+U_BOOT_DRIVER(test_pre_reloc_drv) = {
+ .name = "test_pre_reloc_drv",
+ .id = UCLASS_TEST,
+ .ops = &test_manual_ops,
+ .bind = test_manual_bind,
+ .probe = test_manual_probe,
+ .remove = test_manual_remove,
+ .unbind = test_manual_unbind,
+ .flags = DM_FLAG_PRE_RELOC,
+};
int ret;
int i;
- ret = dm_scan_fdt(gd->fdt_blob);
+ ret = dm_scan_fdt(gd->fdt_blob, false);
ut_assert(!ret);
ret = uclass_get(UCLASS_TEST_FDT, &uc);
return 0;
}
DM_TEST(dm_test_fdt, 0);
+
+static int dm_test_fdt_pre_reloc(struct dm_test_state *dms)
+{
+ struct uclass *uc;
+ int ret;
+
+ ret = dm_scan_fdt(gd->fdt_blob, true);
+ ut_assert(!ret);
+
+ ret = uclass_get(UCLASS_TEST_FDT, &uc);
+ ut_assert(!ret);
+
+ /* These is only one pre-reloc device */
+ ut_asserteq(1, list_count_items(&uc->dev_head));
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_pre_reloc, 0);
ut_assertok(dm_test_init(dms));
if (test->flags & DM_TESTF_SCAN_PDATA)
- ut_assertok(dm_scan_platdata());
+ ut_assertok(dm_scan_platdata(false));
if (test->flags & DM_TESTF_PROBE_TEST)
ut_assertok(do_autoprobe(dms));
if (test->flags & DM_TESTF_SCAN_FDT)
- ut_assertok(dm_scan_fdt(gd->fdt_blob));
+ ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
if (test->func(dms))
break;
#address-cells = <1>;
#size-cells = <0>;
+ aliases {
+ console = &uart0;
+ };
+
+ uart0: serial {
+ compatible = "sandbox,serial";
+ u-boot,dm-pre-reloc;
+ };
+
a-test {
reg = <0>;
compatible = "denx,u-boot-fdt-test";
ping-expect = <0>;
ping-add = <0>;
+ u-boot,dm-pre-reloc;
};
junk {