1 /* $NetBSD: acpi_pci_link.c,v 1.18 2010/04/23 15:52:26 jruoho Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 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 AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, 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: acpi_pci_link.c,v 1.18 2010/04/23 15:52:26 jruoho Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/malloc.h> 34 #include <sys/queue.h> 35 #include <sys/reboot.h> 36 #include <sys/systm.h> 37 38 #include <dev/acpi/acpireg.h> 39 #include <dev/acpi/acpivar.h> 40 41 #include <dev/pci/pcireg.h> 42 43 #include "opt_acpi.h" 44 45 46 #define _COMPONENT ACPI_BUS_COMPONENT 47 ACPI_MODULE_NAME ("acpi_pci_link") 48 49 MALLOC_DECLARE(M_ACPI); 50 51 #define NUM_ISA_INTERRUPTS 16 52 #define NUM_ACPI_INTERRUPTS 256 53 54 #define PCI_INVALID_IRQ 255 55 #define PCI_INTERRUPT_VALID(x) ((x) != PCI_INVALID_IRQ && (x) != 0) 56 57 #define ACPI_SERIAL_BEGIN(x) 58 #define ACPI_SERIAL_END(x) 59 60 /* 61 * An ACPI PCI link device may contain multiple links. Each link has its 62 * own ACPI resource. _PRT entries specify which link is being used via 63 * the Source Index. 64 * 65 * XXX: A note about Source Indices and DPFs: Currently we assume that 66 * the DPF start and end tags are not counted towards the index that 67 * Source Index corresponds to. Also, we assume that when DPFs are in use 68 * they various sets overlap in terms of Indices. Here's an example 69 * resource list indicating these assumptions: 70 * 71 * Resource Index 72 * -------- ----- 73 * I/O Port 0 74 * Start DPF - 75 * IRQ 1 76 * MemIO 2 77 * Start DPF - 78 * IRQ 1 79 * MemIO 2 80 * End DPF - 81 * DMA Channel 3 82 * 83 * The XXX is because I'm not sure if this is a valid assumption to make. 84 */ 85 86 /* States during DPF processing. */ 87 #define DPF_OUTSIDE 0 88 #define DPF_FIRST 1 89 #define DPF_IGNORE 2 90 91 struct link; 92 93 struct acpi_pci_link_softc { 94 int pl_num_links; 95 int pl_crs_bad; 96 struct link *pl_links; 97 char pl_name[32]; 98 ACPI_HANDLE pl_handle; 99 TAILQ_ENTRY(acpi_pci_link_softc) pl_list; 100 }; 101 102 static TAILQ_HEAD(, acpi_pci_link_softc) acpi_pci_linkdevs = 103 TAILQ_HEAD_INITIALIZER(acpi_pci_linkdevs); 104 105 106 struct link { 107 struct acpi_pci_link_softc *l_sc; 108 uint8_t l_bios_irq; 109 uint8_t l_irq; 110 uint8_t l_trig; 111 uint8_t l_pol; 112 uint8_t l_initial_irq; 113 int l_res_index; 114 int l_num_irqs; 115 int *l_irqs; 116 int l_references; 117 int l_dev_count; 118 pcitag_t *l_devices; 119 int l_routed:1; 120 int l_isa_irq:1; 121 ACPI_RESOURCE l_prs_template; 122 }; 123 124 struct link_count_request { 125 int in_dpf; 126 int count; 127 }; 128 129 struct link_res_request { 130 struct acpi_pci_link_softc *sc; 131 int in_dpf; 132 int res_index; 133 int link_index; 134 }; 135 136 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS]; 137 static int pci_link_bios_isa_irqs; 138 139 static ACPI_STATUS acpi_count_irq_resources(ACPI_RESOURCE *, void *); 140 static ACPI_STATUS link_add_crs(ACPI_RESOURCE *, void *); 141 static ACPI_STATUS link_add_prs(ACPI_RESOURCE *, void *); 142 static int link_valid_irq(struct link *, int); 143 static void acpi_pci_link_dump(struct acpi_pci_link_softc *); 144 static int acpi_pci_link_attach(struct acpi_pci_link_softc *); 145 static uint8_t acpi_pci_link_search_irq(struct acpi_pci_link_softc *, int, int, 146 int); 147 static struct link *acpi_pci_link_lookup(struct acpi_pci_link_softc *, int); 148 static ACPI_STATUS acpi_pci_link_srs(struct acpi_pci_link_softc *, 149 ACPI_BUFFER *); 150 static ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *, ACPI_RESOURCE *); 151 152 static ACPI_STATUS 153 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context) 154 { 155 struct link_count_request *req; 156 157 req = (struct link_count_request *)context; 158 switch (res->Type) { 159 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 160 switch (req->in_dpf) { 161 case DPF_OUTSIDE: 162 /* We've started the first DPF. */ 163 req->in_dpf = DPF_FIRST; 164 break; 165 case DPF_FIRST: 166 /* We've started the second DPF. */ 167 req->in_dpf = DPF_IGNORE; 168 break; 169 } 170 break; 171 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 172 /* We are finished with DPF parsing. */ 173 KASSERT(req->in_dpf != DPF_OUTSIDE); 174 req->in_dpf = DPF_OUTSIDE; 175 break; 176 case ACPI_RESOURCE_TYPE_IRQ: 177 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 178 /* 179 * Don't count resources if we are in a DPF set that we are 180 * ignoring. 181 */ 182 if (req->in_dpf != DPF_IGNORE) 183 req->count++; 184 } 185 return (AE_OK); 186 } 187 188 static ACPI_STATUS 189 link_add_crs(ACPI_RESOURCE *res, void *context) 190 { 191 struct link_res_request *req; 192 struct link *link; 193 194 req = (struct link_res_request *)context; 195 switch (res->Type) { 196 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 197 switch (req->in_dpf) { 198 case DPF_OUTSIDE: 199 /* We've started the first DPF. */ 200 req->in_dpf = DPF_FIRST; 201 break; 202 case DPF_FIRST: 203 /* We've started the second DPF. */ 204 panic( 205 "%s: Multiple dependent functions within a current resource", 206 __func__); 207 break; 208 } 209 break; 210 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 211 /* We are finished with DPF parsing. */ 212 KASSERT(req->in_dpf != DPF_OUTSIDE); 213 req->in_dpf = DPF_OUTSIDE; 214 break; 215 case ACPI_RESOURCE_TYPE_IRQ: 216 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 217 KASSERT(req->link_index < req->sc->pl_num_links); 218 link = &req->sc->pl_links[req->link_index]; 219 link->l_res_index = req->res_index; 220 req->link_index++; 221 req->res_index++; 222 223 /* 224 * Only use the current value if there's one IRQ. Some 225 * systems return multiple IRQs (which is nonsense for _CRS) 226 * when the link hasn't been programmed. 227 */ 228 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) { 229 if (res->Data.Irq.InterruptCount == 1) { 230 link->l_irq = res->Data.Irq.Interrupts[0]; 231 link->l_trig = res->Data.Irq.Triggering; 232 link->l_pol = res->Data.Irq.Polarity; 233 } 234 } else if (res->Data.ExtendedIrq.InterruptCount == 1) { 235 link->l_irq = res->Data.ExtendedIrq.Interrupts[0]; 236 link->l_trig = res->Data.ExtendedIrq.Triggering; 237 link->l_pol = res->Data.ExtendedIrq.Polarity; 238 } 239 240 /* 241 * An IRQ of zero means that the link isn't routed. 242 */ 243 if (link->l_irq == 0) 244 link->l_irq = PCI_INVALID_IRQ; 245 break; 246 default: 247 req->res_index++; 248 } 249 return (AE_OK); 250 } 251 252 /* 253 * Populate the set of possible IRQs for each device. 254 */ 255 static ACPI_STATUS 256 link_add_prs(ACPI_RESOURCE *res, void *context) 257 { 258 struct link_res_request *req; 259 struct link *link; 260 uint8_t *irqs = NULL; 261 uint32_t *ext_irqs = NULL; 262 int i, is_ext_irq = 1; 263 264 req = (struct link_res_request *)context; 265 switch (res->Type) { 266 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 267 switch (req->in_dpf) { 268 case DPF_OUTSIDE: 269 /* We've started the first DPF. */ 270 req->in_dpf = DPF_FIRST; 271 break; 272 case DPF_FIRST: 273 /* We've started the second DPF. */ 274 req->in_dpf = DPF_IGNORE; 275 break; 276 } 277 break; 278 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 279 /* We are finished with DPF parsing. */ 280 KASSERT(req->in_dpf != DPF_OUTSIDE); 281 req->in_dpf = DPF_OUTSIDE; 282 break; 283 case ACPI_RESOURCE_TYPE_IRQ: 284 is_ext_irq = 0; 285 /* fall through */ 286 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 287 /* 288 * Don't parse resources if we are in a DPF set that we are 289 * ignoring. 290 */ 291 if (req->in_dpf == DPF_IGNORE) 292 break; 293 294 KASSERT(req->link_index < req->sc->pl_num_links); 295 link = &req->sc->pl_links[req->link_index]; 296 if (link->l_res_index == -1) { 297 KASSERT(req->sc->pl_crs_bad); 298 link->l_res_index = req->res_index; 299 } 300 req->link_index++; 301 req->res_index++; 302 303 /* 304 * Stash a copy of the resource for later use when 305 * doing _SRS. 306 * 307 * Note that in theory res->Length may exceed the size 308 * of ACPI_RESOURCE, due to variable length lists in 309 * subtypes. However, all uses of l_prs_template only 310 * rely on lists lengths of zero or one, for which 311 * sizeof(ACPI_RESOURCE) is sufficient space anyway. 312 * We cannot read longer than Length bytes, in case we 313 * read off the end of mapped memory. So we read 314 * whichever length is shortest, Length or 315 * sizeof(ACPI_RESOURCE). 316 */ 317 KASSERT(res->Length >= ACPI_RS_SIZE_MIN); 318 319 memset(&link->l_prs_template, 0, sizeof(link->l_prs_template)); 320 memcpy(&link->l_prs_template, res, 321 MIN(res->Length, sizeof(link->l_prs_template))); 322 323 if (is_ext_irq) { 324 link->l_num_irqs = 325 res->Data.ExtendedIrq.InterruptCount; 326 link->l_trig = res->Data.ExtendedIrq.Triggering; 327 link->l_pol = res->Data.ExtendedIrq.Polarity; 328 ext_irqs = res->Data.ExtendedIrq.Interrupts; 329 } else { 330 link->l_num_irqs = res->Data.Irq.InterruptCount; 331 link->l_trig = res->Data.Irq.Triggering; 332 link->l_pol = res->Data.Irq.Polarity; 333 irqs = res->Data.Irq.Interrupts; 334 } 335 if (link->l_num_irqs == 0) 336 break; 337 338 /* 339 * Save a list of the valid IRQs. Also, if all of the 340 * valid IRQs are ISA IRQs, then mark this link as 341 * routed via an ISA interrupt. 342 */ 343 link->l_isa_irq = TRUE; 344 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs, 345 M_ACPI, M_WAITOK | M_ZERO); 346 for (i = 0; i < link->l_num_irqs; i++) { 347 if (is_ext_irq) { 348 link->l_irqs[i] = ext_irqs[i]; 349 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS) 350 link->l_isa_irq = FALSE; 351 } else { 352 link->l_irqs[i] = irqs[i]; 353 if (irqs[i] >= NUM_ISA_INTERRUPTS) 354 link->l_isa_irq = FALSE; 355 } 356 } 357 break; 358 default: 359 if (req->in_dpf == DPF_IGNORE) 360 break; 361 if (req->sc->pl_crs_bad) 362 aprint_normal("%s: Warning: possible resource %d " 363 "will be lost during _SRS\n", req->sc->pl_name, 364 req->res_index); 365 req->res_index++; 366 } 367 return (AE_OK); 368 } 369 370 static int 371 link_valid_irq(struct link *link, int irq) 372 { 373 int i; 374 375 /* Invalid interrupts are never valid. */ 376 if (!PCI_INTERRUPT_VALID(irq)) 377 return (FALSE); 378 379 /* Any interrupt in the list of possible interrupts is valid. */ 380 for (i = 0; i < link->l_num_irqs; i++) 381 if (link->l_irqs[i] == irq) 382 return (TRUE); 383 384 /* 385 * For links routed via an ISA interrupt, if the SCI is routed via 386 * an ISA interrupt, the SCI is always treated as a valid IRQ. 387 */ 388 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq && 389 irq < NUM_ISA_INTERRUPTS) 390 return (TRUE); 391 392 /* If the interrupt wasn't found in the list it is not valid. */ 393 return (FALSE); 394 } 395 396 void 397 acpi_pci_link_state(void) 398 { 399 struct acpi_pci_link_softc *sc; 400 401 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) { 402 acpi_pci_link_dump(sc); 403 } 404 } 405 406 static void 407 acpi_pci_link_dump(struct acpi_pci_link_softc *sc) 408 { 409 struct link *link; 410 int i, j; 411 412 printf("Link Device %s:\n", sc->pl_name); 413 printf("Index IRQ Rtd Ref IRQs\n"); 414 for (i = 0; i < sc->pl_num_links; i++) { 415 link = &sc->pl_links[i]; 416 printf("%5d %3d %c %3d ", i, link->l_irq, 417 link->l_routed ? 'Y' : 'N', link->l_references); 418 if (link->l_num_irqs == 0) 419 printf(" none"); 420 else for (j = 0; j < link->l_num_irqs; j++) 421 printf(" %d", link->l_irqs[j]); 422 printf(" polarity %u trigger %u\n", link->l_pol, link->l_trig); 423 } 424 printf("\n"); 425 } 426 427 static int 428 acpi_pci_link_attach(struct acpi_pci_link_softc *sc) 429 { 430 struct link_count_request creq; 431 struct link_res_request rreq; 432 ACPI_STATUS status; 433 int i; 434 435 ACPI_SERIAL_BEGIN(pci_link); 436 437 /* 438 * Count the number of current resources so we know how big of 439 * a link array to allocate. On some systems, _CRS is broken, 440 * so for those systems try to derive the count from _PRS instead. 441 */ 442 creq.in_dpf = DPF_OUTSIDE; 443 creq.count = 0; 444 status = AcpiWalkResources(sc->pl_handle, "_CRS", 445 acpi_count_irq_resources, &creq); 446 sc->pl_crs_bad = ACPI_FAILURE(status); 447 if (sc->pl_crs_bad) { 448 creq.in_dpf = DPF_OUTSIDE; 449 creq.count = 0; 450 status = AcpiWalkResources(sc->pl_handle, "_PRS", 451 acpi_count_irq_resources, &creq); 452 if (ACPI_FAILURE(status)) { 453 aprint_error("%s: Unable to parse _CRS or _PRS: %s\n", 454 sc->pl_name, AcpiFormatException(status)); 455 ACPI_SERIAL_END(pci_link); 456 return (ENXIO); 457 } 458 } 459 sc->pl_num_links = creq.count; 460 if (creq.count == 0) { 461 ACPI_SERIAL_END(pci_link); 462 return (0); 463 } 464 sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links, 465 M_ACPI, M_WAITOK | M_ZERO); 466 467 /* Initialize the child links. */ 468 for (i = 0; i < sc->pl_num_links; i++) { 469 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 470 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ; 471 sc->pl_links[i].l_sc = sc; 472 sc->pl_links[i].l_isa_irq = FALSE; 473 sc->pl_links[i].l_res_index = -1; 474 sc->pl_links[i].l_dev_count = 0; 475 sc->pl_links[i].l_devices = NULL; 476 } 477 478 /* Try to read the current settings from _CRS if it is valid. */ 479 if (!sc->pl_crs_bad) { 480 rreq.in_dpf = DPF_OUTSIDE; 481 rreq.link_index = 0; 482 rreq.res_index = 0; 483 rreq.sc = sc; 484 status = AcpiWalkResources(sc->pl_handle, "_CRS", 485 link_add_crs, &rreq); 486 if (ACPI_FAILURE(status)) { 487 aprint_error("%s: Unable to parse _CRS: %s\n", 488 sc->pl_name, AcpiFormatException(status)); 489 goto fail; 490 } 491 } 492 493 /* 494 * Try to read the possible settings from _PRS. Note that if the 495 * _CRS is toast, we depend on having a working _PRS. However, if 496 * _CRS works, then it is ok for _PRS to be missing. 497 */ 498 rreq.in_dpf = DPF_OUTSIDE; 499 rreq.link_index = 0; 500 rreq.res_index = 0; 501 rreq.sc = sc; 502 status = AcpiWalkResources(sc->pl_handle, "_PRS", 503 link_add_prs, &rreq); 504 if (ACPI_FAILURE(status) && 505 (status != AE_NOT_FOUND || sc->pl_crs_bad)) { 506 aprint_error("%s: Unable to parse _PRS: %s\n", 507 sc->pl_name, AcpiFormatException(status)); 508 goto fail; 509 } 510 if (boothowto & AB_VERBOSE) { 511 aprint_normal("%s: Links after initial probe:\n", sc->pl_name); 512 acpi_pci_link_dump(sc); 513 } 514 515 /* Verify initial IRQs if we have _PRS. */ 516 if (status != AE_NOT_FOUND) 517 for (i = 0; i < sc->pl_num_links; i++) 518 if (!link_valid_irq(&sc->pl_links[i], 519 sc->pl_links[i].l_irq)) 520 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 521 if (boothowto & AB_VERBOSE) { 522 printf("%s: Links after initial validation:\n", sc->pl_name); 523 acpi_pci_link_dump(sc); 524 } 525 526 /* Save initial IRQs. */ 527 for (i = 0; i < sc->pl_num_links; i++) 528 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq; 529 530 /* 531 * Try to disable this link. If successful, set the current IRQ to 532 * zero and flags to indicate this link is not routed. If we can't 533 * run _DIS (i.e., the method doesn't exist), assume the initial 534 * IRQ was routed by the BIOS. 535 */ 536 #if 0 /* XXX causes spontaneaous resets on some systems. Disabled for now. */ 537 if (ACPI_SUCCESS(AcpiEvaluateObject(sc->pl_handle, "_DIS", NULL, 538 NULL))) 539 for (i = 0; i < sc->pl_num_links; i++) 540 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 541 else 542 #endif 543 for (i = 0; i < sc->pl_num_links; i++) 544 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq)) 545 sc->pl_links[i].l_routed = TRUE; 546 if (boothowto & AB_VERBOSE) { 547 printf("%s: Links after disable:\n", sc->pl_name); 548 acpi_pci_link_dump(sc); 549 } 550 ACPI_SERIAL_END(pci_link); 551 return (0); 552 fail: 553 ACPI_SERIAL_END(pci_link); 554 for (i = 0; i < sc->pl_num_links; i++) { 555 if (sc->pl_links[i].l_irqs != NULL) 556 free(sc->pl_links[i].l_irqs, M_ACPI); 557 if (sc->pl_links[i].l_devices != NULL) 558 free(sc->pl_links[i].l_devices, M_ACPI); 559 } 560 free(sc->pl_links, M_ACPI); 561 return (ENXIO); 562 } 563 564 static void 565 acpi_pci_link_add_functions(struct acpi_pci_link_softc *sc, struct link *link, 566 int bus, int device, int pin) 567 { 568 uint32_t value; 569 uint8_t func, maxfunc, ipin; 570 pcitag_t tag; 571 572 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, 0); 573 /* See if we have a valid device at function 0. */ 574 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_BHLC_REG); 575 if (PCI_HDRTYPE_TYPE(value) > PCI_HDRTYPE_PCB) 576 return; 577 if (PCI_HDRTYPE_MULTIFN(value)) 578 maxfunc = 7; 579 else 580 maxfunc = 0; 581 582 /* Scan all possible functions at this device. */ 583 for (func = 0; func <= maxfunc; func++) { 584 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, func); 585 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_ID_REG); 586 if (PCI_VENDOR(value) == 0xffff) 587 continue; 588 value = pci_conf_read(acpi_softc->sc_pc, tag, 589 PCI_INTERRUPT_REG); 590 ipin = PCI_INTERRUPT_PIN(value); 591 /* 592 * See if it uses the pin in question. Note that the passed 593 * in pin uses 0 for A, .. 3 for D whereas the intpin 594 * register uses 0 for no interrupt, 1 for A, .. 4 for D. 595 */ 596 if (ipin != pin + 1) 597 continue; 598 599 link->l_devices = realloc(link->l_devices, 600 sizeof(pcitag_t) * (link->l_dev_count + 1), 601 M_ACPI, M_WAITOK); 602 link->l_devices[link->l_dev_count] = tag; 603 ++link->l_dev_count; 604 } 605 } 606 607 static uint8_t 608 acpi_pci_link_search_irq(struct acpi_pci_link_softc *sc, int bus, int device, 609 int pin) 610 { 611 uint32_t value; 612 uint8_t func, maxfunc, ipin, iline; 613 pcitag_t tag; 614 615 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, 0); 616 /* See if we have a valid device at function 0. */ 617 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_BHLC_REG); 618 if (PCI_HDRTYPE_TYPE(value) > PCI_HDRTYPE_PCB) 619 return (PCI_INVALID_IRQ); 620 if (PCI_HDRTYPE_MULTIFN(value)) 621 maxfunc = 7; 622 else 623 maxfunc = 0; 624 625 /* Scan all possible functions at this device. */ 626 for (func = 0; func <= maxfunc; func++) { 627 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, func); 628 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_ID_REG); 629 if (PCI_VENDOR(value) == 0xffff) 630 continue; 631 value = pci_conf_read(acpi_softc->sc_pc, tag, 632 PCI_INTERRUPT_REG); 633 ipin = PCI_INTERRUPT_PIN(value); 634 iline = PCI_INTERRUPT_LINE(value); 635 636 /* 637 * See if it uses the pin in question. Note that the passed 638 * in pin uses 0 for A, .. 3 for D whereas the intpin 639 * register uses 0 for no interrupt, 1 for A, .. 4 for D. 640 */ 641 if (ipin != pin + 1) 642 continue; 643 aprint_verbose( 644 "%s: ACPI: Found matching pin for %d.%d.INT%c" 645 " at func %d: %d\n", 646 sc->pl_name, bus, device, pin + 'A', func, iline); 647 if (PCI_INTERRUPT_VALID(iline)) 648 return (iline); 649 } 650 return (PCI_INVALID_IRQ); 651 } 652 653 /* 654 * Find the link structure that corresponds to the resource index passed in 655 * via 'source_index'. 656 */ 657 static struct link * 658 acpi_pci_link_lookup(struct acpi_pci_link_softc *sc, int source_index) 659 { 660 int i; 661 662 for (i = 0; i < sc->pl_num_links; i++) 663 if (sc->pl_links[i].l_res_index == source_index) 664 return (&sc->pl_links[i]); 665 return (NULL); 666 } 667 668 void 669 acpi_pci_link_add_reference(void *v, int index, int bus, int slot, int pin) 670 { 671 struct acpi_pci_link_softc *sc = v; 672 struct link *link; 673 uint8_t bios_irq; 674 675 /* Bump the reference count. */ 676 ACPI_SERIAL_BEGIN(pci_link); 677 link = acpi_pci_link_lookup(sc, index); 678 if (link == NULL) { 679 printf("%s: apparently invalid index %d\n", sc->pl_name, index); 680 ACPI_SERIAL_END(pci_link); 681 return; 682 } 683 link->l_references++; 684 acpi_pci_link_add_functions(sc, link, bus, slot, pin); 685 if (link->l_routed) 686 pci_link_interrupt_weights[link->l_irq]++; 687 688 /* 689 * The BIOS only routes interrupts via ISA IRQs using the ATPICs 690 * (8259As). Thus, if this link is routed via an ISA IRQ, go 691 * look to see if the BIOS routed an IRQ for this link at the 692 * indicated (bus, slot, pin). If so, we prefer that IRQ for 693 * this link and add that IRQ to our list of known-good IRQs. 694 * This provides a good work-around for link devices whose _CRS 695 * method is either broken or bogus. We only use the value 696 * returned by _CRS if we can't find a valid IRQ via this method 697 * in fact. 698 * 699 * If this link is not routed via an ISA IRQ (because we are using 700 * APIC for example), then don't bother looking up the BIOS IRQ 701 * as if we find one it won't be valid anyway. 702 */ 703 if (!link->l_isa_irq) { 704 ACPI_SERIAL_END(pci_link); 705 return; 706 } 707 708 /* Try to find a BIOS IRQ setting from any matching devices. */ 709 bios_irq = acpi_pci_link_search_irq(sc, bus, slot, pin); 710 if (!PCI_INTERRUPT_VALID(bios_irq)) { 711 ACPI_SERIAL_END(pci_link); 712 return; 713 } 714 715 /* Validate the BIOS IRQ. */ 716 if (!link_valid_irq(link, bios_irq)) { 717 printf("%s: BIOS IRQ %u for %d.%d.INT%c is invalid\n", 718 sc->pl_name, bios_irq, (int)bus, slot, pin + 'A'); 719 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) { 720 link->l_bios_irq = bios_irq; 721 if (bios_irq < NUM_ISA_INTERRUPTS) 722 pci_link_bios_isa_irqs |= (1 << bios_irq); 723 if (bios_irq != link->l_initial_irq && 724 PCI_INTERRUPT_VALID(link->l_initial_irq)) 725 printf( 726 "%s: BIOS IRQ %u does not match initial IRQ %u\n", 727 sc->pl_name, bios_irq, link->l_initial_irq); 728 } else if (bios_irq != link->l_bios_irq) 729 printf( 730 "%s: BIOS IRQ %u for %d.%d.INT%c does not match " 731 "previous BIOS IRQ %u\n", 732 sc->pl_name, bios_irq, (int)bus, slot, pin + 'A', 733 link->l_bios_irq); 734 ACPI_SERIAL_END(pci_link); 735 } 736 737 static ACPI_STATUS 738 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf) 739 { 740 ACPI_RESOURCE *resource, *end, newres, *resptr; 741 ACPI_BUFFER crsbuf; 742 ACPI_STATUS status; 743 struct link *link; 744 int i, in_dpf; 745 746 /* Fetch the _CRS. */ 747 crsbuf.Pointer = NULL; 748 crsbuf.Length = ACPI_ALLOCATE_LOCAL_BUFFER; 749 status = AcpiGetCurrentResources(sc->pl_handle, &crsbuf); 750 if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL) 751 status = AE_NO_MEMORY; 752 if (ACPI_FAILURE(status)) { 753 aprint_verbose("%s: Unable to fetch current resources: %s\n", 754 sc->pl_name, AcpiFormatException(status)); 755 return (status); 756 } 757 758 /* Fill in IRQ resources via link structures. */ 759 srsbuf->Pointer = NULL; 760 link = sc->pl_links; 761 i = 0; 762 in_dpf = DPF_OUTSIDE; 763 resource = (ACPI_RESOURCE *)crsbuf.Pointer; 764 end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length); 765 for (;;) { 766 switch (resource->Type) { 767 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 768 switch (in_dpf) { 769 case DPF_OUTSIDE: 770 /* We've started the first DPF. */ 771 in_dpf = DPF_FIRST; 772 break; 773 case DPF_FIRST: 774 /* We've started the second DPF. */ 775 panic( 776 "%s: Multiple dependent functions within a current resource", 777 __func__); 778 break; 779 } 780 resptr = NULL; 781 break; 782 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 783 /* We are finished with DPF parsing. */ 784 KASSERT(in_dpf != DPF_OUTSIDE); 785 in_dpf = DPF_OUTSIDE; 786 resptr = NULL; 787 break; 788 case ACPI_RESOURCE_TYPE_IRQ: 789 newres = link->l_prs_template; 790 resptr = &newres; 791 resptr->Data.Irq.InterruptCount = 1; 792 if (PCI_INTERRUPT_VALID(link->l_irq)) { 793 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS); 794 resptr->Data.Irq.Interrupts[0] = link->l_irq; 795 resptr->Data.Irq.Triggering = link->l_trig; 796 resptr->Data.Irq.Polarity = link->l_pol; 797 } else 798 resptr->Data.Irq.Interrupts[0] = 0; 799 link++; 800 i++; 801 break; 802 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 803 newres = link->l_prs_template; 804 resptr = &newres; 805 resptr->Data.ExtendedIrq.InterruptCount = 1; 806 if (PCI_INTERRUPT_VALID(link->l_irq)) { 807 resptr->Data.ExtendedIrq.Interrupts[0] = 808 link->l_irq; 809 resptr->Data.ExtendedIrq.Triggering = 810 link->l_trig; 811 resptr->Data.ExtendedIrq.Polarity = link->l_pol; 812 } else 813 resptr->Data.ExtendedIrq.Interrupts[0] = 0; 814 link++; 815 i++; 816 break; 817 default: 818 resptr = resource; 819 } 820 if (resptr != NULL) { 821 status = acpi_AppendBufferResource(srsbuf, resptr); 822 if (ACPI_FAILURE(status)) { 823 printf("%s: Unable to build resources: %s\n", 824 sc->pl_name, AcpiFormatException(status)); 825 if (srsbuf->Pointer != NULL) 826 ACPI_FREE(srsbuf->Pointer); 827 ACPI_FREE(crsbuf.Pointer); 828 return (status); 829 } 830 } 831 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG) 832 break; 833 resource = ACPI_NEXT_RESOURCE(resource); 834 if (resource >= end) 835 break; 836 } 837 ACPI_FREE(crsbuf.Pointer); 838 return (AE_OK); 839 } 840 841 static ACPI_STATUS 842 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc, 843 ACPI_BUFFER *srsbuf) 844 { 845 ACPI_RESOURCE newres; 846 ACPI_STATUS status; 847 struct link *link; 848 int i; 849 850 /* Start off with an empty buffer. */ 851 srsbuf->Pointer = NULL; 852 link = sc->pl_links; 853 for (i = 0; i < sc->pl_num_links; i++) { 854 855 /* Add a new IRQ resource from each link. */ 856 link = &sc->pl_links[i]; 857 newres = link->l_prs_template; 858 if (newres.Type == ACPI_RESOURCE_TYPE_IRQ) { 859 860 /* Build an IRQ resource. */ 861 newres.Data.Irq.InterruptCount = 1; 862 if (PCI_INTERRUPT_VALID(link->l_irq)) { 863 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS); 864 newres.Data.Irq.Interrupts[0] = link->l_irq; 865 newres.Data.Irq.Triggering = link->l_trig; 866 newres.Data.Irq.Polarity = link->l_pol; 867 } else 868 newres.Data.Irq.Interrupts[0] = 0; 869 } else { 870 871 /* Build an ExtIRQ resuorce. */ 872 newres.Data.ExtendedIrq.InterruptCount = 1; 873 if (PCI_INTERRUPT_VALID(link->l_irq)) { 874 newres.Data.ExtendedIrq.Interrupts[0] = 875 link->l_irq; 876 newres.Data.ExtendedIrq.Triggering = 877 link->l_trig; 878 newres.Data.ExtendedIrq.Polarity = 879 link->l_pol; 880 } else { 881 newres.Data.ExtendedIrq.Interrupts[0] = 0; 882 } 883 } 884 885 /* Add the new resource to the end of the _SRS buffer. */ 886 status = acpi_AppendBufferResource(srsbuf, &newres); 887 if (ACPI_FAILURE(status)) { 888 printf("%s: Unable to build resources: %s\n", 889 sc->pl_name, AcpiFormatException(status)); 890 if (srsbuf->Pointer != NULL) 891 ACPI_FREE(srsbuf->Pointer); 892 return (status); 893 } 894 } 895 return (AE_OK); 896 } 897 898 static ACPI_STATUS 899 acpi_pci_link_srs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf) 900 { 901 ACPI_STATUS status; 902 903 if (sc->pl_crs_bad) 904 status = acpi_pci_link_srs_from_links(sc, srsbuf); 905 else 906 status = acpi_pci_link_srs_from_crs(sc, srsbuf); 907 908 /* Write out new resources via _SRS. */ 909 return AcpiSetCurrentResources(sc->pl_handle, srsbuf); 910 } 911 912 static ACPI_STATUS 913 acpi_pci_link_route_irqs(struct acpi_pci_link_softc *sc, int *irq, int *pol, 914 int *trig) 915 { 916 ACPI_RESOURCE *resource, *end; 917 ACPI_BUFFER srsbuf; 918 ACPI_STATUS status; 919 struct link *link; 920 int i, is_ext = 0; 921 922 status = acpi_pci_link_srs(sc, &srsbuf); 923 if (ACPI_FAILURE(status)) { 924 printf("%s: _SRS failed: %s\n", 925 sc->pl_name, AcpiFormatException(status)); 926 return (status); 927 } 928 /* 929 * Perform acpi_config_intr() on each IRQ resource if it was just 930 * routed for the first time. 931 */ 932 link = sc->pl_links; 933 i = 0; 934 resource = (ACPI_RESOURCE *)srsbuf.Pointer; 935 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length); 936 for (;;) { 937 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG) 938 break; 939 switch (resource->Type) { 940 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 941 is_ext = 1; 942 /* FALLTHROUGH */ 943 case ACPI_RESOURCE_TYPE_IRQ: 944 /* 945 * Only configure the interrupt and update the 946 * weights if this link has a valid IRQ and was 947 * previously unrouted. 948 */ 949 if (!link->l_routed && 950 PCI_INTERRUPT_VALID(link->l_irq)) { 951 *trig = is_ext ? 952 resource->Data.ExtendedIrq.Triggering : 953 resource->Data.Irq.Triggering; 954 *pol = is_ext ? 955 resource->Data.ExtendedIrq.Polarity : 956 resource->Data.Irq.Polarity; 957 *irq = is_ext ? 958 resource->Data.ExtendedIrq.Interrupts[0] : 959 resource->Data.Irq.Interrupts[0]; 960 link->l_routed = TRUE; 961 pci_link_interrupt_weights[link->l_irq] += 962 link->l_references; 963 } 964 link++; 965 i++; 966 break; 967 } 968 resource = ACPI_NEXT_RESOURCE(resource); 969 if (resource >= end) 970 break; 971 } 972 ACPI_FREE(srsbuf.Pointer); 973 return (AE_OK); 974 } 975 976 /* 977 * Pick an IRQ to use for this unrouted link. 978 */ 979 static uint8_t 980 acpi_pci_link_choose_irq(struct acpi_pci_link_softc *sc, struct link *link) 981 { 982 u_int8_t best_irq, pos_irq; 983 int best_weight, pos_weight, i; 984 985 KASSERT(!link->l_routed); 986 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq)); 987 988 /* 989 * If we have a valid BIOS IRQ, use that. We trust what the BIOS 990 * says it routed over what _CRS says the link thinks is routed. 991 */ 992 if (PCI_INTERRUPT_VALID(link->l_bios_irq)) 993 return (link->l_bios_irq); 994 995 /* 996 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS, 997 * then use that. 998 */ 999 if (PCI_INTERRUPT_VALID(link->l_initial_irq)) 1000 return (link->l_initial_irq); 1001 1002 /* 1003 * Ok, we have no useful hints, so we have to pick from the 1004 * possible IRQs. For ISA IRQs we only use interrupts that 1005 * have already been used by the BIOS. 1006 */ 1007 best_irq = PCI_INVALID_IRQ; 1008 best_weight = INT_MAX; 1009 for (i = 0; i < link->l_num_irqs; i++) { 1010 pos_irq = link->l_irqs[i]; 1011 if (pos_irq < NUM_ISA_INTERRUPTS && 1012 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0) 1013 continue; 1014 pos_weight = pci_link_interrupt_weights[pos_irq]; 1015 if (pos_weight < best_weight) { 1016 best_weight = pos_weight; 1017 best_irq = pos_irq; 1018 } 1019 } 1020 1021 /* 1022 * If this is an ISA IRQ, try using the SCI if it is also an ISA 1023 * interrupt as a fallback. 1024 */ 1025 if (link->l_isa_irq && !PCI_INTERRUPT_VALID(best_irq)) { 1026 pos_irq = AcpiGbl_FADT.SciInterrupt; 1027 pos_weight = pci_link_interrupt_weights[pos_irq]; 1028 if (pos_weight < best_weight) { 1029 best_weight = pos_weight; 1030 best_irq = pos_irq; 1031 } 1032 } 1033 1034 if (PCI_INTERRUPT_VALID(best_irq)) { 1035 aprint_verbose("%s: Picked IRQ %u with weight %d\n", 1036 sc->pl_name, best_irq, best_weight); 1037 } else 1038 printf("%s: Unable to choose an IRQ\n", sc->pl_name); 1039 return (best_irq); 1040 } 1041 1042 int 1043 acpi_pci_link_route_interrupt(void *v, int index, int *irq, int *pol, int *trig) 1044 { 1045 struct acpi_pci_link_softc *sc = v; 1046 struct link *link; 1047 int i; 1048 pcireg_t reg; 1049 1050 ACPI_SERIAL_BEGIN(pci_link); 1051 link = acpi_pci_link_lookup(sc, index); 1052 if (link == NULL) 1053 panic("%s: apparently invalid index %d", __func__, index); 1054 1055 /* 1056 * If this link device is already routed to an interrupt, just return 1057 * the interrupt it is routed to. 1058 */ 1059 if (link->l_routed) { 1060 KASSERT(PCI_INTERRUPT_VALID(link->l_irq)); 1061 ACPI_SERIAL_END(pci_link); 1062 *irq = link->l_irq; 1063 *pol = link->l_pol; 1064 *trig = link->l_trig; 1065 return (link->l_irq); 1066 } 1067 1068 /* Choose an IRQ if we need one. */ 1069 if (PCI_INTERRUPT_VALID(link->l_irq)) { 1070 *irq = link->l_irq; 1071 *pol = link->l_pol; 1072 *trig = link->l_trig; 1073 goto done; 1074 } 1075 1076 link->l_irq = acpi_pci_link_choose_irq(sc, link); 1077 1078 /* 1079 * Try to route the interrupt we picked. If it fails, then 1080 * assume the interrupt is not routed. 1081 */ 1082 if (!PCI_INTERRUPT_VALID(link->l_irq)) 1083 goto done; 1084 1085 acpi_pci_link_route_irqs(sc, irq, pol, trig); 1086 if (!link->l_routed) { 1087 link->l_irq = PCI_INVALID_IRQ; 1088 goto done; 1089 } 1090 1091 link->l_pol = *pol; 1092 link->l_trig = *trig; 1093 for (i = 0; i < link->l_dev_count; ++i) { 1094 reg = pci_conf_read(acpi_softc->sc_pc, link->l_devices[i], 1095 PCI_INTERRUPT_REG); 1096 reg &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT); 1097 reg |= link->l_irq << PCI_INTERRUPT_LINE_SHIFT; 1098 pci_conf_write(acpi_softc->sc_pc, link->l_devices[i], 1099 PCI_INTERRUPT_REG, reg); 1100 } 1101 1102 done: 1103 ACPI_SERIAL_END(pci_link); 1104 1105 return (link->l_irq); 1106 } 1107 1108 /* 1109 * This is gross, but we abuse the identify routine to perform one-time 1110 * SYSINIT() style initialization for the driver. 1111 */ 1112 static void 1113 acpi_pci_link_init(struct acpi_pci_link_softc *sc) 1114 { 1115 ACPI_BUFFER buf; 1116 1117 /* 1118 * If the SCI is an ISA IRQ, add it to the bitmask of known good 1119 * ISA IRQs. 1120 * 1121 * XXX: If we are using the APIC, the SCI might have been 1122 * rerouted to an APIC pin in which case this is invalid. However, 1123 * if we are using the APIC, we also shouldn't be having any PCI 1124 * interrupts routed via ISA IRQs, so this is probably ok. 1125 */ 1126 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS) 1127 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt); 1128 1129 buf.Length = sizeof (sc->pl_name); 1130 buf.Pointer = sc->pl_name; 1131 1132 if (ACPI_FAILURE(AcpiGetName(sc->pl_handle, ACPI_SINGLE_NAME, &buf))) 1133 snprintf(sc->pl_name, sizeof (sc->pl_name), "%s", 1134 "ACPI link device"); 1135 1136 acpi_pci_link_attach(sc); 1137 } 1138 1139 void * 1140 acpi_pci_link_devbyhandle(ACPI_HANDLE handle) 1141 { 1142 struct acpi_pci_link_softc *sc; 1143 1144 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) { 1145 if (sc->pl_handle == handle) 1146 return sc; 1147 } 1148 1149 sc = malloc(sizeof (*sc), M_ACPI, M_NOWAIT | M_ZERO); 1150 if (sc == NULL) 1151 return NULL; 1152 1153 sc->pl_handle = handle; 1154 1155 acpi_pci_link_init(sc); 1156 1157 TAILQ_INSERT_TAIL(&acpi_pci_linkdevs, sc, pl_list); 1158 1159 return (void *)sc; 1160 } 1161 1162 void 1163 acpi_pci_link_resume(void) 1164 { 1165 struct acpi_pci_link_softc *sc; 1166 ACPI_BUFFER srsbuf; 1167 1168 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) { 1169 ACPI_SERIAL_BEGIN(pci_link); 1170 if (ACPI_SUCCESS(acpi_pci_link_srs(sc, &srsbuf))) 1171 ACPI_FREE(srsbuf.Pointer); 1172 ACPI_SERIAL_END(pci_link); 1173 } 1174 } 1175 1176 ACPI_HANDLE 1177 acpi_pci_link_handle(void *v) 1178 { 1179 struct acpi_pci_link_softc *sc = v; 1180 1181 return sc->pl_handle; 1182 } 1183 1184 char * 1185 acpi_pci_link_name(void *v) 1186 { 1187 struct acpi_pci_link_softc *sc = v; 1188 1189 return sc->pl_name; 1190 } 1191 1192 1193 /* 1194 * Append an ACPI_RESOURCE to an ACPI_BUFFER. 1195 * 1196 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 1197 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 1198 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 1199 * resources. 1200 */ 1201 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 1202 1203 static ACPI_STATUS 1204 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 1205 { 1206 ACPI_RESOURCE *rp; 1207 void *newp; 1208 1209 /* Initialise the buffer if necessary. */ 1210 if (buf->Pointer == NULL) { 1211 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 1212 if ((buf->Pointer = ACPI_ALLOCATE(buf->Length)) == NULL) 1213 return (AE_NO_MEMORY); 1214 rp = (ACPI_RESOURCE *)buf->Pointer; 1215 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 1216 rp->Length = 0; 1217 } 1218 1219 if (res == NULL) 1220 return (AE_OK); 1221 1222 /* 1223 * Scan the current buffer looking for the terminator. 1224 * This will either find the terminator or hit the end 1225 * of the buffer and return an error. 1226 */ 1227 rp = (ACPI_RESOURCE *)buf->Pointer; 1228 for (;;) { 1229 /* Range check, don't go outside the buffer */ 1230 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + 1231 buf->Length)) 1232 return (AE_BAD_PARAMETER); 1233 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 1234 break; 1235 rp = ACPI_NEXT_RESOURCE(rp); 1236 } 1237 1238 /* 1239 * Check the size of the buffer and expand if required. 1240 * 1241 * Required size is: 1242 * size of existing resources before terminator + 1243 * size of new resource and header + 1244 * size of terminator. 1245 * 1246 * Note that this loop should really only run once, unless 1247 * for some reason we are stuffing a *really* huge resource. 1248 */ 1249 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 1250 res->Length + ACPI_RS_SIZE_NO_DATA + 1251 ACPI_RS_SIZE_MIN) >= buf->Length) { 1252 if ((newp = ACPI_ALLOCATE(buf->Length * 2)) == NULL) 1253 return (AE_NO_MEMORY); 1254 memcpy(newp, buf->Pointer, buf->Length); 1255 rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1256 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 1257 ACPI_FREE(buf->Pointer); 1258 buf->Pointer = newp; 1259 buf->Length += buf->Length; 1260 } 1261 1262 /* Insert the new resource. */ 1263 memcpy(rp, res, res->Length + ACPI_RS_SIZE_NO_DATA); 1264 1265 /* And add the terminator. */ 1266 rp = ACPI_NEXT_RESOURCE(rp); 1267 rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 1268 rp->Length = 0; 1269 1270 return (AE_OK); 1271 } 1272