pci_kn300.c revision 1.40 1 /* $NetBSD: pci_kn300.c,v 1.40 2021/06/19 16:59:07 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1998 by Matthew Jacob
5 * NASA AMES Research Center.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34
35 __KERNEL_RCSID(0, "$NetBSD: pci_kn300.c,v 1.40 2021/06/19 16:59:07 thorpej Exp $");
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/malloc.h>
43 #include <sys/device.h>
44 #include <sys/cpu.h>
45 #include <sys/syslog.h>
46
47 #include <machine/autoconf.h>
48 #include <machine/rpb.h>
49
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52
53 #include <alpha/mcbus/mcbusvar.h>
54 #include <alpha/mcbus/mcbusreg.h>
55 #include <alpha/pci/mcpciareg.h>
56 #include <alpha/pci/mcpciavar.h>
57
58 #include "sio.h"
59 #if NSIO > 0 || NPCEB > 0
60 #include <alpha/pci/siovar.h>
61 #endif
62
63 static int dec_kn300_intr_map(const struct pci_attach_args *,
64 pci_intr_handle_t *);
65 static const char *dec_kn300_intr_string(pci_chipset_tag_t, pci_intr_handle_t,
66 char *, size_t);
67 static const struct evcnt *dec_kn300_intr_evcnt(pci_chipset_tag_t,
68 pci_intr_handle_t);
69 static void *dec_kn300_intr_establish(pci_chipset_tag_t,
70 pci_intr_handle_t, int, int (*func)(void *), void *);
71 static void dec_kn300_intr_disestablish(pci_chipset_tag_t, void *);
72
73 #define KN300_PCEB_IRQ 16
74 #define NPIN 4
75
76 #define NIRQ (MAX_MC_BUS * MCPCIA_PER_MCBUS * MCPCIA_MAXSLOT * NPIN)
77 static int savirqs[NIRQ];
78
79 static struct alpha_shared_intr *kn300_pci_intr;
80
81 static struct mcpcia_config *mcpcia_eisaccp = NULL;
82
83 static void kn300_iointr(void *, unsigned long);
84 static void kn300_enable_intr(struct mcpcia_config *, int);
85 static void kn300_disable_intr(struct mcpcia_config *, int);
86
87 static void
88 pci_kn300_pickintr(void *core, bus_space_tag_t iot, bus_space_tag_t memt,
89 pci_chipset_tag_t pc)
90 {
91 struct mcpcia_config *ccp = core;
92 char *cp;
93
94 if (kn300_pci_intr == NULL) {
95 int g;
96
97 #define PCI_KN300_IRQ_STR 16
98 kn300_pci_intr = alpha_shared_intr_alloc(NIRQ,
99 PCI_KN300_IRQ_STR);
100 for (g = 0; g < NIRQ; g++) {
101 alpha_shared_intr_set_maxstrays(kn300_pci_intr, g, 25);
102 cp = alpha_shared_intr_string(kn300_pci_intr, g);
103 snprintf(cp, PCI_KN300_IRQ_STR, "irq %d", g);
104 evcnt_attach_dynamic(alpha_shared_intr_evcnt(
105 kn300_pci_intr, g), EVCNT_TYPE_INTR, NULL,
106 "kn300", cp);
107 savirqs[g] = (char) -1;
108 }
109 }
110
111 pc->pc_intr_v = core;
112 pc->pc_intr_map = dec_kn300_intr_map;
113 pc->pc_intr_string = dec_kn300_intr_string;
114 pc->pc_intr_evcnt = dec_kn300_intr_evcnt;
115 pc->pc_intr_establish = dec_kn300_intr_establish;
116 pc->pc_intr_disestablish = dec_kn300_intr_disestablish;
117
118 /* Not supported on KN300. */
119 pc->pc_pciide_compat_intr_establish = NULL;
120
121 if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
122 mcpcia_eisaccp = ccp;
123 #if NSIO > 0 || NPCEB > 0
124 sio_intr_setup(pc, &ccp->cc_iot);
125 kn300_enable_intr(ccp, KN300_PCEB_IRQ);
126 #endif
127 }
128 }
129 ALPHA_PCI_INTR_INIT(ST_DEC_4100, pci_kn300_pickintr)
130
131 static int
132 dec_kn300_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
133 {
134 pcitag_t bustag = pa->pa_intrtag;
135 int buspin = pa->pa_intrpin;
136 pci_chipset_tag_t pc = pa->pa_pc;
137 struct mcpcia_config *ccp = (struct mcpcia_config *)pc->pc_intr_v;
138 int device;
139 int mcpcia_irq;
140
141 if (buspin == 0) {
142 /* No IRQ used. */
143 return 1;
144 }
145 if (buspin < 0 || buspin > 4) {
146 printf("dec_kn300_intr_map: bad interrupt pin %d\n", buspin);
147 return 1;
148 }
149
150 pci_decompose_tag(pc, bustag, NULL, &device, NULL);
151
152 /*
153 * On MID 5 device 1 is the internal NCR 53c810.
154 */
155 if (ccp->cc_mid == 5 && device == 1) {
156 mcpcia_irq = 16;
157 } else if (device >= 2 && device <= 5) {
158 mcpcia_irq = (device - 2) * 4 + buspin - 1;
159 } else {
160 printf("dec_kn300_intr_map: weird device number %d\n", device);
161 return(1);
162 }
163
164 /*
165 * handle layout:
166 *
167 * Determine kn300 IRQ (encoded in SCB vector):
168 * bits 0..1 buspin-1
169 * bits 2..4 PCI Slot (0..7- yes, some don't exist)
170 * bits 5..7 MID-4
171 * bits 8..10 7-GID
172 *
173 * Software only:
174 * bits 11-15 MCPCIA IRQ
175 */
176 const u_int irq =
177 (buspin - 1 ) |
178 ((device & 0x7) << 2) |
179 ((ccp->cc_mid - 4) << 5) |
180 ((7 - ccp->cc_gid) << 8) |
181 (mcpcia_irq << 11);
182 alpha_pci_intr_handle_init(ihp, irq, 0);
183 return (0);
184 }
185
186 static const char *
187 dec_kn300_intr_string(pci_chipset_tag_t const pc __unused,
188 pci_intr_handle_t const ih, char * const buf, size_t const len)
189 {
190 const u_int irq = alpha_pci_intr_handle_get_irq(&ih) & 0x3ff;
191
192 snprintf(buf, len, "kn300 irq %u", irq);
193 return buf;
194 }
195
196 static const struct evcnt *
197 dec_kn300_intr_evcnt(pci_chipset_tag_t const pc __unused,
198 pci_intr_handle_t const ih)
199 {
200 const u_int irq = alpha_pci_intr_handle_get_irq(&ih) & 0x3ff;
201
202 return alpha_shared_intr_evcnt(kn300_pci_intr, irq);
203 }
204
205 static void *
206 dec_kn300_intr_establish(
207 pci_chipset_tag_t const pc,
208 pci_intr_handle_t const ih,
209 int const level,
210 int (*func)(void *),
211 void *arg)
212 {
213 struct mcpcia_config * const ccp = pc->pc_intr_v;
214 void *cookie;
215 const u_int ihv = alpha_pci_intr_handle_get_irq(&ih);
216 const u_int irq = ihv & 0x3ff;
217 const u_int flags = alpha_pci_intr_handle_get_flags(&ih);
218
219 cookie = alpha_shared_intr_alloc_intrhand(kn300_pci_intr, irq,
220 IST_LEVEL, level, flags, func, arg, "kn300 irq");
221
222 if (cookie == NULL)
223 return NULL;
224
225 mutex_enter(&cpu_lock);
226
227 if (! alpha_shared_intr_link(kn300_pci_intr, cookie, "kn300 irq")) {
228 mutex_exit(&cpu_lock);
229 alpha_shared_intr_free_intrhand(cookie);
230 return NULL;
231 }
232
233 if (alpha_shared_intr_firstactive(kn300_pci_intr, irq)) {
234 scb_set(MCPCIA_VEC_PCI + SCB_IDXTOVEC(irq), kn300_iointr, NULL);
235 alpha_shared_intr_set_private(kn300_pci_intr, irq, ccp);
236 savirqs[irq] = (ihv >> 11) & 0x1f;
237 alpha_mb();
238 kn300_enable_intr(ccp, savirqs[irq]);
239 }
240
241 mutex_exit(&cpu_lock);
242
243 return cookie;
244 }
245
246 static void
247 dec_kn300_intr_disestablish(pci_chipset_tag_t const pc __unused,
248 void * const cookie __unused)
249 {
250 panic("dec_kn300_intr_disestablish not implemented");
251 }
252
253 static void
254 kn300_iointr(void * const arg __unused, unsigned long const vec)
255 {
256 struct mcpcia_softc *mcp;
257 u_long irq;
258
259 irq = SCB_VECTOIDX(vec - MCPCIA_VEC_PCI);
260
261 if (alpha_shared_intr_dispatch(kn300_pci_intr, irq)) {
262 /*
263 * Any claim of an interrupt at this level is a hint to
264 * reset the stray interrupt count- elsewise a slow leak
265 * over time will cause this level to be shutdown.
266 */
267 alpha_shared_intr_reset_strays(kn300_pci_intr, irq);
268 return;
269 }
270
271 /*
272 * If we haven't finished configuring yet, or there is no mcp
273 * registered for this level yet, just return.
274 */
275 mcp = alpha_shared_intr_get_private(kn300_pci_intr, irq);
276 if (mcp == NULL || mcp->mcpcia_cc == NULL)
277 return;
278
279 /*
280 * We're getting an interrupt for a device we haven't enabled.
281 * We had better not try and use -1 to find the right bit to disable.
282 */
283 if (savirqs[irq] == -1) {
284 printf("kn300_iointr: stray interrupt vector 0x%lx\n", vec);
285 return;
286 }
287
288 /*
289 * Stray interrupt; disable the IRQ on the appropriate MCPCIA
290 * if we've reached the limit.
291 */
292 alpha_shared_intr_stray(kn300_pci_intr, irq, "kn300 irq");
293 if (ALPHA_SHARED_INTR_DISABLE(kn300_pci_intr, irq) == 0)
294 return;
295 kn300_disable_intr(mcp->mcpcia_cc, savirqs[irq]);
296 }
297
298 static void
299 kn300_enable_intr(struct mcpcia_config * const ccp, int const irq)
300 {
301 alpha_mb();
302 REGVAL(MCPCIA_INT_MASK0(ccp)) |= (1 << irq);
303 alpha_mb();
304 }
305
306 static void
307 kn300_disable_intr(struct mcpcia_config * const ccp, int const irq)
308 {
309 alpha_mb();
310 REGVAL(MCPCIA_INT_MASK0(ccp)) &= ~(1 << irq);
311 alpha_mb();
312 }
313