pcihost_fdt.c revision 1.7 1 /* $NetBSD: pcihost_fdt.c,v 1.7 2019/02/28 00:17:13 jakllsch Exp $ */
2
3 /*-
4 * Copyright (c) 2018 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 <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pcihost_fdt.c,v 1.7 2019/02/28 00:17:13 jakllsch Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/intr.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/extent.h>
39 #include <sys/queue.h>
40 #include <sys/mutex.h>
41 #include <sys/kmem.h>
42
43 #include <machine/cpu.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 <dev/fdt/fdtvar.h>
52
53 #include <arm/pci/pci_msi_machdep.h>
54
55 #define IH_INDEX_MASK 0x0000ffff
56 #define IH_MPSAFE 0x80000000
57
58 #define PCIHOST_DEFAULT_BUS_MIN 0
59 #define PCIHOST_DEFAULT_BUS_MAX 255
60
61 #define PCIHOST_CACHELINE_SIZE arm_dcache_align
62
63 /* Physical address format bit definitions */
64 #define PHYS_HI_RELO __BIT(31)
65 #define PHYS_HI_PREFETCH __BIT(30)
66 #define PHYS_HI_ALIASED __BIT(29)
67 #define PHYS_HI_SPACE __BITS(25,24)
68 #define PHYS_HI_SPACE_CFG 0
69 #define PHYS_HI_SPACE_IO 1
70 #define PHYS_HI_SPACE_MEM32 2
71 #define PHYS_HI_SPACE_MEM64 3
72 #define PHYS_HI_BUS __BITS(23,16)
73 #define PHYS_HI_DEVICE __BITS(15,11)
74 #define PHYS_HI_FUNCTION __BITS(10,8)
75 #define PHYS_HI_REGISTER __BITS(7,0)
76
77 static int pcihost_segment = 0;
78
79 enum pcihost_type {
80 PCIHOST_CAM = 1,
81 PCIHOST_ECAM,
82 };
83
84 struct pcih_bus_space {
85 struct bus_space bst;
86
87 int (*map)(void *, bus_addr_t, bus_size_t,
88 int, bus_space_handle_t *);
89 struct space_range {
90 bus_addr_t bpci;
91 bus_addr_t bbus;
92 bus_size_t size;
93 } ranges[4];
94 size_t nranges;
95 };
96
97 struct pcihost_softc {
98 device_t sc_dev;
99 bus_dma_tag_t sc_dmat;
100 bus_space_tag_t sc_bst;
101 bus_space_handle_t sc_bsh;
102 int sc_phandle;
103
104 enum pcihost_type sc_type;
105
106 u_int sc_seg;
107 u_int sc_bus_min;
108 u_int sc_bus_max;
109
110 struct arm32_pci_chipset sc_pc;
111
112 struct pcih_bus_space sc_io;
113 struct pcih_bus_space sc_mem;
114 };
115
116 static int pcihost_match(device_t, cfdata_t, void *);
117 static void pcihost_attach(device_t, device_t, void *);
118
119 static void pcihost_init(pci_chipset_tag_t, void *);
120 static int pcihost_config(struct pcihost_softc *);
121
122 static void pcihost_attach_hook(device_t, device_t,
123 struct pcibus_attach_args *);
124 static int pcihost_bus_maxdevs(void *, int);
125 static pcitag_t pcihost_make_tag(void *, int, int, int);
126 static void pcihost_decompose_tag(void *, pcitag_t, int *, int *, int *);
127 static u_int pcihost_get_segment(void *);
128 static pcireg_t pcihost_conf_read(void *, pcitag_t, int);
129 static void pcihost_conf_write(void *, pcitag_t, int, pcireg_t);
130 static int pcihost_conf_hook(void *, int, int, int, pcireg_t);
131 static void pcihost_conf_interrupt(void *, int, int, int, int, int *);
132
133 static int pcihost_intr_map(const struct pci_attach_args *,
134 pci_intr_handle_t *);
135 static const char *pcihost_intr_string(void *, pci_intr_handle_t,
136 char *, size_t);
137 static const struct evcnt *pcihost_intr_evcnt(void *, pci_intr_handle_t);
138 static int pcihost_intr_setattr(void *, pci_intr_handle_t *, int,
139 uint64_t);
140 static void * pcihost_intr_establish(void *, pci_intr_handle_t,
141 int, int (*)(void *), void *,
142 const char *);
143 static void pcihost_intr_disestablish(void *, void *);
144
145 static int pcihost_bus_space_map(void *, bus_addr_t, bus_size_t,
146 int, bus_space_handle_t *);
147
148 CFATTACH_DECL_NEW(pcihost_fdt, sizeof(struct pcihost_softc),
149 pcihost_match, pcihost_attach, NULL, NULL);
150
151 static const struct of_compat_data compat_data[] = {
152 { "pci-host-cam-generic", PCIHOST_CAM },
153 { "pci-host-ecam-generic", PCIHOST_ECAM },
154 { NULL, 0 }
155 };
156
157 static int
158 pcihost_match(device_t parent, cfdata_t cf, void *aux)
159 {
160 struct fdt_attach_args * const faa = aux;
161
162 return of_match_compat_data(faa->faa_phandle, compat_data);
163 }
164
165 static void
166 pcihost_attach(device_t parent, device_t self, void *aux)
167 {
168 struct pcihost_softc * const sc = device_private(self);
169 struct fdt_attach_args * const faa = aux;
170 struct pcibus_attach_args pba;
171 bus_addr_t cs_addr;
172 bus_size_t cs_size;
173 const u_int *data;
174 int error, len;
175
176 if (fdtbus_get_reg(faa->faa_phandle, 0, &cs_addr, &cs_size) != 0) {
177 aprint_error(": couldn't get registers\n");
178 return;
179 }
180
181 sc->sc_dev = self;
182 sc->sc_dmat = faa->faa_dmat;
183 sc->sc_bst = faa->faa_bst;
184 sc->sc_phandle = faa->faa_phandle;
185 error = bus_space_map(sc->sc_bst, cs_addr, cs_size, 0, &sc->sc_bsh);
186 if (error) {
187 aprint_error(": couldn't map registers: %d\n", error);
188 return;
189 }
190 sc->sc_type = of_search_compatible(sc->sc_phandle, compat_data)->data;
191
192 aprint_naive("\n");
193 aprint_normal(": Generic PCI host controller\n");
194
195 if ((data = fdtbus_get_prop(sc->sc_phandle, "bus-range", &len)) != NULL) {
196 if (len != 8) {
197 aprint_error_dev(self, "malformed 'bus-range' property\n");
198 return;
199 }
200 sc->sc_bus_min = be32toh(data[0]);
201 sc->sc_bus_max = be32toh(data[1]);
202 } else {
203 sc->sc_bus_min = PCIHOST_DEFAULT_BUS_MIN;
204 sc->sc_bus_max = PCIHOST_DEFAULT_BUS_MAX;
205 }
206
207 /*
208 * Assign a fixed PCI segment ("domain") number. If the property is not
209 * present, assign one. The binding spec says if this property is used to
210 * assign static segment numbers, all host bridges should have segments
211 * astatic assigned to prevent overlaps.
212 */
213 if (of_getprop_uint32(sc->sc_phandle, "linux,pci-domain", &sc->sc_seg))
214 sc->sc_seg = pcihost_segment++;
215
216 pcihost_init(&sc->sc_pc, sc);
217
218 if (pcihost_config(sc) != 0)
219 return;
220
221 memset(&pba, 0, sizeof(pba));
222 pba.pba_flags = PCI_FLAGS_MRL_OKAY |
223 PCI_FLAGS_MRM_OKAY |
224 PCI_FLAGS_MWI_OKAY |
225 PCI_FLAGS_IO_OKAY |
226 PCI_FLAGS_MEM_OKAY;
227 #ifdef __HAVE_PCI_MSI_MSIX
228 if (sc->sc_type == PCIHOST_ECAM) {
229 pba.pba_flags |= PCI_FLAGS_MSI_OKAY |
230 PCI_FLAGS_MSIX_OKAY;
231 }
232 #endif
233 pba.pba_iot = &sc->sc_io.bst;
234 pba.pba_memt = &sc->sc_mem.bst;
235 pba.pba_dmat = sc->sc_dmat;
236 #ifdef _PCI_HAVE_DMA64
237 pba.pba_dmat64 = sc->sc_dmat;
238 #endif
239 pba.pba_pc = &sc->sc_pc;
240 pba.pba_bus = sc->sc_bus_min;
241
242 config_found_ia(self, "pcibus", &pba, pcibusprint);
243 }
244
245 static void
246 pcihost_init(pci_chipset_tag_t pc, void *priv)
247 {
248 pc->pc_conf_v = priv;
249 pc->pc_attach_hook = pcihost_attach_hook;
250 pc->pc_bus_maxdevs = pcihost_bus_maxdevs;
251 pc->pc_make_tag = pcihost_make_tag;
252 pc->pc_decompose_tag = pcihost_decompose_tag;
253 pc->pc_get_segment = pcihost_get_segment;
254 pc->pc_conf_read = pcihost_conf_read;
255 pc->pc_conf_write = pcihost_conf_write;
256 pc->pc_conf_hook = pcihost_conf_hook;
257 pc->pc_conf_interrupt = pcihost_conf_interrupt;
258
259 pc->pc_intr_v = priv;
260 pc->pc_intr_map = pcihost_intr_map;
261 pc->pc_intr_string = pcihost_intr_string;
262 pc->pc_intr_evcnt = pcihost_intr_evcnt;
263 pc->pc_intr_setattr = pcihost_intr_setattr;
264 pc->pc_intr_establish = pcihost_intr_establish;
265 pc->pc_intr_disestablish = pcihost_intr_disestablish;
266 }
267
268 static int
269 pcihost_config(struct pcihost_softc *sc)
270 {
271 struct extent *ioext = NULL, *memext = NULL, *pmemext = NULL;
272 const u_int *ranges;
273 u_int probe_only;
274 int error, len;
275
276 struct pcih_bus_space * const pibs = &sc->sc_io;
277 pibs->bst = *sc->sc_bst;
278 pibs->bst.bs_cookie = pibs;
279 pibs->map = pibs->bst.bs_map;
280 pibs->bst.bs_map = pcihost_bus_space_map;
281
282 struct pcih_bus_space * const pmbs = &sc->sc_mem;
283 pmbs->bst = *sc->sc_bst;
284 pmbs->bst.bs_cookie = pmbs;
285 pmbs->map = pmbs->bst.bs_map;
286 pmbs->bst.bs_map = pcihost_bus_space_map;
287
288 /*
289 * If this flag is set, skip configuration of the PCI bus and use existing config.
290 */
291 if (of_getprop_uint32(sc->sc_phandle, "linux,pci-probe-only", &probe_only))
292 probe_only = 0;
293 if (probe_only)
294 return 0;
295
296 ranges = fdtbus_get_prop(sc->sc_phandle, "ranges", &len);
297 if (ranges == NULL) {
298 aprint_error_dev(sc->sc_dev, "missing 'ranges' property\n");
299 return EINVAL;
300 }
301
302 /*
303 * Each entry in the ranges table contains:
304 * - bus address (3 cells)
305 * - cpu physical address (2 cells)
306 * - size (2 cells)
307 * Total size for each entry is 28 bytes (7 cells).
308 */
309 while (len >= 28) {
310 const uint32_t phys_hi = be32dec(&ranges[0]);
311 const uint64_t bus_phys = be64dec(&ranges[1]);
312 const uint64_t cpu_phys = be64dec(&ranges[3]);
313 const uint64_t size = be64dec(&ranges[5]);
314
315 len -= 28;
316 ranges += 7;
317
318 const bool is64 = (__SHIFTOUT(phys_hi, PHYS_HI_SPACE) ==
319 PHYS_HI_SPACE_MEM64) ? true : false;
320 switch (__SHIFTOUT(phys_hi, PHYS_HI_SPACE)) {
321 case PHYS_HI_SPACE_IO:
322 if (pibs->nranges + 1 >= __arraycount(pibs->ranges)) {
323 aprint_error_dev(sc->sc_dev, "too many IO ranges\n");
324 continue;
325 }
326 pibs->ranges[pibs->nranges].bpci = bus_phys;
327 pibs->ranges[pibs->nranges].bbus = cpu_phys;
328 pibs->ranges[pibs->nranges].size = size;
329 ++pibs->nranges;
330 if (ioext != NULL) {
331 aprint_error_dev(sc->sc_dev, "ignoring duplicate IO space range\n");
332 continue;
333 }
334 ioext = extent_create("pciio", bus_phys, bus_phys + size - 1, NULL, 0, EX_NOWAIT);
335 aprint_verbose_dev(sc->sc_dev,
336 "IO: 0x%" PRIx64 "+0x%" PRIx64 "@0x%" PRIx64 "\n",
337 bus_phys, size, cpu_phys);
338 /* reserve a PC-like legacy IO ports range, perhaps for access to VGA registers */
339 if (bus_phys == 0 && size >= 0x10000)
340 extent_alloc_region(ioext, 0, 0x1000, EX_WAITOK);
341 break;
342 case PHYS_HI_SPACE_MEM64:
343 /* FALLTHROUGH */
344 case PHYS_HI_SPACE_MEM32:
345 if (pmbs->nranges + 1 >= __arraycount(pmbs->ranges)) {
346 aprint_error_dev(sc->sc_dev, "too many mem ranges\n");
347 continue;
348 }
349 /* both pmem and mem spaces are in the same tag */
350 pmbs->ranges[pmbs->nranges].bpci = bus_phys;
351 pmbs->ranges[pmbs->nranges].bbus = cpu_phys;
352 pmbs->ranges[pmbs->nranges].size = size;
353 ++pmbs->nranges;
354 if ((phys_hi & PHYS_HI_PREFETCH) != 0 ||
355 __SHIFTOUT(phys_hi, PHYS_HI_SPACE) == PHYS_HI_SPACE_MEM64) {
356 if (pmemext != NULL) {
357 aprint_error_dev(sc->sc_dev, "ignoring duplicate mem (prefetchable) range\n");
358 continue;
359 }
360 pmemext = extent_create("pcipmem", bus_phys, bus_phys + size - 1, NULL, 0, EX_NOWAIT);
361 aprint_verbose_dev(sc->sc_dev,
362 "MMIO (%d-bit prefetchable): 0x%" PRIx64 "+0x%" PRIx64 "@0x%" PRIx64 "\n",
363 is64 ? 64 : 32, bus_phys, size, cpu_phys);
364 } else {
365 if (memext != NULL) {
366 aprint_error_dev(sc->sc_dev, "ignoring duplicate mem (non-prefetchable) range\n");
367 continue;
368 }
369 memext = extent_create("pcimem", bus_phys, bus_phys + size - 1, NULL, 0, EX_NOWAIT);
370 aprint_verbose_dev(sc->sc_dev,
371 "MMIO (%d-bit non-prefetchable): 0x%" PRIx64 "+0x%" PRIx64 "@0x%" PRIx64 "\n",
372 is64 ? 64 : 32, bus_phys, size, cpu_phys);
373 }
374 break;
375 default:
376 break;
377 }
378 }
379
380 error = pci_configure_bus(&sc->sc_pc, ioext, memext, pmemext, sc->sc_bus_min, PCIHOST_CACHELINE_SIZE);
381
382 if (ioext)
383 extent_destroy(ioext);
384 if (memext)
385 extent_destroy(memext);
386 if (pmemext)
387 extent_destroy(pmemext);
388
389 if (error) {
390 aprint_error_dev(sc->sc_dev, "configuration failed: %d\n", error);
391 return error;
392 }
393
394 return 0;
395 }
396
397 static void
398 pcihost_attach_hook(device_t parent, device_t self,
399 struct pcibus_attach_args *pba)
400 {
401 }
402
403 static int
404 pcihost_bus_maxdevs(void *v, int busno)
405 {
406 return 32;
407 }
408
409 static pcitag_t
410 pcihost_make_tag(void *v, int b, int d, int f)
411 {
412 return (b << 16) | (d << 11) | (f << 8);
413 }
414
415 static void
416 pcihost_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
417 {
418 if (bp)
419 *bp = (tag >> 16) & 0xff;
420 if (dp)
421 *dp = (tag >> 11) & 0x1f;
422 if (fp)
423 *fp = (tag >> 8) & 0x7;
424 }
425
426 static u_int
427 pcihost_get_segment(void *v)
428 {
429 struct pcihost_softc *sc = v;
430
431 return sc->sc_seg;
432 }
433
434 static pcireg_t
435 pcihost_conf_read(void *v, pcitag_t tag, int offset)
436 {
437 struct pcihost_softc *sc = v;
438 int b, d, f;
439 u_int reg;
440
441 pcihost_decompose_tag(v, tag, &b, &d, &f);
442
443 if (b < sc->sc_bus_min || b > sc->sc_bus_max)
444 return (pcireg_t) -1;
445
446 if (sc->sc_type == PCIHOST_CAM) {
447 if (offset & ~0xff)
448 return (pcireg_t) -1;
449 reg = (b << 16) | (d << 11) | (f << 8) | offset;
450 } else if (sc->sc_type == PCIHOST_ECAM) {
451 if (offset & ~0xfff)
452 return (pcireg_t) -1;
453 reg = (b << 20) | (d << 15) | (f << 12) | offset;
454 } else {
455 return (pcireg_t) -1;
456 }
457
458 return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg);
459 }
460
461 static void
462 pcihost_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
463 {
464 struct pcihost_softc *sc = v;
465 int b, d, f;
466 u_int reg;
467
468 pcihost_decompose_tag(v, tag, &b, &d, &f);
469
470 if (b < sc->sc_bus_min || b > sc->sc_bus_max)
471 return;
472
473 if (sc->sc_type == PCIHOST_CAM) {
474 if (offset & ~0xff)
475 return;
476 reg = (b << 16) | (d << 11) | (f << 8) | offset;
477 } else if (sc->sc_type == PCIHOST_ECAM) {
478 if (offset & ~0xfff)
479 return;
480 reg = (b << 20) | (d << 15) | (f << 12) | offset;
481 } else {
482 return;
483 }
484
485 bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val);
486 }
487
488 static int
489 pcihost_conf_hook(void *v, int b, int d, int f, pcireg_t id)
490 {
491 return PCI_CONF_DEFAULT;
492 }
493
494 static void
495 pcihost_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *ilinep)
496 {
497 }
498
499 static int
500 pcihost_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
501 {
502 struct pcihost_softc *sc = pa->pa_pc->pc_intr_v;
503 u_int addr_cells, interrupt_cells;
504 const u_int *imap, *imask;
505 int imaplen, imasklen;
506 u_int match[4];
507 int index;
508
509 if (pa->pa_intrpin == 0)
510 return EINVAL;
511
512 imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
513 imask = fdtbus_get_prop(sc->sc_phandle, "interrupt-map-mask", &imasklen);
514 if (imap == NULL || imask == NULL || imasklen != 16)
515 return EINVAL;
516
517 /* Convert attach args to specifier */
518 match[0] = htobe32(
519 __SHIFTIN(pa->pa_bus, PHYS_HI_BUS) |
520 __SHIFTIN(pa->pa_device, PHYS_HI_DEVICE) |
521 __SHIFTIN(pa->pa_function, PHYS_HI_FUNCTION)
522 ) & imask[0];
523 match[1] = htobe32(0) & imask[1];
524 match[2] = htobe32(0) & imask[2];
525 match[3] = htobe32(pa->pa_intrpin) & imask[3];
526
527 index = 0;
528 while (imaplen >= 20) {
529 const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
530 if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
531 addr_cells = 2;
532 if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
533 interrupt_cells = 0;
534 if (imaplen < (addr_cells + interrupt_cells) * 4)
535 return ENXIO;
536
537 if ((imap[0] & imask[0]) == match[0] &&
538 (imap[1] & imask[1]) == match[1] &&
539 (imap[2] & imask[2]) == match[2] &&
540 (imap[3] & imask[3]) == match[3]) {
541 *ih = index;
542 return 0;
543 }
544
545 imap += (5 + addr_cells + interrupt_cells);
546 imaplen -= (5 + addr_cells + interrupt_cells) * 4;
547 index++;
548 }
549
550 return EINVAL;
551 }
552
553 static const u_int *
554 pcihost_find_intr(struct pcihost_softc *sc, pci_intr_handle_t ih, int *pihandle)
555 {
556 u_int addr_cells, interrupt_cells;
557 int imaplen, index;
558 const u_int *imap;
559
560 imap = fdtbus_get_prop(sc->sc_phandle, "interrupt-map", &imaplen);
561 KASSERT(imap != NULL);
562
563 index = 0;
564 while (imaplen >= 20) {
565 const int map_ihandle = fdtbus_get_phandle_from_native(be32toh(imap[4]));
566 if (of_getprop_uint32(map_ihandle, "#address-cells", &addr_cells))
567 addr_cells = 2;
568 if (of_getprop_uint32(map_ihandle, "#interrupt-cells", &interrupt_cells))
569 interrupt_cells = 0;
570 if (imaplen < (addr_cells + interrupt_cells) * 4)
571 return NULL;
572
573 if (index == ih) {
574 *pihandle = map_ihandle;
575 return imap + 5 + addr_cells;
576 }
577
578 imap += (5 + addr_cells + interrupt_cells);
579 imaplen -= (5 + addr_cells + interrupt_cells) * 4;
580 index++;
581 }
582
583 return NULL;
584 }
585
586 static const char *
587 pcihost_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
588 {
589 const int irq = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
590 const int vec = __SHIFTOUT(ih, ARM_PCI_INTR_MSI_VEC);
591 struct pcihost_softc *sc = v;
592 const u_int *specifier;
593 int ihandle;
594
595 if (ih & ARM_PCI_INTR_MSIX) {
596 snprintf(buf, len, "irq %d (MSI-X vec %d)", irq, vec);
597 } else if (ih & ARM_PCI_INTR_MSI) {
598 snprintf(buf, len, "irq %d (MSI vec %d)", irq, vec);
599 } else {
600 specifier = pcihost_find_intr(sc, ih & IH_INDEX_MASK, &ihandle);
601 if (specifier == NULL)
602 return NULL;
603
604 if (!fdtbus_intr_str_raw(ihandle, specifier, buf, len))
605 return NULL;
606 }
607
608 return buf;
609 }
610
611 const struct evcnt *
612 pcihost_intr_evcnt(void *v, pci_intr_handle_t ih)
613 {
614 return NULL;
615 }
616
617 static int
618 pcihost_intr_setattr(void *v, pci_intr_handle_t *ih, int attr, uint64_t data)
619 {
620 switch (attr) {
621 case PCI_INTR_MPSAFE:
622 if (data)
623 *ih |= IH_MPSAFE;
624 else
625 *ih &= ~IH_MPSAFE;
626 return 0;
627 default:
628 return ENODEV;
629 }
630 }
631
632 static void *
633 pcihost_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
634 int (*callback)(void *), void *arg, const char *xname)
635 {
636 struct pcihost_softc *sc = v;
637 const int flags = (ih & IH_MPSAFE) ? FDT_INTR_MPSAFE : 0;
638 const u_int *specifier;
639 int ihandle;
640
641 if ((ih & (ARM_PCI_INTR_MSI | ARM_PCI_INTR_MSIX)) != 0)
642 return arm_pci_msi_intr_establish(&sc->sc_pc, ih, ipl, callback, arg, xname);
643
644 specifier = pcihost_find_intr(sc, ih & IH_INDEX_MASK, &ihandle);
645 if (specifier == NULL)
646 return NULL;
647
648 return fdtbus_intr_establish_raw(ihandle, specifier, ipl, flags, callback, arg);
649 }
650
651 static void
652 pcihost_intr_disestablish(void *v, void *vih)
653 {
654 struct pcihost_softc *sc = v;
655
656 fdtbus_intr_disestablish(sc->sc_phandle, vih);
657 }
658
659 static int
660 pcihost_bus_space_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
661 bus_space_handle_t *bshp)
662 {
663 struct pcih_bus_space * const pbs = t;
664
665 for (size_t i = 0; i < pbs->nranges; i++) {
666 const bus_addr_t rmin = pbs->ranges[i].bpci;
667 const bus_addr_t rmax = pbs->ranges[i].bpci - 1 + pbs->ranges[i].size;
668 if ((bpa >= rmin) && ((bpa - 1 + size) <= rmax)) {
669 return pbs->map(t, bpa - pbs->ranges[i].bpci + pbs->ranges[i].bbus, size, flag, bshp);
670 }
671 }
672
673 return ERANGE;
674 }
675