LCOV - code coverage report
Current view: top level - core/test - run-device.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 288 293 98.3 %
Date: 2024-01-02 21:04:04 Functions: 6 6 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  * Copyright 2012-2018 IBM Corp.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <skiboot.h>
       7                 :            : #include <stdlib.h>
       8                 :            : 
       9                 :            : /* Override this for testing. */
      10                 :            : #define is_rodata(p) fake_is_rodata(p)
      11                 :            : 
      12                 :            : char __rodata_start[16];
      13                 :            : #define __rodata_end (__rodata_start + sizeof(__rodata_start))
      14                 :            : 
      15                 :        347 : static inline bool fake_is_rodata(const void *p)
      16                 :            : {
      17                 :        347 :         return ((char *)p >= __rodata_start && (char *)p < __rodata_end);
      18                 :            : }
      19                 :            : 
      20                 :            : #define zalloc(bytes) calloc((bytes), 1)
      21                 :            : 
      22                 :            : #include "../device.c"
      23                 :            : #include <assert.h>
      24                 :            : #include "../../test/dt_common.c"
      25                 :            : const char *prop_to_fix[] = {"something", NULL};
      26                 :            : const char **props_to_fix(struct dt_node *node);
      27                 :            : 
      28                 :         10 : static void check_path(const struct dt_node *node, const char * expected_path)
      29                 :            : {
      30                 :            :         char * path;
      31                 :         10 :         path = dt_get_path(node);
      32                 :         10 :         if (strcmp(path, expected_path) != 0) {
      33                 :          0 :                 printf("check_path: expected %s, got %s\n", expected_path, path);
      34                 :            :         }
      35                 :         10 :         assert(strcmp(path, expected_path) == 0);
      36                 :         10 :         free(path);
      37                 :         10 : }
      38                 :            : 
      39                 :            : /* constructs a random nodes only device tree */
      40                 :         26 : static void build_tree(int max_depth, int min_depth, struct dt_node *parent)
      41                 :            : {
      42                 :            :         char name[64];
      43                 :            :         int i;
      44                 :            : 
      45                 :        111 :         for (i = 0; i < max_depth; i++) {
      46                 :            :                 struct dt_node *new;
      47                 :            : 
      48                 :         85 :                 snprintf(name, sizeof name, "prefix@%.8x", rand());
      49                 :            : 
      50                 :         85 :                 new = dt_new(parent, name);
      51                 :            : 
      52                 :         85 :                 if(max_depth > min_depth)
      53                 :         25 :                         build_tree(max_depth - 1, min_depth, new);
      54                 :            :         }
      55                 :         26 : }
      56                 :            : 
      57                 :        179 : static bool is_sorted(const struct dt_node *root)
      58                 :            : {
      59                 :        179 :         struct dt_node *end = list_tail(&root->children, struct dt_node, list);
      60                 :            :         struct dt_node *node;
      61                 :            : 
      62                 :        355 :         dt_for_each_child(root, node) {
      63                 :        176 :                 struct dt_node *next =
      64                 :        176 :                         list_entry(node->list.next, struct dt_node, list);
      65                 :            : 
      66                 :            :                 /* current node must be "less than" the next node */
      67                 :        176 :                 if (node != end && dt_cmp_subnodes(node, next) != -1) {
      68                 :          0 :                         printf("nodes '%s' and '%s' out of order\n",
      69                 :            :                                 node->name, next->name);
      70                 :            : 
      71                 :          0 :                         return false;
      72                 :            :                 }
      73                 :            : 
      74                 :        176 :                 if (!is_sorted(node))
      75                 :          0 :                         return false;
      76                 :            :         }
      77                 :            : 
      78                 :        179 :         return true;
      79                 :            : }
      80                 :            : 
      81                 :            : /*handler for phandle fixup test */
      82                 :          6 : const char **props_to_fix(struct dt_node *node)
      83                 :            : {
      84                 :            :         const struct dt_property *prop;
      85                 :            : 
      86                 :          6 :         prop = dt_find_property(node, "something");
      87                 :          6 :         if (prop)
      88                 :          2 :                 return prop_to_fix;
      89                 :            : 
      90                 :          4 :         return NULL;
      91                 :            : }
      92                 :            : 
      93                 :          1 : int main(void)
      94                 :            : {
      95                 :            :         struct dt_node *root, *other_root, *c1, *c2, *c2_c, *gc1, *gc2, *gc3, *ggc1, *ggc2;
      96                 :            :         struct dt_node *addrs, *addr1, *addr2;
      97                 :            :         struct dt_node *i, *subtree, *ev1, *ut1, *ut2;
      98                 :            :         const struct dt_property *p;
      99                 :            :         struct dt_property *p2;
     100                 :            :         unsigned int n;
     101                 :            :         char *s;
     102                 :            :         size_t sz;
     103                 :            :         u32 phandle, ev1_ph, new_prop_ph;
     104                 :            : 
     105                 :          1 :         root = dt_new_root("");
     106                 :          1 :         assert(!list_top(&root->properties, struct dt_property, list));
     107                 :          1 :         check_path(root, "/");
     108                 :            : 
     109                 :          1 :         c1 = dt_new_check(root, "c1");
     110                 :          1 :         assert(!list_top(&c1->properties, struct dt_property, list));
     111                 :          1 :         check_path(c1, "/c1");
     112                 :          1 :         assert(dt_find_by_name(root, "c1") == c1);
     113                 :          1 :         assert(dt_find_by_path(root, "/c1") == c1);
     114                 :          1 :         assert(dt_new(root, "c1") == NULL);
     115                 :            : 
     116                 :          1 :         c2 = dt_new(root, "c2");
     117                 :          1 :         c2_c = dt_new_check(root, "c2");
     118                 :          1 :         assert(c2 == c2_c);
     119                 :          1 :         assert(!list_top(&c2->properties, struct dt_property, list));
     120                 :          1 :         check_path(c2, "/c2");
     121                 :          1 :         assert(dt_find_by_name(root, "c2") == c2);
     122                 :          1 :         assert(dt_find_by_path(root, "/c2") == c2);
     123                 :            : 
     124                 :          1 :         gc1 = dt_new(c1, "gc1");
     125                 :          1 :         assert(!list_top(&gc1->properties, struct dt_property, list));
     126                 :          1 :         check_path(gc1, "/c1/gc1");
     127                 :          1 :         assert(dt_find_by_name(root, "gc1") == gc1);
     128                 :          1 :         assert(dt_find_by_path(root, "/c1/gc1") == gc1);
     129                 :            : 
     130                 :          1 :         gc2 = dt_new(c1, "gc2");
     131                 :          1 :         assert(!list_top(&gc2->properties, struct dt_property, list));
     132                 :          1 :         check_path(gc2, "/c1/gc2");
     133                 :          1 :         assert(dt_find_by_name(root, "gc2") == gc2);
     134                 :          1 :         assert(dt_find_by_path(root, "/c1/gc2") == gc2);
     135                 :            : 
     136                 :          1 :         gc3 = dt_new(c1, "gc3");
     137                 :          1 :         assert(!list_top(&gc3->properties, struct dt_property, list));
     138                 :          1 :         check_path(gc3, "/c1/gc3");
     139                 :          1 :         assert(dt_find_by_name(root, "gc3") == gc3);
     140                 :          1 :         assert(dt_find_by_path(root, "/c1/gc3") == gc3);
     141                 :            : 
     142                 :          1 :         ggc1 = dt_new(gc1, "ggc1");
     143                 :          1 :         assert(!list_top(&ggc1->properties, struct dt_property, list));
     144                 :          1 :         check_path(ggc1, "/c1/gc1/ggc1");
     145                 :          1 :         assert(dt_find_by_name(root, "ggc1") == ggc1);
     146                 :          1 :         assert(dt_find_by_path(root, "/c1/gc1/ggc1") == ggc1);
     147                 :            : 
     148                 :          1 :         addrs = dt_new(root, "addrs");
     149                 :          1 :         assert(!list_top(&addrs->properties, struct dt_property, list));
     150                 :          1 :         check_path(addrs, "/addrs");
     151                 :          1 :         assert(dt_find_by_name(root, "addrs") == addrs);
     152                 :          1 :         assert(dt_find_by_path(root, "/addrs") == addrs);
     153                 :            : 
     154                 :          1 :         addr1 = dt_new_addr(addrs, "addr", 0x1337);
     155                 :          1 :         assert(!list_top(&addr1->properties, struct dt_property, list));
     156                 :          1 :         check_path(addr1, "/addrs/addr@1337");
     157                 :          1 :         assert(dt_find_by_name(root, "addr@1337") == addr1);
     158                 :          1 :         assert(dt_find_by_name_addr(root, "addr", 0x1337) == addr1);
     159                 :          1 :         assert(dt_find_by_path(root, "/addrs/addr@1337") == addr1);
     160                 :          1 :         assert(dt_new_addr(addrs, "addr", 0x1337) == NULL);
     161                 :            : 
     162                 :          1 :         addr2 = dt_new_2addr(addrs, "2addr", 0xdead, 0xbeef);
     163                 :          1 :         assert(!list_top(&addr2->properties, struct dt_property, list));
     164                 :          1 :         check_path(addr2, "/addrs/2addr@dead,beef");
     165                 :          1 :         assert(dt_find_by_name(root, "2addr@dead,beef") == addr2);
     166                 :          1 :         assert(dt_find_by_path(root, "/addrs/2addr@dead,beef") == addr2);
     167                 :          1 :         assert(dt_new_2addr(addrs, "2addr", 0xdead, 0xbeef) == NULL);
     168                 :            : 
     169                 :            :         /* Test walking the tree, checking and setting values */
     170                 :         10 :         for (n = 0, i = dt_first(root); i; i = dt_next(root, i), n++) {
     171                 :          9 :                 assert(!list_top(&i->properties, struct dt_property, list));
     172                 :          9 :                 dt_add_property_cells(i, "visited", 1);
     173                 :            :         }
     174                 :          1 :         assert(n == 9);
     175                 :            : 
     176                 :         10 :         for (n = 0, i = dt_first(root); i; i = dt_next(root, i), n++) {
     177                 :          9 :                 p = list_top(&i->properties, struct dt_property, list);
     178                 :          9 :                 assert(strcmp(p->name, "visited") == 0);
     179                 :          9 :                 assert(p->len == sizeof(u32));
     180                 :          9 :                 assert(fdt32_to_cpu(*(u32 *)p->prop) == 1);
     181                 :            :         }
     182                 :          1 :         assert(n == 9);
     183                 :            : 
     184                 :            :         /* Test cells */
     185                 :          1 :         dt_add_property_cells(c1, "some-property", 1, 2, 3);
     186                 :          1 :         p = dt_find_property(c1, "some-property");
     187                 :          1 :         assert(p);
     188                 :          1 :         assert(strcmp(p->name, "some-property") == 0);
     189                 :          1 :         assert(p->len == sizeof(u32) * 3);
     190                 :          1 :         assert(fdt32_to_cpu(*(u32 *)p->prop) == 1);
     191                 :          1 :         assert(dt_prop_get_cell(c1, "some-property", 0) == 1);
     192                 :          1 :         assert(fdt32_to_cpu(*((u32 *)p->prop + 1)) == 2);
     193                 :          1 :         assert(dt_prop_get_cell(c1, "some-property", 1) == 2);
     194                 :          1 :         assert(fdt32_to_cpu(*((u32 *)p->prop + 2)) == 3);
     195                 :          1 :         assert(dt_prop_get_cell_def(c1, "some-property", 2, 42) == 3);
     196                 :            : 
     197                 :          1 :         assert(dt_prop_get_cell_def(c1, "not-a-property", 2, 42) == 42);
     198                 :            : 
     199                 :            :         /* Test u64s */
     200                 :          1 :         dt_add_property_u64s(c2, "some-property", (2LL << 33), (3LL << 33), (4LL << 33));
     201                 :          1 :         p = dt_find_property(c2, "some-property");
     202                 :          1 :         assert(p);
     203                 :          1 :         assert(p->len == sizeof(u64) * 3);
     204                 :          1 :         assert(fdt64_to_cpu(*(u64 *)p->prop) == (2LL << 33));
     205                 :          1 :         assert(fdt64_to_cpu(*((u64 *)p->prop + 1)) == (3LL << 33));
     206                 :          1 :         assert(fdt64_to_cpu(*((u64 *)p->prop + 2)) == (4LL << 33));
     207                 :            : 
     208                 :            :         /* Test u32/u64 get defaults */
     209                 :          1 :         assert(dt_prop_get_u32_def(c1, "u32", 42) == 42);
     210                 :          1 :         dt_add_property_cells(c1, "u32", 1337);
     211                 :          1 :         assert(dt_prop_get_u32_def(c1, "u32", 42) == 1337);
     212                 :          1 :         assert(dt_prop_get_u32(c1, "u32") == 1337);
     213                 :            : 
     214                 :          1 :         assert(dt_prop_get_u64_def(c1, "u64", (42LL << 42)) == (42LL << 42));
     215                 :          1 :         dt_add_property_u64s(c1, "u64", (1337LL << 42));
     216                 :          1 :         assert(dt_prop_get_u64_def(c1, "u64", (42LL << 42)) == (1337LL << 42));
     217                 :          1 :         assert(dt_prop_get_u64(c1, "u64") == (1337LL << 42));
     218                 :            : 
     219                 :            :         /* Test freeing a single node */
     220                 :          1 :         assert(!list_empty(&gc1->children));
     221                 :          1 :         dt_free(ggc1);
     222                 :          1 :         assert(list_empty(&gc1->children));
     223                 :            : 
     224                 :            :         /* Test rodata logic. */
     225                 :          1 :         assert(!is_rodata("hello"));
     226                 :          1 :         assert(is_rodata(__rodata_start));
     227                 :          1 :         strcpy(__rodata_start, "name");
     228                 :          1 :         ggc1 = dt_new(root, __rodata_start);
     229                 :          1 :         assert(ggc1->name == __rodata_start);
     230                 :            : 
     231                 :            :         /* Test string node. */
     232                 :          1 :         dt_add_property_string(ggc1, "somestring", "someval");
     233                 :          1 :         assert(dt_has_node_property(ggc1, "somestring", "someval"));
     234                 :          1 :         assert(!dt_has_node_property(ggc1, "somestrin", "someval"));
     235                 :          1 :         assert(!dt_has_node_property(ggc1, "somestring", "someva"));
     236                 :          1 :         assert(!dt_has_node_property(ggc1, "somestring", "somevale"));
     237                 :            : 
     238                 :            :         /* Test nstr, which allows for non-null-terminated inputs */
     239                 :          1 :         dt_add_property_nstr(ggc1, "nstring", "somevalue_long", 7);
     240                 :          1 :         assert(dt_has_node_property(ggc1, "nstring", "someval"));
     241                 :          1 :         assert(!dt_has_node_property(ggc1, "nstring", "someva"));
     242                 :          1 :         assert(!dt_has_node_property(ggc1, "nstring", "somevalue_long"));
     243                 :            : 
     244                 :            :         /* Test multiple strings */
     245                 :          1 :         dt_add_property_strings(ggc1, "somestrings",
     246                 :            :                                 "These", "are", "strings!");
     247                 :          1 :         p = dt_find_property(ggc1, "somestrings");
     248                 :          1 :         assert(p);
     249                 :          1 :         assert(p->len == sizeof(char) * (6 + 4 + 9));
     250                 :          1 :         s = (char *)p->prop;
     251                 :          1 :         assert(strcmp(s, "These") == 0);
     252                 :          1 :         assert(strlen(s) == 5);
     253                 :          1 :         s += 6;
     254                 :          1 :         assert(strcmp(s, "are") == 0);
     255                 :          1 :         assert(strlen(s) == 3);
     256                 :          1 :         s += 4;
     257                 :          1 :         assert(strcmp(s, "strings!") == 0);
     258                 :          1 :         assert(strlen(s) == 8);
     259                 :          1 :         s += 9;
     260                 :          1 :         assert(s == (char *)p->prop + p->len);
     261                 :          1 :         assert(dt_prop_find_string(p, "These"));
     262                 :            :         /* dt_prop_find_string is case insensitve */
     263                 :          1 :         assert(dt_prop_find_string(p, "ARE"));
     264                 :          1 :         assert(!dt_prop_find_string(p, "integers!"));
     265                 :            :         /* And always returns false for NULL properties */
     266                 :          1 :         assert(!dt_prop_find_string(NULL, "anything!"));
     267                 :            : 
     268                 :            :         /* Test more get/get_def varieties */
     269                 :          1 :         assert(dt_prop_get_def(c1, "does-not-exist", NULL) == NULL);
     270                 :          1 :         sz = 0xbad;
     271                 :          1 :         assert(dt_prop_get_def_size(c1, "does-not-exist", NULL, &sz) == NULL);
     272                 :          1 :         assert(sz == 0);
     273                 :          1 :         dt_add_property_string(c1, "another-property", "xyzzy");
     274                 :          1 :         assert(dt_prop_get_def(c1, "another-property", NULL) != NULL);
     275                 :          1 :         assert(strcmp(dt_prop_get(c1, "another-property"), "xyzzy") == 0);
     276                 :          1 :         n = 0xbad;
     277                 :          1 :         assert(dt_prop_get_def_size(c1, "another-property", NULL, &sz) != NULL);
     278                 :          1 :         assert(sz == strlen("xyzzy") + 1);
     279                 :            : 
     280                 :            :         /* Test resizing property. */
     281                 :          1 :         p = p2 = __dt_find_property(c1, "some-property");
     282                 :          1 :         assert(p);
     283                 :          1 :         n = p2->len;
     284                 :          3 :         while (p2 == p) {
     285                 :          2 :                 n *= 2;
     286                 :          2 :                 dt_resize_property(&p2, n);
     287                 :            :         }
     288                 :            : 
     289                 :          1 :         assert(dt_find_property(c1, "some-property") == p2);
     290                 :          1 :         list_check(&c1->properties, "properties after resizing");
     291                 :            : 
     292                 :          1 :         dt_del_property(c1, p2);
     293                 :          1 :         list_check(&c1->properties, "properties after delete");
     294                 :            : 
     295                 :            :         /* No leaks for valgrind! */
     296                 :          1 :         dt_free(root);
     297                 :            : 
     298                 :            :         /* Test compatible and chip id. */
     299                 :          1 :         root = dt_new_root("");
     300                 :            : 
     301                 :          1 :         c1 = dt_new(root, "chip1");
     302                 :          1 :         dt_add_property_cells(c1, "ibm,chip-id", 0xcafe);
     303                 :          1 :         assert(dt_get_chip_id(c1) == 0xcafe);
     304                 :          1 :         dt_add_property_strings(c1, "compatible",
     305                 :            :                                 "specific-fake-chip",
     306                 :            :                                 "generic-fake-chip");
     307                 :          1 :         assert(dt_node_is_compatible(c1, "specific-fake-chip"));
     308                 :          1 :         assert(dt_node_is_compatible(c1, "generic-fake-chip"));
     309                 :            : 
     310                 :          1 :         c2 = dt_new(root, "chip2");
     311                 :          1 :         dt_add_property_cells(c2, "ibm,chip-id", 0xbeef);
     312                 :          1 :         assert(dt_get_chip_id(c2) == 0xbeef);
     313                 :          1 :         dt_add_property_strings(c2, "compatible",
     314                 :            :                                 "specific-fake-bus",
     315                 :            :                                 "generic-fake-bus");
     316                 :            : 
     317                 :          1 :         gc1 = dt_new(c1, "coprocessor1");
     318                 :          1 :         dt_add_property_strings(gc1, "compatible",
     319                 :            :                                 "specific-fake-coprocessor");
     320                 :          1 :         gc2 = dt_new(gc1, "coprocessor2");
     321                 :          1 :         dt_add_property_strings(gc2, "compatible",
     322                 :            :                                 "specific-fake-coprocessor");
     323                 :          1 :         gc3 = dt_new(c1, "coprocessor3");
     324                 :          1 :         dt_add_property_strings(gc3, "compatible",
     325                 :            :                                 "specific-fake-coprocessor");
     326                 :            : 
     327                 :            : 
     328                 :          1 :         assert(dt_find_compatible_node(root, NULL, "generic-fake-bus") == c2);
     329                 :          1 :         assert(dt_find_compatible_node(root, c2, "generic-fake-bus") == NULL);
     330                 :            : 
     331                 :            :         /* we can find all compatible nodes */
     332                 :          1 :         assert(dt_find_compatible_node(c1, NULL, "specific-fake-coprocessor") == gc1);
     333                 :          1 :         assert(dt_find_compatible_node(c1, gc1, "specific-fake-coprocessor") == gc2);
     334                 :          1 :         assert(dt_find_compatible_node(c1, gc2, "specific-fake-coprocessor") == gc3);
     335                 :          1 :         assert(dt_find_compatible_node(c1, gc3, "specific-fake-coprocessor") == NULL);
     336                 :          1 :         assert(dt_find_compatible_node(root, NULL, "specific-fake-coprocessor") == gc1);
     337                 :          1 :         assert(dt_find_compatible_node(root, gc1, "specific-fake-coprocessor") == gc2);
     338                 :          1 :         assert(dt_find_compatible_node(root, gc2, "specific-fake-coprocessor") == gc3);
     339                 :          1 :         assert(dt_find_compatible_node(root, gc3, "specific-fake-coprocessor") == NULL);
     340                 :            : 
     341                 :            :         /* we can find the coprocessor once on the cpu */
     342                 :          1 :         assert(dt_find_compatible_node_on_chip(root,
     343                 :            :                                                NULL,
     344                 :            :                                                "specific-fake-coprocessor",
     345                 :            :                                                0xcafe) == gc1);
     346                 :          1 :         assert(dt_find_compatible_node_on_chip(root,
     347                 :            :                                                gc1,
     348                 :            :                                                "specific-fake-coprocessor",
     349                 :            :                                                0xcafe) == gc2);
     350                 :          1 :         assert(dt_find_compatible_node_on_chip(root,
     351                 :            :                                                gc2,
     352                 :            :                                                "specific-fake-coprocessor",
     353                 :            :                                                0xcafe) == gc3);
     354                 :          1 :         assert(dt_find_compatible_node_on_chip(root,
     355                 :            :                                                gc3,
     356                 :            :                                                "specific-fake-coprocessor",
     357                 :            :                                                0xcafe) == NULL);
     358                 :            : 
     359                 :            :         /* we can't find the coprocessor on the bus */
     360                 :          1 :         assert(dt_find_compatible_node_on_chip(root,
     361                 :            :                                                NULL,
     362                 :            :                                                "specific-fake-coprocessor",
     363                 :            :                                                0xbeef) == NULL);
     364                 :            : 
     365                 :            :         /* Test phandles. We override the automatically generated one. */
     366                 :          1 :         phandle = 0xf00;
     367                 :          1 :         dt_add_property(gc3, "phandle", (const void *)&phandle, 4);
     368                 :          1 :         assert(last_phandle == 0xf00);
     369                 :          1 :         assert(dt_find_by_phandle(root, 0xf00) == gc3);
     370                 :          1 :         assert(dt_find_by_phandle(root, 0xf0f) == NULL);
     371                 :            : 
     372                 :          1 :         dt_free(root);
     373                 :            : 
     374                 :            :         /* basic sorting */
     375                 :          1 :         root = dt_new_root("rewt");
     376                 :          1 :         dt_new(root, "a@1");
     377                 :          1 :         dt_new(root, "a@2");
     378                 :          1 :         dt_new(root, "a@3");
     379                 :          1 :         dt_new(root, "a@4");
     380                 :          1 :         dt_new(root, "b@4");
     381                 :          1 :         dt_new(root, "c@4");
     382                 :            : 
     383                 :          1 :         assert(is_sorted(root));
     384                 :            : 
     385                 :            :         /* Now test dt_attach_root */
     386                 :          1 :         other_root = dt_new_root("other_root");
     387                 :          1 :         dt_new(other_root, "d@1");
     388                 :            : 
     389                 :          1 :         assert(dt_attach_root(root, other_root));
     390                 :          1 :         other_root = dt_new_root("other_root");
     391                 :          1 :         assert(!dt_attach_root(root, other_root));
     392                 :          1 :         dt_free(root);
     393                 :            : 
     394                 :            :         /* Test child node sorting */
     395                 :          1 :         root = dt_new_root("test root");
     396                 :          1 :         build_tree(5, 3, root);
     397                 :            : 
     398                 :          1 :         if (!is_sorted(root)) {
     399                 :          0 :                 dump_dt(root, 1, false);
     400                 :            :         }
     401                 :          1 :         assert(is_sorted(root));
     402                 :            : 
     403                 :          1 :         dt_free(root);
     404                 :            : 
     405                 :            :         /* check dt_translate_address */
     406                 :            : 
     407                 :            :         /* NB: the root bus has two address cells */
     408                 :          1 :         root = dt_new_root("");
     409                 :            : 
     410                 :          1 :         c1 = dt_new_addr(root, "some-32bit-bus", 0x80000000);
     411                 :          1 :         dt_add_property_cells(c1, "#address-cells", 1);
     412                 :          1 :         dt_add_property_cells(c1, "#size-cells", 1);
     413                 :          1 :         dt_add_property_cells(c1, "ranges", 0x0, 0x8, 0x0, 0x1000);
     414                 :            : 
     415                 :          1 :         gc1 = dt_new_addr(c1, "test", 0x0500);
     416                 :          1 :         dt_add_property_cells(gc1, "reg", 0x0500, 0x10);
     417                 :            : 
     418                 :          1 :         assert(dt_translate_address(gc1, 0, NULL) == 0x800000500ul);
     419                 :            : 
     420                 :            :         /* try three level translation */
     421                 :            : 
     422                 :          1 :         gc2 = dt_new_addr(c1, "another-32bit-bus", 0x40000000);
     423                 :          1 :         dt_add_property_cells(gc2, "#address-cells", 1);
     424                 :          1 :         dt_add_property_cells(gc2, "#size-cells", 1);
     425                 :          1 :         dt_add_property_cells(gc2, "ranges",  0x0, 0x600, 0x100,
     426                 :            :                                                 0x100, 0x800, 0x100);
     427                 :            : 
     428                 :          1 :         ggc1 = dt_new_addr(gc2, "test", 0x50);
     429                 :          1 :         dt_add_property_cells(ggc1, "reg", 0x50, 0x10);
     430                 :          1 :         assert(dt_translate_address(ggc1, 0, NULL) == 0x800000650ul);
     431                 :            : 
     432                 :            :         /* test multiple ranges work */
     433                 :          1 :         ggc2 = dt_new_addr(gc2, "test", 0x150);
     434                 :          1 :         dt_add_property_cells(ggc2, "reg", 0x150, 0x10);
     435                 :          1 :         assert(dt_translate_address(ggc2, 0, NULL) == 0x800000850ul);
     436                 :            : 
     437                 :            :         /* try 64bit -> 64bit */
     438                 :            : 
     439                 :          1 :         c2 = dt_new_addr(root, "some-64bit-bus", 0xe00000000);
     440                 :          1 :         dt_add_property_cells(c2, "#address-cells", 2);
     441                 :          1 :         dt_add_property_cells(c2, "#size-cells", 2);
     442                 :          1 :         dt_add_property_cells(c2, "ranges", 0x0, 0x0, 0xe, 0x0, 0x2, 0x0);
     443                 :            : 
     444                 :          1 :         gc2 = dt_new_addr(c2, "test", 0x100000000ul);
     445                 :          1 :         dt_add_property_u64s(gc2, "reg", 0x100000000ul, 0x10ul);
     446                 :          1 :         assert(dt_translate_address(gc2, 0, NULL) == 0xf00000000ul);
     447                 :            : 
     448                 :          1 :         dt_free(root);
     449                 :            : 
     450                 :            :         /* phandle fixup test */
     451                 :          1 :         subtree = dt_new_root("subtree");
     452                 :          1 :         ev1 = dt_new(subtree, "ev@1");
     453                 :          1 :         ev1_ph = ev1->phandle;
     454                 :          1 :         dt_new(ev1,"a@1");
     455                 :          1 :         dt_new(ev1,"a@2");
     456                 :          1 :         dt_new(ev1,"a@3");
     457                 :          1 :         ut1 = dt_new(subtree, "ut@1");
     458                 :          1 :         dt_add_property(ut1, "something", (const void *)&ev1->phandle, 4);
     459                 :          1 :         ut2 = dt_new(subtree, "ut@2");
     460                 :          1 :         dt_add_property(ut2, "something", (const void *)&ev1->phandle, 4);
     461                 :            : 
     462                 :          1 :         dt_adjust_subtree_phandle(subtree, props_to_fix);
     463                 :          1 :         assert(!(ev1->phandle == ev1_ph));
     464                 :          1 :         new_prop_ph = dt_prop_get_u32(ut1, "something");
     465                 :          1 :         assert(!(new_prop_ph == ev1_ph));
     466                 :          1 :         new_prop_ph = dt_prop_get_u32(ut2, "something");
     467                 :          1 :         assert(!(new_prop_ph == ev1_ph));
     468                 :          1 :         dt_free(subtree);
     469                 :            : 
     470                 :            :         /* Test dt_find_by_name_before_addr */
     471                 :          1 :         root = dt_new_root("");
     472                 :          1 :         addr1 = dt_new_addr(root, "node", 0x1);
     473                 :          1 :         addr2 = dt_new_addr(root, "node0_1", 0x2);
     474                 :          1 :         assert(dt_find_by_name(root, "node@1") == addr1);
     475                 :          1 :         assert(dt_find_by_name(root, "node0_1@2") == addr2);
     476                 :          1 :         assert(dt_find_by_name_before_addr(root, "node") == addr1);
     477                 :          1 :         assert(dt_find_by_name_before_addr(root, "node0") == NULL);
     478                 :          1 :         assert(dt_find_by_name_before_addr(root, "node0_") == NULL);
     479                 :          1 :         assert(dt_find_by_name_before_addr(root, "node0_1") == addr2);
     480                 :          1 :         dt_free(root);
     481                 :            : 
     482                 :          1 :         return 0;
     483                 :            : }
     484                 :            : 

Generated by: LCOV version 1.14