blob: 1820b82009a6d6499cf28e6b8d08ecd4b8948b14 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2012-2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <arch.h>
24#include <err.h>
25#include <debug.h>
26#include <trace.h>
27#include <dev/interrupt/arm_gic.h>
28#include <dev/timer/arm_generic.h>
29#include <dev/uart.h>
30#include <dev/virtio.h>
31#include <dev/virtio/net.h>
32#include <lk/init.h>
33#include <kernel/vm.h>
34#include <kernel/spinlock.h>
35#include <platform.h>
36#include <platform/gic.h>
37#include <platform/interrupts.h>
38#include <platform/qemu-virt.h>
39#include <libfdt.h>
40#include "platform_p.h"
41
42#if WITH_LIB_MINIP
43#include <lib/minip.h>
44#endif
45
46#define DEFAULT_MEMORY_SIZE (MEMSIZE) /* try to fetch from the emulator via the fdt */
47
48/* initial memory mappings. parsed by start.S */
49struct mmu_initial_mapping mmu_initial_mappings[] = {
50 /* all of memory */
51 {
52 .phys = MEMORY_BASE_PHYS,
53 .virt = KERNEL_BASE,
54 .size = MEMORY_APERTURE_SIZE,
55 .flags = 0,
56 .name = "memory"
57 },
58
59 /* 1GB of peripherals */
60 {
61 .phys = PERIPHERAL_BASE_PHYS,
62 .virt = PERIPHERAL_BASE_VIRT,
63 .size = PERIPHERAL_BASE_SIZE,
64 .flags = MMU_INITIAL_MAPPING_FLAG_DEVICE,
65 .name = "peripherals"
66 },
67
68 /* null entry to terminate the list */
69 { 0 }
70};
71
72static pmm_arena_t arena = {
73 .name = "ram",
74 .base = MEMORY_BASE_PHYS,
75 .size = DEFAULT_MEMORY_SIZE,
76 .flags = PMM_ARENA_FLAG_KMAP,
77};
78
79extern void psci_call(ulong arg0, ulong arg1, ulong arg2, ulong arg3);
80
81void platform_early_init(void)
82{
83 /* initialize the interrupt controller */
84 arm_gic_init();
85
86 arm_generic_timer_init(ARM_GENERIC_TIMER_PHYSICAL_INT, 0);
87
88 uart_init_early();
89
90 /* look for a flattened device tree just before the kernel */
91 const void *fdt = (void *)KERNEL_BASE;
92 int err = fdt_check_header(fdt);
93 if (err >= 0) {
94 /* walk the nodes, looking for 'memory' */
95 int depth = 0;
96 int offset = 0;
97 for (;;) {
98 offset = fdt_next_node(fdt, offset, &depth);
99 if (offset < 0)
100 break;
101
102 /* get the name */
103 const char *name = fdt_get_name(fdt, offset, NULL);
104 if (!name)
105 continue;
106
107 /* look for the 'memory' property */
108 if (strcmp(name, "memory") == 0) {
109 int lenp;
110 const void *prop_ptr = fdt_getprop(fdt, offset, "reg", &lenp);
111 if (prop_ptr && lenp == 0x10) {
112 /* we're looking at a memory descriptor */
113 //uint64_t base = fdt64_to_cpu(*(uint64_t *)prop_ptr);
114 uint64_t len = fdt64_to_cpu(*((const uint64_t *)prop_ptr + 1));
115
116 /* trim size on certain platforms */
117#if ARCH_ARM
118 if (len > 1024*1024*1024U) {
119 len = 1024*1024*1024; /* only use the first 1GB on ARM32 */
120 printf("trimming memory to 1GB\n");
121 }
122#endif
123
124 /* set the size in the pmm arena */
125 arena.size = len;
126 }
127 }
128 }
129 }
130
131 /* add the main memory arena */
132 pmm_add_arena(&arena);
133
134 /* reserve the first 64k of ram, which should be holding the fdt */
135 struct list_node list = LIST_INITIAL_VALUE(list);
136 pmm_alloc_range(MEMBASE, 0x10000 / PAGE_SIZE, &list);
137
138 /* boot the secondary cpus using the Power State Coordintion Interface */
139 ulong psci_call_num = 0x84000000 + 3; /* SMC32 CPU_ON */
140#if ARCH_ARM64
141 psci_call_num += 0x40000000; /* SMC64 */
142#endif
143 for (uint i = 1; i < SMP_MAX_CPUS; i++) {
144 psci_call(psci_call_num, i, MEMBASE + KERNEL_LOAD_OFFSET, 0);
145 }
146}
147
148void platform_init(void)
149{
150 uart_init();
151
152 /* detect any virtio devices */
153 uint virtio_irqs[NUM_VIRTIO_TRANSPORTS];
154 for (int i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
155 virtio_irqs[i] = VIRTIO0_INT + i;
156 }
157
158 virtio_mmio_detect((void *)VIRTIO_BASE, NUM_VIRTIO_TRANSPORTS, virtio_irqs);
159
160#if WITH_LIB_MINIP
161 if (virtio_net_found() > 0) {
162 uint8_t mac_addr[6];
163
164 virtio_net_get_mac_addr(mac_addr);
165
166 TRACEF("found virtio networking interface\n");
167
168 /* start minip */
169 minip_set_macaddr(mac_addr);
170
171 __UNUSED uint32_t ip_addr = IPV4(192, 168, 0, 99);
172 __UNUSED uint32_t ip_mask = IPV4(255, 255, 255, 0);
173 __UNUSED uint32_t ip_gateway = IPV4_NONE;
174
175 //minip_init(virtio_net_send_minip_pkt, NULL, ip_addr, ip_mask, ip_gateway);
176 minip_init_dhcp(virtio_net_send_minip_pkt, NULL);
177
178 virtio_net_start();
179 }
180#endif
181}