ppb.c revision 1.64 1 /* $NetBSD: ppb.c,v 1.64 2018/12/07 09:36:26 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.64 2018/12/07 09:36:26 msaitoh Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_ppb.h"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/evcnt.h>
45
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/ppbreg.h>
49 #include <dev/pci/ppbvar.h>
50 #include <dev/pci/pcidevs.h>
51
52 #define PCIE_SLCSR_ENABLE_MASK \
53 (PCIE_SLCSR_ABE | PCIE_SLCSR_PFE | PCIE_SLCSR_MSE | \
54 PCIE_SLCSR_PDE | PCIE_SLCSR_CCE | PCIE_SLCSR_HPE | \
55 PCIE_SLCSR_DLLSCE)
56
57 #define PCIE_SLCSR_STATCHG_MASK \
58 (PCIE_SLCSR_ABP | PCIE_SLCSR_PFD | PCIE_SLCSR_MSC | \
59 PCIE_SLCSR_PDC | PCIE_SLCSR_CC | PCIE_SLCSR_LACS)
60
61 static const char pcie_linkspeed_strings[4][5] = {
62 "1.25", "2.5", "5.0", "8.0",
63 };
64
65 int ppb_printevent = 0; /* Print event type if the value is not 0 */
66
67 static int ppbmatch(device_t, cfdata_t, void *);
68 static void ppbattach(device_t, device_t, void *);
69 static int ppbdetach(device_t, int);
70 static void ppbchilddet(device_t, device_t);
71 #ifdef PPB_USEINTR
72 static int ppb_intr(void *);
73 #endif
74 static bool ppb_resume(device_t, const pmf_qual_t *);
75 static bool ppb_suspend(device_t, const pmf_qual_t *);
76
77 CFATTACH_DECL3_NEW(ppb, sizeof(struct ppb_softc),
78 ppbmatch, ppbattach, ppbdetach, NULL, NULL, ppbchilddet,
79 DVF_DETACH_SHUTDOWN);
80
81 static int
82 ppbmatch(device_t parent, cfdata_t match, void *aux)
83 {
84 struct pci_attach_args *pa = aux;
85
86 /*
87 * Check the ID register to see that it's a PCI bridge.
88 * If it is, we assume that we can deal with it; it _should_
89 * work in a standardized way...
90 */
91 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
92 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI)
93 return 1;
94
95 #ifdef __powerpc__
96 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PROCESSOR &&
97 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PROCESSOR_POWERPC) {
98 pcireg_t bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag,
99 PCI_BHLC_REG);
100 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_FREESCALE
101 && PCI_HDRTYPE(bhlc) == PCI_HDRTYPE_RC)
102 return 1;
103 }
104 #endif
105
106 #ifdef _MIPS_PADDR_T_64BIT
107 /* The LDT HB acts just like a PPB. */
108 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SIBYTE
109 && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SIBYTE_BCM1250_LDTHB)
110 return 1;
111 #endif
112
113 return 0;
114 }
115
116 static void
117 ppb_print_pcie(device_t self)
118 {
119 struct ppb_softc *sc = device_private(self);
120 pcireg_t reg;
121 int off, capversion, devtype;
122
123 if (!pci_get_capability(sc->sc_pc, sc->sc_tag, PCI_CAP_PCIEXPRESS,
124 &off, ®))
125 return; /* Not a PCIe device */
126
127 capversion = PCIE_XCAP_VER(reg);
128 devtype = PCIE_XCAP_TYPE(reg);
129 aprint_normal_dev(self, "PCI Express capability version ");
130 switch (capversion) {
131 case PCIE_XCAP_VER_1:
132 aprint_normal("1");
133 break;
134 case PCIE_XCAP_VER_2:
135 aprint_normal("2");
136 break;
137 default:
138 aprint_normal_dev(self, "unsupported (%d)\n", capversion);
139 return;
140 }
141 aprint_normal(" <");
142 switch (devtype) {
143 case PCIE_XCAP_TYPE_PCIE_DEV:
144 aprint_normal("PCI-E Endpoint device");
145 break;
146 case PCIE_XCAP_TYPE_PCI_DEV:
147 aprint_normal("Legacy PCI-E Endpoint device");
148 break;
149 case PCIE_XCAP_TYPE_ROOT:
150 aprint_normal("Root Port of PCI-E Root Complex");
151 break;
152 case PCIE_XCAP_TYPE_UP:
153 aprint_normal("Upstream Port of PCI-E Switch");
154 break;
155 case PCIE_XCAP_TYPE_DOWN:
156 aprint_normal("Downstream Port of PCI-E Switch");
157 break;
158 case PCIE_XCAP_TYPE_PCIE2PCI:
159 aprint_normal("PCI-E to PCI/PCI-X Bridge");
160 break;
161 case PCIE_XCAP_TYPE_PCI2PCIE:
162 aprint_normal("PCI/PCI-X to PCI-E Bridge");
163 break;
164 default:
165 aprint_normal("Device/Port Type %x", devtype);
166 break;
167 }
168
169 switch (devtype) {
170 case PCIE_XCAP_TYPE_ROOT:
171 case PCIE_XCAP_TYPE_DOWN:
172 case PCIE_XCAP_TYPE_PCI2PCIE:
173 reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCAP);
174 u_int mlw = __SHIFTOUT(reg, PCIE_LCAP_MAX_WIDTH);
175 u_int mls = __SHIFTOUT(reg, PCIE_LCAP_MAX_SPEED);
176
177 if (mls < __arraycount(pcie_linkspeed_strings)) {
178 aprint_normal("> x%d @ %sGT/s\n",
179 mlw, pcie_linkspeed_strings[mls]);
180 } else {
181 aprint_normal("> x%d @ %d.%dGT/s\n",
182 mlw, (mls * 25) / 10, (mls * 25) % 10);
183 }
184
185 reg = pci_conf_read(sc->sc_pc, sc->sc_tag, off + PCIE_LCSR);
186 if (reg & PCIE_LCSR_DLACTIVE) { /* DLLA */
187 u_int lw = __SHIFTOUT(reg, PCIE_LCSR_NLW);
188 u_int ls = __SHIFTOUT(reg, PCIE_LCSR_LINKSPEED);
189
190 if (lw != mlw || ls != mls) {
191 if (ls < __arraycount(pcie_linkspeed_strings)) {
192 aprint_normal_dev(self,
193 "link is x%d @ %sGT/s\n",
194 lw, pcie_linkspeed_strings[ls]);
195 } else {
196 aprint_normal_dev(self,
197 "link is x%d @ %d.%dGT/s\n",
198 lw, (ls * 25) / 10, (ls * 25) % 10);
199 }
200 }
201 }
202 break;
203 default:
204 aprint_normal(">\n");
205 break;
206 }
207 }
208
209 static void
210 ppbattach(device_t parent, device_t self, void *aux)
211 {
212 struct ppb_softc *sc = device_private(self);
213 struct pci_attach_args *pa = aux;
214 pci_chipset_tag_t pc = pa->pa_pc;
215 struct pcibus_attach_args pba;
216 #ifdef PPB_USEINTR
217 char const *intrstr;
218 char intrbuf[PCI_INTRSTR_LEN];
219 #endif
220 pcireg_t busdata, reg;
221
222 pci_aprint_devinfo(pa, NULL);
223
224 sc->sc_pc = pc;
225 sc->sc_tag = pa->pa_tag;
226 sc->sc_dev = self;
227
228 busdata = pci_conf_read(pc, pa->pa_tag, PPB_REG_BUSINFO);
229
230 if (PPB_BUSINFO_SECONDARY(busdata) == 0) {
231 aprint_normal_dev(self, "not configured by system firmware\n");
232 return;
233 }
234
235 ppb_print_pcie(self);
236
237 #if 0
238 /*
239 * XXX can't do this, because we're not given our bus number
240 * (we shouldn't need it), and because we've no way to
241 * decompose our tag.
242 */
243 /* sanity check. */
244 if (pa->pa_bus != PPB_BUSINFO_PRIMARY(busdata))
245 panic("ppbattach: bus in tag (%d) != bus in reg (%d)",
246 pa->pa_bus, PPB_BUSINFO_PRIMARY(busdata));
247 #endif
248
249 /* Check for PCI Express capabilities and setup hotplug support. */
250 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
251 &sc->sc_pciecapoff, ®) && (reg & PCIE_XCAP_SI)) {
252 /*
253 * First, disable all interrupts because BIOS might
254 * enable them.
255 */
256 reg = pci_conf_read(sc->sc_pc, sc->sc_tag,
257 sc->sc_pciecapoff + PCIE_SLCSR);
258 if (reg & PCIE_SLCSR_ENABLE_MASK) {
259 reg &= ~PCIE_SLCSR_ENABLE_MASK;
260 pci_conf_write(sc->sc_pc, sc->sc_tag,
261 sc->sc_pciecapoff + PCIE_SLCSR, reg);
262 }
263 #ifdef PPB_USEINTR
264 #if 0 /* notyet */
265 /*
266 * XXX Initialize workqueue or something else for
267 * HotPlug support.
268 */
269 #endif
270 if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) == 0)
271 sc->sc_intrhand = pci_intr_establish_xname(pc,
272 sc->sc_pihp[0], IPL_BIO, ppb_intr, sc,
273 device_xname(sc->sc_dev));
274 #endif
275 }
276
277 #ifdef PPB_USEINTR
278 if (sc->sc_intrhand != NULL) {
279 pcireg_t slcap, slcsr, val;
280
281 intrstr = pci_intr_string(pc, sc->sc_pihp[0], intrbuf,
282 sizeof(intrbuf));
283 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
284
285 /* Clear any pending events */
286 slcsr = pci_conf_read(pc, pa->pa_tag,
287 sc->sc_pciecapoff + PCIE_SLCSR);
288 pci_conf_write(pc, pa->pa_tag,
289 sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
290
291 /* Enable interrupt. */
292 val = 0;
293 slcap = pci_conf_read(pc, pa->pa_tag,
294 sc->sc_pciecapoff + PCIE_SLCAP);
295 if (slcap & PCIE_SLCAP_ABP)
296 val |= PCIE_SLCSR_ABE;
297 if (slcap & PCIE_SLCAP_PCP)
298 val |= PCIE_SLCSR_PFE;
299 if (slcap & PCIE_SLCAP_MSP)
300 val |= PCIE_SLCSR_MSE;
301 #if 0
302 /*
303 * XXX Disable for a while because setting
304 * PCIE_SLCSR_CCE makes break device access on
305 * some environment.
306 */
307 if ((slcap & PCIE_SLCAP_NCCS) == 0)
308 val |= PCIE_SLCSR_CCE;
309 #endif
310 /* Attention indicator off by default */
311 if (slcap & PCIE_SLCAP_AIP) {
312 val |= __SHIFTIN(PCIE_SLCSR_IND_OFF,
313 PCIE_SLCSR_AIC);
314 }
315 /* Power indicator */
316 if (slcap & PCIE_SLCAP_PIP) {
317 /*
318 * Indicator off:
319 * a) card not present
320 * b) power fault
321 * c) MRL sensor off
322 */
323 if (((slcsr & PCIE_SLCSR_PDS) == 0)
324 || ((slcsr & PCIE_SLCSR_PFD) != 0)
325 || (((slcap & PCIE_SLCAP_MSP) != 0)
326 && ((slcsr & PCIE_SLCSR_MS) != 0)))
327 val |= __SHIFTIN(PCIE_SLCSR_IND_OFF,
328 PCIE_SLCSR_PIC);
329 else
330 val |= __SHIFTIN(PCIE_SLCSR_IND_ON,
331 PCIE_SLCSR_PIC);
332 }
333
334 val |= PCIE_SLCSR_DLLSCE | PCIE_SLCSR_HPE | PCIE_SLCSR_PDE;
335 slcsr = val;
336 pci_conf_write(pc, pa->pa_tag,
337 sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
338
339 /* Attach event counters */
340 evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR, NULL,
341 device_xname(sc->sc_dev), "Interrupt");
342 evcnt_attach_dynamic(&sc->sc_ev_abp, EVCNT_TYPE_MISC, NULL,
343 device_xname(sc->sc_dev), "Attention Button Pressed");
344 evcnt_attach_dynamic(&sc->sc_ev_pfd, EVCNT_TYPE_MISC, NULL,
345 device_xname(sc->sc_dev), "Power Fault Detected");
346 evcnt_attach_dynamic(&sc->sc_ev_msc, EVCNT_TYPE_MISC, NULL,
347 device_xname(sc->sc_dev), "MRL Sensor Changed");
348 evcnt_attach_dynamic(&sc->sc_ev_pdc, EVCNT_TYPE_MISC, NULL,
349 device_xname(sc->sc_dev), "Presence Detect Changed");
350 evcnt_attach_dynamic(&sc->sc_ev_cc, EVCNT_TYPE_MISC, NULL,
351 device_xname(sc->sc_dev), "Command Completed");
352 evcnt_attach_dynamic(&sc->sc_ev_lacs, EVCNT_TYPE_MISC, NULL,
353 device_xname(sc->sc_dev), "Data Link Layer State Changed");
354 }
355 #endif /* PPB_USEINTR */
356
357 if (!pmf_device_register(self, ppb_suspend, ppb_resume))
358 aprint_error_dev(self, "couldn't establish power handler\n");
359
360 /*
361 * Attach the PCI bus that hangs off of it.
362 *
363 * XXX Don't pass-through Memory Read Multiple. Should we?
364 * XXX Consult the spec...
365 */
366 pba.pba_iot = pa->pa_iot;
367 pba.pba_memt = pa->pa_memt;
368 pba.pba_dmat = pa->pa_dmat;
369 pba.pba_dmat64 = pa->pa_dmat64;
370 pba.pba_pc = pc;
371 pba.pba_flags = pa->pa_flags & ~PCI_FLAGS_MRM_OKAY;
372 pba.pba_bus = PPB_BUSINFO_SECONDARY(busdata);
373 pba.pba_sub = PPB_BUSINFO_SUBORDINATE(busdata);
374 pba.pba_bridgetag = &sc->sc_tag;
375 pba.pba_intrswiz = pa->pa_intrswiz;
376 pba.pba_intrtag = pa->pa_intrtag;
377
378 config_found_ia(self, "pcibus", &pba, pcibusprint);
379 }
380
381 static int
382 ppbdetach(device_t self, int flags)
383 {
384 #ifdef PPB_USEINTR
385 struct ppb_softc *sc = device_private(self);
386 pcireg_t slcsr;
387 #endif
388 int rc;
389
390 if ((rc = config_detach_children(self, flags)) != 0)
391 return rc;
392
393 #ifdef PPB_USEINTR
394 if (sc->sc_intrhand != NULL) {
395 /* Detach event counters */
396 evcnt_detach(&sc->sc_ev_intr);
397 evcnt_detach(&sc->sc_ev_abp);
398 evcnt_detach(&sc->sc_ev_pfd);
399 evcnt_detach(&sc->sc_ev_msc);
400 evcnt_detach(&sc->sc_ev_pdc);
401 evcnt_detach(&sc->sc_ev_cc);
402 evcnt_detach(&sc->sc_ev_lacs);
403
404 /* Clear any pending events and disable interrupt */
405 slcsr = pci_conf_read(sc->sc_pc, sc->sc_tag,
406 sc->sc_pciecapoff + PCIE_SLCSR);
407 slcsr &= ~PCIE_SLCSR_ENABLE_MASK;
408 pci_conf_write(sc->sc_pc, sc->sc_tag,
409 sc->sc_pciecapoff + PCIE_SLCSR, slcsr);
410
411 /* Disestablish the interrupt handler */
412 pci_intr_disestablish(sc->sc_pc, sc->sc_intrhand);
413 pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
414 }
415 #endif
416
417 pmf_device_deregister(self);
418 return 0;
419 }
420
421 static bool
422 ppb_resume(device_t dv, const pmf_qual_t *qual)
423 {
424 struct ppb_softc *sc = device_private(dv);
425 int off;
426 pcireg_t val;
427
428 for (off = 0x40; off <= 0xff; off += 4) {
429 val = pci_conf_read(sc->sc_pc, sc->sc_tag, off);
430 if (val != sc->sc_pciconfext[(off - 0x40) / 4])
431 pci_conf_write(sc->sc_pc, sc->sc_tag, off,
432 sc->sc_pciconfext[(off - 0x40)/4]);
433 }
434
435 return true;
436 }
437
438 static bool
439 ppb_suspend(device_t dv, const pmf_qual_t *qual)
440 {
441 struct ppb_softc *sc = device_private(dv);
442 int off;
443
444 for (off = 0x40; off <= 0xff; off += 4)
445 sc->sc_pciconfext[(off - 0x40) / 4] =
446 pci_conf_read(sc->sc_pc, sc->sc_tag, off);
447
448 return true;
449 }
450
451 static void
452 ppbchilddet(device_t self, device_t child)
453 {
454 /* we keep no references to child devices, so do nothing */
455 }
456
457 #ifdef PPB_USEINTR
458 static int
459 ppb_intr(void *arg)
460 {
461 struct ppb_softc *sc = arg;
462 device_t dev = sc->sc_dev;
463 pcireg_t reg;
464
465 reg = pci_conf_read(sc->sc_pc, sc->sc_tag,
466 sc->sc_pciecapoff + PCIE_SLCSR);
467
468 /*
469 * Not me. This check is only required for INTx.
470 * ppb_intr() would be spilted int ppb_intr_legacy() and ppb_intr_msi()
471 */
472 if ((reg & PCIE_SLCSR_STATCHG_MASK) == 0)
473 return 0;
474
475 /* Clear interrupts. */
476 pci_conf_write(sc->sc_pc, sc->sc_tag,
477 sc->sc_pciecapoff + PCIE_SLCSR, reg);
478
479 sc->sc_ev_intr.ev_count++;
480
481 /* Attention Button Pressed */
482 if (reg & PCIE_SLCSR_ABP) {
483 sc->sc_ev_abp.ev_count++;
484 if (ppb_printevent)
485 device_printf(dev, "Attention Button Pressed\n");
486 }
487
488 /* Power Fault Detected */
489 if (reg & PCIE_SLCSR_PFD) {
490 sc->sc_ev_pfd.ev_count++;
491 if (ppb_printevent)
492 device_printf(dev, "Power Fault Detected\n");
493 }
494
495 /* MRL Sensor Changed */
496 if (reg & PCIE_SLCSR_MSC) {
497 sc->sc_ev_msc.ev_count++;
498 if (ppb_printevent)
499 device_printf(dev, "MRL Sensor Changed\n");
500 }
501
502 /* Presence Detect Changed */
503 if (reg & PCIE_SLCSR_PDC) {
504 sc->sc_ev_pdc.ev_count++;
505 if (ppb_printevent)
506 device_printf(dev, "Presence Detect Changed\n");
507 if (reg & PCIE_SLCSR_PDS) {
508 /* XXX Insert */
509 } else {
510 /* XXX Remove */
511 }
512 }
513
514 /* Command Completed */
515 if (reg & PCIE_SLCSR_CC) {
516 sc->sc_ev_cc.ev_count++;
517 if (ppb_printevent)
518 device_printf(dev, "Command Completed\n");
519 }
520
521 /* Data Link Layer State Changed */
522 if (reg & PCIE_SLCSR_LACS) {
523 sc->sc_ev_lacs.ev_count++;
524 if (ppb_printevent)
525 device_printf(dev, "Data Link Layer State Changed\n");
526 }
527
528 return 1;
529 }
530 #endif /* PPB_USEINTR */
531