Laurentiu Tudor | 528d6b3 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2020 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 8 | #include <asm/global_data.h> |
Laurentiu Tudor | 528d6b3 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 9 | #include <dm/of_extra.h> |
| 10 | #include <dm/test.h> |
| 11 | #include <test/ut.h> |
| 12 | |
Bin Meng | c9a1df0 | 2020-06-22 22:55:47 -0700 | [diff] [blame] | 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
Laurentiu Tudor | 528d6b3 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 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 | |
Bin Meng | 866f11e | 2020-06-22 22:55:48 -0700 | [diff] [blame] | 26 | /* Make a writable copy of the fdt blob */ |
Laurentiu Tudor | 528d6b3 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 27 | ut_assertok(fdt_open_into(gd->fdt_blob, blob, blob_sz)); |
| 28 | |
| 29 | resv.start = 0x1000; |
| 30 | resv.end = 0x2000; |
Thierry Reding | 9019487 | 2021-09-03 15:16:20 +0200 | [diff] [blame] | 31 | ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 2, |
Thierry Reding | b9aad37 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 32 | &resv, "test_resv1", NULL, 0, 0)); |
Laurentiu Tudor | 528d6b3 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 33 | |
| 34 | resv.start = 0x10000; |
| 35 | resv.end = 0x20000; |
Thierry Reding | 9019487 | 2021-09-03 15:16:20 +0200 | [diff] [blame] | 36 | ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 1, |
Thierry Reding | b9aad37 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 37 | &resv, "test_resv2", NULL, 0, 0)); |
Laurentiu Tudor | 528d6b3 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 38 | |
| 39 | resv.start = 0x100000; |
| 40 | resv.end = 0x200000; |
Thierry Reding | 9019487 | 2021-09-03 15:16:20 +0200 | [diff] [blame] | 41 | ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 0, |
Thierry Reding | b9aad37 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 42 | &resv, "test_resv3", NULL, 0, 0)); |
Laurentiu Tudor | 528d6b3 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 43 | |
| 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 | } |
| 58 | DM_TEST(dm_test_fdtdec_set_carveout, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 59 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 60 | |
| 61 | static 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 Reding | b9aad37 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 67 | unsigned long flags = FDTDEC_RESERVED_MEMORY_NO_MAP; |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 68 | 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 Reding | 46cb067 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 81 | ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region", &resv, |
Thierry Reding | b9aad37 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 82 | NULL, 0, &phandle, 0)); |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 83 | |
| 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 Reding | 46cb067 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 102 | ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region1", &resv, |
Thierry Reding | b9aad37 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 103 | NULL, 0, &phandle1, flags)); |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 104 | subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1"); |
| 105 | ut_assert(subnode > 0); |
| 106 | |
Patrice Chotard | 6613ed1 | 2020-09-10 10:50:00 +0200 | [diff] [blame] | 107 | /* check that no-map property is present */ |
| 108 | ut_assert(fdt_getprop(blob, subnode, "no-map", NULL) > 0); |
| 109 | |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 110 | /* 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 Reding | 46cb067 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 119 | ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region2", &resv, |
Thierry Reding | b9aad37 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 120 | NULL, 0, &phandle1, 0)); |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 121 | 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 | } |
| 131 | DM_TEST(dm_test_fdtdec_add_reserved_memory, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 132 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |