xref: /freebsd/sys/powerpc/powerpc/interrupt.c (revision c697fb7f)
1 /*-
2  * Copyright 2002 by Peter Grehan. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 /*
31  * Interrupts are dispatched to here from locore asm
32  */
33 
34 #include "opt_hwpmc_hooks.h"
35 #include "opt_platform.h"
36 
37 #include <sys/cdefs.h>                  /* RCS ID & Copyright macro defns */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/interrupt.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
45 #include <sys/ktr.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #ifdef HWPMC_HOOKS
50 #include <sys/pmckern.h>
51 #endif
52 #include <sys/proc.h>
53 #include <sys/smp.h>
54 #include <sys/unistd.h>
55 #include <sys/vmmeter.h>
56 
57 #include <machine/cpu.h>
58 #include <machine/clock.h>
59 #include <machine/db_machdep.h>
60 #include <machine/fpu.h>
61 #include <machine/frame.h>
62 #include <machine/intr_machdep.h>
63 #include <machine/md_var.h>
64 #include <machine/pcb.h>
65 #include <machine/psl.h>
66 #include <machine/trap.h>
67 #include <machine/spr.h>
68 #include <machine/sr.h>
69 
70 #include "pic_if.h"
71 
72 #ifdef POWERNV
73 int (*hmi_handler)(struct trapframe *);
74 #endif
75 
76 /*
77  * A very short dispatch, to try and maximise assembler code use
78  * between all exception types. Maybe 'true' interrupts should go
79  * here, and the trap code can come in separately
80  */
81 void
82 powerpc_interrupt(struct trapframe *framep)
83 {
84 	struct thread *td;
85 	struct trapframe *oldframe;
86 	register_t ee;
87 
88 	td = curthread;
89 
90 	CTR2(KTR_INTR, "%s: EXC=%x", __func__, framep->exc);
91 
92 	switch (framep->exc) {
93 	case EXC_EXI:
94 	case EXC_HVI:
95 		critical_enter();
96 		PIC_DISPATCH(root_pic, framep);
97 		critical_exit();
98 #ifdef BOOKE
99 		framep->srr1 &= ~PSL_WE;
100 #endif
101 		break;
102 
103 	case EXC_DECR:
104 		critical_enter();
105 		atomic_add_int(&td->td_intr_nesting_level, 1);
106 		oldframe = td->td_intr_frame;
107 		td->td_intr_frame = framep;
108 		decr_intr(framep);
109 		td->td_intr_frame = oldframe;
110 		atomic_subtract_int(&td->td_intr_nesting_level, 1);
111 		critical_exit();
112 #ifdef BOOKE
113 		framep->srr1 &= ~PSL_WE;
114 #endif
115 		break;
116 #ifdef HWPMC_HOOKS
117 	case EXC_PERF:
118 		critical_enter();
119 		KASSERT(pmc_intr != NULL, ("Performance exception, but no handler!"));
120 		(*pmc_intr)(framep);
121 		if (pmc_hook && (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
122 			pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, framep);
123 		critical_exit();
124 		break;
125 #endif
126 
127 #ifdef POWERNV
128 	case EXC_HMI:
129 		if (hmi_handler != 0 && hmi_handler(framep) == 0)
130 			break;
131 		/* If no handler, or failure to handle, just drop to trap. */
132 #endif
133 
134 	default:
135 		/* Re-enable interrupts if applicable. */
136 		ee = framep->srr1 & PSL_EE;
137 		if (ee != 0)
138 			mtmsr(mfmsr() | ee);
139 		trap(framep);
140 	}
141 }
142