2 * Copyright (C) 2008-2010 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
34 #include <sys/kernel.h>
35 #include <sys/pciio.h>
38 #include <dev/ofw/openfirm.h>
39 #include <dev/ofw/ofw_pci.h>
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcireg.h>
44 #include <machine/bus.h>
45 #include <machine/intr_machdep.h>
46 #include <machine/md_var.h>
47 #include <machine/openpicvar.h>
48 #include <machine/pio.h>
49 #include <machine/resource.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53 #include <powerpc/ofw/ofw_pci.h>
62 * IBM CPC9X5 Hypertransport Device interface.
64 static int cpcht_probe(device_t);
65 static int cpcht_attach(device_t);
67 static void cpcht_configure_htbridge(device_t, phandle_t);
72 static u_int32_t cpcht_read_config(device_t, u_int, u_int, u_int,
74 static void cpcht_write_config(device_t, u_int, u_int, u_int,
75 u_int, u_int32_t, int);
76 static int cpcht_route_interrupt(device_t, device_t, int);
77 static int cpcht_alloc_msi(device_t dev, device_t child,
78 int count, int maxcount, int *irqs);
79 static int cpcht_release_msi(device_t dev, device_t child,
80 int count, int *irqs);
81 static int cpcht_alloc_msix(device_t dev, device_t child,
83 static int cpcht_release_msix(device_t dev, device_t child,
85 static int cpcht_map_msi(device_t dev, device_t child,
86 int irq, uint64_t *addr, uint32_t *data);
91 static device_method_t cpcht_methods[] = {
92 /* Device interface */
93 DEVMETHOD(device_probe, cpcht_probe),
94 DEVMETHOD(device_attach, cpcht_attach),
97 DEVMETHOD(pcib_read_config, cpcht_read_config),
98 DEVMETHOD(pcib_write_config, cpcht_write_config),
99 DEVMETHOD(pcib_route_interrupt, cpcht_route_interrupt),
100 DEVMETHOD(pcib_alloc_msi, cpcht_alloc_msi),
101 DEVMETHOD(pcib_release_msi, cpcht_release_msi),
102 DEVMETHOD(pcib_alloc_msix, cpcht_alloc_msix),
103 DEVMETHOD(pcib_release_msix, cpcht_release_msix),
104 DEVMETHOD(pcib_map_msi, cpcht_map_msi),
111 IRQ_NONE, IRQ_HT, IRQ_MSI, IRQ_INTERNAL
117 vm_offset_t apple_eoi;
122 static struct cpcht_irq *cpcht_irqmap = NULL;
123 uint32_t cpcht_msipic = 0;
126 struct ofw_pci_softc pci_sc;
128 uint64_t sc_populated_slots;
130 struct cpcht_irq htirq_map[128];
131 struct mtx htirq_mtx;
134 static devclass_t cpcht_devclass;
135 DEFINE_CLASS_1(pcib, cpcht_driver, cpcht_methods, sizeof(struct cpcht_softc),
137 DRIVER_MODULE(cpcht, nexus, cpcht_driver, cpcht_devclass, 0, 0);
139 #define CPCHT_IOPORT_BASE 0xf4000000UL /* Hardwired */
140 #define CPCHT_IOPORT_SIZE 0x00400000UL
142 #define HTAPIC_REQUEST_EOI 0x20
143 #define HTAPIC_TRIGGER_LEVEL 0x02
144 #define HTAPIC_MASK 0x01
147 cpcht_probe(device_t dev)
149 const char *type, *compatible;
151 type = ofw_bus_get_type(dev);
152 compatible = ofw_bus_get_compat(dev);
154 if (type == NULL || compatible == NULL)
157 if (strcmp(type, "ht") != 0)
160 if (strcmp(compatible, "u3-ht") != 0)
163 device_set_desc(dev, "IBM CPC9X5 HyperTransport Tunnel");
168 cpcht_attach(device_t dev)
170 struct cpcht_softc *sc;
171 phandle_t node, child;
175 node = ofw_bus_get_node(dev);
176 sc = device_get_softc(dev);
178 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 12)
181 if (OF_getproplen(node, "ranges") <= 0)
182 sc->pci_sc.sc_quirks = OFW_PCI_QUIRK_RANGES_ON_CHILDREN;
183 sc->sc_populated_slots = 0;
184 sc->sc_data = (vm_offset_t)pmap_mapdev(reg[1], reg[2]);
187 * Set up the resource manager and the HT->MPIC mapping. For cpcht,
188 * the ranges are properties of the child bridges, and this is also
189 * where we get the HT interrupts properties.
193 /* I/O port mappings are usually not in the device tree */
194 rman_manage_region(&sc->pci_sc.sc_io_rman, 0, CPCHT_IOPORT_SIZE - 1);
197 bzero(sc->htirq_map, sizeof(sc->htirq_map));
198 mtx_init(&sc->htirq_mtx, "cpcht irq", NULL, MTX_DEF);
199 for (i = 0; i < 8; i++)
200 sc->htirq_map[i].irq_type = IRQ_INTERNAL;
201 for (child = OF_child(node); child != 0; child = OF_peer(child))
202 cpcht_configure_htbridge(dev, child);
204 /* Now make the mapping table available to the MPIC */
205 cpcht_irqmap = sc->htirq_map;
207 return (ofw_pci_attach(dev));
211 cpcht_configure_htbridge(device_t dev, phandle_t child)
213 struct cpcht_softc *sc;
214 struct ofw_pci_register pcir;
220 sc = device_get_softc(dev);
221 if (OF_getprop(child, "reg", &pcir, sizeof(pcir)) == -1)
224 b = OFW_PCI_PHYS_HI_BUS(pcir.phys_hi);
225 s = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
226 f = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
229 * Mark this slot is populated. The remote south bridge does
230 * not like us talking to unpopulated slots on the root bus.
232 sc->sc_populated_slots |= (1 << s);
235 * Next build up any HT->MPIC mappings for this sub-bus. One would
236 * naively hope that enabling, disabling, and EOIing interrupts would
237 * cause the appropriate HT bus transactions to that effect. This is
240 * Instead, we have to muck about on the HT peer's root PCI bridges,
241 * figure out what interrupts they send, enable them, and cache
242 * the location of their WaitForEOI registers so that we can
246 /* All the devices we are interested in have caps */
247 if (!(PCIB_READ_CONFIG(dev, b, s, f, PCIR_STATUS, 2)
248 & PCIM_STATUS_CAPPRESENT))
251 nextptr = PCIB_READ_CONFIG(dev, b, s, f, PCIR_CAP_PTR, 1);
252 while (nextptr != 0) {
254 nextptr = PCIB_READ_CONFIG(dev, b, s, f,
255 ptr + PCICAP_NEXTPTR, 1);
257 /* Find the HT IRQ capabilities */
258 if (PCIB_READ_CONFIG(dev, b, s, f,
259 ptr + PCICAP_ID, 1) != PCIY_HT)
262 val = PCIB_READ_CONFIG(dev, b, s, f, ptr + PCIR_HT_COMMAND, 2);
263 if ((val & PCIM_HTCMD_CAP_MASK) != PCIM_HTCAP_INTERRUPT)
266 /* Ask for the IRQ count */
267 PCIB_WRITE_CONFIG(dev, b, s, f, ptr + PCIR_HT_COMMAND, 0x1, 1);
268 nirq = PCIB_READ_CONFIG(dev, b, s, f, ptr + 4, 4);
269 nirq = ((nirq >> 16) & 0xff) + 1;
271 device_printf(dev, "%d HT IRQs on device %d.%d\n", nirq, s, f);
273 for (i = 0; i < nirq; i++) {
274 PCIB_WRITE_CONFIG(dev, b, s, f,
275 ptr + PCIR_HT_COMMAND, 0x10 + (i << 1), 1);
276 irq = PCIB_READ_CONFIG(dev, b, s, f, ptr + 4, 4);
279 * Mask this interrupt for now.
281 PCIB_WRITE_CONFIG(dev, b, s, f, ptr + 4,
282 irq | HTAPIC_MASK, 4);
283 irq = (irq >> 16) & 0xff;
285 sc->htirq_map[irq].irq_type = IRQ_HT;
286 sc->htirq_map[irq].ht_source = i;
287 sc->htirq_map[irq].ht_base = sc->sc_data +
288 (((((s & 0x1f) << 3) | (f & 0x07)) << 8) | (ptr));
290 PCIB_WRITE_CONFIG(dev, b, s, f,
291 ptr + PCIR_HT_COMMAND, 0x11 + (i << 1), 1);
292 sc->htirq_map[irq].eoi_data =
293 PCIB_READ_CONFIG(dev, b, s, f, ptr + 4, 4) |
297 * Apple uses a non-compliant IO/APIC that differs
298 * in how we signal EOIs. Check if this device was
299 * made by Apple, and act accordingly.
301 vend = PCIB_READ_CONFIG(dev, b, s, f,
303 if ((vend & 0xffff) == 0x106b)
304 sc->htirq_map[irq].apple_eoi =
305 (sc->htirq_map[irq].ht_base - ptr) + 0x60;
311 cpcht_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
314 struct cpcht_softc *sc;
317 sc = device_get_softc(dev);
318 caoff = sc->sc_data +
319 (((((slot & 0x1f) << 3) | (func & 0x07)) << 8) | reg);
321 if (bus == 0 && (!(sc->sc_populated_slots & (1 << slot)) || func > 0))
325 caoff += 0x01000000UL + (bus << 16);
329 return (in8rb(caoff));
332 return (in16rb(caoff));
335 return (in32rb(caoff));
343 cpcht_write_config(device_t dev, u_int bus, u_int slot, u_int func,
344 u_int reg, u_int32_t val, int width)
346 struct cpcht_softc *sc;
349 sc = device_get_softc(dev);
350 caoff = sc->sc_data +
351 (((((slot & 0x1f) << 3) | (func & 0x07)) << 8) | reg);
353 if (bus == 0 && (!(sc->sc_populated_slots & (1 << slot)) || func > 0))
357 caoff += 0x01000000UL + (bus << 16);
373 cpcht_route_interrupt(device_t bus, device_t dev, int pin)
379 cpcht_alloc_msi(device_t dev, device_t child, int count, int maxcount,
382 struct cpcht_softc *sc;
385 sc = device_get_softc(dev);
388 /* Bail if no MSI PIC yet */
389 if (cpcht_msipic == 0)
392 mtx_lock(&sc->htirq_mtx);
393 for (i = 8; i < 124 - count; i++) {
394 for (j = 0; j < count; j++) {
395 if (sc->htirq_map[i+j].irq_type != IRQ_NONE)
401 i += j; /* We know there isn't a large enough run */
405 mtx_unlock(&sc->htirq_mtx);
409 for (j = 0; j < count; j++) {
410 irqs[j] = MAP_IRQ(cpcht_msipic, i+j);
411 sc->htirq_map[i+j].irq_type = IRQ_MSI;
413 mtx_unlock(&sc->htirq_mtx);
419 cpcht_release_msi(device_t dev, device_t child, int count, int *irqs)
421 struct cpcht_softc *sc;
424 sc = device_get_softc(dev);
426 mtx_lock(&sc->htirq_mtx);
427 for (i = 0; i < count; i++)
428 sc->htirq_map[irqs[i] & 0xff].irq_type = IRQ_NONE;
429 mtx_unlock(&sc->htirq_mtx);
435 cpcht_alloc_msix(device_t dev, device_t child, int *irq)
437 struct cpcht_softc *sc;
440 sc = device_get_softc(dev);
442 /* Bail if no MSI PIC yet */
443 if (cpcht_msipic == 0)
446 mtx_lock(&sc->htirq_mtx);
447 for (i = 8; i < 124; i++) {
448 if (sc->htirq_map[i].irq_type == IRQ_NONE) {
449 sc->htirq_map[i].irq_type = IRQ_MSI;
450 *irq = MAP_IRQ(cpcht_msipic, i);
452 mtx_unlock(&sc->htirq_mtx);
456 mtx_unlock(&sc->htirq_mtx);
462 cpcht_release_msix(device_t dev, device_t child, int irq)
464 struct cpcht_softc *sc;
466 sc = device_get_softc(dev);
468 mtx_lock(&sc->htirq_mtx);
469 sc->htirq_map[irq & 0xff].irq_type = IRQ_NONE;
470 mtx_unlock(&sc->htirq_mtx);
476 cpcht_map_msi(device_t dev, device_t child, int irq, uint64_t *addr,
480 struct pci_devinfo *dinfo;
481 struct pcicfg_ht *ht = NULL;
483 for (pcib = child; pcib != dev; pcib =
484 device_get_parent(device_get_parent(pcib))) {
485 dinfo = device_get_ivars(pcib);
495 *addr = ht->ht_msiaddr;
502 * Driver for the integrated MPIC on U3/U4 (CPC925/CPC945)
505 static int openpic_cpcht_probe(device_t);
506 static int openpic_cpcht_attach(device_t);
507 static void openpic_cpcht_config(device_t, u_int irq,
508 enum intr_trigger trig, enum intr_polarity pol);
509 static void openpic_cpcht_enable(device_t, u_int irq, u_int vector);
510 static void openpic_cpcht_unmask(device_t, u_int irq);
511 static void openpic_cpcht_eoi(device_t, u_int irq);
513 static device_method_t openpic_cpcht_methods[] = {
514 /* Device interface */
515 DEVMETHOD(device_probe, openpic_cpcht_probe),
516 DEVMETHOD(device_attach, openpic_cpcht_attach),
519 DEVMETHOD(pic_bind, openpic_bind),
520 DEVMETHOD(pic_config, openpic_cpcht_config),
521 DEVMETHOD(pic_dispatch, openpic_dispatch),
522 DEVMETHOD(pic_enable, openpic_cpcht_enable),
523 DEVMETHOD(pic_eoi, openpic_cpcht_eoi),
524 DEVMETHOD(pic_ipi, openpic_ipi),
525 DEVMETHOD(pic_mask, openpic_mask),
526 DEVMETHOD(pic_unmask, openpic_cpcht_unmask),
531 struct openpic_cpcht_softc {
532 struct openpic_softc sc_openpic;
534 struct mtx sc_ht_mtx;
537 static driver_t openpic_cpcht_driver = {
539 openpic_cpcht_methods,
540 sizeof(struct openpic_cpcht_softc),
543 DRIVER_MODULE(openpic, unin, openpic_cpcht_driver, openpic_devclass, 0, 0);
546 openpic_cpcht_probe(device_t dev)
548 const char *type = ofw_bus_get_type(dev);
550 if (strcmp(type, "open-pic") != 0)
553 device_set_desc(dev, OPENPIC_DEVSTR);
558 openpic_cpcht_attach(device_t dev)
560 struct openpic_cpcht_softc *sc;
564 node = ofw_bus_get_node(dev);
565 err = openpic_common_attach(dev, node);
570 * The HT APIC stuff is not thread-safe, so we need a mutex to
573 sc = device_get_softc(dev);
574 mtx_init(&sc->sc_ht_mtx, "htpic", NULL, MTX_SPIN);
577 * Interrupts 0-3 are internally sourced and are level triggered
578 * active low. Interrupts 4-123 are connected to a pulse generator
579 * and should be programmed as edge triggered low-to-high.
581 * IBM CPC945 Manual, Section 9.3.
584 for (irq = 0; irq < 4; irq++)
585 openpic_config(dev, irq, INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
586 for (irq = 4; irq < 124; irq++)
587 openpic_config(dev, irq, INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
590 * Use this PIC for MSI only if it is the root PIC. This may not
591 * be necessary, but Linux does it, and I cannot find any U3 machines
592 * with MSI devices to test.
601 openpic_cpcht_config(device_t dev, u_int irq, enum intr_trigger trig,
602 enum intr_polarity pol)
604 struct openpic_cpcht_softc *sc;
608 * The interrupt settings for the MPIC are completely determined
609 * by the internal wiring in the northbridge. Real changes to these
610 * settings need to be negotiated with the remote IO-APIC on the HT
614 sc = device_get_softc(dev);
616 if (cpcht_irqmap != NULL && irq < 128 &&
617 cpcht_irqmap[irq].ht_base > 0 && !cpcht_irqmap[irq].edge) {
618 mtx_lock_spin(&sc->sc_ht_mtx);
620 /* Program the data port */
621 out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
622 0x10 + (cpcht_irqmap[irq].ht_source << 1));
624 /* Grab the IRQ config register */
625 ht_irq = in32rb(cpcht_irqmap[irq].ht_base + 4);
627 /* Mask the IRQ while we fiddle settings */
628 out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq | HTAPIC_MASK);
630 /* Program the interrupt sense */
631 ht_irq &= ~(HTAPIC_TRIGGER_LEVEL | HTAPIC_REQUEST_EOI);
632 if (trig == INTR_TRIGGER_EDGE) {
633 cpcht_irqmap[irq].edge = 1;
635 cpcht_irqmap[irq].edge = 0;
636 ht_irq |= HTAPIC_TRIGGER_LEVEL | HTAPIC_REQUEST_EOI;
638 out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq);
640 mtx_unlock_spin(&sc->sc_ht_mtx);
645 openpic_cpcht_enable(device_t dev, u_int irq, u_int vec)
647 struct openpic_cpcht_softc *sc;
650 openpic_enable(dev, irq, vec);
652 sc = device_get_softc(dev);
654 if (cpcht_irqmap != NULL && irq < 128 &&
655 cpcht_irqmap[irq].ht_base > 0) {
656 mtx_lock_spin(&sc->sc_ht_mtx);
658 /* Program the data port */
659 out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
660 0x10 + (cpcht_irqmap[irq].ht_source << 1));
662 /* Unmask the interrupt */
663 ht_irq = in32rb(cpcht_irqmap[irq].ht_base + 4);
664 ht_irq &= ~HTAPIC_MASK;
665 out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq);
667 mtx_unlock_spin(&sc->sc_ht_mtx);
670 openpic_cpcht_eoi(dev, irq);
674 openpic_cpcht_unmask(device_t dev, u_int irq)
676 struct openpic_cpcht_softc *sc;
679 openpic_unmask(dev, irq);
681 sc = device_get_softc(dev);
683 if (cpcht_irqmap != NULL && irq < 128 &&
684 cpcht_irqmap[irq].ht_base > 0) {
685 mtx_lock_spin(&sc->sc_ht_mtx);
687 /* Program the data port */
688 out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
689 0x10 + (cpcht_irqmap[irq].ht_source << 1));
691 /* Unmask the interrupt */
692 ht_irq = in32rb(cpcht_irqmap[irq].ht_base + 4);
693 ht_irq &= ~HTAPIC_MASK;
694 out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq);
696 mtx_unlock_spin(&sc->sc_ht_mtx);
699 openpic_cpcht_eoi(dev, irq);
703 openpic_cpcht_eoi(device_t dev, u_int irq)
705 struct openpic_cpcht_softc *sc;
711 sc = device_get_softc(dev);
713 if (cpcht_irqmap != NULL && irq < 128 &&
714 cpcht_irqmap[irq].ht_base > 0 && !cpcht_irqmap[irq].edge) {
715 /* If this is an HT IRQ, acknowledge it at the remote APIC */
717 if (cpcht_irqmap[irq].apple_eoi) {
718 off = (cpcht_irqmap[irq].ht_source >> 3) & ~3;
719 mask = 1 << (cpcht_irqmap[irq].ht_source & 0x1f);
720 out32rb(cpcht_irqmap[irq].apple_eoi + off, mask);
722 mtx_lock_spin(&sc->sc_ht_mtx);
724 out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
725 0x11 + (cpcht_irqmap[irq].ht_source << 1));
726 out32rb(cpcht_irqmap[irq].ht_base + 4,
727 cpcht_irqmap[irq].eoi_data);
729 mtx_unlock_spin(&sc->sc_ht_mtx);
733 openpic_eoi(dev, irq);