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