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