ofw_subr.S revision 1.19 1 /* $NetBSD: ofw_subr.S,v 1.19 2021/02/24 17:35:39 thorpej Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifdef _KERNEL_OPT
35 #include "opt_ppcarch.h"
36 #endif
37
38 /* Stack used to call into OpenFirmware. */
39 .lcomm firmstk,NBPG,16
40
41 /* Buffer used to pass data to/from OpenFirmware. */
42 .lcomm OF_buffer,NBPG + 36,4
43
44 /* The OpenFirmware entry point, provided by OpenFirmware. */
45 .lcomm ofentry,4,4
46
47 /* Entry trampoline used by openfirmware(). */
48 .lcomm oftramp,4,4
49
50 /* Client SR save area */
51 .lcomm clsrsave,64,4
52
53 /* MSR used in OpenFirmware */
54 .globl ofwmsr
55 .comm ofwmsr,4,4
56
57 #ifdef FIRMWORKSBUGS
58 .lcomm ofwreal_incharge,4,4
59 #endif
60
61 /*
62 * Called by start to save the initial OFW state so we can restore it
63 * when call back to OFW.
64 *
65 * We expect the registers to be as for the entry point into the kernel:
66 *
67 * %r1 Stack provided by OpenFirmware / boot loader
68 * %r5 OpenFirmware client entry point
69 *
70 * (others -- don't care)
71 */
72 ENTRY_NOPROFILE(ofwinit)
73 /* Save return address, Push a stack frame. */
74 mflr %r0
75 stw %r0,4(%r1)
76 stwu %r1,-16(%r1)
77
78 #ifdef FIRMWORKSBUGS
79 mfmsr %r0
80 andi. %r0,%r0,PSL_IR|PSL_DR
81 beq 1f
82
83 li %r8,1
84 lis %r9,ofwreal_incharge@ha
85 stw %r8,ofwreal_incharge@l(%r9)
86
87 bl _C_LABEL(ofwr_init)
88 1:
89 #endif
90
91 lis %r8,ofentry@ha
92 stw %r5,ofentry@l(%r8) /* save client interface handler */
93
94 /*
95 * Call directly into OpenFirmware until we're ready to use
96 * the trampoline.
97 */
98 lis %r8,oftramp@ha
99 stw %r5,oftramp@l(%r8)
100
101 /* Save the MSR that OpenFirmware is using. */
102 mfmsr %r0
103 lis %r9,ofwmsr@ha
104 stw %r0,ofwmsr@l(%r9)
105
106 lis %r8,OF_buffer@ha
107 addi %r8,%r8,OF_buffer@l
108 lis %r9,_C_LABEL(OF_buf)@ha
109 stw %r8,_C_LABEL(OF_buf)@l(%r9)
110
111 /*
112 * Call into C code to perform additional early initialization.
113 */
114 lis %r8,_C_LABEL(ofw_bootstrap)@ha
115 addi %r8,%r8,_C_LABEL(ofw_bootstrap)@l
116 mtctr %r8
117 bctrl
118
119 /*
120 * Jump off the trampoline for all subsequent calls
121 * into OpenFirmware.
122 */
123 lis %r5,_C_LABEL(openfirmware_trampoline)@ha
124 addi %r5,%r5,_C_LABEL(openfirmware_trampoline)@l
125 lis %r8,oftramp@ha
126 stw %r5,oftramp@l(%r8)
127
128 /* Retrieve LR, pop stack frame. */
129 addi %r1,%r1,16
130 lwz %r0,4(%r1)
131 mtlr %r0
132
133 blr
134
135 /*
136 * OpenFirmware trampoline. We are already on the OpenFirmware stack.
137 */
138 ENTRY_NOPROFILE(openfirmware_trampoline)
139 mflr %r0
140 stw %r0,4(%r1) /* save return address */
141
142 /*
143 * Push stack frame and save area:
144 *
145 * [SP+8 save area]
146 * [SP+4 slot for saved LR in callee]
147 * [SP+0 saved SP]
148 */
149 stwu %r1,-48(%r1)
150
151 lis %r4,ofentry@ha /* get firmware entry point */
152 lwz %r4,ofentry@l(%r4)
153 mtlr %r4
154
155 #ifdef FIRMWORKSBUGS
156 lis %r4,ofwreal_incharge@ha
157 lwz %r4,ofwreal_incharge@l(%r4)
158 cmpwi %r4,1
159 bne 1f
160 blrl
161 b 4f
162 1:
163 #endif
164 mfmsr %r4 /* save msr */
165 stw %r4,8(%r1)
166
167 li %r0,0 /* clear battable translations */
168 mtmsr %r0
169 #if defined (PPC_OEA) || defined (PPC_OEA64_BRIDGE)
170 mtdbatu 2,%r0
171 mtdbatu 3,%r0
172 mtibatu 2,%r0
173 mtibatu 3,%r0
174 #endif /* PPC_OEA */
175
176 lis %r4,clsrsave@ha /* save current SRs */
177 addi %r4,%r4,clsrsave@l
178 li %r5,0
179 1: mfsrin %r0,%r5
180 stw %r0,0(%r4)
181 addi %r4,%r4,4
182 addis %r5,%r5,0x10000000@h
183 cmpwi %r5,0
184 bne 1b
185
186 lis %r4,_C_LABEL(ofw_pmap)@ha /* load OFW SR */
187 addi %r4,%r4,_C_LABEL(ofw_pmap)@l
188 lwz %r0,PM_KERNELSR(%r4)
189 cmpwi %r0,0 /* pm_sr[KERNEL_SR] == 0? */
190 beq 2f /* then skip (not initialized yet) */
191 li %r5,0
192 1: lwz %r0,0(%r4)
193 mtsrin %r0,%r5
194 addi %r4,%r4,4
195 addis %r5,%r5,0x10000000@h
196 cmpwi %r5,0
197 bne 1b
198 2:
199 /* curcpu()->ci_battable = &ofw_battable */
200 GET_CPUINFO(%r4)
201 lis %r5,_C_LABEL(ofw_battable)@ha
202 addi %r5,%r5,_C_LABEL(ofw_battable)@l
203 stw %r5,CI_BATTABLE(%r4)
204
205 lis %r4,ofwmsr@ha /* Open Firmware msr */
206 lwz %r5,ofwmsr@l(%r4)
207 mtmsr %r5
208 isync
209
210 blrl /* call Open Firmware */
211
212 /* curcpu()->ci_battable = &battable */
213 GET_CPUINFO(%r4)
214 lis %r5,_C_LABEL(battable)@ha
215 addi %r5,%r5,_C_LABEL(battable)@l
216 stw %r5,CI_BATTABLE(%r4)
217
218 lis %r4,clsrsave@ha /* restore saved SRs */
219 addi %r4,%r4,clsrsave@l
220 li %r5,0
221 1: lwz %r0,0(%r4)
222 mtsrin %r0,%r5
223 addi %r4,%r4,4
224 addis %r5,%r5,0x10000000@h
225 cmpwi %r5,0
226 bne 1b
227
228 lwz %r4,8(%r1) /* restore msr */
229 mtmsr %r4
230 isync
231 4:
232 addi %r1,%r1,48 /* pop stack frame and save area */
233 lwz %r0,4(%r1) /* return address */
234 mtlr %r0
235 blr
236
237 /*
238 * Call into OpenFirmware.
239 */
240 ENTRY_NOPROFILE(openfirmware)
241 mflr %r0
242 stw %r0,4(%r1) /* save return address */
243
244 /* Switch to OpenFirmware stack. */
245 lis %r7,firmstk+NBPG-16@ha
246 addi %r7,%r7,firmstk+NBPG-16@l
247 stw %r1,0(%r7) /* stash away prev stack pointer */
248 mr %r1,%r7
249
250 lis %r4,oftramp@ha
251 lwz %r4,oftramp@l(%r4)
252
253 mtctr %r4
254 bctrl
255
256 lwz %r1,0(%r1) /* restore previous stack pointer */
257 lwz %r0,4(%r1) /* return address */
258 mtlr %r0
259 blr
260