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