summaryrefslogtreecommitdiff
path: root/rtl/core/mmu/pagewalk.sv
blob: b16ce260cbc092d82535557b7c25dd7f38406e81 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
`include "core/mmu/format.sv"
`include "core/uarch.sv"

module core_mmu_pagewalk
(
	input  logic      clk,
	                  rst_n,

	input  logic      mmu_enable,
	input  mmu_base   mmu_ttbr,

	input  logic      bus_ready,
	input  word       bus_data_rd,

	input  ptr        core_addr,
	input  word       core_data_wr,
	input  logic[3:0] core_data_be,
	input  logic      core_start,
	                  core_write,

	output ptr        bus_addr,
	output word       bus_data_wr,
	output logic[3:0] bus_data_be,
	output logic      bus_start,
	                  bus_write,

	output logic      core_ready,
	output word       core_data_rd
);

	enum int unsigned
	{
		IDLE,
		L1,
		L2,
		DATA,
		FAULT
	} state;

	mmu_l1_pagetable pagetable;
	assign pagetable = bus_data_rd;

	mmu_l1_section section;
	assign section = bus_data_rd;

	mmu_l2_large ptentry_large;
	assign ptentry_large = bus_data_rd;

	mmu_l2_small ptentry_small;
	assign ptentry_small = bus_data_rd;

	ptr target;
	word hold_data;
	logic[3:0] hold_be;

	logic hold_write;

	always @(posedge clk or negedge rst_n)
		if(!rst_n) begin
			state <= IDLE;
			target <= 0;

			hold_be <= 0;
			hold_data <= 0;
			hold_write <= 0;

			bus_addr <= 0;
			bus_start <= 0;
			bus_write <= 0;
			bus_data_be <= 0;
			bus_data_wr <= 0;

			core_ready <= 0;
			core_data_rd <= 0;
		end else begin
			if(bus_start)
				bus_start <= 0;

			if(core_ready)
				core_ready <= 0;

			unique case(state)
				IDLE:
					if(core_start) begin
						bus_start <= 1;

						if(mmu_enable) begin
							target <= core_addr;
							hold_be <= core_data_be;
							hold_data <= core_data_wr;
							hold_write <= core_write;

							state <= L1;
							bus_addr <= {mmu_ttbr, core_addr `MMU_L1_INDEX};
						end else begin
							state <= DATA;
							bus_addr <= core_addr;
							bus_write <= core_write;
							bus_data_wr <= core_data_wr;
							bus_data_be <= core_data_be;
						end
					end

				L1:
					if(bus_ready)
						unique case(bus_data_rd[1:0])
							`MMU_L1_PAGETABLE: begin
								state <= L2;
								bus_addr <= {pagetable.base, target `MMU_L2_INDEX};
								bus_start <= 1;
							end

							`MMU_L1_SECTION: begin
								state <= DATA;

								bus_addr <= {section.base, target `MMU_SECTION_INDEX};
								bus_start <= 1;
								bus_write <= hold_write;
								bus_data_wr <= hold_data;
								bus_data_be <= hold_be;
							end

							// Tiny (1KiB wtf?) pages and supersections not supported
							default:
								state <= FAULT;
						endcase

				L2:
					if(bus_ready) begin
						state <= DATA;

						bus_write <= hold_write;
						bus_data_wr <= hold_data;
						bus_data_be <= hold_be;

						unique case(bus_data_rd[1:0])
							`MMU_L2_FAULT:
								state <= FAULT;

							`MMU_L2_LARGE: begin
								bus_addr <= {ptentry_large.base, target `MMU_LARGE_INDEX};
								bus_start <= 1;
							end

							`MMU_L2_SMALL, `MMU_L2_SMALLEXT: begin
								bus_addr <= {ptentry_small.base, target `MMU_SMALL_INDEX};
								bus_start <= 1;
							end
						endcase
					end

				DATA:
					if(bus_ready) begin
						state <= IDLE;
						bus_write <= 0;
						core_ready <= 1;
						core_data_rd <= bus_data_rd;
					end

				//TODO
				FAULT: ;
			endcase
		end

endmodule