ofw_subr.S revision 1.11 1 1.11 macallan /* $NetBSD: ofw_subr.S,v 1.11 2019/11/15 23:43:12 macallan Exp $ */
2 1.1 matt
3 1.1 matt /*
4 1.1 matt * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.1 matt * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.1 matt * All rights reserved.
7 1.1 matt *
8 1.1 matt * Redistribution and use in source and binary forms, with or without
9 1.1 matt * modification, are permitted provided that the following conditions
10 1.1 matt * are met:
11 1.1 matt * 1. Redistributions of source code must retain the above copyright
12 1.1 matt * notice, this list of conditions and the following disclaimer.
13 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer in the
15 1.1 matt * documentation and/or other materials provided with the distribution.
16 1.1 matt * 3. All advertising materials mentioning features or use of this software
17 1.1 matt * must display the following acknowledgement:
18 1.1 matt * This product includes software developed by TooLs GmbH.
19 1.1 matt * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 matt * derived from this software without specific prior written permission.
21 1.1 matt *
22 1.1 matt * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 matt * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 matt * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 matt * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 matt * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 matt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 matt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 matt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 matt */
33 1.1 matt
34 1.1 matt
35 1.1 matt .local firmstk
36 1.3 sanjayl .globl openfirmware_entry
37 1.10 macallan .globl ofwmsr
38 1.1 matt .local ofwsrsave
39 1.1 matt .local OF_buffer
40 1.1 matt
41 1.9 matt .lcomm firmstk,NBPG,16
42 1.11 macallan .lcomm OF_buffer,NBPG + 36,4
43 1.1 matt .comm openfirmware_entry,4,4 /* openfirmware entry point */
44 1.9 matt .lcomm ofwsrsave,64,4 /* openfirmware SR savearea */
45 1.10 macallan .comm ofwmsr,20,4 /* msr & sprg[0-3] used in OF */
46 1.9 matt .comm ofwreal_incharge,4,4
47 1.1 matt
48 1.1 matt /*
49 1.1 matt * Called by start to save the initial OFW state so we can restore it
50 1.1 matt * when call back to OFW.
51 1.1 matt */
52 1.1 matt ENTRY_NOPROFILE(ofwinit)
53 1.1 matt #ifdef FIRMWORKSBUGS
54 1.7 garbled mfmsr %r0
55 1.7 garbled andi. %r0,%r0,PSL_IR|PSL_DR
56 1.1 matt beq 1f
57 1.1 matt
58 1.7 garbled li %r8,1
59 1.7 garbled lis %r9,ofwreal_incharge@ha
60 1.9 matt stw %r8,ofwreal_incharge@l(%r9)
61 1.4 aymeric
62 1.7 garbled mflr %r30
63 1.1 matt bl _C_LABEL(ofwr_init)
64 1.7 garbled mtlr %r30
65 1.1 matt 1:
66 1.1 matt #endif
67 1.7 garbled lis %r8,openfirmware_entry@ha
68 1.7 garbled stw %r5,openfirmware_entry@l(%r8) /* save client interface handler*/
69 1.1 matt
70 1.7 garbled mfmsr %r0
71 1.10 macallan /*
72 1.10 macallan * XXX
73 1.10 macallan * doing this here instead of later on in ofwoea_initppc() after setting
74 1.10 macallan * up the console and such makes my PowerBook 3400c hang.
75 1.10 macallan * Probably just another OF 2.0 weirdness
76 1.10 macallan */
77 1.10 macallan /*li %r8,PSL_IP*/
78 1.10 macallan /*andc %r0,%r0,%r8*/ /* make sure PSL_IP is off */
79 1.9 matt lis %r9,ofwmsr@ha
80 1.9 matt stwu %r0,ofwmsr@l(%r9) /* save initial MSR value */
81 1.9 matt
82 1.9 matt mfsprg0 %r0 /* save SPRGs */
83 1.9 matt stw %r0,4(%r9)
84 1.9 matt mfsprg1 %r0
85 1.9 matt stw %r0,8(%r9)
86 1.9 matt mfsprg2 %r0
87 1.9 matt stw %r0,12(%r9)
88 1.9 matt mfsprg3 %r0
89 1.9 matt stw %r0,16(%r9)
90 1.7 garbled
91 1.7 garbled lis %r8,OF_buffer@ha
92 1.7 garbled addi %r8,%r8,OF_buffer@l
93 1.7 garbled lis %r9,_C_LABEL(OF_buf)@ha
94 1.7 garbled stw %r8,_C_LABEL(OF_buf)@l(%r9)
95 1.1 matt
96 1.1 matt blr
97 1.1 matt
98 1.1 matt /*
99 1.1 matt * OpenFirmware entry point
100 1.1 matt */
101 1.1 matt .text
102 1.1 matt ENTRY(openfirmware)
103 1.7 garbled mflr %r0 /* save return address */
104 1.7 garbled stw %r0,4(%r1)
105 1.9 matt stwu %r1,-32(%r1) /* setup stack frame */
106 1.7 garbled
107 1.7 garbled lis %r4,openfirmware_entry@ha /* get firmware entry point */
108 1.7 garbled lwz %r4,openfirmware_entry@l(%r4)
109 1.7 garbled mtlr %r4
110 1.7 garbled
111 1.9 matt mfsprg0 %r5 /* save current sprg0 (curcpu) */
112 1.9 matt stw %r5,16(%r1)
113 1.9 matt mfsprg1 %r5 /* save current sprg1 */
114 1.9 matt stw %r5,20(%r1)
115 1.9 matt mfsprg2 %r5 /* save current sprg1 */
116 1.9 matt stw %r5,24(%r1)
117 1.9 matt mfsprg3 %r5 /* save current sprg3 */
118 1.9 matt stw %r5,28(%r1)
119 1.4 aymeric
120 1.4 aymeric #ifdef FIRMWORKSBUGS
121 1.7 garbled lis %r4,ofwreal_incharge@ha
122 1.7 garbled lwz %r4,ofwreal_incharge@l(%r4)
123 1.7 garbled cmpwi %r4,1
124 1.4 aymeric bne 1f
125 1.4 aymeric blrl
126 1.6 garbled b 4f
127 1.4 aymeric 1:
128 1.6 garbled #endif
129 1.7 garbled mfmsr %r4 /* save msr */
130 1.7 garbled stw %r4,8(%r1)
131 1.4 aymeric
132 1.7 garbled li %r0,0 /* clear battable translations */
133 1.8 garbled mtmsr %r0
134 1.6 garbled #if defined (PPC_OEA) || defined (PPC_OEA64_BRIDGE)
135 1.7 garbled mtdbatu 2,%r0
136 1.7 garbled mtdbatu 3,%r0
137 1.7 garbled mtibatu 2,%r0
138 1.7 garbled mtibatu 3,%r0
139 1.3 sanjayl #endif /* PPC_OEA */
140 1.1 matt
141 1.7 garbled lis %r4,ofwsrsave@ha /* save current SRs */
142 1.7 garbled addi %r4,%r4,ofwsrsave@l
143 1.7 garbled li %r5,0
144 1.7 garbled 1: mfsrin %r0,%r5
145 1.7 garbled stw %r0,0(%r4)
146 1.7 garbled addi %r4,%r4,4
147 1.7 garbled addis %r5,%r5,0x10000000@h
148 1.7 garbled cmpwi %r5,0
149 1.1 matt bne 1b
150 1.1 matt
151 1.7 garbled lis %r4,_C_LABEL(ofw_pmap)@ha /* load OFW SR */
152 1.7 garbled addi %r4,%r4,_C_LABEL(ofw_pmap)@l
153 1.7 garbled lwz %r0,PM_KERNELSR(%r4)
154 1.7 garbled cmpwi %r0,0 /* pm_sr[KERNEL_SR] == 0? */
155 1.1 matt beq 2f /* then skip (not initialized yet) */
156 1.7 garbled li %r5,0
157 1.7 garbled 1: lwz %r0,0(%r4)
158 1.7 garbled mtsrin %r0,%r5
159 1.7 garbled addi %r4,%r4,4
160 1.7 garbled addis %r5,%r5,0x10000000@h
161 1.7 garbled cmpwi %r5,0
162 1.1 matt bne 1b
163 1.1 matt 2:
164 1.9 matt lis %r4,ofwmsr+16@ha /* Open Firmware msr + sprg[0-3] */
165 1.9 matt lwzu %r5,ofwmsr+16@l(%r4)
166 1.9 matt mtsprg3 %r5
167 1.7 garbled lwz %r5,-4(%r4)
168 1.9 matt mtsprg2 %r5
169 1.9 matt lwz %r5,-8(%r4)
170 1.9 matt mtsprg1 %r5
171 1.9 matt lwz %r5,-12(%r4)
172 1.9 matt mtsprg0 %r5
173 1.9 matt lwz %r5,-16(%r4)
174 1.7 garbled mtmsr %r5
175 1.1 matt isync
176 1.1 matt
177 1.1 matt blrl /* call Open Firmware */
178 1.1 matt
179 1.7 garbled lis %r4,ofwsrsave@ha /* restore saved SRs */
180 1.7 garbled addi %r4,%r4,ofwsrsave@l
181 1.7 garbled li %r5,0
182 1.7 garbled 1: lwz %r0,0(%r4)
183 1.7 garbled mtsrin %r0,%r5
184 1.7 garbled addi %r4,%r4,4
185 1.7 garbled addis %r5,%r5,0x10000000@h
186 1.7 garbled cmpwi %r5,0
187 1.1 matt bne 1b
188 1.1 matt
189 1.7 garbled lwz %r4,8(%r1) /* restore msr */
190 1.7 garbled mtmsr %r4
191 1.1 matt isync
192 1.6 garbled 4:
193 1.9 matt lwz %r5,16(%r1) /* restore saved sprgs (curcpu) */
194 1.9 matt mtsprg0 %r5
195 1.9 matt lwz %r5,20(%r1)
196 1.9 matt mtsprg1 %r5
197 1.9 matt lwz %r5,24(%r1)
198 1.9 matt mtsprg2 %r5
199 1.9 matt lwz %r5,28(%r1)
200 1.9 matt mtsprg3 %r5
201 1.7 garbled
202 1.7 garbled lwz %r1,0(%r1) /* and return */
203 1.7 garbled lwz %r0,4(%r1)
204 1.7 garbled mtlr %r0
205 1.1 matt blr
206 1.1 matt
207 1.1 matt /*
208 1.1 matt * Switch to/from OpenFirmware real mode stack
209 1.1 matt *
210 1.1 matt * Note: has to be called as the very first thing in OpenFirmware interface
211 1.1 matt * routines.
212 1.1 matt * E.g.:
213 1.1 matt * int
214 1.1 matt * OF_xxx(arg1, arg2)
215 1.1 matt * type arg1, arg2;
216 1.1 matt * {
217 1.1 matt * static struct {
218 1.1 matt * char *name;
219 1.1 matt * int nargs;
220 1.1 matt * int nreturns;
221 1.1 matt * char *method;
222 1.1 matt * int arg1;
223 1.1 matt * int arg2;
224 1.1 matt * int ret;
225 1.1 matt * } args = {
226 1.1 matt * "xxx",
227 1.1 matt * 2,
228 1.1 matt * 1,
229 1.1 matt * };
230 1.1 matt *
231 1.1 matt * ofw_stack();
232 1.1 matt * args.arg1 = arg1;
233 1.1 matt * args.arg2 = arg2;
234 1.1 matt * if (openfirmware(&args) < 0)
235 1.1 matt * return -1;
236 1.1 matt * return args.ret;
237 1.1 matt * }
238 1.1 matt */
239 1.1 matt
240 1.1 matt ENTRY(ofw_stack)
241 1.7 garbled mfmsr %r8 /* turn off interrupts */
242 1.7 garbled andi. %r0,%r8,~(PSL_EE|PSL_RI)@l
243 1.7 garbled mtmsr %r0
244 1.7 garbled stw %r8,4(%r1) /* abuse return address slot */
245 1.7 garbled
246 1.7 garbled lwz %r5,0(%r1) /* get length of stack frame */
247 1.7 garbled subf %r5,%r1,%r5
248 1.7 garbled
249 1.7 garbled lis %r7,firmstk+NBPG-8@ha
250 1.7 garbled addi %r7,%r7,firmstk+NBPG-8@l
251 1.7 garbled lis %r6,ofw_back@ha
252 1.7 garbled addi %r6,%r6,ofw_back@l
253 1.7 garbled subf %r4,%r5,%r7 /* make room for stack frame on
254 1.1 matt new stack */
255 1.7 garbled stw %r6,-4(%r7) /* setup return pointer */
256 1.7 garbled stwu %r1,-8(%r7)
257 1.1 matt
258 1.7 garbled stw %r7,-8(%r4)
259 1.1 matt
260 1.7 garbled addi %r3,%r1,8
261 1.7 garbled addi %r1,%r4,-8
262 1.7 garbled subi %r5,%r5,8
263 1.1 matt
264 1.1 matt b _C_LABEL(ofbcopy) /* and copy it */
265 1.1 matt
266 1.1 matt ofw_back:
267 1.7 garbled lwz %r1,0(%r1) /* get callers original stack pointer */
268 1.1 matt
269 1.7 garbled lwz %r0,4(%r1) /* get saved msr from abused slot */
270 1.7 garbled mtmsr %r0
271 1.1 matt
272 1.7 garbled lwz %r1,0(%r1) /* return */
273 1.7 garbled lwz %r0,4(%r1)
274 1.7 garbled mtlr %r0
275 1.1 matt blr
276