xref: /netbsd-src/sys/arch/arm/ofw/ofw_irq.S (revision 5e6a531eb4f38811a4142ff3bba1f07c97aa9b9f)
1*5e6a531eSskrll/*	$NetBSD: ofw_irq.S,v 1.17 2020/11/21 19:57:35 skrll Exp $	*/
2d17cc4f0Sthorpej
3d17cc4f0Sthorpej/*
4d17cc4f0Sthorpej * Copyright (c) 1994-1998 Mark Brinicombe.
5d17cc4f0Sthorpej * Copyright (c) 1994 Brini.
6d17cc4f0Sthorpej * All rights reserved.
7d17cc4f0Sthorpej *
8d17cc4f0Sthorpej * This code is derived from software written for Brini by Mark Brinicombe
9d17cc4f0Sthorpej *
10d17cc4f0Sthorpej * Redistribution and use in source and binary forms, with or without
11d17cc4f0Sthorpej * modification, are permitted provided that the following conditions
12d17cc4f0Sthorpej * are met:
13d17cc4f0Sthorpej * 1. Redistributions of source code must retain the above copyright
14d17cc4f0Sthorpej *    notice, this list of conditions and the following disclaimer.
15d17cc4f0Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
16d17cc4f0Sthorpej *    notice, this list of conditions and the following disclaimer in the
17d17cc4f0Sthorpej *    documentation and/or other materials provided with the distribution.
18d17cc4f0Sthorpej * 3. All advertising materials mentioning features or use of this software
19d17cc4f0Sthorpej *    must display the following acknowledgement:
20d17cc4f0Sthorpej *	This product includes software developed by Mark Brinicombe
21d17cc4f0Sthorpej *	for the NetBSD Project.
22d17cc4f0Sthorpej * 4. The name of the company nor the name of the author may be used to
23d17cc4f0Sthorpej *    endorse or promote products derived from this software without specific
24d17cc4f0Sthorpej *    prior written permission.
25d17cc4f0Sthorpej *
26d17cc4f0Sthorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27d17cc4f0Sthorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28d17cc4f0Sthorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29d17cc4f0Sthorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30d17cc4f0Sthorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31d17cc4f0Sthorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32d17cc4f0Sthorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33d17cc4f0Sthorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34d17cc4f0Sthorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35d17cc4f0Sthorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36d17cc4f0Sthorpej *
37d17cc4f0Sthorpej * Low level irq and fiq handlers
38d17cc4f0Sthorpej *
39d17cc4f0Sthorpej * Created      : 27/09/94
40d17cc4f0Sthorpej */
41d17cc4f0Sthorpej
42d17cc4f0Sthorpej#include "assym.h"
43ab152917Smatt#include <arm/asm.h>
44ab152917Smatt#include <arm/locore.h>
45ab152917Smatt
46d17cc4f0Sthorpej#include <machine/irqhandler.h>
47d17cc4f0Sthorpej
48d17cc4f0Sthorpej	.text
49d17cc4f0Sthorpej	.align	0
50d17cc4f0Sthorpej
51d17cc4f0Sthorpej/*
52d17cc4f0Sthorpej *
53d17cc4f0Sthorpej * irq_entry
54d17cc4f0Sthorpej *
55d17cc4f0Sthorpej * Main entry point for the IRQ vector
56d17cc4f0Sthorpej *
57d17cc4f0Sthorpej * This function is called only on timer ticks, passed on to the
58d17cc4f0Sthorpej * kernel from the OFW tick handler.
59d17cc4f0Sthorpej *
60d17cc4f0Sthorpej * For now, I am trying to re-use as much of the code from the
61d17cc4f0Sthorpej * IOMD interrupt-handler as possible.  In time, I will strip this
62d17cc4f0Sthorpej * down to something OFW-specific.
63d17cc4f0Sthorpej *
64d17cc4f0Sthorpej * Here's the original, IOMD-specific description:
65d17cc4f0Sthorpej * This function reads the irq request bits in the IOMD registers
66d17cc4f0Sthorpej * IRQRQA, IRQRQB and DMARQ
67d17cc4f0Sthorpej * It then calls an installed handler for each bit that is set.
68d17cc4f0Sthorpej * The function stray_irqhandler is called if a handler is not defined
69d17cc4f0Sthorpej * for a particular interrupt.
70d17cc4f0Sthorpej * If a interrupt handler is found then it is called with r0 containing
71d17cc4f0Sthorpej * the argument defined in the handler structure. If the field ih_arg
72d17cc4f0Sthorpej * is zero then a pointer to the IRQ frame on the stack is passed instead.
73d17cc4f0Sthorpej */
74d17cc4f0Sthorpej
75d17cc4f0SthorpejLdisabled_mask:
76d17cc4f0Sthorpej	.word	_C_LABEL(disabled_mask)
77d17cc4f0Sthorpej
78d17cc4f0SthorpejLspl_masks:
79d17cc4f0Sthorpej	.word	_C_LABEL(spl_masks)
80d17cc4f0Sthorpej
81d17cc4f0SthorpejLofw_ticktmp:
82d17cc4f0Sthorpej	.word	_C_LABEL(ofw_ticktmp)
83d17cc4f0Sthorpej
84d17cc4f0SthorpejLirq_entry:
85d17cc4f0Sthorpej	.word	irq_entry
86d17cc4f0Sthorpej
87d17cc4f0SthorpejLofwirqstk:			/* hack */
88d17cc4f0Sthorpej	.word	ofwirqstk + 4096
89d17cc4f0Sthorpej
90165d4e6dSthorpejLOCK_CAS_CHECK_LOCALS
91165d4e6dSthorpej
92f77bf2bbSscwAST_ALIGNMENT_FAULT_LOCALS
93f77bf2bbSscw
94d17cc4f0Sthorpej/*
95d17cc4f0Sthorpej * Regsister usage
96d17cc4f0Sthorpej *
972650ef28Smatt *  r4	- Address of cpu_info (on entry)
98*5e6a531eSskrll *  r5  - Address of curlwp
99d17cc4f0Sthorpej *  r6  - Address of current handler
1002650ef28Smatt *  r7  - pspr mode (must be preserved)
101d17cc4f0Sthorpej *  r8  - Current IRQ requests.
102d17cc4f0Sthorpej *  r9  - Used to count through possible IRQ bits.
103d17cc4f0Sthorpej *  r10 - Base address of IOMD
104*5e6a531eSskrll *  r11  - Pointer to handler pointer list
105d17cc4f0Sthorpej */
106d17cc4f0Sthorpej
107d17cc4f0SthorpejASENTRY_NP(irq_entry)
108d17cc4f0Sthorpej	/*
109d17cc4f0Sthorpej	 *  We come here following an OFW-handled timer tick.
110d17cc4f0Sthorpej	 *
111d17cc4f0Sthorpej 	 *  We are in the SVC frame, and interrupts are disabled.
112d17cc4f0Sthorpej 	 *  The state of the interrupted context is partially in
113d17cc4f0Sthorpej 	 *  the registers and partially in the global storage area
114d17cc4f0Sthorpej 	 *  labeled ofw_ticktmp.  ofw_ticktmp is filled-in by the
115d17cc4f0Sthorpej 	 *  tick callback that is invoked by OFW on the way out of
116d17cc4f0Sthorpej 	 *  its interrupt handler.  ofw_ticktmp contains the following:
117d17cc4f0Sthorpej 	 *
118d17cc4f0Sthorpej 	 *      pc			// interrupted instruction
119d17cc4f0Sthorpej 	 *      lr_usr
120d17cc4f0Sthorpej 	 *      sp_usr
121d17cc4f0Sthorpej 	 *      r1			// makes r1 available for scratch
122d17cc4f0Sthorpej 	 *      r0			// makes r0 available for scratch
123d17cc4f0Sthorpej 	 *      spsr_svc		// cpsr of interrupted context
124d17cc4f0Sthorpej 	 *
125d17cc4f0Sthorpej 	 *  The prologue of this routine must re-construct the
126d17cc4f0Sthorpej 	 *  machine state that existed at the time OFW's interrupt-
127d17cc4f0Sthorpej 	 *  handler fielded the interrupt.  That allows us to use
128d17cc4f0Sthorpej 	 *  the rest of the code in this routine, and have it all
129d17cc4f0Sthorpej 	 *  "just work."
130d17cc4f0Sthorpej	 */
131d17cc4f0Sthorpej
132d17cc4f0Sthorpej	/*
133d17cc4f0Sthorpej	 * Switch to IRQ mode.
134d17cc4f0Sthorpej	 * First check the spsr in ofw_ticktmp to see what the FIQ bit should be.
135d17cc4f0Sthorpej	 *
136d17cc4f0Sthorpej	 * I need 2 scratch registers to do this.
137d17cc4f0Sthorpej	 * Fortunately, r0 and r1 are already saved in ofw_ticktmp.
138d17cc4f0Sthorpej	 * How convenient.
139d17cc4f0Sthorpej	 */
140d17cc4f0Sthorpej	ldr	r0, Lofw_ticktmp
141d17cc4f0Sthorpej	ldr	r0, [r0]
142d17cc4f0Sthorpej	and	r0, r0, #F32_bit
143d17cc4f0Sthorpej	mov	r1, #(I32_bit | PSR_IRQ32_MODE)
144d17cc4f0Sthorpej	orr	r1, r1, r0
145d17cc4f0Sthorpej	msr	cpsr_all, r1
146d17cc4f0Sthorpej
147d17cc4f0Sthorpej	/* Now we're in IRQ mode. */
148d17cc4f0Sthorpej	/* Restore contents of ofw_ticktmp. */
149d599df95Sbjh21	adr	r0, Lofwirqstk    /* Bummer!  Mitch hasn't left me a stack. */
150d17cc4f0Sthorpej	ldr	sp, [r0]		/* I'll use my own for now... */
151d17cc4f0Sthorpej	ldr	r0, Lofw_ticktmp	/* r0 now points to ofw_ticktmp[0] */
152d17cc4f0Sthorpej	ldr	r1, [r0], #(4*3)	/* skip over saved {r0, r1} */
153d17cc4f0Sthorpej	msr	spsr_all, r1		/* restore spsr */
154d17cc4f0Sthorpej	ldmia	r0, {sp, lr}^		/* restore user sp and lr */
155d17cc4f0Sthorpej	add	r0, r0, #(4*2)		/* previous instruction can't writeback */
156d17cc4f0Sthorpej					/* this one can't use banked registers */
157d17cc4f0Sthorpej	ldr	lr, [r0], #(-4*4)	/* restore pc; point r0 at ofw_ticktmp[1] */
158d17cc4f0Sthorpej	add	lr, lr, #4		/* pc += 4; will be decremented below */
159d17cc4f0Sthorpej	ldmia	r0, {r0, r1}		/* restore r0 and r1 */
160d17cc4f0Sthorpej
161d17cc4f0Sthorpej	/* OK, the machine state should be identical now to that when */
162d17cc4f0Sthorpej	/* OFW fielded the interrupt.  So just fall through... */
163d17cc4f0Sthorpej
164d17cc4f0Sthorpej	sub	lr, lr, #0x00000004	/* Adjust the lr */
165d17cc4f0Sthorpej
166d17cc4f0Sthorpej	PUSHFRAMEINSVC			/* Push an interrupt frame */
167d17cc4f0Sthorpej
168d17cc4f0Sthorpej	/*
169825088edSmatt	 *  Can't field this interrupt now if priority is IPL_CLOCK
170d17cc4f0Sthorpej	 *  or higher.  For now, we'll just ignore the interrupt.
171d17cc4f0Sthorpej	 *  Soon, we will have to schedule it for later action.
172d17cc4f0Sthorpej	 */
173d17cc4f0Sthorpej	ldr	r0, Lcurrent_spl_level
174825088edSmatt	ldr	r0, [r4, #CI_CPL]
175825088edSmatt	cmp	r0, #IPL_CLOCK
176d17cc4f0Sthorpej	blt	ofwtakeint
177d17cc4f0Sthorpej
178d17cc4f0Sthorpej	PULLFRAMEFROMSVCANDEXIT
179d17cc4f0Sthorpej	movs	pc, lr			/* Exit */
180d17cc4f0Sthorpej
181d17cc4f0Sthorpej 	/*
182d17cc4f0Sthorpej 	 *  Stuff a bit-mask into r8 indicating which interrupts
183d17cc4f0Sthorpej 	 *  are pending.  In our case, that is just the timer0
184d17cc4f0Sthorpej 	 *  interrupt:  (1 << TIMER0).  The existing code will take
185d17cc4f0Sthorpej 	 *  care of invoking that handler and the softint/ast stuff
186d17cc4f0Sthorpej 	 *  which follows it.
187d17cc4f0Sthorpej	 */
188d17cc4f0Sthorpejofwtakeint:
189f309b668Smanu#ifdef EXEC_AOUT
190f77bf2bbSscw	ldr	r0, [sp]		/* Fetch SPSR */
191f77bf2bbSscw#endif
192*5e6a531eSskrll	ENABLE_ALIGNMENT_FAULTS		/* puts cur{cpu,lwp} in r4/r5 */
193f77bf2bbSscw
194d17cc4f0Sthorpej	mov	r8, #0x00000001		/* timer interrupt pending! */
195d17cc4f0Sthorpej	mov	r8, r8, lsl #IRQ_TIMER0
196d17cc4f0Sthorpej
197d17cc4f0Sthorpej	/*
198d17cc4f0Sthorpej	 * Note that we have entered the IRQ handler.
199d17cc4f0Sthorpej	 * We are in SVC mode so we cannot use the processor mode
200d17cc4f0Sthorpej	 * to determine if we are in an IRQ. Instead we will count the
201d17cc4f0Sthorpej	 * each time the interrupt handler is nested.
202d17cc4f0Sthorpej	 */
203d17cc4f0Sthorpej
204825088edSmatt	ldr	r1, [r4, #CI_INTR_DEPTH]
205d17cc4f0Sthorpej	add	r1, r1, #1
206825088edSmatt	str	r1, [r4, #CI_INTR_DEPTH]
207d17cc4f0Sthorpej
208d17cc4f0Sthorpej	/* Block the current requested interrupts */
209d17cc4f0Sthorpej	ldr	r1, Ldisabled_mask
210d17cc4f0Sthorpej	ldr	r0, [r1]
211d17cc4f0Sthorpej	stmfd	sp!, {r0}
212d17cc4f0Sthorpej	orr	r0, r0, r8
213d17cc4f0Sthorpej
214d17cc4f0Sthorpej	/*
215d17cc4f0Sthorpej 	 * Need to block all interrupts at the IPL or lower for
216d17cc4f0Sthorpej	 * all asserted interrupts.
217d17cc4f0Sthorpej	 * This basically emulates hardware interrupt priority levels.
218d17cc4f0Sthorpej	 * Means we need to go through the interrupt mask and for
219d17cc4f0Sthorpej	 * every asserted interrupt we need to mask out all other
220d17cc4f0Sthorpej	 * interrupts at the same or lower IPL.
221d17cc4f0Sthorpej	 * If only we could wait until the main loop but we need to sort
222d17cc4f0Sthorpej	 * this out first so interrupts can be re-enabled.
223d17cc4f0Sthorpej	 *
224d17cc4f0Sthorpej	 * This would benefit from a special ffs type routine
225d17cc4f0Sthorpej	 */
226d17cc4f0Sthorpej
227825088edSmatt	mov	r9, #(NIPL - 1)
228*5e6a531eSskrll	ldr	r11, Lspl_masks
229d17cc4f0Sthorpej
230d17cc4f0SthorpejLfind_highest_ipl:
231*5e6a531eSskrll	ldr	r2, [r11, r9, lsl #2]
232d17cc4f0Sthorpej	tst	r8, r2
233d17cc4f0Sthorpej	subeq	r9, r9, #1
234d17cc4f0Sthorpej	beq	Lfind_highest_ipl
235d17cc4f0Sthorpej
236d17cc4f0Sthorpej	/* r9 = SPL level of highest priority interrupt */
237d17cc4f0Sthorpej	add	r9, r9, #1
238*5e6a531eSskrll	ldr	r2, [r11, r9, lsl #2]
239d17cc4f0Sthorpej	mvn	r2, r2
240d17cc4f0Sthorpej	orr	r0, r0, r2
241d17cc4f0Sthorpej
242d17cc4f0Sthorpej	str	r0, [r1]
243d17cc4f0Sthorpej
244d17cc4f0Sthorpej	ldr	r0, Lcurrent_spl_level
245825088edSmatt	ldr	r1, [r4, #CI_CPL]
246825088edSmatt	str	r9, [r4, #CI_CPL]
247d17cc4f0Sthorpej	stmfd	sp!, {r1}
248d17cc4f0Sthorpej
249d17cc4f0Sthorpej	/* Update the irq masks */
250d17cc4f0Sthorpej	bl	_C_LABEL(irq_setmasks)
251d17cc4f0Sthorpej
2528b058f85Sjoerg	mrs     r0, cpsr		/* Enable IRQ's */
253d17cc4f0Sthorpej	bic	r0, r0, #I32_bit
254d17cc4f0Sthorpej	msr	cpsr_all, r0
255d17cc4f0Sthorpej
256*5e6a531eSskrll	ldr	r11, Lirqhandlers
257d17cc4f0Sthorpej	mov	r9, #0x00000001
258d17cc4f0Sthorpej
259d17cc4f0Sthorpejirqloop:
260d17cc4f0Sthorpej	/* This would benefit from a special ffs type routine */
261d17cc4f0Sthorpej	tst	r8, r9			/* Is a bit set ? */
262d17cc4f0Sthorpej	beq	nextirq			/* No ? try next bit */
263d17cc4f0Sthorpej
264*5e6a531eSskrll	ldr	r6, [r11]		/* Get address of first handler structure */
265d17cc4f0Sthorpej
266d17cc4f0Sthorpej	teq	r6, #0x00000000		/* Do we have a handler */
267d17cc4f0Sthorpej	moveq	r0, r8			/* IRQ requests as arg 0 */
268d17cc4f0Sthorpej	beq	_C_LABEL(stray_irqhandler) /* call special handler */
269d17cc4f0Sthorpej
2706a66466fSmatt	ldr	r1, [r4, #(CI_CC_NINTR)]
2716a66466fSmatt	ldr	r2, [r4, #(CI_CC_NINTR+4)]
2726a66466fSmatt#ifdef _ARMEL
2736a66466fSmatt	adds	r1, r1, #0x00000001
2746a66466fSmatt	adc	r2, r2, #0x00000000
2756a66466fSmatt#else
2766a66466fSmatt	adds	r2, r2, #0x00000001
2776a66466fSmatt	adc	r1, r1, #0x00000000
2786a66466fSmatt#endif
2796a66466fSmatt	str	r1, [r4, #(CI_CC_NINTR)]
2806a66466fSmatt	str	r2, [r4, #(CI_CC_NINTR+4)]
281d17cc4f0Sthorpej
282d17cc4f0Sthorpejirqchainloop:
283d17cc4f0Sthorpej	ldr	r0, [r6, #(IH_ARG)]	/* Get argument pointer */
284d17cc4f0Sthorpej	teq	r0, #0x00000000		/* If arg is zero pass stack frame */
285d17cc4f0Sthorpej	addeq	r0, sp, #8		/* ... stack frame */
286d599df95Sbjh21	mov	lr, pc			/* return address */
287d17cc4f0Sthorpej	ldr	pc, [r6, #(IH_FUNC)]	/* Call handler */
288d17cc4f0Sthorpej
289d17cc4f0Sthorpej	teq	r0, #0x00000001		/* Was the irq serviced ? */
290d17cc4f0Sthorpej	beq	irqdone
291d17cc4f0Sthorpej
292d17cc4f0Sthorpej	ldr	r6, [r6, #(IH_NEXT)]
293d17cc4f0Sthorpej	teq	r6, #0x00000000
294d17cc4f0Sthorpej	bne	irqchainloop
295d471ccf3Smatt	b	nextirq
296d17cc4f0Sthorpej
297d17cc4f0Sthorpejirqdone:
298d471ccf3Smatt	add	r3, r6, #IH_EV_COUNT	/* get address of ih's ev_count */
299d471ccf3Smatt	ldmia   r3, {r1-r2}		/* load ev_count */
300d471ccf3Smatt	adds	r1, r1, #0x00000001	/* 64bit incr (lo) */
301d471ccf3Smatt	adc     r2, r2, #0x00000000	/* 64bit incr (hi) */
302d471ccf3Smatt	stmia   r3, {r1-r2}		/* store ev_count */
303d471ccf3Smatt
304d17cc4f0Sthorpejnextirq:
305*5e6a531eSskrll	add	r11, r11, #0x00000004	/* update pointer to handlers */
306d17cc4f0Sthorpej	mov	r9, r9, lsl #1		/* move on to next bit */
307d17cc4f0Sthorpej	teq	r9, #(1 << 24)		/* done the last bit ? */
308d17cc4f0Sthorpej	bne	irqloop			/* no - loop back. */
309d17cc4f0Sthorpej
310d17cc4f0Sthorpej	ldmfd	sp!, {r2}
311825088edSmatt	str	r2, [r4, #CI_CPL]
312d17cc4f0Sthorpej
313d17cc4f0Sthorpej	/* Restore previous disabled mask */
314d17cc4f0Sthorpej	ldmfd	sp!, {r2}
315d17cc4f0Sthorpej	ldr	r1, Ldisabled_mask
316d17cc4f0Sthorpej	str	r2, [r1]
317d17cc4f0Sthorpej	bl	_C_LABEL(irq_setmasks)
318d17cc4f0Sthorpej
319d17cc4f0Sthorpej	/* Kill IRQ's in preparation for exit */
3208b058f85Sjoerg	mrs     r0, cpsr
321d17cc4f0Sthorpej	orr     r0, r0, #(I32_bit)
322d17cc4f0Sthorpej	msr     cpsr_all, r0
323d17cc4f0Sthorpej
324d17cc4f0Sthorpej	/* Decrement the nest count */
325825088edSmatt	ldr	r1, [r4, #CI_INTR_DEPTH]
326d17cc4f0Sthorpej	sub	r1, r1, #1
327825088edSmatt	str	r1, [r4, #CI_INTR_DEPTH]
328d17cc4f0Sthorpej
329165d4e6dSthorpej	LOCK_CAS_CHECK
330165d4e6dSthorpej
331f77bf2bbSscw	DO_AST_AND_RESTORE_ALIGNMENT_FAULTS
332d17cc4f0Sthorpej	PULLFRAMEFROMSVCANDEXIT
333d17cc4f0Sthorpej	movs	pc, lr			/* Exit */
334d17cc4f0Sthorpej
335d17cc4f0SthorpejLcurrent_mask:
336d17cc4f0Sthorpej	.word	_C_LABEL(current_mask)	/* irq's that are usable */
337d17cc4f0Sthorpej
338d17cc4f0Sthorpej
339d17cc4f0SthorpejENTRY(irq_setmasks)
340d17cc4f0Sthorpej	/* Do nothing */
341d17cc4f0Sthorpej	mov	pc, lr
342d17cc4f0Sthorpej
343d17cc4f0Sthorpej
344d17cc4f0SthorpejLirqhandlers:
345d17cc4f0Sthorpej	.word	_C_LABEL(irqhandlers)	/* Pointer to array of irqhandlers */
346d17cc4f0Sthorpej
347d17cc4f0Sthorpej	.text
348d17cc4f0Sthorpej	.global	_C_LABEL(dotickgrovelling)
349d17cc4f0Sthorpej
350d17cc4f0Sthorpej/*
351d17cc4f0Sthorpej *  Do magic to cause OFW to call our irq_entry
352d17cc4f0Sthorpej *  routine when it returns from its tick-handling.
353d17cc4f0Sthorpej *
354d17cc4f0Sthorpej *  This consists of two sub-tasks:
355d17cc4f0Sthorpej *    - save some machine state in ofw_ticktmp
356d17cc4f0Sthorpej *    - punch some new machine state into the
357d17cc4f0Sthorpej *      OFW-supplied frame
358d17cc4f0Sthorpej *
359d17cc4f0Sthorpej *  We are running in the IRQ frame, with
360d17cc4f0Sthorpej *  interrupts disabled.
361d17cc4f0Sthorpej *
362d17cc4f0Sthorpej *  r0 - base of saved OFW interrupt frame, which
363d17cc4f0Sthorpej *       has the following format:
364d17cc4f0Sthorpej *
365d17cc4f0Sthorpej *         pc			// interrupted instruction
366d17cc4f0Sthorpej *         lr			// lr of interrupted context
367d17cc4f0Sthorpej *         sp			// sp of interrupted context
368d17cc4f0Sthorpej *         r12
369d17cc4f0Sthorpej *         ...		// non-banked register values
370d17cc4f0Sthorpej *         ...		//   of interrupted context
371d17cc4f0Sthorpej *         r0
372d17cc4f0Sthorpej *         spsr		// psr of interrupted context
373d17cc4f0Sthorpej *
374d17cc4f0Sthorpej */
375d17cc4f0Sthorpej
376d17cc4f0Sthorpej_C_LABEL(dotickgrovelling):
377d17cc4f0Sthorpej	/*assert((cpsr & PSR_MODE) == PSR_IRQ32_MODE);*/
378d17cc4f0Sthorpej
379d17cc4f0Sthorpej	stmfd	sp!, {r1-r5}		/* scratch registers r1-r5 */
380d17cc4f0Sthorpej
381d17cc4f0Sthorpej	/*
382d17cc4f0Sthorpej	 *  Sub-task 1:
383d17cc4f0Sthorpej	 *
384d17cc4f0Sthorpej	 *    Our irq_entry routine needs to re-construct
385d17cc4f0Sthorpej	 *    the state of the machine at the time OFW
386d17cc4f0Sthorpej	 *    fielded the interrupt, so that we can use
387d17cc4f0Sthorpej	 *    the rest of the standard interrupt-handling
388d17cc4f0Sthorpej	 *    code.  Specifically, irq_entry needs to get
389d17cc4f0Sthorpej	 *    at the following machine state:
390d17cc4f0Sthorpej	 *
391d17cc4f0Sthorpej 	 *      pc              // interrupted instruction
392d17cc4f0Sthorpej 	 *      lr_usr
393d17cc4f0Sthorpej 	 *      sp_usr
394d17cc4f0Sthorpej	 *      r0-r12          // the non-banked registers
395d17cc4f0Sthorpej	 *                      //   at the time of interruption
396d17cc4f0Sthorpej 	 *      spsr            // cpsr of interrupted context
397d17cc4f0Sthorpej	 *
398d17cc4f0Sthorpej	 *    The non-banked registers will be valid at the
399d17cc4f0Sthorpej	 *    time irq_entry is called, but the other values
400d17cc4f0Sthorpej	 *    will not be.  We must save them here, in the
401d17cc4f0Sthorpej	 *    ofw_ticktmp storage block.  We also save r0
402d17cc4f0Sthorpej	 *    and r1 so that we have some free registers
403d17cc4f0Sthorpej	 *    when it's time to do the re-construction.
404d17cc4f0Sthorpej	 *
405d17cc4f0Sthorpej	 *    Note that interrupts are not enabled before
406d17cc4f0Sthorpej	 *    irq_entry is entered, so we don't have to
407d17cc4f0Sthorpej	 *    worry about ofw_ticktmp getting clobbered.
408d17cc4f0Sthorpej	 */
409d17cc4f0Sthorpej	ldr	r1, Lofw_ticktmp		/* r1 points to ofw_ticktmp[0] */
410d17cc4f0Sthorpej
411d17cc4f0Sthorpej	ldr	r2, [r0, #0]			/* ofwframe[0] is spsr */
412d17cc4f0Sthorpej	stmia	r1!, {r2}			/* put it in ofw_ticktmp[0] */
413d17cc4f0Sthorpej
414d17cc4f0Sthorpej	ldr	r2, [r0, #(4*1)]		/* ofwframe[1] is saved r0 */
415d17cc4f0Sthorpej	stmia	r1!, {r2}			/* put it in ofw_ticktmp[1] */
416d17cc4f0Sthorpej
417d17cc4f0Sthorpej	ldr	r2, [r0, #(4*2)]		/* ofwframe[2] is saved r1 */
418d17cc4f0Sthorpej	stmia	r1!, {r2}			/* put it in ofw_ticktmp[2] */
419d17cc4f0Sthorpej
420d17cc4f0Sthorpej	stmia	r1, {sp, lr}^		/* put {sp,lr}_usr in ofw_ticktmp[3,4]; */
421d17cc4f0Sthorpej							/* the user registers are still valid */
422d17cc4f0Sthorpej							/* because we haven't left IRQ mode */
423d17cc4f0Sthorpej	add	r1, r1, #(4*2)		/* previous instruction can't writeback */
424d17cc4f0Sthorpej							/* this one can't use banked registers */
425d17cc4f0Sthorpej
426d17cc4f0Sthorpej	ldr	r2, [r0, #(4*16)]		/* ofwframe[16] is pc */
427d17cc4f0Sthorpej	stmia	r1!, {r2}			/* put it in ofw_ticktmp[5] */
428d17cc4f0Sthorpej
429d17cc4f0Sthorpej
430d17cc4f0Sthorpej	/*
431d17cc4f0Sthorpej	 *  Sub-task 2:
432d17cc4f0Sthorpej	 *
433d17cc4f0Sthorpej	 *    Diddle the OFW-supplied frame such that
434d17cc4f0Sthorpej	 *    control passes to irq_entry when OFW does
435d17cc4f0Sthorpej	 *    its return from interrupt.  There are 4
436d17cc4f0Sthorpej	 *    fields in that frame that we need to plug:
437d17cc4f0Sthorpej	 *
438d17cc4f0Sthorpej	 *        pc			// gets irq_entry
439d17cc4f0Sthorpej	 *        lr			// gets lr_svc
440d17cc4f0Sthorpej	 *        sp			// gets sp_svc
441d17cc4f0Sthorpej	 *        spsr		// gets (I32_bit | PSR_SVC32_MODE)
442d17cc4f0Sthorpej	 *
443d17cc4f0Sthorpej	 */
444d17cc4f0Sthorpej	mov	r1, #(I32_bit | PSR_SVC32_MODE)
445d17cc4f0Sthorpej	str	r1, [r0, #0]			/* plug spsr */
446d17cc4f0Sthorpej
447d17cc4f0Sthorpej	/* Sneak into SVC mode to get sp and lr */
4488b058f85Sjoerg	mrs	r3, cpsr
449d17cc4f0Sthorpej	bic	r3, r3, #(PSR_MODE)
450d17cc4f0Sthorpej	orr	r3, r3, #(PSR_SVC32_MODE)
451d17cc4f0Sthorpej	msr	cpsr_all, r3
452d17cc4f0Sthorpej	mov	r4, lr				/* snarf lr_svc */
453d17cc4f0Sthorpej	mov	r5, sp				/* snarf sp_svc */
454d17cc4f0Sthorpej	bic	r3, r3, #(PSR_MODE)
455d17cc4f0Sthorpej	orr	r3, r3, #(PSR_IRQ32_MODE)
456d17cc4f0Sthorpej	msr	cpsr_all, r3
457d17cc4f0Sthorpej	str	r5, [r0, #(4*14)]		/* plug sp */
458d17cc4f0Sthorpej	str	r4, [r0, #(4*15)]		/* plug lr */
459d17cc4f0Sthorpej
460d599df95Sbjh21	ldr	r1, Lirq_entry
461d17cc4f0Sthorpej	str	r1, [r0, #(4*16)]		/* plug pc */
462d17cc4f0Sthorpej
463d17cc4f0Sthorpej	ldmfd	sp!, {r1-r5}
464d17cc4f0Sthorpej	mov	pc, lr
465d17cc4f0Sthorpej
466d17cc4f0Sthorpej
467d17cc4f0Sthorpej	.bss
468d17cc4f0Sthorpej	.align 0
469d17cc4f0Sthorpej
470d17cc4f0Sthorpej_C_LABEL(ofw_ticktmp):
471d17cc4f0Sthorpej	.space	4 * 6	/* temporary storage for 6 words of machine state */
472d17cc4f0Sthorpej
473d17cc4f0Sthorpejofwirqstk:			/* hack */
474d17cc4f0Sthorpej	.space	4096
475