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