sio_pic.c revision 1.50 1 1.50 thorpej /* $NetBSD: sio_pic.c,v 1.50 2021/06/25 18:08:34 thorpej Exp $ */
2 1.20 thorpej
3 1.20 thorpej /*-
4 1.44 thorpej * Copyright (c) 1998, 2000, 2020 The NetBSD Foundation, Inc.
5 1.20 thorpej * All rights reserved.
6 1.20 thorpej *
7 1.20 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.20 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.20 thorpej * NASA Ames Research Center.
10 1.20 thorpej *
11 1.20 thorpej * Redistribution and use in source and binary forms, with or without
12 1.20 thorpej * modification, are permitted provided that the following conditions
13 1.20 thorpej * are met:
14 1.20 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.20 thorpej * notice, this list of conditions and the following disclaimer.
16 1.20 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.20 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.20 thorpej * documentation and/or other materials provided with the distribution.
19 1.20 thorpej *
20 1.20 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.20 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.20 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.20 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.20 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.20 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.20 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.20 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.20 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.20 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.20 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.20 thorpej */
32 1.1 cgd
33 1.1 cgd /*
34 1.6 cgd * Copyright (c) 1995, 1996 Carnegie-Mellon University.
35 1.1 cgd * All rights reserved.
36 1.1 cgd *
37 1.1 cgd * Author: Chris G. Demetriou
38 1.42 matt *
39 1.1 cgd * Permission to use, copy, modify and distribute this software and
40 1.1 cgd * its documentation is hereby granted, provided that both the copyright
41 1.1 cgd * notice and this permission notice appear in all copies of the
42 1.1 cgd * software, derivative works or modified versions, and any portions
43 1.1 cgd * thereof, and that both notices appear in supporting documentation.
44 1.42 matt *
45 1.42 matt * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 1.42 matt * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47 1.1 cgd * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 1.42 matt *
49 1.1 cgd * Carnegie Mellon requests users of this software to return to
50 1.1 cgd *
51 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
52 1.1 cgd * School of Computer Science
53 1.1 cgd * Carnegie Mellon University
54 1.1 cgd * Pittsburgh PA 15213-3890
55 1.1 cgd *
56 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
57 1.1 cgd * rights to redistribute these changes.
58 1.1 cgd */
59 1.17 cgd
60 1.18 cgd #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
61 1.18 cgd
62 1.50 thorpej __KERNEL_RCSID(0, "$NetBSD: sio_pic.c,v 1.50 2021/06/25 18:08:34 thorpej Exp $");
63 1.1 cgd
64 1.1 cgd #include <sys/param.h>
65 1.1 cgd #include <sys/systm.h>
66 1.1 cgd #include <sys/device.h>
67 1.1 cgd #include <sys/malloc.h>
68 1.45 thorpej #include <sys/cpu.h>
69 1.1 cgd #include <sys/syslog.h>
70 1.1 cgd
71 1.46 thorpej #include <machine/alpha.h>
72 1.4 cgd #include <machine/intr.h>
73 1.41 dyoung #include <sys/bus.h>
74 1.4 cgd
75 1.20 thorpej #include <dev/pci/pcireg.h>
76 1.20 thorpej #include <dev/pci/pcivar.h>
77 1.20 thorpej #include <dev/pci/pcidevs.h>
78 1.20 thorpej
79 1.44 thorpej #include <dev/pci/pciidereg.h>
80 1.44 thorpej #include <dev/pci/pciidevar.h>
81 1.44 thorpej
82 1.28 thorpej #include <dev/pci/cy82c693reg.h>
83 1.28 thorpej #include <dev/pci/cy82c693var.h>
84 1.28 thorpej
85 1.2 cgd #include <dev/isa/isareg.h>
86 1.1 cgd #include <dev/isa/isavar.h>
87 1.49 thorpej #include <alpha/pci/sioreg.h>
88 1.2 cgd #include <alpha/pci/siovar.h>
89 1.2 cgd
90 1.2 cgd #include "sio.h"
91 1.1 cgd
92 1.1 cgd /*
93 1.1 cgd * To add to the long history of wonderful PROM console traits,
94 1.1 cgd * AlphaStation PROMs don't reset themselves completely on boot!
95 1.1 cgd * Therefore, if an interrupt was turned on when the kernel was
96 1.1 cgd * started, we're not going to EVER turn it off... I don't know
97 1.1 cgd * what will happen if new interrupts (that the PROM console doesn't
98 1.1 cgd * want) are turned on. I'll burn that bridge when I come to it.
99 1.1 cgd */
100 1.1 cgd #define BROKEN_PROM_CONSOLE
101 1.1 cgd
102 1.2 cgd /*
103 1.2 cgd * Private functions and variables.
104 1.2 cgd */
105 1.4 cgd
106 1.48 thorpej static bus_space_tag_t sio_iot;
107 1.48 thorpej static pci_chipset_tag_t sio_pc;
108 1.48 thorpej static bus_space_handle_t sio_ioh_icu1, sio_ioh_icu2;
109 1.1 cgd
110 1.1 cgd #define ICU_LEN 16 /* number of ISA IRQs */
111 1.1 cgd
112 1.16 cgd static struct alpha_shared_intr *sio_intr;
113 1.1 cgd
114 1.1 cgd #ifndef STRAY_MAX
115 1.1 cgd #define STRAY_MAX 5
116 1.1 cgd #endif
117 1.1 cgd
118 1.1 cgd #ifdef BROKEN_PROM_CONSOLE
119 1.1 cgd /*
120 1.1 cgd * If prom console is broken, must remember the initial interrupt
121 1.1 cgd * settings and enforce them. WHEE!
122 1.1 cgd */
123 1.47 thorpej static uint8_t initial_ocw1[2];
124 1.47 thorpej static uint8_t initial_elcr[2];
125 1.1 cgd #endif
126 1.1 cgd
127 1.47 thorpej static void sio_setirqstat(int, int, int);
128 1.20 thorpej
129 1.47 thorpej static uint8_t (*sio_read_elcr)(int);
130 1.47 thorpej static void (*sio_write_elcr)(int, uint8_t);
131 1.37 dsl static void specific_eoi(int);
132 1.25 mycroft #ifdef BROKEN_PROM_CONSOLE
133 1.47 thorpej static void sio_intr_shutdown(void *);
134 1.25 mycroft #endif
135 1.20 thorpej
136 1.20 thorpej /******************** i82378 SIO ELCR functions ********************/
137 1.20 thorpej
138 1.47 thorpej static bus_space_handle_t sio_ioh_elcr;
139 1.20 thorpej
140 1.47 thorpej static uint8_t i82378_read_elcr(int);
141 1.47 thorpej static void i82378_write_elcr(int, uint8_t);
142 1.28 thorpej
143 1.47 thorpej static int
144 1.42 matt i82378_setup_elcr(void)
145 1.20 thorpej {
146 1.20 thorpej int rv;
147 1.20 thorpej
148 1.20 thorpej /*
149 1.20 thorpej * We could probe configuration space to see that there's
150 1.20 thorpej * actually an SIO present, but we are using this as a
151 1.20 thorpej * fall-back in case nothing else matches.
152 1.20 thorpej */
153 1.20 thorpej
154 1.20 thorpej rv = bus_space_map(sio_iot, 0x4d0, 2, 0, &sio_ioh_elcr);
155 1.20 thorpej
156 1.20 thorpej if (rv == 0) {
157 1.20 thorpej sio_read_elcr = i82378_read_elcr;
158 1.20 thorpej sio_write_elcr = i82378_write_elcr;
159 1.20 thorpej }
160 1.20 thorpej
161 1.20 thorpej return (rv);
162 1.20 thorpej }
163 1.20 thorpej
164 1.47 thorpej static uint8_t
165 1.38 dsl i82378_read_elcr(int elcr)
166 1.20 thorpej {
167 1.20 thorpej
168 1.20 thorpej return (bus_space_read_1(sio_iot, sio_ioh_elcr, elcr));
169 1.20 thorpej }
170 1.20 thorpej
171 1.47 thorpej static void
172 1.42 matt i82378_write_elcr(int elcr, uint8_t val)
173 1.20 thorpej {
174 1.20 thorpej
175 1.20 thorpej bus_space_write_1(sio_iot, sio_ioh_elcr, elcr, val);
176 1.20 thorpej }
177 1.20 thorpej
178 1.20 thorpej /******************** Cypress CY82C693 ELCR functions ********************/
179 1.20 thorpej
180 1.47 thorpej static const struct cy82c693_handle *sio_cy82c693_handle;
181 1.20 thorpej
182 1.47 thorpej static uint8_t cy82c693_read_elcr(int);
183 1.47 thorpej static void cy82c693_write_elcr(int, uint8_t);
184 1.28 thorpej
185 1.47 thorpej static int
186 1.42 matt cy82c693_setup_elcr(void)
187 1.20 thorpej {
188 1.20 thorpej int device, maxndevs;
189 1.20 thorpej pcitag_t tag;
190 1.20 thorpej pcireg_t id;
191 1.20 thorpej
192 1.20 thorpej /*
193 1.20 thorpej * Search PCI configuration space for a Cypress CY82C693.
194 1.20 thorpej *
195 1.20 thorpej * Note we can make some assumptions about our bus number
196 1.20 thorpej * here, because:
197 1.20 thorpej *
198 1.20 thorpej * (1) there can be at most one ISA/EISA bridge per PCI bus, and
199 1.20 thorpej *
200 1.20 thorpej * (2) any ISA/EISA bridges must be attached to primary PCI
201 1.20 thorpej * busses (i.e. bus zero).
202 1.20 thorpej */
203 1.20 thorpej
204 1.20 thorpej maxndevs = pci_bus_maxdevs(sio_pc, 0);
205 1.20 thorpej
206 1.20 thorpej for (device = 0; device < maxndevs; device++) {
207 1.20 thorpej tag = pci_make_tag(sio_pc, 0, device, 0);
208 1.20 thorpej id = pci_conf_read(sio_pc, tag, PCI_ID_REG);
209 1.20 thorpej
210 1.20 thorpej /* Invalid vendor ID value? */
211 1.20 thorpej if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
212 1.20 thorpej continue;
213 1.20 thorpej /* XXX Not invalid, but we've done this ~forever. */
214 1.20 thorpej if (PCI_VENDOR(id) == 0)
215 1.20 thorpej continue;
216 1.20 thorpej
217 1.20 thorpej if (PCI_VENDOR(id) != PCI_VENDOR_CONTAQ ||
218 1.20 thorpej PCI_PRODUCT(id) != PCI_PRODUCT_CONTAQ_82C693)
219 1.20 thorpej continue;
220 1.20 thorpej
221 1.20 thorpej /*
222 1.20 thorpej * Found one!
223 1.20 thorpej */
224 1.20 thorpej
225 1.20 thorpej #if 0
226 1.20 thorpej printf("cy82c693_setup_elcr: found 82C693 at device %d\n",
227 1.20 thorpej device);
228 1.20 thorpej #endif
229 1.20 thorpej
230 1.28 thorpej sio_cy82c693_handle = cy82c693_init(sio_iot);
231 1.20 thorpej sio_read_elcr = cy82c693_read_elcr;
232 1.20 thorpej sio_write_elcr = cy82c693_write_elcr;
233 1.20 thorpej
234 1.20 thorpej return (0);
235 1.20 thorpej }
236 1.20 thorpej
237 1.20 thorpej /*
238 1.20 thorpej * Didn't find a CY82C693.
239 1.20 thorpej */
240 1.20 thorpej return (ENODEV);
241 1.20 thorpej }
242 1.20 thorpej
243 1.47 thorpej static uint8_t
244 1.38 dsl cy82c693_read_elcr(int elcr)
245 1.20 thorpej {
246 1.20 thorpej
247 1.28 thorpej return (cy82c693_read(sio_cy82c693_handle, CONFIG_ELCR1 + elcr));
248 1.20 thorpej }
249 1.20 thorpej
250 1.47 thorpej static void
251 1.42 matt cy82c693_write_elcr(int elcr, uint8_t val)
252 1.20 thorpej {
253 1.20 thorpej
254 1.28 thorpej cy82c693_write(sio_cy82c693_handle, CONFIG_ELCR1 + elcr, val);
255 1.20 thorpej }
256 1.20 thorpej
257 1.20 thorpej /******************** ELCR access function configuration ********************/
258 1.20 thorpej
259 1.20 thorpej /*
260 1.20 thorpej * Put the Intel SIO at the end, so we fall back on it if we don't
261 1.20 thorpej * find anything else. If any of the non-Intel functions find a
262 1.20 thorpej * matching device, but are unable to map it for whatever reason,
263 1.20 thorpej * they should panic.
264 1.20 thorpej */
265 1.20 thorpej
266 1.48 thorpej static int (*const sio_elcr_setup_funcs[])(void) = {
267 1.20 thorpej cy82c693_setup_elcr,
268 1.20 thorpej i82378_setup_elcr,
269 1.20 thorpej NULL,
270 1.20 thorpej };
271 1.20 thorpej
272 1.20 thorpej /******************** Shared SIO/Cypress functions ********************/
273 1.15 cgd
274 1.47 thorpej static void
275 1.39 dsl sio_setirqstat(int irq, int enabled, int type)
276 1.1 cgd {
277 1.42 matt uint8_t ocw1[2], elcr[2];
278 1.1 cgd int icu, bit;
279 1.1 cgd
280 1.1 cgd #if 0
281 1.13 christos printf("sio_setirqstat: irq %d: %s, %s\n", irq,
282 1.1 cgd enabled ? "enabled" : "disabled", isa_intr_typename(type));
283 1.1 cgd #endif
284 1.1 cgd
285 1.1 cgd icu = irq / 8;
286 1.1 cgd bit = irq % 8;
287 1.1 cgd
288 1.14 cgd ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
289 1.14 cgd ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
290 1.20 thorpej elcr[0] = (*sio_read_elcr)(0); /* XXX */
291 1.20 thorpej elcr[1] = (*sio_read_elcr)(1); /* XXX */
292 1.1 cgd
293 1.1 cgd /*
294 1.1 cgd * interrupt enable: set bit to mask (disable) interrupt.
295 1.1 cgd */
296 1.1 cgd if (enabled)
297 1.1 cgd ocw1[icu] &= ~(1 << bit);
298 1.1 cgd else
299 1.1 cgd ocw1[icu] |= 1 << bit;
300 1.1 cgd
301 1.1 cgd /*
302 1.1 cgd * interrupt type select: set bit to get level-triggered.
303 1.1 cgd */
304 1.3 mycroft if (type == IST_LEVEL)
305 1.1 cgd elcr[icu] |= 1 << bit;
306 1.1 cgd else
307 1.1 cgd elcr[icu] &= ~(1 << bit);
308 1.1 cgd
309 1.1 cgd #ifdef not_here
310 1.1 cgd /* see the init function... */
311 1.1 cgd ocw1[0] &= ~0x04; /* always enable IRQ2 on first PIC */
312 1.1 cgd elcr[0] &= ~0x07; /* IRQ[0-2] must be edge-triggered */
313 1.1 cgd elcr[1] &= ~0x21; /* IRQ[13,8] must be edge-triggered */
314 1.1 cgd #endif
315 1.1 cgd
316 1.14 cgd bus_space_write_1(sio_iot, sio_ioh_icu1, 1, ocw1[0]);
317 1.14 cgd bus_space_write_1(sio_iot, sio_ioh_icu2, 1, ocw1[1]);
318 1.20 thorpej (*sio_write_elcr)(0, elcr[0]); /* XXX */
319 1.20 thorpej (*sio_write_elcr)(1, elcr[1]); /* XXX */
320 1.1 cgd }
321 1.1 cgd
322 1.1 cgd void
323 1.38 dsl sio_intr_setup(pci_chipset_tag_t pc, bus_space_tag_t iot)
324 1.1 cgd {
325 1.27 thorpej char *cp;
326 1.1 cgd int i;
327 1.1 cgd
328 1.14 cgd sio_iot = iot;
329 1.20 thorpej sio_pc = pc;
330 1.4 cgd
331 1.28 thorpej if (bus_space_map(sio_iot, IO_ICU1, 2, 0, &sio_ioh_icu1) ||
332 1.28 thorpej bus_space_map(sio_iot, IO_ICU2, 2, 0, &sio_ioh_icu2))
333 1.20 thorpej panic("sio_intr_setup: can't map ICU I/O ports");
334 1.20 thorpej
335 1.20 thorpej for (i = 0; sio_elcr_setup_funcs[i] != NULL; i++)
336 1.20 thorpej if ((*sio_elcr_setup_funcs[i])() == 0)
337 1.20 thorpej break;
338 1.20 thorpej if (sio_elcr_setup_funcs[i] == NULL)
339 1.20 thorpej panic("sio_intr_setup: can't map ELCR");
340 1.2 cgd
341 1.1 cgd #ifdef BROKEN_PROM_CONSOLE
342 1.1 cgd /*
343 1.25 mycroft * Remember the initial values, so we can restore them later.
344 1.1 cgd */
345 1.14 cgd initial_ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
346 1.14 cgd initial_ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
347 1.20 thorpej initial_elcr[0] = (*sio_read_elcr)(0); /* XXX */
348 1.20 thorpej initial_elcr[1] = (*sio_read_elcr)(1); /* XXX */
349 1.25 mycroft shutdownhook_establish(sio_intr_shutdown, 0);
350 1.1 cgd #endif
351 1.1 cgd
352 1.43 christos #define PCI_SIO_IRQ_STR 8
353 1.43 christos sio_intr = alpha_shared_intr_alloc(ICU_LEN, PCI_SIO_IRQ_STR);
354 1.16 cgd
355 1.1 cgd /*
356 1.1 cgd * set up initial values for interrupt enables.
357 1.1 cgd */
358 1.1 cgd for (i = 0; i < ICU_LEN; i++) {
359 1.16 cgd alpha_shared_intr_set_maxstrays(sio_intr, i, STRAY_MAX);
360 1.16 cgd
361 1.27 thorpej cp = alpha_shared_intr_string(sio_intr, i);
362 1.43 christos snprintf(cp, PCI_SIO_IRQ_STR, "irq %d", i);
363 1.27 thorpej evcnt_attach_dynamic(alpha_shared_intr_evcnt(sio_intr, i),
364 1.27 thorpej EVCNT_TYPE_INTR, NULL, "isa", cp);
365 1.27 thorpej
366 1.1 cgd switch (i) {
367 1.1 cgd case 0:
368 1.1 cgd case 1:
369 1.1 cgd case 8:
370 1.1 cgd case 13:
371 1.1 cgd /*
372 1.1 cgd * IRQs 0, 1, 8, and 13 must always be
373 1.1 cgd * edge-triggered.
374 1.1 cgd */
375 1.32 mycroft sio_setirqstat(i, 0, IST_EDGE);
376 1.16 cgd alpha_shared_intr_set_dfltsharetype(sio_intr, i,
377 1.16 cgd IST_EDGE);
378 1.24 ross specific_eoi(i);
379 1.1 cgd break;
380 1.1 cgd
381 1.1 cgd case 2:
382 1.1 cgd /*
383 1.1 cgd * IRQ 2 must be edge-triggered, and should be
384 1.1 cgd * enabled (otherwise IRQs 8-15 are ignored).
385 1.1 cgd */
386 1.3 mycroft sio_setirqstat(i, 1, IST_EDGE);
387 1.16 cgd alpha_shared_intr_set_dfltsharetype(sio_intr, i,
388 1.16 cgd IST_UNUSABLE);
389 1.1 cgd break;
390 1.1 cgd
391 1.1 cgd default:
392 1.1 cgd /*
393 1.1 cgd * Otherwise, disable the IRQ and set its
394 1.1 cgd * type to (effectively) "unknown."
395 1.1 cgd */
396 1.32 mycroft sio_setirqstat(i, 0, IST_NONE);
397 1.16 cgd alpha_shared_intr_set_dfltsharetype(sio_intr, i,
398 1.32 mycroft IST_NONE);
399 1.24 ross specific_eoi(i);
400 1.1 cgd break;
401 1.1 cgd }
402 1.1 cgd }
403 1.1 cgd }
404 1.1 cgd
405 1.25 mycroft #ifdef BROKEN_PROM_CONSOLE
406 1.47 thorpej static void
407 1.38 dsl sio_intr_shutdown(void *arg)
408 1.25 mycroft {
409 1.25 mycroft /*
410 1.25 mycroft * Restore the initial values, to make the PROM happy.
411 1.25 mycroft */
412 1.25 mycroft bus_space_write_1(sio_iot, sio_ioh_icu1, 1, initial_ocw1[0]);
413 1.25 mycroft bus_space_write_1(sio_iot, sio_ioh_icu2, 1, initial_ocw1[1]);
414 1.25 mycroft (*sio_write_elcr)(0, initial_elcr[0]); /* XXX */
415 1.25 mycroft (*sio_write_elcr)(1, initial_elcr[1]); /* XXX */
416 1.25 mycroft }
417 1.25 mycroft #endif
418 1.25 mycroft
419 1.4 cgd const char *
420 1.43 christos sio_intr_string(void *v, int irq, char *buf, size_t len)
421 1.4 cgd {
422 1.4 cgd if (irq == 0 || irq >= ICU_LEN || irq == 2)
423 1.43 christos panic("%s: bogus isa irq 0x%x", __func__, irq);
424 1.4 cgd
425 1.43 christos snprintf(buf, len, "isa irq %d", irq);
426 1.43 christos return buf;
427 1.26 cgd }
428 1.26 cgd
429 1.26 cgd const struct evcnt *
430 1.38 dsl sio_intr_evcnt(void *v, int irq)
431 1.26 cgd {
432 1.26 cgd
433 1.27 thorpej if (irq == 0 || irq >= ICU_LEN || irq == 2)
434 1.43 christos panic("%s: bogus isa irq 0x%x", __func__, irq);
435 1.27 thorpej
436 1.27 thorpej return (alpha_shared_intr_evcnt(sio_intr, irq));
437 1.4 cgd }
438 1.4 cgd
439 1.1 cgd void *
440 1.44 thorpej sio_intr_establish(void *v, int irq, int type, int level, int flags,
441 1.44 thorpej int (*fn)(void *), void *arg)
442 1.1 cgd {
443 1.16 cgd void *cookie;
444 1.1 cgd
445 1.3 mycroft if (irq > ICU_LEN || type == IST_NONE)
446 1.1 cgd panic("sio_intr_establish: bogus irq or type");
447 1.1 cgd
448 1.45 thorpej cookie = alpha_shared_intr_alloc_intrhand(sio_intr, irq, type, level,
449 1.50 thorpej flags, fn, arg, "isa");
450 1.1 cgd
451 1.45 thorpej if (cookie == NULL)
452 1.45 thorpej return NULL;
453 1.45 thorpej
454 1.45 thorpej mutex_enter(&cpu_lock);
455 1.45 thorpej
456 1.50 thorpej if (! alpha_shared_intr_link(sio_intr, cookie, "isa")) {
457 1.45 thorpej mutex_exit(&cpu_lock);
458 1.45 thorpej alpha_shared_intr_free_intrhand(cookie);
459 1.45 thorpej return NULL;
460 1.45 thorpej }
461 1.45 thorpej
462 1.45 thorpej if (alpha_shared_intr_firstactive(sio_intr, irq)) {
463 1.44 thorpej scb_set(0x800 + SCB_IDXTOVEC(irq), sio_iointr, NULL);
464 1.30 thorpej sio_setirqstat(irq, 1,
465 1.16 cgd alpha_shared_intr_get_sharetype(sio_intr, irq));
466 1.46 thorpej
467 1.46 thorpej /*
468 1.46 thorpej * I've obsesrved stray ISA interrupts when interacting
469 1.46 thorpej * with the serial console under Qemu. Work around that
470 1.46 thorpej * for now by suppressing stray interrupt reporting for
471 1.46 thorpej * edge-triggered interrupts.
472 1.46 thorpej */
473 1.46 thorpej if (alpha_is_qemu && type == IST_EDGE) {
474 1.46 thorpej alpha_shared_intr_set_maxstrays(sio_intr, irq, 0);
475 1.46 thorpej }
476 1.30 thorpej }
477 1.1 cgd
478 1.45 thorpej mutex_exit(&cpu_lock);
479 1.45 thorpej
480 1.45 thorpej return cookie;
481 1.1 cgd }
482 1.1 cgd
483 1.1 cgd void
484 1.38 dsl sio_intr_disestablish(void *v, void *cookie)
485 1.1 cgd {
486 1.22 thorpej struct alpha_shared_intrhand *ih = cookie;
487 1.45 thorpej int ist, irq = ih->ih_num;
488 1.1 cgd
489 1.45 thorpej mutex_enter(&cpu_lock);
490 1.22 thorpej
491 1.22 thorpej /*
492 1.22 thorpej * Decide if we should disable the interrupt. We must ensure
493 1.22 thorpej * that:
494 1.22 thorpej *
495 1.22 thorpej * - An initially-enabled interrupt is never disabled.
496 1.22 thorpej * - An initially-LT interrupt is never untyped.
497 1.22 thorpej */
498 1.45 thorpej if (alpha_shared_intr_firstactive(sio_intr, irq)) {
499 1.23 thorpej /*
500 1.23 thorpej * IRQs 0, 1, 8, and 13 must always be edge-triggered
501 1.23 thorpej * (see setup).
502 1.23 thorpej */
503 1.23 thorpej switch (irq) {
504 1.23 thorpej case 0:
505 1.23 thorpej case 1:
506 1.23 thorpej case 8:
507 1.23 thorpej case 13:
508 1.23 thorpej /*
509 1.23 thorpej * If the interrupt was initially level-triggered
510 1.23 thorpej * a warning was printed in setup.
511 1.23 thorpej */
512 1.23 thorpej ist = IST_EDGE;
513 1.23 thorpej break;
514 1.23 thorpej
515 1.23 thorpej default:
516 1.32 mycroft ist = IST_NONE;
517 1.23 thorpej break;
518 1.23 thorpej }
519 1.32 mycroft sio_setirqstat(irq, 0, ist);
520 1.22 thorpej alpha_shared_intr_set_dfltsharetype(sio_intr, irq, ist);
521 1.46 thorpej alpha_shared_intr_set_maxstrays(sio_intr, irq, STRAY_MAX);
522 1.30 thorpej
523 1.30 thorpej /* Release our SCB vector. */
524 1.30 thorpej scb_free(0x800 + SCB_IDXTOVEC(irq));
525 1.22 thorpej }
526 1.22 thorpej
527 1.45 thorpej /* Remove it from the link. */
528 1.50 thorpej alpha_shared_intr_unlink(sio_intr, cookie, "isa");
529 1.45 thorpej
530 1.45 thorpej mutex_exit(&cpu_lock);
531 1.45 thorpej
532 1.45 thorpej alpha_shared_intr_free_intrhand(cookie);
533 1.1 cgd }
534 1.1 cgd
535 1.49 thorpej /* XXX All known Alpha systems with Intel i82378 have it at device 7. */
536 1.49 thorpej #define SIO_I82378_DEV 7
537 1.49 thorpej
538 1.49 thorpej int
539 1.49 thorpej sio_pirq_intr_map(pci_chipset_tag_t pc, int pirq, pci_intr_handle_t *ihp)
540 1.49 thorpej {
541 1.49 thorpej KASSERT(pirq >= 0 && pirq <= 3);
542 1.49 thorpej KASSERT(pc == sio_pc);
543 1.49 thorpej
544 1.49 thorpej const pcireg_t rtctrl =
545 1.49 thorpej pci_conf_read(sio_pc, pci_make_tag(sio_pc, 0, SIO_I82378_DEV, 0),
546 1.49 thorpej SIO_PCIREG_PIRQ_RTCTRL);
547 1.49 thorpej const pcireg_t pirqreg = PIRQ_RTCTRL_PIRQx(rtctrl, pirq);
548 1.49 thorpej
549 1.49 thorpej if (pirqreg & PIRQ_RTCTRL_NOT_ROUTED) {
550 1.49 thorpej /* not routed -> no mapping */
551 1.49 thorpej return 1;
552 1.49 thorpej }
553 1.49 thorpej
554 1.49 thorpej const int irq = __SHIFTOUT(pirqreg, PIRQ_RTCTRL_IRQ);
555 1.49 thorpej
556 1.49 thorpej #if 0
557 1.49 thorpej printf("sio_pirq_intr_map: pirq %d -> ISA irq %d, rtctl = 0x%08x\n",
558 1.49 thorpej pirq, irq, rtctrl);
559 1.49 thorpej #endif
560 1.49 thorpej
561 1.49 thorpej alpha_pci_intr_handle_init(ihp, irq, 0);
562 1.49 thorpej return 0;
563 1.49 thorpej }
564 1.49 thorpej
565 1.44 thorpej const char *
566 1.44 thorpej sio_pci_intr_string(pci_chipset_tag_t const pc, pci_intr_handle_t const ih,
567 1.44 thorpej char * const buf, size_t const len)
568 1.44 thorpej {
569 1.44 thorpej const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
570 1.44 thorpej
571 1.44 thorpej return sio_intr_string(NULL /*XXX*/, irq, buf, len);
572 1.44 thorpej }
573 1.44 thorpej
574 1.44 thorpej const struct evcnt *
575 1.44 thorpej sio_pci_intr_evcnt(pci_chipset_tag_t const pc, pci_intr_handle_t const ih)
576 1.44 thorpej {
577 1.44 thorpej const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
578 1.44 thorpej
579 1.44 thorpej return sio_intr_evcnt(NULL /*XXX*/, irq);
580 1.44 thorpej }
581 1.44 thorpej
582 1.44 thorpej void *
583 1.44 thorpej sio_pci_intr_establish(pci_chipset_tag_t const pc, pci_intr_handle_t ih,
584 1.44 thorpej int const level, int (*func)(void *), void *arg)
585 1.44 thorpej {
586 1.44 thorpej const u_int irq = alpha_pci_intr_handle_get_irq(&ih);
587 1.44 thorpej const u_int flags = alpha_pci_intr_handle_get_flags(&ih);
588 1.44 thorpej
589 1.44 thorpej return sio_intr_establish(NULL /*XXX*/, irq, IST_LEVEL, level, flags,
590 1.44 thorpej func, arg);
591 1.44 thorpej }
592 1.44 thorpej
593 1.44 thorpej void
594 1.44 thorpej sio_pci_intr_disestablish(pci_chipset_tag_t const pc, void *cookie)
595 1.44 thorpej {
596 1.44 thorpej sio_intr_disestablish(NULL /*XXX*/, cookie);
597 1.44 thorpej }
598 1.44 thorpej
599 1.44 thorpej void *
600 1.44 thorpej sio_pciide_compat_intr_establish(device_t const dev,
601 1.44 thorpej const struct pci_attach_args * const pa,
602 1.44 thorpej int const chan, int (*func)(void *), void *arg)
603 1.44 thorpej {
604 1.44 thorpej pci_chipset_tag_t const pc = pa->pa_pc;
605 1.44 thorpej void *cookie;
606 1.44 thorpej int bus, irq;
607 1.44 thorpej char buf[64];
608 1.44 thorpej int flags = 0; /* XXX How to pass MPSAFE? */
609 1.44 thorpej
610 1.44 thorpej pci_decompose_tag(pc, pa->pa_tag, &bus, NULL, NULL);
611 1.44 thorpej
612 1.44 thorpej /*
613 1.44 thorpej * If this isn't PCI bus #0, all bets are off.
614 1.44 thorpej */
615 1.44 thorpej if (bus != 0)
616 1.44 thorpej return NULL;
617 1.44 thorpej
618 1.44 thorpej irq = PCIIDE_COMPAT_IRQ(chan);
619 1.44 thorpej cookie = sio_intr_establish(NULL /*XXX*/, irq, IST_EDGE, IPL_BIO,
620 1.44 thorpej flags, func, arg);
621 1.44 thorpej if (cookie == NULL)
622 1.44 thorpej return NULL;
623 1.44 thorpej
624 1.44 thorpej aprint_normal_dev(dev, "%s channel interrupting at %s\n",
625 1.44 thorpej PCIIDE_CHANNEL_NAME(chan),
626 1.44 thorpej sio_intr_string(NULL /*XXX*/, irq, buf, sizeof(buf)));
627 1.44 thorpej
628 1.44 thorpej return cookie;
629 1.44 thorpej }
630 1.44 thorpej
631 1.44 thorpej void *
632 1.44 thorpej sio_isa_intr_establish(void *v, int irq, int type, int level,
633 1.44 thorpej int (*fn)(void *), void *arg)
634 1.44 thorpej {
635 1.44 thorpej return sio_intr_establish(v, irq, type, level, 0, fn, arg);
636 1.44 thorpej }
637 1.44 thorpej
638 1.1 cgd void
639 1.38 dsl sio_iointr(void *arg, unsigned long vec)
640 1.1 cgd {
641 1.16 cgd int irq;
642 1.1 cgd
643 1.30 thorpej irq = SCB_VECTOIDX(vec - 0x800);
644 1.30 thorpej
645 1.1 cgd #ifdef DIAGNOSTIC
646 1.1 cgd if (irq > ICU_LEN || irq < 0)
647 1.1 cgd panic("sio_iointr: irq out of range (%d)", irq);
648 1.2 cgd #endif
649 1.2 cgd
650 1.16 cgd if (!alpha_shared_intr_dispatch(sio_intr, irq))
651 1.50 thorpej alpha_shared_intr_stray(sio_intr, irq, "isa");
652 1.33 thorpej else
653 1.33 thorpej alpha_shared_intr_reset_strays(sio_intr, irq);
654 1.1 cgd
655 1.1 cgd /*
656 1.1 cgd * Some versions of the machines which use the SIO
657 1.1 cgd * (or is it some PALcode revisions on those machines?)
658 1.1 cgd * require the non-specific EOI to be fed to the PIC(s)
659 1.1 cgd * by the interrupt handler.
660 1.1 cgd */
661 1.24 ross specific_eoi(irq);
662 1.21 matt }
663 1.21 matt
664 1.21 matt #define LEGAL_IRQ(x) ((x) >= 0 && (x) < ICU_LEN && (x) != 2)
665 1.21 matt
666 1.21 matt int
667 1.38 dsl sio_intr_alloc(void *v, int mask, int type, int *irq)
668 1.21 matt {
669 1.21 matt int i, tmp, bestirq, count;
670 1.21 matt struct alpha_shared_intrhand **p, *q;
671 1.21 matt
672 1.21 matt if (type == IST_NONE)
673 1.21 matt panic("intr_alloc: bogus type");
674 1.21 matt
675 1.21 matt bestirq = -1;
676 1.21 matt count = -1;
677 1.21 matt
678 1.21 matt /* some interrupts should never be dynamically allocated */
679 1.21 matt mask &= 0xdef8;
680 1.21 matt
681 1.21 matt /*
682 1.21 matt * XXX some interrupts will be used later (6 for fdc, 12 for pms).
683 1.21 matt * the right answer is to do "breadth-first" searching of devices.
684 1.21 matt */
685 1.21 matt mask &= 0xefbf;
686 1.21 matt
687 1.21 matt for (i = 0; i < ICU_LEN; i++) {
688 1.21 matt if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
689 1.21 matt continue;
690 1.21 matt
691 1.21 matt switch(sio_intr[i].intr_sharetype) {
692 1.21 matt case IST_NONE:
693 1.21 matt /*
694 1.21 matt * if nothing's using the irq, just return it
695 1.21 matt */
696 1.21 matt *irq = i;
697 1.21 matt return (0);
698 1.21 matt
699 1.21 matt case IST_EDGE:
700 1.21 matt case IST_LEVEL:
701 1.21 matt if (type != sio_intr[i].intr_sharetype)
702 1.21 matt continue;
703 1.21 matt /*
704 1.21 matt * if the irq is shareable, count the number of other
705 1.21 matt * handlers, and if it's smaller than the last irq like
706 1.21 matt * this, remember it
707 1.21 matt *
708 1.21 matt * XXX We should probably also consider the
709 1.21 matt * interrupt level and stick IPL_TTY with other
710 1.21 matt * IPL_TTY, etc.
711 1.21 matt */
712 1.21 matt for (p = &TAILQ_FIRST(&sio_intr[i].intr_q), tmp = 0;
713 1.21 matt (q = *p) != NULL; p = &TAILQ_NEXT(q, ih_q), tmp++)
714 1.21 matt ;
715 1.21 matt if ((bestirq == -1) || (count > tmp)) {
716 1.21 matt bestirq = i;
717 1.21 matt count = tmp;
718 1.21 matt }
719 1.21 matt break;
720 1.21 matt
721 1.21 matt case IST_PULSE:
722 1.21 matt /* this just isn't shareable */
723 1.21 matt continue;
724 1.21 matt }
725 1.21 matt }
726 1.21 matt
727 1.21 matt if (bestirq == -1)
728 1.21 matt return (1);
729 1.21 matt
730 1.21 matt *irq = bestirq;
731 1.21 matt
732 1.21 matt return (0);
733 1.24 ross }
734 1.24 ross
735 1.24 ross static void
736 1.38 dsl specific_eoi(int irq)
737 1.24 ross {
738 1.24 ross if (irq > 7)
739 1.24 ross bus_space_write_1(sio_iot,
740 1.32 mycroft sio_ioh_icu2, 0, 0x60 | (irq & 0x07)); /* XXX */
741 1.32 mycroft bus_space_write_1(sio_iot, sio_ioh_icu1, 0, 0x60 | (irq > 7 ? 2 : irq));
742 1.1 cgd }
743