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