rmixl_pcie.c revision 1.1.2.21 1 /* $NetBSD: rmixl_pcie.c,v 1.1.2.21 2012/01/04 16:17:54 matt Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * PCI configuration support for RMI XLS SoC
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: rmixl_pcie.c,v 1.1.2.21 2012/01/04 16:17:54 matt Exp $");
44
45 #include "opt_pci.h"
46 #include "pci.h"
47
48 #include <sys/cdefs.h>
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/extent.h>
54 #include <sys/malloc.h>
55 #include <sys/kernel.h> /* for 'hz' */
56 #include <sys/cpu.h>
57
58 #include <uvm/uvm_extern.h>
59
60 #include <machine/bus.h>
61 #include <machine/intr.h>
62
63 #include <mips/rmi/rmixlreg.h>
64 #include <mips/rmi/rmixlvar.h>
65 #include <mips/rmi/rmixl_intr.h>
66 #include <mips/rmi/rmixl_pcievar.h>
67
68 #include <mips/rmi/rmixl_obiovar.h>
69
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pcidevs.h>
72 #include <dev/pci/pciconf.h>
73
74 #ifdef PCI_NETBSD_CONFIGURE
75 #include <mips/cache.h>
76 #endif
77
78 #include <machine/pci_machdep.h>
79
80 #ifdef PCI_DEBUG
81 int rmixl_pcie_debug = PCI_DEBUG;
82 # define DPRINTF(x) do { if (rmixl_pcie_debug) printf x ; } while (0)
83 #else
84 # define DPRINTF(x)
85 #endif
86
87 #ifndef DDB
88 # define STATIC static
89 #else
90 # define STATIC
91 #endif
92
93
94 /*
95 * XLS PCIe Extended Configuration Registers
96 */
97 #define RMIXL_PCIE_ECFG_UESR 0x104 /* Uncorrectable Error Status Reg */
98 #define RMIXL_PCIE_ECFG_UEMR 0x108 /* Uncorrectable Error Mask Reg */
99 #define RMIXL_PCIE_ECFG_UEVR 0x10c /* Uncorrectable Error seVerity Reg */
100 #define PCIE_ECFG_UEVR_DFLT \
101 (__BITS(18,17) | __BIT(31) | __BITS(5,4) | __BIT(0))
102 #define PCIE_ECFG_UExR_RESV (__BITS(31,21) | __BITS(11,6) | __BITS(3,1))
103 #define RMIXL_PCIE_ECFG_CESR 0x110 /* Correctable Error Status Reg */
104 #define RMIXL_PCIE_ECFG_CEMR 0x114 /* Correctable Error Mask Reg */
105 #define PCIE_ECFG_CExR_RESV (__BITS(31,14) | __BITS(11,9) | __BITS(5,1))
106 #define RMIXL_PCIE_ECFG_ACCR 0x118 /* Adv. Capabilities Control Reg */
107 #define RMIXL_PCIE_ECFG_HLRn(n) (0x11c + ((n) * 4)) /* Header Log Regs */
108 #define RMIXL_PCIE_ECFG_RECR 0x12c /* Root Error Command Reg */
109 #define PCIE_ECFG_RECR_RESV __BITS(31,3)
110 #define RMIXL_PCIE_ECFG_RESR 0x130 /* Root Error Status Reg */
111 #define PCIE_ECFG_RESR_RESV __BITS(26,7)
112 #define RMIXL_PCIE_ECFG_ESI 0x134 /* Error Source Identification Reg */
113 #define RMIXL_PCIE_ECFG_DSNCR 0x140 /* Dev Serial Number Capability Regs */
114
115 static const struct {
116 u_int offset;
117 u_int32_t rw1c;
118 } pcie_ecfg_errs_tab[] = {
119 { RMIXL_PCIE_ECFG_UESR, (__BITS(20,12) | __BIT(4)) },
120 { RMIXL_PCIE_ECFG_CESR, (__BITS(20,12) | __BIT(4)) },
121 { RMIXL_PCIE_ECFG_HLRn(0), 0 },
122 { RMIXL_PCIE_ECFG_HLRn(1), 0 },
123 { RMIXL_PCIE_ECFG_HLRn(2), 0 },
124 { RMIXL_PCIE_ECFG_HLRn(3), 0 },
125 { RMIXL_PCIE_ECFG_RESR, __BITS(6,0) },
126 { RMIXL_PCIE_ECFG_ESI, 0 },
127 };
128 #define PCIE_ECFG_ERRS_OFFTAB_NENTRIES \
129 (sizeof(pcie_ecfg_errs_tab)/sizeof(pcie_ecfg_errs_tab[0]))
130
131 typedef struct rmixl_pcie_int_csr {
132 uint r0;
133 uint r1;
134 } rmixl_pcie_int_csr_t;
135
136 static const rmixl_pcie_int_csr_t int_enb_offset[4] = {
137 { RMIXL_PCIE_LINK0_INT_ENABLE0, RMIXL_PCIE_LINK0_INT_ENABLE1 },
138 { RMIXL_PCIE_LINK1_INT_ENABLE0, RMIXL_PCIE_LINK1_INT_ENABLE1 },
139 { RMIXL_PCIE_LINK2_INT_ENABLE0, RMIXL_PCIE_LINK2_INT_ENABLE1 },
140 { RMIXL_PCIE_LINK3_INT_ENABLE0, RMIXL_PCIE_LINK3_INT_ENABLE1 },
141 };
142
143 static const rmixl_pcie_int_csr_t int_sts_offset[4] = {
144 { RMIXL_PCIE_LINK0_INT_STATUS0, RMIXL_PCIE_LINK0_INT_STATUS1 },
145 { RMIXL_PCIE_LINK1_INT_STATUS0, RMIXL_PCIE_LINK1_INT_STATUS1 },
146 { RMIXL_PCIE_LINK2_INT_STATUS0, RMIXL_PCIE_LINK2_INT_STATUS1 },
147 { RMIXL_PCIE_LINK3_INT_STATUS0, RMIXL_PCIE_LINK3_INT_STATUS1 },
148 };
149
150 static const u_int msi_enb_offset[4] = {
151 RMIXL_PCIE_LINK0_MSI_ENABLE,
152 RMIXL_PCIE_LINK1_MSI_ENABLE,
153 RMIXL_PCIE_LINK2_MSI_ENABLE,
154 RMIXL_PCIE_LINK3_MSI_ENABLE
155 };
156
157 #define RMIXL_PCIE_LINK_STATUS0_ERRORS __BITS(6,4)
158 #define RMIXL_PCIE_LINK_STATUS1_ERRORS __BITS(10,0)
159 #define RMIXL_PCIE_LINK_STATUS_ERRORS \
160 ((((uint64_t)RMIXL_PCIE_LINK_STATUS1_ERRORS) << 32) | \
161 (uint64_t)RMIXL_PCIE_LINK_STATUS0_ERRORS)
162
163 #define RMIXL_PCIE_EVCNT(sc, link, bitno, cpu) \
164 &(sc)->sc_evcnts[link][(bitno) * (ncpu) + (cpu)]
165
166 static int rmixl_pcie_match(device_t, cfdata_t, void *);
167 static void rmixl_pcie_attach(device_t, device_t, void *);
168 static void rmixl_pcie_init(struct rmixl_pcie_softc *);
169 static void rmixl_pcie_init_ecfg(struct rmixl_pcie_softc *);
170 static void rmixl_pcie_attach_hook(struct device *, struct device *,
171 struct pcibus_attach_args *);
172 static void rmixl_pcie_lnkcfg_xls4xx(rmixl_pcie_lnktab_t *, uint32_t);
173 static void rmixl_pcie_lnkcfg_xls408Lite(rmixl_pcie_lnktab_t *, uint32_t);
174 static void rmixl_pcie_lnkcfg_xls2xx(rmixl_pcie_lnktab_t *, uint32_t);
175 static void rmixl_pcie_lnkcfg_xls1xx(rmixl_pcie_lnktab_t *, uint32_t);
176 static void rmixl_pcie_lnkcfg(struct rmixl_pcie_softc *);
177 static void rmixl_pcie_intcfg(struct rmixl_pcie_softc *);
178 static void rmixl_pcie_errata(struct rmixl_pcie_softc *);
179 static void rmixl_conf_interrupt(void *, int, int, int, int, int *);
180 static int rmixl_pcie_bus_maxdevs(void *, int);
181 static pcitag_t rmixl_tag_ecfg_to_cfg(pcitag_t);
182 static pcitag_t rmixl_pcie_make_tag(void *, int, int, int);
183 static void rmixl_pcie_decompose_tag(void *, pcitag_t, int *, int *, int *);
184 void rmixl_pcie_tag_print(const char *restrict, void *, pcitag_t, int, vaddr_t, u_long);
185 static int rmixl_pcie_conf_setup(struct rmixl_pcie_softc *,
186 pcitag_t, int *, bus_space_tag_t *,
187 bus_space_handle_t *);
188 static pcireg_t rmixl_pcie_conf_read(void *, pcitag_t, int);
189 static void rmixl_pcie_conf_write(void *, pcitag_t, int, pcireg_t);
190 #ifdef __PCI_DEV_FUNCORDER
191 static bool rmixl_pcie_dev_funcorder(void *, int, int, int, char *);
192 #endif
193
194 static int rmixl_pcie_intr_map(struct pci_attach_args *,
195 pci_intr_handle_t *);
196 static const char *
197 rmixl_pcie_intr_string(void *, pci_intr_handle_t);
198 static const struct evcnt *
199 rmixl_pcie_intr_evcnt(void *, pci_intr_handle_t);
200 static pci_intr_handle_t
201 rmixl_pcie_make_pih(u_int, u_int, u_int);
202 static void rmixl_pcie_decompose_pih(pci_intr_handle_t, u_int *, u_int *, u_int *);
203 static void rmixl_pcie_intr_disestablish(void *, void *);
204 static void *rmixl_pcie_intr_establish(void *, pci_intr_handle_t,
205 int, int (*)(void *), void *);
206 static rmixl_pcie_link_intr_t *
207 rmixl_pcie_lip_add_1(rmixl_pcie_softc_t *, u_int, int, int);
208 static void rmixl_pcie_lip_free_callout(rmixl_pcie_link_intr_t *);
209 static void rmixl_pcie_lip_free(void *);
210 static int rmixl_pcie_intr(void *);
211 static void rmixl_pcie_link_error_intr(u_int, uint32_t, uint32_t);
212 #if defined(DEBUG) || defined(DDB)
213 int rmixl_pcie_error_check(void);
214 #endif
215 static int _rmixl_pcie_error_check(void *);
216 static int rmixl_pcie_error_intr(void *);
217
218 static void rmixl_physaddr_add(struct extent *, const char *,
219 struct rmixl_region *, bus_addr_t, bus_size_t);
220
221 #define RMIXL_PCIE_BAR_INIT(rp, reg, bar, size, align) { \
222 struct extent *ext = rmixl_configuration.rc_phys_ex; \
223 u_long region_start; \
224 int err; \
225 \
226 err = extent_alloc(ext, (size), (align), 0UL, EX_NOWAIT, \
227 ®ion_start); \
228 if (err != 0) \
229 panic("%s: extent_alloc(%p, %#lx, %#lx, %#lx, %#x, %p)",\
230 __func__, ext, size, align, 0UL, EX_NOWAIT, \
231 ®ion_start); \
232 const uint64_t pbase = (uint64_t)region_start << 20; \
233 bar = RMIXL_PCIE_##reg##_BAR(pbase, 1); \
234 DPRINTF(("%s: PCIE_%s_BAR was not enabled by firmware\n" \
235 "%s enabling PCIE_%s_BAR at phys %#" PRIxBUSADDR \
236 ", size %luMB\n", \
237 __func__, __STRING(reg), \
238 __func__, __STRING(reg), pbase, size)); \
239 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_BRIDGE \
240 + RMIXLS_SBC_PCIE_##reg##_BAR, bar); \
241 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE \
242 + RMIXLS_SBC_PCIE_##reg##_BAR); \
243 DPRINTF(("%s: %s BAR %#x\n", __func__, __STRING(reg), bar)); \
244 (rp)->r_pbase = pbase; \
245 (rp)->r_size = (size) << 20; \
246 }
247
248
249 #if defined(DEBUG) || defined(DDB)
250 static void *rmixl_pcie_v;
251 #endif
252
253 CFATTACH_DECL_NEW(rmixl_pcie, sizeof(struct rmixl_pcie_softc),
254 rmixl_pcie_match, rmixl_pcie_attach, NULL, NULL);
255
256 static bool rmixl_pcie_found;
257
258 static int
259 rmixl_pcie_match(device_t parent, cfdata_t cf, void *aux)
260 {
261 uint32_t r;
262
263 /*
264 * A PCIe interface exists only on XLS chips.
265 */
266 if (! cpu_rmixls(mips_options.mips_cpu))
267 return 0;
268
269 /*
270 * There is only one PCIe Interface on chip
271 */
272 if (rmixl_pcie_found)
273 return 0;
274
275 /* read GPIO Reset Configuration register */
276 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET_CFG);
277 r >>= 26;
278 r &= 3;
279 if (r != 0)
280 return 0; /* strapped for SRIO */
281
282 return 1;
283 }
284
285 static void
286 rmixl_pcie_attach(device_t parent, device_t self, void *aux)
287 {
288 struct rmixl_pcie_softc *sc = device_private(self);
289 struct obio_attach_args *obio = aux;
290 struct rmixl_config *rcp = &rmixl_configuration;
291 struct pcibus_attach_args pba;
292 uint32_t bar;
293
294 rmixl_pcie_found = true;
295 sc->sc_dev = self;
296
297 aprint_normal(": RMI XLS PCIe Interface\n");
298
299 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_HIGH);
300
301 rmixl_pcie_lnkcfg(sc);
302
303 rmixl_pcie_intcfg(sc);
304
305 rmixl_pcie_errata(sc);
306
307 sc->sc_dmat29 = obio->obio_dmat29;
308 sc->sc_dmat32 = obio->obio_dmat32;
309 sc->sc_dmat64 = obio->obio_dmat64;
310 sc->sc_pc = &rcp->rc_pci_chipset;
311
312 /*
313 * get PCI config space base addr from SBC PCIe CFG BAR
314 * initialize it if necessary
315 */
316 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLS_SBC_PCIE_CFG_BAR);
317 DPRINTF(("%s: PCIE_CFG_BAR %#x\n", __func__, bar));
318 if ((bar & RMIXL_PCIE_CFG_BAR_ENB) == 0) {
319 u_long n = RMIXL_PCIE_CFG_SIZE / (1024 * 1024);
320 RMIXL_PCIE_BAR_INIT(&rcp->rc_pci_cfg, CFG, bar, n, n);
321 }
322
323 /*
324 * get PCIE Extended config space base addr from SBC PCIe ECFG BAR
325 * initialize it if necessary
326 */
327 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLS_SBC_PCIE_ECFG_BAR);
328 DPRINTF(("%s: PCIE_ECFG_BAR %#x\n", __func__, bar));
329 if ((bar & RMIXL_PCIE_ECFG_BAR_ENB) == 0) {
330 u_long n = RMIXL_PCIE_ECFG_SIZE / (1024 * 1024);
331 RMIXL_PCIE_BAR_INIT(&rcp->rc_pci_ecfg, ECFG, bar, n, n);
332 }
333
334 /*
335 * get PCI MEM space base [addr, size] from SBC PCIe MEM BAR
336 * initialize it if necessary
337 */
338 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLS_SBC_PCIE_MEM_BAR);
339 DPRINTF(("%s: PCIE_MEM_BAR %#x\n", __func__, bar));
340 if ((bar & RMIXL_PCIE_MEM_BAR_ENB) == 0) {
341 u_long n = 256; /* 256 MB */
342 RMIXL_PCIE_BAR_INIT(&rcp->rc_pci_mem, MEM, bar, n, n);
343 }
344
345 /*
346 * get PCI IO space base [addr, size] from SBC PCIe IO BAR
347 * initialize it if necessary
348 */
349 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLS_SBC_PCIE_IO_BAR);
350 DPRINTF(("%s: PCIE_IO_BAR %#x\n", __func__, bar));
351 if ((bar & RMIXL_PCIE_IO_BAR_ENB) == 0) {
352 u_long n = 32; /* 32 MB */
353 RMIXL_PCIE_BAR_INIT(&rcp->rc_pci_io, IO, bar, n, n);
354 }
355
356 /*
357 * initialize the PCI CFG, ECFG bus space tags
358 */
359 sc->sc_pci_cfg_memt = &rcp->rc_pci_cfg_memt;
360 rmixl_pci_cfg_el_bus_mem_init(sc->sc_pci_cfg_memt, rcp);
361
362 /*
363 * This should be in KSEG1 and has no extent tracking.
364 * (bit 24 controls little (0) or big (1) endian access).
365 */
366 if (bus_space_map(sc->sc_pci_cfg_memt, 0, rcp->rc_pci_cfg.r_size / 2,
367 0, &rcp->rc_pci_cfg_memh))
368 panic("%s: failed to map pci CFG registers "
369 "(base=%#"PRIxBUSADDR" size=%#"PRIxBUSSIZE")",
370 __func__, rcp->rc_pci_cfg.r_pbase,
371 rcp->rc_pci_cfg.r_size / 2);
372
373 sc->sc_pci_cfg_memh = rcp->rc_pci_cfg_memh;
374
375 sc->sc_pci_ecfg_memt = &rcp->rc_pci_ecfg_el_memt;
376 rmixl_pci_ecfg_el_bus_mem_init(sc->sc_pci_ecfg_memt, rcp);
377
378 /*
379 * This is too big to in KSEG1 but is accessible via XKPHYS
380 * and has no extent tracking.
381 * (bit 28 controls little (0) or big (1) endian access).
382 */
383 #ifdef _LP64
384 if (bus_space_map(sc->sc_pci_ecfg_memt, 0, rcp->rc_pci_ecfg.r_size / 2,
385 0, &rcp->rc_pci_ecfg_el_memh))
386 panic("%s: failed to map pci ECFG LE registers "
387 "(base=%#"PRIxBUSADDR" size=%#"PRIxBUSSIZE")",
388 __func__, rcp->rc_pci_ecfg.r_pbase,
389 rcp->rc_pci_ecfg.r_size / 2);
390
391 sc->sc_pci_ecfg_memh = rcp->rc_pci_ecfg_el_memh;
392 #else
393 printf("%s: skipping mapping of pci ECFG LE registers "
394 "(base=%#"PRIxBUSADDR" size=%#"PRIxBUSSIZE")\n",
395 __func__, rcp->rc_pci_ecfg.r_pbase,
396 rcp->rc_pci_ecfg.r_size / 2);
397 #endif
398
399 /*
400 * initialize the PCI MEM and IO bus space tags
401 */
402 rmixl_pci_el_bus_mem_init(&rcp->rc_pci_memt, rcp);
403 rmixl_pci_bus_io_init(&rcp->rc_pci_iot, rcp);
404
405 /*
406 * initialize the extended configuration regs
407 */
408 rmixl_pcie_init_ecfg(sc);
409
410 /*
411 * initialize the PCI chipset tag
412 */
413 rmixl_pcie_init(sc);
414
415 /*
416 * attach the PCI bus
417 */
418 memset(&pba, 0, sizeof(pba));
419 pba.pba_memt = &rcp->rc_pci_memt;
420 pba.pba_iot = &rcp->rc_pci_iot;
421 pba.pba_dmat = sc->sc_dmat32;
422 pba.pba_dmat64 = sc->sc_dmat64;
423 pba.pba_pc = sc->sc_pc;
424 pba.pba_bus = 0;
425 pba.pba_bridgetag = NULL;
426 pba.pba_intrswiz = 0;
427 pba.pba_intrtag = 0;
428 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
429 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
430
431 (void) config_found_ia(self, "pcibus", &pba, pcibusprint);
432 }
433
434 /*
435 * rmixl_pcie_lnkcfg_xls4xx - link configs for XLS4xx and XLS6xx
436 * use IO_AD[11] and IO_AD[10], observable in
437 * Bits[21:20] of the GPIO Reset Configuration register
438 */
439 static void
440 rmixl_pcie_lnkcfg_xls4xx(rmixl_pcie_lnktab_t *ltp, uint32_t grcr)
441 {
442 u_int index;
443 static const rmixl_pcie_lnkcfg_t lnktab_xls4xx[4][4] = {
444 {{ LCFG_EP, 4}, {LCFG_NO, 0}, {LCFG_NO, 0}, {LCFG_NO, 0}},
445 {{ LCFG_RC, 4}, {LCFG_NO, 0}, {LCFG_NO, 0}, {LCFG_NO, 0}},
446 {{ LCFG_EP, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}},
447 {{ LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}},
448 };
449 static const char * const lnkstr_xls4xx[4] = {
450 "1EPx4",
451 "1RCx4",
452 "1EPx1, 3RCx1",
453 "4RCx1"
454 };
455 index = (grcr >> 20) & 3;
456 ltp->ncfgs = 4;
457 ltp->cfg = lnktab_xls4xx[index];
458 ltp->str = lnkstr_xls4xx[index];
459 }
460
461 /*
462 * rmixl_pcie_lnkcfg_xls408Lite - link configs for XLS408Lite and XLS04A
463 * use IO_AD[11] and IO_AD[10], observable in
464 * Bits[21:20] of the GPIO Reset Configuration register
465 */
466 static void
467 rmixl_pcie_lnkcfg_xls408Lite(rmixl_pcie_lnktab_t *ltp, uint32_t grcr)
468 {
469 u_int index;
470 static const rmixl_pcie_lnkcfg_t lnktab_xls408Lite[4][2] = {
471 {{ LCFG_EP, 4}, {LCFG_NO, 0}},
472 {{ LCFG_RC, 4}, {LCFG_NO, 0}},
473 {{ LCFG_EP, 1}, {LCFG_RC, 1}},
474 {{ LCFG_RC, 1}, {LCFG_RC, 1}},
475 };
476 static const char * const lnkstr_xls408Lite[4] = {
477 "4EPx4",
478 "1RCx4",
479 "1EPx1, 1RCx1",
480 "2RCx1"
481 };
482
483 index = (grcr >> 20) & 3;
484 ltp->ncfgs = 2;
485 ltp->cfg = lnktab_xls408Lite[index];
486 ltp->str = lnkstr_xls408Lite[index];
487 }
488
489 /*
490 * rmixl_pcie_lnkcfg_xls2xx - link configs for XLS2xx
491 * use IO_AD[10], observable in Bit[20] of the
492 * GPIO Reset Configuration register
493 */
494 static void
495 rmixl_pcie_lnkcfg_xls2xx(rmixl_pcie_lnktab_t *ltp, uint32_t grcr)
496 {
497 u_int index;
498 static const rmixl_pcie_lnkcfg_t lnktab_xls2xx[2][4] = {
499 {{ LCFG_EP, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}},
500 {{ LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}}
501 };
502 static const char * const lnkstr_xls2xx[2] = {
503 "1EPx1, 3RCx1",
504 "4RCx1",
505 };
506
507 index = (grcr >> 20) & 1;
508 ltp->ncfgs = 4;
509 ltp->cfg = lnktab_xls2xx[index];
510 ltp->str = lnkstr_xls2xx[index];
511 }
512
513 /*
514 * rmixl_pcie_lnkcfg_xls1xx - link configs for XLS1xx
515 * use IO_AD[10], observable in Bit[20] of the
516 * GPIO Reset Configuration register
517 */
518 static void
519 rmixl_pcie_lnkcfg_xls1xx(rmixl_pcie_lnktab_t *ltp, uint32_t grcr)
520 {
521 u_int index;
522 static const rmixl_pcie_lnkcfg_t lnktab_xls1xx[2][2] = {
523 {{ LCFG_EP, 1}, {LCFG_RC, 1}},
524 {{ LCFG_RC, 1}, {LCFG_RC, 1}}
525 };
526 static const char * const lnkstr_xls1xx[2] = {
527 "1EPx1, 1RCx1",
528 "2RCx1",
529 };
530
531 index = (grcr >> 20) & 1;
532 ltp->ncfgs = 2;
533 ltp->cfg = lnktab_xls1xx[index];
534 ltp->str = lnkstr_xls1xx[index];
535 }
536
537 /*
538 * rmixl_pcie_lnkcfg - determine PCI Express Link Configuration
539 */
540 static void
541 rmixl_pcie_lnkcfg(struct rmixl_pcie_softc *sc)
542 {
543 uint32_t r;
544
545 /* read GPIO Reset Configuration register */
546 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET_CFG);
547 DPRINTF(("%s: GPIO RCR %#x\n", __func__, r));
548
549 switch (MIPS_PRID_IMPL(mips_options.mips_cpu_id)) {
550 case MIPS_XLS104:
551 case MIPS_XLS108:
552 rmixl_pcie_lnkcfg_xls1xx(&sc->sc_pcie_lnktab, r);
553 break;
554 case MIPS_XLS204:
555 case MIPS_XLS208:
556 rmixl_pcie_lnkcfg_xls2xx(&sc->sc_pcie_lnktab, r);
557 break;
558 case MIPS_XLS404LITE:
559 case MIPS_XLS408LITE:
560 rmixl_pcie_lnkcfg_xls408Lite(&sc->sc_pcie_lnktab, r);
561 break;
562 case MIPS_XLS404:
563 case MIPS_XLS408:
564 case MIPS_XLS416:
565 case MIPS_XLS608:
566 case MIPS_XLS616:
567 /* 6xx uses same table as 4xx */
568 rmixl_pcie_lnkcfg_xls4xx(&sc->sc_pcie_lnktab, r);
569 break;
570 default:
571 panic("%s: unknown RMI PRID IMPL", __func__);
572 }
573
574 aprint_normal_dev(sc->sc_dev, "link config %s\n",
575 sc->sc_pcie_lnktab.str);
576 }
577
578 /*
579 * rmixl_pcie_intcfg - init PCIe Link interrupt enables
580 */
581 static void
582 rmixl_pcie_intcfg(struct rmixl_pcie_softc *sc)
583 {
584 int link;
585 size_t size;
586 rmixl_pcie_evcnt_t *ev;
587
588 DPRINTF(("%s: disable all link interrupts\n", __func__));
589 for (link=0; link < sc->sc_pcie_lnktab.ncfgs; link++) {
590 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_PCIE_LE + int_enb_offset[link].r0,
591 RMIXL_PCIE_LINK_STATUS0_ERRORS);
592 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_PCIE_LE + int_enb_offset[link].r1,
593 RMIXL_PCIE_LINK_STATUS1_ERRORS);
594 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_PCIE_LE + msi_enb_offset[link], 0);
595 sc->sc_link_intr[link] = NULL;
596
597 /*
598 * allocate per-cpu, per-pin interrupt event counters
599 */
600 size = ncpu * PCI_INTERRUPT_PIN_MAX * sizeof(rmixl_pcie_evcnt_t);
601 ev = malloc(size, M_DEVBUF, M_NOWAIT);
602 if (ev == NULL)
603 panic("%s: cannot malloc evcnts\n", __func__);
604 sc->sc_evcnts[link] = ev;
605 for (int pin=PCI_INTERRUPT_PIN_A; pin <= PCI_INTERRUPT_PIN_MAX; pin++) {
606 for (int cpu=0; cpu < ncpu; cpu++) {
607 ev = RMIXL_PCIE_EVCNT(sc, link, pin - 1, cpu);
608 snprintf(ev->name, sizeof(ev->name),
609 "cpu%d, link %d, pin %d", cpu, link, pin);
610 evcnt_attach_dynamic(&ev->evcnt, EVCNT_TYPE_INTR,
611 NULL, "rmixl_pcie", ev->name);
612 }
613 }
614 }
615 }
616
617 static void
618 rmixl_pcie_errata(struct rmixl_pcie_softc *sc)
619 {
620 const mips_prid_t cpu_id = mips_options.mips_cpu_id;
621 u_int rev;
622 u_int lanes;
623 bool e391 = false;
624
625 /*
626 * 3.9.1 PCIe Link-0 Registers Reset to Incorrect Values
627 * check if it allies to this CPU implementation and revision
628 */
629 rev = MIPS_PRID_REV(cpu_id);
630 switch (MIPS_PRID_IMPL(cpu_id)) {
631 case MIPS_XLS104:
632 case MIPS_XLS108:
633 break;
634 case MIPS_XLS204:
635 case MIPS_XLS208:
636 /* stepping A0 is affected */
637 if (rev == 0)
638 e391 = true;
639 break;
640 case MIPS_XLS404LITE:
641 case MIPS_XLS408LITE:
642 break;
643 case MIPS_XLS404:
644 case MIPS_XLS408:
645 case MIPS_XLS416:
646 /* steppings A0 and A1 are affected */
647 if ((rev == 0) || (rev == 1))
648 e391 = true;
649 break;
650 case MIPS_XLS608:
651 case MIPS_XLS616:
652 break;
653 default:
654 panic("unknown RMI PRID IMPL");
655 }
656
657 /*
658 * for XLS we only need to check entry #0
659 * this may need to change for later XL family chips
660 */
661 lanes = sc->sc_pcie_lnktab.cfg[0].lanes;
662
663 if ((e391 != false) && ((lanes == 2) || (lanes == 4))) {
664 /*
665 * attempt work around for errata 3.9.1
666 * "PCIe Link-0 Registers Reset to Incorrect Values"
667 * the registers are write-once: if the firmware already wrote,
668 * then our writes are ignored; hope they did it right.
669 */
670 uint32_t queuectrl;
671 uint32_t bufdepth;
672 #ifdef DIAGNOSTIC
673 uint32_t r;
674 #endif
675
676 aprint_normal("%s: attempt work around for errata 3.9.1",
677 device_xname(sc->sc_dev));
678 if (lanes == 4) {
679 queuectrl = 0x00018074;
680 bufdepth = 0x001901D1;
681 } else {
682 queuectrl = 0x00018036;
683 bufdepth = 0x001900D9;
684 }
685
686 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_PCIE_BE +
687 RMIXL_VC0_POSTED_RX_QUEUE_CTRL, queuectrl);
688 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_PCIE_BE +
689 RMIXL_VC0_POSTED_BUFFER_DEPTH, bufdepth);
690
691 #ifdef DIAGNOSTIC
692 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_PCIE_BE +
693 RMIXL_VC0_POSTED_RX_QUEUE_CTRL);
694 printf("\nVC0_POSTED_RX_QUEUE_CTRL %#x\n", r);
695
696 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_PCIE_BE +
697 RMIXL_VC0_POSTED_BUFFER_DEPTH);
698 printf("VC0_POSTED_BUFFER_DEPTH %#x\n", r);
699 #endif
700 }
701 }
702
703 static void
704 rmixl_pcie_init(struct rmixl_pcie_softc *sc)
705 {
706 pci_chipset_tag_t pc = sc->sc_pc;
707 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
708 struct extent *ioext, *memext;
709 #endif
710
711 pc->pc_conf_v = (void *)sc;
712 pc->pc_attach_hook = rmixl_pcie_attach_hook;
713 pc->pc_bus_maxdevs = rmixl_pcie_bus_maxdevs;
714 pc->pc_make_tag = rmixl_pcie_make_tag;
715 pc->pc_decompose_tag = rmixl_pcie_decompose_tag;
716 pc->pc_conf_read = rmixl_pcie_conf_read;
717 pc->pc_conf_write = rmixl_pcie_conf_write;
718 #ifdef __PCI_DEV_FUNCORDER
719 pc->pc_dev_funcorder = rmixl_pcie_dev_funcorder;
720 #endif
721
722 pc->pc_intr_v = (void *)sc;
723 pc->pc_intr_map = rmixl_pcie_intr_map;
724 pc->pc_intr_string = rmixl_pcie_intr_string;
725 pc->pc_intr_evcnt = rmixl_pcie_intr_evcnt;
726 pc->pc_intr_establish = rmixl_pcie_intr_establish;
727 pc->pc_intr_disestablish = rmixl_pcie_intr_disestablish;
728 pc->pc_conf_interrupt = rmixl_conf_interrupt;
729
730 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
731 /*
732 * Configure the PCI bus.
733 */
734 struct rmixl_config *rcp = &rmixl_configuration;
735
736 aprint_normal_dev(sc->sc_dev, "configuring PCI bus\n");
737
738 ioext = extent_create("pciio",
739 rcp->rc_pci_io.r_pbase,
740 rcp->rc_pci_io.r_pbase + rcp->rc_pci_io.r_size - 1,
741 M_DEVBUF, NULL, 0, EX_NOWAIT);
742
743 memext = extent_create("pcimem",
744 rcp->rc_pci_mem.r_pbase,
745 rcp->rc_pci_mem.r_pbase + rcp->rc_pci_mem.r_size - 1,
746 M_DEVBUF, NULL, 0, EX_NOWAIT);
747
748 pci_configure_bus(pc, ioext, memext, NULL, 0,
749 mips_cache_info.mci_dcache_align);
750
751 extent_destroy(ioext);
752 extent_destroy(memext);
753 #endif
754 }
755
756 static void
757 rmixl_pcie_init_ecfg(struct rmixl_pcie_softc *sc)
758 {
759 void *v;
760 pcitag_t tag;
761 pcireg_t r;
762
763 v = sc;
764 tag = rmixl_pcie_make_tag(v, 0, 0, 0);
765
766 #ifdef PCI_DEBUG
767 int i, offset;
768 static const int offtab[] =
769 { 0, 4, 8, 0xc, 0x10, 0x14, 0x18, 0x1c,
770 0x2c, 0x30, 0x34 };
771 for (i=0; i < sizeof(offtab)/sizeof(offtab[0]); i++) {
772 offset = 0x100 + offtab[i];
773 r = rmixl_pcie_conf_read(v, tag, offset);
774 printf("%s: %#x: %#x\n", __func__, offset, r);
775 }
776 #endif
777 r = rmixl_pcie_conf_read(v, tag, 0x100);
778 if (r == -1)
779 return; /* cannot access */
780
781 /* check pre-existing uncorrectable errs */
782 r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_UESR);
783 r &= ~PCIE_ECFG_UExR_RESV;
784 if (r != 0)
785 panic("%s: Uncorrectable Error Status: %#x\n",
786 __func__, r);
787
788 /* unmask all uncorrectable errs */
789 r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_UEMR);
790 r &= ~PCIE_ECFG_UExR_RESV;
791 rmixl_pcie_conf_write(v, tag, RMIXL_PCIE_ECFG_UEMR, r);
792
793 /* ensure default uncorrectable err severity confniguration */
794 r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_UEVR);
795 r &= ~PCIE_ECFG_UExR_RESV;
796 r |= PCIE_ECFG_UEVR_DFLT;
797 rmixl_pcie_conf_write(v, tag, RMIXL_PCIE_ECFG_UEVR, r);
798
799 /* check pre-existing correctable errs */
800 r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_CESR);
801 r &= ~PCIE_ECFG_CExR_RESV;
802 #ifdef DIAGNOSTIC
803 if (r != 0)
804 aprint_normal("%s: Correctable Error Status: %#x\n",
805 device_xname(sc->sc_dev), r);
806 #endif
807
808 /* unmask all correctable errs */
809 r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_CEMR);
810 r &= ~PCIE_ECFG_CExR_RESV;
811 rmixl_pcie_conf_write(v, tag, RMIXL_PCIE_ECFG_UEMR, r);
812
813 /* check pre-existing Root Error Status */
814 r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_RESR);
815 r &= ~PCIE_ECFG_RESR_RESV;
816 if (r != 0)
817 panic("%s: Root Error Status: %#x\n", __func__, r);
818 /* XXX TMP FIXME */
819
820 /* enable all Root errs */
821 r = (pcireg_t)(~PCIE_ECFG_RECR_RESV);
822 rmixl_pcie_conf_write(v, tag, RMIXL_PCIE_ECFG_RECR, r);
823
824 /*
825 * establish ISR for PCIE Fatal Error interrupt
826 * - for XLS4xxLite, XLS2xx, XLS1xx only
827 */
828 switch (MIPS_PRID_IMPL(mips_options.mips_cpu_id)) {
829 case MIPS_XLS104:
830 case MIPS_XLS108:
831 case MIPS_XLS204:
832 case MIPS_XLS208:
833 case MIPS_XLS404LITE:
834 case MIPS_XLS408LITE:
835 sc->sc_fatal_ih = rmixl_intr_establish(29, IPL_HIGH,
836 IST_LEVEL_HIGH, rmixl_pcie_error_intr, v, false);
837 break;
838 default:
839 break;
840 }
841
842 #if defined(DEBUG) || defined(DDB)
843 rmixl_pcie_v = v;
844 #endif
845 }
846
847 void
848 rmixl_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *iline)
849 {
850 DPRINTF(("%s: %p, %d, %d, %d, %d, %p\n",
851 __func__, v, bus, dev, ipin, swiz, iline));
852 }
853
854 void
855 rmixl_pcie_attach_hook(struct device *parent, struct device *self,
856 struct pcibus_attach_args *pba)
857 {
858 DPRINTF(("%s: pba_bus %d, pba_bridgetag %p, pc_conf_v %p\n",
859 __func__, pba->pba_bus, pba->pba_bridgetag,
860 pba->pba_pc->pc_conf_v));
861 }
862
863 int
864 rmixl_pcie_bus_maxdevs(void *v, int busno)
865 {
866 return (32); /* XXX depends on the family of XLS SoC */
867 }
868
869 /*
870 * rmixl_tag_ecfg_to_cfg - convert ecfg address to cfg (generic tag) address
871 *
872 * ecfg cfg
873 * 39:29 39:25 (reserved)
874 * 28 24 Swap (0=little, 1=big endian)
875 * 27:20 23:16 Bus number
876 * 19:15 15:11 Device number
877 * 14:12 10:8 Function number
878 * 11:0 7:0 Register number
879 */
880 static inline pcitag_t
881 rmixl_tag_ecfg_to_cfg(pcitag_t tag)
882 {
883 KASSERT(_RMIXL_PCITAG_OFFSET(tag) == 0);
884 return tag >> 4;
885 }
886
887 /*
888 * XLS pci (extended) tag is a 40 bit address composed thusly:
889 * 39:29 (reserved)
890 * 28 Swap (0=little, 1=big endian)
891 * 27:20 Bus number
892 * 19:15 Device number
893 * 14:12 Function number
894 * 11:0 Register number
895 *
896 * Note: this is the "native" composition for addressing CFG space, but not for ECFG space.
897 */
898 pcitag_t
899 rmixl_pcie_make_tag(void *v, int bus, int dev, int fun)
900 {
901 return _RMIXL_PCITAG(bus, dev, fun);
902 }
903
904 void
905 rmixl_pcie_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
906 {
907 if (bp != NULL)
908 *bp = _RMIXL_PCITAG_BUS(tag);
909 if (dp != NULL)
910 *dp = _RMIXL_PCITAG_DEV(tag);
911 if (fp != NULL)
912 *fp = _RMIXL_PCITAG_FUNC(tag);
913 }
914
915 void
916 rmixl_pcie_tag_print(const char *restrict s, void *v, pcitag_t tag, int offset,
917 vaddr_t va, u_long r)
918 {
919 int bus, dev, fun;
920
921 rmixl_pcie_decompose_tag(v, tag, &bus, &dev, &fun);
922 printf("%s: %lx: %d/%d/%d/%d - %#" PRIxVADDR ":%#lx\n",
923 s, tag, bus, dev, fun, offset, va, r);
924 }
925
926 static int
927 rmixl_pcie_conf_setup(struct rmixl_pcie_softc *sc,
928 pcitag_t tag, int *offp, bus_space_tag_t *bstp,
929 bus_space_handle_t *bshp)
930 {
931 bus_space_tag_t bst;
932 bus_space_handle_t bsh;
933
934 #ifdef _LP64
935 bst = sc->sc_pci_ecfg_memt;
936 bus_space_subregion(bst, sc->sc_pci_ecfg_memh, tag, 0x1000, &bsh);
937 #else
938 /*
939 * bus space depends on offset
940 */
941 if ((*offp >= 0) && (*offp < 0x100)) {
942 bst = sc->sc_pci_cfg_memt;
943 tag = rmixl_tag_ecfg_to_cfg(tag); /* convert to CFG format */
944 bus_space_subregion(bst, sc->sc_pci_cfg_memh, tag, 0x100, &bsh);
945 } else if ((*offp >= 0x100) && (*offp <= 0x700)) {
946 static bus_space_handle_t ecfg_bsh;
947 static bus_addr_t ecfg_oba = -1;
948 const pcitag_t mask = __BITS(14,0); /* all fnctns of a device */
949 bst = sc->sc_pci_ecfg_memt;
950 const bus_addr_t ba = (tag & ~mask);
951 *offp += (tag & mask);
952 if (ba != ecfg_oba) {
953 bus_size_t size = (bus_size_t)(mask + 1);
954 if (ecfg_oba != -1)
955 bus_space_unmap(bst, ecfg_bsh, size);
956 int err = bus_space_map(bst, ba, size, 0, &ecfg_bsh);
957 if (err != 0) {
958 ecfg_oba = -1;
959 #ifdef DEBUG
960 panic("%s: ECFG: "
961 "bus_space_map(%p, %#"PRIxBUSADDR
962 ", %#"PRIxBUSSIZE", 0, %p): %d",
963 __func__, bst, ba, size, &ecfg_bsh, err);
964 #endif
965 return -1;
966 }
967 ecfg_oba = ba;
968 }
969 bsh = ecfg_bsh;
970 } else {
971 #ifdef DEBUG
972 panic("%s: offset %#x: unknown", __func__, *offp);
973 #endif
974 return -1;
975 }
976 #endif /* _LP64 */
977
978 *bstp = bst;
979 *bshp = bsh;
980
981 return 0;
982 }
983
984 pcireg_t
985 rmixl_pcie_conf_read(void *v, pcitag_t tag, int offset)
986 {
987 struct rmixl_pcie_softc *sc = v;
988 static bus_space_handle_t bsh;
989 bus_space_tag_t bst;
990 pcireg_t rv;
991 uint64_t cfg0;
992
993 mutex_enter(&sc->sc_mutex);
994
995 if (rmixl_pcie_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
996 cfg0 = rmixl_cache_err_dis();
997 rv = bus_space_read_4(bst, bsh, (bus_size_t)offset);
998 if (rmixl_cache_err_check() != 0) {
999 #ifdef DIAGNOSTIC
1000 int bus, dev, fun;
1001
1002 rmixl_pcie_decompose_tag(v, tag, &bus, &dev, &fun);
1003 printf("%s: %d/%d/%d, offset %#x: bad address\n",
1004 __func__, bus, dev, fun, offset);
1005 #endif
1006 rv = (pcireg_t) -1;
1007 }
1008 rmixl_cache_err_restore(cfg0);
1009 } else {
1010 rv = -1;
1011 }
1012
1013 mutex_exit(&sc->sc_mutex);
1014
1015 return rv;
1016 }
1017
1018 void
1019 rmixl_pcie_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
1020 {
1021 struct rmixl_pcie_softc *sc = v;
1022 static bus_space_handle_t bsh;
1023 bus_space_tag_t bst;
1024 uint64_t cfg0;
1025
1026 mutex_enter(&sc->sc_mutex);
1027
1028 if (rmixl_pcie_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
1029 cfg0 = rmixl_cache_err_dis();
1030 bus_space_write_4(bst, bsh, (bus_size_t)offset, val);
1031 if (rmixl_cache_err_check() != 0) {
1032 #ifdef DIAGNOSTIC
1033 int bus, dev, fun;
1034
1035 rmixl_pcie_decompose_tag(v, tag, &bus, &dev, &fun);
1036 printf("%s: %d/%d/%d, offset %#x: bad address\n",
1037 __func__, bus, dev, fun, offset);
1038 #endif
1039 }
1040 rmixl_cache_err_restore(cfg0);
1041 }
1042
1043 mutex_exit(&sc->sc_mutex);
1044 }
1045
1046 #ifdef __PCI_DEV_FUNCORDER
1047 bool
1048 rmixl_pcie_dev_funcorder(void *v, int bus, int device, int nfunctions,
1049 char *funcs)
1050 {
1051 return false;
1052 }
1053 #endif
1054
1055 int
1056 rmixl_pcie_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *pih)
1057 {
1058 int device;
1059 u_int link;
1060 u_int irq;
1061
1062 /*
1063 * The bus is unimportant since it can change depending on the
1064 * configuration. We are tied to device # of PCIe bridge we are
1065 * ultimately attached to.
1066 */
1067 pci_decompose_tag(pa->pa_pc, pa->pa_intrtag,
1068 NULL, &device, NULL);
1069
1070 #ifdef DEBUG
1071 DPRINTF(("%s: ps_bus %d, pa_intrswiz %#x, pa_intrtag %#lx,"
1072 " pa_intrpin %d, pa_intrline %d, pa_rawintrpin %d\n",
1073 __func__, pa->pa_bus, pa->pa_intrswiz, pa->pa_intrtag,
1074 pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin));
1075 #endif
1076
1077 /*
1078 * PCIe Link INT irq assignment is cpu implementation specific
1079 */
1080 switch (MIPS_PRID_IMPL(mips_options.mips_cpu_id)) {
1081 case MIPS_XLS104:
1082 case MIPS_XLS108:
1083 case MIPS_XLS404LITE:
1084 case MIPS_XLS408LITE:
1085 if (device > 1)
1086 panic("%s: bad bus %d", __func__, device);
1087 link = device;
1088 irq = device + 26;
1089 break;
1090 case MIPS_XLS204:
1091 case MIPS_XLS208: {
1092 if (device > 3)
1093 panic("%s: bad bus %d", __func__, device);
1094 link = device;
1095 irq = device + (device & 2 ? 21 : 26);
1096 break;
1097 }
1098 case MIPS_XLS404:
1099 case MIPS_XLS408:
1100 case MIPS_XLS416:
1101 case MIPS_XLS608:
1102 case MIPS_XLS616:
1103 if (device > 3)
1104 panic("%s: bad bus %d", __func__, device);
1105 link = device;
1106 irq = device + 26;
1107 break;
1108 default:
1109 panic("%s: cpu IMPL %#x not supported\n",
1110 __func__, MIPS_PRID_IMPL(mips_options.mips_cpu_id));
1111 }
1112
1113 if (pa->pa_intrpin != PCI_INTERRUPT_PIN_NONE)
1114 *pih = rmixl_pcie_make_pih(link, pa->pa_intrpin - 1, irq);
1115 else
1116 *pih = ~0;
1117
1118 return 0;
1119 }
1120
1121 const char *
1122 rmixl_pcie_intr_string(void *v, pci_intr_handle_t pih)
1123 {
1124 const char *name = "(illegal)";
1125 u_int link, bitno, irq;
1126
1127 rmixl_pcie_decompose_pih(pih, &link, &bitno, &irq);
1128
1129 switch (MIPS_PRID_IMPL(mips_options.mips_cpu_id)) {
1130 case MIPS_XLS104:
1131 case MIPS_XLS108:
1132 case MIPS_XLS404LITE:
1133 case MIPS_XLS408LITE:
1134 switch (irq) {
1135 case 26:
1136 case 27:
1137 name = rmixl_irt_string(irq);
1138 break;
1139 }
1140 break;
1141 case MIPS_XLS204:
1142 case MIPS_XLS208:
1143 switch (irq) {
1144 case 23:
1145 case 24:
1146 case 26:
1147 case 27:
1148 name = rmixl_irt_string(irq);
1149 break;
1150 }
1151 break;
1152 case MIPS_XLS404:
1153 case MIPS_XLS408:
1154 case MIPS_XLS416:
1155 case MIPS_XLS608:
1156 case MIPS_XLS616:
1157 switch (irq) {
1158 case 26:
1159 case 27:
1160 case 28:
1161 case 29:
1162 name = rmixl_irt_string(irq);
1163 break;
1164 }
1165 break;
1166 default:
1167 panic("%s: cpu IMPL %#x not supported\n",
1168 __func__, MIPS_PRID_IMPL(mips_options.mips_cpu_id));
1169 }
1170
1171 return name;
1172 }
1173
1174 const struct evcnt *
1175 rmixl_pcie_intr_evcnt(void *v, pci_intr_handle_t pih)
1176 {
1177 return NULL;
1178 }
1179
1180 static pci_intr_handle_t
1181 rmixl_pcie_make_pih(u_int link, u_int bitno, u_int irq)
1182 {
1183 pci_intr_handle_t pih;
1184
1185 KASSERT(link < RMIXL_PCIE_NLINKS_MAX);
1186 KASSERT(bitno < 64);
1187 KASSERT(irq < 32);
1188
1189 pih = (irq << 10);
1190 pih |= (bitno << 4);
1191 pih |= link;
1192
1193 return pih;
1194 }
1195
1196 static void
1197 rmixl_pcie_decompose_pih(pci_intr_handle_t pih, u_int *link, u_int *bitno, u_int *irq)
1198 {
1199 *link = (u_int)(pih & 0xf);
1200 *bitno = (u_int)((pih >> 4) & 0x3f);
1201 *irq = (u_int)(pih >> 10);
1202
1203 KASSERT(*link < RMIXL_PCIE_NLINKS_MAX);
1204 KASSERT(*bitno < 64);
1205 KASSERT(*irq < 32);
1206 }
1207
1208 static void
1209 rmixl_pcie_intr_disestablish(void *v, void *ih)
1210 {
1211 rmixl_pcie_softc_t *sc = v;
1212 rmixl_pcie_link_dispatch_t *dip = ih;
1213 rmixl_pcie_link_intr_t *lip = sc->sc_link_intr[dip->link];
1214 uint32_t r;
1215 uint32_t bit;
1216 u_int offset;
1217 u_int other;
1218 bool busy;
1219
1220 DPRINTF(("%s: link=%d pin=%d irq=%d\n",
1221 __func__, dip->link, dip->bitno + 1, dip->irq));
1222
1223 mutex_enter(&sc->sc_mutex);
1224
1225 dip->func = NULL; /* mark unused, prevent further dispatch */
1226
1227 /*
1228 * if no other dispatch handle is using this interrupt,
1229 * we can disable it
1230 */
1231 busy = false;
1232 for (int i=0; i < lip->dispatch_count; i++) {
1233 rmixl_pcie_link_dispatch_t *d = &lip->dispatch_data[i];
1234 if (d == dip)
1235 continue;
1236 if (d->bitno == dip->bitno) {
1237 busy = true;
1238 break;
1239 }
1240 }
1241 if (! busy) {
1242 if (dip->bitno < 32) {
1243 bit = 1 << dip->bitno;
1244 offset = int_enb_offset[dip->link].r0;
1245 other = int_enb_offset[dip->link].r1;
1246 } else {
1247 bit = 1 << (dip->bitno - 32);
1248 offset = int_enb_offset[dip->link].r1;
1249 other = int_enb_offset[dip->link].r0;
1250 }
1251
1252 /* disable this interrupt in the PCIe bridge */
1253 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_PCIE_LE + offset);
1254 r &= ~bit;
1255 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_PCIE_LE + offset, r);
1256
1257 /*
1258 * if both ENABLE0 and ENABLE1 are 0
1259 * disable the link interrupt
1260 */
1261 if (r == 0) {
1262 /* check the other reg */
1263 if (RMIXL_IOREG_READ(RMIXL_IO_DEV_PCIE_LE + other) == 0) {
1264 DPRINTF(("%s: disable link %d\n", __func__, lip->link));
1265
1266 /* tear down interrupt on this link */
1267 rmixl_intr_disestablish(lip->ih);
1268
1269 /* commit NULL interrupt set */
1270 sc->sc_link_intr[dip->link] = NULL;
1271
1272 /* schedule delayed free of the old link interrupt set */
1273 rmixl_pcie_lip_free_callout(lip);
1274 }
1275 }
1276 }
1277
1278 mutex_exit(&sc->sc_mutex);
1279 }
1280
1281 static void *
1282 rmixl_pcie_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
1283 int (*func)(void *), void *arg)
1284 {
1285 rmixl_pcie_softc_t *sc = v;
1286 u_int link, bitno, irq;
1287 uint32_t r;
1288 rmixl_pcie_link_intr_t *lip;
1289 rmixl_pcie_link_dispatch_t *dip = NULL;
1290 uint32_t bit;
1291 u_int offset;
1292
1293 if (pih == ~0) {
1294 DPRINTF(("%s: bad pih=%#lx, implies PCI_INTERRUPT_PIN_NONE\n",
1295 __func__, pih));
1296 return NULL;
1297 }
1298
1299 rmixl_pcie_decompose_pih(pih, &link, &bitno, &irq);
1300 DPRINTF(("%s: link=%d pin=%d irq=%d\n",
1301 __func__, link, bitno + 1, irq));
1302
1303 mutex_enter(&sc->sc_mutex);
1304
1305 lip = rmixl_pcie_lip_add_1(sc, link, irq, ipl);
1306 if (lip == NULL)
1307 return NULL;
1308
1309 /*
1310 * initializae our new interrupt, the last element in dispatch_data[]
1311 */
1312 dip = &lip->dispatch_data[lip->dispatch_count - 1];
1313 dip->link = link;
1314 dip->bitno = bitno;
1315 dip->irq = irq;
1316 dip->func = func;
1317 dip->arg = arg;
1318 dip->counts = RMIXL_PCIE_EVCNT(sc, link, bitno, 0);
1319
1320 if (bitno < 32) {
1321 offset = int_enb_offset[link].r0;
1322 bit = 1 << bitno;
1323 } else {
1324 offset = int_enb_offset[link].r1;
1325 bit = 1 << (bitno - 32);
1326 }
1327
1328 /* commit the new link interrupt set */
1329 sc->sc_link_intr[link] = lip;
1330
1331 /* enable this interrupt in the PCIe bridge */
1332 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_PCIE_LE + offset);
1333 r |= bit;
1334 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_PCIE_LE + offset, r);
1335
1336 mutex_exit(&sc->sc_mutex);
1337 return dip;
1338 }
1339
1340 rmixl_pcie_link_intr_t *
1341 rmixl_pcie_lip_add_1(rmixl_pcie_softc_t *sc, u_int link, int irq, int ipl)
1342 {
1343 rmixl_pcie_link_intr_t *lip_old = sc->sc_link_intr[link];
1344 rmixl_pcie_link_intr_t *lip_new;
1345 u_int dispatch_count;
1346 size_t size;
1347
1348 dispatch_count = 1;
1349 size = sizeof(rmixl_pcie_link_intr_t);
1350 if (lip_old != NULL) {
1351 /*
1352 * count only those dispatch elements still in use
1353 * unused ones will be pruned during copy
1354 * i.e. we are "lazy" there is no rmixl_pcie_lip_sub_1
1355 */
1356 for (int i=0; i < lip_old->dispatch_count; i++) {
1357 if (lip_old->dispatch_data[i].func != NULL) {
1358 dispatch_count++;
1359 size += sizeof(rmixl_pcie_link_intr_t);
1360 }
1361 }
1362 }
1363
1364 /*
1365 * allocate and initialize link intr struct
1366 * with one or more dispatch handles
1367 */
1368 lip_new = malloc(size, M_DEVBUF, M_NOWAIT);
1369 if (lip_new == NULL) {
1370 #ifdef DIAGNOSTIC
1371 printf("%s: cannot malloc\n", __func__);
1372 #endif
1373 return NULL;
1374 }
1375
1376 if (lip_old == NULL) {
1377 /* initialize the link interrupt struct */
1378 lip_new->sc = sc;
1379 lip_new->link = link;
1380 lip_new->ipl = ipl;
1381 lip_new->ih = rmixl_intr_establish(irq, ipl, IST_LEVEL_HIGH,
1382 rmixl_pcie_intr, lip_new, false);
1383 if (lip_new->ih == NULL)
1384 panic("%s: cannot establish irq %d", __func__, irq);
1385 } else {
1386 /*
1387 * all intrs on a link get same ipl and sc
1388 * first intr established sets the standard
1389 */
1390 KASSERT(sc == lip_old->sc);
1391 if (sc != lip_old->sc) {
1392 printf("%s: sc %p mismatch\n", __func__, sc);
1393 free(lip_new, M_DEVBUF);
1394 return NULL;
1395 }
1396 KASSERT (ipl == lip_old->ipl);
1397 if (ipl != lip_old->ipl) {
1398 printf("%s: ipl %d mismatch\n", __func__, ipl);
1399 free(lip_new, M_DEVBUF);
1400 return NULL;
1401 }
1402 /*
1403 * copy lip_old to lip_new, skipping unused dispatch elemets
1404 */
1405 memcpy(lip_new, lip_old, sizeof(rmixl_pcie_link_intr_t));
1406 for (int j=0, i=0; i < lip_old->dispatch_count; i++) {
1407 if (lip_old->dispatch_data[i].func != NULL) {
1408 memcpy(&lip_new->dispatch_data[j],
1409 &lip_old->dispatch_data[i],
1410 sizeof(rmixl_pcie_link_dispatch_t));
1411 j++;
1412 }
1413 }
1414
1415 /*
1416 * schedule delayed free of old link interrupt set
1417 */
1418 rmixl_pcie_lip_free_callout(lip_old);
1419 }
1420 lip_new->dispatch_count = dispatch_count;
1421
1422 return lip_new;
1423 }
1424
1425 /*
1426 * delay free of the old link interrupt set
1427 * to allow anyone still using it to do so safely
1428 * XXX 2 seconds should be plenty?
1429 */
1430 static void
1431 rmixl_pcie_lip_free_callout(rmixl_pcie_link_intr_t *lip)
1432 {
1433 callout_init(&lip->callout, 0);
1434 callout_reset(&lip->callout, 2 * hz, rmixl_pcie_lip_free, lip);
1435 }
1436
1437 static void
1438 rmixl_pcie_lip_free(void *arg)
1439 {
1440 rmixl_pcie_link_intr_t *lip = arg;
1441
1442 callout_destroy(&lip->callout);
1443 free(lip, M_DEVBUF);
1444 }
1445
1446 static int
1447 rmixl_pcie_intr(void *arg)
1448 {
1449 rmixl_pcie_link_intr_t *lip = arg;
1450 u_int link = lip->link;
1451 int rv = 0;
1452
1453 uint32_t status0 = RMIXL_IOREG_READ(RMIXL_IO_DEV_PCIE_LE + int_sts_offset[link].r0);
1454 uint32_t status1 = RMIXL_IOREG_READ(RMIXL_IO_DEV_PCIE_LE + int_sts_offset[link].r1);
1455 uint64_t status = ((uint64_t)status1 << 32) | status0;
1456 DPRINTF(("%s: %d:%#"PRIx64"\n", __func__, link, status));
1457
1458 if (status != 0) {
1459 rmixl_pcie_link_dispatch_t *dip;
1460
1461 if (status & RMIXL_PCIE_LINK_STATUS_ERRORS)
1462 rmixl_pcie_link_error_intr(link, status0, status1);
1463
1464 for (u_int i=0; i < lip->dispatch_count; i++) {
1465 dip = &lip->dispatch_data[i];
1466 int (*func)(void *) = dip->func;
1467 if (func != NULL) {
1468 uint64_t bit = 1 << dip->bitno;
1469 if ((status & bit) != 0) {
1470 (void)(*func)(dip->arg);
1471 dip->counts[cpu_index(curcpu())].evcnt.ev_count++;
1472 rv = 1;
1473 }
1474 }
1475 }
1476 }
1477
1478 return rv;
1479 }
1480
1481 static void
1482 rmixl_pcie_link_error_intr(u_int link, uint32_t status0, uint32_t status1)
1483 {
1484 printf("%s: mask %#"PRIx64"\n",
1485 __func__, RMIXL_PCIE_LINK_STATUS_ERRORS);
1486 printf("%s: PCIe Link Error: link=%d status0=%#x status1=%#x\n",
1487 __func__, link, status0, status1);
1488 #if defined(DDB) && defined(DEBUG)
1489 Debugger();
1490 #endif
1491 }
1492
1493 #if defined(DEBUG) || defined(DDB)
1494 /* this function exists to facilitate call from ddb */
1495 int
1496 rmixl_pcie_error_check(void)
1497 {
1498 if (rmixl_pcie_v != 0)
1499 return _rmixl_pcie_error_check(rmixl_pcie_v);
1500 return -1;
1501 }
1502 #endif
1503
1504 STATIC int
1505 _rmixl_pcie_error_check(void *v)
1506 {
1507 int i, offset;
1508 pcireg_t r;
1509 pcitag_t tag;
1510 int err=0;
1511 #ifdef DIAGNOSTIC
1512 pcireg_t regs[PCIE_ECFG_ERRS_OFFTAB_NENTRIES];
1513 #endif
1514
1515 tag = rmixl_pcie_make_tag(v, 0, 0, 0); /* XXX */
1516
1517 for (i=0; i < PCIE_ECFG_ERRS_OFFTAB_NENTRIES; i++) {
1518 offset = pcie_ecfg_errs_tab[i].offset;
1519 r = rmixl_pcie_conf_read(v, tag, offset);
1520 #ifdef DIAGNOSTIC
1521 regs[i] = r;
1522 #endif
1523 if (r != 0) {
1524 pcireg_t rw1c = r & pcie_ecfg_errs_tab[i].rw1c;
1525 if (rw1c != 0) {
1526 /* attempt to clear the error */
1527 rmixl_pcie_conf_write(v, tag, offset, rw1c);
1528 };
1529 if (offset == RMIXL_PCIE_ECFG_CESR)
1530 err |= 1; /* correctable */
1531 else
1532 err |= 2; /* uncorrectable */
1533 }
1534 }
1535 #ifdef DIAGNOSTIC
1536 if (err != 0) {
1537 for (i=0; i < PCIE_ECFG_ERRS_OFFTAB_NENTRIES; i++) {
1538 offset = pcie_ecfg_errs_tab[i].offset;
1539 printf("%s: %#x: %#x\n", __func__, offset, regs[i]);
1540 }
1541 }
1542 #endif
1543
1544 return err;
1545 }
1546
1547 static int
1548 rmixl_pcie_error_intr(void *v)
1549 {
1550 if (_rmixl_pcie_error_check(v) < 2)
1551 return 0; /* correctable */
1552
1553 /* uncorrectable */
1554 #if DDB
1555 Debugger();
1556 #endif
1557
1558 /* XXX reset and recover? */
1559
1560 panic("%s\n", __func__);
1561 }
1562
1563 /*
1564 * rmixl_physaddr_init_pcie:
1565 * called from rmixl_physaddr_init to get region addrs & sizes
1566 * from PCIE CFG, ECFG, IO, MEM BARs
1567 */
1568 void
1569 rmixl_physaddr_init_pcie(struct extent *ext)
1570 {
1571 struct rmixl_config * const rcp = &rmixl_configuration;
1572 uint32_t r;
1573
1574 r = RMIXL_IOREG_READ(RMIXLS_SBC_PCIE_CFG_BAR);
1575 if ((r & RMIXL_PCIE_CFG_BAR_ENB) != 0) {
1576 rmixl_physaddr_add(ext, "pcicfg", &rcp->rc_pci_cfg,
1577 (bus_addr_t)RMIXL_PCIE_CFG_BAR_TO_BA((uint64_t)r),
1578 (bus_size_t)RMIXL_PCIE_CFG_SIZE);
1579 DPRINTF(("%s: %s: %#"PRIx64":%"PRIu64" MB\n", __func__,
1580 "pci-cfg", rcp->rc_pci_cfg.r_pbase,
1581 rcp->rc_pci_cfg.r_size >> 20));
1582
1583 }
1584
1585 r = RMIXL_IOREG_READ(RMIXLS_SBC_PCIE_ECFG_BAR);
1586 if ((r & RMIXL_PCIE_ECFG_BAR_ENB) != 0) {
1587 rmixl_physaddr_add(ext, "pciecfg", &rcp->rc_pci_ecfg,
1588 (bus_addr_t)RMIXL_PCIE_ECFG_BAR_TO_BA((uint64_t)r),
1589 (bus_size_t)RMIXL_PCIE_ECFG_SIZE);
1590 DPRINTF(("%s: %s: %#"PRIx64":%"PRIu64" MB\n", __func__,
1591 "pci-ecfg", rcp->rc_pci_ecfg.r_pbase,
1592 rcp->rc_pci_ecfg.r_size >> 20));
1593 }
1594
1595 r = RMIXL_IOREG_READ(RMIXLS_SBC_PCIE_MEM_BAR);
1596 if ((r & RMIXL_PCIE_MEM_BAR_ENB) != 0) {
1597 rmixl_physaddr_add(ext, "pcimem", &rcp->rc_pci_mem,
1598 (bus_addr_t)RMIXL_PCIE_MEM_BAR_TO_BA((uint64_t)r),
1599 (bus_size_t)RMIXL_PCIE_MEM_BAR_TO_SIZE((uint64_t)r));
1600 DPRINTF(("%s: %s: %#"PRIx64":%"PRIu64" MB\n", __func__,
1601 "pci-mem", rcp->rc_pci_mem.r_pbase,
1602 rcp->rc_pci_mem.r_size >> 20));
1603 }
1604
1605 r = RMIXL_IOREG_READ(RMIXLS_SBC_PCIE_IO_BAR);
1606 if ((r & RMIXL_PCIE_IO_BAR_ENB) != 0) {
1607 rmixl_physaddr_add(ext, "pciio", &rcp->rc_pci_io,
1608 (bus_addr_t)RMIXL_PCIE_IO_BAR_TO_BA((uint64_t)r),
1609 (bus_size_t)RMIXL_PCIE_IO_BAR_TO_SIZE((uint64_t)r));
1610 DPRINTF(("%s: %s: %#"PRIx64":%"PRIu64" MB\n", __func__,
1611 "pci-io", rcp->rc_pci_io.r_pbase,
1612 rcp->rc_pci_io.r_size >> 20));
1613 }
1614 }
1615