Rev 3228 | Rev 3231 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2005 Jakub Jermar |
1 | jermar | 3 | * All rights reserved. |
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1776 | jermar | 29 | /** @addtogroup mips32mm |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | /** @file |
||
33 | */ |
||
34 | |||
3228 | decky | 35 | #include <macros.h> |
1 | jermar | 36 | #include <arch/mm/frame.h> |
3228 | decky | 37 | #include <arch/mm/tlb.h> |
3230 | decky | 38 | #include <interrupt.h> |
1 | jermar | 39 | #include <mm/frame.h> |
3228 | decky | 40 | #include <mm/asid.h> |
368 | jermar | 41 | #include <config.h> |
3228 | decky | 42 | #include <arch/drivers/msim.h> |
43 | #include <arch/drivers/serial.h> |
||
44 | #include <print.h> |
||
1 | jermar | 45 | |
3230 | decky | 46 | #define ZERO_PAGE_MASK TLB_PAGE_MASK_1M |
3228 | decky | 47 | #define ZERO_FRAMES 4096 |
48 | #define ZERO_PAGE_WIDTH 20 /* 1M */ |
||
49 | #define ZERO_PAGE_SIZE (1 << ZERO_PAGE_WIDTH) |
||
50 | #define ZERO_PAGE_ASID ASID_INVALID |
||
51 | #define ZERO_PAGE_TLBI 0 |
||
52 | #define ZERO_PAGE_ADDR 0 |
||
53 | #define ZERO_PAGE_OFFSET (ZERO_PAGE_SIZE / sizeof(uint32_t) - 1) |
||
3230 | decky | 54 | #define ZERO_PAGE_VALUE (((volatile uint32_t *) ZERO_PAGE_ADDR)[ZERO_PAGE_OFFSET]) |
3228 | decky | 55 | |
56 | #define MAX_REGIONS 32 |
||
57 | |||
58 | typedef struct { |
||
59 | pfn_t start; |
||
60 | pfn_t count; |
||
61 | } phys_region_t; |
||
62 | |||
63 | static count_t phys_regions_count = 0; |
||
64 | static phys_region_t phys_regions[MAX_REGIONS]; |
||
65 | |||
66 | |||
67 | /** Check whether frame is available |
||
68 | * |
||
69 | * Returns true if given frame is generally available for use. |
||
70 | * Returns false if given frame is used for physical memory |
||
71 | * mapped devices and cannot be used. |
||
72 | * |
||
73 | */ |
||
74 | static bool frame_available(pfn_t frame) |
||
75 | { |
||
76 | /* MSIM device (dprinter) */ |
||
77 | if (frame == (KA2PA(MSIM_VIDEORAM) >> ZERO_PAGE_WIDTH)) |
||
78 | return false; |
||
79 | |||
80 | /* MSIM device (dkeyboard) */ |
||
81 | if (frame == (KA2PA(MSIM_KBD_ADDRESS) >> ZERO_PAGE_WIDTH)) |
||
82 | return false; |
||
83 | |||
84 | /* Simics device (serial line) */ |
||
85 | if (frame == (KA2PA(SERIAL_ADDRESS) >> ZERO_PAGE_WIDTH)) |
||
86 | return false; |
||
87 | |||
88 | return true; |
||
89 | } |
||
90 | |||
91 | |||
92 | /** Check whether frame is safe to write |
||
93 | * |
||
94 | * Returns true if given frame is safe for read/write test. |
||
95 | * Returns false if given frame should not be touched. |
||
96 | * |
||
97 | */ |
||
98 | static bool frame_safe(pfn_t frame) __attribute__((unused)); |
||
99 | static bool frame_safe(pfn_t frame) |
||
100 | { |
||
101 | /* Kernel structures */ |
||
102 | if ((frame << ZERO_PAGE_WIDTH) < KA2PA(config.base)) |
||
103 | return false; |
||
104 | |||
105 | /* Kernel */ |
||
106 | if (overlaps(frame << ZERO_PAGE_WIDTH, ZERO_PAGE_SIZE, |
||
107 | KA2PA(config.base), config.kernel_size)) |
||
108 | return false; |
||
109 | |||
110 | /* Kernel stack */ |
||
111 | if (overlaps(frame << ZERO_PAGE_WIDTH, ZERO_PAGE_SIZE, |
||
112 | KA2PA(config.stack_base), config.stack_size)) |
||
113 | return false; |
||
114 | |||
115 | /* Init tasks */ |
||
116 | bool safe = true; |
||
117 | count_t i; |
||
118 | for (i = 0; i < init.cnt; i++) |
||
119 | if (overlaps(frame << ZERO_PAGE_WIDTH, ZERO_PAGE_SIZE, |
||
120 | KA2PA(init.tasks[i].addr), init.tasks[i].size)) { |
||
121 | safe = false; |
||
122 | break; |
||
123 | } |
||
124 | |||
125 | return safe; |
||
126 | } |
||
127 | |||
128 | static void frame_add_region(pfn_t start_frame, pfn_t end_frame) |
||
129 | { |
||
130 | if (end_frame > start_frame) { |
||
131 | /* Convert 1M frames to 16K frames */ |
||
132 | pfn_t first = ADDR2PFN(start_frame << ZERO_PAGE_WIDTH); |
||
3230 | decky | 133 | pfn_t count = ADDR2PFN((end_frame - start_frame) << ZERO_PAGE_WIDTH); |
3228 | decky | 134 | |
135 | /* Interrupt vector frame is blacklisted */ |
||
136 | pfn_t conf_frame; |
||
137 | if (start_frame == 0) |
||
138 | conf_frame = 1; |
||
139 | else |
||
140 | conf_frame = first; |
||
141 | |||
142 | zone_create(first, count, conf_frame, 0); |
||
143 | |||
144 | if (phys_regions_count < MAX_REGIONS) { |
||
145 | phys_regions[phys_regions_count].start = first; |
||
146 | phys_regions[phys_regions_count].count = count; |
||
147 | phys_regions_count++; |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | |||
573 | palkovsky | 153 | /** Create memory zones |
154 | * |
||
3228 | decky | 155 | * Walk through available 1 MB chunks of physical |
156 | * memory and create zones. |
||
157 | * |
||
3230 | decky | 158 | * Note: It is assumed that the TLB is not yet being |
159 | * used in any way, thus there is no interference. |
||
160 | * |
||
573 | palkovsky | 161 | */ |
1 | jermar | 162 | void frame_arch_init(void) |
163 | { |
||
3230 | decky | 164 | ipl_t ipl = interrupts_disable(); |
3228 | decky | 165 | |
3230 | decky | 166 | /* Clear and initialize TLB */ |
167 | cp0_pagemask_write(ZERO_PAGE_MASK); |
||
168 | cp0_entry_lo0_write(0); |
||
169 | cp0_entry_lo1_write(0); |
||
170 | cp0_entry_hi_write(0); |
||
171 | |||
172 | count_t i; |
||
173 | for (i = 0; i < TLB_ENTRY_COUNT; i++) { |
||
174 | cp0_index_write(i); |
||
175 | tlbwi(); |
||
176 | } |
||
3228 | decky | 177 | |
178 | pfn_t start_frame = 0; |
||
179 | pfn_t frame; |
||
180 | bool avail = true; |
||
181 | |||
182 | /* Walk through all 1 MB frames */ |
||
183 | for (frame = 0; frame < ZERO_FRAMES; frame++) { |
||
184 | if (!frame_available(frame)) |
||
185 | avail = false; |
||
186 | else { |
||
187 | if (frame_safe(frame)) { |
||
188 | entry_lo_t lo0; |
||
189 | entry_lo_t lo1; |
||
190 | entry_hi_t hi; |
||
191 | tlb_prepare_entry_lo(&lo0, false, true, true, false, frame << (ZERO_PAGE_WIDTH - 12)); |
||
192 | tlb_prepare_entry_lo(&lo1, false, false, false, false, 0); |
||
193 | tlb_prepare_entry_hi(&hi, ZERO_PAGE_ASID, ZERO_PAGE_ADDR); |
||
194 | |||
3230 | decky | 195 | cp0_pagemask_write(ZERO_PAGE_MASK); |
3228 | decky | 196 | cp0_entry_lo0_write(lo0.value); |
197 | cp0_entry_lo1_write(lo1.value); |
||
198 | cp0_entry_hi_write(hi.value); |
||
3230 | decky | 199 | cp0_index_write(ZERO_PAGE_TLBI); |
3228 | decky | 200 | tlbwi(); |
201 | |||
202 | ZERO_PAGE_VALUE = 0; |
||
203 | if (ZERO_PAGE_VALUE != 0) |
||
204 | avail = false; |
||
205 | else { |
||
206 | ZERO_PAGE_VALUE = 0xdeadbeef; |
||
207 | if (ZERO_PAGE_VALUE != 0xdeadbeef) |
||
208 | avail = false; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | if (!avail) { |
||
214 | frame_add_region(start_frame, frame); |
||
215 | start_frame = frame + 1; |
||
216 | avail = true; |
||
217 | } |
||
814 | palkovsky | 218 | } |
3228 | decky | 219 | |
220 | frame_add_region(start_frame, frame); |
||
221 | |||
3230 | decky | 222 | /* Blacklist interrupt vector frame */ |
223 | frame_mark_unavailable(0, 1); |
||
224 | |||
225 | /* Cleanup */ |
||
226 | cp0_pagemask_write(ZERO_PAGE_MASK); |
||
227 | cp0_entry_lo0_write(0); |
||
228 | cp0_entry_lo1_write(0); |
||
229 | cp0_entry_hi_write(0); |
||
3228 | decky | 230 | cp0_index_write(ZERO_PAGE_TLBI); |
231 | tlbwi(); |
||
232 | |||
3230 | decky | 233 | interrupts_restore(ipl); |
1 | jermar | 234 | } |
1702 | cejka | 235 | |
3228 | decky | 236 | |
237 | void physmem_print(void) |
||
238 | { |
||
239 | printf("Base Size\n"); |
||
240 | printf("---------- ----------\n"); |
||
241 | |||
242 | count_t i; |
||
243 | for (i = 0; i < phys_regions_count; i++) { |
||
244 | printf("%#10x %10u\n", |
||
245 | PFN2ADDR(phys_regions[i].start), PFN2ADDR(phys_regions[i].count)); |
||
246 | } |
||
247 | } |
||
248 | |||
1776 | jermar | 249 | /** @} |
1702 | cejka | 250 | */ |