aboutsummaryrefslogtreecommitdiff
path: root/arch/riscv/init/init.c
blob: 73727a741080d658251fef4a6928f79fe8fd45a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <apos/init.h>
#include <apos/sizes.h>
#include <apos/types.h>
#include <apos/string.h>
#include <apos/debug.h>
#include <apos/pmem.h>
#include <apos/utils.h>
#include <libfdt.h>
#include <pages.h>
#include <csr.h>
#include <vmap.h>

struct mem_layout {
	pm_t base;
	pm_t top;
};

struct cell_info {
	uint32_t size_cells;
	uint32_t addr_cells;
};

static struct cell_info get_cellinfo(void *fdt, int offset)
{
	struct cell_info ret = {
		fdt_size_cells(fdt, offset),
		fdt_address_cells(fdt, offset)
	};

	return ret;
}

static struct mem_layout get_memlayout(void *fdt)
{
	struct cell_info ci = get_cellinfo(fdt, 0);

	int mem_offset = fdt_path_offset(fdt, "/memory");
	uint8_t *mem_reg =
		(uint8_t *) fdt_getprop(fdt, mem_offset, "reg", NULL);

	pm_t base;
	/* if riscv128 comes around we will probably see addr_cells == 4, but
	 * I'm not too concerned about it at the moment */
	if (ci.addr_cells == 2) {
		base = fdt64_to_cpu(*(fdt64_t *) mem_reg);
		mem_reg += sizeof(fdt64_t);
	} else {
		base = fdt32_to_cpu(*(fdt32_t *) mem_reg);
		mem_reg += sizeof(fdt32_t);
	}

	pm_t top;
	if (ci.size_cells == 2)
		top = fdt64_to_cpu(*(fdt64_t *) mem_reg) + base;
	else
		top = fdt32_to_cpu(*(fdt64_t *) mem_reg) + base;

	struct mem_layout ret = { base, top };
	return ret;
}

#ifdef DEBUG

/* this should probably be improved in the future, possibly also moved
 * somewhere? */
static enum serial_dev_t serial_dev_enum(const char *dev_name)
{
	if (strncmp("ns16550", dev_name, 7) == 0)
		return NS16550A;

	return -1;
}

static void init_debug(void *fdt)
{
	int chosen_offset = fdt_path_offset(fdt, "/chosen");
	const char *stdout = fdt_getprop(fdt, chosen_offset, "stdout-path", 0);

	int stdout_offset = fdt_path_offset(fdt, stdout);

	/* get serial device type */
	const char *dev_name = (const char *)fdt_getprop(fdt, stdout_offset,
			"compatible", NULL);

	enum serial_dev_t dev = serial_dev_enum(dev_name);

	/* get serial device address */
	struct cell_info ci = get_cellinfo(fdt, stdout_offset);
	void *reg_ptr = (void *)fdt_getprop(fdt, stdout_offset, "reg", NULL);

	void *uart_ptr = 0;
	if (ci.addr_cells == 2)
		uart_ptr = (void *)(pm_t)fdt64_to_cpu(*(fdt64_t *) reg_ptr);
	else
		uart_ptr = (void *)(pm_t)fdt32_to_cpu(*(fdt32_t *) reg_ptr);

	dbg_init(uart_ptr, dev);
}

#else
#define init_debug(...)
#endif

/* TODO: these */
static pm_t get_kerneltop(void *fdt)
{
	return 0;
}

static pm_t get_initrdtop(void *fdt)
{
	return 0;
}

static pm_t get_fdttop(void *fdt)
{
	return 0;
}

static void setup_pmem(void *fdt)
{
	struct mem_layout pmem = get_memlayout(fdt);

	pm_t kernel_top = get_kerneltop(fdt);
	pm_t initrd_top = get_initrdtop(fdt);
	pm_t fdt_top = get_fdttop(fdt);

	pm_t top = MAX3(kernel_top, initrd_top, fdt_top);
}

void __noreturn init(void *fdt)
{
	init_debug(fdt);
	dbg_fdt(fdt);
	setup_pmem(fdt);

	/* basic memory layout info */
	struct mem_layout pmem = get_memlayout(fdt);

	/* TODO: find first contiguous region */

	/* generate pagetable at contiguous region */
	populate_pmap(pmem.base, pmem.top - pmem.base, 0/* something */);

	/* TODO: mark all used pages */
	update_pmap(0/* TODO: figure out where in virtual memory the page map should be mapped */);
	/* TODO: jump to kernel */
}