plic.c revision 1.2 1 1.2 skrll /* $NetBSD: plic.c,v 1.2 2023/09/02 09:58:15 skrll Exp $ */
2 1.1 skrll
3 1.1 skrll /*-
4 1.1 skrll * Copyright (c) 2022 The NetBSD Foundation, Inc.
5 1.1 skrll * All rights reserved.
6 1.1 skrll *
7 1.1 skrll * Portions of this code is derived from software contributed to The NetBSD
8 1.1 skrll * Foundation by Simon Burge.
9 1.1 skrll *
10 1.1 skrll * Redistribution and use in source and binary forms, with or without
11 1.1 skrll * modification, are permitted provided that the following conditions
12 1.1 skrll * are met:
13 1.1 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1 skrll * notice, this list of conditions and the following disclaimer.
15 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1 skrll * documentation and/or other materials provided with the distribution.
18 1.1 skrll *
19 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.1 skrll */
31 1.1 skrll
32 1.1 skrll #include "opt_multiprocessor.h"
33 1.1 skrll
34 1.1 skrll #include <sys/cdefs.h>
35 1.2 skrll __KERNEL_RCSID(0, "$NetBSD: plic.c,v 1.2 2023/09/02 09:58:15 skrll Exp $");
36 1.1 skrll
37 1.1 skrll #include <sys/param.h>
38 1.1 skrll
39 1.1 skrll #include <sys/bus.h>
40 1.1 skrll #include <sys/cpu.h>
41 1.1 skrll #include <sys/kmem.h>
42 1.1 skrll
43 1.1 skrll #include <riscv/sysreg.h>
44 1.1 skrll #include <riscv/dev/plicreg.h>
45 1.1 skrll #include <riscv/dev/plicvar.h>
46 1.1 skrll
47 1.1 skrll #define PLIC_PRIORITY(irq) (PLIC_PRIORITY_BASE + (irq) * 4)
48 1.1 skrll
49 1.2 skrll #define PLIC_ENABLE(sc, h, irq) (PLIC_ENABLE_BASE + \
50 1.2 skrll sc->sc_context[(h)] * PLIC_ENABLE_SIZE + \
51 1.1 skrll ((irq / 32) * sizeof(uint32_t)))
52 1.1 skrll
53 1.2 skrll #define PLIC_CONTEXT(sc, h) (PLIC_CONTEXT_BASE + \
54 1.2 skrll sc->sc_context[(h)] * PLIC_CONTEXT_SIZE)
55 1.2 skrll #define PLIC_CLAIM(sc, h) (PLIC_CONTEXT(sc, h) + PLIC_CLAIM_COMPLETE_OFFS)
56 1.2 skrll #define PLIC_COMPLETE(sc, h) PLIC_CLAIM(sc, h) /* same address */
57 1.2 skrll #define PLIC_THRESHOLD(sc, h) (PLIC_CONTEXT(sc, h) + PLIC_THRESHOLD_OFFS)
58 1.1 skrll
59 1.1 skrll #define PLIC_READ(sc, reg) \
60 1.1 skrll bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
61 1.1 skrll #define PLIC_WRITE(sc, reg, val) \
62 1.1 skrll bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
63 1.1 skrll
64 1.1 skrll
65 1.1 skrll struct plic_softc *plic_sc;
66 1.1 skrll
67 1.1 skrll
68 1.1 skrll void *
69 1.1 skrll plic_intr_establish_xname(u_int irq, int ipl, int flags,
70 1.1 skrll int (*func)(void *), void *arg, const char *xname)
71 1.1 skrll {
72 1.1 skrll struct plic_softc * const sc = plic_sc;
73 1.1 skrll struct plic_intrhand *ih;
74 1.1 skrll
75 1.2 skrll /*
76 1.2 skrll * Choose hart 0.
77 1.2 skrll * XXX need a better hart selection method
78 1.2 skrll */
79 1.2 skrll u_int hartid = 0;
80 1.1 skrll
81 1.1 skrll evcnt_attach_dynamic(&sc->sc_intrevs[irq], EVCNT_TYPE_INTR, NULL,
82 1.1 skrll "plic", xname);
83 1.1 skrll
84 1.1 skrll ih = &sc->sc_intr[irq];
85 1.1 skrll KASSERTMSG(ih->ih_func == NULL,
86 1.1 skrll "Oops, we need to chain PLIC interrupt handlers");
87 1.1 skrll if (ih->ih_func != NULL) {
88 1.1 skrll aprint_error_dev(sc->sc_dev, "irq slot %d already used\n", irq);
89 1.1 skrll return NULL;
90 1.1 skrll }
91 1.1 skrll ih->ih_mpsafe = (flags & IST_MPSAFE) != 0;
92 1.1 skrll ih->ih_func = func;
93 1.1 skrll ih->ih_arg = arg;
94 1.1 skrll ih->ih_irq = irq;
95 1.2 skrll ih->ih_hartid = hartid;
96 1.1 skrll
97 1.1 skrll plic_set_priority(sc, irq, 1);
98 1.2 skrll plic_enable(sc, hartid, irq);
99 1.1 skrll
100 1.1 skrll return ih;
101 1.1 skrll }
102 1.1 skrll
103 1.1 skrll void
104 1.1 skrll plic_intr_disestablish(void *cookie)
105 1.1 skrll {
106 1.1 skrll struct plic_softc * const sc = plic_sc;
107 1.1 skrll struct plic_intrhand * const ih = cookie;
108 1.2 skrll const u_int hartid = ih->ih_hartid;
109 1.1 skrll const u_int irq = ih->ih_irq;
110 1.1 skrll
111 1.2 skrll plic_disable(sc, hartid, irq);
112 1.1 skrll plic_set_priority(sc, irq, 0);
113 1.1 skrll
114 1.1 skrll memset(&sc->sc_intr[irq], 0, sizeof(*sc->sc_intr));
115 1.1 skrll }
116 1.1 skrll
117 1.1 skrll int
118 1.1 skrll plic_intr(void *arg)
119 1.1 skrll {
120 1.1 skrll struct plic_softc * const sc = arg;
121 1.2 skrll const cpuid_t hartid = cpu_number();
122 1.2 skrll const bus_addr_t claim_addr = PLIC_CLAIM(sc, hartid);
123 1.2 skrll const bus_addr_t complete_addr = PLIC_COMPLETE(sc, hartid);
124 1.1 skrll uint32_t pending;
125 1.1 skrll int rv = 0;
126 1.1 skrll
127 1.1 skrll while ((pending = PLIC_READ(sc, claim_addr)) > 0) {
128 1.1 skrll struct plic_intrhand *ih = &sc->sc_intr[pending];
129 1.1 skrll
130 1.1 skrll sc->sc_intrevs[pending].ev_count++;
131 1.1 skrll
132 1.1 skrll KASSERT(ih->ih_func != NULL);
133 1.1 skrll #ifdef MULTIPROCESSOR
134 1.1 skrll if (!ih->ih_mpsafe) {
135 1.1 skrll KERNEL_LOCK(1, NULL);
136 1.1 skrll rv |= ih->ih_func(ih->ih_arg);
137 1.1 skrll KERNEL_UNLOCK_ONE(NULL);
138 1.1 skrll } else
139 1.1 skrll #endif
140 1.1 skrll rv |= ih->ih_func(ih->ih_arg);
141 1.1 skrll
142 1.1 skrll PLIC_WRITE(sc, complete_addr, pending);
143 1.1 skrll }
144 1.1 skrll
145 1.1 skrll return rv;
146 1.1 skrll }
147 1.1 skrll
148 1.1 skrll void
149 1.2 skrll plic_enable(struct plic_softc *sc, u_int hartid, u_int irq)
150 1.1 skrll {
151 1.1 skrll KASSERT(irq < PLIC_NIRQ);
152 1.2 skrll const bus_addr_t addr = PLIC_ENABLE(sc, hartid, irq);
153 1.1 skrll const uint32_t mask = __BIT(irq % 32);
154 1.1 skrll
155 1.1 skrll uint32_t reg = PLIC_READ(sc, addr);
156 1.1 skrll reg |= mask;
157 1.2 skrll
158 1.1 skrll PLIC_WRITE(sc, addr, reg);
159 1.1 skrll }
160 1.1 skrll
161 1.1 skrll void
162 1.2 skrll plic_disable(struct plic_softc *sc, u_int hartid, u_int irq)
163 1.1 skrll {
164 1.1 skrll KASSERT(irq < PLIC_NIRQ);
165 1.2 skrll const bus_addr_t addr = PLIC_ENABLE(sc, hartid, irq);
166 1.1 skrll const uint32_t mask = __BIT(irq % 32);
167 1.1 skrll
168 1.1 skrll uint32_t reg = PLIC_READ(sc, addr);
169 1.1 skrll reg &= ~mask;
170 1.1 skrll PLIC_WRITE(sc, addr, reg);
171 1.1 skrll }
172 1.1 skrll
173 1.1 skrll void
174 1.1 skrll plic_set_priority(struct plic_softc *sc, u_int irq, uint32_t priority)
175 1.1 skrll {
176 1.1 skrll KASSERT(irq < PLIC_NIRQ);
177 1.1 skrll const bus_addr_t addr = PLIC_PRIORITY(irq);
178 1.1 skrll
179 1.1 skrll PLIC_WRITE(sc, addr, priority);
180 1.1 skrll }
181 1.1 skrll
182 1.1 skrll void
183 1.2 skrll plic_set_threshold(struct plic_softc *sc, cpuid_t hartid, uint32_t threshold)
184 1.1 skrll {
185 1.2 skrll const bus_addr_t addr = PLIC_THRESHOLD(sc, hartid);
186 1.1 skrll
187 1.1 skrll PLIC_WRITE(sc, addr, threshold);
188 1.1 skrll }
189 1.1 skrll
190 1.1 skrll int
191 1.1 skrll plic_attach_common(struct plic_softc *sc, bus_addr_t addr, bus_size_t size)
192 1.1 skrll {
193 1.1 skrll struct cpu_info *ci;
194 1.1 skrll CPU_INFO_ITERATOR cii;
195 1.1 skrll u_int irq;
196 1.1 skrll
197 1.1 skrll sc->sc_intr = kmem_zalloc(sizeof(*sc->sc_intr) * sc->sc_ndev,
198 1.1 skrll KM_SLEEP);
199 1.1 skrll sc->sc_intrevs = kmem_zalloc(sizeof(*sc->sc_intrevs) * sc->sc_ndev,
200 1.1 skrll KM_SLEEP);
201 1.1 skrll
202 1.1 skrll if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
203 1.1 skrll aprint_error("couldn't map registers\n");
204 1.1 skrll return -1;
205 1.1 skrll }
206 1.1 skrll
207 1.1 skrll aprint_naive("\n");
208 1.1 skrll aprint_normal("RISC-V PLIC (%u IRQs)\n", sc->sc_ndev);
209 1.1 skrll
210 1.1 skrll plic_sc = sc;
211 1.1 skrll
212 1.1 skrll /* Start with all interrupts disabled. */
213 1.1 skrll for (irq = PLIC_FIRST_IRQ; irq < sc->sc_ndev; irq++) {
214 1.1 skrll plic_set_priority(sc, irq, 0);
215 1.1 skrll }
216 1.1 skrll
217 1.1 skrll /* Set priority thresholds for all interrupts to 0 (not masked). */
218 1.1 skrll for (CPU_INFO_FOREACH(cii, ci)) {
219 1.1 skrll plic_set_threshold(sc, ci->ci_cpuid, 0);
220 1.1 skrll }
221 1.1 skrll
222 1.1 skrll return 0;
223 1.1 skrll }
224