Home | History | Annotate | Line # | Download | only in pci
ppb.c revision 1.57
      1 /*	$NetBSD: ppb.c,v 1.57 2017/04/18 05:21:34 msaitoh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1998 Christopher G. Demetriou.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Christopher G. Demetriou
     17  *	for the NetBSD Project.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: ppb.c,v 1.57 2017/04/18 05:21:34 msaitoh Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/evcnt.h>
     41 
     42 #include <dev/pci/pcireg.h>
     43 #include <dev/pci/pcivar.h>
     44 #include <dev/pci/ppbreg.h>
     45 #include <dev/pci/ppbvar.h>
     46 #include <dev/pci/pcidevs.h>
     47 
     48 #define	PCIE_SLCSR_NOTIFY_MASK					\
     49 	(PCIE_SLCSR_ABE | PCIE_SLCSR_PFE | PCIE_SLCSR_MSE |	\
     50 	 PCIE_SLCSR_PDE | PCIE_SLCSR_CCE | PCIE_SLCSR_HPE)
     51 
     52 static const char pcie_linkspeed_strings[4][5] = {
     53 	"1.25", "2.5", "5.0", "8.0",
     54 };
     55 
     56 int	ppb_printevent = 0; /* Print event type if the value is not 0 */
     57 
     58 static int	ppbmatch(device_t, cfdata_t, void *);
     59 static void	ppbattach(device_t, device_t, void *);
     60 static int	ppbdetach(device_t, int);
     61 static void	ppbchilddet(device_t, device_t);
     62 static int	ppb_intr(void *);
     63 static bool	ppb_resume(device_t, const pmf_qual_t *);
     64 static bool	ppb_suspend(device_t, const pmf_qual_t *);
     65 
     66 CFATTACH_DECL3_NEW(ppb, sizeof(struct ppb_softc),
     67     ppbmatch, ppbattach, ppbdetach, NULL, NULL, ppbchilddet,
     68     DVF_DETACH_SHUTDOWN);
     69 
     70 static int
     71 ppbmatch(device_t parent, cfdata_t match, void *aux)
     72 {
     73 	struct pci_attach_args *pa = aux;
     74 
     75 	/*
     76 	 * Check the ID register to see that it's a PCI bridge.
     77 	 * If it is, we assume that we can deal with it; it _should_
     78 	 * work in a standardized way...
     79 	 */
     80 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
     81 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI)
     82 		return 1;
     83 
     84 #ifdef __powerpc__
     85 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PROCESSOR &&
     86 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PROCESSOR_POWERPC) {
     87 		pcireg_t bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag,
     88 		    PCI_BHLC_REG);
     89 		if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_FREESCALE
     90 		    && PCI_HDRTYPE(bhlc) == PCI_HDRTYPE_RC)
     91 		return 1;
     92 	}
     93 #endif
     94 
     95 #ifdef _MIPS_PADDR_T_64BIT
     96 	/* The LDT HB acts just like a PPB.  */
     97 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SIBYTE
     98 	    && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SIBYTE_BCM1250_LDTHB)
     99 		return 1;
    100 #endif
    101 
    102 	return 0;
    103 }
    104 
    105 static void
    106 ppb_print_pcie(device_t self)
    107 {
    108 	struct ppb_softc *sc = device_private(self);
    109 	pcireg_t reg;
    110 	int off, capversion, devtype;
    111 
    112 	if (!pci_get_capability(sc->sc_pc, sc->sc_tag, PCI_CAP_PCIEXPRESS,
    113 				&off, &reg))
    114 		return; /* Not a PCIe device */
    115 
    116 	capversion = PCIE_XCAP_VER(reg);
    117 	devtype = PCIE_XCAP_TYPE(reg);
    118 	aprint_normal_dev(self, "PCI Express capability version ");
    119 	switch (capversion) {
    120 	case PCIE_XCAP_VER_1:
    121 		aprint_normal("1");
    122 		break;
    123 	case PCIE_XCAP_VER_2:
    124 		aprint_normal("2");
    125 		break;
    126 	default:
    127 		aprint_normal_dev(self, "unsupported (%d)\n", capversion);
    128 		return;
    129 	}
    130 	aprint_normal(" <");
    131 	switch (devtype) {
    132 	case PCIE_XCAP_TYPE_PCIE_DEV:
    133 		aprint_normal("PCI-E Endpoint device");
    134 		break;
    135 	case PCIE_XCAP_TYPE_PCI_DEV:
    136 		aprint_normal("Legacy PCI-E Endpoint device");
    137 		break;
    138 	case PCIE_XCAP_TYPE_ROOT:
    139 		aprint_normal("Root Port of PCI-E Root Complex");
    140 		break;
    141 	case PCIE_XCAP_TYPE_UP:
    142 		aprint_normal("Upstream Port of PCI-E Switch");
    143 		break;
    144 	case PCIE_XCAP_TYPE_DOWN:
    145 		aprint_normal("Downstream Port of PCI-E Switch");
    146 		break;
    147 	case PCIE_XCAP_TYPE_PCIE2PCI:
    148 		aprint_normal("PCI-E to PCI/PCI-X Bridge");
    149 		break;
    150 	case PCIE_XCAP_TYPE_PCI2PCIE:
    151 		aprint_normal("PCI/PCI-X to PCI-E Bridge");
    152 		break;
    153 	default:
    154 		aprint_normal("Device/Port Type %x", devtype);
    155 		break;
    156 	}
    157 
    158 	switch (devtype) {
    159 	case PCIE_XCAP_TYPE_ROOT:
    160 	case PCIE_XCAP_TYPE_DOWN:
    161 	case PCIE_XCAP_TYPE_PCI2PCIE:
    162 		reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCAP);
    163 		u_int mlw = __SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH);
    164 		u_int mls = __SHIFTOUT(reg, PCIE_LCAP_MAX_SPEED);
    165 
    166 		if (mls < __arraycount(pcie_linkspeed_strings)) {
    167 			aprint_normal("> x%d @ %sGT/s\n",
    168 			    mlw, pcie_linkspeed_strings[mls]);
    169 		} else {
    170 			aprint_normal("> x%d @ %d.%dGT/s\n",
    171 			    mlw, (mls * 25) / 10, (mls * 25) % 10);
    172 		}
    173 
    174 		reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCSR);
    175 		if (reg & PCIE_LCSR_DLACTIVE) {	/* DLLA */
    176 			u_int lw = __SHIFTOUT(reg, PCIE_LCSR_NLW);
    177 			u_int ls = __SHIFTOUT(reg, PCIE_LCSR_LINKSPEED);
    178 
    179 			if (lw != mlw || ls != mls) {
    180 				if (ls < __arraycount(pcie_linkspeed_strings)) {
    181 					aprint_normal_dev(self,
    182 					    "link is x%d @ %sGT/s\n",
    183 					    lw, pcie_linkspeed_strings[ls]);
    184 				} else {
    185 					aprint_normal_dev(self,
    186 					    "link is x%d @ %d.%dGT/s\n",
    187 					    lw, (ls * 25) / 10, (ls * 25) % 10);
    188 				}
    189 			}
    190 		}
    191 		break;
    192 	default:
    193 		aprint_normal(">\n");
    194 		break;
    195 	}
    196 }
    197 
    198 static void
    199 ppbattach(device_t parent, device_t self, void *aux)
    200 {
    201 	struct ppb_softc *sc = device_private(self);
    202 	struct pci_attach_args *pa = aux;
    203 	pci_chipset_tag_t pc = pa->pa_pc;
    204 	struct pcibus_attach_args pba;
    205 	char const *intrstr;
    206 	char intrbuf[PCI_INTRSTR_LEN];
    207 	pcireg_t busdata, reg;
    208 
    209 	pci_aprint_devinfo(pa, NULL);
    210 
    211 	sc->sc_pc = pc;
    212 	sc->sc_tag = pa->pa_tag;
    213 	sc->sc_dev = self;
    214 
    215 	busdata = pci_conf_read(pc, pa->pa_tag, PPB_REG_BUSINFO);
    216 
    217 	if (PPB_BUSINFO_SECONDARY(busdata) == 0) {
    218 		aprint_normal_dev(self, "not configured by system firmware\n");
    219 		return;
    220 	}
    221 
    222 	ppb_print_pcie(self);
    223 
    224 #if 0
    225 	/*
    226 	 * XXX can't do this, because we're not given our bus number
    227 	 * (we shouldn't need it), and because we've no way to
    228 	 * decompose our tag.
    229 	 */
    230 	/* sanity check. */
    231 	if (pa->pa_bus != PPB_BUSINFO_PRIMARY(busdata))
    232 		panic("ppbattach: bus in tag (%d) != bus in reg (%d)",
    233 		    pa->pa_bus, PPB_BUSINFO_PRIMARY(busdata));
    234 #endif
    235 
    236 	/* Check for PCI Express capabilities and setup hotplug support. */
    237 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
    238 	    &sc->sc_pciecapoff, &reg) && (reg & PCIE_XCAP_SI)) {
    239 #if 0
    240 		/*
    241 		 * XXX Initialize workqueue or something else for
    242 		 * HotPlug support.
    243 		 */
    244 #endif
    245 
    246 		if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) == 0)
    247 			sc->sc_intrhand = pci_intr_establish_xname(pc,
    248 			    sc->sc_pihp[0], IPL_BIO, ppb_intr, sc,
    249 			    device_xname(sc->sc_dev));
    250 
    251 		if (sc->sc_intrhand) {
    252 			pcireg_t slcap, slcsr, val;
    253 
    254 			intrstr = pci_intr_string(pc, sc->sc_pihp[0], intrbuf,
    255 			    sizeof(intrbuf));
    256 			aprint_normal_dev(self, "%s\n", intrstr);
    257 
    258 			/* Clear any pending events */
    259 			slcsr = pci_conf_read(pc, pa->pa_tag,
    260 			    sc->sc_pciecapoff + PCIE_SLCSR);
    261 			pci_conf_write(pc, pa->pa_tag,
    262 			    sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
    263 
    264 			/* Enable interrupt. */
    265 			slcap = pci_conf_read(pc, pa->pa_tag,
    266 			    sc->sc_pciecapoff + PCIE_SLCAP);
    267 			val = 0;
    268 			if (slcap & PCIE_SLCAP_ABP)
    269 				val |= PCIE_SLCSR_ABE;
    270 			if (slcap & PCIE_SLCAP_PCP)
    271 				val |= PCIE_SLCSR_PFE;
    272 			if (slcap & PCIE_SLCAP_MSP)
    273 				val |= PCIE_SLCSR_MSE;
    274 			if ((slcap & PCIE_SLCAP_NCCS) == 0)
    275 				val |= PCIE_SLCSR_CCE;
    276 			/* Attention indicator off by default */
    277 			if (slcap & PCIE_SLCAP_AIP) {
    278 				val |= __SHIFTIN(PCIE_SLCSR_IND_OFF,
    279 				    PCIE_SLCSR_AIC);
    280 			}
    281 			/* Power indicator */
    282 			if (slcap & PCIE_SLCAP_PIP) {
    283 				/*
    284 				 * Indicator off:
    285 				 *  a) card not present
    286 				 *  b) power fault
    287 				 *  c) MRL sensor off
    288 				 */
    289 				if (((slcsr & PCIE_SLCSR_PDS) == 0)
    290 				    || ((slcsr & PCIE_SLCSR_PFD) != 0)
    291 				    || (((slcap & PCIE_SLCAP_MSP) != 0)
    292 					&& ((slcsr & PCIE_SLCSR_MS) != 0)))
    293 					val |= __SHIFTIN(PCIE_SLCSR_IND_OFF,
    294 					    PCIE_SLCSR_PIC);
    295 				else
    296 					val |= __SHIFTIN(PCIE_SLCSR_IND_ON,
    297 					    PCIE_SLCSR_PIC);
    298 			}
    299 
    300 			val |= PCIE_SLCSR_DLLSCE | PCIE_SLCSR_HPE
    301 			    | PCIE_SLCSR_PDE;
    302 			slcsr = val;
    303 			pci_conf_write(pc, pa->pa_tag,
    304 			    sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
    305 		}
    306 	}
    307 
    308 	if (!pmf_device_register(self, ppb_suspend, ppb_resume))
    309 		aprint_error_dev(self, "couldn't establish power handler\n");
    310 
    311 	/*
    312 	 * Attach the PCI bus than hangs off of it.
    313 	 *
    314 	 * XXX Don't pass-through Memory Read Multiple.  Should we?
    315 	 * XXX Consult the spec...
    316 	 */
    317 	pba.pba_iot = pa->pa_iot;
    318 	pba.pba_memt = pa->pa_memt;
    319 	pba.pba_dmat = pa->pa_dmat;
    320 	pba.pba_dmat64 = pa->pa_dmat64;
    321 	pba.pba_pc = pc;
    322 	pba.pba_flags = pa->pa_flags & ~PCI_FLAGS_MRM_OKAY;
    323 	pba.pba_bus = PPB_BUSINFO_SECONDARY(busdata);
    324 	pba.pba_sub = PPB_BUSINFO_SUBORDINATE(busdata);
    325 	pba.pba_bridgetag = &sc->sc_tag;
    326 	pba.pba_intrswiz = pa->pa_intrswiz;
    327 	pba.pba_intrtag = pa->pa_intrtag;
    328 
    329 	/* Attach event counters */
    330 	evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR, NULL,
    331 	    device_xname(sc->sc_dev), "Interrupt");
    332 	evcnt_attach_dynamic(&sc->sc_ev_abp, EVCNT_TYPE_MISC, NULL,
    333 	    device_xname(sc->sc_dev), "Attention Button Pressed");
    334 	evcnt_attach_dynamic(&sc->sc_ev_pfd, EVCNT_TYPE_MISC, NULL,
    335 	    device_xname(sc->sc_dev), "Power Fault Detected");
    336 	evcnt_attach_dynamic(&sc->sc_ev_msc, EVCNT_TYPE_MISC, NULL,
    337 	    device_xname(sc->sc_dev), "MRL Sensor Changed");
    338 	evcnt_attach_dynamic(&sc->sc_ev_pdc, EVCNT_TYPE_MISC, NULL,
    339 	    device_xname(sc->sc_dev), "Presence Detect Changed");
    340 	evcnt_attach_dynamic(&sc->sc_ev_cc, EVCNT_TYPE_MISC, NULL,
    341 	    device_xname(sc->sc_dev), "Command Completed");
    342 	evcnt_attach_dynamic(&sc->sc_ev_lacs, EVCNT_TYPE_MISC, NULL,
    343 	    device_xname(sc->sc_dev), "Data Link Layer State Changed");
    344 
    345 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    346 }
    347 
    348 static int
    349 ppbdetach(device_t self, int flags)
    350 {
    351 	struct ppb_softc *sc = device_private(self);
    352 	int rc;
    353 
    354 	/* Detach event counters */
    355 	evcnt_detach(&sc->sc_ev_intr);
    356 	evcnt_detach(&sc->sc_ev_abp);
    357 	evcnt_detach(&sc->sc_ev_pfd);
    358 	evcnt_detach(&sc->sc_ev_msc);
    359 	evcnt_detach(&sc->sc_ev_pdc);
    360 	evcnt_detach(&sc->sc_ev_cc);
    361 	evcnt_detach(&sc->sc_ev_lacs);
    362 
    363 	if ((rc = config_detach_children(self, flags)) != 0)
    364 		return rc;
    365 	pmf_device_deregister(self);
    366 	return 0;
    367 }
    368 
    369 static bool
    370 ppb_resume(device_t dv, const pmf_qual_t *qual)
    371 {
    372 	struct ppb_softc *sc = device_private(dv);
    373 	int off;
    374 	pcireg_t val;
    375 
    376         for (off = 0x40; off <= 0xff; off += 4) {
    377 		val = pci_conf_read(sc->sc_pc, sc->sc_tag, off);
    378 		if (val != sc->sc_pciconfext[(off - 0x40) / 4])
    379 			pci_conf_write(sc->sc_pc, sc->sc_tag, off,
    380 			    sc->sc_pciconfext[(off - 0x40)/4]);
    381 	}
    382 
    383 	return true;
    384 }
    385 
    386 static bool
    387 ppb_suspend(device_t dv, const pmf_qual_t *qual)
    388 {
    389 	struct ppb_softc *sc = device_private(dv);
    390 	int off;
    391 
    392 	for (off = 0x40; off <= 0xff; off += 4)
    393 		sc->sc_pciconfext[(off - 0x40) / 4] =
    394 		    pci_conf_read(sc->sc_pc, sc->sc_tag, off);
    395 
    396 	return true;
    397 }
    398 
    399 static void
    400 ppbchilddet(device_t self, device_t child)
    401 {
    402 	/* we keep no references to child devices, so do nothing */
    403 }
    404 
    405 static int
    406 ppb_intr(void *arg)
    407 {
    408 	struct ppb_softc *sc = arg;
    409 	device_t dev = sc->sc_dev;
    410 	pcireg_t reg;
    411 
    412 	sc->sc_ev_intr.ev_count++;
    413 	reg = pci_conf_read(sc->sc_pc, sc->sc_tag,
    414 	    sc->sc_pciecapoff + PCIE_SLCSR);
    415 
    416 	/* Clear interrupts. */
    417 	pci_conf_write(sc->sc_pc, sc->sc_tag,
    418 	    sc->sc_pciecapoff + PCIE_SLCSR, reg);
    419 
    420 	/* Attention Button Pressed */
    421 	if (reg & PCIE_SLCSR_ABP) {
    422 		sc->sc_ev_abp.ev_count++;
    423 		if (ppb_printevent)
    424 			device_printf(dev, "Attention Button Pressed\n");
    425 	}
    426 
    427 	/* Power Fault Detected */
    428 	if (reg & PCIE_SLCSR_PFD) {
    429 		sc->sc_ev_pfd.ev_count++;
    430 		if (ppb_printevent)
    431 			device_printf(dev, "Power Fault Detected\n");
    432 	}
    433 
    434 	/* MRL Sensor Changed */
    435 	if (reg & PCIE_SLCSR_MSC) {
    436 		sc->sc_ev_msc.ev_count++;
    437 		if (ppb_printevent)
    438 			device_printf(dev, "MRL Sensor Changed\n");
    439 	}
    440 
    441 	/* Presence Detect Changed */
    442 	if (reg & PCIE_SLCSR_PDC) {
    443 		sc->sc_ev_pdc.ev_count++;
    444 		if (ppb_printevent)
    445 			device_printf(dev, "Presence Detect Changed\n");
    446 		if (reg & PCIE_SLCSR_PDS) {
    447 			/* XXX Insert */
    448 		} else {
    449 			/* XXX Remove */
    450 		}
    451 	}
    452 
    453 	/* Command Completed */
    454 	if (reg & PCIE_SLCSR_CC) {
    455 		sc->sc_ev_cc.ev_count++;
    456 		if (ppb_printevent)
    457 			device_printf(dev, "Command Completed\n");
    458 	}
    459 
    460 	/* Data Link Layer State Changed */
    461 	if (reg & PCIE_SLCSR_LACS) {
    462 		sc->sc_ev_lacs.ev_count++;
    463 		if (ppb_printevent)
    464 			device_printf(dev, "Data Link Layer State Changed\n");
    465 	}
    466 
    467 	return 0;
    468 }
    469