xref: /openbsd-src/sys/arch/riscv64/dev/riscv_cpu_intc.c (revision f1dd7b858388b4a23f4f67a4957ec5ff656ebbe8)
1 /*	$OpenBSD: riscv_cpu_intc.c,v 1.8 2021/05/14 06:48:52 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 2020, Mars Li <mengshi.li.mars@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/malloc.h>
22 #include <sys/device.h>
23 
24 #include <machine/fdt.h>
25 #include <machine/riscvreg.h>
26 
27 #include <dev/ofw/openfirm.h>
28 
29 #include "riscv_cpu_intc.h"
30 
31 struct intrhand {
32 	int (*ih_func)(void *);		/* handler */
33 	void *ih_arg;			/* arg for handler */
34 	int ih_irq;			/* IRQ number */
35 	char *ih_name;
36 };
37 
38 struct intrhand* intc_handler[INTC_NIRQS] = {NULL};
39 struct interrupt_controller intc_ic;
40 
41 int	riscv_intc_match(struct device *, void *, void *);
42 void	riscv_intc_attach(struct device *, struct device *, void *);
43 
44 void	riscv_intc_irq_handler(void *);
45 void	*riscv_intc_intr_establish(int, int, int (*)(void *),
46 		void *, char *);
47 void	riscv_intc_intr_disestablish(void *);
48 
49 
50 struct cfattach intc_ca = {
51 	sizeof (struct device), riscv_intc_match, riscv_intc_attach
52 };
53 
54 struct cfdriver intc_cd = {
55 	NULL, "intc", DV_DULL
56 };
57 
58 int
59 riscv_intc_match(struct device *parent, void *match, void *aux)
60 {
61 	struct fdt_attach_args *faa = aux;
62 	int node = faa->fa_node;
63 	return (OF_getproplen(node, "interrupt-controller") >= 0 &&
64 	    OF_is_compatible(node, "riscv,cpu-intc"));
65 }
66 
67 void
68 riscv_intc_attach(struct device *parent, struct device *self, void *aux)
69 {
70 	struct fdt_attach_args *faa = aux;/* should only use fa_node field */
71 
72 	riscv_init_smask();
73 
74 	/* hook the intr_handler */
75 	riscv_set_intr_handler(riscv_intc_irq_handler);
76 
77 	printf("\n");
78 
79 	intc_ic.ic_node = faa->fa_node;
80 	intc_ic.ic_cookie = &intc_ic;
81 
82 	/*
83 	 * only allow install/uninstall handler to/from global vector
84 	 * by calling riscv_intc_intr_establish/disestablish
85 	 */
86 	intc_ic.ic_establish = NULL;
87 	intc_ic.ic_disestablish = NULL;
88 
89 	riscv_intr_register_fdt(&intc_ic);
90 
91 	/*
92 	 * XXX right time to enable interrupts ??
93 	 * might need to postpone untile autoconf is finished
94 	 */
95 	intr_enable();
96 }
97 
98 
99 /* global interrupt handler */
100 void
101 riscv_intc_irq_handler(void *frame)
102 {
103 	int irq;
104 	struct intrhand *ih;
105 	struct trapframe *_frame = (struct trapframe*) frame;
106 
107 	KASSERTMSG(_frame->tf_scause & EXCP_INTR,
108 		"riscv_cpu_intr: wrong frame passed");
109 
110 	irq = (_frame->tf_scause & EXCP_MASK);
111 #ifdef DEBUG_INTC
112 	printf("irq %d fired\n", irq);
113 #endif
114 
115 	ih = intc_handler[irq];
116 	if (ih->ih_func(frame) == 0)
117 #ifdef DEBUG_INTC
118 		printf("fail in handling irq %d %s\n", irq, ih->ih_name);
119 #else
120 		;
121 #endif /* DEBUG_INTC */
122 }
123 
124 void *
125 riscv_intc_intr_establish(int irqno, int dummy_level, int (*func)(void *),
126     void *arg, char *name)
127 {
128 	struct intrhand *ih;
129 	u_long sie;
130 
131 	if (irqno < 0 || irqno >= INTC_NIRQS)
132 		panic("intc_intr_establish: bogus irqnumber %d: %s",
133 		    irqno, name);
134 
135 	ih = malloc(sizeof(*ih), M_DEVBUF, M_WAITOK);
136 	ih->ih_func = func;
137 	ih->ih_arg = arg;
138 	ih->ih_irq = irqno;
139 	ih->ih_name = name;
140 
141 	sie = intr_disable();
142 	intc_handler[irqno] = ih;
143 	intr_restore(sie);
144 
145 	return (ih);
146 }
147 
148 void
149 riscv_intc_intr_disestablish(void *cookie)
150 {
151 	struct intrhand *ih = cookie;
152 	int irqno = ih->ih_irq;
153 	u_long sie;
154 
155 	sie = intr_disable();
156 	intc_handler[irqno] = NULL;
157 	intr_restore(sie);
158 
159 	free(ih, M_DEVBUF, 0);
160 }
161