acpi_pci_link.c revision 1.20 1 /* $NetBSD: acpi_pci_link.c,v 1.20 2013/10/16 17:31:28 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki (at) 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.20 2013/10/16 17:31:28 christos 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 (ACPI_SUCCESS(AcpiEvaluateObject(sc->pl_handle, "_DIS", NULL,
537 NULL)))
538 for (i = 0; i < sc->pl_num_links; i++)
539 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
540 else
541 for (i = 0; i < sc->pl_num_links; i++)
542 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
543 sc->pl_links[i].l_routed = TRUE;
544 if (boothowto & AB_VERBOSE) {
545 printf("%s: Links after disable:\n", sc->pl_name);
546 acpi_pci_link_dump(sc);
547 }
548 ACPI_SERIAL_END(pci_link);
549 return (0);
550 fail:
551 ACPI_SERIAL_END(pci_link);
552 for (i = 0; i < sc->pl_num_links; i++) {
553 if (sc->pl_links[i].l_irqs != NULL)
554 free(sc->pl_links[i].l_irqs, M_ACPI);
555 if (sc->pl_links[i].l_devices != NULL)
556 free(sc->pl_links[i].l_devices, M_ACPI);
557 }
558 free(sc->pl_links, M_ACPI);
559 return (ENXIO);
560 }
561
562 static void
563 acpi_pci_link_add_functions(struct acpi_pci_link_softc *sc, struct link *link,
564 int bus, int device, int pin)
565 {
566 uint32_t value;
567 uint8_t func, maxfunc, ipin;
568 pcitag_t tag;
569
570 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, 0);
571 /* See if we have a valid device at function 0. */
572 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_BHLC_REG);
573 if (PCI_HDRTYPE_TYPE(value) > PCI_HDRTYPE_PCB)
574 return;
575 if (PCI_HDRTYPE_MULTIFN(value))
576 maxfunc = 7;
577 else
578 maxfunc = 0;
579
580 /* Scan all possible functions at this device. */
581 for (func = 0; func <= maxfunc; func++) {
582 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, func);
583 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_ID_REG);
584 if (PCI_VENDOR(value) == 0xffff)
585 continue;
586 value = pci_conf_read(acpi_softc->sc_pc, tag,
587 PCI_INTERRUPT_REG);
588 ipin = PCI_INTERRUPT_PIN(value);
589 /*
590 * See if it uses the pin in question. Note that the passed
591 * in pin uses 0 for A, .. 3 for D whereas the intpin
592 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
593 */
594 if (ipin != pin + 1)
595 continue;
596
597 link->l_devices = realloc(link->l_devices,
598 sizeof(pcitag_t) * (link->l_dev_count + 1),
599 M_ACPI, M_WAITOK);
600 link->l_devices[link->l_dev_count] = tag;
601 ++link->l_dev_count;
602 }
603 }
604
605 static uint8_t
606 acpi_pci_link_search_irq(struct acpi_pci_link_softc *sc, int bus, int device,
607 int pin)
608 {
609 uint32_t value;
610 uint8_t func, maxfunc, ipin, iline;
611 pcitag_t tag;
612
613 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, 0);
614 /* See if we have a valid device at function 0. */
615 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_BHLC_REG);
616 if (PCI_HDRTYPE_TYPE(value) > PCI_HDRTYPE_PCB)
617 return (PCI_INVALID_IRQ);
618 if (PCI_HDRTYPE_MULTIFN(value))
619 maxfunc = 7;
620 else
621 maxfunc = 0;
622
623 /* Scan all possible functions at this device. */
624 for (func = 0; func <= maxfunc; func++) {
625 tag = pci_make_tag(acpi_softc->sc_pc, bus, device, func);
626 value = pci_conf_read(acpi_softc->sc_pc, tag, PCI_ID_REG);
627 if (PCI_VENDOR(value) == 0xffff)
628 continue;
629 value = pci_conf_read(acpi_softc->sc_pc, tag,
630 PCI_INTERRUPT_REG);
631 ipin = PCI_INTERRUPT_PIN(value);
632 iline = PCI_INTERRUPT_LINE(value);
633
634 /*
635 * See if it uses the pin in question. Note that the passed
636 * in pin uses 0 for A, .. 3 for D whereas the intpin
637 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
638 */
639 if (ipin != pin + 1)
640 continue;
641 aprint_verbose(
642 "%s: ACPI: Found matching pin for %d.%d.INT%c"
643 " at func %d: %d\n",
644 sc->pl_name, bus, device, pin + 'A', func, iline);
645 if (PCI_INTERRUPT_VALID(iline))
646 return (iline);
647 }
648 return (PCI_INVALID_IRQ);
649 }
650
651 /*
652 * Find the link structure that corresponds to the resource index passed in
653 * via 'source_index'.
654 */
655 static struct link *
656 acpi_pci_link_lookup(struct acpi_pci_link_softc *sc, int source_index)
657 {
658 int i;
659
660 for (i = 0; i < sc->pl_num_links; i++)
661 if (sc->pl_links[i].l_res_index == source_index)
662 return (&sc->pl_links[i]);
663 return (NULL);
664 }
665
666 void
667 acpi_pci_link_add_reference(void *v, int index, int bus, int slot, int pin)
668 {
669 struct acpi_pci_link_softc *sc = v;
670 struct link *link;
671 uint8_t bios_irq;
672
673 /* Bump the reference count. */
674 ACPI_SERIAL_BEGIN(pci_link);
675 link = acpi_pci_link_lookup(sc, index);
676 if (link == NULL) {
677 printf("%s: apparently invalid index %d\n", sc->pl_name, index);
678 ACPI_SERIAL_END(pci_link);
679 return;
680 }
681 link->l_references++;
682 acpi_pci_link_add_functions(sc, link, bus, slot, pin);
683 if (link->l_routed)
684 pci_link_interrupt_weights[link->l_irq]++;
685
686 /*
687 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
688 * (8259As). Thus, if this link is routed via an ISA IRQ, go
689 * look to see if the BIOS routed an IRQ for this link at the
690 * indicated (bus, slot, pin). If so, we prefer that IRQ for
691 * this link and add that IRQ to our list of known-good IRQs.
692 * This provides a good work-around for link devices whose _CRS
693 * method is either broken or bogus. We only use the value
694 * returned by _CRS if we can't find a valid IRQ via this method
695 * in fact.
696 *
697 * If this link is not routed via an ISA IRQ (because we are using
698 * APIC for example), then don't bother looking up the BIOS IRQ
699 * as if we find one it won't be valid anyway.
700 */
701 if (!link->l_isa_irq) {
702 ACPI_SERIAL_END(pci_link);
703 return;
704 }
705
706 /* Try to find a BIOS IRQ setting from any matching devices. */
707 bios_irq = acpi_pci_link_search_irq(sc, bus, slot, pin);
708 if (!PCI_INTERRUPT_VALID(bios_irq)) {
709 ACPI_SERIAL_END(pci_link);
710 return;
711 }
712
713 /* Validate the BIOS IRQ. */
714 if (!link_valid_irq(link, bios_irq)) {
715 printf("%s: BIOS IRQ %u for %d.%d.INT%c is invalid\n",
716 sc->pl_name, bios_irq, (int)bus, slot, pin + 'A');
717 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
718 link->l_bios_irq = bios_irq;
719 if (bios_irq < NUM_ISA_INTERRUPTS)
720 pci_link_bios_isa_irqs |= (1 << bios_irq);
721 if (bios_irq != link->l_initial_irq &&
722 PCI_INTERRUPT_VALID(link->l_initial_irq))
723 printf(
724 "%s: BIOS IRQ %u does not match initial IRQ %u\n",
725 sc->pl_name, bios_irq, link->l_initial_irq);
726 } else if (bios_irq != link->l_bios_irq)
727 printf(
728 "%s: BIOS IRQ %u for %d.%d.INT%c does not match "
729 "previous BIOS IRQ %u\n",
730 sc->pl_name, bios_irq, (int)bus, slot, pin + 'A',
731 link->l_bios_irq);
732 ACPI_SERIAL_END(pci_link);
733 }
734
735 static ACPI_STATUS
736 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
737 {
738 ACPI_RESOURCE *resource, *end, newres, *resptr;
739 ACPI_BUFFER crsbuf;
740 ACPI_STATUS status;
741 struct link *link;
742 int i, in_dpf;
743
744 /* Fetch the _CRS. */
745 crsbuf.Pointer = NULL;
746 crsbuf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
747 status = AcpiGetCurrentResources(sc->pl_handle, &crsbuf);
748 if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL)
749 status = AE_NO_MEMORY;
750 if (ACPI_FAILURE(status)) {
751 aprint_verbose("%s: Unable to fetch current resources: %s\n",
752 sc->pl_name, AcpiFormatException(status));
753 return (status);
754 }
755
756 /* Fill in IRQ resources via link structures. */
757 srsbuf->Pointer = NULL;
758 link = sc->pl_links;
759 i = 0;
760 in_dpf = DPF_OUTSIDE;
761 resource = (ACPI_RESOURCE *)crsbuf.Pointer;
762 end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length);
763 for (;;) {
764 switch (resource->Type) {
765 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
766 switch (in_dpf) {
767 case DPF_OUTSIDE:
768 /* We've started the first DPF. */
769 in_dpf = DPF_FIRST;
770 break;
771 case DPF_FIRST:
772 /* We've started the second DPF. */
773 panic(
774 "%s: Multiple dependent functions within a current resource",
775 __func__);
776 break;
777 }
778 resptr = NULL;
779 break;
780 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
781 /* We are finished with DPF parsing. */
782 KASSERT(in_dpf != DPF_OUTSIDE);
783 in_dpf = DPF_OUTSIDE;
784 resptr = NULL;
785 break;
786 case ACPI_RESOURCE_TYPE_IRQ:
787 newres = link->l_prs_template;
788 resptr = &newres;
789 resptr->Data.Irq.InterruptCount = 1;
790 if (PCI_INTERRUPT_VALID(link->l_irq)) {
791 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS);
792 resptr->Data.Irq.Interrupts[0] = link->l_irq;
793 resptr->Data.Irq.Triggering = link->l_trig;
794 resptr->Data.Irq.Polarity = link->l_pol;
795 } else
796 resptr->Data.Irq.Interrupts[0] = 0;
797 link++;
798 i++;
799 break;
800 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
801 newres = link->l_prs_template;
802 resptr = &newres;
803 resptr->Data.ExtendedIrq.InterruptCount = 1;
804 if (PCI_INTERRUPT_VALID(link->l_irq)) {
805 resptr->Data.ExtendedIrq.Interrupts[0] =
806 link->l_irq;
807 resptr->Data.ExtendedIrq.Triggering =
808 link->l_trig;
809 resptr->Data.ExtendedIrq.Polarity = link->l_pol;
810 } else
811 resptr->Data.ExtendedIrq.Interrupts[0] = 0;
812 link++;
813 i++;
814 break;
815 default:
816 resptr = resource;
817 }
818 if (resptr != NULL) {
819 status = acpi_AppendBufferResource(srsbuf, resptr);
820 if (ACPI_FAILURE(status)) {
821 printf("%s: Unable to build resources: %s\n",
822 sc->pl_name, AcpiFormatException(status));
823 if (srsbuf->Pointer != NULL)
824 ACPI_FREE(srsbuf->Pointer);
825 ACPI_FREE(crsbuf.Pointer);
826 return (status);
827 }
828 }
829 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
830 break;
831 resource = ACPI_NEXT_RESOURCE(resource);
832 if (resource >= end)
833 break;
834 }
835 ACPI_FREE(crsbuf.Pointer);
836 return (AE_OK);
837 }
838
839 static ACPI_STATUS
840 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
841 ACPI_BUFFER *srsbuf)
842 {
843 ACPI_RESOURCE newres;
844 ACPI_STATUS status;
845 struct link *link;
846 int i;
847
848 /* Start off with an empty buffer. */
849 srsbuf->Pointer = NULL;
850 link = sc->pl_links;
851 for (i = 0; i < sc->pl_num_links; i++) {
852
853 /* Add a new IRQ resource from each link. */
854 link = &sc->pl_links[i];
855 newres = link->l_prs_template;
856 if (newres.Type == ACPI_RESOURCE_TYPE_IRQ) {
857
858 /* Build an IRQ resource. */
859 newres.Data.Irq.InterruptCount = 1;
860 if (PCI_INTERRUPT_VALID(link->l_irq)) {
861 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS);
862 newres.Data.Irq.Interrupts[0] = link->l_irq;
863 newres.Data.Irq.Triggering = link->l_trig;
864 newres.Data.Irq.Polarity = link->l_pol;
865 } else
866 newres.Data.Irq.Interrupts[0] = 0;
867 } else {
868
869 /* Build an ExtIRQ resuorce. */
870 newres.Data.ExtendedIrq.InterruptCount = 1;
871 if (PCI_INTERRUPT_VALID(link->l_irq)) {
872 newres.Data.ExtendedIrq.Interrupts[0] =
873 link->l_irq;
874 newres.Data.ExtendedIrq.Triggering =
875 link->l_trig;
876 newres.Data.ExtendedIrq.Polarity =
877 link->l_pol;
878 } else {
879 newres.Data.ExtendedIrq.Interrupts[0] = 0;
880 }
881 }
882
883 /* Add the new resource to the end of the _SRS buffer. */
884 status = acpi_AppendBufferResource(srsbuf, &newres);
885 if (ACPI_FAILURE(status)) {
886 printf("%s: Unable to build resources: %s\n",
887 sc->pl_name, AcpiFormatException(status));
888 if (srsbuf->Pointer != NULL)
889 ACPI_FREE(srsbuf->Pointer);
890 return (status);
891 }
892 }
893 return (AE_OK);
894 }
895
896 static ACPI_STATUS
897 acpi_pci_link_srs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
898 {
899 ACPI_STATUS status;
900
901 if (sc->pl_crs_bad)
902 status = acpi_pci_link_srs_from_links(sc, srsbuf);
903 else
904 status = acpi_pci_link_srs_from_crs(sc, srsbuf);
905
906 if (ACPI_FAILURE(status))
907 printf("%s: Unable to find link srs : %s\n",
908 sc->pl_name, AcpiFormatException(status));
909
910 /* Write out new resources via _SRS. */
911 return AcpiSetCurrentResources(sc->pl_handle, srsbuf);
912 }
913
914 static ACPI_STATUS
915 acpi_pci_link_route_irqs(struct acpi_pci_link_softc *sc, int *irq, int *pol,
916 int *trig)
917 {
918 ACPI_RESOURCE *resource, *end;
919 ACPI_BUFFER srsbuf;
920 ACPI_STATUS status;
921 struct link *link;
922 int i, is_ext = 0;
923
924 status = acpi_pci_link_srs(sc, &srsbuf);
925 if (ACPI_FAILURE(status)) {
926 printf("%s: _SRS failed: %s\n",
927 sc->pl_name, AcpiFormatException(status));
928 return (status);
929 }
930 /*
931 * Perform acpi_config_intr() on each IRQ resource if it was just
932 * routed for the first time.
933 */
934 link = sc->pl_links;
935 i = 0;
936 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
937 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
938 for (;;) {
939 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
940 break;
941 switch (resource->Type) {
942 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
943 is_ext = 1;
944 /* FALLTHROUGH */
945 case ACPI_RESOURCE_TYPE_IRQ:
946 /*
947 * Only configure the interrupt and update the
948 * weights if this link has a valid IRQ and was
949 * previously unrouted.
950 */
951 if (!link->l_routed &&
952 PCI_INTERRUPT_VALID(link->l_irq)) {
953 *trig = is_ext ?
954 resource->Data.ExtendedIrq.Triggering :
955 resource->Data.Irq.Triggering;
956 *pol = is_ext ?
957 resource->Data.ExtendedIrq.Polarity :
958 resource->Data.Irq.Polarity;
959 *irq = is_ext ?
960 resource->Data.ExtendedIrq.Interrupts[0] :
961 resource->Data.Irq.Interrupts[0];
962 link->l_routed = TRUE;
963 pci_link_interrupt_weights[link->l_irq] +=
964 link->l_references;
965 }
966 link++;
967 i++;
968 break;
969 }
970 resource = ACPI_NEXT_RESOURCE(resource);
971 if (resource >= end)
972 break;
973 }
974 ACPI_FREE(srsbuf.Pointer);
975 return (AE_OK);
976 }
977
978 /*
979 * Pick an IRQ to use for this unrouted link.
980 */
981 static uint8_t
982 acpi_pci_link_choose_irq(struct acpi_pci_link_softc *sc, struct link *link)
983 {
984 u_int8_t best_irq, pos_irq;
985 int best_weight, pos_weight, i;
986
987 KASSERT(!link->l_routed);
988 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq));
989
990 /*
991 * If we have a valid BIOS IRQ, use that. We trust what the BIOS
992 * says it routed over what _CRS says the link thinks is routed.
993 */
994 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
995 return (link->l_bios_irq);
996
997 /*
998 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
999 * then use that.
1000 */
1001 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
1002 return (link->l_initial_irq);
1003
1004 /*
1005 * Ok, we have no useful hints, so we have to pick from the
1006 * possible IRQs. For ISA IRQs we only use interrupts that
1007 * have already been used by the BIOS.
1008 */
1009 best_irq = PCI_INVALID_IRQ;
1010 best_weight = INT_MAX;
1011 for (i = 0; i < link->l_num_irqs; i++) {
1012 pos_irq = link->l_irqs[i];
1013 if (pos_irq < NUM_ISA_INTERRUPTS &&
1014 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
1015 continue;
1016 pos_weight = pci_link_interrupt_weights[pos_irq];
1017 if (pos_weight < best_weight) {
1018 best_weight = pos_weight;
1019 best_irq = pos_irq;
1020 }
1021 }
1022
1023 /*
1024 * If this is an ISA IRQ, try using the SCI if it is also an ISA
1025 * interrupt as a fallback.
1026 */
1027 if (link->l_isa_irq && !PCI_INTERRUPT_VALID(best_irq)) {
1028 pos_irq = AcpiGbl_FADT.SciInterrupt;
1029 pos_weight = pci_link_interrupt_weights[pos_irq];
1030 if (pos_weight < best_weight) {
1031 best_weight = pos_weight;
1032 best_irq = pos_irq;
1033 }
1034 }
1035
1036 if (PCI_INTERRUPT_VALID(best_irq)) {
1037 aprint_verbose("%s: Picked IRQ %u with weight %d\n",
1038 sc->pl_name, best_irq, best_weight);
1039 } else
1040 printf("%s: Unable to choose an IRQ\n", sc->pl_name);
1041 return (best_irq);
1042 }
1043
1044 int
1045 acpi_pci_link_route_interrupt(void *v, int index, int *irq, int *pol, int *trig)
1046 {
1047 struct acpi_pci_link_softc *sc = v;
1048 struct link *link;
1049 int i;
1050 pcireg_t reg;
1051
1052 ACPI_SERIAL_BEGIN(pci_link);
1053 link = acpi_pci_link_lookup(sc, index);
1054 if (link == NULL)
1055 panic("%s: apparently invalid index %d", __func__, index);
1056
1057 /*
1058 * If this link device is already routed to an interrupt, just return
1059 * the interrupt it is routed to.
1060 */
1061 if (link->l_routed) {
1062 KASSERT(PCI_INTERRUPT_VALID(link->l_irq));
1063 ACPI_SERIAL_END(pci_link);
1064 *irq = link->l_irq;
1065 *pol = link->l_pol;
1066 *trig = link->l_trig;
1067 return (link->l_irq);
1068 }
1069
1070 /* Choose an IRQ if we need one. */
1071 if (PCI_INTERRUPT_VALID(link->l_irq)) {
1072 *irq = link->l_irq;
1073 *pol = link->l_pol;
1074 *trig = link->l_trig;
1075 goto done;
1076 }
1077
1078 link->l_irq = acpi_pci_link_choose_irq(sc, link);
1079
1080 /*
1081 * Try to route the interrupt we picked. If it fails, then
1082 * assume the interrupt is not routed.
1083 */
1084 if (!PCI_INTERRUPT_VALID(link->l_irq))
1085 goto done;
1086
1087 acpi_pci_link_route_irqs(sc, irq, pol, trig);
1088 if (!link->l_routed) {
1089 link->l_irq = PCI_INVALID_IRQ;
1090 goto done;
1091 }
1092
1093 link->l_pol = *pol;
1094 link->l_trig = *trig;
1095 for (i = 0; i < link->l_dev_count; ++i) {
1096 reg = pci_conf_read(acpi_softc->sc_pc, link->l_devices[i],
1097 PCI_INTERRUPT_REG);
1098 reg &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
1099 reg |= link->l_irq << PCI_INTERRUPT_LINE_SHIFT;
1100 pci_conf_write(acpi_softc->sc_pc, link->l_devices[i],
1101 PCI_INTERRUPT_REG, reg);
1102 }
1103
1104 done:
1105 ACPI_SERIAL_END(pci_link);
1106
1107 return (link->l_irq);
1108 }
1109
1110 /*
1111 * This is gross, but we abuse the identify routine to perform one-time
1112 * SYSINIT() style initialization for the driver.
1113 */
1114 static void
1115 acpi_pci_link_init(struct acpi_pci_link_softc *sc)
1116 {
1117 ACPI_BUFFER buf;
1118
1119 /*
1120 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1121 * ISA IRQs.
1122 *
1123 * XXX: If we are using the APIC, the SCI might have been
1124 * rerouted to an APIC pin in which case this is invalid. However,
1125 * if we are using the APIC, we also shouldn't be having any PCI
1126 * interrupts routed via ISA IRQs, so this is probably ok.
1127 */
1128 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS)
1129 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1130
1131 buf.Length = sizeof (sc->pl_name);
1132 buf.Pointer = sc->pl_name;
1133
1134 if (ACPI_FAILURE(AcpiGetName(sc->pl_handle, ACPI_SINGLE_NAME, &buf)))
1135 snprintf(sc->pl_name, sizeof (sc->pl_name), "%s",
1136 "ACPI link device");
1137
1138 acpi_pci_link_attach(sc);
1139 }
1140
1141 void *
1142 acpi_pci_link_devbyhandle(ACPI_HANDLE handle)
1143 {
1144 struct acpi_pci_link_softc *sc;
1145
1146 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) {
1147 if (sc->pl_handle == handle)
1148 return sc;
1149 }
1150
1151 sc = malloc(sizeof (*sc), M_ACPI, M_NOWAIT | M_ZERO);
1152 if (sc == NULL)
1153 return NULL;
1154
1155 sc->pl_handle = handle;
1156
1157 acpi_pci_link_init(sc);
1158
1159 TAILQ_INSERT_TAIL(&acpi_pci_linkdevs, sc, pl_list);
1160
1161 return (void *)sc;
1162 }
1163
1164 void
1165 acpi_pci_link_resume(void)
1166 {
1167 struct acpi_pci_link_softc *sc;
1168 ACPI_BUFFER srsbuf;
1169
1170 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) {
1171 ACPI_SERIAL_BEGIN(pci_link);
1172 if (ACPI_SUCCESS(acpi_pci_link_srs(sc, &srsbuf)))
1173 ACPI_FREE(srsbuf.Pointer);
1174 ACPI_SERIAL_END(pci_link);
1175 }
1176 }
1177
1178 ACPI_HANDLE
1179 acpi_pci_link_handle(void *v)
1180 {
1181 struct acpi_pci_link_softc *sc = v;
1182
1183 return sc->pl_handle;
1184 }
1185
1186 char *
1187 acpi_pci_link_name(void *v)
1188 {
1189 struct acpi_pci_link_softc *sc = v;
1190
1191 return sc->pl_name;
1192 }
1193
1194
1195 /*
1196 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1197 *
1198 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1199 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1200 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1201 * resources.
1202 */
1203 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1204
1205 static ACPI_STATUS
1206 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1207 {
1208 ACPI_RESOURCE *rp;
1209 void *newp;
1210
1211 /* Initialise the buffer if necessary. */
1212 if (buf->Pointer == NULL) {
1213 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1214 if ((buf->Pointer = ACPI_ALLOCATE(buf->Length)) == NULL)
1215 return (AE_NO_MEMORY);
1216 rp = (ACPI_RESOURCE *)buf->Pointer;
1217 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1218 rp->Length = 0;
1219 }
1220
1221 if (res == NULL)
1222 return (AE_OK);
1223
1224 /*
1225 * Scan the current buffer looking for the terminator.
1226 * This will either find the terminator or hit the end
1227 * of the buffer and return an error.
1228 */
1229 rp = (ACPI_RESOURCE *)buf->Pointer;
1230 for (;;) {
1231 /* Range check, don't go outside the buffer */
1232 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer +
1233 buf->Length))
1234 return (AE_BAD_PARAMETER);
1235 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
1236 break;
1237 rp = ACPI_NEXT_RESOURCE(rp);
1238 }
1239
1240 /*
1241 * Check the size of the buffer and expand if required.
1242 *
1243 * Required size is:
1244 * size of existing resources before terminator +
1245 * size of new resource and header +
1246 * size of terminator.
1247 *
1248 * Note that this loop should really only run once, unless
1249 * for some reason we are stuffing a *really* huge resource.
1250 */
1251 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1252 res->Length + ACPI_RS_SIZE_NO_DATA +
1253 ACPI_RS_SIZE_MIN) >= buf->Length) {
1254 if ((newp = ACPI_ALLOCATE(buf->Length * 2)) == NULL)
1255 return (AE_NO_MEMORY);
1256 memcpy(newp, buf->Pointer, buf->Length);
1257 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1258 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1259 ACPI_FREE(buf->Pointer);
1260 buf->Pointer = newp;
1261 buf->Length += buf->Length;
1262 }
1263
1264 /* Insert the new resource. */
1265 memcpy(rp, res, res->Length + ACPI_RS_SIZE_NO_DATA);
1266
1267 /* And add the terminator. */
1268 rp = ACPI_NEXT_RESOURCE(rp);
1269 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1270 rp->Length = 0;
1271
1272 return (AE_OK);
1273 }
1274