acpipci.c revision 1.15
1/*	$OpenBSD: acpipci.c,v 1.15 2020/06/11 18:13:53 kettenis Exp $	*/
2/*
3 * Copyright (c) 2018 Mark Kettenis
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/param.h>
19#include <sys/device.h>
20#include <sys/extent.h>
21#include <sys/malloc.h>
22#include <sys/systm.h>
23
24#include <machine/bus.h>
25
26#include <dev/acpi/acpireg.h>
27#include <dev/acpi/acpivar.h>
28#include <dev/acpi/acpidev.h>
29#include <dev/acpi/amltypes.h>
30#include <dev/acpi/dsdt.h>
31
32#include <dev/pci/pcidevs.h>
33#include <dev/pci/pcireg.h>
34#include <dev/pci/pcivar.h>
35#include <dev/pci/ppbreg.h>
36
37struct acpipci_mcfg {
38	SLIST_ENTRY(acpipci_mcfg) am_list;
39
40	uint16_t	am_segment;
41	uint8_t		am_min_bus;
42	uint8_t		am_max_bus;
43
44	bus_space_tag_t	am_iot;
45	bus_space_handle_t am_ioh;
46
47	struct arm64_pci_chipset am_pc;
48};
49
50struct acpipci_trans {
51	struct acpipci_trans *at_next;
52	bus_space_tag_t	at_iot;
53	bus_addr_t	at_base;
54	bus_size_t	at_size;
55	bus_size_t	at_offset;
56};
57
58struct acpipci_softc {
59	struct device	sc_dev;
60	struct acpi_softc *sc_acpi;
61	struct aml_node *sc_node;
62	bus_space_tag_t	sc_iot;
63	pci_chipset_tag_t sc_pc;
64
65	struct bus_space sc_bus_iot;
66	struct bus_space sc_bus_memt;
67	struct acpipci_trans *sc_io_trans;
68	struct acpipci_trans *sc_mem_trans;
69
70	struct extent	*sc_busex;
71	struct extent	*sc_memex;
72	struct extent	*sc_ioex;
73	char		sc_busex_name[32];
74	char		sc_ioex_name[32];
75	char		sc_memex_name[32];
76	int		sc_bus;
77	uint32_t	sc_seg;
78};
79
80int	acpipci_match(struct device *, void *, void *);
81void	acpipci_attach(struct device *, struct device *, void *);
82
83struct cfattach acpipci_ca = {
84	sizeof(struct acpipci_softc), acpipci_match, acpipci_attach
85};
86
87struct cfdriver acpipci_cd = {
88	NULL, "acpipci", DV_DULL
89};
90
91const char *acpipci_hids[] = {
92	"PNP0A08",
93	NULL
94};
95
96int	acpipci_parse_resources(int, union acpi_resource *, void *);
97int	acpipci_bs_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
98	    bus_space_handle_t *);
99
100void	acpipci_attach_hook(struct device *, struct device *,
101	    struct pcibus_attach_args *);
102int	acpipci_bus_maxdevs(void *, int);
103pcitag_t acpipci_make_tag(void *, int, int, int);
104void	acpipci_decompose_tag(void *, pcitag_t, int *, int *, int *);
105int	acpipci_conf_size(void *, pcitag_t);
106pcireg_t acpipci_conf_read(void *, pcitag_t, int);
107void	acpipci_conf_write(void *, pcitag_t, int, pcireg_t);
108
109int	acpipci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
110const char *acpipci_intr_string(void *, pci_intr_handle_t);
111void	*acpipci_intr_establish(void *, pci_intr_handle_t, int,
112	    int (*)(void *), void *, char *);
113void	acpipci_intr_disestablish(void *, void *);
114
115uint32_t acpipci_iort_map_msi(pci_chipset_tag_t, pcitag_t);
116
117int
118acpipci_match(struct device *parent, void *match, void *aux)
119{
120	struct acpi_attach_args *aaa = aux;
121	struct cfdata *cf = match;
122
123	return acpi_matchhids(aaa, acpipci_hids, cf->cf_driver->cd_name);
124}
125
126void
127acpipci_attach(struct device *parent, struct device *self, void *aux)
128{
129	struct acpi_attach_args *aaa = aux;
130	struct acpipci_softc *sc = (struct acpipci_softc *)self;
131	struct pcibus_attach_args pba;
132	struct aml_value res;
133	uint64_t bbn = 0;
134	uint64_t seg = 0;
135
136	sc->sc_acpi = (struct acpi_softc *)parent;
137	sc->sc_node = aaa->aaa_node;
138	printf(" %s", sc->sc_node->name);
139
140	if (aml_evalname(sc->sc_acpi, sc->sc_node, "_CRS", 0, NULL, &res)) {
141		printf(": can't find resources\n");
142		return;
143	}
144
145	aml_evalinteger(sc->sc_acpi, sc->sc_node, "_BBN", 0, NULL, &bbn);
146	sc->sc_bus = bbn;
147
148	aml_evalinteger(sc->sc_acpi, sc->sc_node, "_SEG", 0, NULL, &seg);
149	sc->sc_seg = seg;
150
151	sc->sc_iot = aaa->aaa_memt;
152
153	printf("\n");
154
155	/* Create extents for our address spaces. */
156	snprintf(sc->sc_busex_name, sizeof(sc->sc_busex_name),
157	    "%s pcibus", sc->sc_dev.dv_xname);
158	snprintf(sc->sc_ioex_name, sizeof(sc->sc_ioex_name),
159	    "%s pciio", sc->sc_dev.dv_xname);
160	snprintf(sc->sc_memex_name, sizeof(sc->sc_memex_name),
161	    "%s pcimem", sc->sc_dev.dv_xname);
162	sc->sc_busex = extent_create(sc->sc_busex_name, 0, 255,
163	    M_DEVBUF, NULL, 0, EX_WAITOK | EX_FILLED);
164	sc->sc_ioex = extent_create(sc->sc_ioex_name, 0, 0xffffffff,
165	    M_DEVBUF, NULL, 0, EX_WAITOK | EX_FILLED);
166	sc->sc_memex = extent_create(sc->sc_memex_name, 0, (u_long)-1,
167	    M_DEVBUF, NULL, 0, EX_WAITOK | EX_FILLED);
168
169	aml_parse_resource(&res, acpipci_parse_resources, sc);
170
171	memcpy(&sc->sc_bus_iot, sc->sc_iot, sizeof(sc->sc_bus_iot));
172	sc->sc_bus_iot.bus_private = sc->sc_io_trans;
173	sc->sc_bus_iot._space_map = acpipci_bs_map;
174	memcpy(&sc->sc_bus_memt, sc->sc_iot, sizeof(sc->sc_bus_memt));
175	sc->sc_bus_memt.bus_private = sc->sc_mem_trans;
176	sc->sc_bus_memt._space_map = acpipci_bs_map;
177
178	sc->sc_pc = pci_lookup_segment(seg);
179	KASSERT(sc->sc_pc->pc_intr_v == NULL);
180
181	sc->sc_pc->pc_intr_v = sc;
182	sc->sc_pc->pc_intr_map = acpipci_intr_map;
183	sc->sc_pc->pc_intr_map_msi = _pci_intr_map_msi;
184	sc->sc_pc->pc_intr_map_msix = _pci_intr_map_msix;
185	sc->sc_pc->pc_intr_string = acpipci_intr_string;
186	sc->sc_pc->pc_intr_establish = acpipci_intr_establish;
187	sc->sc_pc->pc_intr_disestablish = acpipci_intr_disestablish;
188
189	memset(&pba, 0, sizeof(pba));
190	pba.pba_busname = "pci";
191	pba.pba_iot = &sc->sc_bus_iot;
192	pba.pba_memt = &sc->sc_bus_memt;
193	pba.pba_dmat = aaa->aaa_dmat;
194	pba.pba_pc = sc->sc_pc;
195	pba.pba_busex = sc->sc_busex;
196	pba.pba_ioex = sc->sc_ioex;
197	pba.pba_memex = sc->sc_memex;
198	pba.pba_pmemex = sc->sc_memex;
199	pba.pba_domain = pci_ndomains++;
200	pba.pba_bus = sc->sc_bus;
201	pba.pba_flags |= PCI_FLAGS_MSI_ENABLED;
202
203	config_found(self, &pba, NULL);
204}
205
206int
207acpipci_parse_resources(int crsidx, union acpi_resource *crs, void *arg)
208{
209	struct acpipci_softc *sc = arg;
210	struct acpipci_trans *at;
211	int type = AML_CRSTYPE(crs);
212	int restype, tflags;
213	u_long min, len = 0, tra;
214
215	switch (type) {
216	case LR_WORD:
217		restype = crs->lr_word.type;
218		tflags = crs->lr_word.tflags;
219		min = crs->lr_word._min;
220		len = crs->lr_word._len;
221		tra = crs->lr_word._tra;
222		break;
223	case LR_DWORD:
224		restype = crs->lr_dword.type;
225		tflags = crs->lr_dword.tflags;
226		min = crs->lr_dword._min;
227		len = crs->lr_dword._len;
228		tra = crs->lr_dword._tra;
229		break;
230	case LR_QWORD:
231		restype = crs->lr_qword.type;
232		tflags = crs->lr_qword.tflags;
233		min = crs->lr_qword._min;
234		len = crs->lr_qword._len;
235		tra = crs->lr_qword._tra;
236		break;
237	}
238
239	if (len == 0)
240		return 0;
241
242	switch (restype) {
243	case LR_TYPE_MEMORY:
244		if (tflags & LR_MEMORY_TTP)
245			return 0;
246		extent_free(sc->sc_memex, min, len, EX_WAITOK);
247		at = malloc(sizeof(struct acpipci_trans), M_DEVBUF, M_WAITOK);
248		at->at_iot = sc->sc_iot;
249		at->at_base = min;
250		at->at_size = len;
251		at->at_offset = tra;
252		at->at_next = sc->sc_mem_trans;
253		sc->sc_mem_trans = at;
254		break;
255	case LR_TYPE_IO:
256		/*
257		 * Don't check _TTP as various firmwares don't set it,
258		 * even though they should!!
259		 */
260		extent_free(sc->sc_ioex, min, len, EX_WAITOK);
261		at = malloc(sizeof(struct acpipci_trans), M_DEVBUF, M_WAITOK);
262		at->at_iot = sc->sc_iot;
263		at->at_base = min;
264		at->at_size = len;
265		at->at_offset = tra;
266		at->at_next = sc->sc_io_trans;
267		sc->sc_io_trans = at;
268		break;
269	case LR_TYPE_BUS:
270		extent_free(sc->sc_busex, min, len, EX_WAITOK);
271		/*
272		 * Let _CRS minimum bus number override _BBN.
273		 */
274		sc->sc_bus = min;
275		break;
276	}
277
278	return 0;
279}
280
281void
282acpipci_attach_hook(struct device *parent, struct device *self,
283    struct pcibus_attach_args *pba)
284{
285}
286
287int
288acpipci_bus_maxdevs(void *v, int bus)
289{
290	return 32;
291}
292
293pcitag_t
294acpipci_make_tag(void *v, int bus, int device, int function)
295{
296	return ((bus << 20) | (device << 15) | (function << 12));
297}
298
299void
300acpipci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
301{
302	if (bp != NULL)
303		*bp = (tag >> 20) & 0xff;
304	if (dp != NULL)
305		*dp = (tag >> 15) & 0x1f;
306	if (fp != NULL)
307		*fp = (tag >> 12) & 0x7;
308}
309
310int
311acpipci_conf_size(void *v, pcitag_t tag)
312{
313	return PCIE_CONFIG_SPACE_SIZE;
314}
315
316pcireg_t
317acpipci_conf_read(void *v, pcitag_t tag, int reg)
318{
319	struct acpipci_mcfg *am = v;
320
321	if (tag < (am->am_min_bus << 20) ||
322	    tag >= ((am->am_max_bus + 1) << 20))
323		return 0xffffffff;
324
325	return bus_space_read_4(am->am_iot, am->am_ioh, tag | reg);
326}
327
328void
329acpipci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
330{
331	struct acpipci_mcfg *am = v;
332
333	if (tag < (am->am_min_bus << 20) ||
334	    tag >= ((am->am_max_bus + 1) << 20))
335		return;
336
337	bus_space_write_4(am->am_iot, am->am_ioh, tag | reg, data);
338}
339
340int
341acpipci_intr_swizzle(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
342{
343	int dev, swizpin;
344
345	if (pa->pa_bridgeih == NULL)
346		return -1;
347
348	pci_decompose_tag(pa->pa_pc, pa->pa_tag, NULL, &dev, NULL);
349	swizpin = PPB_INTERRUPT_SWIZZLE(pa->pa_rawintrpin, dev);
350	if (pa->pa_bridgeih[swizpin - 1].ih_type == PCI_NONE)
351		return -1;
352
353	*ihp = pa->pa_bridgeih[swizpin - 1];
354	return 0;
355}
356
357int
358acpipci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
359{
360	struct acpipci_softc *sc = pa->pa_pc->pc_intr_v;
361	struct aml_node *node = sc->sc_node;
362	struct aml_value res;
363	uint64_t addr, pin, source, index;
364	int i;
365
366	/*
367	 * If we're behind a bridge, we need to look for a _PRT for
368	 * it.  If we don't find a _PRT, we need to swizzle.  If we're
369	 * not behind a bridge we need to look for a _PRT on the host
370	 * bridge node itself.
371	 */
372	if (pa->pa_bridgetag) {
373		node = acpi_find_pci(pa->pa_pc, *pa->pa_bridgetag);
374		if (node == NULL)
375			return acpipci_intr_swizzle(pa, ihp);
376	}
377
378	if (aml_evalname(sc->sc_acpi, node, "_PRT", 0, NULL, &res))
379		return acpipci_intr_swizzle(pa, ihp);
380
381	if (res.type != AML_OBJTYPE_PACKAGE)
382		return -1;
383
384	for (i = 0; i < res.length; i++) {
385		struct aml_value *val = res.v_package[i];
386
387		if (val->type != AML_OBJTYPE_PACKAGE)
388			continue;
389		if (val->length != 4)
390			continue;
391		if (val->v_package[0]->type != AML_OBJTYPE_INTEGER ||
392		    val->v_package[1]->type != AML_OBJTYPE_INTEGER ||
393		    val->v_package[2]->type != AML_OBJTYPE_INTEGER ||
394		    val->v_package[3]->type != AML_OBJTYPE_INTEGER)
395			continue;
396
397		addr = val->v_package[0]->v_integer;
398		pin = val->v_package[1]->v_integer;
399		source = val->v_package[2]->v_integer;
400		index = val->v_package[3]->v_integer;
401		if (ACPI_ADR_PCIDEV(addr) != pa->pa_device ||
402		    ACPI_ADR_PCIFUN(addr) != 0xffff ||
403		    pin != pa->pa_intrpin - 1 || source != 0)
404			continue;
405
406		ihp->ih_pc = pa->pa_pc;
407		ihp->ih_tag = pa->pa_tag;
408		ihp->ih_intrpin = index;
409		ihp->ih_type = PCI_INTX;
410
411		return 0;
412	}
413
414	return -1;
415}
416
417const char *
418acpipci_intr_string(void *v, pci_intr_handle_t ih)
419{
420	static char irqstr[32];
421
422	switch (ih.ih_type) {
423	case PCI_MSI:
424		return "msi";
425	case PCI_MSIX:
426		return "msix";
427	}
428
429	snprintf(irqstr, sizeof(irqstr), "irq %d", ih.ih_intrpin);
430	return irqstr;
431}
432
433void *
434acpipci_intr_establish(void *v, pci_intr_handle_t ih, int level,
435    int (*func)(void *), void *arg, char *name)
436{
437	struct acpipci_softc *sc = v;
438	struct interrupt_controller *ic;
439	void *cookie;
440
441	extern LIST_HEAD(, interrupt_controller) interrupt_controllers;
442	LIST_FOREACH(ic, &interrupt_controllers, ic_list) {
443		if (ic->ic_establish_msi)
444			break;
445	}
446	if (ic == NULL)
447		return NULL;
448
449	KASSERT(ih.ih_type != PCI_NONE);
450
451	if (ih.ih_type != PCI_INTX) {
452		uint64_t addr, data;
453
454		/* Map Requester ID through IORT to get sideband data. */
455		data = acpipci_iort_map_msi(ih.ih_pc, ih.ih_tag);
456		cookie = ic->ic_establish_msi(ic->ic_cookie, &addr,
457		    &data, level, func, arg, name);
458		if (cookie == NULL)
459			return NULL;
460
461		/* TODO: translate address to the PCI device's view */
462
463		if (ih.ih_type == PCI_MSIX) {
464			pci_msix_enable(ih.ih_pc, ih.ih_tag,
465			    &sc->sc_bus_memt, ih.ih_intrpin, addr, data);
466		} else
467			pci_msi_enable(ih.ih_pc, ih.ih_tag, addr, data);
468	} else {
469		cookie = acpi_intr_establish(ih.ih_intrpin, 0, level,
470		    func, arg, name);
471	}
472
473	return cookie;
474}
475
476void
477acpipci_intr_disestablish(void *v, void *cookie)
478{
479	panic("%s", __func__);
480}
481
482/*
483 * Translate memory address if needed.
484 */
485int
486acpipci_bs_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size,
487    int flags, bus_space_handle_t *bshp)
488{
489	struct acpipci_trans *at;
490
491	for (at = t->bus_private; at; at = at->at_next) {
492		if (addr >= at->at_base && addr < at->at_base + at->at_size) {
493			return bus_space_map(at->at_iot,
494			    addr + at->at_offset, size, flags, bshp);
495		}
496	}
497
498	return ENXIO;
499}
500
501SLIST_HEAD(,acpipci_mcfg) acpipci_mcfgs =
502    SLIST_HEAD_INITIALIZER(acpipci_mcfgs);
503
504void
505pci_mcfg_init(bus_space_tag_t iot, bus_addr_t addr, int segment,
506    int min_bus, int max_bus)
507{
508	struct acpipci_mcfg *am;
509
510	am = malloc(sizeof(struct acpipci_mcfg), M_DEVBUF, M_WAITOK | M_ZERO);
511	am->am_segment = segment;
512	am->am_min_bus = min_bus;
513	am->am_max_bus = max_bus;
514
515	am->am_iot = iot;
516	if (bus_space_map(iot, addr, (max_bus + 1) << 20, 0, &am->am_ioh))
517		panic("%s: can't map config space", __func__);
518
519	am->am_pc.pc_conf_v = am;
520	am->am_pc.pc_attach_hook = acpipci_attach_hook;
521	am->am_pc.pc_bus_maxdevs = acpipci_bus_maxdevs;
522	am->am_pc.pc_make_tag = acpipci_make_tag;
523	am->am_pc.pc_decompose_tag = acpipci_decompose_tag;
524	am->am_pc.pc_conf_size = acpipci_conf_size;
525	am->am_pc.pc_conf_read = acpipci_conf_read;
526	am->am_pc.pc_conf_write = acpipci_conf_write;
527	SLIST_INSERT_HEAD(&acpipci_mcfgs, am, am_list);
528}
529
530pcireg_t
531acpipci_dummy_conf_read(void *v, pcitag_t tag, int reg)
532{
533	return 0xffffffff;
534}
535
536void
537acpipci_dummy_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
538{
539}
540
541struct arm64_pci_chipset acpipci_dummy_chipset = {
542	.pc_attach_hook = acpipci_attach_hook,
543	.pc_bus_maxdevs = acpipci_bus_maxdevs,
544	.pc_make_tag = acpipci_make_tag,
545	.pc_decompose_tag = acpipci_decompose_tag,
546	.pc_conf_size = acpipci_conf_size,
547	.pc_conf_read = acpipci_dummy_conf_read,
548	.pc_conf_write = acpipci_dummy_conf_write,
549};
550
551pci_chipset_tag_t
552pci_lookup_segment(int segment)
553{
554	struct acpipci_mcfg *am;
555
556	SLIST_FOREACH(am, &acpipci_mcfgs, am_list) {
557		if (am->am_segment == segment)
558			return &am->am_pc;
559	}
560
561	return &acpipci_dummy_chipset;
562}
563
564/*
565 * IORT support.
566 */
567
568struct acpi_iort {
569	struct acpi_table_header	hdr;
570#define IORT_SIG	"IORT"
571	uint32_t	number_of_nodes;
572	uint32_t	offset;
573	uint32_t	reserved;
574} __packed;
575
576struct acpi_iort_node {
577	uint8_t		type;
578#define ACPI_IORT_ITS		0
579#define ACPI_IORT_ROOT_COMPLEX	2
580#define ACPI_IORT_SMMU		3
581	uint16_t	length;
582	uint8_t		revision;
583	uint32_t	reserved1;
584	uint32_t	number_of_mappings;
585	uint32_t	mapping_offset;
586	uint64_t	memory_access_properties;
587	uint32_t	atf_attributes;
588	uint32_t	segment;
589	uint8_t		memory_address_size_limit;
590	uint8_t		reserved2[3];
591} __packed;
592
593struct acpi_iort_mapping {
594	uint32_t	input_base;
595	uint32_t	number_of_ids;
596	uint32_t	output_base;
597	uint32_t	output_reference;
598	uint32_t	flags;
599#define ACPI_IORT_MAPPING_SINGLE	0x00000001
600} __packed;
601
602uint32_t acpipci_iort_map(struct acpi_iort *, uint32_t, uint32_t);
603
604uint32_t
605acpipci_iort_map_node(struct acpi_iort *iort,
606    struct acpi_iort_node *node, uint32_t id)
607{
608	struct acpi_iort_mapping *map =
609	    (struct acpi_iort_mapping *)((char *)node + node->mapping_offset);
610	int i;
611
612	for (i = 0; i < node->number_of_mappings; i++) {
613		uint32_t offset = map[i].output_reference;
614
615		if (map[i].flags & ACPI_IORT_MAPPING_SINGLE) {
616			id = map[i].output_base;
617			return acpipci_iort_map(iort, offset, id);
618		}
619
620		/* Mapping encodes number of IDs in the range minus one. */
621		if (map[i].input_base <= id &&
622		    id <= map[i].input_base + map[i].number_of_ids) {
623			id = map[i].output_base + (id - map[i].input_base);
624			return acpipci_iort_map(iort, offset, id);
625		}
626	}
627
628	return id;
629}
630
631uint32_t
632acpipci_iort_map(struct acpi_iort *iort, uint32_t offset, uint32_t id)
633{
634	struct acpi_iort_node *node =
635	    (struct acpi_iort_node *)((char *)iort + offset);
636
637	switch (node->type) {
638	case ACPI_IORT_ITS:
639		return id;
640	case ACPI_IORT_SMMU:
641		return acpipci_iort_map_node(iort, node, id);
642	}
643
644	return id;
645}
646
647uint32_t
648acpipci_iort_map_msi(pci_chipset_tag_t pc, pcitag_t tag)
649{
650	struct acpipci_softc *sc = pc->pc_intr_v;
651	struct acpi_table_header *hdr;
652	struct acpi_iort *iort = NULL;
653	struct acpi_iort_node *node;
654	struct acpi_q *entry;
655	uint32_t rid, offset;
656	int i;
657
658	rid = pci_requester_id(pc, tag);
659
660	/* Look for IORT table. */
661	SIMPLEQ_FOREACH(entry, &sc->sc_acpi->sc_tables, q_next) {
662		hdr = entry->q_table;
663		if (strncmp(hdr->signature, IORT_SIG,
664		    sizeof(hdr->signature)) == 0) {
665			iort = entry->q_table;
666			break;
667		}
668	}
669	if (iort == NULL)
670		return rid;
671
672	/* Find our root complex and map. */
673	offset = iort->offset;
674	for (i = 0; i < iort->number_of_nodes; i++) {
675		node = (struct acpi_iort_node *)((char *)iort + offset);
676		switch (node->type) {
677		case ACPI_IORT_ROOT_COMPLEX:
678			if (node->segment == sc->sc_seg)
679				return acpipci_iort_map_node(iort, node, rid);
680			break;
681		}
682		offset += node->length;
683	}
684
685	return rid;
686}
687