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