blob: acd355ecc27381443c711bef80c134b66abd2d55 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000017#include <gmock/gmock.h>
Andrew Walbran679cdf42018-09-18 15:36:17 +010018
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000019extern "C" {
Andrew Walbran679cdf42018-09-18 15:36:17 +010020#include "hf/boot_params.h"
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000021#include "hf/fdt_handler.h"
22#include "hf/mpool.h"
Andrew Walbran679cdf42018-09-18 15:36:17 +010023}
24
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010025#include <memory>
26
Andrew Scull232d5602018-10-15 11:07:45 +010027namespace
28{
Andrew Walbran679cdf42018-09-18 15:36:17 +010029using ::testing::Eq;
Andrew Scullb401ba32018-11-09 10:30:54 +000030using ::testing::NotNull;
Andrew Walbran679cdf42018-09-18 15:36:17 +010031
Andrew Scull232d5602018-10-15 11:07:45 +010032constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 10;
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010033
Andrew Walbran679cdf42018-09-18 15:36:17 +010034/*
35 * /dts-v1/;
36 *
37 * / {
38 * #address-cells = <2>;
39 * #size-cells = <2>;
40 *
41 * memory@0 {
42 * device_type = "memory";
43 * reg = <0x00000000 0x00000000 0x00000000 0x20000000
44 * 0x00000000 0x30000000 0x00000000 0x00010000>;
45 * };
46 * memory@1 {
47 * device_type = "memory";
48 * reg = <0x00000000 0x30020000 0x00000000 0x00010000>;
49 * };
50 *
51 * chosen {
52 * linux,initrd-start = <0x00000000>;
53 * linux,initrd-end = <0x00000000>;
54 * };
55 * };
56 *
57 * $ dtc --boot-cpu 0 --in-format dts --out-format dtb --out-version 17 test.dts
58 * | xxd -i
59 */
60
Andrew Scull232d5602018-10-15 11:07:45 +010061constexpr uint8_t test_dtb[] = {
Andrew Walbran679cdf42018-09-18 15:36:17 +010062 0xd0, 0x0d, 0xfe, 0xed, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x00, 0x38,
63 0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x11,
64 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f,
65 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
69 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x02,
70 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x30,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07,
72 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x27,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
77 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x31,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07,
79 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x27,
81 0x00, 0x00, 0x00, 0x00, 0x30, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
83 0x63, 0x68, 0x6f, 0x73, 0x65, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
84 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3e,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
87 0x00, 0x00, 0x00, 0x09, 0x23, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73,
88 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x23, 0x73, 0x69, 0x7a, 0x65,
89 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x64, 0x65, 0x76, 0x69, 0x63,
90 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x00, 0x72, 0x65, 0x67, 0x00, 0x6c,
91 0x69, 0x6e, 0x75, 0x78, 0x2c, 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d,
92 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x2c,
93 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d, 0x65, 0x6e, 0x64, 0x00};
94
Andrew Scullb401ba32018-11-09 10:30:54 +000095TEST(fdt, find_memory_ranges)
Andrew Walbran679cdf42018-09-18 15:36:17 +010096{
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000097 struct mpool ppool;
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010098 std::unique_ptr<uint8_t[]> test_heap(new uint8_t[TEST_HEAP_SIZE]);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000099
100 mpool_init(&ppool, sizeof(struct mm_page_table));
101 mpool_add_chunk(&ppool, test_heap.get(), TEST_HEAP_SIZE);
Andrew Scullb2910562019-09-17 14:08:27 +0100102 mm_init(&ppool);
Andrew Walbran0ea8ced2018-10-03 14:56:58 +0100103
Andrew Scullb401ba32018-11-09 10:30:54 +0000104 struct fdt_header *fdt;
105 struct fdt_node n;
Andrew Walbran0ea8ced2018-10-03 14:56:58 +0100106 struct boot_params params = {};
Andrew Scullb401ba32018-11-09 10:30:54 +0000107
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100108 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
109 fdt = fdt_map(mm_stage1_locked, pa_init((uintpaddr_t)&test_dtb), &n,
110 &ppool);
Andrew Scullb401ba32018-11-09 10:30:54 +0000111 ASSERT_THAT(fdt, NotNull());
112 ASSERT_TRUE(fdt_find_child(&n, ""));
113 fdt_find_memory_ranges(&n, &params);
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100114 ASSERT_TRUE(fdt_unmap(mm_stage1_locked, fdt, &ppool));
115 mm_unlock_stage1(&mm_stage1_locked);
Andrew Scullb401ba32018-11-09 10:30:54 +0000116
Andrew Walbran679cdf42018-09-18 15:36:17 +0100117 EXPECT_THAT(params.mem_ranges_count, Eq(3));
118 EXPECT_THAT(pa_addr(params.mem_ranges[0].begin), Eq(0x00000000));
119 EXPECT_THAT(pa_addr(params.mem_ranges[0].end), Eq(0x20000000));
120 EXPECT_THAT(pa_addr(params.mem_ranges[1].begin), Eq(0x30000000));
121 EXPECT_THAT(pa_addr(params.mem_ranges[1].end), Eq(0x30010000));
122 EXPECT_THAT(pa_addr(params.mem_ranges[2].begin), Eq(0x30020000));
123 EXPECT_THAT(pa_addr(params.mem_ranges[2].end), Eq(0x30030000));
124}
Andrew Scull232d5602018-10-15 11:07:45 +0100125
126} /* namespace */