Debian Codegraph @

src/arch/ppc64/bootblock_crt0.S

raw
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Early initialization code for POWER8/POWER9.
4 */
5
6 #include <cpu/power/spr.h>
7
8 #define FIXUP_ENDIAN \
9 tdi 0,0,0x48; /* Reverse endian of b . + 8 */ \
10 b $+44; /* Skip trampoline if endian is good */ \
11 .long 0xa600607d; /* mfmsr r11 */ \
12 .long 0x01006b69; /* xori r11,r11,1 */ \
13 .long 0x00004039; /* li r10,0 */ \
14 .long 0x6401417d; /* mtmsrd r10,1 */ \
15 .long 0x05009f42; /* bcl 20,31,$+4 */ \
16 .long 0xa602487d; /* mflr r10 */ \
17 .long 0x14004a39; /* addi r10,r10,20 */ \
18 .long 0xa6035a7d; /* mtsrr0 r10 */ \
19 .long 0xa6037b7d; /* mtsrr1 r11 */ \
20 .long 0x2400004c /* rfid */
21
22 /* Load an immediate 64-bit value into a register */
23 #define LOAD_IMM64(r, e) \
24 lis r,(e)@highest; \
25 ori r,r,(e)@higher; \
26 rldicr r,r, 32, 31; \
27 oris r,r, (e)@h; \
28 ori r,r, (e)@l;
29
30 /* Load an immediate 32-bit value into a register */
31 #define LOAD_IMM32(r, e) \
32 li r, 0; \
33 oris r,r, (e)@h; \
34 ori r,r, (e)@l;
35
36 /*
37 * On POWER, 0 is wherever HRMOR points to rather than physical DRAM start.
38 * HRMOR is ORed with address, not added to it, meaning that memory space
39 * overlaps after 2^(least significant set bit of HRMOR). This becomes
40 * chaotic when nonconsecutive bits are set...
41 *
42 * Two and a half possible cases:
43 * 0. bootblock started with QEMU in hb-mode
44 * - NIA = 0x10 (bug?)
45 * - HRMOR = 0x08000000 (128M)
46 * - no physical memory to enable/train, everything accessible from start
47 * 1. bootblock loaded by HBBL
48 * - NIA = 0
49 * - HRMOR = 0xF8000000 (4G - 128M)
50 * - initialized L3 = 0x400000 (4M)
51 * - top address before RAM = 0xF8400000
52 * 2. bootblock in SEEPROM, loaded by SBE
53 * - NIA = 0x3000 (placeholder for int. vectors)
54 * - HRMOR = 0xF8200000 (4G - 128 M + 2 M)
55 * - initialized L3 = 0x8000 (bootblock/HBBL size = 32K)
56 * - no way 32K will be enough, must initialize more L3 in bootblock
57 * - HRMOR still applies, so memory overlaps every 2M
58 *
59 * Common subset (assuming 2. initializes as much memory as possible) is
60 * 0xF8200000-0xF8400000. 2M should be more than enough for pre-RAM code,
61 * but it isn't enough to load ramstage. We could implement postcar stage,
62 * but KISS: initialize L3 from _ebootblock to 0xF8980000: up to 9.5M into
63 * cache, leaving bottom 2M (0xF8000000-0xF8200000) either uninitialized
64 * (when started from SEEPROM) or just unused for anything but bootblock
65 * (loaded by HBBL). Last 0.5M of L3 cache is left for interrupt vectors
66 * normally located at address 0.
67 *
68 * Set HRMOR to 0 before jumping to C code in bootblock and forget it even
69 * exists.
70 *
71 * For QEMU s/0xF8/0x08/ in above description but code remains the same.
72 * L3 initialization is unnecessary in this case but won't break anything.
73 *
74 * TODO: there is a structure with SBE->HBBL data at 0 in 2nd option. It
75 * holds some useful data like XSCOM BAR and LPC BAR. If, for any reason,
76 * these addresses are different than default, they should be used instead
77 * of predefined values.
78 */
79
80 .section ".text._start", "ax", %progbits
81 .globl _start
82 _start:
83 /* QEMU with hb-mode=on starts at address 0x10, while hardware at 0x0 */
84 #if !CONFIG(BOOTBLOCK_IN_SEEPROM)
85 nop
86 nop
87 nop
88 nop
89 FIXUP_ENDIAN
90 #endif
91
92 /* Store FDT address provided by QEMU in %r3 to pass it later to
93 * payload */
94 mtspr SPR_HSPRG0, %r3
95
96 /* Set program priority to medium */
97 or %r2, %r2, %r2
98
99 li %r10, 1
100 rotldi %r10, %r10, 63 /* MSB is "ignore HRMOR" */
101
102 /* Assumption: we are linked at address that isn't changed by HRMOR */
103 LOAD_IMM32(%r7, ignoreHRMOR)
104 or %r9, %r7, %r10
105
106 mtlr %r9
107 blr
108
109 ignoreHRMOR:
110 /* Now we are at 0x8000000000000000 | linked address */
111 li %r0, 0
112 mtspr SPR_HRMOR, %r0 /* Clear HRMOR */
113 isync
114
115 /* We can't just "b stopIgnoringHRMOR", it would use relative offset */
116 addi %r9, %r7, stopIgnoringHRMOR - ignoreHRMOR
117 mtlr %r9
118 blr
119
120 stopIgnoringHRMOR:
121 /* Now we are at linked address */
122 slbia 7
123 sync
124 isync
125
126 /*
127 * When coming from SBE, L3 cache is invalid except for [2M, end of HBBL]
128 * range. Make the rest of it valid, or embrace the checkstops.
129 */
130
131 /* Validate and initialize to zeroes [end of HBBL, 9.5M] range */
132 LOAD_IMM32(%r5, _ebootblock) /* Assume it is at least 128B aligned */
133 LOAD_IMM32(%r6, _epreram_cbfs_cache) /* Same */
134 addi %r6, %r6, -1
135 1:
136 dcbz 0, %r5
137 addi %r5, %r5, 128
138 cmpld cr7, %r5, %r6
139 blt cr7, 1b
140
141 /* Stack */
142 LOAD_IMM32(%r1, _estack)
143
144 /* This is tested by checkstack() just before jumping to payload */
145 LOAD_IMM64(%r3, 0xDEADBEEFDEADBEEF)
146 LOAD_IMM32(%r5, _stack - 8)
147 sub %r4, %r1, %r5
148 sradi %r4, %r4, 3 /* Divide by 8 */
149 mtctr %r4
150 1:
151 stdu %r3, 8(%r5)
152 bc 25, 0, 1b
153
154 /* Enable floating point and vector operations */
155 /* Vector operations are sometimes generated for code like
156 * 'uint8_t x[32] = {0}', this results in an exception when vector
157 * registers (VEC) are not enabled. VSX (vector-scalar extension) is
158 * also enabled, there is no reason not to. Floating point must also be
159 * enabled for VSX.
160 */
161 mfmsr %r3
162 ori %r3, %r3, 0x2000 /* FP = 1 */
163 oris %r3, %r3, 0x0280 /* VEC = 1, VSX = 1 */
164 mtmsrd %r3
165
166 /* Load official procedure descriptor address for main() */
167 LOAD_IMM32(%r12, main)
168
169 /* Load TOC pointer and jump to main() */
170 ld %r2, 8(%r12)
171 b main