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