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; |
| 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 | } |
| 61 | DM_TEST(dm_test_fdtdec_set_carveout, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 62 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 63 | |
| 64 | static 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 Carriere | ccaa574 | 2020-09-10 10:49:59 +0200 | [diff] [blame] | 84 | &resv, &phandle, false)); |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 85 | |
| 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 Chotard | 6613ed1 | 2020-09-10 10:50:00 +0200 | [diff] [blame] | 105 | &resv, &phandle1, true)); |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 106 | subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1"); |
| 107 | ut_assert(subnode > 0); |
| 108 | |
Patrice Chotard | 6613ed1 | 2020-09-10 10:50:00 +0200 | [diff] [blame] | 109 | /* check that no-map property is present */ |
| 110 | ut_assert(fdt_getprop(blob, subnode, "no-map", NULL) > 0); |
| 111 | |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 112 | /* 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 Carriere | ccaa574 | 2020-09-10 10:49:59 +0200 | [diff] [blame] | 122 | &resv, &phandle1, false)); |
Bin Meng | 229806f | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 123 | 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 | } |
| 133 | DM_TEST(dm_test_fdtdec_add_reserved_memory, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 134 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |