Rev 3827 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
913 | decky | 1 | # |
2071 | jermar | 2 | # Copyright (c) 2006 Martin Decky |
913 | decky | 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 | |||
1058 | decky | 29 | #include "asm.h" |
913 | decky | 30 | #include "regname.h" |
31 | |||
3855 | decky | 32 | .macro FLUSH_CACHE addr |
33 | dcbst 0, \addr |
||
34 | sync |
||
35 | icbi 0, \addr |
||
36 | isync |
||
37 | .endm |
||
38 | |||
913 | decky | 39 | .text |
40 | |||
1058 | decky | 41 | .global halt |
1075 | decky | 42 | .global memcpy |
913 | decky | 43 | .global jump_to_kernel |
44 | |||
1058 | decky | 45 | halt: |
46 | b halt |
||
913 | decky | 47 | |
1075 | decky | 48 | memcpy: |
49 | srwi. r7, r5, 3 |
||
50 | addi r6, r3, -4 |
||
51 | addi r4, r4, -4 |
||
52 | beq 2f |
||
53 | |||
54 | andi. r0, r6, 3 |
||
55 | mtctr r7 |
||
56 | bne 5f |
||
57 | |||
58 | 1: |
||
59 | |||
60 | lwz r7, 4(r4) |
||
61 | lwzu r8, 8(r4) |
||
62 | stw r7, 4(r6) |
||
63 | stwu r8, 8(r6) |
||
64 | bdnz 1b |
||
65 | |||
66 | andi. r5, r5, 7 |
||
67 | |||
68 | 2: |
||
69 | |||
70 | cmplwi 0, r5, 4 |
||
71 | blt 3f |
||
72 | |||
73 | lwzu r0, 4(r4) |
||
74 | addi r5, r5, -4 |
||
75 | stwu r0, 4(r6) |
||
76 | |||
77 | 3: |
||
78 | |||
79 | cmpwi 0, r5, 0 |
||
80 | beqlr |
||
81 | mtctr r5 |
||
82 | addi r4, r4, 3 |
||
83 | addi r6, r6, 3 |
||
84 | |||
85 | 4: |
||
86 | |||
87 | lbzu r0, 1(r4) |
||
88 | stbu r0, 1(r6) |
||
89 | bdnz 4b |
||
90 | blr |
||
91 | |||
92 | 5: |
||
93 | |||
94 | subfic r0, r0, 4 |
||
95 | mtctr r0 |
||
96 | |||
97 | 6: |
||
98 | |||
99 | lbz r7, 4(r4) |
||
100 | addi r4, r4, 1 |
||
101 | stb r7, 4(r6) |
||
102 | addi r6, r6, 1 |
||
103 | bdnz 6b |
||
104 | subf r5, r0, r5 |
||
105 | rlwinm. r7, r5, 32-3, 3, 31 |
||
106 | beq 2b |
||
107 | mtctr r7 |
||
108 | b 1b |
||
109 | |||
110 | |||
913 | decky | 111 | jump_to_kernel: |
1022 | decky | 112 | |
1131 | decky | 113 | # r3 = bootinfo (pa) |
114 | # r4 = bootinfo_size |
||
115 | # r5 = trans (pa) |
||
1372 | decky | 116 | # r6 = bytes to copy |
117 | # r7 = real_mode (pa) |
||
1478 | decky | 118 | # r8 = framebuffer (pa) |
119 | # r9 = scanline |
||
1022 | decky | 120 | |
1216 | decky | 121 | # disable interrupts |
122 | |||
123 | mfmsr r31 |
||
124 | rlwinm r31, r31, 0, 17, 15 |
||
125 | mtmsr r31 |
||
126 | |||
127 | # set real_mode meeting point address |
||
128 | |||
1372 | decky | 129 | mtspr srr0, r7 |
1022 | decky | 130 | |
131 | # jumps to real_mode |
||
132 | |||
1058 | decky | 133 | mfmsr r31 |
134 | lis r30, ~0@h |
||
1478 | decky | 135 | ori r30, r30, ~(msr_ir | msr_dr | msr_ee)@l |
1058 | decky | 136 | and r31, r31, r30 |
137 | mtspr srr1, r31 |
||
1157 | decky | 138 | |
139 | sync |
||
140 | isync |
||
1022 | decky | 141 | rfi |
142 | |||
1214 | decky | 143 | .section REALMODE, "ax" |
1058 | decky | 144 | .align PAGE_WIDTH |
1022 | decky | 145 | .global real_mode |
146 | |||
147 | real_mode: |
||
1479 | decky | 148 | |
1058 | decky | 149 | # copy kernel to proper location |
150 | # |
||
1131 | decky | 151 | # r5 = trans (pa) |
1372 | decky | 152 | # r6 = bytes to copy |
1478 | decky | 153 | # r8 = framebuffer (pa) |
154 | # r9 = scanline |
||
1058 | decky | 155 | |
1068 | decky | 156 | li r31, PAGE_SIZE >> 2 |
1058 | decky | 157 | li r30, 0 |
158 | |||
159 | page_copy: |
||
160 | |||
1131 | decky | 161 | cmpwi r6, 0 |
1058 | decky | 162 | beq copy_end |
163 | |||
1068 | decky | 164 | # copy page |
1058 | decky | 165 | |
166 | mtctr r31 |
||
1131 | decky | 167 | lwz r29, 0(r5) |
1058 | decky | 168 | |
169 | copy_loop: |
||
170 | |||
171 | lwz r28, 0(r29) |
||
172 | stw r28, 0(r30) |
||
173 | |||
3855 | decky | 174 | FLUSH_CACHE r30 |
175 | |||
1058 | decky | 176 | addi r29, r29, 4 |
177 | addi r30, r30, 4 |
||
1131 | decky | 178 | subi r6, r6, 4 |
1058 | decky | 179 | |
1131 | decky | 180 | cmpwi r6, 0 |
1068 | decky | 181 | beq copy_end |
182 | |||
1058 | decky | 183 | bdnz copy_loop |
1479 | decky | 184 | |
1131 | decky | 185 | addi r5, r5, 4 |
1058 | decky | 186 | b page_copy |
187 | |||
188 | copy_end: |
||
1068 | decky | 189 | |
1214 | decky | 190 | # initially fill segment registers |
1728 | decky | 191 | |
1071 | decky | 192 | li r31, 0 |
1728 | decky | 193 | |
194 | li r29, 8 |
||
195 | mtctr r29 |
||
1737 | decky | 196 | li r30, 0 # ASID 0 (VSIDs 0 .. 7) |
3855 | decky | 197 | |
1728 | decky | 198 | seg_fill_uspace: |
1022 | decky | 199 | |
1071 | decky | 200 | mtsrin r30, r31 |
1728 | decky | 201 | addi r30, r30, 1 |
1071 | decky | 202 | addis r31, r31, 0x1000 # move to next SR |
203 | |||
1728 | decky | 204 | bdnz seg_fill_uspace |
1022 | decky | 205 | |
1728 | decky | 206 | li r29, 8 |
207 | mtctr r29 |
||
1737 | decky | 208 | lis r30, 0x4000 # priviledged access only |
209 | ori r30, r30, 8 # ASID 0 (VSIDs 8 .. 15) |
||
1728 | decky | 210 | |
211 | seg_fill_kernel: |
||
212 | |||
213 | mtsrin r30, r31 |
||
214 | addi r30, r30, 1 |
||
215 | addis r31, r31, 0x1000 # move to next SR |
||
216 | |||
217 | bdnz seg_fill_kernel |
||
218 | |||
1068 | decky | 219 | # invalidate block address translation registers |
220 | |||
1373 | decky | 221 | li r30, 0 |
222 | |||
1068 | decky | 223 | mtspr ibat0u, r30 |
224 | mtspr ibat0l, r30 |
||
225 | |||
226 | mtspr ibat1u, r30 |
||
227 | mtspr ibat1l, r30 |
||
228 | |||
229 | mtspr ibat2u, r30 |
||
230 | mtspr ibat2l, r30 |
||
231 | |||
232 | mtspr ibat3u, r30 |
||
233 | mtspr ibat3l, r30 |
||
234 | |||
235 | mtspr dbat0u, r30 |
||
236 | mtspr dbat0l, r30 |
||
237 | |||
238 | mtspr dbat1u, r30 |
||
239 | mtspr dbat1l, r30 |
||
240 | |||
241 | mtspr dbat2u, r30 |
||
242 | mtspr dbat2l, r30 |
||
243 | |||
244 | mtspr dbat3u, r30 |
||
245 | mtspr dbat3l, r30 |
||
246 | |||
1738 | decky | 247 | # create empty Page Hash Table |
248 | # on top of memory, size 64 KB |
||
1725 | decky | 249 | |
1738 | decky | 250 | lwz r31, 0(r3) # r31 = memory size |
1725 | decky | 251 | |
1738 | decky | 252 | lis r30, 65536@h |
253 | ori r30, r30, 65536@l # r30 = 65536 |
||
254 | |||
255 | subi r29, r30, 1 # r29 = 65535 |
||
256 | |||
257 | sub r31, r31, r30 |
||
258 | andc r31, r31, r29 # pht = ALIGN_DOWN(memory_size - 65536, 65536) |
||
259 | |||
260 | mtsdr1 r31 |
||
261 | |||
262 | li r29, 2 |
||
263 | srw r30, r30, r29 # r30 = 16384 |
||
1728 | decky | 264 | li r29, 0 |
265 | |||
266 | pht_clear: |
||
267 | |||
1737 | decky | 268 | # write zeroes |
269 | |||
1728 | decky | 270 | stw r29, 0(r31) |
3855 | decky | 271 | FLUSH_CACHE r31 |
1728 | decky | 272 | |
273 | addi r31, r31, 4 |
||
274 | subi r30, r30, 4 |
||
275 | |||
276 | cmpwi r30, 0 |
||
277 | beq clear_end |
||
278 | |||
279 | bdnz pht_clear |
||
280 | |||
281 | clear_end: |
||
282 | |||
283 | #ifdef CONFIG_BAT |
||
284 | |||
1737 | decky | 285 | # create BAT identity mapping |
286 | |||
1738 | decky | 287 | lwz r31, 0(r3) # r31 = memory size |
1068 | decky | 288 | |
1737 | decky | 289 | lis r29, 0x0002 |
290 | cmpw r31, r29 |
||
291 | blt no_bat # less than 128 KB -> no BAT |
||
1068 | decky | 292 | |
1737 | decky | 293 | li r29, 18 |
294 | srw r31, r31, r29 # r31 = total >> 18 |
||
295 | |||
296 | # create Block Length mask by replicating |
||
297 | # the leading logical one 14 times |
||
298 | |||
299 | li r29, 14 |
||
300 | mtctr r31 |
||
301 | li r29, 1 |
||
302 | |||
303 | bat_mask: |
||
304 | srw r30, r31, r29 # r30 = mask >> 1 |
||
305 | or r31, r31, r30 # mask = mask | r30 |
||
306 | |||
307 | bdnz bat_mask |
||
308 | |||
309 | andi. r31, r31, 0x07ff # mask = mask & 0x07ff (BAT can map up to 256 MB) |
||
310 | |||
311 | li r29, 2 |
||
312 | slw r31, r31, r29 # mask = mask << 2 |
||
313 | ori r31, r31, 0x0002 # mask = mask | 0x0002 (priviledged access only) |
||
314 | |||
315 | lis r29, 0x8000 |
||
316 | or r29, r29, r31 |
||
317 | |||
1068 | decky | 318 | lis r30, 0x0000 |
319 | ori r30, r30, 0x0002 |
||
320 | |||
1737 | decky | 321 | mtspr ibat0u, r29 |
1068 | decky | 322 | mtspr ibat0l, r30 |
323 | |||
1737 | decky | 324 | mtspr dbat0u, r29 |
1068 | decky | 325 | mtspr dbat0l, r30 |
1737 | decky | 326 | |
327 | no_bat: |
||
3824 | decky | 328 | |
1728 | decky | 329 | #endif |
1068 | decky | 330 | |
1381 | decky | 331 | tlbsync |
1058 | decky | 332 | |
333 | # start the kernel |
||
1022 | decky | 334 | # |
1728 | decky | 335 | # pc = KERNEL_START_ADDR |
1131 | decky | 336 | # r3 = bootinfo (pa) |
1728 | decky | 337 | # sprg0 = KA2PA(KERNEL_START_ADDR) |
338 | # sprg3 = physical memory size |
||
339 | # sp = 0 (pa) |
||
1022 | decky | 340 | |
1058 | decky | 341 | lis r31, KERNEL_START_ADDR@ha |
342 | addi r31, r31, KERNEL_START_ADDR@l |
||
1022 | decky | 343 | |
1058 | decky | 344 | mtspr srr0, r31 |
1022 | decky | 345 | |
1728 | decky | 346 | subis r31, r31, 0x8000 |
347 | mtsprg0 r31 |
||
348 | |||
349 | lwz r31, 0(r3) |
||
350 | mtsprg3 r31 |
||
351 | |||
352 | li sp, 0 |
||
353 | |||
1058 | decky | 354 | mfmsr r31 |
355 | ori r31, r31, (msr_ir | msr_dr)@l |
||
356 | mtspr srr1, r31 |
||
357 | |||
1146 | decky | 358 | sync |
359 | isync |
||
1022 | decky | 360 | rfi |
1058 | decky | 361 | |
362 | .align PAGE_WIDTH |
||
363 | .global trans |
||
364 | trans: |
||
365 | .space (TRANS_SIZE * TRANS_ITEM_SIZE) |