Home | History | Annotate | Line # | Download | only in pci
pq3pci.c revision 1.2
      1  1.2  matt /*	$NetBSD: pq3pci.c,v 1.2 2011/01/18 01:02:53 matt Exp $	*/
      2  1.2  matt /*-
      3  1.2  matt  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4  1.2  matt  * All rights reserved.
      5  1.2  matt  *
      6  1.2  matt  * This code is derived from software contributed to The NetBSD Foundation
      7  1.2  matt  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8  1.2  matt  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9  1.2  matt  *
     10  1.2  matt  * This material is based upon work supported by the Defense Advanced Research
     11  1.2  matt  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12  1.2  matt  * Contract No. N66001-09-C-2073.
     13  1.2  matt  * Approved for Public Release, Distribution Unlimited
     14  1.2  matt  *
     15  1.2  matt  * Redistribution and use in source and binary forms, with or without
     16  1.2  matt  * modification, are permitted provided that the following conditions
     17  1.2  matt  * are met:
     18  1.2  matt  * 1. Redistributions of source code must retain the above copyright
     19  1.2  matt  *    notice, this list of conditions and the following disclaimer.
     20  1.2  matt  * 2. Redistributions in binary form must reproduce the above copyright
     21  1.2  matt  *    notice, this list of conditions and the following disclaimer in the
     22  1.2  matt  *    documentation and/or other materials provided with the distribution.
     23  1.2  matt  *
     24  1.2  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.2  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.2  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.2  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.2  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.2  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.2  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.2  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.2  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.2  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.2  matt  * POSSIBILITY OF SUCH DAMAGE.
     35  1.2  matt  */
     36  1.2  matt 
     37  1.2  matt #define	PCI_PRIVATE
     38  1.2  matt #define	GLOBAL_PRIVATE
     39  1.2  matt #define	__INTR_PRIVATE
     40  1.2  matt 
     41  1.2  matt #include "opt_mpc85xx.h"
     42  1.2  matt #include "opt_pci.h"
     43  1.2  matt #include "locators.h"
     44  1.2  matt 
     45  1.2  matt #include <sys/cdefs.h>
     46  1.2  matt 
     47  1.2  matt __KERNEL_RCSID(0, "$NetBSD: pq3pci.c,v 1.2 2011/01/18 01:02:53 matt Exp $");
     48  1.2  matt 
     49  1.2  matt #include <sys/param.h>
     50  1.2  matt #include <sys/device.h>
     51  1.2  matt #include <sys/cpu.h>
     52  1.2  matt #include <sys/intr.h>
     53  1.2  matt #include <sys/bus.h>
     54  1.2  matt #include <sys/extent.h>
     55  1.2  matt #include <sys/bitops.h>
     56  1.2  matt #include <sys/kmem.h>
     57  1.2  matt #include <sys/malloc.h>	/* for extent */
     58  1.2  matt 
     59  1.2  matt #include <dev/pci/pcireg.h>
     60  1.2  matt #include <dev/pci/pcivar.h>
     61  1.2  matt #include <dev/pci/pciconf.h>
     62  1.2  matt #include <dev/pci/pcidevs.h>
     63  1.2  matt 
     64  1.2  matt #include <powerpc/booke/cpuvar.h>
     65  1.2  matt #include <powerpc/booke/spr.h>
     66  1.2  matt #include <powerpc/booke/e500var.h>
     67  1.2  matt #include <powerpc/booke/e500reg.h>
     68  1.2  matt #include <powerpc/booke/openpicreg.h>
     69  1.2  matt 
     70  1.2  matt #define	PORDEVSR_MPC8536_TRUTH_ENCODE(inst, field, value, result) \
     71  1.2  matt     TRUTH_ENCODE(SVR_MPC8536v1, inst, PORDEVSR_##field, \
     72  1.2  matt 	__SHIFTIN(field##_##MPC8536##_##value, PORDEVSR_##field), result)
     73  1.2  matt #define	PORDEVSR_MPC8544_TRUTH_ENCODE(inst, field, value, result) \
     74  1.2  matt     TRUTH_ENCODE(SVR_MPC8544v1, inst, PORDEVSR_##field, \
     75  1.2  matt 	__SHIFTIN(field##_##MPC8544##_##value, PORDEVSR_##field), result)
     76  1.2  matt #define	PORDEVSR_MPC8548_TRUTH_ENCODE(inst, field, value, result) \
     77  1.2  matt     TRUTH_ENCODE(SVR_MPC8548v1, inst, PORDEVSR_##field, \
     78  1.2  matt 	__SHIFTIN(field##_##MPC8548##_##value, PORDEVSR_##field), result)
     79  1.2  matt #define	PORDEVSR_MPC8555_TRUTH_ENCODE(inst, field, value, result) \
     80  1.2  matt     TRUTH_ENCODE(SVR_MPC8555v1, inst, PORDEVSR_##field, \
     81  1.2  matt 	__SHIFTIN(field##_##MPC8555##_##value, PORDEVSR_##field), result)
     82  1.2  matt #define	PORDEVSR_MPC8572_TRUTH_ENCODE(inst, field, value, result) \
     83  1.2  matt     TRUTH_ENCODE(SVR_MPC8572v1, inst, PORDEVSR_##field, \
     84  1.2  matt 	__SHIFTIN(field##_##MPC8572##_##value, PORDEVSR_##field), result)
     85  1.2  matt 
     86  1.2  matt 
     87  1.2  matt #define	PORDEVSR_TRUTH_ENCODE(svr, inst, field, value, result) \
     88  1.2  matt     TRUTH_ENCODE(svr, inst, PORDEVSR_##field, \
     89  1.2  matt 	__SHIFTIN(field##_##value, PORDEVSR_##field), result)
     90  1.2  matt 
     91  1.2  matt const struct e500_truthtab pq3pci_pcie_lanes[] = {
     92  1.2  matt #ifdef MPC8548
     93  1.2  matt     PORDEVSR_MPC8548_TRUTH_ENCODE(0, IOSEL, SRIO2500_PCIE1_X4, 4),
     94  1.2  matt     PORDEVSR_MPC8548_TRUTH_ENCODE(0, IOSEL, SRIO1250_PCIE1_X4, 4),
     95  1.2  matt     PORDEVSR_MPC8548_TRUTH_ENCODE(0, IOSEL, PCIE1_X8, 8),
     96  1.2  matt #endif
     97  1.2  matt 
     98  1.2  matt #ifdef MPC8544
     99  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(1, IOSEL, PCIE1_ON, 4),
    100  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(1, IOSEL, PCIE1_SGMII_ON, 4),
    101  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(1, IOSEL, PCIE12_ON, 4),
    102  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(1, IOSEL, PCIE12_SGMII_ON, 4),
    103  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(1, IOSEL, PCIE123_ON, 4),
    104  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(1, IOSEL, PCIE123_SGMII_ON, 4),
    105  1.2  matt 
    106  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(2, IOSEL, PCIE12_ON, 4),
    107  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(2, IOSEL, PCIE12_SGMII_ON, 4),
    108  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(2, IOSEL, PCIE123_ON, 4),
    109  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(2, IOSEL, PCIE123_SGMII_ON, 4),
    110  1.2  matt 
    111  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(3, IOSEL, PCIE123_ON, 1),
    112  1.2  matt     PORDEVSR_MPC8544_TRUTH_ENCODE(3, IOSEL, PCIE123_SGMII_ON, 1),
    113  1.2  matt #endif
    114  1.2  matt 
    115  1.2  matt #ifdef MPC8536
    116  1.2  matt     PORDEVSR_MPC8536_TRUTH_ENCODE(1, IOSEL, PCIE1_X4, 4),
    117  1.2  matt     PORDEVSR_MPC8536_TRUTH_ENCODE(1, IOSEL, PCIE1_X8, 8),
    118  1.2  matt     PORDEVSR_MPC8536_TRUTH_ENCODE(1, IOSEL, PCIE12_X4, 4),
    119  1.2  matt     PORDEVSR_MPC8536_TRUTH_ENCODE(1, IOSEL, PCIE1_X4_PCI23_X2, 4),
    120  1.2  matt 
    121  1.2  matt     PORDEVSR_MPC8536_TRUTH_ENCODE(2, IOSEL, PCIE12_X4, 4),
    122  1.2  matt     PORDEVSR_MPC8536_TRUTH_ENCODE(2, IOSEL, PCIE1_X4_PCI23_X2, 2),
    123  1.2  matt 
    124  1.2  matt     PORDEVSR_MPC8536_TRUTH_ENCODE(3, IOSEL, PCIE1_X4_PCI23_X2, 2),
    125  1.2  matt #endif
    126  1.2  matt 
    127  1.2  matt #ifdef MPC8572
    128  1.2  matt     PORDEVSR_MPC8572_TRUTH_ENCODE(1, IOSEL, SRIO2500_PCIE1_X4, 4),
    129  1.2  matt     PORDEVSR_MPC8572_TRUTH_ENCODE(1, IOSEL, SRIO1250_PCIE1_X4, 4),
    130  1.2  matt     PORDEVSR_MPC8572_TRUTH_ENCODE(1, IOSEL, PCIE1_X4, 4),
    131  1.2  matt     PORDEVSR_MPC8572_TRUTH_ENCODE(1, IOSEL, PCIE12_X4, 4),
    132  1.2  matt     PORDEVSR_MPC8572_TRUTH_ENCODE(1, IOSEL, PCIE1_X4_23_X2, 4),
    133  1.2  matt     PORDEVSR_MPC8572_TRUTH_ENCODE(1, IOSEL, PCIE1_X8, 8),
    134  1.2  matt 
    135  1.2  matt     PORDEVSR_MPC8572_TRUTH_ENCODE(2, IOSEL, PCIE12_X4, 4),
    136  1.2  matt     PORDEVSR_MPC8572_TRUTH_ENCODE(2, IOSEL, PCIE1_X4_23_X2, 2),
    137  1.2  matt 
    138  1.2  matt     PORDEVSR_MPC8572_TRUTH_ENCODE(3, IOSEL, PCIE1_X4_23_X2, 2),
    139  1.2  matt #endif
    140  1.2  matt };
    141  1.2  matt 
    142  1.2  matt static const struct e500_truthtab pq3pci_pci_pcix[] = {
    143  1.2  matt #ifdef MPC8548
    144  1.2  matt     PORDEVSR_TRUTH_ENCODE(SVR_MPC8548v1, 1, PCI1, PCIX, 1),
    145  1.2  matt #endif
    146  1.2  matt };
    147  1.2  matt 
    148  1.2  matt static const struct e500_truthtab pq3pci_pci_pci32[] = {
    149  1.2  matt #ifdef MPC8548
    150  1.2  matt     PORDEVSR_TRUTH_ENCODE(SVR_MPC8548v1, 1, PCI32, FALSE, 64),
    151  1.2  matt     PORDEVSR_TRUTH_ENCODE(SVR_MPC8548v1, 1, PCI32, TRUE, 32),
    152  1.2  matt #endif
    153  1.2  matt 
    154  1.2  matt #ifdef MPC8555
    155  1.2  matt     PORDEVSR_TRUTH_ENCODE(SVR_MPC8555v1, 0, PCI32, FALSE, 64),
    156  1.2  matt     PORDEVSR_TRUTH_ENCODE(SVR_MPC8555v1, 0, PCI32, TRUE, 32),
    157  1.2  matt #endif
    158  1.2  matt };
    159  1.2  matt 
    160  1.2  matt struct pq3pci_bst {
    161  1.2  matt 	struct powerpc_bus_space bs_tag;
    162  1.2  matt 	char bs_name[16];
    163  1.2  matt 	char bs_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8)] __aligned(8);
    164  1.2  matt };
    165  1.2  matt 
    166  1.2  matt typedef enum { IH_NONE, IH_INTX, IH_MSI, IH_MSIX } pq3pci_intr_class_t;
    167  1.2  matt 
    168  1.2  matt struct pq3pci_genihand {
    169  1.2  matt 	pq3pci_intr_class_t ih_class;
    170  1.2  matt 	int (*ih_func)(void *);
    171  1.2  matt 	void *ih_arg;
    172  1.2  matt 	struct pq3pci_softc *ih_sc;
    173  1.2  matt };
    174  1.2  matt 
    175  1.2  matt struct pq3pci_intrhand {
    176  1.2  matt 	struct pq3pci_genihand pih_ih;
    177  1.2  matt 	SIMPLEQ_ENTRY(pq3pci_intrhand) pih_link;
    178  1.2  matt 	int pih_ipl;
    179  1.2  matt 	struct pq3pci_intrsource *pih_source;
    180  1.2  matt 	uint64_t pih_count;
    181  1.2  matt };
    182  1.2  matt 
    183  1.2  matt struct pq3pci_callhand {
    184  1.2  matt 	struct pq3pci_genihand pch_ih;
    185  1.2  matt 	struct callout pch_callout;
    186  1.2  matt 	int pch_ipl;
    187  1.2  matt };
    188  1.2  matt 
    189  1.2  matt #define	PIH_MAKE(irq, ist, nmsi) (((nmsi) << 20) | ((irq) << 8) | (ist))
    190  1.2  matt #define	PIH_IST(pih)		(((pih) >> 0) & 0xff)
    191  1.2  matt #define	PIH_IRQ(pih)		(((pih) >> 8) & 0xfff)
    192  1.2  matt #define	PIH_NMSI(pih)		(((pih) >> 20) & 0xff)
    193  1.2  matt 
    194  1.2  matt struct pq3pci_intrsource {
    195  1.2  matt 	SIMPLEQ_ENTRY(pq3pci_intrsource) pis_link;
    196  1.2  matt 	SIMPLEQ_HEAD(,pq3pci_intrhand) pis_ihands;
    197  1.2  matt 	struct evcnt pis_ev;
    198  1.2  matt 	struct evcnt pis_ev_spurious;
    199  1.2  matt 	kmutex_t *pis_lock;
    200  1.2  matt 	pci_intr_handle_t pis_handle;
    201  1.2  matt 	void *pis_ih;
    202  1.2  matt };
    203  1.2  matt 
    204  1.2  matt struct pq3pci_msihand {
    205  1.2  matt 	struct pq3pci_genihand msih_ih;
    206  1.2  matt 	struct pq3pci_msigroup *msih_group;
    207  1.2  matt 	struct evcnt msih_ev;
    208  1.2  matt 	struct evcnt msih_ev_spurious;
    209  1.2  matt 	pcitag_t msih_tag;
    210  1.2  matt 	int msih_msioff;
    211  1.2  matt };
    212  1.2  matt 
    213  1.2  matt struct pq3pci_msigroup {
    214  1.2  matt 	kmutex_t *msig_lock;
    215  1.2  matt 	void *msig_ih;
    216  1.2  matt 	uint32_t msig_free_mask;
    217  1.2  matt 	int msig_ipl;
    218  1.2  matt 	u_int msig_group;
    219  1.2  matt 	bus_size_t msig_msir;
    220  1.2  matt 	struct pq3pci_msihand msig_ihands[32];
    221  1.2  matt };
    222  1.2  matt 
    223  1.2  matt struct pq3pci_softc {
    224  1.2  matt 	device_t sc_dev;
    225  1.2  matt 	bus_space_tag_t sc_bst;
    226  1.2  matt 	bus_space_handle_t sc_bsh;
    227  1.2  matt 	void *sc_ih;
    228  1.2  matt 	bool sc_pcie;
    229  1.2  matt 	struct genppc_pci_chipset sc_pc;
    230  1.2  matt 	struct pq3pci_bst sc_pci_io_bst;
    231  1.2  matt 	struct pq3pci_bst sc_pci_mem_bst;
    232  1.2  matt 	u_int sc_pba_flags;
    233  1.2  matt 	kmutex_t *sc_conf_lock;
    234  1.2  matt 	kmutex_t *sc_intr_lock;
    235  1.2  matt 	struct evcnt sc_ev_spurious;
    236  1.2  matt 	prop_dictionary_t sc_intrmap;
    237  1.2  matt 	uint32_t sc_intrmask;
    238  1.2  matt };
    239  1.2  matt 
    240  1.2  matt static int pq3pci_cpunode_match(device_t, cfdata_t, void *aux);
    241  1.2  matt static void pq3pci_cpunode_attach(device_t, device_t, void *aux);
    242  1.2  matt static pci_chipset_tag_t pq3pci_pci_chipset_init(struct pq3pci_softc *);
    243  1.2  matt 
    244  1.2  matt static SIMPLEQ_HEAD(,pq3pci_intrsource) pq3pci_intrsources
    245  1.2  matt     = SIMPLEQ_HEAD_INITIALIZER(pq3pci_intrsources);
    246  1.2  matt static struct pq3pci_msigroup *pq3pci_msigroups[8];
    247  1.2  matt 
    248  1.2  matt static struct pq3pci_intrsource *
    249  1.2  matt 	pq3pci_intr_source_lookup(struct pq3pci_softc *, pci_intr_handle_t);
    250  1.2  matt 
    251  1.2  matt static const char msi_intr_names[8][32][8] = {
    252  1.2  matt     {
    253  1.2  matt 	"msi 0",   "msi 1",   "msi 2",   "msi 3",
    254  1.2  matt 	"msi 4",   "msi 5",   "msi 6",   "msi 7",
    255  1.2  matt 	"msi 8",   "msi 9",   "msi 10",  "msi 11",
    256  1.2  matt 	"msi 12",  "msi 13",  "msi 14",  "msi 15",
    257  1.2  matt 	"msi 16",  "msi 17",  "msi 18",  "msi 19",
    258  1.2  matt 	"msi 20",  "msi 21",  "msi 22",  "msi 23",
    259  1.2  matt 	"msi 24",  "msi 25",  "msi 26",  "msi 27",
    260  1.2  matt 	"msi 28",  "msi 29",  "msi 30",  "msi 31",
    261  1.2  matt     }, {
    262  1.2  matt 	"msi 32",  "msi 33",  "msi 34",  "msi 35",
    263  1.2  matt 	"msi 36",  "msi 37",  "msi 38",  "msi 39",
    264  1.2  matt 	"msi 40",  "msi 41",  "msi 42",  "msi 43",
    265  1.2  matt 	"msi 44",  "msi 45",  "msi 46",  "msi 47",
    266  1.2  matt 	"msi 48",  "msi 49",  "msi 50",  "msi 51",
    267  1.2  matt 	"msi 52",  "msi 53",  "msi 54",  "msi 55",
    268  1.2  matt 	"msi 56",  "msi 57",  "msi 58",  "msi 59",
    269  1.2  matt 	"msi 60",  "msi 61",  "msi 62",  "msi 63",
    270  1.2  matt     }, {
    271  1.2  matt 	"msi 64",  "msi 65",  "msi 66",  "msi 67",
    272  1.2  matt 	"msi 68",  "msi 69",  "msi 70",  "msi 71",
    273  1.2  matt 	"msi 72",  "msi 73",  "msi 74",  "msi 75",
    274  1.2  matt 	"msi 76",  "msi 77",  "msi 78",  "msi 79",
    275  1.2  matt 	"msi 80",  "msi 81",  "msi 82",  "msi 83",
    276  1.2  matt 	"msi 84",  "msi 85",  "msi 86",  "msi 87",
    277  1.2  matt 	"msi 88",  "msi 89",  "msi 90",  "msi 91",
    278  1.2  matt 	"msi 92",  "msi 93",  "msi 94",  "msi 95",
    279  1.2  matt     }, {
    280  1.2  matt 	"msi 96",  "msi 97",  "msi 98",  "msi 99",
    281  1.2  matt 	"msi 100", "msi 101", "msi 102", "msi 103",
    282  1.2  matt 	"msi 104", "msi 105", "msi 106", "msi 107",
    283  1.2  matt 	"msi 108", "msi 109", "msi 110", "msi 111",
    284  1.2  matt 	"msi 112", "msi 113", "msi 114", "msi 115",
    285  1.2  matt 	"msi 116", "msi 117", "msi 118", "msi 119",
    286  1.2  matt 	"msi 120", "msi 121", "msi 122", "msi 123",
    287  1.2  matt 	"msi 124", "msi 125", "msi 126", "msi 127",
    288  1.2  matt     }, {
    289  1.2  matt 	"msi 128", "msi 129", "msi 130", "msi 131",
    290  1.2  matt 	"msi 132", "msi 133", "msi 134", "msi 135",
    291  1.2  matt 	"msi 136", "msi 137", "msi 138", "msi 139",
    292  1.2  matt 	"msi 140", "msi 141", "msi 142", "msi 143",
    293  1.2  matt 	"msi 144", "msi 145", "msi 146", "msi 147",
    294  1.2  matt 	"msi 148", "msi 149", "msi 150", "msi 151",
    295  1.2  matt 	"msi 152", "msi 153", "msi 154", "msi 155",
    296  1.2  matt 	"msi 156", "msi 157", "msi 158", "msi 159",
    297  1.2  matt     }, {
    298  1.2  matt 	"msi 160", "msi 161", "msi 162", "msi 163",
    299  1.2  matt 	"msi 164", "msi 165", "msi 166", "msi 167",
    300  1.2  matt 	"msi 168", "msi 169", "msi 170", "msi 171",
    301  1.2  matt 	"msi 172", "msi 173", "msi 174", "msi 175",
    302  1.2  matt 	"msi 176", "msi 177", "msi 178", "msi 179",
    303  1.2  matt 	"msi 180", "msi 181", "msi 182", "msi 183",
    304  1.2  matt 	"msi 184", "msi 185", "msi 186", "msi 187",
    305  1.2  matt 	"msi 188", "msi 189", "msi 190", "msi 191",
    306  1.2  matt     }, {
    307  1.2  matt 	"msi 192", "msi 193", "msi 194", "msi 195",
    308  1.2  matt 	"msi 196", "msi 197", "msi 198", "msi 199",
    309  1.2  matt 	"msi 200", "msi 201", "msi 202", "msi 203",
    310  1.2  matt 	"msi 204", "msi 205", "msi 206", "msi 207",
    311  1.2  matt 	"msi 208", "msi 209", "msi 210", "msi 211",
    312  1.2  matt 	"msi 212", "msi 213", "msi 214", "msi 215",
    313  1.2  matt 	"msi 216", "msi 217", "msi 218", "msi 219",
    314  1.2  matt 	"msi 220", "msi 221", "msi 222", "msi 223",
    315  1.2  matt     }, {
    316  1.2  matt 	"msi 224", "msi 225", "msi 226", "msi 227",
    317  1.2  matt 	"msi 228", "msi 229", "msi 230", "msi 231",
    318  1.2  matt 	"msi 232", "msi 233", "msi 234", "msi 235",
    319  1.2  matt 	"msi 236", "msi 237", "msi 238", "msi 239",
    320  1.2  matt 	"msi 240", "msi 241", "msi 242", "msi 243",
    321  1.2  matt 	"msi 244", "msi 245", "msi 246", "msi 247",
    322  1.2  matt 	"msi 248", "msi 249", "msi 250", "msi 251",
    323  1.2  matt 	"msi 252", "msi 253", "msi 254", "msi 255",
    324  1.2  matt     },
    325  1.2  matt };
    326  1.2  matt 
    327  1.2  matt CFATTACH_DECL_NEW(pq3pci_cpunode, sizeof(struct pq3pci_softc),
    328  1.2  matt     pq3pci_cpunode_match, pq3pci_cpunode_attach, NULL, NULL);
    329  1.2  matt 
    330  1.2  matt CFATTACH_DECL_NEW(pq3pcie_cpunode, sizeof(struct pq3pci_softc),
    331  1.2  matt     pq3pci_cpunode_match, pq3pci_cpunode_attach, NULL, NULL);
    332  1.2  matt 
    333  1.2  matt int
    334  1.2  matt pq3pci_cpunode_match(device_t parent, cfdata_t cf, void *aux)
    335  1.2  matt {
    336  1.2  matt 
    337  1.2  matt 	if (!e500_cpunode_submatch(parent, cf, cf->cf_name + 3, aux))
    338  1.2  matt 		return 0;
    339  1.2  matt 
    340  1.2  matt 	return 1;
    341  1.2  matt }
    342  1.2  matt 
    343  1.2  matt struct pq3pci_owin {
    344  1.2  matt 	uint32_t potar;
    345  1.2  matt 	uint32_t potear;
    346  1.2  matt 	uint32_t powbar;
    347  1.2  matt 	uint32_t powar;
    348  1.2  matt };
    349  1.2  matt 
    350  1.2  matt static bool
    351  1.2  matt pq3pci_owin_setup(struct pq3pci_softc *sc, u_int winnum,
    352  1.2  matt 	const struct pq3pci_owin *owin)
    353  1.2  matt {
    354  1.2  matt 	const bool io_win = (owin->powar & PEXOWAR_RTT) == PEXOWAR_RTT_IO;
    355  1.2  matt 	struct pq3pci_bst *bs = io_win ? &sc->sc_pci_io_bst : &sc->sc_pci_mem_bst;
    356  1.2  matt 	const uint64_t pci_base = ((uint64_t)owin->potar << 12)
    357  1.2  matt 	    | ((uint64_t)owin->potear << (32+12));
    358  1.2  matt 	const uint64_t local_base = (uint64_t)owin->powbar << 12;
    359  1.2  matt 	const u_int win_size_log2 = PEXIWAR_IWS_GET(owin->powar) + 1;
    360  1.2  matt 
    361  1.2  matt 	bs->bs_tag.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN
    362  1.2  matt 	    | (io_win ? _BUS_SPACE_IO_TYPE : _BUS_SPACE_MEM_TYPE);
    363  1.2  matt 	bs->bs_tag.pbs_base = pci_base;
    364  1.2  matt 	bs->bs_tag.pbs_offset = local_base - pci_base;
    365  1.2  matt 	bs->bs_tag.pbs_limit = bs->bs_tag.pbs_base + (1ULL << win_size_log2);
    366  1.2  matt 
    367  1.2  matt #if 0
    368  1.2  matt 	const char units[] = " KMGTP";
    369  1.2  matt 	aprint_normal_dev(sc->sc_dev,
    370  1.2  matt 	     "outbound window %u: potar=%#x, potear=%#x, powbar=%x, powar=%#x\n",
    371  1.2  matt 	     winnum, owin->potar, owin->potear, owin->powbar, owin->powar);
    372  1.2  matt 	aprint_normal_dev(sc->sc_dev,
    373  1.2  matt 	    "outbound window %u: maps %u%cB of PCI %s space @ %#"PRIx64" onto local addresses @ %#"PRIx64".\n",
    374  1.2  matt 	    winnum, 1 << (win_size_log2 % 10), units[win_size_log2 / 10],
    375  1.2  matt 	    (owin->powar & PEXOWAR_RTT) == PEXOWAR_RTT_IO ? "I/O" : "memory",
    376  1.2  matt 	    local_base, pci_base);
    377  1.2  matt #endif
    378  1.2  matt 
    379  1.2  matt 	snprintf(bs->bs_name, sizeof(bs->bs_name), "%s-%s@%u",
    380  1.2  matt 	    device_xname(sc->sc_dev), io_win ? "io" : "mem", winnum);
    381  1.2  matt 
    382  1.2  matt #if 0
    383  1.2  matt 	printf("%s: %s: base=%#x offset=%#x limit=%#x\n", __func__, bs->bs_name,
    384  1.2  matt 	    bs->bs_tag.pbs_base, bs->bs_tag.pbs_offset, bs->bs_tag.pbs_limit);
    385  1.2  matt #endif
    386  1.2  matt 
    387  1.2  matt 	int error = bus_space_init(&bs->bs_tag, bs->bs_name,
    388  1.2  matt 	    bs->bs_ex_storage, sizeof(bs->bs_ex_storage));
    389  1.2  matt 	if (error) {
    390  1.2  matt 		aprint_error(": failed to create %s bus space: %d\n",
    391  1.2  matt 		    bs->bs_name, error);
    392  1.2  matt 		return false;
    393  1.2  matt 	}
    394  1.2  matt 	aprint_debug_dev(sc->sc_dev, "bus space %s created\n", bs->bs_name);
    395  1.2  matt 	sc->sc_pba_flags |=
    396  1.2  matt 	    io_win ? PCI_FLAGS_IO_ENABLED : PCI_FLAGS_MEM_ENABLED;
    397  1.2  matt 	return true;
    398  1.2  matt }
    399  1.2  matt 
    400  1.2  matt struct pq3pci_iwin {
    401  1.2  matt 	uint32_t pitar;
    402  1.2  matt 	uint32_t piwbar;
    403  1.2  matt 	uint32_t piwbear;
    404  1.2  matt 	uint32_t piwar;
    405  1.2  matt };
    406  1.2  matt 
    407  1.2  matt static bool
    408  1.2  matt pq3pci_iwin_setup(struct pq3pci_softc *sc, u_int winnum,
    409  1.2  matt 	const struct pq3pci_iwin *iwin)
    410  1.2  matt {
    411  1.2  matt 	const uint64_t pci_base = ((uint64_t)iwin->piwbar << 12)
    412  1.2  matt 	    | ((uint64_t)iwin->piwbear << (32+12));
    413  1.2  matt 	const uint64_t local_base = (uint64_t)iwin->pitar << 12;
    414  1.2  matt 	const u_int win_size_log2 = PEXIWAR_IWS_GET(iwin->piwar) + 1;
    415  1.2  matt #if DEBUG > 1
    416  1.2  matt 	const char units[] = " KMGTP";
    417  1.2  matt 	aprint_normal_dev(sc->sc_dev,
    418  1.2  matt 	    "inbound window %u: pitar=%#x, piwbar=%x, piwbear=%#x, piwar=%#x\n",
    419  1.2  matt 	    winnum, iwin->pitar, iwin->piwbar, iwin->piwbear, iwin->piwar);
    420  1.2  matt 	aprint_normal_dev(sc->sc_dev,
    421  1.2  matt 	    "inbound window %u: maps %u%cB of PCI address space @ %#"PRIx64" to local memory @ %#"PRIx64".\n",
    422  1.2  matt 	    winnum, 1 << (win_size_log2 % 10), units[win_size_log2 / 10],
    423  1.2  matt 	    pci_base, local_base);
    424  1.2  matt #endif /* DEBUG */
    425  1.2  matt 	/*
    426  1.2  matt 	 * Let's make sure this window is usable.
    427  1.2  matt 	 */
    428  1.2  matt 	if (pci_base != 0) {
    429  1.2  matt 		aprint_error(": invalid inbound window: "
    430  1.2  matt 		    "PCI base (%#"PRIx64" != 0\n", pci_base);
    431  1.2  matt 		return false;
    432  1.2  matt 	}
    433  1.2  matt 	if (local_base != 0) {
    434  1.2  matt 		aprint_error(": invalid inbound window: "
    435  1.2  matt 		    "local base (%#"PRIx64" != 0\n", local_base);
    436  1.2  matt 		return false;
    437  1.2  matt 	}
    438  1.2  matt 	if ((iwin->piwar & PEXIWAR_RTT) != PEXIWAR_RTT_MEM_SNOOP) {
    439  1.2  matt 		aprint_error(": invalid inbound window: "
    440  1.2  matt 		    "unsupported read transaction type (%#"PRIxMAX")\n",
    441  1.2  matt 		    iwin->piwar & PEXIWAR_RTT);
    442  1.2  matt 		return false;
    443  1.2  matt 	}
    444  1.2  matt 	if ((iwin->piwar & PEXIWAR_WTT) != PEXIWAR_WTT_MEM_SNOOP) {
    445  1.2  matt 		aprint_error(": invalid inbound window: "
    446  1.2  matt 		    "unsupported write transaction type (%#"PRIxMAX")\n",
    447  1.2  matt 		    iwin->piwar & PEXIWAR_WTT);
    448  1.2  matt 		return false;
    449  1.2  matt 	}
    450  1.2  matt 	if ((iwin->piwar & PEXIWAR_TRGT) != PEXIWAR_TRGT_LOCALMEM) {
    451  1.2  matt 		aprint_error(": invalid inbound window: "
    452  1.2  matt 		    "unsupported target (%#"PRIxMAX")\n",
    453  1.2  matt 		    iwin->piwar & PEXIWAR_TRGT);
    454  1.2  matt 		return false;
    455  1.2  matt 	}
    456  1.2  matt 	if (board_info_get_number("mem-size") > (1ULL << win_size_log2)) {
    457  1.2  matt 		aprint_error(": invalid inbound window: "
    458  1.2  matt 		    "doesn't map all of memory (%#"PRIx64" < %#"PRIx64")\n",
    459  1.2  matt 		    1ULL << win_size_log2, board_info_get_number("mem-size"));
    460  1.2  matt 		return false;
    461  1.2  matt 	}
    462  1.2  matt 	return true;
    463  1.2  matt }
    464  1.2  matt 
    465  1.2  matt static void
    466  1.2  matt pq3pci_pch_callout(void *v)
    467  1.2  matt {
    468  1.2  matt 	struct pq3pci_callhand * const pch = v;
    469  1.2  matt 
    470  1.2  matt 	int s = splraise(pch->pch_ipl);
    471  1.2  matt 	(*pch->pch_ih.ih_func)(pch->pch_ih.ih_arg);
    472  1.2  matt 	splx(s);
    473  1.2  matt 	callout_schedule(&pch->pch_callout, 1);
    474  1.2  matt }
    475  1.2  matt 
    476  1.2  matt static int
    477  1.2  matt pq3pci_msi_spurious_intr(void *v)
    478  1.2  matt {
    479  1.2  matt 	(void) v;
    480  1.2  matt 
    481  1.2  matt 	return 0;
    482  1.2  matt }
    483  1.2  matt 
    484  1.2  matt static int
    485  1.2  matt pq3pci_msi_intr(void *v)
    486  1.2  matt {
    487  1.2  matt 	struct pq3pci_msigroup * const msig = v;
    488  1.2  matt 
    489  1.2  matt 	mutex_spin_enter(msig->msig_lock);
    490  1.2  matt 	KASSERT(curcpu()->ci_cpl == msig->msig_ipl);
    491  1.2  matt 	//KASSERT(curcpu()->ci_idepth == 0);
    492  1.2  matt 	for (int rv = 0;;) {
    493  1.2  matt 		uint32_t group = cpu_read_4(msig->msig_msir);
    494  1.2  matt 		if (group == 0) {
    495  1.2  matt 			mutex_spin_exit(msig->msig_lock);
    496  1.2  matt 			return rv;
    497  1.2  matt 		}
    498  1.2  matt 
    499  1.2  matt 		const bool working_msi_p =
    500  1.2  matt 		    msig->msig_group != 0 || (group & 1) == 0;
    501  1.2  matt 		if (working_msi_p) {
    502  1.2  matt 			/*
    503  1.2  matt 			 * if MSIs are working, just clear the free MSIs.
    504  1.2  matt 			 */
    505  1.2  matt 			group &= ~msig->msig_free_mask;
    506  1.2  matt 		} else {
    507  1.2  matt 			/*
    508  1.2  matt 			 * If MSIs are broken, we don't really what MSIs
    509  1.2  matt 			 * have happened.
    510  1.2  matt 			 */
    511  1.2  matt 			for (struct pq3pci_msihand *msih = msig->msig_ihands + 31;
    512  1.2  matt 			     group != 0;
    513  1.2  matt 			     msih--) {
    514  1.2  matt 				const u_int n = __builtin_clz(group);
    515  1.2  matt 				msih -= n;
    516  1.2  matt 				group <<= n + 1;
    517  1.2  matt 				msih->msih_ev.ev_count++;
    518  1.2  matt 			}
    519  1.2  matt 			group = ~msig->msig_free_mask;
    520  1.2  matt 		}
    521  1.2  matt 		for (struct pq3pci_msihand *msih = msig->msig_ihands + 31;
    522  1.2  matt 		     group != 0;
    523  1.2  matt 		     msih--) {
    524  1.2  matt 			KASSERT(msig->msig_ihands <= msih);
    525  1.2  matt 			KASSERT(msih < &msig->msig_ihands[32]);
    526  1.2  matt 			const u_int n = __builtin_clz(group);
    527  1.2  matt 			msih -= n;
    528  1.2  matt 			group <<= n + 1;
    529  1.2  matt 			msih->msih_ev.ev_count += working_msi_p;
    530  1.2  matt 			if ((*msih->msih_ih.ih_func)(msih->msih_ih.ih_arg)) {
    531  1.2  matt 				rv = 1;
    532  1.2  matt 				msih->msih_ev.ev_count += !working_msi_p;
    533  1.2  matt 			} else {
    534  1.2  matt 				msih->msih_ev_spurious.ev_count += working_msi_p;
    535  1.2  matt 			}
    536  1.2  matt 		}
    537  1.2  matt 	}
    538  1.2  matt }
    539  1.2  matt 
    540  1.2  matt static int
    541  1.2  matt pq3pci_onchip_intr(void *v)
    542  1.2  matt {
    543  1.2  matt 	panic(__func__);
    544  1.2  matt }
    545  1.2  matt 
    546  1.2  matt static int
    547  1.2  matt pq3pci_pis_intr(void *v)
    548  1.2  matt {
    549  1.2  matt 	struct pq3pci_intrsource * const pis = v;
    550  1.2  matt 	struct pq3pci_intrhand *pih;
    551  1.2  matt 	int rv = 0;
    552  1.2  matt 
    553  1.2  matt 	mutex_spin_enter(pis->pis_lock);
    554  1.2  matt 	pis->pis_ev.ev_count++;
    555  1.2  matt 	SIMPLEQ_FOREACH(pih, &pis->pis_ihands, pih_link) {
    556  1.2  matt 		struct pq3pci_softc * const sc = pih->pih_ih.ih_sc;
    557  1.2  matt 		int s = splraise(pih->pih_ipl);
    558  1.2  matt 		pih->pih_count++;
    559  1.2  matt 		rv = (*pih->pih_ih.ih_func)(pih->pih_ih.ih_arg);
    560  1.2  matt 		splx(s);
    561  1.2  matt #if 0
    562  1.2  matt 		printf("%s %d:%s %"PRIu64": %p(%p) %"PRIu64": %d\n", __func__,
    563  1.2  matt 		    curcpu()->ci_idepth,
    564  1.2  matt 		    pis->pis_ev.ev_group, pis->pis_ev.ev_count,
    565  1.2  matt 		    pih->pih_ih.ih_func, pih->pih_ih.ih_arg, pih->pih_count, rv);
    566  1.2  matt #endif
    567  1.2  matt 		if (rv != 0) {
    568  1.2  matt 			bus_space_read_4(sc->sc_bst, sc->sc_bsh, PCI_INT_ACK);
    569  1.2  matt 			break;
    570  1.2  matt 		}
    571  1.2  matt 		pih->pih_count--;
    572  1.2  matt 	}
    573  1.2  matt 	if (rv == 0)
    574  1.2  matt 		pis->pis_ev_spurious.ev_count++;
    575  1.2  matt 	mutex_spin_exit(pis->pis_lock);
    576  1.2  matt 	return rv;
    577  1.2  matt }
    578  1.2  matt 
    579  1.2  matt static void
    580  1.2  matt pq3pci_intr_source_setup(struct pq3pci_softc *sc,
    581  1.2  matt 	struct pq3pci_intrsource *pis, pci_intr_handle_t handle)
    582  1.2  matt {
    583  1.2  matt 	SIMPLEQ_INIT(&pis->pis_ihands);
    584  1.2  matt 	pis->pis_handle = handle;
    585  1.2  matt 	pis->pis_ih = intr_establish(PIH_IRQ(handle), IPL_VM, PIH_IST(handle),
    586  1.2  matt 	    pq3pci_pis_intr, pis);
    587  1.2  matt 	pis->pis_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
    588  1.2  matt 	const char * const intrstr
    589  1.2  matt 	    = intr_string(PIH_IRQ(handle), PIH_IST(handle));
    590  1.2  matt 	evcnt_attach_dynamic(&pis->pis_ev, EVCNT_TYPE_INTR,
    591  1.2  matt 	    NULL, intrstr, "intr");
    592  1.2  matt 	evcnt_attach_dynamic(&pis->pis_ev_spurious, EVCNT_TYPE_INTR,
    593  1.2  matt 	    &pis->pis_ev, intrstr, "spurious intr");
    594  1.2  matt 	SIMPLEQ_INSERT_TAIL(&pq3pci_intrsources, pis, pis_link);
    595  1.2  matt }
    596  1.2  matt 
    597  1.2  matt static bool
    598  1.2  matt pq3pci_intrmap_setup(struct pq3pci_softc *sc,
    599  1.2  matt 	const struct cpunode_locators *cnl)
    600  1.2  matt {
    601  1.2  matt 	char prop_name[32];
    602  1.2  matt 	snprintf(prop_name, sizeof(prop_name), "%s%u-interrupt-map",
    603  1.2  matt 	    cnl->cnl_name, cnl->cnl_instance);
    604  1.2  matt 	sc->sc_intrmap = board_info_get_object(prop_name);
    605  1.2  matt 	if (sc->sc_intrmap == NULL) {
    606  1.2  matt 		aprint_error(": missing %s board property", prop_name);
    607  1.2  matt 		return false;
    608  1.2  matt 	}
    609  1.2  matt 
    610  1.2  matt 	KASSERT(prop_object_type(sc->sc_intrmap) == PROP_TYPE_DICTIONARY);
    611  1.2  matt 	prop_number_t pn = prop_dictionary_get(sc->sc_intrmap, "interrupt-mask");
    612  1.2  matt 	KASSERT(pn != NULL);
    613  1.2  matt 
    614  1.2  matt 	sc->sc_intrmask = prop_number_unsigned_integer_value(pn);
    615  1.2  matt 
    616  1.2  matt 	sc->sc_ih = intr_establish(cnl->cnl_intrs[0], IPL_VM, IST_ONCHIP,
    617  1.2  matt 	    pq3pci_onchip_intr, sc);
    618  1.2  matt 	if (sc->sc_ih == NULL)
    619  1.2  matt 		panic("%s: failed to establish interrupt %d\n",
    620  1.2  matt 		    device_xname(sc->sc_dev), cnl->cnl_intrs[0]);
    621  1.2  matt 
    622  1.2  matt 	return true;
    623  1.2  matt }
    624  1.2  matt 
    625  1.2  matt void
    626  1.2  matt pq3pci_cpunode_attach(device_t parent, device_t self, void *aux)
    627  1.2  matt {
    628  1.2  matt 	struct cpunode_softc * const psc = device_private(parent);
    629  1.2  matt 	struct pq3pci_softc * const sc = device_private(self);
    630  1.2  matt 	struct cpunode_attach_args * const cna = aux;
    631  1.2  matt 	struct cpunode_locators * const cnl = &cna->cna_locs;
    632  1.2  matt 	char buf[32];
    633  1.2  matt 
    634  1.2  matt 	sc->sc_dev = self;
    635  1.2  matt 	sc->sc_bst = cna->cna_memt;
    636  1.2  matt 	psc->sc_children |= cna->cna_childmask;
    637  1.2  matt 	sc->sc_pcie = strcmp(cnl->cnl_name, "pcie") == 0;
    638  1.2  matt 
    639  1.2  matt 	const uint32_t pordevsr = cpu_read_4(GLOBAL_BASE + PORDEVSR);
    640  1.2  matt 	if (sc->sc_pcie) {
    641  1.2  matt 		u_int lanes = e500_truth_decode(cnl->cnl_instance, pordevsr,
    642  1.2  matt 		    pq3pci_pcie_lanes, __arraycount(pq3pci_pcie_lanes), 0);
    643  1.2  matt 		if (lanes == 0) {
    644  1.2  matt 			aprint_normal(": disabled\n");
    645  1.2  matt 			return;
    646  1.2  matt 		}
    647  1.2  matt 		snprintf(buf, sizeof(buf), "PCI-Express x%u", lanes);
    648  1.2  matt 	} else {
    649  1.2  matt 		bool pcix_p = e500_truth_decode(cnl->cnl_instance, pordevsr,
    650  1.2  matt 		    pq3pci_pci_pcix, __arraycount(pq3pci_pci_pcix), 0);
    651  1.2  matt 		u_int width = e500_truth_decode(cnl->cnl_instance, pordevsr,
    652  1.2  matt 		    pq3pci_pci_pci32, __arraycount(pq3pci_pci_pci32), 32);
    653  1.2  matt 		snprintf(buf, sizeof(buf), "%u-bit PCI%s",
    654  1.2  matt 		    width, (pcix_p ? "X" : ""));
    655  1.2  matt 	}
    656  1.2  matt 
    657  1.2  matt 	if (!pq3pci_intrmap_setup(sc, cnl))
    658  1.2  matt 		return;
    659  1.2  matt 
    660  1.2  matt 	evcnt_attach_dynamic(&sc->sc_ev_spurious, EVCNT_TYPE_INTR, NULL,
    661  1.2  matt 	    device_xname(self), "spurious intr");
    662  1.2  matt 
    663  1.2  matt 	int error = bus_space_map(sc->sc_bst, cnl->cnl_addr, cnl->cnl_size, 0,
    664  1.2  matt 	    &sc->sc_bsh);
    665  1.2  matt 	if (error) {
    666  1.2  matt 		aprint_error(": failed to map registers: %d\n", error);
    667  1.2  matt 		return;
    668  1.2  matt 	}
    669  1.2  matt 
    670  1.2  matt 	u_int valid_owins = 0;
    671  1.2  matt 	for (u_int i = 1, off = PEXOTAR1 - PEXOTAR0;
    672  1.2  matt 	     i < 4; i++, off += PEXOTAR1 - PEXOTAR0) {
    673  1.2  matt 		struct pq3pci_owin owin;
    674  1.2  matt 		owin.potar = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    675  1.2  matt 		    PEXOTAR0 + off);
    676  1.2  matt 		owin.potear = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    677  1.2  matt 		    PEXOTEAR0 + off);
    678  1.2  matt 		owin.powbar = 0;
    679  1.2  matt 		if (i > 0) {
    680  1.2  matt 			/* Doesn't exist for outbound window 0 */
    681  1.2  matt 			owin.powbar = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    682  1.2  matt 			    PEXOWBAR1 - (PEXOTAR1 - PEXOTAR0) + off);
    683  1.2  matt 		}
    684  1.2  matt 		owin.powar = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    685  1.2  matt 		    PEXOWAR0 + off);
    686  1.2  matt #if 0
    687  1.2  matt 		aprint_normal_dev(self,
    688  1.2  matt 		    "owin[%u]: potar=%#x potear=%#x powbar=%#x powar=%#x\n",
    689  1.2  matt 		    i, owin.potar, owin.potear, owin.powbar, owin.powar);
    690  1.2  matt #endif
    691  1.2  matt 		if (owin.powar & PEXOWAR_EN) {
    692  1.2  matt 			valid_owins++;
    693  1.2  matt 			if (!pq3pci_owin_setup(sc, i, &owin))
    694  1.2  matt 				return;
    695  1.2  matt 		}
    696  1.2  matt 	}
    697  1.2  matt #ifndef PCI_NETBSD_CONFIGURE
    698  1.2  matt 	if (valid_owins == 0) {
    699  1.2  matt 		aprint_normal(": %s controller%s\n", buf,
    700  1.2  matt 		    " (disabled)");
    701  1.2  matt 		return;
    702  1.2  matt 	}
    703  1.2  matt #endif
    704  1.2  matt 
    705  1.2  matt 	u_int valid_iwins = 0;
    706  1.2  matt 	for (u_int i = 0, off = 0; i < 3; i++, off += PEXITAR2 - PEXITAR1) {
    707  1.2  matt 		struct pq3pci_iwin iwin;
    708  1.2  matt 		iwin.pitar = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    709  1.2  matt 		    PEXITAR1 + off);
    710  1.2  matt 		iwin.piwbar = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    711  1.2  matt 		    PEXIWBAR1 + off);
    712  1.2  matt 		if (i > 0) {
    713  1.2  matt 			/* Doesn't exist */
    714  1.2  matt 			iwin.piwbear = bus_space_read_4(sc->sc_bst,
    715  1.2  matt 			    sc->sc_bsh,
    716  1.2  matt 			    PEXIWBEAR2 - (PEXITAR2 - PEXITAR1) + off);
    717  1.2  matt 		} else {
    718  1.2  matt 			iwin.piwbear = 0;
    719  1.2  matt 		}
    720  1.2  matt 		iwin.piwar = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    721  1.2  matt 		    PEXIWAR1 + off);
    722  1.2  matt #if 0
    723  1.2  matt 		aprint_normal_dev(self,
    724  1.2  matt 		    "iwin[%u]: pitar=%#x piwbar=%#x piwbear=%#x piwar=%#x\n",
    725  1.2  matt 		    i, iwin.pitar, iwin.piwbar, iwin.piwbear, iwin.piwar);
    726  1.2  matt #endif
    727  1.2  matt 		if (iwin.piwar & PEXIWAR_EN) {
    728  1.2  matt 			valid_iwins++;
    729  1.2  matt 			if (!pq3pci_iwin_setup(sc, i, &iwin))
    730  1.2  matt 				return;
    731  1.2  matt 		}
    732  1.2  matt 	}
    733  1.2  matt 
    734  1.2  matt 	sc->sc_conf_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
    735  1.2  matt 
    736  1.2  matt 	pci_chipset_tag_t pc = pq3pci_pci_chipset_init(sc);
    737  1.2  matt 
    738  1.2  matt #ifndef PCI_NETBSD_CONFIGURE
    739  1.2  matt 	if (valid_iwins == 0) {
    740  1.2  matt 		aprint_normal(": %s controller%s\n", buf,
    741  1.2  matt 		    " (disabled)");
    742  1.2  matt 		return;
    743  1.2  matt 	}
    744  1.2  matt #else
    745  1.2  matt 	if (sc->sc_pcie && pci_conf_read(pc, 0, PEX_LTSSM) < LTSSM_L0) {
    746  1.2  matt 		aprint_normal(": %s controller%s\n", buf,
    747  1.2  matt 		    " (offline)");
    748  1.2  matt 		return;
    749  1.2  matt 	}
    750  1.2  matt 	if (!sc->sc_pcie && (pci_conf_read(pc, 0, PCI_PBFR) & PBFR_PAH)) {
    751  1.2  matt 		aprint_normal(": %s controller%s\n", buf,
    752  1.2  matt 		    " (agent mode)");
    753  1.2  matt 		return;
    754  1.2  matt 	}
    755  1.2  matt 	if (valid_iwins == 0) {
    756  1.2  matt 		struct pq3pci_iwin iwin = {
    757  1.2  matt 		    .pitar = 0,
    758  1.2  matt 		    .piwbar = 0,
    759  1.2  matt 		    .piwbear = 0,
    760  1.2  matt 		    .piwar = PEXIWAR_EN|PEXIWAR_PF|PEXIWAR_TRGT_LOCALMEM
    761  1.2  matt 			|PEXIWAR_RTT_MEM_SNOOP|PEXIWAR_WTT_MEM_SNOOP
    762  1.2  matt 			|__SHIFTIN(30-__builtin_clz(pmemsize),PEXIWAR_IWS),
    763  1.2  matt 		};
    764  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXITAR2, iwin.pitar);
    765  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXIWBAR2, iwin.piwbar);
    766  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXIWBEAR2, iwin.piwbear);
    767  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXIWAR2, iwin.piwar);
    768  1.2  matt 
    769  1.2  matt 		if (!pq3pci_iwin_setup(sc, 2, &iwin)) {
    770  1.2  matt 			aprint_error(": error creating inbound window\n");
    771  1.2  matt 			return;
    772  1.2  matt 		}
    773  1.2  matt 
    774  1.2  matt 	}
    775  1.2  matt 
    776  1.2  matt 	if (valid_owins == 0) {
    777  1.2  matt 		u_long membase, iobase;
    778  1.2  matt 		error = extent_alloc(pcimem_ex, PCI_MEMSIZE, PCI_MEMSIZE,
    779  1.2  matt 		   PCI_MEMSIZE, EX_WAITOK, &membase);
    780  1.2  matt 		if (error) {
    781  1.2  matt 			aprint_error(
    782  1.2  matt 			    ": error allocating address space for %s: %d\n",
    783  1.2  matt 			    "PCI memory", error);
    784  1.2  matt 			return;
    785  1.2  matt 		}
    786  1.2  matt 		struct pq3pci_owin owin1 = {
    787  1.2  matt 		    .potar = membase >> 12,
    788  1.2  matt 		    .potear = 0,
    789  1.2  matt 		    .powbar = membase >> 12,
    790  1.2  matt 		    .powar = PEXOWAR_EN|PEXOWAR_TC0
    791  1.2  matt 			|PEXOWAR_RTT_MEM|PEXOWAR_WTT_MEM
    792  1.2  matt 			|__SHIFTIN(ilog2(PCI_MEMSIZE)-1,PEXOWAR_OWS),
    793  1.2  matt 		};
    794  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXOTAR1, owin1.potar);
    795  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXOTEAR1, owin1.potear);
    796  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXOWBAR1, owin1.powbar);
    797  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXOWAR1, owin1.powar);
    798  1.2  matt 		if (!pq3pci_owin_setup(sc, 1, &owin1)) {
    799  1.2  matt 			aprint_error(
    800  1.2  matt 			    ": error creating bus space for %s\n",
    801  1.2  matt 			    "PCI memory");
    802  1.2  matt 			return;
    803  1.2  matt 		}
    804  1.2  matt 
    805  1.2  matt 		error = extent_alloc(pciio_ex, PCI_IOSIZE, PCI_IOSIZE,
    806  1.2  matt 		   PCI_IOSIZE, EX_WAITOK, &iobase);
    807  1.2  matt 		if (error) {
    808  1.2  matt 			aprint_error(
    809  1.2  matt 			    ": error allocating address space for %s: %d\n",
    810  1.2  matt 			    "PCI I/O space", error);
    811  1.2  matt 			return;
    812  1.2  matt 		}
    813  1.2  matt 		struct pq3pci_owin owin2 = {
    814  1.2  matt 		    .potar = 0,
    815  1.2  matt 		    .potear = 0,
    816  1.2  matt 		    .powbar = iobase >> 12,
    817  1.2  matt 		    .powar = PEXOWAR_EN|PEXOWAR_TC0
    818  1.2  matt 			|PEXOWAR_RTT_IO|PEXOWAR_WTT_IO
    819  1.2  matt 			|__SHIFTIN(ilog2(PCI_IOSIZE)-1,PEXOWAR_OWS),
    820  1.2  matt 		};
    821  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXOTAR2, owin2.potar);
    822  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXOTEAR2, owin2.potear);
    823  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXOWBAR2, owin2.powbar);
    824  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEXOWAR2, owin2.powar);
    825  1.2  matt 		if (!pq3pci_owin_setup(sc, 2, &owin2)) {
    826  1.2  matt 			aprint_error(
    827  1.2  matt 			    ": error creating bus space for %s\n",
    828  1.2  matt 			    "PCI I/O space");
    829  1.2  matt 			return;
    830  1.2  matt 		}
    831  1.2  matt 
    832  1.2  matt 		struct extent *ioext = extent_create("pciio", 0, PCI_IOSIZE,
    833  1.2  matt 		     M_DEVBUF, NULL, 0, EX_NOWAIT);
    834  1.2  matt 		struct extent *memext = extent_create("pcimem", membase,
    835  1.2  matt 		     membase + PCI_MEMSIZE, M_DEVBUF, NULL, 0, EX_NOWAIT);
    836  1.2  matt 
    837  1.2  matt 		error = pci_configure_bus(pc, ioext, memext, NULL, 0,
    838  1.2  matt 		    curcpu()->ci_ci.dcache_line_size);
    839  1.2  matt 
    840  1.2  matt 		extent_destroy(ioext);
    841  1.2  matt 		extent_destroy(memext);
    842  1.2  matt 
    843  1.2  matt 		if (error) {
    844  1.2  matt 			aprint_normal(": configuration failed\n");
    845  1.2  matt 			return;
    846  1.2  matt 		}
    847  1.2  matt 	}
    848  1.2  matt #endif
    849  1.2  matt 
    850  1.2  matt 	aprint_normal(": %s controller%s\n", buf, "");
    851  1.2  matt 
    852  1.2  matt 	struct pcibus_attach_args pba;
    853  1.2  matt 	memset(&pba, 0, sizeof(pba));
    854  1.2  matt 
    855  1.2  matt 	pba.pba_flags = sc->sc_pba_flags | PCI_FLAGS_MSI_OKAY
    856  1.2  matt 	    | PCI_FLAGS_MSIX_OKAY;
    857  1.2  matt 	if (pba.pba_flags & PCI_FLAGS_IO_ENABLED)
    858  1.2  matt 		pba.pba_iot = pc->pc_iot;
    859  1.2  matt 	if (pba.pba_flags & PCI_FLAGS_MEM_ENABLED)
    860  1.2  matt 		pba.pba_memt = pc->pc_memt;
    861  1.2  matt 	pba.pba_dmat = cna->cna_dmat;
    862  1.2  matt 	pba.pba_pc = pc;
    863  1.2  matt 	pba.pba_bus = 0;
    864  1.2  matt 
    865  1.2  matt 	/*
    866  1.2  matt 	 * Program BAR0 so that MSIs can work.
    867  1.2  matt 	 */
    868  1.2  matt 	pci_conf_write(pc, 0, PCI_BAR0, sc->sc_bst->pbs_offset);
    869  1.2  matt 	pcireg_t cmdsts = pci_conf_read(pc, 0, PCI_COMMAND_STATUS_REG);
    870  1.2  matt 	cmdsts |= PCI_COMMAND_INTERRUPT_DISABLE;
    871  1.2  matt 	pci_conf_write(pc, 0, PCI_COMMAND_STATUS_REG, cmdsts);
    872  1.2  matt 
    873  1.2  matt #if 0
    874  1.2  matt 	/*
    875  1.2  matt 	 *
    876  1.2  matt 	 */
    877  1.2  matt 	pq3pci_intr_source_lookup(sc, PIH_MAKE(0, IST_LEVEL, 0));
    878  1.2  matt #endif
    879  1.2  matt #if 0
    880  1.2  matt 	if (sc->sc_pcie)
    881  1.2  matt 		pci_conf_print(pc, 0, NULL);
    882  1.2  matt #endif
    883  1.2  matt 
    884  1.2  matt 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    885  1.2  matt }
    886  1.2  matt 
    887  1.2  matt static void
    888  1.2  matt pq3pci_attach_hook(device_t parent, device_t self,
    889  1.2  matt 	struct pcibus_attach_args *pba)
    890  1.2  matt {
    891  1.2  matt 	/* do nothing */
    892  1.2  matt }
    893  1.2  matt 
    894  1.2  matt static int
    895  1.2  matt pq3pci_bus_maxdevs(void *v, int busno)
    896  1.2  matt {
    897  1.2  matt 	struct pq3pci_softc * const sc = v;
    898  1.2  matt 	return sc->sc_pcie && busno < 2 ? 1 : 32;
    899  1.2  matt }
    900  1.2  matt 
    901  1.2  matt static void
    902  1.2  matt pq3pci_decompose_tag(void *v, pcitag_t tag, int *bus, int *dev, int *func)
    903  1.2  matt {
    904  1.2  matt 	if (bus)
    905  1.2  matt 		*bus = (tag >> 16) & 0xff;
    906  1.2  matt 	if (dev)
    907  1.2  matt 		*dev = (tag >> 11) & 0x1f;
    908  1.2  matt 	if (func)
    909  1.2  matt 		*func = (tag >> 8) & 0x07;
    910  1.2  matt }
    911  1.2  matt 
    912  1.2  matt static pcitag_t
    913  1.2  matt pq3pci_make_tag(void *v, int bus, int dev, int func)
    914  1.2  matt {
    915  1.2  matt 	return (bus << 16) | (dev << 11) | (func << 8);
    916  1.2  matt }
    917  1.2  matt 
    918  1.2  matt static inline pcitag_t
    919  1.2  matt pq3pci_config_addr_read(pci_chipset_tag_t pc)
    920  1.2  matt {
    921  1.2  matt 	pcitag_t v;
    922  1.2  matt         __asm volatile("lwz\t%0, 0(%1)" : "=r"(v) : "b"(pc->pc_addr));
    923  1.2  matt         __asm volatile("mbar\n\tmsync");
    924  1.2  matt 	return v;
    925  1.2  matt }
    926  1.2  matt 
    927  1.2  matt static inline void
    928  1.2  matt pq3pci_config_addr_write(pci_chipset_tag_t pc, pcitag_t v)
    929  1.2  matt {
    930  1.2  matt         __asm volatile("stw\t%0, 0(%1)" :: "r"(v), "b"(pc->pc_addr));
    931  1.2  matt         __asm volatile("mbar\n\tmsync");
    932  1.2  matt }
    933  1.2  matt 
    934  1.2  matt static inline pcireg_t
    935  1.2  matt pq3pci_config_data_read(pci_chipset_tag_t pc)
    936  1.2  matt {
    937  1.2  matt 	pcireg_t v;
    938  1.2  matt         __asm volatile("lwbrx\t%0, 0, %1" : "=r"(v) : "b"(pc->pc_data));
    939  1.2  matt         __asm volatile("mbar\n\tmsync");
    940  1.2  matt 	return v;
    941  1.2  matt }
    942  1.2  matt 
    943  1.2  matt static inline void
    944  1.2  matt pq3pci_config_data_write(pci_chipset_tag_t pc, pcireg_t v)
    945  1.2  matt {
    946  1.2  matt         __asm volatile("stwbrx\t%0, 0, %1" :: "r"(v), "r"(pc->pc_data));
    947  1.2  matt         __asm volatile("mbar\n\tmsync");
    948  1.2  matt }
    949  1.2  matt 
    950  1.2  matt static pcireg_t
    951  1.2  matt pq3pci_conf_read(void *v, pcitag_t tag, int reg)
    952  1.2  matt {
    953  1.2  matt 	struct pq3pci_softc * const sc = v;
    954  1.2  matt 	struct genppc_pci_chipset * const pc = &sc->sc_pc;
    955  1.2  matt 
    956  1.2  matt 	if (reg >= 256) {
    957  1.2  matt 		if (!sc->sc_pcie)
    958  1.2  matt 			return 0xffffffff;
    959  1.2  matt 		reg = (reg & 0xff) | ((reg & 0xf00) << 16);
    960  1.2  matt 	}
    961  1.2  matt 	if (sc->sc_pcie && ((tag >> 16) & 0xff) != 0) {
    962  1.2  matt 	//	pcireg_t slot_status = pci_conf_read(pc, 0, 0x64);
    963  1.2  matt 	//	printf("%s: tag 0x0 slot status: %#x\n",__func__, slot_status);
    964  1.2  matt 	//	if ((slot_status & __BIT(6+16)) == 0)
    965  1.2  matt 	//		printf(" addr=%#llx ", tag | reg | PEX_CONFIG_ADDR_EN);
    966  1.2  matt 	//		return 0xffffffff;
    967  1.2  matt 	}
    968  1.2  matt 
    969  1.2  matt 	mutex_spin_enter(sc->sc_conf_lock);
    970  1.2  matt 
    971  1.2  matt 	pq3pci_config_addr_write(pc, tag | reg | PEX_CONFIG_ADDR_EN);
    972  1.2  matt 	pcireg_t rv = pq3pci_config_data_read(pc);
    973  1.2  matt 
    974  1.2  matt 	mutex_spin_exit(sc->sc_conf_lock);
    975  1.2  matt 
    976  1.2  matt #if 0
    977  1.2  matt 	uint32_t err = bus_space_read_4(sc->sc_bst, sc->sc_bsh, PEX_ERR_DR);
    978  1.2  matt 	if (err & PEXERRDR_ICCA) {
    979  1.2  matt 		aprint_error_dev(sc->sc_dev, "%s: tag %#x reg %#x icca: %#x\n",
    980  1.2  matt 		    __func__, tag, reg, pq3pci_config_addr_read(pc));
    981  1.2  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, PEX_ERR_DR,
    982  1.2  matt 		    PEXERRDR_ICCA);
    983  1.2  matt 	}
    984  1.2  matt #endif
    985  1.2  matt 	return rv;
    986  1.2  matt }
    987  1.2  matt 
    988  1.2  matt static void
    989  1.2  matt pq3pci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
    990  1.2  matt {
    991  1.2  matt 	struct pq3pci_softc * const sc = v;
    992  1.2  matt 	struct genppc_pci_chipset * const pc = &sc->sc_pc;
    993  1.2  matt 
    994  1.2  matt 	if (reg >= 256) {
    995  1.2  matt 		if (!sc->sc_pcie)
    996  1.2  matt 			return;
    997  1.2  matt 		reg = (reg & 0xff) | ((reg & 0xf00) << 16);
    998  1.2  matt 	}
    999  1.2  matt 
   1000  1.2  matt 	mutex_spin_enter(sc->sc_conf_lock);
   1001  1.2  matt 
   1002  1.2  matt #if 0
   1003  1.2  matt 	aprint_error_dev(sc->sc_dev, "%s: tag %#x reg %#x data %#x\n",
   1004  1.2  matt 	    __func__, tag, reg, data);
   1005  1.2  matt #endif
   1006  1.2  matt 	pq3pci_config_addr_write(pc, tag | reg | PEX_CONFIG_ADDR_EN);
   1007  1.2  matt 	pq3pci_config_data_write(pc, data);
   1008  1.2  matt 
   1009  1.2  matt 	mutex_spin_exit(sc->sc_conf_lock);
   1010  1.2  matt }
   1011  1.2  matt 
   1012  1.2  matt static int
   1013  1.2  matt pq3pci_conf_hook(pci_chipset_tag_t pc, int bus, int dev, int func,
   1014  1.2  matt 	pcireg_t id)
   1015  1.2  matt {
   1016  1.2  matt 	struct pq3pci_softc * const sc = pc->pc_conf_v;
   1017  1.2  matt 	if (sc->sc_pcie && bus != 0) {
   1018  1.2  matt 		pcireg_t slot_status = pci_conf_read(pc, 0, 0x64);
   1019  1.2  matt 		if ((slot_status & __BIT(6+16)) == 0)
   1020  1.2  matt 			return 0;
   1021  1.2  matt 	}
   1022  1.2  matt 	if (!sc->sc_pcie && bus == 0 && dev == 0) {
   1023  1.2  matt 		return PCI_CONF_DEFAULT ^ (PCI_CONF_MAP_IO|PCI_CONF_MAP_MEM|PCI_CONF_MAP_ROM);
   1024  1.2  matt 	}
   1025  1.2  matt 	return PCI_CONF_DEFAULT;
   1026  1.2  matt }
   1027  1.2  matt 
   1028  1.2  matt static void
   1029  1.2  matt pq3pci_msi_group_setup(struct pq3pci_msigroup *msig, u_int group, int ipl)
   1030  1.2  matt {
   1031  1.2  matt 	const char (*intr_names)[8] = msi_intr_names[group];
   1032  1.2  matt 
   1033  1.2  matt 	KASSERT(ipl == IPL_VM);
   1034  1.2  matt 
   1035  1.2  matt 	pq3pci_msigroups[group] = msig;
   1036  1.2  matt 	msig->msig_group = group;
   1037  1.2  matt 	msig->msig_free_mask = ~0 << (group == 0);
   1038  1.2  matt 	msig->msig_ipl = ipl;
   1039  1.2  matt 	msig->msig_lock = mutex_obj_alloc(MUTEX_DEFAULT, ipl);
   1040  1.2  matt 	msig->msig_ih = intr_establish(msig->msig_group, ipl, IST_MSIGROUP,
   1041  1.2  matt 	    pq3pci_msi_intr, msig);
   1042  1.2  matt 	msig->msig_msir = OPENPIC_BASE + OPENPIC_MSIR(msig->msig_group);
   1043  1.2  matt 	for (u_int i = 0; i < __arraycount(msig->msig_ihands); i++) {
   1044  1.2  matt 		struct pq3pci_msihand * const msih = msig->msig_ihands + i;
   1045  1.2  matt 		msih->msih_ih.ih_class = IH_MSI;
   1046  1.2  matt 		msih->msih_ih.ih_func = pq3pci_msi_spurious_intr;
   1047  1.2  matt 		msih->msih_ih.ih_arg = msih;
   1048  1.2  matt 		msih->msih_group = msig;
   1049  1.2  matt 		evcnt_attach_dynamic(&msih->msih_ev, EVCNT_TYPE_INTR,
   1050  1.2  matt 		    NULL, intr_names[i], "intr");
   1051  1.2  matt 		evcnt_attach_dynamic(&msih->msih_ev_spurious, EVCNT_TYPE_INTR,
   1052  1.2  matt 		    &msih->msih_ev, intr_names[i], "spurious intr");
   1053  1.2  matt 	}
   1054  1.2  matt }
   1055  1.2  matt 
   1056  1.2  matt static pci_intr_handle_t
   1057  1.2  matt pq3pci_msi_alloc(int ipl, u_int rmsi)
   1058  1.2  matt {
   1059  1.2  matt 	size_t freegroup = 0;
   1060  1.2  matt 	size_t maplen = __arraycount(pq3pci_msigroups);
   1061  1.2  matt 	KASSERT(rmsi <= 5);
   1062  1.2  matt 	uint32_t bitmap[maplen];
   1063  1.2  matt 
   1064  1.2  matt 	for (u_int i = 0; i < maplen; i++) {
   1065  1.2  matt 		struct pq3pci_msigroup * const msig = pq3pci_msigroups[i];
   1066  1.2  matt 		if (msig == NULL) {
   1067  1.2  matt 			bitmap[i] = 0;
   1068  1.2  matt 			if (freegroup == 0)
   1069  1.2  matt 				freegroup = i + 1;
   1070  1.2  matt 			continue;
   1071  1.2  matt 		}
   1072  1.2  matt 		/*
   1073  1.2  matt 		 * If this msigroup has the wrong IPL or there's nothing
   1074  1.2  matt 		 * free, try the next one.
   1075  1.2  matt 		 */
   1076  1.2  matt 		if (msig->msig_ipl != ipl || msig->msig_free_mask == 0) {
   1077  1.2  matt 			bitmap[i] = 0;
   1078  1.2  matt 			continue;
   1079  1.2  matt 		}
   1080  1.2  matt 
   1081  1.2  matt 		bitmap[i] = msig->msig_free_mask;
   1082  1.2  matt 	}
   1083  1.2  matt 	for (u_int i = 0; i < maplen; i++) {
   1084  1.2  matt 		uint32_t mapbits = bitmap[i];
   1085  1.2  matt 		u_int n = ffs(mapbits);
   1086  1.2  matt 		if (n--) {
   1087  1.2  matt 			return PIH_MAKE(i * 32 + n, IST_MSI, 0);
   1088  1.2  matt 		}
   1089  1.2  matt 	}
   1090  1.2  matt 
   1091  1.2  matt 	if (freegroup-- == 0)
   1092  1.2  matt 		return 0;
   1093  1.2  matt 
   1094  1.2  matt 	struct pq3pci_msigroup * const msig =
   1095  1.2  matt 	    kmem_zalloc(sizeof(*msig), KM_SLEEP);
   1096  1.2  matt 	KASSERT(msig != NULL);
   1097  1.2  matt 	pq3pci_msi_group_setup(msig, freegroup, ipl);
   1098  1.2  matt 	u_int n = ffs(msig->msig_free_mask) - 1;
   1099  1.2  matt 	return PIH_MAKE(freegroup * 32 + n, IST_MSI, 0);
   1100  1.2  matt }
   1101  1.2  matt 
   1102  1.2  matt static struct pq3pci_msihand *
   1103  1.2  matt pq3pci_msi_lookup(pci_intr_handle_t handle)
   1104  1.2  matt {
   1105  1.2  matt 	const int irq = PIH_IRQ(handle);
   1106  1.2  matt 	KASSERT(irq < 256);
   1107  1.2  matt 	struct pq3pci_msigroup * const msig = pq3pci_msigroups[irq / 32];
   1108  1.2  matt 	KASSERT(msig != NULL);
   1109  1.2  matt 	return &msig->msig_ihands[irq & 31];
   1110  1.2  matt }
   1111  1.2  matt 
   1112  1.2  matt static struct pq3pci_msihand *
   1113  1.2  matt pq3pci_msi_claim(pci_intr_handle_t handle)
   1114  1.2  matt {
   1115  1.2  matt 	const int irq = PIH_IRQ(handle);
   1116  1.2  matt 	uint32_t irq_mask = __BIT(irq & 31);
   1117  1.2  matt 	KASSERT(irq < 256);
   1118  1.2  matt 	struct pq3pci_msigroup * const msig = pq3pci_msigroups[irq / 32];
   1119  1.2  matt 	KASSERT(msig != NULL);
   1120  1.2  matt 	struct pq3pci_msihand * const msih = &msig->msig_ihands[irq & 31];
   1121  1.2  matt 	mutex_spin_enter(msig->msig_lock);
   1122  1.2  matt 	KASSERT(msig->msig_free_mask & irq_mask);
   1123  1.2  matt 	msig->msig_free_mask ^= irq_mask;
   1124  1.2  matt 	mutex_spin_exit(msig->msig_lock);
   1125  1.2  matt 	return msih;
   1126  1.2  matt }
   1127  1.2  matt 
   1128  1.2  matt static struct pq3pci_intrsource *
   1129  1.2  matt pq3pci_intr_source_lookup(struct pq3pci_softc *sc, pci_intr_handle_t handle)
   1130  1.2  matt {
   1131  1.2  matt 	struct pq3pci_intrsource *pis;
   1132  1.2  matt 	SIMPLEQ_FOREACH(pis, &pq3pci_intrsources, pis_link) {
   1133  1.2  matt 		if (pis->pis_handle == handle)
   1134  1.2  matt 			return pis;
   1135  1.2  matt 	}
   1136  1.2  matt 	pis = kmem_zalloc(sizeof(*pis), KM_SLEEP);
   1137  1.2  matt 	pq3pci_intr_source_setup(sc, pis, handle);
   1138  1.2  matt 	return pis;
   1139  1.2  matt }
   1140  1.2  matt 
   1141  1.2  matt static pci_intr_handle_t
   1142  1.2  matt pq3pci_intr_handle_lookup(struct pq3pci_softc *sc, struct pci_attach_args *pa)
   1143  1.2  matt {
   1144  1.2  matt 	prop_dictionary_t entry;
   1145  1.2  matt 
   1146  1.2  matt 	if (sc->sc_pcie) do {
   1147  1.2  matt 		pcireg_t msictl;
   1148  1.2  matt 		int msioff;
   1149  1.2  matt 		if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI,
   1150  1.2  matt 					&msioff, &msictl))
   1151  1.2  matt 			break;
   1152  1.2  matt 		msictl = pci_conf_read(pa->pa_pc, pa->pa_tag, msioff);
   1153  1.2  matt 		msictl &= ~PCI_MSI_CTL_MSI_ENABLE;
   1154  1.2  matt 		msictl &= ~(PCI_MSI_CTL_MME_MASK << PCI_MSI_CTL_MME_SHIFT);
   1155  1.2  matt 		int rmsi = (msictl >> PCI_MSI_CTL_MMC_SHIFT) & PCI_MSI_CTL_MMC_MASK;
   1156  1.2  matt 		pci_conf_write(pa->pa_pc, pa->pa_tag, msioff, msictl);
   1157  1.2  matt 		pci_intr_handle_t handle = pq3pci_msi_alloc(IPL_VM, rmsi);
   1158  1.2  matt 		struct pq3pci_msihand * const msih = pq3pci_msi_lookup(handle);
   1159  1.2  matt 		msih->msih_tag = pa->pa_tag;
   1160  1.2  matt 		msih->msih_msioff = msioff;
   1161  1.2  matt 		return handle;
   1162  1.2  matt 	} while (false);
   1163  1.2  matt 
   1164  1.2  matt 
   1165  1.2  matt 	if (sc->sc_intrmask == 0) {
   1166  1.2  matt 		entry = prop_dictionary_get(sc->sc_intrmap, "000000");
   1167  1.2  matt 	} else {
   1168  1.2  matt 		char prop_name[8];
   1169  1.2  matt 		u_int intrinc = __LOWEST_SET_BIT(sc->sc_intrmask);
   1170  1.2  matt 		pcitag_t tag = (pa->pa_intrpin - PCI_INTERRUPT_PIN_A) * intrinc;
   1171  1.2  matt 
   1172  1.2  matt 		snprintf(prop_name, sizeof(prop_name), "%06x",
   1173  1.2  matt 		    tag & sc->sc_intrmask);
   1174  1.2  matt 
   1175  1.2  matt #if 0
   1176  1.2  matt 		printf("%s: %#x %#x %u (%u) -> %#x & %#x -> %#x <%s>\n",
   1177  1.2  matt 		    __func__, pa->pa_tag, pa->pa_intrtag, pa->pa_intrpin, pa->pa_rawintrpin,
   1178  1.2  matt 		    tag, sc->sc_intrmask, tag & sc->sc_intrmask, prop_name);
   1179  1.2  matt #endif
   1180  1.2  matt 
   1181  1.2  matt 		entry = prop_dictionary_get(sc->sc_intrmap, prop_name);
   1182  1.2  matt 	}
   1183  1.2  matt 	KASSERT(entry != NULL);
   1184  1.2  matt 	KASSERT(prop_object_type(entry) == PROP_TYPE_DICTIONARY);
   1185  1.2  matt 
   1186  1.2  matt 	prop_number_t pn_irq = prop_dictionary_get(entry, "interrupt");
   1187  1.2  matt 	KASSERT(pn_irq != NULL);
   1188  1.2  matt 	KASSERT(prop_object_type(pn_irq) == PROP_TYPE_NUMBER);
   1189  1.2  matt 	int irq = prop_number_unsigned_integer_value(pn_irq);
   1190  1.2  matt 	prop_number_t pn_ist = prop_dictionary_get(entry, "type");
   1191  1.2  matt 	KASSERT(pn_ist != NULL);
   1192  1.2  matt 	KASSERT(prop_object_type(pn_ist) == PROP_TYPE_NUMBER);
   1193  1.2  matt 	int ist = prop_number_unsigned_integer_value(pn_ist);
   1194  1.2  matt 
   1195  1.2  matt 	return PIH_MAKE(irq, ist, 0);
   1196  1.2  matt }
   1197  1.2  matt 
   1198  1.2  matt static int
   1199  1.2  matt pq3pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *handlep)
   1200  1.2  matt {
   1201  1.2  matt 	struct pq3pci_softc * const sc = pa->pa_pc->pc_intr_v;
   1202  1.2  matt 
   1203  1.2  matt 	if (pa->pa_intrpin == PCI_INTERRUPT_PIN_NONE)
   1204  1.2  matt 		return ENOENT;
   1205  1.2  matt 
   1206  1.2  matt 	*handlep = pq3pci_intr_handle_lookup(sc, pa);
   1207  1.2  matt 
   1208  1.2  matt 	return 0;
   1209  1.2  matt }
   1210  1.2  matt 
   1211  1.2  matt static const char *
   1212  1.2  matt pq3pci_intr_string(void *v, pci_intr_handle_t handle)
   1213  1.2  matt {
   1214  1.2  matt 	if (PIH_IST(handle) == IST_MSI) {
   1215  1.2  matt 		const char (*intr_names)[8] = msi_intr_names[0];
   1216  1.2  matt 		return intr_names[PIH_IRQ(handle)];
   1217  1.2  matt 	}
   1218  1.2  matt 
   1219  1.2  matt 	return intr_string(PIH_IRQ(handle), PIH_IST(handle));
   1220  1.2  matt }
   1221  1.2  matt 
   1222  1.2  matt static const struct evcnt *
   1223  1.2  matt pq3pci_intr_evcnt(void *v, pci_intr_handle_t handle)
   1224  1.2  matt {
   1225  1.2  matt 	struct pq3pci_softc * const sc = v;
   1226  1.2  matt 	struct pq3pci_intrsource * const pis =
   1227  1.2  matt 	    pq3pci_intr_source_lookup(sc, handle);
   1228  1.2  matt 
   1229  1.2  matt 	KASSERT(pis != NULL);
   1230  1.2  matt 
   1231  1.2  matt 	return &pis->pis_ev;
   1232  1.2  matt }
   1233  1.2  matt 
   1234  1.2  matt static void *
   1235  1.2  matt pq3pci_intr_establish(void *v, pci_intr_handle_t handle, int ipl,
   1236  1.2  matt 	int (*func)(void *), void *arg)
   1237  1.2  matt {
   1238  1.2  matt 	struct pq3pci_softc * const sc = v;
   1239  1.2  matt 
   1240  1.2  matt 	if (0) {
   1241  1.2  matt 		struct pq3pci_callhand * const pch =
   1242  1.2  matt 		    kmem_zalloc(sizeof(*pch), KM_SLEEP);
   1243  1.2  matt 		KASSERT(pch);
   1244  1.2  matt 		pch->pch_ih.ih_arg = arg;
   1245  1.2  matt 		pch->pch_ih.ih_func = func;
   1246  1.2  matt 		pch->pch_ih.ih_sc = sc;
   1247  1.2  matt 		pch->pch_ipl = ipl;
   1248  1.2  matt 
   1249  1.2  matt 		callout_init(&pch->pch_callout, 0);
   1250  1.2  matt 		callout_reset(&pch->pch_callout, 1, pq3pci_pch_callout, pch);
   1251  1.2  matt 
   1252  1.2  matt 		return pch;
   1253  1.2  matt 	}
   1254  1.2  matt 
   1255  1.2  matt 	const int ist = PIH_IST(handle);
   1256  1.2  matt 
   1257  1.2  matt 	if (ist == IST_MSI) {
   1258  1.2  matt 		pci_chipset_tag_t pc = &sc->sc_pc;
   1259  1.2  matt 		struct pq3pci_msihand * const msih = pq3pci_msi_claim(handle);
   1260  1.2  matt 		pcireg_t cmdsts, msictl;
   1261  1.2  matt 
   1262  1.2  matt 		if (msih == NULL)
   1263  1.2  matt 			return NULL;
   1264  1.2  matt 
   1265  1.2  matt 		struct pq3pci_msigroup * const msig = msih->msih_group;
   1266  1.2  matt 		const pcitag_t tag = msih->msih_tag;
   1267  1.2  matt 
   1268  1.2  matt 		mutex_spin_enter(msig->msig_lock);
   1269  1.2  matt 		msih->msih_ih.ih_class = IH_MSI;
   1270  1.2  matt 		msih->msih_ih.ih_arg = arg;
   1271  1.2  matt 		msih->msih_ih.ih_func = func;
   1272  1.2  matt 		msih->msih_ih.ih_sc = sc;
   1273  1.2  matt 
   1274  1.2  matt 		int off = msih->msih_msioff;
   1275  1.2  matt 		msictl = pci_conf_read(pc, tag, off);
   1276  1.2  matt 
   1277  1.2  matt 		/*
   1278  1.2  matt 		 * The PCSRBAR has already been setup as a 1:1 BAR so we point
   1279  1.2  matt 		 * MSIs at the MSII register in the OpenPIC.
   1280  1.2  matt 		 */
   1281  1.2  matt 		off += 4;
   1282  1.2  matt 		pci_conf_write(pc, tag, off,
   1283  1.2  matt 		    sc->sc_bst->pbs_offset + OPENPIC_BASE + OPENPIC_MSIIR);
   1284  1.2  matt 
   1285  1.2  matt 		/*
   1286  1.2  matt 		 * Upper address is going to be 0.
   1287  1.2  matt 		 */
   1288  1.2  matt 		if (msictl & PCI_MSI_CTL_64BIT_ADDR) {
   1289  1.2  matt 			off += 4;
   1290  1.2  matt 			pci_conf_write(pc, tag, off, 0);
   1291  1.2  matt 		}
   1292  1.2  matt 
   1293  1.2  matt 		/*
   1294  1.2  matt 		 * Set the magic value.  Since PCI writes this to the least
   1295  1.2  matt 		 * significant byte of AD[31:0], let's hope the bridge byte
   1296  1.2  matt 		 * swaps to so it's the most significant bytes or nothing is
   1297  1.2  matt 		 * going to happen.
   1298  1.2  matt 		 */
   1299  1.2  matt 		off += 4;
   1300  1.2  matt 		pci_conf_write(pc, tag, off, PIH_IRQ(handle));
   1301  1.2  matt 
   1302  1.2  matt 		/*
   1303  1.2  matt 		 * Should the driver do this?  How would it know to do it?
   1304  1.2  matt 		 */
   1305  1.2  matt 		if (msictl & PCI_MSI_CTL_PERVEC_MASK) {
   1306  1.2  matt 			off += 4;
   1307  1.2  matt 			pci_conf_write(pc, tag, off, 0);
   1308  1.2  matt 		}
   1309  1.2  matt 
   1310  1.2  matt 		/*
   1311  1.2  matt 		 * Let's make sure he won't raise any INTx.  Technically
   1312  1.2  matt 		 * setting MSI enable will prevent that as well but might
   1313  1.2  matt 		 * as well be as safe as possible.
   1314  1.2  matt 		 */
   1315  1.2  matt 		cmdsts = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
   1316  1.2  matt 		cmdsts |= PCI_COMMAND_INTERRUPT_DISABLE;
   1317  1.2  matt 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, cmdsts);
   1318  1.2  matt 
   1319  1.2  matt #if 1
   1320  1.2  matt 		/*
   1321  1.2  matt 		 * Now we can enable the MSI
   1322  1.2  matt 		 */
   1323  1.2  matt 		msictl |= PCI_MSI_CTL_MSI_ENABLE;
   1324  1.2  matt 		pci_conf_write(pc, tag, msih->msih_msioff, msictl);
   1325  1.2  matt #endif
   1326  1.2  matt 
   1327  1.2  matt 		mutex_spin_exit(msig->msig_lock);
   1328  1.2  matt 
   1329  1.2  matt #if 0
   1330  1.2  matt 		struct pq3pci_callhand * const pch =
   1331  1.2  matt 		    kmem_zalloc(sizeof(*pch), KM_SLEEP);
   1332  1.2  matt 		KASSERT(pch);
   1333  1.2  matt 
   1334  1.2  matt 		pch->pch_ih.ih_arg = msig;
   1335  1.2  matt 		pch->pch_ih.ih_func = pq3pci_msi_intr;
   1336  1.2  matt #if 1
   1337  1.2  matt 		pch->pch_ih.ih_arg = arg;
   1338  1.2  matt 		pch->pch_ih.ih_func = func;
   1339  1.2  matt #endif
   1340  1.2  matt 		pch->pch_ih.ih_sc = sc;
   1341  1.2  matt 		pch->pch_ipl = ipl;
   1342  1.2  matt 
   1343  1.2  matt 		callout_init(&pch->pch_callout, 0);
   1344  1.2  matt 		callout_reset(&pch->pch_callout, 1, pq3pci_pch_callout, pch);
   1345  1.2  matt 
   1346  1.2  matt #if 1
   1347  1.2  matt 		return pch;
   1348  1.2  matt #endif
   1349  1.2  matt #endif
   1350  1.2  matt 
   1351  1.2  matt 		return msih;
   1352  1.2  matt 	} else {
   1353  1.2  matt 		struct pq3pci_intrsource * const pis =
   1354  1.2  matt 		    pq3pci_intr_source_lookup(sc, handle);
   1355  1.2  matt 		KASSERT(pis != NULL);
   1356  1.2  matt 
   1357  1.2  matt 		struct pq3pci_intrhand * const pih =
   1358  1.2  matt 		    kmem_zalloc(sizeof(*pih), KM_SLEEP);
   1359  1.2  matt 
   1360  1.2  matt 		if (pih == NULL)
   1361  1.2  matt 			return NULL;
   1362  1.2  matt 
   1363  1.2  matt 		pih->pih_ih.ih_class = IH_INTX;
   1364  1.2  matt 		pih->pih_ih.ih_func = func;
   1365  1.2  matt 		pih->pih_ih.ih_arg = arg;
   1366  1.2  matt 		pih->pih_ih.ih_sc = sc;
   1367  1.2  matt 		pih->pih_ipl = ipl;
   1368  1.2  matt 		pih->pih_source = pis;
   1369  1.2  matt 
   1370  1.2  matt 		mutex_spin_enter(pis->pis_lock);
   1371  1.2  matt 		SIMPLEQ_INSERT_TAIL(&pis->pis_ihands, pih, pih_link);
   1372  1.2  matt 		mutex_spin_exit(pis->pis_lock);
   1373  1.2  matt 
   1374  1.2  matt 		return pih;
   1375  1.2  matt 	}
   1376  1.2  matt }
   1377  1.2  matt 
   1378  1.2  matt static void
   1379  1.2  matt pq3pci_intr_disestablish(void *v, void *ih)
   1380  1.2  matt {
   1381  1.2  matt 	struct pq3pci_genihand * const gih = ih;
   1382  1.2  matt 
   1383  1.2  matt 	if (gih->ih_class == IH_INTX) {
   1384  1.2  matt 		struct pq3pci_intrhand * const pih = ih;
   1385  1.2  matt 		struct pq3pci_intrsource * const pis = pih->pih_source;
   1386  1.2  matt 
   1387  1.2  matt 		mutex_spin_enter(pis->pis_lock);
   1388  1.2  matt 		SIMPLEQ_REMOVE(&pis->pis_ihands, pih, pq3pci_intrhand, pih_link);
   1389  1.2  matt 		mutex_spin_exit(pis->pis_lock);
   1390  1.2  matt 
   1391  1.2  matt 		kmem_free(pih, sizeof(*pih));
   1392  1.2  matt 		return;
   1393  1.2  matt 	}
   1394  1.2  matt 	struct pq3pci_msihand * const msih = ih;
   1395  1.2  matt 	struct pq3pci_msigroup * const msig = msih->msih_group;
   1396  1.2  matt 	struct genppc_pci_chipset * const pc = &msih->msih_ih.ih_sc->sc_pc;
   1397  1.2  matt 	const pcitag_t tag = msih->msih_tag;
   1398  1.2  matt 
   1399  1.2  matt 	mutex_spin_enter(msig->msig_lock);
   1400  1.2  matt 
   1401  1.2  matt 	/*
   1402  1.2  matt 	 * disable the MSI
   1403  1.2  matt 	 */
   1404  1.2  matt 	pcireg_t msictl = pci_conf_read(pc, tag, msih->msih_msioff);
   1405  1.2  matt 	msictl &= ~PCI_MSI_CTL_MSI_ENABLE;
   1406  1.2  matt 	pci_conf_write(pc, tag, msih->msih_msioff, msictl);
   1407  1.2  matt 
   1408  1.2  matt 	msih->msih_ih.ih_func = pq3pci_msi_spurious_intr;
   1409  1.2  matt 	msih->msih_ih.ih_arg = msig;
   1410  1.2  matt 	msih->msih_ih.ih_sc = NULL;
   1411  1.2  matt 	msih->msih_tag = 0;
   1412  1.2  matt 	msih->msih_msioff = 0;
   1413  1.2  matt 	mutex_spin_exit(msig->msig_lock);
   1414  1.2  matt }
   1415  1.2  matt 
   1416  1.2  matt static void
   1417  1.2  matt pq3pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
   1418  1.2  matt 	int swiz, int *iline)
   1419  1.2  matt {
   1420  1.2  matt }
   1421  1.2  matt 
   1422  1.2  matt static pci_chipset_tag_t
   1423  1.2  matt pq3pci_pci_chipset_init(struct pq3pci_softc *sc)
   1424  1.2  matt {
   1425  1.2  matt 	struct genppc_pci_chipset * const pc = &sc->sc_pc;
   1426  1.2  matt 
   1427  1.2  matt 	pc->pc_conf_v = sc;
   1428  1.2  matt 	pc->pc_attach_hook = pq3pci_attach_hook;
   1429  1.2  matt         pc->pc_bus_maxdevs = pq3pci_bus_maxdevs;
   1430  1.2  matt         pc->pc_make_tag = pq3pci_make_tag;
   1431  1.2  matt         pc->pc_conf_read = pq3pci_conf_read;
   1432  1.2  matt         pc->pc_conf_write = pq3pci_conf_write;
   1433  1.2  matt #ifdef PCI_NETBSD_CONFIGURE
   1434  1.2  matt         pc->pc_conf_hook = pq3pci_conf_hook;
   1435  1.2  matt #endif
   1436  1.2  matt 
   1437  1.2  matt         pc->pc_intr_v = sc;
   1438  1.2  matt         pc->pc_intr_map = pq3pci_intr_map;
   1439  1.2  matt         pc->pc_intr_string = pq3pci_intr_string;
   1440  1.2  matt         pc->pc_intr_evcnt = pq3pci_intr_evcnt;
   1441  1.2  matt         pc->pc_intr_establish = pq3pci_intr_establish;
   1442  1.2  matt         pc->pc_intr_disestablish = pq3pci_intr_disestablish;
   1443  1.2  matt         pc->pc_conf_interrupt = pq3pci_conf_interrupt;
   1444  1.2  matt         pc->pc_decompose_tag = pq3pci_decompose_tag;
   1445  1.2  matt         pc->pc_conf_hook = pq3pci_conf_hook;
   1446  1.2  matt 
   1447  1.2  matt 	/*
   1448  1.2  matt 	 * This is a horrible kludge but it makes life easier.
   1449  1.2  matt 	 */
   1450  1.2  matt         pc->pc_addr = (void *)(sc->sc_bsh + PEX_CONFIG_ADDR);
   1451  1.2  matt         pc->pc_data = (void *)(sc->sc_bsh + PEX_CONFIG_DATA);
   1452  1.2  matt         pc->pc_bus = 0;
   1453  1.2  matt         pc->pc_memt = &sc->sc_pci_mem_bst.bs_tag;
   1454  1.2  matt         pc->pc_iot = &sc->sc_pci_io_bst.bs_tag;
   1455  1.2  matt 
   1456  1.2  matt 	SIMPLEQ_INIT(&pc->pc_pbi);
   1457  1.2  matt 
   1458  1.2  matt 	return pc;
   1459  1.2  matt }
   1460