]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
dm: Allow drivers to be marked 'before relocation'
authorSimon Glass <sjg@chromium.org>
Wed, 23 Jul 2014 12:55:03 +0000 (06:55 -0600)
committerSimon Glass <sjg@chromium.org>
Wed, 23 Jul 2014 13:07:24 +0000 (14:07 +0100)
Driver model currently only operates after relocation is complete. In this
state U-Boot typically has a small amount of memory available. In adding
support for driver model prior to relocation we must try to use as little
memory as possible.

In addition, on some machines the memory has not be inited and/or the CPU
is not running at full speed or the data cache is off. These can reduce
execution performance, so the less initialisation that is done before
relocation the better.

An immediately-obvious improvement is to only initialise drivers which are
actually going to be used before relocation. On many boards the only such
driver is a serial UART, so this provides a very large potential benefit.

Allow drivers to mark themselves as 'pre-reloc' which means that they will
be initialised prior to relocation. This can be done either with a driver
flag or with a 'dm,pre-reloc' device tree property.

To support this, the various dm scanning function now take a 'pre_reloc_only'
parameter which indicates that only drivers marked pre-reloc should be
bound.

Signed-off-by: Simon Glass <sjg@chromium.org>
14 files changed:
common/board_r.c
doc/driver-model/README.txt
drivers/core/device.c
drivers/core/lists.c
drivers/core/root.c
include/dm/device-internal.h
include/dm/device.h
include/dm/lists.h
include/dm/root.h
test/dm/core.c
test/dm/test-driver.c
test/dm/test-fdt.c
test/dm/test-main.c
test/dm/test.dts

index 2298ba5761dba9a39ed5bc8ee2d5528a0bdc0ead..fbabc898d8c39b48ecb0c795e9620d0234a52606 100644 (file)
@@ -280,13 +280,13 @@ static int initr_dm(void)
                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;
index 22c3fcb6eff81aedb062d197e97f5ab089eb3f50..907ff671002d7b69fc1616186ade354f61d48e08 100644 (file)
@@ -95,26 +95,24 @@ are provided in test/dm. To run them, try:
 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?
 -----------------
@@ -538,26 +536,35 @@ dealing with this might not be worth it.
 - 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
index c73c339d18ca7c7056fb5d6de5ae1808ae6ebcc0..86b9ff891117672ee1ee565892b775b097d1e164 100644 (file)
@@ -129,14 +129,16 @@ fail_bind:
        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);
index 87164a5cf9a06449a3dfa0cce1a3701f2b7f4913..5f1c85fd6e41353aa9866369e50eed674e06c6f3 100644 (file)
@@ -62,7 +62,7 @@ struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
        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);
@@ -73,8 +73,8 @@ int lists_bind_drivers(struct udevice *parent)
        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;
index 346d462470e34716f2b3c8f930cce4d938c3f19d..ce4eef30540bf4e1b7bfbf0087b29e1aad1d14d3 100644 (file)
@@ -46,7 +46,7 @@ int dm_init(void)
        }
        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);
@@ -64,11 +64,11 @@ int dm_uninit(void)
        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;
@@ -80,7 +80,7 @@ int dm_scan_platdata(void)
 }
 
 #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;
@@ -89,6 +89,9 @@ int dm_scan_fdt(const void *blob)
        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;
index 26e5cf530ebc8b6bb81ec34c72abc0a4ecd268f2..7005d03d08f5d96adcd3f28c2a11f7f9e318ec44 100644 (file)
@@ -45,12 +45,14 @@ int device_bind(struct udevice *parent, struct driver *drv,
  * 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
index ae75a3f54db53c927fb81159b42f7870c81b64f7..46799795e48730c99606b818237cda4e3e815dac 100644 (file)
@@ -23,6 +23,9 @@ struct driver_info;
 /* 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
  *
@@ -117,6 +120,7 @@ struct udevice_id {
  * 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;
@@ -130,6 +134,7 @@ struct driver {
        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 */
index 49d87e617687465d289b88e9a002f0ac4ea14223..87a3af59c2e33b355fb4980debe11ecc854f6a1b 100644 (file)
@@ -42,7 +42,7 @@ struct uclass_driver *lists_uclass_lookup(enum uclass_id id);
  * @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
index 35818b1deedcc895c38795b2d2237bae30d18a4b..d37b452eef1b3c78d9052914536a3b96511c164e 100644 (file)
@@ -26,9 +26,11 @@ struct udevice *dm_root(void);
  *
  * 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
@@ -36,9 +38,11 @@ int dm_scan_platdata(void);
  * 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
index 8c187806ec581b1a21de14436772d658b12ecccd..24e0b6b898dbb7c1aed601b2ce5bf6a601a57909 100644 (file)
@@ -25,6 +25,7 @@ enum {
        TEST_INTVAL2            = 3,
        TEST_INTVAL3            = 6,
        TEST_INTVAL_MANUAL      = 101112,
+       TEST_INTVAL_PRE_RELOC   = 7,
 };
 
 static const struct dm_test_pdata test_pdata[] = {
@@ -37,6 +38,10 @@ static const struct dm_test_pdata test_pdata_manual = {
        .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],
@@ -57,6 +62,11 @@ static struct driver_info driver_info_manual = {
        .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)
 {
@@ -71,7 +81,7 @@ 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));
@@ -181,7 +191,7 @@ static int dm_test_lifecycle(struct dm_test_state *dms)
 
        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]
@@ -232,15 +242,15 @@ static int dm_test_ordering(struct dm_test_state *dms)
        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);
 
@@ -255,7 +265,8 @@ static int dm_test_ordering(struct dm_test_state *dms)
        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 */
@@ -375,8 +386,8 @@ static int dm_test_leak(struct dm_test_state *dms)
                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++) {
@@ -444,8 +455,8 @@ static int create_children(struct dm_test_state *dms, struct udevice *parent,
        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;
@@ -542,3 +553,20 @@ static int dm_test_children(struct dm_test_state *dms)
        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);
index 0f1a37b36e52faabdba5b2a78e28a33852600c4f..bc6a6e721d4922a524dc0c1affa77e98085741da 100644 (file)
@@ -144,3 +144,14 @@ U_BOOT_DRIVER(test_manual_drv) = {
        .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,
+};
index 0f505373a3be6c3d31ed03cbff37cfe36547a115..d284f7fa051ca805191c3b767c3aa8d5d0f3c297 100644 (file)
@@ -100,7 +100,7 @@ static int dm_test_fdt(struct dm_test_state *dms)
        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);
@@ -145,3 +145,21 @@ static int dm_test_fdt(struct dm_test_state *dms)
        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);
index fbdae688e09274842ecf197444caea76bc5e4caa..94ce72abfd5ced11313480ba736c88d65e01d862 100644 (file)
@@ -89,11 +89,11 @@ int dm_test_main(void)
                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;
index 74d236b495be752d0075552030da03d3595f2f8d..cd18a3107b321427b3d1fce0df0c46d6a63e42a3 100644 (file)
@@ -6,11 +6,21 @@
        #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 {