apple_pcie.c revision 1.2 1 /* $NetBSD: apple_pcie.c,v 1.2 2021/09/04 12:35:31 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2021 Jared 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 <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: apple_pcie.c,v 1.2 2021/09/04 12:35:31 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kmem.h>
38 #include <sys/bitops.h>
39
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pciconf.h>
43
44 #include <dev/fdt/fdtvar.h>
45
46 #include <arm/pci/pci_msi_machdep.h>
47 #include <arm/fdt/pcihost_fdtvar.h>
48
49 #define PCIE_MSI_CTRL 0x0124
50 #define PCIE_MSI_CTRL_EN (1U << 0)
51 #define PCIE_MSI_CTRL_32 (5U << 4)
52 #define PCIE_MSI_REMAP 0x0128
53 #define PCIE_MSI_DOORBELL 0x0168
54
55 struct apple_pcie_softc {
56 struct pcihost_softc sc_pcihost;
57
58 int sc_phandle;
59 struct arm_pci_msi sc_msi;
60 u_int sc_msi_start;
61 u_int sc_nmsi;
62 struct pci_attach_args **sc_msi_pa;
63 void **sc_msi_ih;
64 uint64_t sc_msi_addr;
65 };
66
67 static int apple_pcie_match(device_t, cfdata_t, void *);
68 static void apple_pcie_attach(device_t, device_t, void *);
69
70 static void apple_pcie_attach_hook(device_t, device_t,
71 struct pcibus_attach_args *);
72 static int apple_pcie_msi_init(struct apple_pcie_softc *);
73
74 CFATTACH_DECL_NEW(apple_pcie, sizeof(struct apple_pcie_softc),
75 apple_pcie_match, apple_pcie_attach, NULL, NULL);
76
77 static const struct device_compatible_entry compat_data[] = {
78 { .compat = "apple,pcie" },
79 DEVICE_COMPAT_EOL
80 };
81
82 static int
83 apple_pcie_match(device_t parent, cfdata_t cf, void *aux)
84 {
85 struct fdt_attach_args * const faa = aux;
86
87 return of_compatible_match(faa->faa_phandle, compat_data);
88 }
89
90 static void
91 apple_pcie_attach(device_t parent, device_t self, void *aux)
92 {
93 struct apple_pcie_softc * const asc = device_private(self);
94 struct pcihost_softc * const sc = &asc->sc_pcihost;
95 struct fdt_attach_args * const faa = aux;
96 const int phandle = faa->faa_phandle;
97 bus_addr_t cs_addr;
98 bus_size_t cs_size;
99 int error;
100
101 if (fdtbus_get_reg(phandle, 0, &cs_addr, &cs_size) != 0) {
102 aprint_error(": couldn't get registers\n");
103 return;
104 }
105
106 sc->sc_dev = self;
107 sc->sc_dmat = faa->faa_dmat;
108 sc->sc_bst = faa->faa_bst;
109 sc->sc_phandle = phandle;
110 error = bus_space_map(sc->sc_bst, cs_addr, cs_size,
111 _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &sc->sc_bsh);
112 if (error) {
113 aprint_error(": couldn't map registers: %d\n", error);
114 return;
115 }
116 sc->sc_type = PCIHOST_ECAM;
117
118 if (apple_pcie_msi_init(asc) == 0) {
119 sc->sc_pci_flags |= PCI_FLAGS_MSI_OKAY;
120 #if notyet
121 sc->sc_pci_flags |= PCI_FLAGS_MSIX_OKAY;
122 #endif
123 }
124
125 aprint_naive("\n");
126 aprint_normal(": Apple PCIe host controller\n");
127
128 pcihost_init(&sc->sc_pc, sc);
129 sc->sc_pc.pc_attach_hook = apple_pcie_attach_hook;
130 pcihost_init2(sc);
131 }
132
133 static void
134 apple_pcie_setup_port(struct apple_pcie_softc *sc, u_int portno)
135 {
136 const int phandle = sc->sc_pcihost.sc_phandle;
137 bus_space_tag_t bst = sc->sc_pcihost.sc_bst;
138 char regname[sizeof("portX")];
139 bus_space_handle_t bsh;
140 bus_addr_t addr;
141 bus_size_t size;
142 int error;
143
144 snprintf(regname, sizeof(regname), "port%u", portno);
145 if (fdtbus_get_reg_byname(phandle, regname, &addr, &size) != 0) {
146 aprint_error(": couldn't get %s regs\n", regname);
147 return;
148 }
149 error = bus_space_map(bst, addr, size,
150 _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &bsh);
151 if (error != 0) {
152 aprint_error(": couldn't map %s regs\n", regname);
153 return;
154 }
155
156 /* Doorbell address must be below 4GB */
157 KASSERT((sc->sc_msi_addr & ~0xffffffffUL) == 0);
158
159 bus_space_write_4(bst, bsh, PCIE_MSI_CTRL,
160 PCIE_MSI_CTRL_32 | PCIE_MSI_CTRL_EN);
161 bus_space_write_4(bst, bsh, PCIE_MSI_REMAP, 0);
162 bus_space_write_4(bst, bsh, PCIE_MSI_DOORBELL,
163 (uint32_t)sc->sc_msi_addr);
164
165 bus_space_unmap(bst, bsh, size);
166 }
167
168 static void
169 apple_pcie_attach_hook(device_t parent, device_t self,
170 struct pcibus_attach_args *pba)
171 {
172 struct apple_pcie_softc *sc = pba->pba_pc->pc_conf_v;
173 const int phandle = sc->sc_pcihost.sc_phandle;
174 bus_dma_tag_t dmat;
175
176 KASSERT(device_is_a(sc->sc_pcihost.sc_dev, "applepcie"));
177
178 /* XXX this should be per-device, not per-bus */
179 const uint32_t rid = pba->pba_bus << 8;
180
181 dmat = fdtbus_iommu_map_pci(phandle, rid, sc->sc_pcihost.sc_dmat);
182 pba->pba_dmat = pba->pba_dmat64 = dmat;
183 }
184
185 static int
186 apple_pcie_msi_alloc_msi(struct apple_pcie_softc *sc, int count,
187 const struct pci_attach_args *pa)
188 {
189 struct pci_attach_args *new_pa;
190 int msi, n;
191
192 for (msi = 0; msi < sc->sc_nmsi; msi += count) {
193 if (sc->sc_msi_pa[msi] == NULL) {
194 for (n = 1; n < count; n++) {
195 if (msi + n < sc->sc_nmsi &&
196 sc->sc_msi_pa[msi + n] != NULL) {
197 continue;
198 }
199 }
200
201 for (n = 0; n < count; n++) {
202 new_pa = kmem_alloc(sizeof(*new_pa), KM_SLEEP);
203 memcpy(new_pa, pa, sizeof(*new_pa));
204 sc->sc_msi_pa[msi + n] = new_pa;
205 }
206
207 return msi;
208 }
209 }
210
211 return -1;
212 }
213
214 static void
215 apple_pcie_msi_free_msi(struct apple_pcie_softc *sc, int msi)
216 {
217 struct pci_attach_args *pa;
218
219 pa = sc->sc_msi_pa[msi];
220 sc->sc_msi_pa[msi] = NULL;
221
222 if (pa != NULL) {
223 kmem_free(pa, sizeof(*pa));
224 }
225 }
226
227 static int
228 apple_pcie_msi_available_msi(struct apple_pcie_softc *sc)
229 {
230 int msi, n;
231
232 for (n = 0, msi = 0; msi < sc->sc_nmsi; msi++) {
233 if (sc->sc_msi_pa[msi] == NULL) {
234 n++;
235 }
236 }
237
238 return n;
239 }
240
241 static void
242 apple_pcie_msi_msi_enable(struct apple_pcie_softc *sc, int msi, int count)
243 {
244 const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
245 pci_chipset_tag_t pc = pa->pa_pc;
246 pcitag_t tag = pa->pa_tag;
247 pcireg_t ctl;
248 int off;
249
250 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
251 panic("apple_pcie_msi_msi_enable: device is not MSI-capable");
252
253 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
254 ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
255 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
256
257 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
258 ctl &= ~PCI_MSI_CTL_MME_MASK;
259 ctl |= __SHIFTIN(ilog2(count), PCI_MSI_CTL_MME_MASK);
260 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
261
262 const uint64_t addr = sc->sc_msi_addr;
263 const uint32_t data = msi;
264
265 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
266 if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
267 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
268 addr & 0xffffffff);
269 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
270 (addr >> 32) & 0xffffffff);
271 pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, data);
272 } else {
273 pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
274 addr & 0xffffffff);
275 pci_conf_write(pc, tag, off + PCI_MSI_MDATA, data);
276 }
277 ctl |= PCI_MSI_CTL_MSI_ENABLE;
278 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
279 }
280
281 static void
282 apple_pcie_msi_msi_disable(struct apple_pcie_softc *sc, int msi)
283 {
284 const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
285 pci_chipset_tag_t pc = pa->pa_pc;
286 pcitag_t tag = pa->pa_tag;
287 pcireg_t ctl;
288 int off;
289
290 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
291 panic("apple_pcie_msi_msi_disable: device is not MSI-capable");
292
293 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
294 ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
295 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
296 }
297
298 static void
299 apple_pcie_msi_msix_enable(struct apple_pcie_softc *sc, int msi, int msix_vec,
300 bus_space_tag_t bst, bus_space_handle_t bsh)
301 {
302 const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
303 pci_chipset_tag_t pc = pa->pa_pc;
304 pcitag_t tag = pa->pa_tag;
305 pcireg_t ctl;
306 uint32_t val;
307 int off;
308
309 if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
310 panic("apple_pcie_msi_msix_enable: device is not MSI-X-capable");
311
312 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
313 ctl &= ~PCI_MSIX_CTL_ENABLE;
314 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
315
316 const uint64_t addr = sc->sc_msi_addr;
317 const uint32_t data = msi;
318 const uint64_t entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
319 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO,
320 (uint32_t)addr);
321 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI,
322 (uint32_t)(addr >> 32));
323 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_DATA,
324 data);
325 val = bus_space_read_4(bst, bsh,
326 entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL);
327 val &= ~PCI_MSIX_VECTCTL_MASK;
328 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL,
329 val);
330
331 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
332 ctl |= PCI_MSIX_CTL_ENABLE;
333 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
334 }
335
336 static void
337 apple_pcie_msi_msix_disable(struct apple_pcie_softc *sc, int msi)
338 {
339 const struct pci_attach_args *pa = sc->sc_msi_pa[msi];
340 pci_chipset_tag_t pc = pa->pa_pc;
341 pcitag_t tag = pa->pa_tag;
342 pcireg_t ctl;
343 int off;
344
345 if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
346 panic("apple_pcie_msi_msix_disable: device is not MSI-X-capable");
347
348 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
349 ctl &= ~PCI_MSIX_CTL_ENABLE;
350 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
351 }
352
353 static pci_intr_handle_t *
354 apple_pcie_msi_msi_alloc(struct arm_pci_msi *msi, int *count,
355 const struct pci_attach_args *pa, bool exact)
356 {
357 struct apple_pcie_softc * const sc = msi->msi_priv;
358 pci_intr_handle_t *vectors;
359 int n, off;
360
361 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
362 return NULL;
363
364 const int avail = apple_pcie_msi_available_msi(sc);
365 if (exact && *count > avail)
366 return NULL;
367
368 while (*count > avail) {
369 if (avail < *count)
370 (*count) >>= 1;
371 }
372 if (*count == 0)
373 return NULL;
374
375 const int msi_base = apple_pcie_msi_alloc_msi(sc, *count, pa);
376 if (msi_base == -1)
377 return NULL;
378
379 vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
380 for (n = 0; n < *count; n++) {
381 const int msino = msi_base + n;
382 vectors[n] = ARM_PCI_INTR_MSI |
383 __SHIFTIN(msino, ARM_PCI_INTR_IRQ) |
384 __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
385 __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
386 }
387
388 apple_pcie_msi_msi_enable(sc, msi_base, *count);
389
390 return vectors;
391 }
392
393 static pci_intr_handle_t *
394 apple_pcie_msi_msix_alloc(struct arm_pci_msi *msi, u_int *table_indexes,
395 int *count, const struct pci_attach_args *pa, bool exact)
396 {
397 struct apple_pcie_softc * const sc = msi->msi_priv;
398 pci_intr_handle_t *vectors;
399 bus_space_tag_t bst;
400 bus_space_handle_t bsh;
401 bus_size_t bsz;
402 uint32_t table_offset, table_size;
403 int n, off, bar, error;
404 pcireg_t tbl;
405
406 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &off, NULL))
407 return NULL;
408
409 const int avail = apple_pcie_msi_available_msi(sc);
410 if (exact && *count > avail)
411 return NULL;
412
413 while (*count > avail) {
414 if (avail < *count)
415 (*count) >>= 1;
416 }
417 if (*count == 0)
418 return NULL;
419
420 tbl = pci_conf_read(pa->pa_pc, pa->pa_tag, off + PCI_MSIX_TBLOFFSET);
421 bar = PCI_BAR0 + (4 * (tbl & PCI_MSIX_TBLBIR_MASK));
422 table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
423 table_size = pci_msix_count(pa->pa_pc, pa->pa_tag) * PCI_MSIX_TABLE_ENTRY_SIZE;
424 if (table_size == 0)
425 return NULL;
426
427 error = pci_mapreg_submap(pa, bar, pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar),
428 BUS_SPACE_MAP_LINEAR, roundup(table_size, PAGE_SIZE), table_offset,
429 &bst, &bsh, NULL, &bsz);
430 if (error)
431 return NULL;
432
433 const int msi_base = apple_pcie_msi_alloc_msi(sc, *count, pa);
434 if (msi_base == -1) {
435 bus_space_unmap(bst, bsh, bsz);
436 return NULL;
437 }
438
439 vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
440 for (n = 0; n < *count; n++) {
441 const int msino = msi_base + n;
442 const int msix_vec = table_indexes ? table_indexes[n] : n;
443 vectors[msix_vec] = ARM_PCI_INTR_MSIX |
444 __SHIFTIN(msino, ARM_PCI_INTR_IRQ) |
445 __SHIFTIN(msix_vec, ARM_PCI_INTR_MSI_VEC) |
446 __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
447
448 apple_pcie_msi_msix_enable(sc, msino, msix_vec, bst, bsh);
449 }
450
451 bus_space_unmap(bst, bsh, bsz);
452
453 return vectors;
454 }
455
456 static void *
457 apple_pcie_msi_intr_establish(struct arm_pci_msi *msi,
458 pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg, const char *xname)
459 {
460 struct apple_pcie_softc * const sc = msi->msi_priv;
461
462 const int msino = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
463 const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? FDT_INTR_MPSAFE : 0;
464
465 KASSERT(sc->sc_msi_ih[msino] == NULL);
466 sc->sc_msi_ih[msino] = intr_establish_xname(sc->sc_msi_start + msino,
467 ipl, IST_LEVEL | (mpsafe ? IST_MPSAFE : 0), func, arg, xname);
468
469 return sc->sc_msi_ih[msino];
470 }
471
472 static void
473 apple_pcie_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
474 int count)
475 {
476 struct apple_pcie_softc * const sc = msi->msi_priv;
477 int n;
478
479 for (n = 0; n < count; n++) {
480 const int msino = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
481 if (pih[n] & ARM_PCI_INTR_MSIX)
482 apple_pcie_msi_msix_disable(sc, msino);
483 if (pih[n] & ARM_PCI_INTR_MSI)
484 apple_pcie_msi_msi_disable(sc, msino);
485 apple_pcie_msi_free_msi(sc, msino);
486 if (sc->sc_msi_ih[msino] != NULL) {
487 intr_disestablish(sc->sc_msi_ih[msino]);
488 sc->sc_msi_ih[msino] = NULL;
489 }
490 }
491 }
492
493 static int
494 apple_pcie_msi_init(struct apple_pcie_softc *sc)
495 {
496 struct arm_pci_msi *msi = &sc->sc_msi;
497 const int phandle = sc->sc_pcihost.sc_phandle;
498 u_int portno;
499 int len;
500
501 const u_int *data = fdtbus_get_prop(phandle, "msi-ranges", &len);
502 if (len != 8) {
503 aprint_error_dev(sc->sc_pcihost.sc_dev,
504 "WARNING: bad msi-ranges property, MSI not enabled!\n");
505 return ENXIO;
506 }
507 sc->sc_msi_start = be32toh(data[0]);
508 sc->sc_nmsi = be32toh(data[1]);
509 sc->sc_msi_pa = kmem_zalloc(sizeof(*sc->sc_msi_pa) * sc->sc_nmsi,
510 KM_SLEEP);
511 sc->sc_msi_ih = kmem_zalloc(sizeof(*sc->sc_msi_ih) * sc->sc_nmsi,
512 KM_SLEEP);
513
514 if (of_getprop_uint64(phandle, "msi-doorbell", &sc->sc_msi_addr)) {
515 sc->sc_msi_addr = 0xffff000ULL;
516 }
517
518 for (portno = 0; portno < 3; portno++) {
519 apple_pcie_setup_port(sc, portno);
520 }
521
522 msi->msi_dev = sc->sc_pcihost.sc_dev;
523 msi->msi_priv = sc;
524 msi->msi_alloc = apple_pcie_msi_msi_alloc;
525 msi->msix_alloc = apple_pcie_msi_msix_alloc;
526 msi->msi_intr_establish = apple_pcie_msi_intr_establish;
527 msi->msi_intr_release = apple_pcie_msi_intr_release;
528
529 return arm_pci_msi_add(msi);
530 }
531