blob: 5c1e638df0756d494f70ae8dd11ce6c4328aa86a [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 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +00009#include <gmock/gmock.h>
Andrew Walbran679cdf42018-09-18 15:36:17 +010010
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000011extern "C" {
Andrew Walbran679cdf42018-09-18 15:36:17 +010012#include "hf/boot_params.h"
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000013#include "hf/fdt_handler.h"
14#include "hf/mpool.h"
Andrew Walbran679cdf42018-09-18 15:36:17 +010015}
16
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010017#include <memory>
18
Andrew Scull232d5602018-10-15 11:07:45 +010019namespace
20{
Andrew Walbran679cdf42018-09-18 15:36:17 +010021using ::testing::Eq;
Andrew Scullb401ba32018-11-09 10:30:54 +000022using ::testing::NotNull;
Andrew Walbran679cdf42018-09-18 15:36:17 +010023
Andrew Scull232d5602018-10-15 11:07:45 +010024constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 10;
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010025
Andrew Walbran679cdf42018-09-18 15:36:17 +010026/*
27 * /dts-v1/;
28 *
29 * / {
30 * #address-cells = <2>;
31 * #size-cells = <2>;
32 *
33 * memory@0 {
34 * device_type = "memory";
35 * reg = <0x00000000 0x00000000 0x00000000 0x20000000
36 * 0x00000000 0x30000000 0x00000000 0x00010000>;
37 * };
38 * memory@1 {
39 * device_type = "memory";
40 * reg = <0x00000000 0x30020000 0x00000000 0x00010000>;
41 * };
42 *
43 * chosen {
44 * linux,initrd-start = <0x00000000>;
45 * linux,initrd-end = <0x00000000>;
46 * };
47 * };
48 *
49 * $ dtc --boot-cpu 0 --in-format dts --out-format dtb --out-version 17 test.dts
50 * | xxd -i
51 */
52
Andrew Scull232d5602018-10-15 11:07:45 +010053constexpr uint8_t test_dtb[] = {
Andrew Walbran679cdf42018-09-18 15:36:17 +010054 0xd0, 0x0d, 0xfe, 0xed, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x00, 0x38,
55 0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x11,
56 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f,
57 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
61 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x02,
62 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x30,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07,
64 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x27,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
69 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x31,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07,
71 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x27,
73 0x00, 0x00, 0x00, 0x00, 0x30, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
75 0x63, 0x68, 0x6f, 0x73, 0x65, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
76 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3e,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
79 0x00, 0x00, 0x00, 0x09, 0x23, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73,
80 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x23, 0x73, 0x69, 0x7a, 0x65,
81 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x64, 0x65, 0x76, 0x69, 0x63,
82 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x00, 0x72, 0x65, 0x67, 0x00, 0x6c,
83 0x69, 0x6e, 0x75, 0x78, 0x2c, 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d,
84 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x2c,
85 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d, 0x65, 0x6e, 0x64, 0x00};
86
Andrew Scullb401ba32018-11-09 10:30:54 +000087TEST(fdt, find_memory_ranges)
Andrew Walbran679cdf42018-09-18 15:36:17 +010088{
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000089 struct mpool ppool;
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010090 std::unique_ptr<uint8_t[]> test_heap(new uint8_t[TEST_HEAP_SIZE]);
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000091
92 mpool_init(&ppool, sizeof(struct mm_page_table));
93 mpool_add_chunk(&ppool, test_heap.get(), TEST_HEAP_SIZE);
Andrew Scullb2910562019-09-17 14:08:27 +010094 mm_init(&ppool);
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010095
David Brazdilb856be62020-03-25 10:14:55 +000096 struct fdt fdt;
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010097 struct boot_params params = {};
Andrew Scullb401ba32018-11-09 10:30:54 +000098
Andrew Scull3c0a90a2019-07-01 11:55:53 +010099 struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1();
Andrew Scull6c9a4ab2020-01-27 17:09:12 +0000100 struct string memory = STRING_INIT("memory");
David Brazdilb856be62020-03-25 10:14:55 +0000101 ASSERT_TRUE(fdt_map(&fdt, mm_stage1_locked,
102 pa_init((uintpaddr_t)&test_dtb), &ppool));
103 fdt_find_memory_ranges(&fdt, &memory, params.mem_ranges,
Andrew Scull6c9a4ab2020-01-27 17:09:12 +0000104 &params.mem_ranges_count, MAX_MEM_RANGES);
David Brazdilb856be62020-03-25 10:14:55 +0000105 ASSERT_TRUE(fdt_unmap(&fdt, mm_stage1_locked, &ppool));
Andrew Scull3c0a90a2019-07-01 11:55:53 +0100106 mm_unlock_stage1(&mm_stage1_locked);
Andrew Scullb401ba32018-11-09 10:30:54 +0000107
Andrew Walbran679cdf42018-09-18 15:36:17 +0100108 EXPECT_THAT(params.mem_ranges_count, Eq(3));
109 EXPECT_THAT(pa_addr(params.mem_ranges[0].begin), Eq(0x00000000));
110 EXPECT_THAT(pa_addr(params.mem_ranges[0].end), Eq(0x20000000));
111 EXPECT_THAT(pa_addr(params.mem_ranges[1].begin), Eq(0x30000000));
112 EXPECT_THAT(pa_addr(params.mem_ranges[1].end), Eq(0x30010000));
113 EXPECT_THAT(pa_addr(params.mem_ranges[2].begin), Eq(0x30020000));
114 EXPECT_THAT(pa_addr(params.mem_ranges[2].end), Eq(0x30030000));
115}
Andrew Scull232d5602018-10-15 11:07:45 +0100116
117} /* namespace */