blob: 1f630ea3eee1d7f59660d1ea3f783600c4bda51a [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>
Simon Glass3ba929a2020-10-30 21:38:53 -06008#include <asm/global_data.h>
Laurentiu Tudor62070e02020-04-03 13:43:04 +03009#include <dm/of_extra.h>
10#include <dm/test.h>
11#include <test/ut.h>
12
Bin Mengcc201402020-06-22 22:55:47 -070013DECLARE_GLOBAL_DATA_PTR;
14
Laurentiu Tudor62070e02020-04-03 13:43:04 +030015static 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
Bin Meng9db5b502020-06-22 22:55:48 -070026 /* Make a writable copy of the fdt blob */
Laurentiu Tudor62070e02020-04-03 13:43:04 +030027 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}
61DM_TEST(dm_test_fdtdec_set_carveout,
Simon Glass974dccd2020-07-28 19:41:12 -060062 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Bin Meng0473dfb2020-06-22 22:55:49 -070063
64static 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",
Etienne Carriere646f8c62020-09-10 10:49:59 +020084 &resv, &phandle, false));
Bin Meng0473dfb2020-06-22 22:55:49 -070085
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",
Patrice Chotard498ebe92020-09-10 10:50:00 +0200105 &resv, &phandle1, true));
Bin Meng0473dfb2020-06-22 22:55:49 -0700106 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1");
107 ut_assert(subnode > 0);
108
Patrice Chotard498ebe92020-09-10 10:50:00 +0200109 /* check that no-map property is present */
110 ut_assert(fdt_getprop(blob, subnode, "no-map", NULL) > 0);
111
Bin Meng0473dfb2020-06-22 22:55:49 -0700112 /* 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",
Etienne Carriere646f8c62020-09-10 10:49:59 +0200122 &resv, &phandle1, false));
Bin Meng0473dfb2020-06-22 22:55:49 -0700123 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}
133DM_TEST(dm_test_fdtdec_add_reserved_memory,
Simon Glass974dccd2020-07-28 19:41:12 -0600134 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);