tegra_pcie.c revision 1.1 1 /* $NetBSD: tegra_pcie.c,v 1.1 2015/05/03 01:07:44 jmcneill 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.1 2015/05/03 01:07:44 jmcneill 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_a1;
71 bus_space_handle_t sc_bsh_a2;
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
88 static void tegra_pcie_attach_hook(device_t, device_t,
89 struct pcibus_attach_args *);
90 static int tegra_pcie_bus_maxdevs(void *, int);
91 static pcitag_t tegra_pcie_make_tag(void *, int, int, int);
92 static void tegra_pcie_decompose_tag(void *, pcitag_t, int *, int *, int *);
93 static pcireg_t tegra_pcie_conf_read(void *, pcitag_t, int);
94 static void tegra_pcie_conf_write(void *, pcitag_t, int, pcireg_t);
95 static int tegra_pcie_conf_hook(void *, int, int, int, pcireg_t);
96 static void tegra_pcie_conf_interrupt(void *, int, int, int, int, int *);
97
98 static int tegra_pcie_intr_map(const struct pci_attach_args *,
99 pci_intr_handle_t *);
100 static const char *tegra_pcie_intr_string(void *, pci_intr_handle_t,
101 char *, size_t);
102 const struct evcnt *tegra_pcie_intr_evcnt(void *, pci_intr_handle_t);
103 static void * tegra_pcie_intr_establish(void *, pci_intr_handle_t,
104 int, int (*)(void *), void *);
105 static void tegra_pcie_intr_disestablish(void *, void *);
106
107 CFATTACH_DECL_NEW(tegra_pcie, sizeof(struct tegra_pcie_softc),
108 tegra_pcie_match, tegra_pcie_attach, NULL, NULL);
109
110 static int
111 tegra_pcie_match(device_t parent, cfdata_t cf, void *aux)
112 {
113 return 1;
114 }
115
116 static void
117 tegra_pcie_attach(device_t parent, device_t self, void *aux)
118 {
119 struct tegra_pcie_softc * const sc = device_private(self);
120 struct tegraio_attach_args * const tio = aux;
121 const struct tegra_locators * const loc = &tio->tio_loc;
122 struct extent *memext, *pmemext;
123 struct pcibus_attach_args pba;
124 int error;
125
126 sc->sc_dev = self;
127 sc->sc_dmat = tio->tio_coherent_dmat;
128 sc->sc_bst = tio->tio_bst;
129 sc->sc_intr = loc->loc_intr;
130 if (bus_space_map(sc->sc_bst, TEGRA_PCIE_AFI_BASE, TEGRA_PCIE_AFI_SIZE,
131 0, &sc->sc_bsh_afi) != 0)
132 panic("couldn't map PCIE AFI");
133 if (bus_space_map(sc->sc_bst, TEGRA_PCIE_A1_BASE, TEGRA_PCIE_A1_SIZE,
134 0, &sc->sc_bsh_a1) != 0)
135 panic("couldn't map PCIE A1");
136 if (bus_space_map(sc->sc_bst, TEGRA_PCIE_A2_BASE, TEGRA_PCIE_A2_SIZE,
137 0, &sc->sc_bsh_a2) != 0)
138 panic("couldn't map PCIE A2");
139
140 TAILQ_INIT(&sc->sc_intrs);
141 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
142
143 aprint_naive("\n");
144 aprint_normal(": PCIE\n");
145
146 sc->sc_ih = intr_establish(loc->loc_intr, IPL_VM, IST_LEVEL,
147 tegra_pcie_intr, sc);
148 if (sc->sc_ih == NULL) {
149 aprint_error_dev(self, "failed to establish interrupt %d\n",
150 loc->loc_intr);
151 return;
152 }
153 aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
154
155 tegra_pcie_init(&sc->sc_pc, sc);
156
157 memext = extent_create("pcimem", TEGRA_PCIE_MEM_BASE,
158 TEGRA_PCIE_MEM_BASE + TEGRA_PCIE_MEM_SIZE - 1,
159 NULL, 0, EX_NOWAIT);
160 pmemext = extent_create("pcipmem", TEGRA_PCIE_PMEM_BASE,
161 TEGRA_PCIE_PMEM_BASE + TEGRA_PCIE_PMEM_SIZE - 1,
162 NULL, 0, EX_NOWAIT);
163
164 error = pci_configure_bus(&sc->sc_pc, NULL, memext, pmemext, 0,
165 arm_dcache_align);
166
167 extent_destroy(memext);
168 extent_destroy(pmemext);
169
170 if (error) {
171 aprint_error_dev(self, "configuration failed (%d)\n",
172 error);
173 return;
174 }
175
176 tegra_pcie_enable(sc);
177
178 memset(&pba, 0, sizeof(pba));
179 pba.pba_flags = PCI_FLAGS_MRL_OKAY |
180 PCI_FLAGS_MRM_OKAY |
181 PCI_FLAGS_MWI_OKAY |
182 PCI_FLAGS_MEM_OKAY;
183 pba.pba_memt = sc->sc_bst;
184 pba.pba_dmat = sc->sc_dmat;
185 pba.pba_pc = &sc->sc_pc;
186 pba.pba_bus = 0;
187
188 config_found_ia(self, "pcibus", &pba, pcibusprint);
189 }
190
191 static int
192 tegra_pcie_intr(void *priv)
193 {
194 struct tegra_pcie_softc *sc = priv;
195 struct tegra_pcie_ih *pcie_ih;
196
197 const uint32_t code = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
198 AFI_INTR_CODE_REG);
199 const uint32_t sig = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
200 AFI_INTR_SIGNATURE_REG);
201 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
202
203 switch (__SHIFTOUT(code, AFI_INTR_CODE_INT_CODE)) {
204 case AFI_INTR_CODE_SM_MSG:
205 mutex_enter(&sc->sc_lock);
206 const u_int lastgen = sc->sc_intrgen;
207 TAILQ_FOREACH(pcie_ih, &sc->sc_intrs, ih_entry) {
208 int (*callback)(void *) = pcie_ih->ih_callback;
209 void *arg = pcie_ih->ih_arg;
210 mutex_exit(&sc->sc_lock);
211 const int rv = callback(arg);
212 if (rv)
213 return rv;
214 mutex_enter(&sc->sc_lock);
215 if (lastgen != sc->sc_intrgen)
216 break;
217 }
218 mutex_exit(&sc->sc_lock);
219 return 0;
220 default:
221 device_printf(sc->sc_dev, "intr: code %#x sig %#x\n",
222 code, sig);
223 return 1;
224 }
225 }
226
227 static void
228 tegra_pcie_enable(struct tegra_pcie_softc *sc)
229 {
230 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
231 AFI_SM_INTR_ENABLE_REG, 0xffffffff);
232 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
233 AFI_AFI_INTR_ENABLE_REG, 0);
234 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
235 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
236 AFI_INTR_MASK_REG, AFI_INTR_MASK_INT);
237 }
238
239 void
240 tegra_pcie_init(pci_chipset_tag_t pc, void *priv)
241 {
242 pc->pc_conf_v = priv;
243 pc->pc_attach_hook = tegra_pcie_attach_hook;
244 pc->pc_bus_maxdevs = tegra_pcie_bus_maxdevs;
245 pc->pc_make_tag = tegra_pcie_make_tag;
246 pc->pc_decompose_tag = tegra_pcie_decompose_tag;
247 pc->pc_conf_read = tegra_pcie_conf_read;
248 pc->pc_conf_write = tegra_pcie_conf_write;
249 pc->pc_conf_hook = tegra_pcie_conf_hook;
250 pc->pc_conf_interrupt = tegra_pcie_conf_interrupt;
251
252 pc->pc_intr_v = priv;
253 pc->pc_intr_map = tegra_pcie_intr_map;
254 pc->pc_intr_string = tegra_pcie_intr_string;
255 pc->pc_intr_evcnt = tegra_pcie_intr_evcnt;
256 pc->pc_intr_establish = tegra_pcie_intr_establish;
257 pc->pc_intr_disestablish = tegra_pcie_intr_disestablish;
258 }
259
260 static void
261 tegra_pcie_attach_hook(device_t parent, device_t self,
262 struct pcibus_attach_args *pba)
263 {
264 }
265
266 static int
267 tegra_pcie_bus_maxdevs(void *v, int busno)
268 {
269 return busno == 0 ? 2 : 32;
270 }
271
272 static pcitag_t
273 tegra_pcie_make_tag(void *v, int b, int d, int f)
274 {
275 return (b << 16) | (d << 11) | (f << 8);
276 }
277
278 static void
279 tegra_pcie_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
280 {
281 if (bp)
282 *bp = (tag >> 16) & 0xff;
283 if (dp)
284 *dp = (tag >> 11) & 0x1f;
285 if (fp)
286 *fp = (tag >> 8) & 0x7;
287 }
288
289 static pcireg_t
290 tegra_pcie_conf_read(void *v, pcitag_t tag, int offset)
291 {
292 struct tegra_pcie_softc *sc = v;
293 bus_space_handle_t bsh;
294 int b, d, f;
295 u_int reg;
296
297 tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
298
299 if (b == 0) {
300 reg = d * 0x1000 + offset;
301 bsh = sc->sc_bsh_a1;
302 } else {
303 reg = tag | offset;
304 bsh = sc->sc_bsh_a2;
305 }
306
307 return bus_space_read_4(sc->sc_bst, bsh, reg);
308 }
309
310 static void
311 tegra_pcie_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
312 {
313 struct tegra_pcie_softc *sc = v;
314 bus_space_handle_t bsh;
315 int b, d, f;
316 u_int reg;
317
318 tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
319
320 if (b == 0) {
321 reg = d * 0x1000 + offset;
322 bsh = sc->sc_bsh_a1;
323 } else {
324 reg = tag | offset;
325 bsh = sc->sc_bsh_a2;
326 }
327
328 bus_space_write_4(sc->sc_bst, bsh, reg, val);
329 }
330
331 static int
332 tegra_pcie_conf_hook(void *v, int b, int d, int f, pcireg_t id)
333 {
334 return PCI_CONF_ENABLE_MEM | PCI_CONF_MAP_MEM | PCI_CONF_ENABLE_BM;
335 }
336
337 static void
338 tegra_pcie_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz,
339 int *ilinep)
340 {
341 *ilinep = 5;
342 }
343
344 static int
345 tegra_pcie_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
346 {
347 if (pa->pa_intrpin == 0)
348 return EINVAL;
349 *ih = pa->pa_intrpin;
350 return 0;
351 }
352
353 static const char *
354 tegra_pcie_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
355 {
356 struct tegra_pcie_softc *sc = v;
357
358 if (ih == PCI_INTERRUPT_PIN_NONE)
359 return NULL;
360
361 snprintf(buf, len, "irq %d", sc->sc_intr);
362 return buf;
363 }
364
365 const struct evcnt *
366 tegra_pcie_intr_evcnt(void *v, pci_intr_handle_t ih)
367 {
368 return NULL;
369 }
370
371 static void *
372 tegra_pcie_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
373 int (*callback)(void *), void *arg)
374 {
375 struct tegra_pcie_softc *sc = v;
376 struct tegra_pcie_ih *pcie_ih;
377
378 if (ih == 0)
379 return NULL;
380
381 pcie_ih = kmem_alloc(sizeof(*pcie_ih), KM_SLEEP);
382 pcie_ih->ih_callback = callback;
383 pcie_ih->ih_arg = arg;
384 pcie_ih->ih_ipl = ipl;
385
386 mutex_enter(&sc->sc_lock);
387 TAILQ_INSERT_TAIL(&sc->sc_intrs, pcie_ih, ih_entry);
388 sc->sc_intrgen++;
389 mutex_exit(&sc->sc_lock);
390
391 return pcie_ih;
392 }
393
394 static void
395 tegra_pcie_intr_disestablish(void *v, void *vih)
396 {
397 struct tegra_pcie_softc *sc = v;
398 struct tegra_pcie_ih *pcie_ih = vih;
399
400 mutex_enter(&sc->sc_lock);
401 TAILQ_REMOVE(&sc->sc_intrs, pcie_ih, ih_entry);
402 mutex_exit(&sc->sc_lock);
403
404 kmem_free(pcie_ih, sizeof(*pcie_ih));
405 }
406