2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2008-2010 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
36 #include <sys/kernel.h>
38 #include <sys/mutex.h>
39 #include <sys/pciio.h>
42 #include <dev/ofw/openfirm.h>
43 #include <dev/ofw/ofw_pci.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
48 #include <machine/bus.h>
49 #include <machine/intr_machdep.h>
50 #include <machine/md_var.h>
51 #include <machine/openpicreg.h>
52 #include <machine/openpicvar.h>
53 #include <machine/pio.h>
54 #include <machine/resource.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
58 #include <dev/ofw/ofwpci.h>
64 #include <dev/pci/pcib_private.h>
68 * IBM CPC9X5 Hypertransport Device interface.
70 static int cpcht_probe(device_t);
71 static int cpcht_attach(device_t);
73 static void cpcht_configure_htbridge(device_t, phandle_t);
78 static u_int32_t cpcht_read_config(device_t, u_int, u_int, u_int,
80 static void cpcht_write_config(device_t, u_int, u_int, u_int,
81 u_int, u_int32_t, int);
82 static int cpcht_route_interrupt(device_t, device_t, int);
83 static int cpcht_alloc_msi(device_t dev, device_t child,
84 int count, int maxcount, int *irqs);
85 static int cpcht_release_msi(device_t dev, device_t child,
86 int count, int *irqs);
87 static int cpcht_alloc_msix(device_t dev, device_t child,
89 static int cpcht_release_msix(device_t dev, device_t child,
91 static int cpcht_map_msi(device_t dev, device_t child,
92 int irq, uint64_t *addr, uint32_t *data);
97 static device_method_t cpcht_methods[] = {
98 /* Device interface */
99 DEVMETHOD(device_probe, cpcht_probe),
100 DEVMETHOD(device_attach, cpcht_attach),
103 DEVMETHOD(pcib_read_config, cpcht_read_config),
104 DEVMETHOD(pcib_write_config, cpcht_write_config),
105 DEVMETHOD(pcib_route_interrupt, cpcht_route_interrupt),
106 DEVMETHOD(pcib_alloc_msi, cpcht_alloc_msi),
107 DEVMETHOD(pcib_release_msi, cpcht_release_msi),
108 DEVMETHOD(pcib_alloc_msix, cpcht_alloc_msix),
109 DEVMETHOD(pcib_release_msix, cpcht_release_msix),
110 DEVMETHOD(pcib_map_msi, cpcht_map_msi),
111 DEVMETHOD(pcib_request_feature, pcib_request_feature_allow),
118 IRQ_NONE, IRQ_HT, IRQ_MSI, IRQ_INTERNAL
124 vm_offset_t apple_eoi;
129 static struct cpcht_irq *cpcht_irqmap = NULL;
130 uint32_t cpcht_msipic = 0;
133 struct ofw_pci_softc pci_sc;
135 uint64_t sc_populated_slots;
137 struct cpcht_irq htirq_map[128];
138 struct mtx htirq_mtx;
141 DEFINE_CLASS_1(pcib, cpcht_driver, cpcht_methods, sizeof(struct cpcht_softc),
143 EARLY_DRIVER_MODULE(cpcht, ofwbus, cpcht_driver, 0, 0, BUS_PASS_BUS);
145 #define CPCHT_IOPORT_BASE 0xf4000000UL /* Hardwired */
146 #define CPCHT_IOPORT_SIZE 0x00400000UL
148 #define HTAPIC_REQUEST_EOI 0x20
149 #define HTAPIC_TRIGGER_LEVEL 0x02
150 #define HTAPIC_MASK 0x01
153 cpcht_probe(device_t dev)
155 const char *type, *compatible;
157 type = ofw_bus_get_type(dev);
158 compatible = ofw_bus_get_compat(dev);
160 if (type == NULL || compatible == NULL)
163 if (strcmp(type, "ht") != 0)
166 if (strcmp(compatible, "u3-ht") != 0)
169 device_set_desc(dev, "IBM CPC9X5 HyperTransport Tunnel");
174 cpcht_attach(device_t dev)
176 struct cpcht_softc *sc;
177 phandle_t node, child;
181 node = ofw_bus_get_node(dev);
182 sc = device_get_softc(dev);
184 if (OF_getencprop(node, "reg", reg, sizeof(reg)) < 12)
187 if (OF_getproplen(node, "ranges") <= 0)
188 sc->pci_sc.sc_quirks = OFW_PCI_QUIRK_RANGES_ON_CHILDREN;
189 sc->sc_populated_slots = 0;
190 sc->sc_data = (vm_offset_t)pmap_mapdev(reg[1], reg[2]);
193 * Set up the resource manager and the HT->MPIC mapping. For cpcht,
194 * the ranges are properties of the child bridges, and this is also
195 * where we get the HT interrupts properties.
199 /* I/O port mappings are usually not in the device tree */
200 rman_manage_region(&sc->pci_sc.sc_io_rman, 0, CPCHT_IOPORT_SIZE - 1);
203 bzero(sc->htirq_map, sizeof(sc->htirq_map));
204 mtx_init(&sc->htirq_mtx, "cpcht irq", NULL, MTX_DEF);
205 for (i = 0; i < 8; i++)
206 sc->htirq_map[i].irq_type = IRQ_INTERNAL;
207 for (child = OF_child(node); child != 0; child = OF_peer(child))
208 cpcht_configure_htbridge(dev, child);
210 /* Now make the mapping table available to the MPIC */
211 cpcht_irqmap = sc->htirq_map;
213 return (ofw_pcib_attach(dev));
217 cpcht_configure_htbridge(device_t dev, phandle_t child)
219 struct cpcht_softc *sc;
220 struct ofw_pci_register pcir;
226 sc = device_get_softc(dev);
227 if (OF_getencprop(child, "reg", (pcell_t *)&pcir, sizeof(pcir)) == -1)
230 b = OFW_PCI_PHYS_HI_BUS(pcir.phys_hi);
231 s = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
232 f = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
235 * Mark this slot is populated. The remote south bridge does
236 * not like us talking to unpopulated slots on the root bus.
238 sc->sc_populated_slots |= (1 << s);
241 * Next build up any HT->MPIC mappings for this sub-bus. One would
242 * naively hope that enabling, disabling, and EOIing interrupts would
243 * cause the appropriate HT bus transactions to that effect. This is
246 * Instead, we have to muck about on the HT peer's root PCI bridges,
247 * figure out what interrupts they send, enable them, and cache
248 * the location of their WaitForEOI registers so that we can
252 /* All the devices we are interested in have caps */
253 if (!(PCIB_READ_CONFIG(dev, b, s, f, PCIR_STATUS, 2)
254 & PCIM_STATUS_CAPPRESENT))
257 nextptr = PCIB_READ_CONFIG(dev, b, s, f, PCIR_CAP_PTR, 1);
258 while (nextptr != 0) {
260 nextptr = PCIB_READ_CONFIG(dev, b, s, f,
261 ptr + PCICAP_NEXTPTR, 1);
263 /* Find the HT IRQ capabilities */
264 if (PCIB_READ_CONFIG(dev, b, s, f,
265 ptr + PCICAP_ID, 1) != PCIY_HT)
268 val = PCIB_READ_CONFIG(dev, b, s, f, ptr + PCIR_HT_COMMAND, 2);
269 if ((val & PCIM_HTCMD_CAP_MASK) != PCIM_HTCAP_INTERRUPT)
272 /* Ask for the IRQ count */
273 PCIB_WRITE_CONFIG(dev, b, s, f, ptr + PCIR_HT_COMMAND, 0x1, 1);
274 nirq = PCIB_READ_CONFIG(dev, b, s, f, ptr + 4, 4);
275 nirq = ((nirq >> 16) & 0xff) + 1;
277 device_printf(dev, "%d HT IRQs on device %d.%d\n", nirq, s, f);
279 for (i = 0; i < nirq; i++) {
280 PCIB_WRITE_CONFIG(dev, b, s, f,
281 ptr + PCIR_HT_COMMAND, 0x10 + (i << 1), 1);
282 irq = PCIB_READ_CONFIG(dev, b, s, f, ptr + 4, 4);
285 * Mask this interrupt for now.
287 PCIB_WRITE_CONFIG(dev, b, s, f, ptr + 4,
288 irq | HTAPIC_MASK, 4);
289 irq = (irq >> 16) & 0xff;
291 sc->htirq_map[irq].irq_type = IRQ_HT;
292 sc->htirq_map[irq].ht_source = i;
293 sc->htirq_map[irq].ht_base = sc->sc_data +
294 (((((s & 0x1f) << 3) | (f & 0x07)) << 8) | (ptr));
296 PCIB_WRITE_CONFIG(dev, b, s, f,
297 ptr + PCIR_HT_COMMAND, 0x11 + (i << 1), 1);
298 sc->htirq_map[irq].eoi_data =
299 PCIB_READ_CONFIG(dev, b, s, f, ptr + 4, 4) |
303 * Apple uses a non-compliant IO/APIC that differs
304 * in how we signal EOIs. Check if this device was
305 * made by Apple, and act accordingly.
307 vend = PCIB_READ_CONFIG(dev, b, s, f,
309 if ((vend & 0xffff) == 0x106b)
310 sc->htirq_map[irq].apple_eoi =
311 (sc->htirq_map[irq].ht_base - ptr) + 0x60;
317 cpcht_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
320 struct cpcht_softc *sc;
323 sc = device_get_softc(dev);
324 caoff = sc->sc_data +
325 (((((slot & 0x1f) << 3) | (func & 0x07)) << 8) | reg);
327 if (bus == 0 && (!(sc->sc_populated_slots & (1 << slot)) || func > 0))
331 caoff += 0x01000000UL + (bus << 16);
335 return (in8rb(caoff));
338 return (in16rb(caoff));
341 return (in32rb(caoff));
349 cpcht_write_config(device_t dev, u_int bus, u_int slot, u_int func,
350 u_int reg, u_int32_t val, int width)
352 struct cpcht_softc *sc;
355 sc = device_get_softc(dev);
356 caoff = sc->sc_data +
357 (((((slot & 0x1f) << 3) | (func & 0x07)) << 8) | reg);
359 if (bus == 0 && (!(sc->sc_populated_slots & (1 << slot)) || func > 0))
363 caoff += 0x01000000UL + (bus << 16);
379 cpcht_route_interrupt(device_t bus, device_t dev, int pin)
385 cpcht_alloc_msi(device_t dev, device_t child, int count, int maxcount,
388 struct cpcht_softc *sc;
391 sc = device_get_softc(dev);
394 /* Bail if no MSI PIC yet */
395 if (cpcht_msipic == 0)
398 mtx_lock(&sc->htirq_mtx);
399 for (i = 8; i < 124 - count; i++) {
400 for (j = 0; j < count; j++) {
401 if (sc->htirq_map[i+j].irq_type != IRQ_NONE)
407 i += j; /* We know there isn't a large enough run */
411 mtx_unlock(&sc->htirq_mtx);
415 for (j = 0; j < count; j++) {
416 irqs[j] = MAP_IRQ(cpcht_msipic, i+j);
417 sc->htirq_map[i+j].irq_type = IRQ_MSI;
419 mtx_unlock(&sc->htirq_mtx);
425 cpcht_release_msi(device_t dev, device_t child, int count, int *irqs)
427 struct cpcht_softc *sc;
430 sc = device_get_softc(dev);
432 mtx_lock(&sc->htirq_mtx);
433 for (i = 0; i < count; i++)
434 sc->htirq_map[irqs[i] & 0xff].irq_type = IRQ_NONE;
435 mtx_unlock(&sc->htirq_mtx);
441 cpcht_alloc_msix(device_t dev, device_t child, int *irq)
443 struct cpcht_softc *sc;
446 sc = device_get_softc(dev);
448 /* Bail if no MSI PIC yet */
449 if (cpcht_msipic == 0)
452 mtx_lock(&sc->htirq_mtx);
453 for (i = 8; i < 124; i++) {
454 if (sc->htirq_map[i].irq_type == IRQ_NONE) {
455 sc->htirq_map[i].irq_type = IRQ_MSI;
456 *irq = MAP_IRQ(cpcht_msipic, i);
458 mtx_unlock(&sc->htirq_mtx);
462 mtx_unlock(&sc->htirq_mtx);
468 cpcht_release_msix(device_t dev, device_t child, int irq)
470 struct cpcht_softc *sc;
472 sc = device_get_softc(dev);
474 mtx_lock(&sc->htirq_mtx);
475 sc->htirq_map[irq & 0xff].irq_type = IRQ_NONE;
476 mtx_unlock(&sc->htirq_mtx);
482 cpcht_map_msi(device_t dev, device_t child, int irq, uint64_t *addr,
486 struct pci_devinfo *dinfo;
487 struct pcicfg_ht *ht = NULL;
489 for (pcib = child; pcib != dev; pcib =
490 device_get_parent(device_get_parent(pcib))) {
491 dinfo = device_get_ivars(pcib);
501 *addr = ht->ht_msiaddr;
508 * Driver for the integrated MPIC on U3/U4 (CPC925/CPC945)
511 static int openpic_cpcht_probe(device_t);
512 static int openpic_cpcht_attach(device_t);
513 static void openpic_cpcht_config(device_t, u_int irq,
514 enum intr_trigger trig, enum intr_polarity pol);
515 static void openpic_cpcht_enable(device_t, u_int irq, u_int vector,
517 static void openpic_cpcht_unmask(device_t, u_int irq, void *priv);
518 static void openpic_cpcht_eoi(device_t, u_int irq, void *priv);
520 static device_method_t openpic_cpcht_methods[] = {
521 /* Device interface */
522 DEVMETHOD(device_probe, openpic_cpcht_probe),
523 DEVMETHOD(device_attach, openpic_cpcht_attach),
526 DEVMETHOD(pic_bind, openpic_bind),
527 DEVMETHOD(pic_config, openpic_cpcht_config),
528 DEVMETHOD(pic_dispatch, openpic_dispatch),
529 DEVMETHOD(pic_enable, openpic_cpcht_enable),
530 DEVMETHOD(pic_eoi, openpic_cpcht_eoi),
531 DEVMETHOD(pic_ipi, openpic_ipi),
532 DEVMETHOD(pic_mask, openpic_mask),
533 DEVMETHOD(pic_unmask, openpic_cpcht_unmask),
538 struct openpic_cpcht_softc {
539 struct openpic_softc sc_openpic;
541 struct mtx sc_ht_mtx;
544 static driver_t openpic_cpcht_driver = {
546 openpic_cpcht_methods,
547 sizeof(struct openpic_cpcht_softc),
550 EARLY_DRIVER_MODULE(openpic, unin, openpic_cpcht_driver, 0, 0,
554 openpic_cpcht_probe(device_t dev)
556 const char *type = ofw_bus_get_type(dev);
558 if (strcmp(type, "open-pic") != 0)
561 device_set_desc(dev, OPENPIC_DEVSTR);
566 openpic_cpcht_attach(device_t dev)
568 struct openpic_cpcht_softc *sc;
572 node = ofw_bus_get_node(dev);
573 err = openpic_common_attach(dev, node);
578 * The HT APIC stuff is not thread-safe, so we need a mutex to
581 sc = device_get_softc(dev);
582 mtx_init(&sc->sc_ht_mtx, "htpic", NULL, MTX_SPIN);
585 * Interrupts 0-3 are internally sourced and are level triggered
586 * active low. Interrupts 4-123 are connected to a pulse generator
587 * and should be programmed as edge triggered low-to-high.
589 * IBM CPC945 Manual, Section 9.3.
592 for (irq = 0; irq < 4; irq++)
593 openpic_config(dev, irq, INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
594 for (irq = 4; irq < 124; irq++)
595 openpic_config(dev, irq, INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
598 * Use this PIC for MSI only if it is the root PIC. This may not
599 * be necessary, but Linux does it, and I cannot find any U3 machines
600 * with MSI devices to test.
609 openpic_cpcht_config(device_t dev, u_int irq, enum intr_trigger trig,
610 enum intr_polarity pol)
612 struct openpic_cpcht_softc *sc;
616 * The interrupt settings for the MPIC are completely determined
617 * by the internal wiring in the northbridge. Real changes to these
618 * settings need to be negotiated with the remote IO-APIC on the HT
622 sc = device_get_softc(dev);
624 if (cpcht_irqmap != NULL && irq < 128 &&
625 cpcht_irqmap[irq].ht_base > 0 && !cpcht_irqmap[irq].edge) {
626 mtx_lock_spin(&sc->sc_ht_mtx);
628 /* Program the data port */
629 out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
630 0x10 + (cpcht_irqmap[irq].ht_source << 1));
632 /* Grab the IRQ config register */
633 ht_irq = in32rb(cpcht_irqmap[irq].ht_base + 4);
635 /* Mask the IRQ while we fiddle settings */
636 out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq | HTAPIC_MASK);
638 /* Program the interrupt sense */
639 ht_irq &= ~(HTAPIC_TRIGGER_LEVEL | HTAPIC_REQUEST_EOI);
640 if (trig == INTR_TRIGGER_EDGE) {
641 cpcht_irqmap[irq].edge = 1;
643 cpcht_irqmap[irq].edge = 0;
644 ht_irq |= HTAPIC_TRIGGER_LEVEL | HTAPIC_REQUEST_EOI;
646 out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq);
648 mtx_unlock_spin(&sc->sc_ht_mtx);
653 openpic_cpcht_enable(device_t dev, u_int irq, u_int vec, void **priv)
655 struct openpic_cpcht_softc *sc;
658 openpic_enable(dev, irq, vec, priv);
660 sc = device_get_softc(dev);
662 if (cpcht_irqmap != NULL && irq < 128 &&
663 cpcht_irqmap[irq].ht_base > 0) {
664 mtx_lock_spin(&sc->sc_ht_mtx);
666 /* Program the data port */
667 out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
668 0x10 + (cpcht_irqmap[irq].ht_source << 1));
670 /* Unmask the interrupt */
671 ht_irq = in32rb(cpcht_irqmap[irq].ht_base + 4);
672 ht_irq &= ~HTAPIC_MASK;
673 out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq);
675 mtx_unlock_spin(&sc->sc_ht_mtx);
678 openpic_cpcht_eoi(dev, irq, *priv);
682 openpic_cpcht_unmask(device_t dev, u_int irq, void *priv)
684 struct openpic_cpcht_softc *sc;
687 openpic_unmask(dev, irq, priv);
689 sc = device_get_softc(dev);
691 if (cpcht_irqmap != NULL && irq < 128 &&
692 cpcht_irqmap[irq].ht_base > 0) {
693 mtx_lock_spin(&sc->sc_ht_mtx);
695 /* Program the data port */
696 out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
697 0x10 + (cpcht_irqmap[irq].ht_source << 1));
699 /* Unmask the interrupt */
700 ht_irq = in32rb(cpcht_irqmap[irq].ht_base + 4);
701 ht_irq &= ~HTAPIC_MASK;
702 out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq);
704 mtx_unlock_spin(&sc->sc_ht_mtx);
707 openpic_cpcht_eoi(dev, irq, priv);
711 openpic_cpcht_eoi(device_t dev, u_int irq, void *priv)
713 struct openpic_cpcht_softc *sc;
719 sc = device_get_softc(dev);
721 if (cpcht_irqmap != NULL && irq < 128 &&
722 cpcht_irqmap[irq].ht_base > 0 && !cpcht_irqmap[irq].edge) {
723 /* If this is an HT IRQ, acknowledge it at the remote APIC */
725 if (cpcht_irqmap[irq].apple_eoi) {
726 off = (cpcht_irqmap[irq].ht_source >> 3) & ~3;
727 mask = 1 << (cpcht_irqmap[irq].ht_source & 0x1f);
728 out32rb(cpcht_irqmap[irq].apple_eoi + off, mask);
730 mtx_lock_spin(&sc->sc_ht_mtx);
732 out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
733 0x11 + (cpcht_irqmap[irq].ht_source << 1));
734 out32rb(cpcht_irqmap[irq].ht_base + 4,
735 cpcht_irqmap[irq].eoi_data);
737 mtx_unlock_spin(&sc->sc_ht_mtx);
741 openpic_eoi(dev, irq, priv);