blob: 087d4846da89a082b7c4ba726cfbe577dd8e54b5 [file] [log] [blame]
Laurentiu Tudor528d6b32020-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 Glass401d1c42020-10-30 21:38:53 -06008#include <asm/global_data.h>
Laurentiu Tudor528d6b32020-04-03 13:43:04 +03009#include <dm/of_extra.h>
10#include <dm/test.h>
11#include <test/ut.h>
12
Bin Mengc9a1df02020-06-22 22:55:47 -070013DECLARE_GLOBAL_DATA_PTR;
14
Laurentiu Tudor528d6b32020-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 Meng866f11e2020-06-22 22:55:48 -070026 /* Make a writable copy of the fdt blob */
Laurentiu Tudor528d6b32020-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;
Thierry Reding90194872021-09-03 15:16:20 +020031 ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 2,
Thierry Redingb9aad372021-09-03 15:16:21 +020032 &resv, "test_resv1", NULL, 0, 0));
Laurentiu Tudor528d6b32020-04-03 13:43:04 +030033
34 resv.start = 0x10000;
35 resv.end = 0x20000;
Thierry Reding90194872021-09-03 15:16:20 +020036 ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 1,
Thierry Redingb9aad372021-09-03 15:16:21 +020037 &resv, "test_resv2", NULL, 0, 0));
Laurentiu Tudor528d6b32020-04-03 13:43:04 +030038
39 resv.start = 0x100000;
40 resv.end = 0x200000;
Thierry Reding90194872021-09-03 15:16:20 +020041 ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 0,
Thierry Redingb9aad372021-09-03 15:16:21 +020042 &resv, "test_resv3", NULL, 0, 0));
Laurentiu Tudor528d6b32020-04-03 13:43:04 +030043
44 offset = fdt_path_offset(blob, "/a-test");
45 ut_assert(offset > 0);
46 prop = fdt_getprop(blob, offset, "memory-region", &len);
47 ut_assertnonnull(prop);
48
49 ut_asserteq(len, 12);
50 ut_assert(fdt_node_offset_by_phandle(blob, fdt32_to_cpu(prop[0])) > 0);
51 ut_assert(fdt_node_offset_by_phandle(blob, fdt32_to_cpu(prop[1])) > 0);
52 ut_assert(fdt_node_offset_by_phandle(blob, fdt32_to_cpu(prop[2])) > 0);
53
54 free(blob);
55
56 return 0;
57}
58DM_TEST(dm_test_fdtdec_set_carveout,
Simon Glasse180c2b2020-07-28 19:41:12 -060059 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Bin Meng229806f2020-06-22 22:55:49 -070060
61static int dm_test_fdtdec_add_reserved_memory(struct unit_test_state *uts)
62{
63 struct fdt_memory resv;
64 fdt_addr_t addr;
65 fdt_size_t size;
66 void *blob;
Thierry Redingb9aad372021-09-03 15:16:21 +020067 unsigned long flags = FDTDEC_RESERVED_MEMORY_NO_MAP;
Bin Meng229806f2020-06-22 22:55:49 -070068 int blob_sz, parent, subnode;
69 uint32_t phandle, phandle1;
70
71 blob_sz = fdt_totalsize(gd->fdt_blob) + 128;
72 blob = malloc(blob_sz);
73 ut_assertnonnull(blob);
74
75 /* Make a writable copy of the fdt blob */
76 ut_assertok(fdt_open_into(gd->fdt_blob, blob, blob_sz));
77
78 /* Insert a memory region in /reserved-memory node */
79 resv.start = 0x1000;
80 resv.end = 0x1fff;
Thierry Reding46cb0672021-09-03 15:16:19 +020081 ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region", &resv,
Thierry Redingb9aad372021-09-03 15:16:21 +020082 NULL, 0, &phandle, 0));
Bin Meng229806f2020-06-22 22:55:49 -070083
84 /* Test /reserve-memory and its subnode should exist */
85 parent = fdt_path_offset(blob, "/reserved-memory");
86 ut_assert(parent > 0);
87 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region");
88 ut_assert(subnode > 0);
89
90 /* Test reg property of /reserved-memory/rsvd_region node */
91 addr = fdtdec_get_addr_size_auto_parent(blob, parent, subnode,
92 "reg", 0, &size, false);
93 ut_assert(addr == resv.start);
94 ut_assert(size == resv.end - resv.start + 1);
95
96 /* Insert another memory region in /reserved-memory node */
97 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1");
98 ut_assert(subnode < 0);
99
100 resv.start = 0x2000;
101 resv.end = 0x2fff;
Thierry Reding46cb0672021-09-03 15:16:19 +0200102 ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region1", &resv,
Thierry Redingb9aad372021-09-03 15:16:21 +0200103 NULL, 0, &phandle1, flags));
Bin Meng229806f2020-06-22 22:55:49 -0700104 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1");
105 ut_assert(subnode > 0);
106
Patrice Chotard6613ed12020-09-10 10:50:00 +0200107 /* check that no-map property is present */
108 ut_assert(fdt_getprop(blob, subnode, "no-map", NULL) > 0);
109
Bin Meng229806f2020-06-22 22:55:49 -0700110 /* phandles must be different */
111 ut_assert(phandle != phandle1);
112
113 /*
114 * Insert a 3rd memory region with the same addr/size as the 1st one,
115 * but a new node should not be inserted due to the same addr/size.
116 */
117 resv.start = 0x1000;
118 resv.end = 0x1fff;
Thierry Reding46cb0672021-09-03 15:16:19 +0200119 ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region2", &resv,
Thierry Redingb9aad372021-09-03 15:16:21 +0200120 NULL, 0, &phandle1, 0));
Bin Meng229806f2020-06-22 22:55:49 -0700121 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region2");
122 ut_assert(subnode < 0);
123
124 /* phandle must be same as the 1st one */
125 ut_assert(phandle == phandle1);
126
127 free(blob);
128
129 return 0;
130}
131DM_TEST(dm_test_fdtdec_add_reserved_memory,
Simon Glasse180c2b2020-07-28 19:41:12 -0600132 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);