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