xref: /openbsd/sys/arch/riscv64/dev/riscv_cpu_intc.c (revision 03bd8600)
1 /*	$OpenBSD: riscv_cpu_intc.c,v 1.11 2024/04/19 14:39:34 jca 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 const 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
riscv_intc_match(struct device * parent,void * match,void * aux)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
riscv_intc_attach(struct device * parent,struct device * self,void * aux)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 #ifdef MULTIPROCESSOR
92 	extern int ipi_intr(void *);
93 	riscv_intc_intr_establish(IRQ_SOFTWARE_SUPERVISOR, 0,
94 	    ipi_intr, NULL, NULL);
95 #endif
96 
97 	/*
98 	 * XXX right time to enable interrupts ??
99 	 * might need to postpone until autoconf is finished
100 	 */
101 	intr_enable();
102 }
103 
104 
105 /* global interrupt handler */
106 void
riscv_intc_irq_handler(void * frame)107 riscv_intc_irq_handler(void *frame)
108 {
109 	int irq;
110 	struct intrhand *ih;
111 	struct trapframe *_frame = (struct trapframe*) frame;
112 
113 	KASSERTMSG(_frame->tf_scause & EXCP_INTR,
114 		"riscv_cpu_intr: wrong frame passed");
115 
116 	irq = (_frame->tf_scause & EXCP_MASK);
117 #ifdef DEBUG_INTC
118 	printf("irq %d fired\n", irq);
119 #endif
120 
121 	ih = intc_handler[irq];
122 	if (ih->ih_func(frame) == 0)
123 #ifdef DEBUG_INTC
124 		printf("fail in handling irq %d %s\n", irq, ih->ih_name);
125 #else
126 		;
127 #endif /* DEBUG_INTC */
128 }
129 
130 void *
riscv_intc_intr_establish(int irqno,int dummy_level,int (* func)(void *),void * arg,char * name)131 riscv_intc_intr_establish(int irqno, int dummy_level, int (*func)(void *),
132     void *arg, char *name)
133 {
134 	struct intrhand *ih;
135 	u_long sie;
136 
137 	if (irqno < 0 || irqno >= INTC_NIRQS)
138 		panic("intc_intr_establish: bogus irqnumber %d: %s",
139 		    irqno, name);
140 
141 	ih = malloc(sizeof(*ih), M_DEVBUF, M_WAITOK);
142 	ih->ih_func = func;
143 	ih->ih_arg = arg;
144 	ih->ih_irq = irqno;
145 	ih->ih_name = name;
146 
147 	sie = intr_disable();
148 	intc_handler[irqno] = ih;
149 	intr_restore(sie);
150 
151 	return (ih);
152 }
153 
154 void
riscv_intc_intr_disestablish(void * cookie)155 riscv_intc_intr_disestablish(void *cookie)
156 {
157 	struct intrhand *ih = cookie;
158 	int irqno = ih->ih_irq;
159 	u_long sie;
160 
161 	sie = intr_disable();
162 	intc_handler[irqno] = NULL;
163 	intr_restore(sie);
164 
165 	free(ih, M_DEVBUF, 0);
166 }
167