tegra_pcie.c revision 1.10 1 /* $NetBSD: tegra_pcie.c,v 1.10 2015/11/14 03:44:52 jakllsch Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "locators.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: tegra_pcie.c,v 1.10 2015/11/14 03:44:52 jakllsch Exp $");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/intr.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/extent.h>
41 #include <sys/queue.h>
42 #include <sys/mutex.h>
43 #include <sys/kmem.h>
44
45 #include <arm/cpufunc.h>
46
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pciconf.h>
50
51 #include <arm/nvidia/tegra_reg.h>
52 #include <arm/nvidia/tegra_pciereg.h>
53 #include <arm/nvidia/tegra_var.h>
54
55 static int tegra_pcie_match(device_t, cfdata_t, void *);
56 static void tegra_pcie_attach(device_t, device_t, void *);
57
58 struct tegra_pcie_ih {
59 int (*ih_callback)(void *);
60 void *ih_arg;
61 int ih_ipl;
62 TAILQ_ENTRY(tegra_pcie_ih) ih_entry;
63 };
64
65 struct tegra_pcie_softc {
66 device_t sc_dev;
67 bus_dma_tag_t sc_dmat;
68 bus_space_tag_t sc_bst;
69 bus_space_handle_t sc_bsh_afi;
70 bus_space_handle_t sc_bsh_rpconf;
71 bus_space_handle_t sc_bsh_conf;
72 int sc_intr;
73
74 struct arm32_pci_chipset sc_pc;
75
76 void *sc_ih;
77
78 kmutex_t sc_lock;
79
80 TAILQ_HEAD(, tegra_pcie_ih) sc_intrs;
81 u_int sc_intrgen;
82 };
83
84 static int tegra_pcie_intr(void *);
85 static void tegra_pcie_init(pci_chipset_tag_t, void *);
86 static void tegra_pcie_enable(struct tegra_pcie_softc *);
87 static void tegra_pcie_setup(struct tegra_pcie_softc * const);
88
89 static void tegra_pcie_attach_hook(device_t, device_t,
90 struct pcibus_attach_args *);
91 static int tegra_pcie_bus_maxdevs(void *, int);
92 static pcitag_t tegra_pcie_make_tag(void *, int, int, int);
93 static void tegra_pcie_decompose_tag(void *, pcitag_t, int *, int *, int *);
94 static pcireg_t tegra_pcie_conf_read(void *, pcitag_t, int);
95 static void tegra_pcie_conf_write(void *, pcitag_t, int, pcireg_t);
96 static int tegra_pcie_conf_hook(void *, int, int, int, pcireg_t);
97 static void tegra_pcie_conf_interrupt(void *, int, int, int, int, int *);
98
99 static int tegra_pcie_intr_map(const struct pci_attach_args *,
100 pci_intr_handle_t *);
101 static const char *tegra_pcie_intr_string(void *, pci_intr_handle_t,
102 char *, size_t);
103 const struct evcnt *tegra_pcie_intr_evcnt(void *, pci_intr_handle_t);
104 static void * tegra_pcie_intr_establish(void *, pci_intr_handle_t,
105 int, int (*)(void *), void *);
106 static void tegra_pcie_intr_disestablish(void *, void *);
107
108 CFATTACH_DECL_NEW(tegra_pcie, sizeof(struct tegra_pcie_softc),
109 tegra_pcie_match, tegra_pcie_attach, NULL, NULL);
110
111 static int
112 tegra_pcie_match(device_t parent, cfdata_t cf, void *aux)
113 {
114 return 1;
115 }
116
117 static void
118 tegra_pcie_attach(device_t parent, device_t self, void *aux)
119 {
120 struct tegra_pcie_softc * const sc = device_private(self);
121 struct tegraio_attach_args * const tio = aux;
122 const struct tegra_locators * const loc = &tio->tio_loc;
123 struct extent *ioext, *memext, *pmemext;
124 struct pcibus_attach_args pba;
125 int error;
126
127 sc->sc_dev = self;
128 #if notyet
129 sc->sc_dmat = tio->tio_coherent_dmat;
130 #else
131 sc->sc_dmat = tio->tio_dmat;
132 #endif
133 sc->sc_bst = tio->tio_bst;
134 sc->sc_intr = loc->loc_intr;
135 if (bus_space_map(sc->sc_bst, TEGRA_PCIE_AFI_BASE, TEGRA_PCIE_AFI_SIZE,
136 0, &sc->sc_bsh_afi) != 0)
137 panic("couldn't map PCIE AFI");
138 if (bus_space_map(sc->sc_bst, TEGRA_PCIE_RPCONF_BASE,
139 TEGRA_PCIE_RPCONF_SIZE, 0, &sc->sc_bsh_rpconf) != 0)
140 panic("couldn't map PCIE root ports");
141 if (bus_space_map(sc->sc_bst, TEGRA_PCIE_CONF_BASE,
142 TEGRA_PCIE_CONF_SIZE, 0, &sc->sc_bsh_conf) != 0)
143 panic("couldn't map PCIE configuration");
144
145 TAILQ_INIT(&sc->sc_intrs);
146 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
147
148 aprint_naive("\n");
149 aprint_normal(": PCIE\n");
150
151 sc->sc_ih = intr_establish(loc->loc_intr, IPL_VM, IST_LEVEL,
152 tegra_pcie_intr, sc);
153 if (sc->sc_ih == NULL) {
154 aprint_error_dev(self, "failed to establish interrupt %d\n",
155 loc->loc_intr);
156 return;
157 }
158 aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
159
160 tegra_pcie_setup(sc);
161
162 tegra_pcie_init(&sc->sc_pc, sc);
163
164 ioext = extent_create("pciio", TEGRA_PCIE_IO_BASE,
165 TEGRA_PCIE_IO_BASE + TEGRA_PCIE_IO_SIZE - 1,
166 NULL, 0, EX_NOWAIT);
167 memext = extent_create("pcimem", TEGRA_PCIE_MEM_BASE,
168 TEGRA_PCIE_MEM_BASE + TEGRA_PCIE_MEM_SIZE - 1,
169 NULL, 0, EX_NOWAIT);
170 pmemext = extent_create("pcipmem", TEGRA_PCIE_PMEM_BASE,
171 TEGRA_PCIE_PMEM_BASE + TEGRA_PCIE_PMEM_SIZE - 1,
172 NULL, 0, EX_NOWAIT);
173
174 error = pci_configure_bus(&sc->sc_pc, ioext, memext, pmemext, 0,
175 arm_dcache_align);
176
177 extent_destroy(ioext);
178 extent_destroy(memext);
179 extent_destroy(pmemext);
180
181 if (error) {
182 aprint_error_dev(self, "configuration failed (%d)\n",
183 error);
184 return;
185 }
186
187 tegra_pcie_enable(sc);
188
189 memset(&pba, 0, sizeof(pba));
190 pba.pba_flags = PCI_FLAGS_MRL_OKAY |
191 PCI_FLAGS_MRM_OKAY |
192 PCI_FLAGS_MWI_OKAY |
193 PCI_FLAGS_MEM_OKAY |
194 PCI_FLAGS_IO_OKAY;
195 pba.pba_iot = sc->sc_bst;
196 pba.pba_memt = sc->sc_bst;
197 pba.pba_dmat = sc->sc_dmat;
198 pba.pba_pc = &sc->sc_pc;
199 pba.pba_bus = 0;
200
201 config_found_ia(self, "pcibus", &pba, pcibusprint);
202 }
203
204 static int
205 tegra_pcie_legacy_intr(struct tegra_pcie_softc *sc)
206 {
207 const uint32_t msg = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
208 AFI_MSG_REG);
209 struct tegra_pcie_ih *pcie_ih;
210 int rv = 0;
211
212 if (msg & (AFI_MSG_INT0|AFI_MSG_INT1)) {
213 mutex_enter(&sc->sc_lock);
214 const u_int lastgen = sc->sc_intrgen;
215 TAILQ_FOREACH(pcie_ih, &sc->sc_intrs, ih_entry) {
216 int (*callback)(void *) = pcie_ih->ih_callback;
217 void *arg = pcie_ih->ih_arg;
218 mutex_exit(&sc->sc_lock);
219 rv += callback(arg);
220 mutex_enter(&sc->sc_lock);
221 if (lastgen != sc->sc_intrgen)
222 break;
223 }
224 mutex_exit(&sc->sc_lock);
225 } else if (msg & (AFI_MSG_PM_PME0|AFI_MSG_PM_PME1)) {
226 device_printf(sc->sc_dev, "PM PME message; AFI_MSG=%08x\n",
227 msg);
228 } else {
229 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_MSG_REG, msg);
230 rv = 1;
231 }
232
233 return rv;
234 }
235
236 static int
237 tegra_pcie_intr(void *priv)
238 {
239 struct tegra_pcie_softc *sc = priv;
240
241 const uint32_t code = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
242 AFI_INTR_CODE_REG);
243 const uint32_t sig = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
244 AFI_INTR_SIGNATURE_REG);
245 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
246
247 switch (__SHIFTOUT(code, AFI_INTR_CODE_INT_CODE)) {
248 case AFI_INTR_CODE_SM_MSG:
249 return tegra_pcie_legacy_intr(sc);
250 default:
251 device_printf(sc->sc_dev, "intr: code %#x sig %#x\n",
252 code, sig);
253 return 1;
254 }
255 }
256
257 static void
258 tegra_pcie_setup(struct tegra_pcie_softc * const sc)
259 {
260 size_t i;
261
262 /*
263 * Map PCI address spaces into ARM address space via
264 * HyperTransport-like "FPCI".
265 */
266 static const struct { uint32_t size, base, fpci; } pcie_init_table[] = {
267 /*
268 * === BEWARE ===
269 *
270 * We depend on our TEGRA_PCIE_IO window overlaping the
271 * TEGRA_PCIE_A1 window to allow us to use the same
272 * bus_space_tag for both PCI IO and Memory spaces.
273 *
274 * 0xfdfc000000-0xfdfdffffff is the FPCI/HyperTransport
275 * mapping for 0x0000000-0x1ffffff of PCI IO space.
276 */
277 { TEGRA_PCIE_IO_SIZE >> 12, TEGRA_PCIE_IO_BASE,
278 (0xfdfc000000 + TEGRA_PCIE_IO_BASE) >> 8 | 0, },
279
280 /* HyperTransport Technology Type 1 Address Format */
281 { TEGRA_PCIE_CONF_SIZE >> 12, TEGRA_PCIE_CONF_BASE,
282 0xfdff000000 >> 8 | 0, },
283
284 /* 1:1 MMIO mapping */
285 { TEGRA_PCIE_MEM_SIZE >> 12, TEGRA_PCIE_MEM_BASE,
286 TEGRA_PCIE_MEM_BASE >> 8 | 1, },
287
288 /* Extended HyperTransport Technology Type 1 Address Format */
289 { TEGRA_PCIE_EXTC_SIZE >> 12, TEGRA_PCIE_EXTC_BASE,
290 0xfe10000000 >> 8 | 0, },
291
292 /* 1:1 prefetchable MMIO mapping */
293 { TEGRA_PCIE_PMEM_SIZE >> 12, TEGRA_PCIE_PMEM_BASE,
294 TEGRA_PCIE_PMEM_BASE >> 8 | 1, },
295 };
296
297 for (i = 0; i < AFI_AXI_NBAR; i++) {
298 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
299 AFI_AXI_BARi_SZ(i), 0);
300 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
301 AFI_AXI_BARi_START(i), 0);
302 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
303 AFI_FPCI_BARi(i), 0);
304 }
305
306 for (i = 0; i < __arraycount(pcie_init_table); i++) {
307 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
308 AFI_AXI_BARi_START(i), pcie_init_table[i].base);
309 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
310 AFI_FPCI_BARi(i), pcie_init_table[i].fpci);
311 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
312 AFI_AXI_BARi_SZ(i), pcie_init_table[i].size);
313 }
314 }
315
316 static void
317 tegra_pcie_enable(struct tegra_pcie_softc *sc)
318 {
319 /* disable MSI */
320 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
321 AFI_MSI_BAR_SZ_REG, 0);
322 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
323 AFI_MSI_FPCI_BAR_ST_REG, 0);
324 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
325 AFI_MSI_AXI_BAR_ST_REG, 0);
326
327 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
328 AFI_SM_INTR_ENABLE_REG, 0xffffffff);
329 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
330 AFI_AFI_INTR_ENABLE_REG, 0);
331 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
332 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
333 AFI_INTR_MASK_REG, AFI_INTR_MASK_INT);
334 }
335
336 void
337 tegra_pcie_init(pci_chipset_tag_t pc, void *priv)
338 {
339 pc->pc_conf_v = priv;
340 pc->pc_attach_hook = tegra_pcie_attach_hook;
341 pc->pc_bus_maxdevs = tegra_pcie_bus_maxdevs;
342 pc->pc_make_tag = tegra_pcie_make_tag;
343 pc->pc_decompose_tag = tegra_pcie_decompose_tag;
344 pc->pc_conf_read = tegra_pcie_conf_read;
345 pc->pc_conf_write = tegra_pcie_conf_write;
346 pc->pc_conf_hook = tegra_pcie_conf_hook;
347 pc->pc_conf_interrupt = tegra_pcie_conf_interrupt;
348
349 pc->pc_intr_v = priv;
350 pc->pc_intr_map = tegra_pcie_intr_map;
351 pc->pc_intr_string = tegra_pcie_intr_string;
352 pc->pc_intr_evcnt = tegra_pcie_intr_evcnt;
353 pc->pc_intr_establish = tegra_pcie_intr_establish;
354 pc->pc_intr_disestablish = tegra_pcie_intr_disestablish;
355 }
356
357 static void
358 tegra_pcie_attach_hook(device_t parent, device_t self,
359 struct pcibus_attach_args *pba)
360 {
361 }
362
363 static int
364 tegra_pcie_bus_maxdevs(void *v, int busno)
365 {
366 return busno == 0 ? 2 : 32;
367 }
368
369 static pcitag_t
370 tegra_pcie_make_tag(void *v, int b, int d, int f)
371 {
372 return (b << 16) | (d << 11) | (f << 8);
373 }
374
375 static void
376 tegra_pcie_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
377 {
378 if (bp)
379 *bp = (tag >> 16) & 0xff;
380 if (dp)
381 *dp = (tag >> 11) & 0x1f;
382 if (fp)
383 *fp = (tag >> 8) & 0x7;
384 }
385
386 static pcireg_t
387 tegra_pcie_conf_read(void *v, pcitag_t tag, int offset)
388 {
389 struct tegra_pcie_softc *sc = v;
390 bus_space_handle_t bsh;
391 int b, d, f;
392 u_int reg;
393
394 if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
395 return (pcireg_t) -1;
396
397 tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
398
399 if (b == 0) {
400 if (d >= 2 || f != 0)
401 return (pcireg_t) -1;
402 reg = d * 0x1000 + offset;
403 bsh = sc->sc_bsh_rpconf;
404 } else {
405 if ((unsigned int)offset >= PCI_CONF_SIZE)
406 return (pcireg_t) -1;
407 reg = tag | offset;
408 bsh = sc->sc_bsh_conf;
409 }
410
411 return bus_space_read_4(sc->sc_bst, bsh, reg);
412 }
413
414 static void
415 tegra_pcie_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
416 {
417 struct tegra_pcie_softc *sc = v;
418 bus_space_handle_t bsh;
419 int b, d, f;
420 u_int reg;
421
422 if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
423 return;
424
425 tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
426
427 if (b == 0) {
428 if (d >= 2 || f != 0)
429 return;
430 reg = d * 0x1000 + offset;
431 bsh = sc->sc_bsh_rpconf;
432 } else {
433 if ((unsigned int)offset >= PCI_CONF_SIZE)
434 return;
435 reg = tag | offset;
436 bsh = sc->sc_bsh_conf;
437 }
438
439 bus_space_write_4(sc->sc_bst, bsh, reg, val);
440 }
441
442 static int
443 tegra_pcie_conf_hook(void *v, int b, int d, int f, pcireg_t id)
444 {
445 return PCI_CONF_ALL;
446 }
447
448 static void
449 tegra_pcie_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz,
450 int *ilinep)
451 {
452 const struct tegra_pcie_softc * const sc = v;
453
454 *ilinep = sc->sc_intr & PCI_INTERRUPT_LINE_MASK;
455 }
456
457 static int
458 tegra_pcie_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
459 {
460 if (pa->pa_intrpin == 0)
461 return EINVAL;
462 *ih = pa->pa_intrpin;
463 return 0;
464 }
465
466 static const char *
467 tegra_pcie_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
468 {
469 struct tegra_pcie_softc *sc = v;
470
471 if (ih == PCI_INTERRUPT_PIN_NONE)
472 return NULL;
473
474 snprintf(buf, len, "irq %d", sc->sc_intr);
475 return buf;
476 }
477
478 const struct evcnt *
479 tegra_pcie_intr_evcnt(void *v, pci_intr_handle_t ih)
480 {
481 return NULL;
482 }
483
484 static void *
485 tegra_pcie_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
486 int (*callback)(void *), void *arg)
487 {
488 struct tegra_pcie_softc *sc = v;
489 struct tegra_pcie_ih *pcie_ih;
490
491 if (ih == 0)
492 return NULL;
493
494 pcie_ih = kmem_alloc(sizeof(*pcie_ih), KM_SLEEP);
495 pcie_ih->ih_callback = callback;
496 pcie_ih->ih_arg = arg;
497 pcie_ih->ih_ipl = ipl;
498
499 mutex_enter(&sc->sc_lock);
500 TAILQ_INSERT_TAIL(&sc->sc_intrs, pcie_ih, ih_entry);
501 sc->sc_intrgen++;
502 mutex_exit(&sc->sc_lock);
503
504 return pcie_ih;
505 }
506
507 static void
508 tegra_pcie_intr_disestablish(void *v, void *vih)
509 {
510 struct tegra_pcie_softc *sc = v;
511 struct tegra_pcie_ih *pcie_ih = vih;
512
513 mutex_enter(&sc->sc_lock);
514 TAILQ_REMOVE(&sc->sc_intrs, pcie_ih, ih_entry);
515 mutex_exit(&sc->sc_lock);
516
517 kmem_free(pcie_ih, sizeof(*pcie_ih));
518 }
519