blob: 1f24f1d5dffbbdb459ff708d35c216f04579eb99 [file] [log] [blame]
Laurentiu Tudor62070e02020-04-03 13:43:04 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2020 NXP
4 */
5
Laurentiu Tudor62070e02020-04-03 13:43:04 +03006#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06007#include <asm/global_data.h>
Laurentiu Tudor62070e02020-04-03 13:43:04 +03008#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;
Thierry Redingd6ad8072021-09-03 15:16:20 +020030 ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 2,
Thierry Redingbedc4392021-09-03 15:16:21 +020031 &resv, "test_resv1", NULL, 0, 0));
Laurentiu Tudor62070e02020-04-03 13:43:04 +030032
33 resv.start = 0x10000;
34 resv.end = 0x20000;
Thierry Redingd6ad8072021-09-03 15:16:20 +020035 ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 1,
Thierry Redingbedc4392021-09-03 15:16:21 +020036 &resv, "test_resv2", NULL, 0, 0));
Laurentiu Tudor62070e02020-04-03 13:43:04 +030037
38 resv.start = 0x100000;
39 resv.end = 0x200000;
Thierry Redingd6ad8072021-09-03 15:16:20 +020040 ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 0,
Thierry Redingbedc4392021-09-03 15:16:21 +020041 &resv, "test_resv3", NULL, 0, 0));
Laurentiu Tudor62070e02020-04-03 13:43:04 +030042
43 offset = fdt_path_offset(blob, "/a-test");
44 ut_assert(offset > 0);
45 prop = fdt_getprop(blob, offset, "memory-region", &len);
46 ut_assertnonnull(prop);
47
48 ut_asserteq(len, 12);
49 ut_assert(fdt_node_offset_by_phandle(blob, fdt32_to_cpu(prop[0])) > 0);
50 ut_assert(fdt_node_offset_by_phandle(blob, fdt32_to_cpu(prop[1])) > 0);
51 ut_assert(fdt_node_offset_by_phandle(blob, fdt32_to_cpu(prop[2])) > 0);
52
53 free(blob);
54
55 return 0;
56}
57DM_TEST(dm_test_fdtdec_set_carveout,
Simon Glass1a92f832024-08-22 07:57:48 -060058 UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_FLAT_TREE);
Bin Meng0473dfb2020-06-22 22:55:49 -070059
60static int dm_test_fdtdec_add_reserved_memory(struct unit_test_state *uts)
61{
62 struct fdt_memory resv;
63 fdt_addr_t addr;
64 fdt_size_t size;
65 void *blob;
Thierry Redingbedc4392021-09-03 15:16:21 +020066 unsigned long flags = FDTDEC_RESERVED_MEMORY_NO_MAP;
Bin Meng0473dfb2020-06-22 22:55:49 -070067 int blob_sz, parent, subnode;
68 uint32_t phandle, phandle1;
69
70 blob_sz = fdt_totalsize(gd->fdt_blob) + 128;
71 blob = malloc(blob_sz);
72 ut_assertnonnull(blob);
73
74 /* Make a writable copy of the fdt blob */
75 ut_assertok(fdt_open_into(gd->fdt_blob, blob, blob_sz));
76
77 /* Insert a memory region in /reserved-memory node */
78 resv.start = 0x1000;
79 resv.end = 0x1fff;
Thierry Reding5e336912021-09-03 15:16:19 +020080 ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region", &resv,
Thierry Redingbedc4392021-09-03 15:16:21 +020081 NULL, 0, &phandle, 0));
Bin Meng0473dfb2020-06-22 22:55:49 -070082
83 /* Test /reserve-memory and its subnode should exist */
84 parent = fdt_path_offset(blob, "/reserved-memory");
85 ut_assert(parent > 0);
86 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region");
87 ut_assert(subnode > 0);
88
89 /* Test reg property of /reserved-memory/rsvd_region node */
90 addr = fdtdec_get_addr_size_auto_parent(blob, parent, subnode,
91 "reg", 0, &size, false);
92 ut_assert(addr == resv.start);
93 ut_assert(size == resv.end - resv.start + 1);
94
95 /* Insert another memory region in /reserved-memory node */
96 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1");
97 ut_assert(subnode < 0);
98
99 resv.start = 0x2000;
100 resv.end = 0x2fff;
Thierry Reding5e336912021-09-03 15:16:19 +0200101 ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region1", &resv,
Thierry Redingbedc4392021-09-03 15:16:21 +0200102 NULL, 0, &phandle1, flags));
Bin Meng0473dfb2020-06-22 22:55:49 -0700103 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1");
104 ut_assert(subnode > 0);
105
Patrice Chotard498ebe92020-09-10 10:50:00 +0200106 /* check that no-map property is present */
107 ut_assert(fdt_getprop(blob, subnode, "no-map", NULL) > 0);
108
Bin Meng0473dfb2020-06-22 22:55:49 -0700109 /* phandles must be different */
110 ut_assert(phandle != phandle1);
111
112 /*
113 * Insert a 3rd memory region with the same addr/size as the 1st one,
114 * but a new node should not be inserted due to the same addr/size.
115 */
116 resv.start = 0x1000;
117 resv.end = 0x1fff;
Thierry Reding5e336912021-09-03 15:16:19 +0200118 ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region2", &resv,
Thierry Redingbedc4392021-09-03 15:16:21 +0200119 NULL, 0, &phandle1, 0));
Bin Meng0473dfb2020-06-22 22:55:49 -0700120 subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region2");
121 ut_assert(subnode < 0);
122
123 /* phandle must be same as the 1st one */
124 ut_assert(phandle == phandle1);
125
126 free(blob);
127
128 return 0;
129}
130DM_TEST(dm_test_fdtdec_add_reserved_memory,
Simon Glass1a92f832024-08-22 07:57:48 -0600131 UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_FLAT_TREE);