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 | |
Laurentiu Tudor | 62070e0 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 6 | #include <dm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 7 | #include <asm/global_data.h> |
Laurentiu Tudor | 62070e0 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 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; |
Thierry Reding | d6ad807 | 2021-09-03 15:16:20 +0200 | [diff] [blame] | 30 | ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 2, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 31 | &resv, "test_resv1", NULL, 0, 0)); |
Laurentiu Tudor | 62070e0 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 32 | |
| 33 | resv.start = 0x10000; |
| 34 | resv.end = 0x20000; |
Thierry Reding | d6ad807 | 2021-09-03 15:16:20 +0200 | [diff] [blame] | 35 | ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 1, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 36 | &resv, "test_resv2", NULL, 0, 0)); |
Laurentiu Tudor | 62070e0 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 37 | |
| 38 | resv.start = 0x100000; |
| 39 | resv.end = 0x200000; |
Thierry Reding | d6ad807 | 2021-09-03 15:16:20 +0200 | [diff] [blame] | 40 | ut_assertok(fdtdec_set_carveout(blob, "/a-test", "memory-region", 0, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 41 | &resv, "test_resv3", NULL, 0, 0)); |
Laurentiu Tudor | 62070e0 | 2020-04-03 13:43:04 +0300 | [diff] [blame] | 42 | |
| 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 | } |
| 57 | DM_TEST(dm_test_fdtdec_set_carveout, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 58 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
Bin Meng | 0473dfb | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 59 | |
| 60 | static 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 Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 66 | unsigned long flags = FDTDEC_RESERVED_MEMORY_NO_MAP; |
Bin Meng | 0473dfb | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 67 | 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 Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 80 | ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region", &resv, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 81 | NULL, 0, &phandle, 0)); |
Bin Meng | 0473dfb | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 82 | |
| 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 Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 101 | ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region1", &resv, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 102 | NULL, 0, &phandle1, flags)); |
Bin Meng | 0473dfb | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 103 | subnode = fdt_path_offset(blob, "/reserved-memory/rsvd_region1"); |
| 104 | ut_assert(subnode > 0); |
| 105 | |
Patrice Chotard | 498ebe9 | 2020-09-10 10:50:00 +0200 | [diff] [blame] | 106 | /* check that no-map property is present */ |
| 107 | ut_assert(fdt_getprop(blob, subnode, "no-map", NULL) > 0); |
| 108 | |
Bin Meng | 0473dfb | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 109 | /* 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 Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 118 | ut_assertok(fdtdec_add_reserved_memory(blob, "rsvd_region2", &resv, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 119 | NULL, 0, &phandle1, 0)); |
Bin Meng | 0473dfb | 2020-06-22 22:55:49 -0700 | [diff] [blame] | 120 | 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 | } |
| 130 | DM_TEST(dm_test_fdtdec_add_reserved_memory, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 131 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |