1 /* $OpenBSD: acpipci.c,v 1.34 2021/12/11 20:07:27 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 } 263 264 if (len == 0) 265 return 0; 266 267 switch (restype) { 268 case LR_TYPE_MEMORY: 269 if (tflags & LR_MEMORY_TTP) 270 return 0; 271 extent_free(sc->sc_memex, min, len, EX_WAITOK); 272 at = malloc(sizeof(struct acpipci_trans), M_DEVBUF, M_WAITOK); 273 at->at_iot = sc->sc_iot; 274 at->at_base = min; 275 at->at_size = len; 276 at->at_offset = tra; 277 at->at_next = sc->sc_mem_trans; 278 sc->sc_mem_trans = at; 279 break; 280 case LR_TYPE_IO: 281 /* 282 * Don't check _TTP as various firmwares don't set it, 283 * even though they should!! 284 */ 285 extent_free(sc->sc_ioex, min, len, EX_WAITOK); 286 at = malloc(sizeof(struct acpipci_trans), M_DEVBUF, M_WAITOK); 287 at->at_iot = sc->sc_iot; 288 at->at_base = min; 289 at->at_size = len; 290 at->at_offset = tra; 291 at->at_next = sc->sc_io_trans; 292 sc->sc_io_trans = at; 293 break; 294 case LR_TYPE_BUS: 295 extent_free(sc->sc_busex, min, len, EX_WAITOK); 296 /* 297 * Let _CRS minimum bus number override _BBN. 298 */ 299 sc->sc_bus = min; 300 break; 301 } 302 303 return 0; 304 } 305 306 void 307 acpipci_attach_hook(struct device *parent, struct device *self, 308 struct pcibus_attach_args *pba) 309 { 310 } 311 312 int 313 acpipci_bus_maxdevs(void *v, int bus) 314 { 315 return 32; 316 } 317 318 pcitag_t 319 acpipci_make_tag(void *v, int bus, int device, int function) 320 { 321 return ((bus << 20) | (device << 15) | (function << 12)); 322 } 323 324 void 325 acpipci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp) 326 { 327 if (bp != NULL) 328 *bp = (tag >> 20) & 0xff; 329 if (dp != NULL) 330 *dp = (tag >> 15) & 0x1f; 331 if (fp != NULL) 332 *fp = (tag >> 12) & 0x7; 333 } 334 335 int 336 acpipci_conf_size(void *v, pcitag_t tag) 337 { 338 return PCIE_CONFIG_SPACE_SIZE; 339 } 340 341 pcireg_t 342 acpipci_conf_read(void *v, pcitag_t tag, int reg) 343 { 344 struct acpipci_mcfg *am = v; 345 346 if (tag < (am->am_min_bus << 20) || 347 tag >= ((am->am_max_bus + 1) << 20)) 348 return 0xffffffff; 349 350 return bus_space_read_4(am->am_iot, am->am_ioh, tag | reg); 351 } 352 353 void 354 acpipci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data) 355 { 356 struct acpipci_mcfg *am = v; 357 358 if (tag < (am->am_min_bus << 20) || 359 tag >= ((am->am_max_bus + 1) << 20)) 360 return; 361 362 bus_space_write_4(am->am_iot, am->am_ioh, tag | reg, data); 363 } 364 365 int 366 acpipci_probe_device_hook(void *v, struct pci_attach_args *pa) 367 { 368 struct acpipci_mcfg *am = v; 369 struct acpipci_trans *at; 370 struct acpi_table_header *hdr; 371 struct acpi_iort *iort = NULL; 372 struct acpi_iort_node *node; 373 struct acpi_iort_mapping *map; 374 struct acpi_iort_rc_node *rc; 375 struct acpi_q *entry; 376 uint32_t rid, offset; 377 int i; 378 379 rid = pci_requester_id(pa->pa_pc, pa->pa_tag); 380 381 /* Look for IORT table. */ 382 SIMPLEQ_FOREACH(entry, &acpi_softc->sc_tables, q_next) { 383 hdr = entry->q_table; 384 if (strncmp(hdr->signature, IORT_SIG, 385 sizeof(hdr->signature)) == 0) { 386 iort = entry->q_table; 387 break; 388 } 389 } 390 if (iort == NULL) 391 return 0; 392 393 /* Find our root complex. */ 394 offset = iort->offset; 395 for (i = 0; i < iort->number_of_nodes; i++) { 396 node = (struct acpi_iort_node *)((char *)iort + offset); 397 if (node->type == ACPI_IORT_ROOT_COMPLEX) { 398 rc = (struct acpi_iort_rc_node *)&node[1]; 399 if (rc->segment == am->am_segment) 400 break; 401 } 402 offset += node->length; 403 } 404 405 /* No RC found? Weird. */ 406 if (i >= iort->number_of_nodes) 407 return 0; 408 409 /* Find our output base towards SMMU. */ 410 map = (struct acpi_iort_mapping *)((char *)node + node->mapping_offset); 411 for (i = 0; i < node->number_of_mappings; i++) { 412 offset = map[i].output_reference; 413 414 if (map[i].flags & ACPI_IORT_MAPPING_SINGLE) { 415 rid = map[i].output_base; 416 break; 417 } 418 419 /* Mapping encodes number of IDs in the range minus one. */ 420 if (map[i].input_base <= rid && 421 rid <= map[i].input_base + map[i].number_of_ids) { 422 rid = map[i].output_base + (rid - map[i].input_base); 423 break; 424 } 425 } 426 427 /* No mapping found? Even weirder. */ 428 if (i >= node->number_of_mappings) 429 return 0; 430 431 node = (struct acpi_iort_node *)((char *)iort + offset); 432 if (node->type == ACPI_IORT_SMMU) { 433 pa->pa_dmat = acpiiort_smmu_map(node, rid, pa->pa_dmat); 434 for (at = pa->pa_iot->bus_private; at; at = at->at_next) { 435 acpiiort_smmu_reserve_region(node, rid, 436 at->at_base, at->at_size); 437 } 438 for (at = pa->pa_memt->bus_private; at; at = at->at_next) { 439 acpiiort_smmu_reserve_region(node, rid, 440 at->at_base, at->at_size); 441 } 442 } 443 444 return 0; 445 } 446 447 int 448 acpipci_intr_swizzle(struct pci_attach_args *pa, pci_intr_handle_t *ihp) 449 { 450 int dev, swizpin; 451 452 if (pa->pa_bridgeih == NULL) 453 return -1; 454 455 pci_decompose_tag(pa->pa_pc, pa->pa_tag, NULL, &dev, NULL); 456 swizpin = PPB_INTERRUPT_SWIZZLE(pa->pa_rawintrpin, dev); 457 if (pa->pa_bridgeih[swizpin - 1].ih_type == PCI_NONE) 458 return -1; 459 460 *ihp = pa->pa_bridgeih[swizpin - 1]; 461 return 0; 462 } 463 464 int 465 acpipci_getirq(int crsidx, union acpi_resource *crs, void *arg) 466 { 467 int *irq = arg; 468 469 switch (AML_CRSTYPE(crs)) { 470 case SR_IRQ: 471 *irq = ffs(letoh16(crs->sr_irq.irq_mask)) - 1; 472 break; 473 case LR_EXTIRQ: 474 *irq = letoh32(crs->lr_extirq.irq[0]); 475 break; 476 default: 477 break; 478 } 479 480 return 0; 481 } 482 483 int 484 acpipci_intr_link(struct acpipci_softc *sc, struct aml_value *val) 485 { 486 struct aml_value res; 487 int64_t sta; 488 int irq = -1; 489 490 if (val->type == AML_OBJTYPE_OBJREF) 491 val = val->v_objref.ref; 492 if (val->type != AML_OBJTYPE_DEVICE) 493 return -1; 494 495 sta = acpi_getsta(sc->sc_acpi, val->node); 496 if ((sta & STA_PRESENT) == 0) 497 return -1; 498 499 if (aml_evalname(sc->sc_acpi, val->node, "_CRS", 0, NULL, &res)) 500 return -1; 501 aml_parse_resource(&res, acpipci_getirq, &irq); 502 aml_freevalue(&res); 503 504 return irq; 505 } 506 507 int 508 acpipci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp) 509 { 510 struct acpipci_softc *sc = pa->pa_pc->pc_intr_v; 511 struct aml_node *node = sc->sc_node; 512 struct aml_value res; 513 uint64_t addr, pin, source, index; 514 int i; 515 516 /* 517 * If we're behind a bridge, we need to look for a _PRT for 518 * it. If we don't find a _PRT, we need to swizzle. If we're 519 * not behind a bridge we need to look for a _PRT on the host 520 * bridge node itself. 521 */ 522 if (pa->pa_bridgetag) { 523 node = acpi_find_pci(pa->pa_pc, *pa->pa_bridgetag); 524 if (node == NULL) 525 return acpipci_intr_swizzle(pa, ihp); 526 } 527 528 if (aml_evalname(sc->sc_acpi, node, "_PRT", 0, NULL, &res)) 529 return acpipci_intr_swizzle(pa, ihp); 530 531 if (res.type != AML_OBJTYPE_PACKAGE) 532 return -1; 533 534 for (i = 0; i < res.length; i++) { 535 struct aml_value *val = res.v_package[i]; 536 537 if (val->type != AML_OBJTYPE_PACKAGE) 538 continue; 539 if (val->length != 4) 540 continue; 541 if (val->v_package[0]->type != AML_OBJTYPE_INTEGER || 542 val->v_package[1]->type != AML_OBJTYPE_INTEGER || 543 val->v_package[3]->type != AML_OBJTYPE_INTEGER) 544 continue; 545 546 addr = val->v_package[0]->v_integer; 547 pin = val->v_package[1]->v_integer; 548 if (ACPI_ADR_PCIDEV(addr) != pa->pa_device || 549 ACPI_ADR_PCIFUN(addr) != 0xffff || 550 pin != pa->pa_intrpin - 1) 551 continue; 552 553 if (val->v_package[2]->type == AML_OBJTYPE_INTEGER) { 554 source = val->v_package[2]->v_integer; 555 index = val->v_package[3]->v_integer; 556 } else { 557 source = 0; 558 index = acpipci_intr_link(sc, val->v_package[2]); 559 } 560 if (source != 0 || index == -1) 561 continue; 562 563 ihp->ih_pc = pa->pa_pc; 564 ihp->ih_tag = pa->pa_tag; 565 ihp->ih_intrpin = index; 566 ihp->ih_type = PCI_INTX; 567 568 return 0; 569 } 570 571 return -1; 572 } 573 574 const char * 575 acpipci_intr_string(void *v, pci_intr_handle_t ih) 576 { 577 static char irqstr[32]; 578 579 switch (ih.ih_type) { 580 case PCI_MSI: 581 return "msi"; 582 case PCI_MSIX: 583 return "msix"; 584 } 585 586 snprintf(irqstr, sizeof(irqstr), "irq %d", ih.ih_intrpin); 587 return irqstr; 588 } 589 590 void * 591 acpipci_intr_establish(void *v, pci_intr_handle_t ih, int level, 592 struct cpu_info *ci, int (*func)(void *), void *arg, char *name) 593 { 594 struct acpipci_softc *sc = v; 595 struct acpipci_intr_handle *aih; 596 void *cookie; 597 598 KASSERT(ih.ih_type != PCI_NONE); 599 600 if (ih.ih_type != PCI_INTX) { 601 struct interrupt_controller *ic = sc->sc_msi_ic; 602 bus_dma_segment_t seg; 603 uint64_t addr, data; 604 605 KASSERT(ic); 606 607 /* Map Requester ID through IORT to get sideband data. */ 608 data = acpipci_iort_map_msi(ih.ih_pc, ih.ih_tag); 609 cookie = ic->ic_establish_msi(ic->ic_cookie, &addr, 610 &data, level, ci, func, arg, name); 611 if (cookie == NULL) 612 return NULL; 613 614 aih = malloc(sizeof(*aih), M_DEVBUF, M_WAITOK); 615 aih->aih_ih.ih_ic = ic; 616 aih->aih_ih.ih_ih = cookie; 617 aih->aih_dmat = ih.ih_dmat; 618 619 if (bus_dmamap_create(aih->aih_dmat, sizeof(uint32_t), 1, 620 sizeof(uint32_t), 0, BUS_DMA_WAITOK, &aih->aih_map)) { 621 free(aih, M_DEVBUF, sizeof(*aih)); 622 ic->ic_disestablish(cookie); 623 return NULL; 624 } 625 626 memset(&seg, 0, sizeof(seg)); 627 seg.ds_addr = addr; 628 seg.ds_len = sizeof(uint32_t); 629 630 if (bus_dmamap_load_raw(aih->aih_dmat, aih->aih_map, 631 &seg, 1, sizeof(uint32_t), BUS_DMA_WAITOK)) { 632 bus_dmamap_destroy(aih->aih_dmat, aih->aih_map); 633 free(aih, M_DEVBUF, sizeof(*aih)); 634 ic->ic_disestablish(cookie); 635 return NULL; 636 } 637 638 addr = aih->aih_map->dm_segs[0].ds_addr; 639 if (ih.ih_type == PCI_MSIX) { 640 pci_msix_enable(ih.ih_pc, ih.ih_tag, 641 &sc->sc_bus_memt, ih.ih_intrpin, addr, data); 642 } else 643 pci_msi_enable(ih.ih_pc, ih.ih_tag, addr, data); 644 645 cookie = aih; 646 } else { 647 if (ci != NULL && !CPU_IS_PRIMARY(ci)) 648 return NULL; 649 cookie = acpi_intr_establish(ih.ih_intrpin, 0, level, 650 func, arg, name); 651 } 652 653 return cookie; 654 } 655 656 void 657 acpipci_intr_disestablish(void *v, void *cookie) 658 { 659 struct acpipci_intr_handle *aih = cookie; 660 struct interrupt_controller *ic = aih->aih_ih.ih_ic; 661 662 if (ic->ic_establish_msi) { 663 ic->ic_disestablish(aih->aih_ih.ih_ih); 664 bus_dmamap_unload(aih->aih_dmat, aih->aih_map); 665 bus_dmamap_destroy(aih->aih_dmat, aih->aih_map); 666 free(aih, M_DEVBUF, sizeof(*aih)); 667 } else 668 acpi_intr_disestablish(cookie); 669 } 670 671 /* 672 * Translate memory address if needed. 673 */ 674 int 675 acpipci_bs_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size, 676 int flags, bus_space_handle_t *bshp) 677 { 678 struct acpipci_trans *at; 679 680 for (at = t->bus_private; at; at = at->at_next) { 681 if (addr >= at->at_base && addr < at->at_base + at->at_size) { 682 return bus_space_map(at->at_iot, 683 addr + at->at_offset, size, flags, bshp); 684 } 685 } 686 687 return ENXIO; 688 } 689 690 paddr_t 691 acpipci_bs_mmap(bus_space_tag_t t, bus_addr_t addr, off_t off, 692 int prot, int flags) 693 { 694 struct acpipci_trans *at; 695 696 for (at = t->bus_private; at; at = at->at_next) { 697 if (addr >= at->at_base && addr < at->at_base + at->at_size) { 698 return bus_space_mmap(at->at_iot, 699 addr + at->at_offset, off, prot, flags); 700 } 701 } 702 703 return -1; 704 } 705 706 SLIST_HEAD(,acpipci_mcfg) acpipci_mcfgs = 707 SLIST_HEAD_INITIALIZER(acpipci_mcfgs); 708 709 void 710 pci_mcfg_init(bus_space_tag_t iot, bus_addr_t addr, int segment, 711 int min_bus, int max_bus) 712 { 713 struct acpipci_mcfg *am; 714 715 am = malloc(sizeof(struct acpipci_mcfg), M_DEVBUF, M_WAITOK | M_ZERO); 716 am->am_segment = segment; 717 am->am_min_bus = min_bus; 718 am->am_max_bus = max_bus; 719 720 am->am_iot = iot; 721 if (bus_space_map(iot, addr, (max_bus + 1) << 20, 0, &am->am_ioh)) 722 panic("%s: can't map config space", __func__); 723 724 am->am_pc.pc_conf_v = am; 725 am->am_pc.pc_attach_hook = acpipci_attach_hook; 726 am->am_pc.pc_bus_maxdevs = acpipci_bus_maxdevs; 727 am->am_pc.pc_make_tag = acpipci_make_tag; 728 am->am_pc.pc_decompose_tag = acpipci_decompose_tag; 729 am->am_pc.pc_conf_size = acpipci_conf_size; 730 am->am_pc.pc_conf_read = acpipci_conf_read; 731 am->am_pc.pc_conf_write = acpipci_conf_write; 732 SLIST_INSERT_HEAD(&acpipci_mcfgs, am, am_list); 733 } 734 735 pcireg_t 736 acpipci_dummy_conf_read(void *v, pcitag_t tag, int reg) 737 { 738 return 0xffffffff; 739 } 740 741 void 742 acpipci_dummy_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data) 743 { 744 } 745 746 struct machine_pci_chipset acpipci_dummy_chipset = { 747 .pc_attach_hook = acpipci_attach_hook, 748 .pc_bus_maxdevs = acpipci_bus_maxdevs, 749 .pc_make_tag = acpipci_make_tag, 750 .pc_decompose_tag = acpipci_decompose_tag, 751 .pc_conf_size = acpipci_conf_size, 752 .pc_conf_read = acpipci_dummy_conf_read, 753 .pc_conf_write = acpipci_dummy_conf_write, 754 }; 755 756 pci_chipset_tag_t 757 pci_lookup_segment(int segment) 758 { 759 struct acpipci_mcfg *am; 760 761 SLIST_FOREACH(am, &acpipci_mcfgs, am_list) { 762 if (am->am_segment == segment) 763 return &am->am_pc; 764 } 765 766 return &acpipci_dummy_chipset; 767 } 768 769 /* 770 * IORT support. 771 */ 772 773 uint32_t acpipci_iort_map(struct acpi_iort *, uint32_t, uint32_t); 774 775 uint32_t 776 acpipci_iort_map_node(struct acpi_iort *iort, 777 struct acpi_iort_node *node, uint32_t id) 778 { 779 struct acpi_iort_mapping *map = 780 (struct acpi_iort_mapping *)((char *)node + node->mapping_offset); 781 int i; 782 783 for (i = 0; i < node->number_of_mappings; i++) { 784 uint32_t offset = map[i].output_reference; 785 786 if (map[i].flags & ACPI_IORT_MAPPING_SINGLE) { 787 id = map[i].output_base; 788 return acpipci_iort_map(iort, offset, id); 789 } 790 791 /* Mapping encodes number of IDs in the range minus one. */ 792 if (map[i].input_base <= id && 793 id <= map[i].input_base + map[i].number_of_ids) { 794 id = map[i].output_base + (id - map[i].input_base); 795 return acpipci_iort_map(iort, offset, id); 796 } 797 } 798 799 return id; 800 } 801 802 uint32_t 803 acpipci_iort_map(struct acpi_iort *iort, uint32_t offset, uint32_t id) 804 { 805 struct acpi_iort_node *node = 806 (struct acpi_iort_node *)((char *)iort + offset); 807 808 switch (node->type) { 809 case ACPI_IORT_ITS: 810 return id; 811 case ACPI_IORT_SMMU: 812 return acpipci_iort_map_node(iort, node, id); 813 } 814 815 return id; 816 } 817 818 uint32_t 819 acpipci_iort_map_msi(pci_chipset_tag_t pc, pcitag_t tag) 820 { 821 struct acpipci_softc *sc = pc->pc_intr_v; 822 struct acpi_table_header *hdr; 823 struct acpi_iort *iort = NULL; 824 struct acpi_iort_node *node; 825 struct acpi_iort_rc_node *rc; 826 struct acpi_q *entry; 827 uint32_t rid, offset; 828 int i; 829 830 rid = pci_requester_id(pc, tag); 831 832 /* Look for IORT table. */ 833 SIMPLEQ_FOREACH(entry, &sc->sc_acpi->sc_tables, q_next) { 834 hdr = entry->q_table; 835 if (strncmp(hdr->signature, IORT_SIG, 836 sizeof(hdr->signature)) == 0) { 837 iort = entry->q_table; 838 break; 839 } 840 } 841 if (iort == NULL) 842 return rid; 843 844 /* Find our root complex and map. */ 845 offset = iort->offset; 846 for (i = 0; i < iort->number_of_nodes; i++) { 847 node = (struct acpi_iort_node *)((char *)iort + offset); 848 switch (node->type) { 849 case ACPI_IORT_ROOT_COMPLEX: 850 rc = (struct acpi_iort_rc_node *)&node[1]; 851 if (rc->segment == sc->sc_seg) 852 return acpipci_iort_map_node(iort, node, rid); 853 break; 854 } 855 offset += node->length; 856 } 857 858 return rid; 859 } 860