blob: 56f6f4f6cc693230049201449f41c78008bab1ad [file] [log] [blame]
Laurentiu Tudor62070e02020-04-03 13:43:04 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2020 NXP
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <dm/of_extra.h>
9#include <dm/test.h>
10#include <test/ut.h>
11
Bin Mengcc201402020-06-22 22:55:47 -070012DECLARE_GLOBAL_DATA_PTR;
13
Laurentiu Tudor62070e02020-04-03 13:43:04 +030014static int dm_test_fdtdec_set_carveout(struct unit_test_state *uts)
15{
16 struct fdt_memory resv;
17 void *blob;
18 const fdt32_t *prop;
19 int blob_sz, len, offset;
20
21 blob_sz = fdt_totalsize(gd->fdt_blob) + 4096;
22 blob = malloc(blob_sz);
23 ut_assertnonnull(blob);
24
Bin Meng9db5b502020-06-22 22:55:48 -070025 /* Make a writable copy of the fdt blob */
Laurentiu Tudor62070e02020-04-03 13:43:04 +030026 ut_assertok(fdt_open_into(gd->fdt_blob, blob, blob_sz));
27
28 resv.start = 0x1000;
29 resv.end = 0x2000;
30 ut_assertok(fdtdec_set_carveout(blob, "/a-test",
31 "memory-region", 2, "test_resv1",
32 &resv));
33
34 resv.start = 0x10000;
35 resv.end = 0x20000;
36 ut_assertok(fdtdec_set_carveout(blob, "/a-test",
37 "memory-region", 1, "test_resv2",
38 &resv));
39
40 resv.start = 0x100000;
41 resv.end = 0x200000;
42 ut_assertok(fdtdec_set_carveout(blob, "/a-test",
43 "memory-region", 0, "test_resv3",
44 &resv));
45
46 offset = fdt_path_offset(blob, "/a-test");
47 ut_assert(offset > 0);
48 prop = fdt_getprop(blob, offset, "memory-region", &len);
49 ut_assertnonnull(prop);
50
51 ut_asserteq(len, 12);
52 ut_assert(fdt_node_offset_by_phandle(blob, fdt32_to_cpu(prop[0])) > 0);
53 ut_assert(fdt_node_offset_by_phandle(blob, fdt32_to_cpu(prop[1])) > 0);
54 ut_assert(fdt_node_offset_by_phandle(blob, fdt32_to_cpu(prop[2])) > 0);
55
56 free(blob);
57
58 return 0;
59}
60DM_TEST(dm_test_fdtdec_set_carveout,
61 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
Bin Meng0473dfb2020-06-22 22:55:49 -070062
63static int dm_test_fdtdec_add_reserved_memory(struct unit_test_state *uts)
64{
65 struct fdt_memory resv;
66 fdt_addr_t addr;
67 fdt_size_t size;
68 void *blob;
69 int blob_sz, parent, subnode;
70 uint32_t phandle, phandle1;
71
72 blob_sz = fdt_totalsize(gd->fdt_blob) + 128;
73 blob = malloc(blob_sz);
74 ut_assertnonnull(blob);
75
76 /* Make a writable copy of the fdt blob */
77 ut_assertok(fdt_open_into(gd->fdt_blob, blob, blob_sz));
78
79 /* Insert a memory region in /reserved-memory node */
80 resv.start = 0x1000;
81 resv.end = 0x1fff;
82 ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region",
83 &resv, &phandle));
84
85 /* Test /reserve-memory and its subnode should exist */
86 parent = fdt_path_offset(blob, "/reserved-memory");
87 ut_assert(parent > 0);
88 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region");
89 ut_assert(subnode > 0);
90
91 /* Test reg property of /reserved-memory/rsvd_region node */
92 addr = fdtdec_get_addr_size_auto_parent(blob, parent, subnode,
93 "reg", 0, &size, false);
94 ut_assert(addr == resv.start);
95 ut_assert(size == resv.end - resv.start + 1);
96
97 /* Insert another memory region in /reserved-memory node */
98 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1");
99 ut_assert(subnode < 0);
100
101 resv.start = 0x2000;
102 resv.end = 0x2fff;
103 ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region1",
104 &resv, &phandle1));
105 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1");
106 ut_assert(subnode > 0);
107
108 /* phandles must be different */
109 ut_assert(phandle != phandle1);
110
111 /*
112 * Insert a 3rd memory region with the same addr/size as the 1st one,
113 * but a new node should not be inserted due to the same addr/size.
114 */
115 resv.start = 0x1000;
116 resv.end = 0x1fff;
117 ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region2",
118 &resv, &phandle1));
119 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region2");
120 ut_assert(subnode < 0);
121
122 /* phandle must be same as the 1st one */
123 ut_assert(phandle == phandle1);
124
125 free(blob);
126
127 return 0;
128}
129DM_TEST(dm_test_fdtdec_add_reserved_memory,
130 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);