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