xref: /openbsd-src/sys/arch/arm64/dev/acpipci.c (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
1 /*	$OpenBSD: acpipci.c,v 1.35 2022/06/28 19:50:40 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 
37 #include <arm64/dev/acpiiort.h>
38 
39 struct acpipci_mcfg {
40 	SLIST_ENTRY(acpipci_mcfg) am_list;
41 
42 	uint16_t	am_segment;
43 	uint8_t		am_min_bus;
44 	uint8_t		am_max_bus;
45 
46 	bus_space_tag_t	am_iot;
47 	bus_space_handle_t am_ioh;
48 
49 	struct machine_pci_chipset am_pc;
50 };
51 
52 struct acpipci_trans {
53 	struct acpipci_trans *at_next;
54 	bus_space_tag_t	at_iot;
55 	bus_addr_t	at_base;
56 	bus_size_t	at_size;
57 	bus_size_t	at_offset;
58 };
59 
60 struct acpipci_softc {
61 	struct device	sc_dev;
62 	struct acpi_softc *sc_acpi;
63 	struct aml_node *sc_node;
64 	bus_space_tag_t	sc_iot;
65 	pci_chipset_tag_t sc_pc;
66 
67 	struct bus_space sc_bus_iot;
68 	struct bus_space sc_bus_memt;
69 	struct acpipci_trans *sc_io_trans;
70 	struct acpipci_trans *sc_mem_trans;
71 
72 	struct extent	*sc_busex;
73 	struct extent	*sc_memex;
74 	struct extent	*sc_ioex;
75 	char		sc_busex_name[32];
76 	char		sc_ioex_name[32];
77 	char		sc_memex_name[32];
78 	int		sc_bus;
79 	uint32_t	sc_seg;
80 
81 	struct interrupt_controller *sc_msi_ic;
82 };
83 
84 struct acpipci_intr_handle {
85 	struct machine_intr_handle aih_ih;
86 	bus_dma_tag_t		aih_dmat;
87 	bus_dmamap_t		aih_map;
88 };
89 
90 int	acpipci_match(struct device *, void *, void *);
91 void	acpipci_attach(struct device *, struct device *, void *);
92 
93 const struct cfattach acpipci_ca = {
94 	sizeof(struct acpipci_softc), acpipci_match, acpipci_attach
95 };
96 
97 struct cfdriver acpipci_cd = {
98 	NULL, "acpipci", DV_DULL
99 };
100 
101 const char *acpipci_hids[] = {
102 	"PNP0A08",
103 	NULL
104 };
105 
106 int	acpipci_parse_resources(int, union acpi_resource *, void *);
107 int	acpipci_bs_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
108 	    bus_space_handle_t *);
109 paddr_t acpipci_bs_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
110 
111 void	acpipci_attach_hook(struct device *, struct device *,
112 	    struct pcibus_attach_args *);
113 int	acpipci_bus_maxdevs(void *, int);
114 pcitag_t acpipci_make_tag(void *, int, int, int);
115 void	acpipci_decompose_tag(void *, pcitag_t, int *, int *, int *);
116 int	acpipci_conf_size(void *, pcitag_t);
117 pcireg_t acpipci_conf_read(void *, pcitag_t, int);
118 void	acpipci_conf_write(void *, pcitag_t, int, pcireg_t);
119 int	acpipci_probe_device_hook(void *, struct pci_attach_args *);
120 
121 int	acpipci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
122 const char *acpipci_intr_string(void *, pci_intr_handle_t);
123 void	*acpipci_intr_establish(void *, pci_intr_handle_t, int,
124 	    struct cpu_info *, int (*)(void *), void *, char *);
125 void	acpipci_intr_disestablish(void *, void *);
126 
127 uint32_t acpipci_iort_map_msi(pci_chipset_tag_t, pcitag_t);
128 
129 int
130 acpipci_match(struct device *parent, void *match, void *aux)
131 {
132 	struct acpi_attach_args *aaa = aux;
133 	struct cfdata *cf = match;
134 
135 	return acpi_matchhids(aaa, acpipci_hids, cf->cf_driver->cd_name);
136 }
137 
138 void
139 acpipci_attach(struct device *parent, struct device *self, void *aux)
140 {
141 	struct acpi_attach_args *aaa = aux;
142 	struct acpipci_softc *sc = (struct acpipci_softc *)self;
143 	struct interrupt_controller *ic;
144 	struct pcibus_attach_args pba;
145 	struct aml_value res;
146 	uint64_t bbn = 0;
147 	uint64_t seg = 0;
148 
149 	sc->sc_acpi = (struct acpi_softc *)parent;
150 	sc->sc_node = aaa->aaa_node;
151 	printf(" %s", sc->sc_node->name);
152 
153 	if (aml_evalname(sc->sc_acpi, sc->sc_node, "_CRS", 0, NULL, &res)) {
154 		printf(": can't find resources\n");
155 		return;
156 	}
157 
158 	aml_evalinteger(sc->sc_acpi, sc->sc_node, "_BBN", 0, NULL, &bbn);
159 	sc->sc_bus = bbn;
160 
161 	aml_evalinteger(sc->sc_acpi, sc->sc_node, "_SEG", 0, NULL, &seg);
162 	sc->sc_seg = seg;
163 
164 	sc->sc_iot = aaa->aaa_memt;
165 
166 	printf("\n");
167 
168 	/* Create extents for our address spaces. */
169 	snprintf(sc->sc_busex_name, sizeof(sc->sc_busex_name),
170 	    "%s pcibus", sc->sc_dev.dv_xname);
171 	snprintf(sc->sc_ioex_name, sizeof(sc->sc_ioex_name),
172 	    "%s pciio", sc->sc_dev.dv_xname);
173 	snprintf(sc->sc_memex_name, sizeof(sc->sc_memex_name),
174 	    "%s pcimem", sc->sc_dev.dv_xname);
175 	sc->sc_busex = extent_create(sc->sc_busex_name, 0, 255,
176 	    M_DEVBUF, NULL, 0, EX_WAITOK | EX_FILLED);
177 	sc->sc_ioex = extent_create(sc->sc_ioex_name, 0, 0xffffffff,
178 	    M_DEVBUF, NULL, 0, EX_WAITOK | EX_FILLED);
179 	sc->sc_memex = extent_create(sc->sc_memex_name, 0, (u_long)-1,
180 	    M_DEVBUF, NULL, 0, EX_WAITOK | EX_FILLED);
181 
182 	aml_parse_resource(&res, acpipci_parse_resources, sc);
183 
184 	memcpy(&sc->sc_bus_iot, sc->sc_iot, sizeof(sc->sc_bus_iot));
185 	sc->sc_bus_iot.bus_private = sc->sc_io_trans;
186 	sc->sc_bus_iot._space_map = acpipci_bs_map;
187 	sc->sc_bus_iot._space_mmap = acpipci_bs_mmap;
188 	memcpy(&sc->sc_bus_memt, sc->sc_iot, sizeof(sc->sc_bus_memt));
189 	sc->sc_bus_memt.bus_private = sc->sc_mem_trans;
190 	sc->sc_bus_memt._space_map = acpipci_bs_map;
191 	sc->sc_bus_memt._space_mmap = acpipci_bs_mmap;
192 
193 	extern LIST_HEAD(, interrupt_controller) interrupt_controllers;
194 	LIST_FOREACH(ic, &interrupt_controllers, ic_list) {
195 		if (ic->ic_establish_msi)
196 			break;
197 	}
198 	sc->sc_msi_ic = ic;
199 
200 	sc->sc_pc = pci_lookup_segment(seg);
201 	KASSERT(sc->sc_pc->pc_intr_v == NULL);
202 
203 	sc->sc_pc->pc_probe_device_hook = acpipci_probe_device_hook;
204 
205 	sc->sc_pc->pc_intr_v = sc;
206 	sc->sc_pc->pc_intr_map = acpipci_intr_map;
207 	sc->sc_pc->pc_intr_map_msi = _pci_intr_map_msi;
208 	sc->sc_pc->pc_intr_map_msix = _pci_intr_map_msix;
209 	sc->sc_pc->pc_intr_string = acpipci_intr_string;
210 	sc->sc_pc->pc_intr_establish = acpipci_intr_establish;
211 	sc->sc_pc->pc_intr_disestablish = acpipci_intr_disestablish;
212 
213 	memset(&pba, 0, sizeof(pba));
214 	pba.pba_busname = "pci";
215 	pba.pba_iot = &sc->sc_bus_iot;
216 	pba.pba_memt = &sc->sc_bus_memt;
217 	pba.pba_dmat = aaa->aaa_dmat;
218 	pba.pba_pc = sc->sc_pc;
219 	pba.pba_busex = sc->sc_busex;
220 	pba.pba_ioex = sc->sc_ioex;
221 	pba.pba_memex = sc->sc_memex;
222 	pba.pba_pmemex = sc->sc_memex;
223 	pba.pba_domain = pci_ndomains++;
224 	pba.pba_bus = sc->sc_bus;
225 	if (sc->sc_msi_ic)
226 		pba.pba_flags |= PCI_FLAGS_MSI_ENABLED;
227 
228 	config_found(self, &pba, NULL);
229 }
230 
231 int
232 acpipci_parse_resources(int crsidx, union acpi_resource *crs, void *arg)
233 {
234 	struct acpipci_softc *sc = arg;
235 	struct acpipci_trans *at;
236 	int type = AML_CRSTYPE(crs);
237 	int restype, tflags;
238 	u_long min, len = 0, tra;
239 
240 	switch (type) {
241 	case LR_WORD:
242 		restype = crs->lr_word.type;
243 		tflags = crs->lr_word.tflags;
244 		min = crs->lr_word._min;
245 		len = crs->lr_word._len;
246 		tra = crs->lr_word._tra;
247 		break;
248 	case LR_DWORD:
249 		restype = crs->lr_dword.type;
250 		tflags = crs->lr_dword.tflags;
251 		min = crs->lr_dword._min;
252 		len = crs->lr_dword._len;
253 		tra = crs->lr_dword._tra;
254 		break;
255 	case LR_QWORD:
256 		restype = crs->lr_qword.type;
257 		tflags = crs->lr_qword.tflags;
258 		min = crs->lr_qword._min;
259 		len = crs->lr_qword._len;
260 		tra = crs->lr_qword._tra;
261 		break;
262 	case LR_MEM32FIXED:
263 		restype = LR_TYPE_MEMORY;
264 		tflags = 0;
265 		min = crs->lr_m32fixed._bas;
266 		len = crs->lr_m32fixed._len;
267 		tra = 0;
268 		break;
269 	}
270 
271 	if (len == 0)
272 		return 0;
273 
274 	switch (restype) {
275 	case LR_TYPE_MEMORY:
276 		if (tflags & LR_MEMORY_TTP)
277 			return 0;
278 		extent_free(sc->sc_memex, min, len, EX_WAITOK);
279 		at = malloc(sizeof(struct acpipci_trans), M_DEVBUF, M_WAITOK);
280 		at->at_iot = sc->sc_iot;
281 		at->at_base = min;
282 		at->at_size = len;
283 		at->at_offset = tra;
284 		at->at_next = sc->sc_mem_trans;
285 		sc->sc_mem_trans = at;
286 		break;
287 	case LR_TYPE_IO:
288 		/*
289 		 * Don't check _TTP as various firmwares don't set it,
290 		 * even though they should!!
291 		 */
292 		extent_free(sc->sc_ioex, min, len, EX_WAITOK);
293 		at = malloc(sizeof(struct acpipci_trans), M_DEVBUF, M_WAITOK);
294 		at->at_iot = sc->sc_iot;
295 		at->at_base = min;
296 		at->at_size = len;
297 		at->at_offset = tra;
298 		at->at_next = sc->sc_io_trans;
299 		sc->sc_io_trans = at;
300 		break;
301 	case LR_TYPE_BUS:
302 		extent_free(sc->sc_busex, min, len, EX_WAITOK);
303 		/*
304 		 * Let _CRS minimum bus number override _BBN.
305 		 */
306 		sc->sc_bus = min;
307 		break;
308 	}
309 
310 	return 0;
311 }
312 
313 void
314 acpipci_attach_hook(struct device *parent, struct device *self,
315     struct pcibus_attach_args *pba)
316 {
317 }
318 
319 int
320 acpipci_bus_maxdevs(void *v, int bus)
321 {
322 	return 32;
323 }
324 
325 pcitag_t
326 acpipci_make_tag(void *v, int bus, int device, int function)
327 {
328 	return ((bus << 20) | (device << 15) | (function << 12));
329 }
330 
331 void
332 acpipci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
333 {
334 	if (bp != NULL)
335 		*bp = (tag >> 20) & 0xff;
336 	if (dp != NULL)
337 		*dp = (tag >> 15) & 0x1f;
338 	if (fp != NULL)
339 		*fp = (tag >> 12) & 0x7;
340 }
341 
342 int
343 acpipci_conf_size(void *v, pcitag_t tag)
344 {
345 	return PCIE_CONFIG_SPACE_SIZE;
346 }
347 
348 pcireg_t
349 acpipci_conf_read(void *v, pcitag_t tag, int reg)
350 {
351 	struct acpipci_mcfg *am = v;
352 
353 	if (tag < (am->am_min_bus << 20) ||
354 	    tag >= ((am->am_max_bus + 1) << 20))
355 		return 0xffffffff;
356 
357 	return bus_space_read_4(am->am_iot, am->am_ioh, tag | reg);
358 }
359 
360 void
361 acpipci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
362 {
363 	struct acpipci_mcfg *am = v;
364 
365 	if (tag < (am->am_min_bus << 20) ||
366 	    tag >= ((am->am_max_bus + 1) << 20))
367 		return;
368 
369 	bus_space_write_4(am->am_iot, am->am_ioh, tag | reg, data);
370 }
371 
372 int
373 acpipci_probe_device_hook(void *v, struct pci_attach_args *pa)
374 {
375 	struct acpipci_mcfg *am = v;
376 	struct acpipci_trans *at;
377 	struct acpi_table_header *hdr;
378 	struct acpi_iort *iort = NULL;
379 	struct acpi_iort_node *node;
380 	struct acpi_iort_mapping *map;
381 	struct acpi_iort_rc_node *rc;
382 	struct acpi_q *entry;
383 	uint32_t rid, offset;
384 	int i;
385 
386 	rid = pci_requester_id(pa->pa_pc, pa->pa_tag);
387 
388 	/* Look for IORT table. */
389 	SIMPLEQ_FOREACH(entry, &acpi_softc->sc_tables, q_next) {
390 		hdr = entry->q_table;
391 		if (strncmp(hdr->signature, IORT_SIG,
392 		    sizeof(hdr->signature)) == 0) {
393 			iort = entry->q_table;
394 			break;
395 		}
396 	}
397 	if (iort == NULL)
398 		return 0;
399 
400 	/* Find our root complex. */
401 	offset = iort->offset;
402 	for (i = 0; i < iort->number_of_nodes; i++) {
403 		node = (struct acpi_iort_node *)((char *)iort + offset);
404 		if (node->type == ACPI_IORT_ROOT_COMPLEX) {
405 			rc = (struct acpi_iort_rc_node *)&node[1];
406 			if (rc->segment == am->am_segment)
407 				break;
408 		}
409 		offset += node->length;
410 	}
411 
412 	/* No RC found? Weird. */
413 	if (i >= iort->number_of_nodes)
414 		return 0;
415 
416 	/* Find our output base towards SMMU. */
417 	map = (struct acpi_iort_mapping *)((char *)node + node->mapping_offset);
418 	for (i = 0; i < node->number_of_mappings; i++) {
419 		offset = map[i].output_reference;
420 
421 		if (map[i].flags & ACPI_IORT_MAPPING_SINGLE) {
422 			rid = map[i].output_base;
423 			break;
424 		}
425 
426 		/* Mapping encodes number of IDs in the range minus one. */
427 		if (map[i].input_base <= rid &&
428 		    rid <= map[i].input_base + map[i].number_of_ids) {
429 			rid = map[i].output_base + (rid - map[i].input_base);
430 			break;
431 		}
432 	}
433 
434 	/* No mapping found? Even weirder. */
435 	if (i >= node->number_of_mappings)
436 		return 0;
437 
438 	node = (struct acpi_iort_node *)((char *)iort + offset);
439 	if (node->type == ACPI_IORT_SMMU) {
440 		pa->pa_dmat = acpiiort_smmu_map(node, rid, pa->pa_dmat);
441 		for (at = pa->pa_iot->bus_private; at; at = at->at_next) {
442 			acpiiort_smmu_reserve_region(node, rid,
443 			    at->at_base, at->at_size);
444 		}
445 		for (at = pa->pa_memt->bus_private; at; at = at->at_next) {
446 			acpiiort_smmu_reserve_region(node, rid,
447 			    at->at_base, at->at_size);
448 		}
449 	}
450 
451 	return 0;
452 }
453 
454 int
455 acpipci_intr_swizzle(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
456 {
457 	int dev, swizpin;
458 
459 	if (pa->pa_bridgeih == NULL)
460 		return -1;
461 
462 	pci_decompose_tag(pa->pa_pc, pa->pa_tag, NULL, &dev, NULL);
463 	swizpin = PPB_INTERRUPT_SWIZZLE(pa->pa_rawintrpin, dev);
464 	if (pa->pa_bridgeih[swizpin - 1].ih_type == PCI_NONE)
465 		return -1;
466 
467 	*ihp = pa->pa_bridgeih[swizpin - 1];
468 	return 0;
469 }
470 
471 int
472 acpipci_getirq(int crsidx, union acpi_resource *crs, void *arg)
473 {
474 	int *irq = arg;
475 
476 	switch (AML_CRSTYPE(crs)) {
477 	case SR_IRQ:
478 		*irq = ffs(letoh16(crs->sr_irq.irq_mask)) - 1;
479 		break;
480 	case LR_EXTIRQ:
481 		*irq = letoh32(crs->lr_extirq.irq[0]);
482 		break;
483 	default:
484 		break;
485 	}
486 
487 	return 0;
488 }
489 
490 int
491 acpipci_intr_link(struct acpipci_softc *sc, struct aml_value *val)
492 {
493 	struct aml_value res;
494 	int64_t sta;
495 	int irq = -1;
496 
497 	if (val->type == AML_OBJTYPE_OBJREF)
498 		val = val->v_objref.ref;
499 	if (val->type != AML_OBJTYPE_DEVICE)
500 		return -1;
501 
502 	sta = acpi_getsta(sc->sc_acpi, val->node);
503 	if ((sta & STA_PRESENT) == 0)
504 		return -1;
505 
506 	if (aml_evalname(sc->sc_acpi, val->node, "_CRS", 0, NULL, &res))
507 		return -1;
508 	aml_parse_resource(&res, acpipci_getirq, &irq);
509 	aml_freevalue(&res);
510 
511 	return irq;
512 }
513 
514 int
515 acpipci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
516 {
517 	struct acpipci_softc *sc = pa->pa_pc->pc_intr_v;
518 	struct aml_node *node = sc->sc_node;
519 	struct aml_value res;
520 	uint64_t addr, pin, source, index;
521 	int i;
522 
523 	/*
524 	 * If we're behind a bridge, we need to look for a _PRT for
525 	 * it.  If we don't find a _PRT, we need to swizzle.  If we're
526 	 * not behind a bridge we need to look for a _PRT on the host
527 	 * bridge node itself.
528 	 */
529 	if (pa->pa_bridgetag) {
530 		node = acpi_find_pci(pa->pa_pc, *pa->pa_bridgetag);
531 		if (node == NULL)
532 			return acpipci_intr_swizzle(pa, ihp);
533 	}
534 
535 	if (aml_evalname(sc->sc_acpi, node, "_PRT", 0, NULL, &res))
536 		return acpipci_intr_swizzle(pa, ihp);
537 
538 	if (res.type != AML_OBJTYPE_PACKAGE)
539 		return -1;
540 
541 	for (i = 0; i < res.length; i++) {
542 		struct aml_value *val = res.v_package[i];
543 
544 		if (val->type != AML_OBJTYPE_PACKAGE)
545 			continue;
546 		if (val->length != 4)
547 			continue;
548 		if (val->v_package[0]->type != AML_OBJTYPE_INTEGER ||
549 		    val->v_package[1]->type != AML_OBJTYPE_INTEGER ||
550 		    val->v_package[3]->type != AML_OBJTYPE_INTEGER)
551 			continue;
552 
553 		addr = val->v_package[0]->v_integer;
554 		pin = val->v_package[1]->v_integer;
555 		if (ACPI_ADR_PCIDEV(addr) != pa->pa_device ||
556 		    ACPI_ADR_PCIFUN(addr) != 0xffff ||
557 		    pin != pa->pa_intrpin - 1)
558 			continue;
559 
560 		if (val->v_package[2]->type == AML_OBJTYPE_INTEGER) {
561 			source = val->v_package[2]->v_integer;
562 			index = val->v_package[3]->v_integer;
563 		} else {
564 			source = 0;
565 			index = acpipci_intr_link(sc, val->v_package[2]);
566 		}
567 		if (source != 0 || index == -1)
568 			continue;
569 
570 		ihp->ih_pc = pa->pa_pc;
571 		ihp->ih_tag = pa->pa_tag;
572 		ihp->ih_intrpin = index;
573 		ihp->ih_type = PCI_INTX;
574 
575 		return 0;
576 	}
577 
578 	return -1;
579 }
580 
581 const char *
582 acpipci_intr_string(void *v, pci_intr_handle_t ih)
583 {
584 	static char irqstr[32];
585 
586 	switch (ih.ih_type) {
587 	case PCI_MSI:
588 		return "msi";
589 	case PCI_MSIX:
590 		return "msix";
591 	}
592 
593 	snprintf(irqstr, sizeof(irqstr), "irq %d", ih.ih_intrpin);
594 	return irqstr;
595 }
596 
597 void *
598 acpipci_intr_establish(void *v, pci_intr_handle_t ih, int level,
599     struct cpu_info *ci, int (*func)(void *), void *arg, char *name)
600 {
601 	struct acpipci_softc *sc = v;
602 	struct acpipci_intr_handle *aih;
603 	void *cookie;
604 
605 	KASSERT(ih.ih_type != PCI_NONE);
606 
607 	if (ih.ih_type != PCI_INTX) {
608 		struct interrupt_controller *ic = sc->sc_msi_ic;
609 		bus_dma_segment_t seg;
610 		uint64_t addr, data;
611 
612 		KASSERT(ic);
613 
614 		/* Map Requester ID through IORT to get sideband data. */
615 		data = acpipci_iort_map_msi(ih.ih_pc, ih.ih_tag);
616 		cookie = ic->ic_establish_msi(ic->ic_cookie, &addr,
617 		    &data, level, ci, func, arg, name);
618 		if (cookie == NULL)
619 			return NULL;
620 
621 		aih = malloc(sizeof(*aih), M_DEVBUF, M_WAITOK);
622 		aih->aih_ih.ih_ic = ic;
623 		aih->aih_ih.ih_ih = cookie;
624 		aih->aih_dmat = ih.ih_dmat;
625 
626 		if (bus_dmamap_create(aih->aih_dmat, sizeof(uint32_t), 1,
627 		    sizeof(uint32_t), 0, BUS_DMA_WAITOK, &aih->aih_map)) {
628 			free(aih, M_DEVBUF, sizeof(*aih));
629 			ic->ic_disestablish(cookie);
630 			return NULL;
631 		}
632 
633 		memset(&seg, 0, sizeof(seg));
634 		seg.ds_addr = addr;
635 		seg.ds_len = sizeof(uint32_t);
636 
637 		if (bus_dmamap_load_raw(aih->aih_dmat, aih->aih_map,
638 		    &seg, 1, sizeof(uint32_t), BUS_DMA_WAITOK)) {
639 			bus_dmamap_destroy(aih->aih_dmat, aih->aih_map);
640 			free(aih, M_DEVBUF, sizeof(*aih));
641 			ic->ic_disestablish(cookie);
642 			return NULL;
643 		}
644 
645 		addr = aih->aih_map->dm_segs[0].ds_addr;
646 		if (ih.ih_type == PCI_MSIX) {
647 			pci_msix_enable(ih.ih_pc, ih.ih_tag,
648 			    &sc->sc_bus_memt, ih.ih_intrpin, addr, data);
649 		} else
650 			pci_msi_enable(ih.ih_pc, ih.ih_tag, addr, data);
651 
652 		cookie = aih;
653 	} else {
654 		if (ci != NULL && !CPU_IS_PRIMARY(ci))
655 			return NULL;
656 		cookie = acpi_intr_establish(ih.ih_intrpin, 0, level,
657 		    func, arg, name);
658 	}
659 
660 	return cookie;
661 }
662 
663 void
664 acpipci_intr_disestablish(void *v, void *cookie)
665 {
666 	struct acpipci_intr_handle *aih = cookie;
667 	struct interrupt_controller *ic = aih->aih_ih.ih_ic;
668 
669 	if (ic->ic_establish_msi) {
670 		ic->ic_disestablish(aih->aih_ih.ih_ih);
671 		bus_dmamap_unload(aih->aih_dmat, aih->aih_map);
672 		bus_dmamap_destroy(aih->aih_dmat, aih->aih_map);
673 		free(aih, M_DEVBUF, sizeof(*aih));
674 	} else
675 		acpi_intr_disestablish(cookie);
676 }
677 
678 /*
679  * Translate memory address if needed.
680  */
681 int
682 acpipci_bs_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size,
683     int flags, bus_space_handle_t *bshp)
684 {
685 	struct acpipci_trans *at;
686 
687 	for (at = t->bus_private; at; at = at->at_next) {
688 		if (addr >= at->at_base && addr < at->at_base + at->at_size) {
689 			return bus_space_map(at->at_iot,
690 			    addr + at->at_offset, size, flags, bshp);
691 		}
692 	}
693 
694 	return ENXIO;
695 }
696 
697 paddr_t
698 acpipci_bs_mmap(bus_space_tag_t t, bus_addr_t addr, off_t off,
699     int prot, int flags)
700 {
701 	struct acpipci_trans *at;
702 
703 	for (at = t->bus_private; at; at = at->at_next) {
704 		if (addr >= at->at_base && addr < at->at_base + at->at_size) {
705 			return bus_space_mmap(at->at_iot,
706 			    addr + at->at_offset, off, prot, flags);
707 		}
708 	}
709 
710 	return -1;
711 }
712 
713 SLIST_HEAD(,acpipci_mcfg) acpipci_mcfgs =
714     SLIST_HEAD_INITIALIZER(acpipci_mcfgs);
715 
716 void
717 pci_mcfg_init(bus_space_tag_t iot, bus_addr_t addr, int segment,
718     int min_bus, int max_bus)
719 {
720 	struct acpipci_mcfg *am;
721 
722 	am = malloc(sizeof(struct acpipci_mcfg), M_DEVBUF, M_WAITOK | M_ZERO);
723 	am->am_segment = segment;
724 	am->am_min_bus = min_bus;
725 	am->am_max_bus = max_bus;
726 
727 	am->am_iot = iot;
728 	if (bus_space_map(iot, addr, (max_bus + 1) << 20, 0, &am->am_ioh))
729 		panic("%s: can't map config space", __func__);
730 
731 	am->am_pc.pc_conf_v = am;
732 	am->am_pc.pc_attach_hook = acpipci_attach_hook;
733 	am->am_pc.pc_bus_maxdevs = acpipci_bus_maxdevs;
734 	am->am_pc.pc_make_tag = acpipci_make_tag;
735 	am->am_pc.pc_decompose_tag = acpipci_decompose_tag;
736 	am->am_pc.pc_conf_size = acpipci_conf_size;
737 	am->am_pc.pc_conf_read = acpipci_conf_read;
738 	am->am_pc.pc_conf_write = acpipci_conf_write;
739 	SLIST_INSERT_HEAD(&acpipci_mcfgs, am, am_list);
740 }
741 
742 pcireg_t
743 acpipci_dummy_conf_read(void *v, pcitag_t tag, int reg)
744 {
745 	return 0xffffffff;
746 }
747 
748 void
749 acpipci_dummy_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
750 {
751 }
752 
753 struct machine_pci_chipset acpipci_dummy_chipset = {
754 	.pc_attach_hook = acpipci_attach_hook,
755 	.pc_bus_maxdevs = acpipci_bus_maxdevs,
756 	.pc_make_tag = acpipci_make_tag,
757 	.pc_decompose_tag = acpipci_decompose_tag,
758 	.pc_conf_size = acpipci_conf_size,
759 	.pc_conf_read = acpipci_dummy_conf_read,
760 	.pc_conf_write = acpipci_dummy_conf_write,
761 };
762 
763 pci_chipset_tag_t
764 pci_lookup_segment(int segment)
765 {
766 	struct acpipci_mcfg *am;
767 
768 	SLIST_FOREACH(am, &acpipci_mcfgs, am_list) {
769 		if (am->am_segment == segment)
770 			return &am->am_pc;
771 	}
772 
773 	return &acpipci_dummy_chipset;
774 }
775 
776 /*
777  * IORT support.
778  */
779 
780 uint32_t acpipci_iort_map(struct acpi_iort *, uint32_t, uint32_t);
781 
782 uint32_t
783 acpipci_iort_map_node(struct acpi_iort *iort,
784     struct acpi_iort_node *node, uint32_t id)
785 {
786 	struct acpi_iort_mapping *map =
787 	    (struct acpi_iort_mapping *)((char *)node + node->mapping_offset);
788 	int i;
789 
790 	for (i = 0; i < node->number_of_mappings; i++) {
791 		uint32_t offset = map[i].output_reference;
792 
793 		if (map[i].flags & ACPI_IORT_MAPPING_SINGLE) {
794 			id = map[i].output_base;
795 			return acpipci_iort_map(iort, offset, id);
796 		}
797 
798 		/* Mapping encodes number of IDs in the range minus one. */
799 		if (map[i].input_base <= id &&
800 		    id <= map[i].input_base + map[i].number_of_ids) {
801 			id = map[i].output_base + (id - map[i].input_base);
802 			return acpipci_iort_map(iort, offset, id);
803 		}
804 	}
805 
806 	return id;
807 }
808 
809 uint32_t
810 acpipci_iort_map(struct acpi_iort *iort, uint32_t offset, uint32_t id)
811 {
812 	struct acpi_iort_node *node =
813 	    (struct acpi_iort_node *)((char *)iort + offset);
814 
815 	switch (node->type) {
816 	case ACPI_IORT_ITS:
817 		return id;
818 	case ACPI_IORT_SMMU:
819 		return acpipci_iort_map_node(iort, node, id);
820 	}
821 
822 	return id;
823 }
824 
825 uint32_t
826 acpipci_iort_map_msi(pci_chipset_tag_t pc, pcitag_t tag)
827 {
828 	struct acpipci_softc *sc = pc->pc_intr_v;
829 	struct acpi_table_header *hdr;
830 	struct acpi_iort *iort = NULL;
831 	struct acpi_iort_node *node;
832 	struct acpi_iort_rc_node *rc;
833 	struct acpi_q *entry;
834 	uint32_t rid, offset;
835 	int i;
836 
837 	rid = pci_requester_id(pc, tag);
838 
839 	/* Look for IORT table. */
840 	SIMPLEQ_FOREACH(entry, &sc->sc_acpi->sc_tables, q_next) {
841 		hdr = entry->q_table;
842 		if (strncmp(hdr->signature, IORT_SIG,
843 		    sizeof(hdr->signature)) == 0) {
844 			iort = entry->q_table;
845 			break;
846 		}
847 	}
848 	if (iort == NULL)
849 		return rid;
850 
851 	/* Find our root complex and map. */
852 	offset = iort->offset;
853 	for (i = 0; i < iort->number_of_nodes; i++) {
854 		node = (struct acpi_iort_node *)((char *)iort + offset);
855 		switch (node->type) {
856 		case ACPI_IORT_ROOT_COMPLEX:
857 			rc = (struct acpi_iort_rc_node *)&node[1];
858 			if (rc->segment == sc->sc_seg)
859 				return acpipci_iort_map_node(iort, node, rid);
860 			break;
861 		}
862 		offset += node->length;
863 	}
864 
865 	return rid;
866 }
867