xref: /freebsd/sys/dev/acpica/acpi_pci_link.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*-
2  * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 
38 #include <contrib/dev/acpica/include/acpi.h>
39 
40 #include <dev/acpica/acpivar.h>
41 #include <dev/acpica/acpi_pcibvar.h>
42 
43 #include <machine/pci_cfgreg.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
46 #include "pcib_if.h"
47 
48 /* Hooks for the ACPI CA debugging infrastructure. */
49 #define _COMPONENT	ACPI_BUS
50 ACPI_MODULE_NAME("PCI_LINK")
51 
52 ACPI_SERIAL_DECL(pci_link, "ACPI PCI link");
53 
54 #define NUM_ISA_INTERRUPTS	16
55 #define NUM_ACPI_INTERRUPTS	256
56 
57 /*
58  * An ACPI PCI link device may contain multiple links.  Each link has its
59  * own ACPI resource.  _PRT entries specify which link is being used via
60  * the Source Index.
61  *
62  * XXX: A note about Source Indices and DPFs:  Currently we assume that
63  * the DPF start and end tags are not counted towards the index that
64  * Source Index corresponds to.  Also, we assume that when DPFs are in use
65  * they various sets overlap in terms of Indices.  Here's an example
66  * resource list indicating these assumptions:
67  *
68  * Resource		Index
69  * --------		-----
70  * I/O Port		0
71  * Start DPF		-
72  * IRQ			1
73  * MemIO		2
74  * Start DPF		-
75  * IRQ			1
76  * MemIO		2
77  * End DPF		-
78  * DMA Channel		3
79  *
80  * The XXX is because I'm not sure if this is a valid assumption to make.
81  */
82 
83 /* States during DPF processing. */
84 #define	DPF_OUTSIDE	0
85 #define	DPF_FIRST	1
86 #define	DPF_IGNORE	2
87 
88 struct link;
89 
90 struct acpi_pci_link_softc {
91 	int	pl_num_links;
92 	int	pl_crs_bad;
93 	struct link *pl_links;
94 	device_t pl_dev;
95 };
96 
97 struct link {
98 	struct acpi_pci_link_softc *l_sc;
99 	uint8_t	l_bios_irq;
100 	uint8_t	l_irq;
101 	uint8_t	l_initial_irq;
102 	UINT32	l_crs_type;
103 	int	l_res_index;
104 	int	l_num_irqs;
105 	int	*l_irqs;
106 	int	l_references;
107 	int	l_routed:1;
108 	int	l_isa_irq:1;
109 	ACPI_RESOURCE l_prs_template;
110 };
111 
112 struct link_count_request {
113 	int	in_dpf;
114 	int	count;
115 };
116 
117 struct link_res_request {
118 	struct acpi_pci_link_softc *sc;
119 	int	in_dpf;
120 	int	res_index;
121 	int	link_index;
122 };
123 
124 static MALLOC_DEFINE(M_PCI_LINK, "pci_link", "ACPI PCI Link structures");
125 
126 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
127 static int pci_link_bios_isa_irqs;
128 
129 static char *pci_link_ids[] = { "PNP0C0F", NULL };
130 
131 /*
132  * Fetch the short name associated with an ACPI handle and save it in the
133  * passed in buffer.
134  */
135 static ACPI_STATUS
136 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
137 {
138 	ACPI_BUFFER buf;
139 
140 	buf.Length = buflen;
141 	buf.Pointer = buffer;
142 	return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
143 }
144 
145 static int
146 acpi_pci_link_probe(device_t dev)
147 {
148 	char descr[28], name[12];
149 
150 	/*
151 	 * We explicitly do not check _STA since not all systems set it to
152 	 * sensible values.
153 	 */
154 	if (acpi_disabled("pci_link") ||
155 	    ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids) == NULL)
156 		return (ENXIO);
157 
158 	if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
159 	    sizeof(name)))) {
160 		snprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
161 		device_set_desc_copy(dev, descr);
162 	} else
163 		device_set_desc(dev, "ACPI PCI Link");
164 	device_quiet(dev);
165 	return (0);
166 }
167 
168 static ACPI_STATUS
169 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
170 {
171 	struct link_count_request *req;
172 
173 	req = (struct link_count_request *)context;
174 	switch (res->Type) {
175 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
176 		switch (req->in_dpf) {
177 		case DPF_OUTSIDE:
178 			/* We've started the first DPF. */
179 			req->in_dpf = DPF_FIRST;
180 			break;
181 		case DPF_FIRST:
182 			/* We've started the second DPF. */
183 			req->in_dpf = DPF_IGNORE;
184 			break;
185 		}
186 		break;
187 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
188 		/* We are finished with DPF parsing. */
189 		KASSERT(req->in_dpf != DPF_OUTSIDE,
190 		    ("%s: end dpf when not parsing a dpf", __func__));
191 		req->in_dpf = DPF_OUTSIDE;
192 		break;
193 	case ACPI_RESOURCE_TYPE_IRQ:
194 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
195 		/*
196 		 * Don't count resources if we are in a DPF set that we are
197 		 * ignoring.
198 		 */
199 		if (req->in_dpf != DPF_IGNORE)
200 			req->count++;
201 	}
202 	return (AE_OK);
203 }
204 
205 static ACPI_STATUS
206 link_add_crs(ACPI_RESOURCE *res, void *context)
207 {
208 	struct link_res_request *req;
209 	struct link *link;
210 
211 	ACPI_SERIAL_ASSERT(pci_link);
212 	req = (struct link_res_request *)context;
213 	switch (res->Type) {
214 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
215 		switch (req->in_dpf) {
216 		case DPF_OUTSIDE:
217 			/* We've started the first DPF. */
218 			req->in_dpf = DPF_FIRST;
219 			break;
220 		case DPF_FIRST:
221 			/* We've started the second DPF. */
222 			panic(
223 		"%s: Multiple dependent functions within a current resource",
224 			    __func__);
225 			break;
226 		}
227 		break;
228 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
229 		/* We are finished with DPF parsing. */
230 		KASSERT(req->in_dpf != DPF_OUTSIDE,
231 		    ("%s: end dpf when not parsing a dpf", __func__));
232 		req->in_dpf = DPF_OUTSIDE;
233 		break;
234 	case ACPI_RESOURCE_TYPE_IRQ:
235 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
236 		KASSERT(req->link_index < req->sc->pl_num_links,
237 		    ("%s: array boundary violation", __func__));
238 		link = &req->sc->pl_links[req->link_index];
239 		link->l_res_index = req->res_index;
240 		link->l_crs_type = res->Type;
241 		req->link_index++;
242 		req->res_index++;
243 
244 		/*
245 		 * Only use the current value if there's one IRQ.  Some
246 		 * systems return multiple IRQs (which is nonsense for _CRS)
247 		 * when the link hasn't been programmed.
248 		 */
249 		if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
250 			if (res->Data.Irq.InterruptCount == 1)
251 				link->l_irq = res->Data.Irq.Interrupts[0];
252 		} else if (res->Data.ExtendedIrq.InterruptCount == 1)
253 			link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
254 
255 		/*
256 		 * An IRQ of zero means that the link isn't routed.
257 		 */
258 		if (link->l_irq == 0)
259 			link->l_irq = PCI_INVALID_IRQ;
260 		break;
261 	default:
262 		req->res_index++;
263 	}
264 	return (AE_OK);
265 }
266 
267 /*
268  * Populate the set of possible IRQs for each device.
269  */
270 static ACPI_STATUS
271 link_add_prs(ACPI_RESOURCE *res, void *context)
272 {
273 	ACPI_RESOURCE *tmp;
274 	struct link_res_request *req;
275 	struct link *link;
276 	UINT8 *irqs = NULL;
277 	UINT32 *ext_irqs = NULL;
278 	int i, is_ext_irq = 1;
279 
280 	ACPI_SERIAL_ASSERT(pci_link);
281 	req = (struct link_res_request *)context;
282 	switch (res->Type) {
283 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
284 		switch (req->in_dpf) {
285 		case DPF_OUTSIDE:
286 			/* We've started the first DPF. */
287 			req->in_dpf = DPF_FIRST;
288 			break;
289 		case DPF_FIRST:
290 			/* We've started the second DPF. */
291 			req->in_dpf = DPF_IGNORE;
292 			break;
293 		}
294 		break;
295 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
296 		/* We are finished with DPF parsing. */
297 		KASSERT(req->in_dpf != DPF_OUTSIDE,
298 		    ("%s: end dpf when not parsing a dpf", __func__));
299 		req->in_dpf = DPF_OUTSIDE;
300 		break;
301 	case ACPI_RESOURCE_TYPE_IRQ:
302 		is_ext_irq = 0;
303 		/* fall through */
304 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
305 		/*
306 		 * Don't parse resources if we are in a DPF set that we are
307 		 * ignoring.
308 		 */
309 		if (req->in_dpf == DPF_IGNORE)
310 			break;
311 
312 		KASSERT(req->link_index < req->sc->pl_num_links,
313 		    ("%s: array boundary violation", __func__));
314 		link = &req->sc->pl_links[req->link_index];
315 		if (link->l_res_index == -1) {
316 			KASSERT(req->sc->pl_crs_bad,
317 			    ("res_index should be set"));
318 			link->l_res_index = req->res_index;
319 		}
320 		req->link_index++;
321 		req->res_index++;
322 
323 		/*
324 		 * Stash a copy of the resource for later use when doing
325 		 * _SRS.
326 		 */
327 		tmp = &link->l_prs_template;
328 		if (is_ext_irq) {
329 			bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.ExtendedIrq));
330 
331 			/*
332 			 * XXX acpi_AppendBufferResource() cannot handle
333 			 * optional data.
334 			 */
335 			bzero(&tmp->Data.ExtendedIrq.ResourceSource,
336 			    sizeof(tmp->Data.ExtendedIrq.ResourceSource));
337 			tmp->Length = ACPI_RS_SIZE(tmp->Data.ExtendedIrq);
338 
339 			link->l_num_irqs =
340 			    res->Data.ExtendedIrq.InterruptCount;
341 			ext_irqs = res->Data.ExtendedIrq.Interrupts;
342 		} else {
343 			bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.Irq));
344 			link->l_num_irqs = res->Data.Irq.InterruptCount;
345 			irqs = res->Data.Irq.Interrupts;
346 		}
347 		if (link->l_num_irqs == 0)
348 			break;
349 
350 		/*
351 		 * Save a list of the valid IRQs.  Also, if all of the
352 		 * valid IRQs are ISA IRQs, then mark this link as
353 		 * routed via an ISA interrupt.
354 		 */
355 		link->l_isa_irq = TRUE;
356 		link->l_irqs = malloc(sizeof(int) * link->l_num_irqs,
357 		    M_PCI_LINK, M_WAITOK | M_ZERO);
358 		for (i = 0; i < link->l_num_irqs; i++) {
359 			if (is_ext_irq) {
360 				link->l_irqs[i] = ext_irqs[i];
361 				if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
362 					link->l_isa_irq = FALSE;
363 			} else {
364 				link->l_irqs[i] = irqs[i];
365 				if (irqs[i] >= NUM_ISA_INTERRUPTS)
366 					link->l_isa_irq = FALSE;
367 			}
368 		}
369 
370 		/*
371 		 * If this is not an ISA IRQ but _CRS used a non-extended
372 		 * IRQ descriptor, don't use _CRS as a template for _SRS.
373 		 */
374 		if (!req->sc->pl_crs_bad && !link->l_isa_irq &&
375 		    link->l_crs_type == ACPI_RESOURCE_TYPE_IRQ)
376 			req->sc->pl_crs_bad = TRUE;
377 		break;
378 	default:
379 		if (req->in_dpf == DPF_IGNORE)
380 			break;
381 		if (req->sc->pl_crs_bad)
382 			device_printf(req->sc->pl_dev,
383 		    "Warning: possible resource %d will be lost during _SRS\n",
384 			    req->res_index);
385 		req->res_index++;
386 	}
387 	return (AE_OK);
388 }
389 
390 static int
391 link_valid_irq(struct link *link, int irq)
392 {
393 	int i;
394 
395 	ACPI_SERIAL_ASSERT(pci_link);
396 
397 	/* Invalid interrupts are never valid. */
398 	if (!PCI_INTERRUPT_VALID(irq))
399 		return (FALSE);
400 
401 	/* Any interrupt in the list of possible interrupts is valid. */
402 	for (i = 0; i < link->l_num_irqs; i++)
403 		if (link->l_irqs[i] == irq)
404 			 return (TRUE);
405 
406 	/*
407 	 * For links routed via an ISA interrupt, if the SCI is routed via
408 	 * an ISA interrupt, the SCI is always treated as a valid IRQ.
409 	 */
410 	if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
411 	    irq < NUM_ISA_INTERRUPTS)
412 		return (TRUE);
413 
414 	/* If the interrupt wasn't found in the list it is not valid. */
415 	return (FALSE);
416 }
417 
418 static void
419 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
420 {
421 	struct link *link;
422 	char buf[16];
423 	int i, j;
424 
425 	ACPI_SERIAL_ASSERT(pci_link);
426 	if (header) {
427 		snprintf(buf, sizeof(buf), "%s:",
428 		    device_get_nameunit(sc->pl_dev));
429 		printf("%-16.16s  Index  IRQ  Rtd  Ref  IRQs\n", buf);
430 	}
431 	for (i = 0; i < sc->pl_num_links; i++) {
432 		link = &sc->pl_links[i];
433 		printf("  %-14.14s  %5d  %3d   %c   %3d ", i == 0 ? tag : "", i,
434 		    link->l_irq, link->l_routed ? 'Y' : 'N',
435 		    link->l_references);
436 		if (link->l_num_irqs == 0)
437 			printf(" none");
438 		else for (j = 0; j < link->l_num_irqs; j++)
439 			printf(" %d", link->l_irqs[j]);
440 		printf("\n");
441 	}
442 }
443 
444 static int
445 acpi_pci_link_attach(device_t dev)
446 {
447 	struct acpi_pci_link_softc *sc;
448 	struct link_count_request creq;
449 	struct link_res_request rreq;
450 	ACPI_STATUS status;
451 	int i;
452 
453 	sc = device_get_softc(dev);
454 	sc->pl_dev = dev;
455 	ACPI_SERIAL_BEGIN(pci_link);
456 
457 	/*
458 	 * Count the number of current resources so we know how big of
459 	 * a link array to allocate.  On some systems, _CRS is broken,
460 	 * so for those systems try to derive the count from _PRS instead.
461 	 */
462 	creq.in_dpf = DPF_OUTSIDE;
463 	creq.count = 0;
464 	status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
465 	    acpi_count_irq_resources, &creq);
466 	sc->pl_crs_bad = ACPI_FAILURE(status);
467 	if (sc->pl_crs_bad) {
468 		creq.in_dpf = DPF_OUTSIDE;
469 		creq.count = 0;
470 		status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
471 		    acpi_count_irq_resources, &creq);
472 		if (ACPI_FAILURE(status)) {
473 			device_printf(dev,
474 			    "Unable to parse _CRS or _PRS: %s\n",
475 			    AcpiFormatException(status));
476 			ACPI_SERIAL_END(pci_link);
477 			return (ENXIO);
478 		}
479 	}
480 	sc->pl_num_links = creq.count;
481 	if (creq.count == 0) {
482 		ACPI_SERIAL_END(pci_link);
483 		return (0);
484 	}
485 	sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links,
486 	    M_PCI_LINK, M_WAITOK | M_ZERO);
487 
488 	/* Initialize the child links. */
489 	for (i = 0; i < sc->pl_num_links; i++) {
490 		sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
491 		sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
492 		sc->pl_links[i].l_sc = sc;
493 		sc->pl_links[i].l_isa_irq = FALSE;
494 		sc->pl_links[i].l_res_index = -1;
495 	}
496 
497 	/* Try to read the current settings from _CRS if it is valid. */
498 	if (!sc->pl_crs_bad) {
499 		rreq.in_dpf = DPF_OUTSIDE;
500 		rreq.link_index = 0;
501 		rreq.res_index = 0;
502 		rreq.sc = sc;
503 		status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
504 		    link_add_crs, &rreq);
505 		if (ACPI_FAILURE(status)) {
506 			device_printf(dev, "Unable to parse _CRS: %s\n",
507 			    AcpiFormatException(status));
508 			goto fail;
509 		}
510 	}
511 
512 	/*
513 	 * Try to read the possible settings from _PRS.  Note that if the
514 	 * _CRS is toast, we depend on having a working _PRS.  However, if
515 	 * _CRS works, then it is ok for _PRS to be missing.
516 	 */
517 	rreq.in_dpf = DPF_OUTSIDE;
518 	rreq.link_index = 0;
519 	rreq.res_index = 0;
520 	rreq.sc = sc;
521 	status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
522 	    link_add_prs, &rreq);
523 	if (ACPI_FAILURE(status) &&
524 	    (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
525 		device_printf(dev, "Unable to parse _PRS: %s\n",
526 		    AcpiFormatException(status));
527 		goto fail;
528 	}
529 	if (bootverbose)
530 		acpi_pci_link_dump(sc, 1, "Initial Probe");
531 
532 	/* Verify initial IRQs if we have _PRS. */
533 	if (status != AE_NOT_FOUND)
534 		for (i = 0; i < sc->pl_num_links; i++)
535 			if (!link_valid_irq(&sc->pl_links[i],
536 			    sc->pl_links[i].l_irq))
537 				sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
538 	if (bootverbose)
539 		acpi_pci_link_dump(sc, 0, "Validation");
540 
541 	/* Save initial IRQs. */
542 	for (i = 0; i < sc->pl_num_links; i++)
543 		sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
544 
545 	/*
546 	 * Try to disable this link.  If successful, set the current IRQ to
547 	 * zero and flags to indicate this link is not routed.  If we can't
548 	 * run _DIS (i.e., the method doesn't exist), assume the initial
549 	 * IRQ was routed by the BIOS.
550 	 */
551 	if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
552 	    NULL)))
553 		for (i = 0; i < sc->pl_num_links; i++)
554 			sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
555 	else
556 		for (i = 0; i < sc->pl_num_links; i++)
557 			if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
558 				sc->pl_links[i].l_routed = TRUE;
559 	if (bootverbose)
560 		acpi_pci_link_dump(sc, 0, "After Disable");
561 	ACPI_SERIAL_END(pci_link);
562 	return (0);
563 fail:
564 	ACPI_SERIAL_END(pci_link);
565 	for (i = 0; i < sc->pl_num_links; i++)
566 		if (sc->pl_links[i].l_irqs != NULL)
567 			free(sc->pl_links[i].l_irqs, M_PCI_LINK);
568 	free(sc->pl_links, M_PCI_LINK);
569 	return (ENXIO);
570 }
571 
572 /* XXX: Note that this is identical to pci_pir_search_irq(). */
573 static uint8_t
574 acpi_pci_link_search_irq(int bus, int device, int pin)
575 {
576 	uint32_t value;
577 	uint8_t func, maxfunc;
578 
579 	/* See if we have a valid device at function 0. */
580 	value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
581 	if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
582 		return (PCI_INVALID_IRQ);
583 	if (value & PCIM_MFDEV)
584 		maxfunc = PCI_FUNCMAX;
585 	else
586 		maxfunc = 0;
587 
588 	/* Scan all possible functions at this device. */
589 	for (func = 0; func <= maxfunc; func++) {
590 		value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
591 		if (value == 0xffffffff)
592 			continue;
593 		value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
594 
595 		/*
596 		 * See if it uses the pin in question.  Note that the passed
597 		 * in pin uses 0 for A, .. 3 for D whereas the intpin
598 		 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
599 		 */
600 		if (value != pin + 1)
601 			continue;
602 		value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
603 		if (bootverbose)
604 			printf(
605 		"ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
606 			    bus, device, pin + 'A', func, value);
607 		if (value != PCI_INVALID_IRQ)
608 			return (value);
609 	}
610 	return (PCI_INVALID_IRQ);
611 }
612 
613 /*
614  * Find the link structure that corresponds to the resource index passed in
615  * via 'source_index'.
616  */
617 static struct link *
618 acpi_pci_link_lookup(device_t dev, int source_index)
619 {
620 	struct acpi_pci_link_softc *sc;
621 	int i;
622 
623 	ACPI_SERIAL_ASSERT(pci_link);
624 	sc = device_get_softc(dev);
625 	for (i = 0; i < sc->pl_num_links; i++)
626 		if (sc->pl_links[i].l_res_index == source_index)
627 			return (&sc->pl_links[i]);
628 	return (NULL);
629 }
630 
631 void
632 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
633     int pin)
634 {
635 	struct link *link;
636 	uint8_t bios_irq;
637 	uintptr_t bus;
638 
639 	/*
640 	 * Look up the PCI bus for the specified PCI bridge device.  Note
641 	 * that the PCI bridge device might not have any children yet.
642 	 * However, looking up its bus number doesn't require a valid child
643 	 * device, so we just pass NULL.
644 	 */
645 	if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
646 		device_printf(pcib, "Unable to read PCI bus number");
647 		panic("PCI bridge without a bus number");
648 	}
649 
650 	/* Bump the reference count. */
651 	ACPI_SERIAL_BEGIN(pci_link);
652 	link = acpi_pci_link_lookup(dev, index);
653 	if (link == NULL) {
654 		device_printf(dev, "apparently invalid index %d\n", index);
655 		ACPI_SERIAL_END(pci_link);
656 		return;
657 	}
658 	link->l_references++;
659 	if (link->l_routed)
660 		pci_link_interrupt_weights[link->l_irq]++;
661 
662 	/*
663 	 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
664 	 * (8259As).  Thus, if this link is routed via an ISA IRQ, go
665 	 * look to see if the BIOS routed an IRQ for this link at the
666 	 * indicated (bus, slot, pin).  If so, we prefer that IRQ for
667 	 * this link and add that IRQ to our list of known-good IRQs.
668 	 * This provides a good work-around for link devices whose _CRS
669 	 * method is either broken or bogus.  We only use the value
670 	 * returned by _CRS if we can't find a valid IRQ via this method
671 	 * in fact.
672 	 *
673 	 * If this link is not routed via an ISA IRQ (because we are using
674 	 * APIC for example), then don't bother looking up the BIOS IRQ
675 	 * as if we find one it won't be valid anyway.
676 	 */
677 	if (!link->l_isa_irq) {
678 		ACPI_SERIAL_END(pci_link);
679 		return;
680 	}
681 
682 	/* Try to find a BIOS IRQ setting from any matching devices. */
683 	bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
684 	if (!PCI_INTERRUPT_VALID(bios_irq)) {
685 		ACPI_SERIAL_END(pci_link);
686 		return;
687 	}
688 
689 	/* Validate the BIOS IRQ. */
690 	if (!link_valid_irq(link, bios_irq)) {
691 		device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
692 		    bios_irq, (int)bus, slot, pin + 'A');
693 	} else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
694 		link->l_bios_irq = bios_irq;
695 		if (bios_irq < NUM_ISA_INTERRUPTS)
696 			pci_link_bios_isa_irqs |= (1 << bios_irq);
697 		if (bios_irq != link->l_initial_irq &&
698 		    PCI_INTERRUPT_VALID(link->l_initial_irq))
699 			device_printf(dev,
700 			    "BIOS IRQ %u does not match initial IRQ %u\n",
701 			    bios_irq, link->l_initial_irq);
702 	} else if (bios_irq != link->l_bios_irq)
703 		device_printf(dev,
704 	    "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
705 		    bios_irq, (int)bus, slot, pin + 'A',
706 		    link->l_bios_irq);
707 	ACPI_SERIAL_END(pci_link);
708 }
709 
710 static ACPI_STATUS
711 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
712 {
713 	ACPI_RESOURCE *end, *res;
714 	ACPI_STATUS status;
715 	struct link *link;
716 	int i, in_dpf;
717 
718 	/* Fetch the _CRS. */
719 	ACPI_SERIAL_ASSERT(pci_link);
720 	srsbuf->Pointer = NULL;
721 	srsbuf->Length = ACPI_ALLOCATE_BUFFER;
722 	status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), srsbuf);
723 	if (ACPI_SUCCESS(status) && srsbuf->Pointer == NULL)
724 		status = AE_NO_MEMORY;
725 	if (ACPI_FAILURE(status)) {
726 		if (bootverbose)
727 			device_printf(sc->pl_dev,
728 			    "Unable to fetch current resources: %s\n",
729 			    AcpiFormatException(status));
730 		return (status);
731 	}
732 
733 	/* Fill in IRQ resources via link structures. */
734 	link = sc->pl_links;
735 	i = 0;
736 	in_dpf = DPF_OUTSIDE;
737 	res = (ACPI_RESOURCE *)srsbuf->Pointer;
738 	end = (ACPI_RESOURCE *)((char *)srsbuf->Pointer + srsbuf->Length);
739 	for (;;) {
740 		switch (res->Type) {
741 		case ACPI_RESOURCE_TYPE_START_DEPENDENT:
742 			switch (in_dpf) {
743 			case DPF_OUTSIDE:
744 				/* We've started the first DPF. */
745 				in_dpf = DPF_FIRST;
746 				break;
747 			case DPF_FIRST:
748 				/* We've started the second DPF. */
749 				panic(
750 		"%s: Multiple dependent functions within a current resource",
751 				    __func__);
752 				break;
753 			}
754 			break;
755 		case ACPI_RESOURCE_TYPE_END_DEPENDENT:
756 			/* We are finished with DPF parsing. */
757 			KASSERT(in_dpf != DPF_OUTSIDE,
758 			    ("%s: end dpf when not parsing a dpf", __func__));
759 			in_dpf = DPF_OUTSIDE;
760 			break;
761 		case ACPI_RESOURCE_TYPE_IRQ:
762 			MPASS(i < sc->pl_num_links);
763 			res->Data.Irq.InterruptCount = 1;
764 			if (PCI_INTERRUPT_VALID(link->l_irq)) {
765 				KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
766 		("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
767 				    __func__, link->l_irq));
768 				res->Data.Irq.Interrupts[0] = link->l_irq;
769 			} else
770 				res->Data.Irq.Interrupts[0] = 0;
771 			link++;
772 			i++;
773 			break;
774 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
775 			MPASS(i < sc->pl_num_links);
776 			res->Data.ExtendedIrq.InterruptCount = 1;
777 			if (PCI_INTERRUPT_VALID(link->l_irq))
778 				res->Data.ExtendedIrq.Interrupts[0] =
779 				    link->l_irq;
780 			else
781 				res->Data.ExtendedIrq.Interrupts[0] = 0;
782 			link++;
783 			i++;
784 			break;
785 		}
786 		if (res->Type == ACPI_RESOURCE_TYPE_END_TAG)
787 			break;
788 		res = ACPI_NEXT_RESOURCE(res);
789 		if (res >= end)
790 			break;
791 	}
792 	return (AE_OK);
793 }
794 
795 static ACPI_STATUS
796 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
797     ACPI_BUFFER *srsbuf)
798 {
799 	ACPI_RESOURCE newres;
800 	ACPI_STATUS status;
801 	struct link *link;
802 	int i;
803 
804 	/* Start off with an empty buffer. */
805 	srsbuf->Pointer = NULL;
806 	link = sc->pl_links;
807 	for (i = 0; i < sc->pl_num_links; i++) {
808 
809 		/* Add a new IRQ resource from each link. */
810 		link = &sc->pl_links[i];
811 		if (link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ) {
812 
813 			/* Build an IRQ resource. */
814 			bcopy(&link->l_prs_template, &newres,
815 			    ACPI_RS_SIZE(newres.Data.Irq));
816 			newres.Data.Irq.InterruptCount = 1;
817 			if (PCI_INTERRUPT_VALID(link->l_irq)) {
818 				KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
819 		("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
820 				    __func__, link->l_irq));
821 				newres.Data.Irq.Interrupts[0] = link->l_irq;
822 			} else
823 				newres.Data.Irq.Interrupts[0] = 0;
824 		} else {
825 
826 			/* Build an ExtIRQ resuorce. */
827 			bcopy(&link->l_prs_template, &newres,
828 			    ACPI_RS_SIZE(newres.Data.ExtendedIrq));
829 			newres.Data.ExtendedIrq.InterruptCount = 1;
830 			if (PCI_INTERRUPT_VALID(link->l_irq))
831 				newres.Data.ExtendedIrq.Interrupts[0] =
832 				    link->l_irq;
833 			else
834 				newres.Data.ExtendedIrq.Interrupts[0] = 0;
835 		}
836 
837 		/* Add the new resource to the end of the _SRS buffer. */
838 		status = acpi_AppendBufferResource(srsbuf, &newres);
839 		if (ACPI_FAILURE(status)) {
840 			device_printf(sc->pl_dev,
841 			    "Unable to build resources: %s\n",
842 			    AcpiFormatException(status));
843 			if (srsbuf->Pointer != NULL)
844 				AcpiOsFree(srsbuf->Pointer);
845 			return (status);
846 		}
847 	}
848 	return (AE_OK);
849 }
850 
851 static ACPI_STATUS
852 acpi_pci_link_route_irqs(device_t dev)
853 {
854 	struct acpi_pci_link_softc *sc;
855 	ACPI_RESOURCE *resource, *end;
856 	ACPI_BUFFER srsbuf;
857 	ACPI_STATUS status;
858 	struct link *link;
859 	int i;
860 
861 	ACPI_SERIAL_ASSERT(pci_link);
862 	sc = device_get_softc(dev);
863 	if (sc->pl_crs_bad)
864 		status = acpi_pci_link_srs_from_links(sc, &srsbuf);
865 	else
866 		status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
867 
868 	/* Write out new resources via _SRS. */
869 	status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf);
870 	if (ACPI_FAILURE(status)) {
871 		device_printf(dev, "Unable to route IRQs: %s\n",
872 		    AcpiFormatException(status));
873 		AcpiOsFree(srsbuf.Pointer);
874 		return (status);
875 	}
876 
877 	/*
878 	 * Perform acpi_config_intr() on each IRQ resource if it was just
879 	 * routed for the first time.
880 	 */
881 	link = sc->pl_links;
882 	i = 0;
883 	resource = (ACPI_RESOURCE *)srsbuf.Pointer;
884 	end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
885 	for (;;) {
886 		if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
887 			break;
888 		switch (resource->Type) {
889 		case ACPI_RESOURCE_TYPE_IRQ:
890 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
891 			MPASS(i < sc->pl_num_links);
892 
893 			/*
894 			 * Only configure the interrupt and update the
895 			 * weights if this link has a valid IRQ and was
896 			 * previously unrouted.
897 			 */
898 			if (!link->l_routed &&
899 			    PCI_INTERRUPT_VALID(link->l_irq)) {
900 				link->l_routed = TRUE;
901 				acpi_config_intr(dev, resource);
902 				pci_link_interrupt_weights[link->l_irq] +=
903 				    link->l_references;
904 			}
905 			link++;
906 			i++;
907 			break;
908 		}
909 		resource = ACPI_NEXT_RESOURCE(resource);
910 		if (resource >= end)
911 			break;
912 	}
913 	AcpiOsFree(srsbuf.Pointer);
914 	return (AE_OK);
915 }
916 
917 static int
918 acpi_pci_link_resume(device_t dev)
919 {
920 	struct acpi_pci_link_softc *sc;
921 	ACPI_STATUS status;
922 	int i, routed;
923 
924 	/*
925 	 * If all of our links are routed, then restore the link via _SRS,
926 	 * otherwise, disable the link via _DIS.
927 	 */
928 	ACPI_SERIAL_BEGIN(pci_link);
929 	sc = device_get_softc(dev);
930 	routed = 0;
931 	for (i = 0; i < sc->pl_num_links; i++)
932 		if (sc->pl_links[i].l_routed)
933 			routed++;
934 	if (routed == sc->pl_num_links)
935 		status = acpi_pci_link_route_irqs(dev);
936 	else {
937 		AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL);
938 		status = AE_OK;
939 	}
940 	ACPI_SERIAL_END(pci_link);
941 	if (ACPI_FAILURE(status))
942 		return (ENXIO);
943 	else
944 		return (0);
945 }
946 
947 /*
948  * Pick an IRQ to use for this unrouted link.
949  */
950 static uint8_t
951 acpi_pci_link_choose_irq(device_t dev, struct link *link)
952 {
953 	char tunable_buffer[64], link_name[5];
954 	u_int8_t best_irq, pos_irq;
955 	int best_weight, pos_weight, i;
956 
957 	KASSERT(!link->l_routed, ("%s: link already routed", __func__));
958 	KASSERT(!PCI_INTERRUPT_VALID(link->l_irq),
959 	    ("%s: link already has an IRQ", __func__));
960 
961 	/* Check for a tunable override. */
962 	if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name,
963 	    sizeof(link_name)))) {
964 		snprintf(tunable_buffer, sizeof(tunable_buffer),
965 		    "hw.pci.link.%s.%d.irq", link_name, link->l_res_index);
966 		if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
967 			if (!link_valid_irq(link, i))
968 				device_printf(dev,
969 				    "Warning, IRQ %d is not listed as valid\n",
970 				    i);
971 			return (i);
972 		}
973 		snprintf(tunable_buffer, sizeof(tunable_buffer),
974 		    "hw.pci.link.%s.irq", link_name);
975 		if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
976 			if (!link_valid_irq(link, i))
977 				device_printf(dev,
978 				    "Warning, IRQ %d is not listed as valid\n",
979 				    i);
980 			return (i);
981 		}
982 	}
983 
984 	/*
985 	 * If we have a valid BIOS IRQ, use that.  We trust what the BIOS
986 	 * says it routed over what _CRS says the link thinks is routed.
987 	 */
988 	if (PCI_INTERRUPT_VALID(link->l_bios_irq))
989 		return (link->l_bios_irq);
990 
991 	/*
992 	 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
993 	 * then use that.
994 	 */
995 	if (PCI_INTERRUPT_VALID(link->l_initial_irq))
996 		return (link->l_initial_irq);
997 
998 	/*
999 	 * Ok, we have no useful hints, so we have to pick from the
1000 	 * possible IRQs.  For ISA IRQs we only use interrupts that
1001 	 * have already been used by the BIOS.
1002 	 */
1003 	best_irq = PCI_INVALID_IRQ;
1004 	best_weight = INT_MAX;
1005 	for (i = 0; i < link->l_num_irqs; i++) {
1006 		pos_irq = link->l_irqs[i];
1007 		if (pos_irq < NUM_ISA_INTERRUPTS &&
1008 		    (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
1009 			continue;
1010 		pos_weight = pci_link_interrupt_weights[pos_irq];
1011 		if (pos_weight < best_weight) {
1012 			best_weight = pos_weight;
1013 			best_irq = pos_irq;
1014 		}
1015 	}
1016 
1017 	/*
1018 	 * If this is an ISA IRQ, try using the SCI if it is also an ISA
1019 	 * interrupt as a fallback.
1020 	 */
1021 	if (link->l_isa_irq) {
1022 		pos_irq = AcpiGbl_FADT.SciInterrupt;
1023 		pos_weight = pci_link_interrupt_weights[pos_irq];
1024 		if (pos_weight < best_weight) {
1025 			best_weight = pos_weight;
1026 			best_irq = pos_irq;
1027 		}
1028 	}
1029 
1030 	if (PCI_INTERRUPT_VALID(best_irq)) {
1031 		if (bootverbose)
1032 			device_printf(dev, "Picked IRQ %u with weight %d\n",
1033 			    best_irq, best_weight);
1034 	} else
1035 		device_printf(dev, "Unable to choose an IRQ\n");
1036 	return (best_irq);
1037 }
1038 
1039 int
1040 acpi_pci_link_route_interrupt(device_t dev, int index)
1041 {
1042 	struct link *link;
1043 
1044 	if (acpi_disabled("pci_link"))
1045 		return (PCI_INVALID_IRQ);
1046 
1047 	ACPI_SERIAL_BEGIN(pci_link);
1048 	link = acpi_pci_link_lookup(dev, index);
1049 	if (link == NULL)
1050 		panic("%s: apparently invalid index %d", __func__, index);
1051 
1052 	/*
1053 	 * If this link device is already routed to an interrupt, just return
1054 	 * the interrupt it is routed to.
1055 	 */
1056 	if (link->l_routed) {
1057 		KASSERT(PCI_INTERRUPT_VALID(link->l_irq),
1058 		    ("%s: link is routed but has an invalid IRQ", __func__));
1059 		ACPI_SERIAL_END(pci_link);
1060 		return (link->l_irq);
1061 	}
1062 
1063 	/* Choose an IRQ if we need one. */
1064 	if (!PCI_INTERRUPT_VALID(link->l_irq)) {
1065 		link->l_irq = acpi_pci_link_choose_irq(dev, link);
1066 
1067 		/*
1068 		 * Try to route the interrupt we picked.  If it fails, then
1069 		 * assume the interrupt is not routed.
1070 		 */
1071 		if (PCI_INTERRUPT_VALID(link->l_irq)) {
1072 			acpi_pci_link_route_irqs(dev);
1073 			if (!link->l_routed)
1074 				link->l_irq = PCI_INVALID_IRQ;
1075 		}
1076 	}
1077 	ACPI_SERIAL_END(pci_link);
1078 
1079 	return (link->l_irq);
1080 }
1081 
1082 /*
1083  * This is gross, but we abuse the identify routine to perform one-time
1084  * SYSINIT() style initialization for the driver.
1085  */
1086 static void
1087 acpi_pci_link_identify(driver_t *driver, device_t parent)
1088 {
1089 
1090 	/*
1091 	 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1092 	 * ISA IRQs.
1093 	 *
1094 	 * XXX: If we are using the APIC, the SCI might have been
1095 	 * rerouted to an APIC pin in which case this is invalid.  However,
1096 	 * if we are using the APIC, we also shouldn't be having any PCI
1097 	 * interrupts routed via ISA IRQs, so this is probably ok.
1098 	 */
1099 	if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS)
1100 		pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1101 }
1102 
1103 static device_method_t acpi_pci_link_methods[] = {
1104 	/* Device interface */
1105 	DEVMETHOD(device_identify,	acpi_pci_link_identify),
1106 	DEVMETHOD(device_probe,		acpi_pci_link_probe),
1107 	DEVMETHOD(device_attach,	acpi_pci_link_attach),
1108 	DEVMETHOD(device_resume,	acpi_pci_link_resume),
1109 
1110 	DEVMETHOD_END
1111 };
1112 
1113 static driver_t acpi_pci_link_driver = {
1114 	"pci_link",
1115 	acpi_pci_link_methods,
1116 	sizeof(struct acpi_pci_link_softc),
1117 };
1118 
1119 static devclass_t pci_link_devclass;
1120 
1121 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 0,
1122     0);
1123 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);
1124