11c96bdd1SNathan Whitehorn /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni *
4ca2c1931SNathan Whitehorn * Copyright (C) 2008-2010 Nathan Whitehorn
51c96bdd1SNathan Whitehorn * All rights reserved.
61c96bdd1SNathan Whitehorn *
71c96bdd1SNathan Whitehorn * Redistribution and use in source and binary forms, with or without
81c96bdd1SNathan Whitehorn * modification, are permitted provided that the following conditions
91c96bdd1SNathan Whitehorn * are met:
101c96bdd1SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright
111c96bdd1SNathan Whitehorn * notice, this list of conditions and the following disclaimer.
121c96bdd1SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright
131c96bdd1SNathan Whitehorn * notice, this list of conditions and the following disclaimer in the
141c96bdd1SNathan Whitehorn * documentation and/or other materials provided with the distribution.
151c96bdd1SNathan Whitehorn *
161c96bdd1SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171c96bdd1SNathan Whitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181c96bdd1SNathan Whitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191c96bdd1SNathan Whitehorn * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
201c96bdd1SNathan Whitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
211c96bdd1SNathan Whitehorn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
221c96bdd1SNathan Whitehorn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
231c96bdd1SNathan Whitehorn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
241c96bdd1SNathan Whitehorn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
251c96bdd1SNathan Whitehorn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261c96bdd1SNathan Whitehorn */
271c96bdd1SNathan Whitehorn
281c96bdd1SNathan Whitehorn #include <sys/param.h>
291c96bdd1SNathan Whitehorn #include <sys/systm.h>
301c96bdd1SNathan Whitehorn #include <sys/module.h>
311c96bdd1SNathan Whitehorn #include <sys/bus.h>
321c96bdd1SNathan Whitehorn #include <sys/conf.h>
331c96bdd1SNathan Whitehorn #include <sys/kernel.h>
34e2e050c8SConrad Meyer #include <sys/lock.h>
35e2e050c8SConrad Meyer #include <sys/mutex.h>
368aa8e94dSNathan Whitehorn #include <sys/pciio.h>
378aa8e94dSNathan Whitehorn #include <sys/rman.h>
381c96bdd1SNathan Whitehorn
391c96bdd1SNathan Whitehorn #include <dev/ofw/openfirm.h>
4036e9c2ceSZbigniew Bodek #include <dev/ofw/ofw_pci.h>
411c96bdd1SNathan Whitehorn
421c96bdd1SNathan Whitehorn #include <dev/pci/pcivar.h>
431c96bdd1SNathan Whitehorn #include <dev/pci/pcireg.h>
441c96bdd1SNathan Whitehorn
451c96bdd1SNathan Whitehorn #include <machine/bus.h>
46ca2c1931SNathan Whitehorn #include <machine/intr_machdep.h>
471c96bdd1SNathan Whitehorn #include <machine/md_var.h>
489a2edf01SJustin Hibbits #include <machine/openpicreg.h>
49ca2c1931SNathan Whitehorn #include <machine/openpicvar.h>
501c96bdd1SNathan Whitehorn #include <machine/pio.h>
511c96bdd1SNathan Whitehorn #include <machine/resource.h>
521c96bdd1SNathan Whitehorn
531c96bdd1SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
541c96bdd1SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h>
55c43a8674SZbigniew Bodek #include <dev/ofw/ofwpci.h>
561c96bdd1SNathan Whitehorn
571c96bdd1SNathan Whitehorn #include <vm/vm.h>
581c96bdd1SNathan Whitehorn #include <vm/pmap.h>
591c96bdd1SNathan Whitehorn
601c96bdd1SNathan Whitehorn #include "pcib_if.h"
6155157631SWarner Losh #include <dev/pci/pcib_private.h>
62ca2c1931SNathan Whitehorn #include "pic_if.h"
631c96bdd1SNathan Whitehorn
641c96bdd1SNathan Whitehorn /*
65ca2c1931SNathan Whitehorn * IBM CPC9X5 Hypertransport Device interface.
661c96bdd1SNathan Whitehorn */
671c96bdd1SNathan Whitehorn static int cpcht_probe(device_t);
681c96bdd1SNathan Whitehorn static int cpcht_attach(device_t);
691c96bdd1SNathan Whitehorn
70ca2c1931SNathan Whitehorn static void cpcht_configure_htbridge(device_t, phandle_t);
711c96bdd1SNathan Whitehorn
72ca2c1931SNathan Whitehorn /*
73ca2c1931SNathan Whitehorn * pcib interface.
74ca2c1931SNathan Whitehorn */
75ca2c1931SNathan Whitehorn static u_int32_t cpcht_read_config(device_t, u_int, u_int, u_int,
76ca2c1931SNathan Whitehorn u_int, int);
77ca2c1931SNathan Whitehorn static void cpcht_write_config(device_t, u_int, u_int, u_int,
78ca2c1931SNathan Whitehorn u_int, u_int32_t, int);
799a35e64eSNathan Whitehorn static int cpcht_route_interrupt(device_t, device_t, int);
808aa8e94dSNathan Whitehorn static int cpcht_alloc_msi(device_t dev, device_t child,
818aa8e94dSNathan Whitehorn int count, int maxcount, int *irqs);
828aa8e94dSNathan Whitehorn static int cpcht_release_msi(device_t dev, device_t child,
838aa8e94dSNathan Whitehorn int count, int *irqs);
848aa8e94dSNathan Whitehorn static int cpcht_alloc_msix(device_t dev, device_t child,
858aa8e94dSNathan Whitehorn int *irq);
868aa8e94dSNathan Whitehorn static int cpcht_release_msix(device_t dev, device_t child,
878aa8e94dSNathan Whitehorn int irq);
888aa8e94dSNathan Whitehorn static int cpcht_map_msi(device_t dev, device_t child,
898aa8e94dSNathan Whitehorn int irq, uint64_t *addr, uint32_t *data);
90ca2c1931SNathan Whitehorn
91ca2c1931SNathan Whitehorn /*
92ca2c1931SNathan Whitehorn * Driver methods.
93ca2c1931SNathan Whitehorn */
941c96bdd1SNathan Whitehorn static device_method_t cpcht_methods[] = {
951c96bdd1SNathan Whitehorn /* Device interface */
961c96bdd1SNathan Whitehorn DEVMETHOD(device_probe, cpcht_probe),
971c96bdd1SNathan Whitehorn DEVMETHOD(device_attach, cpcht_attach),
981c96bdd1SNathan Whitehorn
99ca2c1931SNathan Whitehorn /* pcib interface */
100ca2c1931SNathan Whitehorn DEVMETHOD(pcib_read_config, cpcht_read_config),
101ca2c1931SNathan Whitehorn DEVMETHOD(pcib_write_config, cpcht_write_config),
102ca2c1931SNathan Whitehorn DEVMETHOD(pcib_route_interrupt, cpcht_route_interrupt),
1038aa8e94dSNathan Whitehorn DEVMETHOD(pcib_alloc_msi, cpcht_alloc_msi),
1048aa8e94dSNathan Whitehorn DEVMETHOD(pcib_release_msi, cpcht_release_msi),
1058aa8e94dSNathan Whitehorn DEVMETHOD(pcib_alloc_msix, cpcht_alloc_msix),
1068aa8e94dSNathan Whitehorn DEVMETHOD(pcib_release_msix, cpcht_release_msix),
1078aa8e94dSNathan Whitehorn DEVMETHOD(pcib_map_msi, cpcht_map_msi),
10828586889SWarner Losh DEVMETHOD(pcib_request_feature, pcib_request_feature_allow),
1091c96bdd1SNathan Whitehorn
1104b7ec270SMarius Strobl DEVMETHOD_END
1111c96bdd1SNathan Whitehorn };
1121c96bdd1SNathan Whitehorn
113ca2c1931SNathan Whitehorn struct cpcht_irq {
1148aa8e94dSNathan Whitehorn enum {
1158aa8e94dSNathan Whitehorn IRQ_NONE, IRQ_HT, IRQ_MSI, IRQ_INTERNAL
1168aa8e94dSNathan Whitehorn } irq_type;
1178aa8e94dSNathan Whitehorn
118ca2c1931SNathan Whitehorn int ht_source;
119ca2c1931SNathan Whitehorn
120ca2c1931SNathan Whitehorn vm_offset_t ht_base;
121ca2c1931SNathan Whitehorn vm_offset_t apple_eoi;
122ca2c1931SNathan Whitehorn uint32_t eoi_data;
123ca2c1931SNathan Whitehorn int edge;
124ca2c1931SNathan Whitehorn };
125ca2c1931SNathan Whitehorn
126ca2c1931SNathan Whitehorn static struct cpcht_irq *cpcht_irqmap = NULL;
1278aa8e94dSNathan Whitehorn uint32_t cpcht_msipic = 0;
128ca2c1931SNathan Whitehorn
129ca2c1931SNathan Whitehorn struct cpcht_softc {
1309a35e64eSNathan Whitehorn struct ofw_pci_softc pci_sc;
131ca2c1931SNathan Whitehorn vm_offset_t sc_data;
132ca2c1931SNathan Whitehorn uint64_t sc_populated_slots;
133ca2c1931SNathan Whitehorn
134ca2c1931SNathan Whitehorn struct cpcht_irq htirq_map[128];
1358aa8e94dSNathan Whitehorn struct mtx htirq_mtx;
136ca2c1931SNathan Whitehorn };
137ca2c1931SNathan Whitehorn
1389a35e64eSNathan Whitehorn DEFINE_CLASS_1(pcib, cpcht_driver, cpcht_methods, sizeof(struct cpcht_softc),
13924042910SMarcin Wojtas ofw_pcib_driver);
140992ae60bSJohn Baldwin EARLY_DRIVER_MODULE(cpcht, ofwbus, cpcht_driver, 0, 0, BUS_PASS_BUS);
1411c96bdd1SNathan Whitehorn
142c04246f4SNathan Whitehorn #define CPCHT_IOPORT_BASE 0xf4000000UL /* Hardwired */
143c04246f4SNathan Whitehorn #define CPCHT_IOPORT_SIZE 0x00400000UL
144c04246f4SNathan Whitehorn
145ca2c1931SNathan Whitehorn #define HTAPIC_REQUEST_EOI 0x20
146ca2c1931SNathan Whitehorn #define HTAPIC_TRIGGER_LEVEL 0x02
147ca2c1931SNathan Whitehorn #define HTAPIC_MASK 0x01
148ca2c1931SNathan Whitehorn
1491c96bdd1SNathan Whitehorn static int
cpcht_probe(device_t dev)1501c96bdd1SNathan Whitehorn cpcht_probe(device_t dev)
1511c96bdd1SNathan Whitehorn {
1521c96bdd1SNathan Whitehorn const char *type, *compatible;
1531c96bdd1SNathan Whitehorn
1541c96bdd1SNathan Whitehorn type = ofw_bus_get_type(dev);
1551c96bdd1SNathan Whitehorn compatible = ofw_bus_get_compat(dev);
1561c96bdd1SNathan Whitehorn
1571c96bdd1SNathan Whitehorn if (type == NULL || compatible == NULL)
1581c96bdd1SNathan Whitehorn return (ENXIO);
1591c96bdd1SNathan Whitehorn
1601c96bdd1SNathan Whitehorn if (strcmp(type, "ht") != 0)
1611c96bdd1SNathan Whitehorn return (ENXIO);
1621c96bdd1SNathan Whitehorn
163ca2c1931SNathan Whitehorn if (strcmp(compatible, "u3-ht") != 0)
1641c96bdd1SNathan Whitehorn return (ENXIO);
165ca2c1931SNathan Whitehorn
166ca2c1931SNathan Whitehorn device_set_desc(dev, "IBM CPC9X5 HyperTransport Tunnel");
167ca2c1931SNathan Whitehorn return (0);
1681c96bdd1SNathan Whitehorn }
1691c96bdd1SNathan Whitehorn
1701c96bdd1SNathan Whitehorn static int
cpcht_attach(device_t dev)1711c96bdd1SNathan Whitehorn cpcht_attach(device_t dev)
1721c96bdd1SNathan Whitehorn {
173ca2c1931SNathan Whitehorn struct cpcht_softc *sc;
174ca2c1931SNathan Whitehorn phandle_t node, child;
175ca2c1931SNathan Whitehorn u_int32_t reg[3];
1769a35e64eSNathan Whitehorn int i;
1771c96bdd1SNathan Whitehorn
1781c96bdd1SNathan Whitehorn node = ofw_bus_get_node(dev);
1791c96bdd1SNathan Whitehorn sc = device_get_softc(dev);
1801c96bdd1SNathan Whitehorn
181509142e1SNathan Whitehorn if (OF_getencprop(node, "reg", reg, sizeof(reg)) < 12)
1821c96bdd1SNathan Whitehorn return (ENXIO);
1831c96bdd1SNathan Whitehorn
184761d31e8SNathan Whitehorn if (OF_getproplen(node, "ranges") <= 0)
1859a35e64eSNathan Whitehorn sc->pci_sc.sc_quirks = OFW_PCI_QUIRK_RANGES_ON_CHILDREN;
186ca2c1931SNathan Whitehorn sc->sc_populated_slots = 0;
187ca2c1931SNathan Whitehorn sc->sc_data = (vm_offset_t)pmap_mapdev(reg[1], reg[2]);
1881c96bdd1SNathan Whitehorn
1891c96bdd1SNathan Whitehorn /*
190ca2c1931SNathan Whitehorn * Set up the resource manager and the HT->MPIC mapping. For cpcht,
191ca2c1931SNathan Whitehorn * the ranges are properties of the child bridges, and this is also
192ca2c1931SNathan Whitehorn * where we get the HT interrupts properties.
1931c96bdd1SNathan Whitehorn */
1941c96bdd1SNathan Whitehorn
1959a35e64eSNathan Whitehorn #if 0
196c04246f4SNathan Whitehorn /* I/O port mappings are usually not in the device tree */
1979a35e64eSNathan Whitehorn rman_manage_region(&sc->pci_sc.sc_io_rman, 0, CPCHT_IOPORT_SIZE - 1);
1989a35e64eSNathan Whitehorn #endif
199c04246f4SNathan Whitehorn
200ca2c1931SNathan Whitehorn bzero(sc->htirq_map, sizeof(sc->htirq_map));
2018aa8e94dSNathan Whitehorn mtx_init(&sc->htirq_mtx, "cpcht irq", NULL, MTX_DEF);
2028aa8e94dSNathan Whitehorn for (i = 0; i < 8; i++)
2038aa8e94dSNathan Whitehorn sc->htirq_map[i].irq_type = IRQ_INTERNAL;
204ca2c1931SNathan Whitehorn for (child = OF_child(node); child != 0; child = OF_peer(child))
205ca2c1931SNathan Whitehorn cpcht_configure_htbridge(dev, child);
2061c96bdd1SNathan Whitehorn
207ca2c1931SNathan Whitehorn /* Now make the mapping table available to the MPIC */
208ca2c1931SNathan Whitehorn cpcht_irqmap = sc->htirq_map;
2091c96bdd1SNathan Whitehorn
21024042910SMarcin Wojtas return (ofw_pcib_attach(dev));
2111c96bdd1SNathan Whitehorn }
2121c96bdd1SNathan Whitehorn
213ca2c1931SNathan Whitehorn static void
cpcht_configure_htbridge(device_t dev,phandle_t child)214ca2c1931SNathan Whitehorn cpcht_configure_htbridge(device_t dev, phandle_t child)
215ca2c1931SNathan Whitehorn {
216ca2c1931SNathan Whitehorn struct cpcht_softc *sc;
217ca2c1931SNathan Whitehorn struct ofw_pci_register pcir;
2189a35e64eSNathan Whitehorn int ptr, nextptr;
219ca2c1931SNathan Whitehorn uint32_t vend, val;
220ca2c1931SNathan Whitehorn int i, nirq, irq;
221761d31e8SNathan Whitehorn u_int b, f, s;
222ca2c1931SNathan Whitehorn
223ca2c1931SNathan Whitehorn sc = device_get_softc(dev);
224509142e1SNathan Whitehorn if (OF_getencprop(child, "reg", (pcell_t *)&pcir, sizeof(pcir)) == -1)
225ca2c1931SNathan Whitehorn return;
226ca2c1931SNathan Whitehorn
227761d31e8SNathan Whitehorn b = OFW_PCI_PHYS_HI_BUS(pcir.phys_hi);
228ca2c1931SNathan Whitehorn s = OFW_PCI_PHYS_HI_DEVICE(pcir.phys_hi);
229ca2c1931SNathan Whitehorn f = OFW_PCI_PHYS_HI_FUNCTION(pcir.phys_hi);
230ca2c1931SNathan Whitehorn
231ca2c1931SNathan Whitehorn /*
232ca2c1931SNathan Whitehorn * Mark this slot is populated. The remote south bridge does
233ca2c1931SNathan Whitehorn * not like us talking to unpopulated slots on the root bus.
234ca2c1931SNathan Whitehorn */
235ca2c1931SNathan Whitehorn sc->sc_populated_slots |= (1 << s);
236ca2c1931SNathan Whitehorn
237ca2c1931SNathan Whitehorn /*
238ca2c1931SNathan Whitehorn * Next build up any HT->MPIC mappings for this sub-bus. One would
239ca2c1931SNathan Whitehorn * naively hope that enabling, disabling, and EOIing interrupts would
240ca2c1931SNathan Whitehorn * cause the appropriate HT bus transactions to that effect. This is
241ca2c1931SNathan Whitehorn * not the case.
242ca2c1931SNathan Whitehorn *
243ca2c1931SNathan Whitehorn * Instead, we have to muck about on the HT peer's root PCI bridges,
244ca2c1931SNathan Whitehorn * figure out what interrupts they send, enable them, and cache
245ca2c1931SNathan Whitehorn * the location of their WaitForEOI registers so that we can
246ca2c1931SNathan Whitehorn * send EOIs later.
247ca2c1931SNathan Whitehorn */
248ca2c1931SNathan Whitehorn
249ca2c1931SNathan Whitehorn /* All the devices we are interested in have caps */
250761d31e8SNathan Whitehorn if (!(PCIB_READ_CONFIG(dev, b, s, f, PCIR_STATUS, 2)
251ca2c1931SNathan Whitehorn & PCIM_STATUS_CAPPRESENT))
252ca2c1931SNathan Whitehorn return;
253ca2c1931SNathan Whitehorn
254761d31e8SNathan Whitehorn nextptr = PCIB_READ_CONFIG(dev, b, s, f, PCIR_CAP_PTR, 1);
255ca2c1931SNathan Whitehorn while (nextptr != 0) {
256ca2c1931SNathan Whitehorn ptr = nextptr;
257761d31e8SNathan Whitehorn nextptr = PCIB_READ_CONFIG(dev, b, s, f,
258ca2c1931SNathan Whitehorn ptr + PCICAP_NEXTPTR, 1);
259ca2c1931SNathan Whitehorn
260ca2c1931SNathan Whitehorn /* Find the HT IRQ capabilities */
261761d31e8SNathan Whitehorn if (PCIB_READ_CONFIG(dev, b, s, f,
262ca2c1931SNathan Whitehorn ptr + PCICAP_ID, 1) != PCIY_HT)
263ca2c1931SNathan Whitehorn continue;
264ca2c1931SNathan Whitehorn
265761d31e8SNathan Whitehorn val = PCIB_READ_CONFIG(dev, b, s, f, ptr + PCIR_HT_COMMAND, 2);
266ca2c1931SNathan Whitehorn if ((val & PCIM_HTCMD_CAP_MASK) != PCIM_HTCAP_INTERRUPT)
267ca2c1931SNathan Whitehorn continue;
268ca2c1931SNathan Whitehorn
269ca2c1931SNathan Whitehorn /* Ask for the IRQ count */
270761d31e8SNathan Whitehorn PCIB_WRITE_CONFIG(dev, b, s, f, ptr + PCIR_HT_COMMAND, 0x1, 1);
271761d31e8SNathan Whitehorn nirq = PCIB_READ_CONFIG(dev, b, s, f, ptr + 4, 4);
272ca2c1931SNathan Whitehorn nirq = ((nirq >> 16) & 0xff) + 1;
273ca2c1931SNathan Whitehorn
274ca2c1931SNathan Whitehorn device_printf(dev, "%d HT IRQs on device %d.%d\n", nirq, s, f);
275ca2c1931SNathan Whitehorn
276ca2c1931SNathan Whitehorn for (i = 0; i < nirq; i++) {
277761d31e8SNathan Whitehorn PCIB_WRITE_CONFIG(dev, b, s, f,
278ca2c1931SNathan Whitehorn ptr + PCIR_HT_COMMAND, 0x10 + (i << 1), 1);
279761d31e8SNathan Whitehorn irq = PCIB_READ_CONFIG(dev, b, s, f, ptr + 4, 4);
280ca2c1931SNathan Whitehorn
281ca2c1931SNathan Whitehorn /*
282ca2c1931SNathan Whitehorn * Mask this interrupt for now.
283ca2c1931SNathan Whitehorn */
284761d31e8SNathan Whitehorn PCIB_WRITE_CONFIG(dev, b, s, f, ptr + 4,
285ca2c1931SNathan Whitehorn irq | HTAPIC_MASK, 4);
286ca2c1931SNathan Whitehorn irq = (irq >> 16) & 0xff;
287ca2c1931SNathan Whitehorn
2888aa8e94dSNathan Whitehorn sc->htirq_map[irq].irq_type = IRQ_HT;
289ca2c1931SNathan Whitehorn sc->htirq_map[irq].ht_source = i;
290ca2c1931SNathan Whitehorn sc->htirq_map[irq].ht_base = sc->sc_data +
291ca2c1931SNathan Whitehorn (((((s & 0x1f) << 3) | (f & 0x07)) << 8) | (ptr));
292ca2c1931SNathan Whitehorn
293761d31e8SNathan Whitehorn PCIB_WRITE_CONFIG(dev, b, s, f,
294ca2c1931SNathan Whitehorn ptr + PCIR_HT_COMMAND, 0x11 + (i << 1), 1);
295ca2c1931SNathan Whitehorn sc->htirq_map[irq].eoi_data =
296761d31e8SNathan Whitehorn PCIB_READ_CONFIG(dev, b, s, f, ptr + 4, 4) |
297ca2c1931SNathan Whitehorn 0x80000000;
298ca2c1931SNathan Whitehorn
299ca2c1931SNathan Whitehorn /*
300ca2c1931SNathan Whitehorn * Apple uses a non-compliant IO/APIC that differs
301ca2c1931SNathan Whitehorn * in how we signal EOIs. Check if this device was
302ca2c1931SNathan Whitehorn * made by Apple, and act accordingly.
303ca2c1931SNathan Whitehorn */
304761d31e8SNathan Whitehorn vend = PCIB_READ_CONFIG(dev, b, s, f,
305ca2c1931SNathan Whitehorn PCIR_DEVVENDOR, 4);
306ca2c1931SNathan Whitehorn if ((vend & 0xffff) == 0x106b)
307ca2c1931SNathan Whitehorn sc->htirq_map[irq].apple_eoi =
308ca2c1931SNathan Whitehorn (sc->htirq_map[irq].ht_base - ptr) + 0x60;
309ca2c1931SNathan Whitehorn }
310ca2c1931SNathan Whitehorn }
311ca2c1931SNathan Whitehorn }
312ca2c1931SNathan Whitehorn
3131c96bdd1SNathan Whitehorn static u_int32_t
cpcht_read_config(device_t dev,u_int bus,u_int slot,u_int func,u_int reg,int width)314ca2c1931SNathan Whitehorn cpcht_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
3151c96bdd1SNathan Whitehorn int width)
3161c96bdd1SNathan Whitehorn {
317ca2c1931SNathan Whitehorn struct cpcht_softc *sc;
3181c96bdd1SNathan Whitehorn vm_offset_t caoff;
3191c96bdd1SNathan Whitehorn
3201c96bdd1SNathan Whitehorn sc = device_get_softc(dev);
3211c96bdd1SNathan Whitehorn caoff = sc->sc_data +
3221c96bdd1SNathan Whitehorn (((((slot & 0x1f) << 3) | (func & 0x07)) << 8) | reg);
3231c96bdd1SNathan Whitehorn
324ca2c1931SNathan Whitehorn if (bus == 0 && (!(sc->sc_populated_slots & (1 << slot)) || func > 0))
325ca2c1931SNathan Whitehorn return (0xffffffff);
326ca2c1931SNathan Whitehorn
327ca2c1931SNathan Whitehorn if (bus > 0)
328ca2c1931SNathan Whitehorn caoff += 0x01000000UL + (bus << 16);
329ca2c1931SNathan Whitehorn
3301c96bdd1SNathan Whitehorn switch (width) {
3311c96bdd1SNathan Whitehorn case 1:
3321c96bdd1SNathan Whitehorn return (in8rb(caoff));
3331c96bdd1SNathan Whitehorn break;
3341c96bdd1SNathan Whitehorn case 2:
3351c96bdd1SNathan Whitehorn return (in16rb(caoff));
3361c96bdd1SNathan Whitehorn break;
3371c96bdd1SNathan Whitehorn case 4:
3381c96bdd1SNathan Whitehorn return (in32rb(caoff));
3391c96bdd1SNathan Whitehorn break;
3401c96bdd1SNathan Whitehorn }
3411c96bdd1SNathan Whitehorn
3421c96bdd1SNathan Whitehorn return (0xffffffff);
3431c96bdd1SNathan Whitehorn }
3441c96bdd1SNathan Whitehorn
3451c96bdd1SNathan Whitehorn static void
cpcht_write_config(device_t dev,u_int bus,u_int slot,u_int func,u_int reg,u_int32_t val,int width)346ca2c1931SNathan Whitehorn cpcht_write_config(device_t dev, u_int bus, u_int slot, u_int func,
3471c96bdd1SNathan Whitehorn u_int reg, u_int32_t val, int width)
3481c96bdd1SNathan Whitehorn {
349ca2c1931SNathan Whitehorn struct cpcht_softc *sc;
3501c96bdd1SNathan Whitehorn vm_offset_t caoff;
3511c96bdd1SNathan Whitehorn
3521c96bdd1SNathan Whitehorn sc = device_get_softc(dev);
3531c96bdd1SNathan Whitehorn caoff = sc->sc_data +
3541c96bdd1SNathan Whitehorn (((((slot & 0x1f) << 3) | (func & 0x07)) << 8) | reg);
3551c96bdd1SNathan Whitehorn
356ca2c1931SNathan Whitehorn if (bus == 0 && (!(sc->sc_populated_slots & (1 << slot)) || func > 0))
357ca2c1931SNathan Whitehorn return;
358ca2c1931SNathan Whitehorn
359ca2c1931SNathan Whitehorn if (bus > 0)
360ca2c1931SNathan Whitehorn caoff += 0x01000000UL + (bus << 16);
361ca2c1931SNathan Whitehorn
3621c96bdd1SNathan Whitehorn switch (width) {
3631c96bdd1SNathan Whitehorn case 1:
3641c96bdd1SNathan Whitehorn out8rb(caoff, val);
3651c96bdd1SNathan Whitehorn break;
3661c96bdd1SNathan Whitehorn case 2:
3671c96bdd1SNathan Whitehorn out16rb(caoff, val);
3681c96bdd1SNathan Whitehorn break;
3691c96bdd1SNathan Whitehorn case 4:
3701c96bdd1SNathan Whitehorn out32rb(caoff, val);
3711c96bdd1SNathan Whitehorn break;
3721c96bdd1SNathan Whitehorn }
3731c96bdd1SNathan Whitehorn }
3741c96bdd1SNathan Whitehorn
3751c96bdd1SNathan Whitehorn static int
cpcht_route_interrupt(device_t bus,device_t dev,int pin)376ca2c1931SNathan Whitehorn cpcht_route_interrupt(device_t bus, device_t dev, int pin)
377ca2c1931SNathan Whitehorn {
378ca2c1931SNathan Whitehorn return (pin);
379ca2c1931SNathan Whitehorn }
380ca2c1931SNathan Whitehorn
3818aa8e94dSNathan Whitehorn static int
cpcht_alloc_msi(device_t dev,device_t child,int count,int maxcount,int * irqs)3828aa8e94dSNathan Whitehorn cpcht_alloc_msi(device_t dev, device_t child, int count, int maxcount,
3838aa8e94dSNathan Whitehorn int *irqs)
3848aa8e94dSNathan Whitehorn {
3858aa8e94dSNathan Whitehorn struct cpcht_softc *sc;
3868aa8e94dSNathan Whitehorn int i, j;
3878aa8e94dSNathan Whitehorn
3888aa8e94dSNathan Whitehorn sc = device_get_softc(dev);
3898aa8e94dSNathan Whitehorn j = 0;
3908aa8e94dSNathan Whitehorn
3918aa8e94dSNathan Whitehorn /* Bail if no MSI PIC yet */
3928aa8e94dSNathan Whitehorn if (cpcht_msipic == 0)
3938aa8e94dSNathan Whitehorn return (ENXIO);
3948aa8e94dSNathan Whitehorn
3958aa8e94dSNathan Whitehorn mtx_lock(&sc->htirq_mtx);
3968aa8e94dSNathan Whitehorn for (i = 8; i < 124 - count; i++) {
3978aa8e94dSNathan Whitehorn for (j = 0; j < count; j++) {
3988aa8e94dSNathan Whitehorn if (sc->htirq_map[i+j].irq_type != IRQ_NONE)
3998aa8e94dSNathan Whitehorn break;
4008aa8e94dSNathan Whitehorn }
4018aa8e94dSNathan Whitehorn if (j == count)
4028aa8e94dSNathan Whitehorn break;
4038aa8e94dSNathan Whitehorn
4048aa8e94dSNathan Whitehorn i += j; /* We know there isn't a large enough run */
4058aa8e94dSNathan Whitehorn }
4068aa8e94dSNathan Whitehorn
4078aa8e94dSNathan Whitehorn if (j != count) {
4088aa8e94dSNathan Whitehorn mtx_unlock(&sc->htirq_mtx);
4098aa8e94dSNathan Whitehorn return (ENXIO);
4108aa8e94dSNathan Whitehorn }
4118aa8e94dSNathan Whitehorn
4128aa8e94dSNathan Whitehorn for (j = 0; j < count; j++) {
413607ebaafSMarcel Moolenaar irqs[j] = MAP_IRQ(cpcht_msipic, i+j);
4148aa8e94dSNathan Whitehorn sc->htirq_map[i+j].irq_type = IRQ_MSI;
4158aa8e94dSNathan Whitehorn }
4168aa8e94dSNathan Whitehorn mtx_unlock(&sc->htirq_mtx);
4178aa8e94dSNathan Whitehorn
4188aa8e94dSNathan Whitehorn return (0);
4198aa8e94dSNathan Whitehorn }
4208aa8e94dSNathan Whitehorn
4218aa8e94dSNathan Whitehorn static int
cpcht_release_msi(device_t dev,device_t child,int count,int * irqs)4228aa8e94dSNathan Whitehorn cpcht_release_msi(device_t dev, device_t child, int count, int *irqs)
4238aa8e94dSNathan Whitehorn {
4248aa8e94dSNathan Whitehorn struct cpcht_softc *sc;
4258aa8e94dSNathan Whitehorn int i;
4268aa8e94dSNathan Whitehorn
4278aa8e94dSNathan Whitehorn sc = device_get_softc(dev);
4288aa8e94dSNathan Whitehorn
4298aa8e94dSNathan Whitehorn mtx_lock(&sc->htirq_mtx);
4308aa8e94dSNathan Whitehorn for (i = 0; i < count; i++)
4318aa8e94dSNathan Whitehorn sc->htirq_map[irqs[i] & 0xff].irq_type = IRQ_NONE;
4328aa8e94dSNathan Whitehorn mtx_unlock(&sc->htirq_mtx);
4338aa8e94dSNathan Whitehorn
4348aa8e94dSNathan Whitehorn return (0);
4358aa8e94dSNathan Whitehorn }
4368aa8e94dSNathan Whitehorn
4378aa8e94dSNathan Whitehorn static int
cpcht_alloc_msix(device_t dev,device_t child,int * irq)4388aa8e94dSNathan Whitehorn cpcht_alloc_msix(device_t dev, device_t child, int *irq)
4398aa8e94dSNathan Whitehorn {
4408aa8e94dSNathan Whitehorn struct cpcht_softc *sc;
4418aa8e94dSNathan Whitehorn int i;
4428aa8e94dSNathan Whitehorn
4438aa8e94dSNathan Whitehorn sc = device_get_softc(dev);
4448aa8e94dSNathan Whitehorn
4458aa8e94dSNathan Whitehorn /* Bail if no MSI PIC yet */
4468aa8e94dSNathan Whitehorn if (cpcht_msipic == 0)
4478aa8e94dSNathan Whitehorn return (ENXIO);
4488aa8e94dSNathan Whitehorn
4498aa8e94dSNathan Whitehorn mtx_lock(&sc->htirq_mtx);
4508aa8e94dSNathan Whitehorn for (i = 8; i < 124; i++) {
4518aa8e94dSNathan Whitehorn if (sc->htirq_map[i].irq_type == IRQ_NONE) {
4528aa8e94dSNathan Whitehorn sc->htirq_map[i].irq_type = IRQ_MSI;
453607ebaafSMarcel Moolenaar *irq = MAP_IRQ(cpcht_msipic, i);
4548aa8e94dSNathan Whitehorn
4558aa8e94dSNathan Whitehorn mtx_unlock(&sc->htirq_mtx);
4568aa8e94dSNathan Whitehorn return (0);
4578aa8e94dSNathan Whitehorn }
4588aa8e94dSNathan Whitehorn }
4598aa8e94dSNathan Whitehorn mtx_unlock(&sc->htirq_mtx);
4608aa8e94dSNathan Whitehorn
4618aa8e94dSNathan Whitehorn return (ENXIO);
4628aa8e94dSNathan Whitehorn }
4638aa8e94dSNathan Whitehorn
4648aa8e94dSNathan Whitehorn static int
cpcht_release_msix(device_t dev,device_t child,int irq)4658aa8e94dSNathan Whitehorn cpcht_release_msix(device_t dev, device_t child, int irq)
4668aa8e94dSNathan Whitehorn {
4678aa8e94dSNathan Whitehorn struct cpcht_softc *sc;
4688aa8e94dSNathan Whitehorn
4698aa8e94dSNathan Whitehorn sc = device_get_softc(dev);
4708aa8e94dSNathan Whitehorn
4718aa8e94dSNathan Whitehorn mtx_lock(&sc->htirq_mtx);
4728aa8e94dSNathan Whitehorn sc->htirq_map[irq & 0xff].irq_type = IRQ_NONE;
4738aa8e94dSNathan Whitehorn mtx_unlock(&sc->htirq_mtx);
4748aa8e94dSNathan Whitehorn
4758aa8e94dSNathan Whitehorn return (0);
4768aa8e94dSNathan Whitehorn }
4778aa8e94dSNathan Whitehorn
4788aa8e94dSNathan Whitehorn static int
cpcht_map_msi(device_t dev,device_t child,int irq,uint64_t * addr,uint32_t * data)4798aa8e94dSNathan Whitehorn cpcht_map_msi(device_t dev, device_t child, int irq, uint64_t *addr,
4808aa8e94dSNathan Whitehorn uint32_t *data)
4818aa8e94dSNathan Whitehorn {
4828aa8e94dSNathan Whitehorn device_t pcib;
4838aa8e94dSNathan Whitehorn struct pci_devinfo *dinfo;
4848aa8e94dSNathan Whitehorn struct pcicfg_ht *ht = NULL;
4858aa8e94dSNathan Whitehorn
4868aa8e94dSNathan Whitehorn for (pcib = child; pcib != dev; pcib =
4878aa8e94dSNathan Whitehorn device_get_parent(device_get_parent(pcib))) {
4888aa8e94dSNathan Whitehorn dinfo = device_get_ivars(pcib);
4898aa8e94dSNathan Whitehorn ht = &dinfo->cfg.ht;
4908aa8e94dSNathan Whitehorn
4918aa8e94dSNathan Whitehorn if (ht == NULL)
4928aa8e94dSNathan Whitehorn continue;
4938aa8e94dSNathan Whitehorn }
4948aa8e94dSNathan Whitehorn
4958aa8e94dSNathan Whitehorn if (ht == NULL)
4968aa8e94dSNathan Whitehorn return (ENXIO);
4978aa8e94dSNathan Whitehorn
4988aa8e94dSNathan Whitehorn *addr = ht->ht_msiaddr;
4998aa8e94dSNathan Whitehorn *data = irq & 0xff;
5008aa8e94dSNathan Whitehorn
5018aa8e94dSNathan Whitehorn return (0);
5028aa8e94dSNathan Whitehorn }
5038aa8e94dSNathan Whitehorn
504ca2c1931SNathan Whitehorn /*
505ca2c1931SNathan Whitehorn * Driver for the integrated MPIC on U3/U4 (CPC925/CPC945)
506ca2c1931SNathan Whitehorn */
507ca2c1931SNathan Whitehorn
508ca2c1931SNathan Whitehorn static int openpic_cpcht_probe(device_t);
509ca2c1931SNathan Whitehorn static int openpic_cpcht_attach(device_t);
510ca2c1931SNathan Whitehorn static void openpic_cpcht_config(device_t, u_int irq,
511ca2c1931SNathan Whitehorn enum intr_trigger trig, enum intr_polarity pol);
51256505ec0SJustin Hibbits static void openpic_cpcht_enable(device_t, u_int irq, u_int vector,
51356505ec0SJustin Hibbits void **priv);
51456505ec0SJustin Hibbits static void openpic_cpcht_unmask(device_t, u_int irq, void *priv);
51556505ec0SJustin Hibbits static void openpic_cpcht_eoi(device_t, u_int irq, void *priv);
516ca2c1931SNathan Whitehorn
517ca2c1931SNathan Whitehorn static device_method_t openpic_cpcht_methods[] = {
518ca2c1931SNathan Whitehorn /* Device interface */
519ca2c1931SNathan Whitehorn DEVMETHOD(device_probe, openpic_cpcht_probe),
520ca2c1931SNathan Whitehorn DEVMETHOD(device_attach, openpic_cpcht_attach),
521ca2c1931SNathan Whitehorn
522ca2c1931SNathan Whitehorn /* PIC interface */
52308393b3eSNathan Whitehorn DEVMETHOD(pic_bind, openpic_bind),
524ca2c1931SNathan Whitehorn DEVMETHOD(pic_config, openpic_cpcht_config),
525ca2c1931SNathan Whitehorn DEVMETHOD(pic_dispatch, openpic_dispatch),
526ca2c1931SNathan Whitehorn DEVMETHOD(pic_enable, openpic_cpcht_enable),
527ca2c1931SNathan Whitehorn DEVMETHOD(pic_eoi, openpic_cpcht_eoi),
528ca2c1931SNathan Whitehorn DEVMETHOD(pic_ipi, openpic_ipi),
529ca2c1931SNathan Whitehorn DEVMETHOD(pic_mask, openpic_mask),
530ca2c1931SNathan Whitehorn DEVMETHOD(pic_unmask, openpic_cpcht_unmask),
531ca2c1931SNathan Whitehorn
532ca2c1931SNathan Whitehorn { 0, 0 },
533ca2c1931SNathan Whitehorn };
534ca2c1931SNathan Whitehorn
535ca2c1931SNathan Whitehorn struct openpic_cpcht_softc {
536ca2c1931SNathan Whitehorn struct openpic_softc sc_openpic;
537ca2c1931SNathan Whitehorn
538ca2c1931SNathan Whitehorn struct mtx sc_ht_mtx;
539ca2c1931SNathan Whitehorn };
540ca2c1931SNathan Whitehorn
541ca2c1931SNathan Whitehorn static driver_t openpic_cpcht_driver = {
542ca2c1931SNathan Whitehorn "htpic",
543ca2c1931SNathan Whitehorn openpic_cpcht_methods,
54494ee1167SNathan Whitehorn sizeof(struct openpic_cpcht_softc),
545ca2c1931SNathan Whitehorn };
546ca2c1931SNathan Whitehorn
54752ed569bSJohn Baldwin EARLY_DRIVER_MODULE(openpic, unin, openpic_cpcht_driver, 0, 0,
54852ed569bSJohn Baldwin BUS_PASS_INTERRUPT);
549ca2c1931SNathan Whitehorn
550ca2c1931SNathan Whitehorn static int
openpic_cpcht_probe(device_t dev)551ca2c1931SNathan Whitehorn openpic_cpcht_probe(device_t dev)
552ca2c1931SNathan Whitehorn {
553ca2c1931SNathan Whitehorn const char *type = ofw_bus_get_type(dev);
554ca2c1931SNathan Whitehorn
555ca2c1931SNathan Whitehorn if (strcmp(type, "open-pic") != 0)
556ca2c1931SNathan Whitehorn return (ENXIO);
557ca2c1931SNathan Whitehorn
558ca2c1931SNathan Whitehorn device_set_desc(dev, OPENPIC_DEVSTR);
559ca2c1931SNathan Whitehorn return (0);
560ca2c1931SNathan Whitehorn }
561ca2c1931SNathan Whitehorn
562ca2c1931SNathan Whitehorn static int
openpic_cpcht_attach(device_t dev)563ca2c1931SNathan Whitehorn openpic_cpcht_attach(device_t dev)
564ca2c1931SNathan Whitehorn {
565ca2c1931SNathan Whitehorn struct openpic_cpcht_softc *sc;
5666d2d7b8cSMarcel Moolenaar phandle_t node;
567ca2c1931SNathan Whitehorn int err, irq;
568ca2c1931SNathan Whitehorn
5696d2d7b8cSMarcel Moolenaar node = ofw_bus_get_node(dev);
5706d2d7b8cSMarcel Moolenaar err = openpic_common_attach(dev, node);
571ca2c1931SNathan Whitehorn if (err != 0)
572ca2c1931SNathan Whitehorn return (err);
573ca2c1931SNathan Whitehorn
574ca2c1931SNathan Whitehorn /*
575ca2c1931SNathan Whitehorn * The HT APIC stuff is not thread-safe, so we need a mutex to
576ca2c1931SNathan Whitehorn * protect it.
577ca2c1931SNathan Whitehorn */
578ca2c1931SNathan Whitehorn sc = device_get_softc(dev);
579ca2c1931SNathan Whitehorn mtx_init(&sc->sc_ht_mtx, "htpic", NULL, MTX_SPIN);
580ca2c1931SNathan Whitehorn
581ca2c1931SNathan Whitehorn /*
582ca2c1931SNathan Whitehorn * Interrupts 0-3 are internally sourced and are level triggered
583ca2c1931SNathan Whitehorn * active low. Interrupts 4-123 are connected to a pulse generator
584ca2c1931SNathan Whitehorn * and should be programmed as edge triggered low-to-high.
585ca2c1931SNathan Whitehorn *
586ca2c1931SNathan Whitehorn * IBM CPC945 Manual, Section 9.3.
587ca2c1931SNathan Whitehorn */
588ca2c1931SNathan Whitehorn
589ca2c1931SNathan Whitehorn for (irq = 0; irq < 4; irq++)
590ca2c1931SNathan Whitehorn openpic_config(dev, irq, INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
591ca2c1931SNathan Whitehorn for (irq = 4; irq < 124; irq++)
592ca2c1931SNathan Whitehorn openpic_config(dev, irq, INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
593ca2c1931SNathan Whitehorn
5948aa8e94dSNathan Whitehorn /*
5958aa8e94dSNathan Whitehorn * Use this PIC for MSI only if it is the root PIC. This may not
5968aa8e94dSNathan Whitehorn * be necessary, but Linux does it, and I cannot find any U3 machines
5978aa8e94dSNathan Whitehorn * with MSI devices to test.
5988aa8e94dSNathan Whitehorn */
5998aa8e94dSNathan Whitehorn if (dev == root_pic)
6006d2d7b8cSMarcel Moolenaar cpcht_msipic = node;
6018aa8e94dSNathan Whitehorn
602ca2c1931SNathan Whitehorn return (0);
603ca2c1931SNathan Whitehorn }
604ca2c1931SNathan Whitehorn
605ca2c1931SNathan Whitehorn static void
openpic_cpcht_config(device_t dev,u_int irq,enum intr_trigger trig,enum intr_polarity pol)606ca2c1931SNathan Whitehorn openpic_cpcht_config(device_t dev, u_int irq, enum intr_trigger trig,
607ca2c1931SNathan Whitehorn enum intr_polarity pol)
608ca2c1931SNathan Whitehorn {
609ca2c1931SNathan Whitehorn struct openpic_cpcht_softc *sc;
610ca2c1931SNathan Whitehorn uint32_t ht_irq;
611ca2c1931SNathan Whitehorn
612ca2c1931SNathan Whitehorn /*
613ca2c1931SNathan Whitehorn * The interrupt settings for the MPIC are completely determined
614ca2c1931SNathan Whitehorn * by the internal wiring in the northbridge. Real changes to these
615ca2c1931SNathan Whitehorn * settings need to be negotiated with the remote IO-APIC on the HT
616ca2c1931SNathan Whitehorn * link.
617ca2c1931SNathan Whitehorn */
618ca2c1931SNathan Whitehorn
619ca2c1931SNathan Whitehorn sc = device_get_softc(dev);
620ca2c1931SNathan Whitehorn
621ca2c1931SNathan Whitehorn if (cpcht_irqmap != NULL && irq < 128 &&
622ca2c1931SNathan Whitehorn cpcht_irqmap[irq].ht_base > 0 && !cpcht_irqmap[irq].edge) {
623ca2c1931SNathan Whitehorn mtx_lock_spin(&sc->sc_ht_mtx);
624ca2c1931SNathan Whitehorn
625ca2c1931SNathan Whitehorn /* Program the data port */
626ca2c1931SNathan Whitehorn out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
627ca2c1931SNathan Whitehorn 0x10 + (cpcht_irqmap[irq].ht_source << 1));
628ca2c1931SNathan Whitehorn
629ca2c1931SNathan Whitehorn /* Grab the IRQ config register */
630ca2c1931SNathan Whitehorn ht_irq = in32rb(cpcht_irqmap[irq].ht_base + 4);
631ca2c1931SNathan Whitehorn
632ca2c1931SNathan Whitehorn /* Mask the IRQ while we fiddle settings */
633ca2c1931SNathan Whitehorn out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq | HTAPIC_MASK);
634ca2c1931SNathan Whitehorn
635ca2c1931SNathan Whitehorn /* Program the interrupt sense */
636ca2c1931SNathan Whitehorn ht_irq &= ~(HTAPIC_TRIGGER_LEVEL | HTAPIC_REQUEST_EOI);
637ca2c1931SNathan Whitehorn if (trig == INTR_TRIGGER_EDGE) {
638ca2c1931SNathan Whitehorn cpcht_irqmap[irq].edge = 1;
639ca2c1931SNathan Whitehorn } else {
640ca2c1931SNathan Whitehorn cpcht_irqmap[irq].edge = 0;
641ca2c1931SNathan Whitehorn ht_irq |= HTAPIC_TRIGGER_LEVEL | HTAPIC_REQUEST_EOI;
642ca2c1931SNathan Whitehorn }
643ca2c1931SNathan Whitehorn out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq);
644ca2c1931SNathan Whitehorn
645ca2c1931SNathan Whitehorn mtx_unlock_spin(&sc->sc_ht_mtx);
646ca2c1931SNathan Whitehorn }
647ca2c1931SNathan Whitehorn }
648ca2c1931SNathan Whitehorn
649ca2c1931SNathan Whitehorn static void
openpic_cpcht_enable(device_t dev,u_int irq,u_int vec,void ** priv)65056505ec0SJustin Hibbits openpic_cpcht_enable(device_t dev, u_int irq, u_int vec, void **priv)
651ca2c1931SNathan Whitehorn {
652ca2c1931SNathan Whitehorn struct openpic_cpcht_softc *sc;
653ca2c1931SNathan Whitehorn uint32_t ht_irq;
654ca2c1931SNathan Whitehorn
65556505ec0SJustin Hibbits openpic_enable(dev, irq, vec, priv);
656ca2c1931SNathan Whitehorn
657ca2c1931SNathan Whitehorn sc = device_get_softc(dev);
658ca2c1931SNathan Whitehorn
659ca2c1931SNathan Whitehorn if (cpcht_irqmap != NULL && irq < 128 &&
660ca2c1931SNathan Whitehorn cpcht_irqmap[irq].ht_base > 0) {
661ca2c1931SNathan Whitehorn mtx_lock_spin(&sc->sc_ht_mtx);
662ca2c1931SNathan Whitehorn
663ca2c1931SNathan Whitehorn /* Program the data port */
664ca2c1931SNathan Whitehorn out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
665ca2c1931SNathan Whitehorn 0x10 + (cpcht_irqmap[irq].ht_source << 1));
666ca2c1931SNathan Whitehorn
667ca2c1931SNathan Whitehorn /* Unmask the interrupt */
668ca2c1931SNathan Whitehorn ht_irq = in32rb(cpcht_irqmap[irq].ht_base + 4);
669ca2c1931SNathan Whitehorn ht_irq &= ~HTAPIC_MASK;
670ca2c1931SNathan Whitehorn out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq);
671ca2c1931SNathan Whitehorn
672ca2c1931SNathan Whitehorn mtx_unlock_spin(&sc->sc_ht_mtx);
673ca2c1931SNathan Whitehorn }
674ca2c1931SNathan Whitehorn
67556505ec0SJustin Hibbits openpic_cpcht_eoi(dev, irq, *priv);
676ca2c1931SNathan Whitehorn }
677ca2c1931SNathan Whitehorn
678ca2c1931SNathan Whitehorn static void
openpic_cpcht_unmask(device_t dev,u_int irq,void * priv)67956505ec0SJustin Hibbits openpic_cpcht_unmask(device_t dev, u_int irq, void *priv)
680ca2c1931SNathan Whitehorn {
681ca2c1931SNathan Whitehorn struct openpic_cpcht_softc *sc;
682ca2c1931SNathan Whitehorn uint32_t ht_irq;
683ca2c1931SNathan Whitehorn
68456505ec0SJustin Hibbits openpic_unmask(dev, irq, priv);
685ca2c1931SNathan Whitehorn
686ca2c1931SNathan Whitehorn sc = device_get_softc(dev);
687ca2c1931SNathan Whitehorn
688ca2c1931SNathan Whitehorn if (cpcht_irqmap != NULL && irq < 128 &&
689ca2c1931SNathan Whitehorn cpcht_irqmap[irq].ht_base > 0) {
690ca2c1931SNathan Whitehorn mtx_lock_spin(&sc->sc_ht_mtx);
691ca2c1931SNathan Whitehorn
692ca2c1931SNathan Whitehorn /* Program the data port */
693ca2c1931SNathan Whitehorn out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
694ca2c1931SNathan Whitehorn 0x10 + (cpcht_irqmap[irq].ht_source << 1));
695ca2c1931SNathan Whitehorn
696ca2c1931SNathan Whitehorn /* Unmask the interrupt */
697ca2c1931SNathan Whitehorn ht_irq = in32rb(cpcht_irqmap[irq].ht_base + 4);
698ca2c1931SNathan Whitehorn ht_irq &= ~HTAPIC_MASK;
699ca2c1931SNathan Whitehorn out32rb(cpcht_irqmap[irq].ht_base + 4, ht_irq);
700ca2c1931SNathan Whitehorn
701ca2c1931SNathan Whitehorn mtx_unlock_spin(&sc->sc_ht_mtx);
702ca2c1931SNathan Whitehorn }
703ca2c1931SNathan Whitehorn
70456505ec0SJustin Hibbits openpic_cpcht_eoi(dev, irq, priv);
705ca2c1931SNathan Whitehorn }
706ca2c1931SNathan Whitehorn
707ca2c1931SNathan Whitehorn static void
openpic_cpcht_eoi(device_t dev,u_int irq,void * priv)70856505ec0SJustin Hibbits openpic_cpcht_eoi(device_t dev, u_int irq, void *priv)
709ca2c1931SNathan Whitehorn {
710ca2c1931SNathan Whitehorn struct openpic_cpcht_softc *sc;
711ca2c1931SNathan Whitehorn uint32_t off, mask;
712ca2c1931SNathan Whitehorn
713ca2c1931SNathan Whitehorn if (irq == 255)
714ca2c1931SNathan Whitehorn return;
715ca2c1931SNathan Whitehorn
716ca2c1931SNathan Whitehorn sc = device_get_softc(dev);
717ca2c1931SNathan Whitehorn
718ca2c1931SNathan Whitehorn if (cpcht_irqmap != NULL && irq < 128 &&
719ca2c1931SNathan Whitehorn cpcht_irqmap[irq].ht_base > 0 && !cpcht_irqmap[irq].edge) {
720ca2c1931SNathan Whitehorn /* If this is an HT IRQ, acknowledge it at the remote APIC */
721ca2c1931SNathan Whitehorn
722ca2c1931SNathan Whitehorn if (cpcht_irqmap[irq].apple_eoi) {
723ca2c1931SNathan Whitehorn off = (cpcht_irqmap[irq].ht_source >> 3) & ~3;
724ca2c1931SNathan Whitehorn mask = 1 << (cpcht_irqmap[irq].ht_source & 0x1f);
725ca2c1931SNathan Whitehorn out32rb(cpcht_irqmap[irq].apple_eoi + off, mask);
726ca2c1931SNathan Whitehorn } else {
727ca2c1931SNathan Whitehorn mtx_lock_spin(&sc->sc_ht_mtx);
728ca2c1931SNathan Whitehorn
729ca2c1931SNathan Whitehorn out8rb(cpcht_irqmap[irq].ht_base + PCIR_HT_COMMAND,
730ca2c1931SNathan Whitehorn 0x11 + (cpcht_irqmap[irq].ht_source << 1));
731ca2c1931SNathan Whitehorn out32rb(cpcht_irqmap[irq].ht_base + 4,
732ca2c1931SNathan Whitehorn cpcht_irqmap[irq].eoi_data);
733ca2c1931SNathan Whitehorn
734ca2c1931SNathan Whitehorn mtx_unlock_spin(&sc->sc_ht_mtx);
735ca2c1931SNathan Whitehorn }
736ca2c1931SNathan Whitehorn }
737ca2c1931SNathan Whitehorn
73856505ec0SJustin Hibbits openpic_eoi(dev, irq, priv);
7391c96bdd1SNathan Whitehorn }
740