1/* $NetBSD: acpi_pci_link.c,v 1.22 2014/09/14 19:54:05 mrg 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.22 2014/09/14 19:54:05 mrg 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
47ACPI_MODULE_NAME ("acpi_pci_link")
48
49MALLOC_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
91struct link;
92
93struct 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
102static TAILQ_HEAD(, acpi_pci_link_softc) acpi_pci_linkdevs =
103 TAILQ_HEAD_INITIALIZER(acpi_pci_linkdevs);
104
105
106struct 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
124struct link_count_request {
125 int in_dpf;
126 int count;
127};
128
129struct link_res_request {
130 struct acpi_pci_link_softc *sc;
131 int in_dpf;
132 int res_index;
133 int link_index;
134};
135
136static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
137static int pci_link_bios_isa_irqs;
138
139static ACPI_STATUS acpi_count_irq_resources(ACPI_RESOURCE *, void *);
140static ACPI_STATUS link_add_crs(ACPI_RESOURCE *, void *);
141static ACPI_STATUS link_add_prs(ACPI_RESOURCE *, void *);
142static int link_valid_irq(struct link *, int);
143static void acpi_pci_link_dump(struct acpi_pci_link_softc *);
144static int acpi_pci_link_attach(struct acpi_pci_link_softc *);
145static uint8_t acpi_pci_link_search_irq(struct acpi_pci_link_softc *, int, int,
146 int);
147static struct link *acpi_pci_link_lookup(struct acpi_pci_link_softc *, int);
148static ACPI_STATUS acpi_pci_link_srs(struct acpi_pci_link_softc *,
149 ACPI_BUFFER *);
150static ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *, ACPI_RESOURCE *);
151
152static ACPI_STATUS
153acpi_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
188static ACPI_STATUS
189link_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 */
255static ACPI_STATUS
256link_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
370static int
371link_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
396void
397acpi_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
406static void
407acpi_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
427static int
428acpi_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#ifndef ACPI__DIS_IS_BROKEN
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);
552fail:
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
564static void
565acpi_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
607static uint8_t
608acpi_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 */
657static struct link *
658acpi_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
668void
669acpi_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
737static ACPI_STATUS
738acpi_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
841static ACPI_STATUS
842acpi_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
898static ACPI_STATUS
899acpi_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 if (ACPI_FAILURE(status))
909 printf("%s: Unable to find link srs : %s\n",
910 sc->pl_name, AcpiFormatException(status));
911
912 /* Write out new resources via _SRS. */
913 return AcpiSetCurrentResources(sc->pl_handle, srsbuf);
914}
915
916static ACPI_STATUS
917acpi_pci_link_route_irqs(struct acpi_pci_link_softc *sc, int *irq, int *pol,
918 int *trig)
919{
920 ACPI_RESOURCE *resource, *end;
921 ACPI_BUFFER srsbuf;
922 ACPI_STATUS status;
923 struct link *link;
924 int i, is_ext = 0;
925
926 status = acpi_pci_link_srs(sc, &srsbuf);
927 if (ACPI_FAILURE(status)) {
928 printf("%s: _SRS failed: %s\n",
929 sc->pl_name, AcpiFormatException(status));
930 return (status);
931 }
932 /*
933 * Perform acpi_config_intr() on each IRQ resource if it was just
934 * routed for the first time.
935 */
936 link = sc->pl_links;
937 i = 0;
938 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
939 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
940 for (;;) {
941 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
942 break;
943 switch (resource->Type) {
944 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
945 is_ext = 1;
946 /* FALLTHROUGH */
947 case ACPI_RESOURCE_TYPE_IRQ:
948 /*
949 * Only configure the interrupt and update the
950 * weights if this link has a valid IRQ and was
951 * previously unrouted.
952 */
953 if (!link->l_routed &&
954 PCI_INTERRUPT_VALID(link->l_irq)) {
955 *trig = is_ext ?
956 resource->Data.ExtendedIrq.Triggering :
957 resource->Data.Irq.Triggering;
958 *pol = is_ext ?
959 resource->Data.ExtendedIrq.Polarity :
960 resource->Data.Irq.Polarity;
961 *irq = is_ext ?
962 resource->Data.ExtendedIrq.Interrupts[0] :
963 resource->Data.Irq.Interrupts[0];
964 link->l_routed = TRUE;
965 pci_link_interrupt_weights[link->l_irq] +=
966 link->l_references;
967 }
968 link++;
969 i++;
970 break;
971 }
972 resource = ACPI_NEXT_RESOURCE(resource);
973 if (resource >= end)
974 break;
975 }
976 ACPI_FREE(srsbuf.Pointer);
977 return (AE_OK);
978}
979
980/*
981 * Pick an IRQ to use for this unrouted link.
982 */
983static uint8_t
984acpi_pci_link_choose_irq(struct acpi_pci_link_softc *sc, struct link *link)
985{
986 u_int8_t best_irq, pos_irq;
987 int best_weight, pos_weight, i;
988
989 KASSERT(!link->l_routed);
990 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq));
991
992 /*
993 * If we have a valid BIOS IRQ, use that. We trust what the BIOS
994 * says it routed over what _CRS says the link thinks is routed.
995 */
996 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
997 return (link->l_bios_irq);
998
999 /*
1000 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
1001 * then use that.
1002 */
1003 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
1004 return (link->l_initial_irq);
1005
1006 /*
1007 * Ok, we have no useful hints, so we have to pick from the
1008 * possible IRQs. For ISA IRQs we only use interrupts that
1009 * have already been used by the BIOS.
1010 */
1011 best_irq = PCI_INVALID_IRQ;
1012 best_weight = INT_MAX;
1013 for (i = 0; i < link->l_num_irqs; i++) {
1014 pos_irq = link->l_irqs[i];
1015 if (pos_irq < NUM_ISA_INTERRUPTS &&
1016 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
1017 continue;
1018 pos_weight = pci_link_interrupt_weights[pos_irq];
1019 if (pos_weight < best_weight) {
1020 best_weight = pos_weight;
1021 best_irq = pos_irq;
1022 }
1023 }
1024
1025 /*
1026 * If this is an ISA IRQ, try using the SCI if it is also an ISA
1027 * interrupt as a fallback.
1028 */
1029 if (link->l_isa_irq && !PCI_INTERRUPT_VALID(best_irq)) {
1030 pos_irq = AcpiGbl_FADT.SciInterrupt;
1031 pos_weight = pci_link_interrupt_weights[pos_irq];
1032 if (pos_weight < best_weight) {
1033 best_weight = pos_weight;
1034 best_irq = pos_irq;
1035 }
1036 }
1037
1038 if (PCI_INTERRUPT_VALID(best_irq)) {
1039 aprint_verbose("%s: Picked IRQ %u with weight %d\n",
1040 sc->pl_name, best_irq, best_weight);
1041 } else
1042 printf("%s: Unable to choose an IRQ\n", sc->pl_name);
1043 return (best_irq);
1044}
1045
1046int
1047acpi_pci_link_route_interrupt(void *v, int index, int *irq, int *pol, int *trig)
1048{
1049 struct acpi_pci_link_softc *sc = v;
1050 struct link *link;
1051 int i;
1052 pcireg_t reg;
1053
1054 ACPI_SERIAL_BEGIN(pci_link);
1055 link = acpi_pci_link_lookup(sc, index);
1056 if (link == NULL)
1057 panic("%s: apparently invalid index %d", __func__, index);
1058
1059 /*
1060 * If this link device is already routed to an interrupt, just return
1061 * the interrupt it is routed to.
1062 */
1063 if (link->l_routed) {
1064 KASSERT(PCI_INTERRUPT_VALID(link->l_irq));
1065 ACPI_SERIAL_END(pci_link);
1066 *irq = link->l_irq;
1067 *pol = link->l_pol;
1068 *trig = link->l_trig;
1069 return (link->l_irq);
1070 }
1071
1072 /* Choose an IRQ if we need one. */
1073 if (PCI_INTERRUPT_VALID(link->l_irq)) {
1074 *irq = link->l_irq;
1075 *pol = link->l_pol;
1076 *trig = link->l_trig;
1077 goto done;
1078 }
1079
1080 link->l_irq = acpi_pci_link_choose_irq(sc, link);
1081
1082 /*
1083 * Try to route the interrupt we picked. If it fails, then
1084 * assume the interrupt is not routed.
1085 */
1086 if (!PCI_INTERRUPT_VALID(link->l_irq))
1087 goto done;
1088
1089 acpi_pci_link_route_irqs(sc, irq, pol, trig);
1090 if (!link->l_routed) {
1091 link->l_irq = PCI_INVALID_IRQ;
1092 goto done;
1093 }
1094
1095 link->l_pol = *pol;
1096 link->l_trig = *trig;
1097 for (i = 0; i < link->l_dev_count; ++i) {
1098 reg = pci_conf_read(acpi_softc->sc_pc, link->l_devices[i],
1099 PCI_INTERRUPT_REG);
1100 reg &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
1101 reg |= link->l_irq << PCI_INTERRUPT_LINE_SHIFT;
1102 pci_conf_write(acpi_softc->sc_pc, link->l_devices[i],
1103 PCI_INTERRUPT_REG, reg);
1104 }
1105
1106done:
1107 ACPI_SERIAL_END(pci_link);
1108
1109 return (link->l_irq);
1110}
1111
1112/*
1113 * This is gross, but we abuse the identify routine to perform one-time
1114 * SYSINIT() style initialization for the driver.
1115 */
1116static void
1117acpi_pci_link_init(struct acpi_pci_link_softc *sc)
1118{
1119 ACPI_BUFFER buf;
1120
1121 /*
1122 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1123 * ISA IRQs.
1124 *
1125 * XXX: If we are using the APIC, the SCI might have been
1126 * rerouted to an APIC pin in which case this is invalid. However,
1127 * if we are using the APIC, we also shouldn't be having any PCI
1128 * interrupts routed via ISA IRQs, so this is probably ok.
1129 */
1130 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS)
1131 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1132
1133 buf.Length = sizeof (sc->pl_name);
1134 buf.Pointer = sc->pl_name;
1135
1136 if (ACPI_FAILURE(AcpiGetName(sc->pl_handle, ACPI_SINGLE_NAME, &buf)))
1137 snprintf(sc->pl_name, sizeof (sc->pl_name), "%s",
1138 "ACPI link device");
1139
1140 acpi_pci_link_attach(sc);
1141}
1142
1143void *
1144acpi_pci_link_devbyhandle(ACPI_HANDLE handle)
1145{
1146 struct acpi_pci_link_softc *sc;
1147
1148 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) {
1149 if (sc->pl_handle == handle)
1150 return sc;
1151 }
1152
1153 sc = malloc(sizeof (*sc), M_ACPI, M_NOWAIT | M_ZERO);
1154 if (sc == NULL)
1155 return NULL;
1156
1157 sc->pl_handle = handle;
1158
1159 acpi_pci_link_init(sc);
1160
1161 TAILQ_INSERT_TAIL(&acpi_pci_linkdevs, sc, pl_list);
1162
1163 return (void *)sc;
1164}
1165
1166void
1167acpi_pci_link_resume(void)
1168{
1169 struct acpi_pci_link_softc *sc;
1170 ACPI_BUFFER srsbuf;
1171
1172 TAILQ_FOREACH(sc, &acpi_pci_linkdevs, pl_list) {
1173 ACPI_SERIAL_BEGIN(pci_link);
1174 if (ACPI_SUCCESS(acpi_pci_link_srs(sc, &srsbuf)))
1175 ACPI_FREE(srsbuf.Pointer);
1176 ACPI_SERIAL_END(pci_link);
1177 }
1178}
1179
1180ACPI_HANDLE
1181acpi_pci_link_handle(void *v)
1182{
1183 struct acpi_pci_link_softc *sc = v;
1184
1185 return sc->pl_handle;
1186}
1187
1188char *
1189acpi_pci_link_name(void *v)
1190{
1191 struct acpi_pci_link_softc *sc = v;
1192
1193 return sc->pl_name;
1194}
1195
1196
1197/*
1198 * Append an ACPI_RESOURCE to an ACPI_BUFFER.
1199 *
1200 * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
1201 * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible
1202 * backing block. If the ACPI_RESOURCE is NULL, return an empty set of
1203 * resources.
1204 */
1205#define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512
1206
1207static ACPI_STATUS
1208acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
1209{
1210 ACPI_RESOURCE *rp;
1211 void *newp;
1212
1213 /* Initialise the buffer if necessary. */
1214 if (buf->Pointer == NULL) {
1215 buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
1216 if ((buf->Pointer = ACPI_ALLOCATE(buf->Length)) == NULL)
1217 return (AE_NO_MEMORY);
1218 rp = (ACPI_RESOURCE *)buf->Pointer;
1219 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1220 rp->Length = 0;
1221 }
1222
1223 if (res == NULL)
1224 return (AE_OK);
1225
1226 /*
1227 * Scan the current buffer looking for the terminator.
1228 * This will either find the terminator or hit the end
1229 * of the buffer and return an error.
1230 */
1231 rp = (ACPI_RESOURCE *)buf->Pointer;
1232 for (;;) {
1233 /* Range check, don't go outside the buffer */
1234 if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer +
1235 buf->Length))
1236 return (AE_BAD_PARAMETER);
1237 if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0)
1238 break;
1239 rp = ACPI_NEXT_RESOURCE(rp);
1240 }
1241
1242 /*
1243 * Check the size of the buffer and expand if required.
1244 *
1245 * Required size is:
1246 * size of existing resources before terminator +
1247 * size of new resource and header +
1248 * size of terminator.
1249 *
1250 * Note that this loop should really only run once, unless
1251 * for some reason we are stuffing a *really* huge resource.
1252 */
1253 while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) +
1254 res->Length + ACPI_RS_SIZE_NO_DATA +
1255 ACPI_RS_SIZE_MIN) >= buf->Length) {
1256 if ((newp = ACPI_ALLOCATE(buf->Length * 2)) == NULL)
1257 return (AE_NO_MEMORY);
1258 memcpy(newp, buf->Pointer, buf->Length);
1259 rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
1260 ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
1261 ACPI_FREE(buf->Pointer);
1262 buf->Pointer = newp;
1263 buf->Length += buf->Length;
1264 }
1265
1266 /* Insert the new resource. */
1267 memcpy(rp, res, res->Length);
1268
1269 /* And add the terminator. */
1270 rp = ACPI_NEXT_RESOURCE(rp);
1271 rp->Type = ACPI_RESOURCE_TYPE_END_TAG;
1272 rp->Length = 0;
1273
1274 return (AE_OK);
1275}
1276