xref: /freebsd/sys/dev/acpica/acpi_pci_link.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
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 	struct link_res_request *req;
272 	struct link *link;
273 	UINT8 *irqs = NULL;
274 	UINT32 *ext_irqs = NULL;
275 	int i, is_ext_irq = 1;
276 
277 	ACPI_SERIAL_ASSERT(pci_link);
278 	req = (struct link_res_request *)context;
279 	switch (res->Type) {
280 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
281 		switch (req->in_dpf) {
282 		case DPF_OUTSIDE:
283 			/* We've started the first DPF. */
284 			req->in_dpf = DPF_FIRST;
285 			break;
286 		case DPF_FIRST:
287 			/* We've started the second DPF. */
288 			req->in_dpf = DPF_IGNORE;
289 			break;
290 		}
291 		break;
292 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
293 		/* We are finished with DPF parsing. */
294 		KASSERT(req->in_dpf != DPF_OUTSIDE,
295 		    ("%s: end dpf when not parsing a dpf", __func__));
296 		req->in_dpf = DPF_OUTSIDE;
297 		break;
298 	case ACPI_RESOURCE_TYPE_IRQ:
299 		is_ext_irq = 0;
300 		/* fall through */
301 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
302 		/*
303 		 * Don't parse resources if we are in a DPF set that we are
304 		 * ignoring.
305 		 */
306 		if (req->in_dpf == DPF_IGNORE)
307 			break;
308 
309 		KASSERT(req->link_index < req->sc->pl_num_links,
310 		    ("%s: array boundary violation", __func__));
311 		link = &req->sc->pl_links[req->link_index];
312 		if (link->l_res_index == -1) {
313 			KASSERT(req->sc->pl_crs_bad,
314 			    ("res_index should be set"));
315 			link->l_res_index = req->res_index;
316 		}
317 		req->link_index++;
318 		req->res_index++;
319 
320 		/*
321 		 * Stash a copy of the resource for later use when doing
322 		 * _SRS.
323 		 */
324 		bcopy(res, &link->l_prs_template, sizeof(ACPI_RESOURCE));
325 		if (is_ext_irq) {
326 			link->l_num_irqs =
327 			    res->Data.ExtendedIrq.InterruptCount;
328 			ext_irqs = res->Data.ExtendedIrq.Interrupts;
329 		} else {
330 			link->l_num_irqs = res->Data.Irq.InterruptCount;
331 			irqs = res->Data.Irq.Interrupts;
332 		}
333 		if (link->l_num_irqs == 0)
334 			break;
335 
336 		/*
337 		 * Save a list of the valid IRQs.  Also, if all of the
338 		 * valid IRQs are ISA IRQs, then mark this link as
339 		 * routed via an ISA interrupt.
340 		 */
341 		link->l_isa_irq = TRUE;
342 		link->l_irqs = malloc(sizeof(int) * link->l_num_irqs,
343 		    M_PCI_LINK, M_WAITOK | M_ZERO);
344 		for (i = 0; i < link->l_num_irqs; i++) {
345 			if (is_ext_irq) {
346 				link->l_irqs[i] = ext_irqs[i];
347 				if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
348 					link->l_isa_irq = FALSE;
349 			} else {
350 				link->l_irqs[i] = irqs[i];
351 				if (irqs[i] >= NUM_ISA_INTERRUPTS)
352 					link->l_isa_irq = FALSE;
353 			}
354 		}
355 		break;
356 	default:
357 		if (req->in_dpf == DPF_IGNORE)
358 			break;
359 		if (req->sc->pl_crs_bad)
360 			device_printf(req->sc->pl_dev,
361 		    "Warning: possible resource %d will be lost during _SRS\n",
362 			    req->res_index);
363 		req->res_index++;
364 	}
365 	return (AE_OK);
366 }
367 
368 static int
369 link_valid_irq(struct link *link, int irq)
370 {
371 	int i;
372 
373 	ACPI_SERIAL_ASSERT(pci_link);
374 
375 	/* Invalid interrupts are never valid. */
376 	if (!PCI_INTERRUPT_VALID(irq))
377 		return (FALSE);
378 
379 	/* Any interrupt in the list of possible interrupts is valid. */
380 	for (i = 0; i < link->l_num_irqs; i++)
381 		if (link->l_irqs[i] == irq)
382 			 return (TRUE);
383 
384 	/*
385 	 * For links routed via an ISA interrupt, if the SCI is routed via
386 	 * an ISA interrupt, the SCI is always treated as a valid IRQ.
387 	 */
388 	if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
389 	    irq < NUM_ISA_INTERRUPTS)
390 		return (TRUE);
391 
392 	/* If the interrupt wasn't found in the list it is not valid. */
393 	return (FALSE);
394 }
395 
396 static void
397 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
398 {
399 	struct link *link;
400 	char buf[16];
401 	int i, j;
402 
403 	ACPI_SERIAL_ASSERT(pci_link);
404 	if (header) {
405 		snprintf(buf, sizeof(buf), "%s:",
406 		    device_get_nameunit(sc->pl_dev));
407 		printf("%-16.16s  Index  IRQ  Rtd  Ref  IRQs\n", buf);
408 	}
409 	for (i = 0; i < sc->pl_num_links; i++) {
410 		link = &sc->pl_links[i];
411 		printf("  %-14.14s  %5d  %3d   %c   %3d ", i == 0 ? tag : "", i,
412 		    link->l_irq, link->l_routed ? 'Y' : 'N',
413 		    link->l_references);
414 		if (link->l_num_irqs == 0)
415 			printf(" none");
416 		else for (j = 0; j < link->l_num_irqs; j++)
417 			printf(" %d", link->l_irqs[j]);
418 		printf("\n");
419 	}
420 }
421 
422 static int
423 acpi_pci_link_attach(device_t dev)
424 {
425 	struct acpi_pci_link_softc *sc;
426 	struct link_count_request creq;
427 	struct link_res_request rreq;
428 	ACPI_STATUS status;
429 	int i;
430 
431 	sc = device_get_softc(dev);
432 	sc->pl_dev = dev;
433 	ACPI_SERIAL_BEGIN(pci_link);
434 
435 	/*
436 	 * Count the number of current resources so we know how big of
437 	 * a link array to allocate.  On some systems, _CRS is broken,
438 	 * so for those systems try to derive the count from _PRS instead.
439 	 */
440 	creq.in_dpf = DPF_OUTSIDE;
441 	creq.count = 0;
442 	status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
443 	    acpi_count_irq_resources, &creq);
444 	sc->pl_crs_bad = ACPI_FAILURE(status);
445 	if (sc->pl_crs_bad) {
446 		creq.in_dpf = DPF_OUTSIDE;
447 		creq.count = 0;
448 		status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
449 		    acpi_count_irq_resources, &creq);
450 		if (ACPI_FAILURE(status)) {
451 			device_printf(dev,
452 			    "Unable to parse _CRS or _PRS: %s\n",
453 			    AcpiFormatException(status));
454 			ACPI_SERIAL_END(pci_link);
455 			return (ENXIO);
456 		}
457 	}
458 	sc->pl_num_links = creq.count;
459 	if (creq.count == 0) {
460 		ACPI_SERIAL_END(pci_link);
461 		return (0);
462 	}
463 	sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links,
464 	    M_PCI_LINK, M_WAITOK | M_ZERO);
465 
466 	/* Initialize the child links. */
467 	for (i = 0; i < sc->pl_num_links; i++) {
468 		sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
469 		sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
470 		sc->pl_links[i].l_sc = sc;
471 		sc->pl_links[i].l_isa_irq = FALSE;
472 		sc->pl_links[i].l_res_index = -1;
473 	}
474 
475 	/* Try to read the current settings from _CRS if it is valid. */
476 	if (!sc->pl_crs_bad) {
477 		rreq.in_dpf = DPF_OUTSIDE;
478 		rreq.link_index = 0;
479 		rreq.res_index = 0;
480 		rreq.sc = sc;
481 		status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
482 		    link_add_crs, &rreq);
483 		if (ACPI_FAILURE(status)) {
484 			device_printf(dev, "Unable to parse _CRS: %s\n",
485 			    AcpiFormatException(status));
486 			goto fail;
487 		}
488 	}
489 
490 	/*
491 	 * Try to read the possible settings from _PRS.  Note that if the
492 	 * _CRS is toast, we depend on having a working _PRS.  However, if
493 	 * _CRS works, then it is ok for _PRS to be missing.
494 	 */
495 	rreq.in_dpf = DPF_OUTSIDE;
496 	rreq.link_index = 0;
497 	rreq.res_index = 0;
498 	rreq.sc = sc;
499 	status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
500 	    link_add_prs, &rreq);
501 	if (ACPI_FAILURE(status) &&
502 	    (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
503 		device_printf(dev, "Unable to parse _PRS: %s\n",
504 		    AcpiFormatException(status));
505 		goto fail;
506 	}
507 	if (bootverbose)
508 		acpi_pci_link_dump(sc, 1, "Initial Probe");
509 
510 	/* Verify initial IRQs if we have _PRS. */
511 	if (status != AE_NOT_FOUND)
512 		for (i = 0; i < sc->pl_num_links; i++)
513 			if (!link_valid_irq(&sc->pl_links[i],
514 			    sc->pl_links[i].l_irq))
515 				sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
516 	if (bootverbose)
517 		acpi_pci_link_dump(sc, 0, "Validation");
518 
519 	/* Save initial IRQs. */
520 	for (i = 0; i < sc->pl_num_links; i++)
521 		sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
522 
523 	/*
524 	 * Try to disable this link.  If successful, set the current IRQ to
525 	 * zero and flags to indicate this link is not routed.  If we can't
526 	 * run _DIS (i.e., the method doesn't exist), assume the initial
527 	 * IRQ was routed by the BIOS.
528 	 */
529 	if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
530 	    NULL)))
531 		for (i = 0; i < sc->pl_num_links; i++)
532 			sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
533 	else
534 		for (i = 0; i < sc->pl_num_links; i++)
535 			if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
536 				sc->pl_links[i].l_routed = TRUE;
537 	if (bootverbose)
538 		acpi_pci_link_dump(sc, 0, "After Disable");
539 	ACPI_SERIAL_END(pci_link);
540 	return (0);
541 fail:
542 	ACPI_SERIAL_END(pci_link);
543 	for (i = 0; i < sc->pl_num_links; i++)
544 		if (sc->pl_links[i].l_irqs != NULL)
545 			free(sc->pl_links[i].l_irqs, M_PCI_LINK);
546 	free(sc->pl_links, M_PCI_LINK);
547 	return (ENXIO);
548 }
549 
550 /* XXX: Note that this is identical to pci_pir_search_irq(). */
551 static uint8_t
552 acpi_pci_link_search_irq(int bus, int device, int pin)
553 {
554 	uint32_t value;
555 	uint8_t func, maxfunc;
556 
557 	/* See if we have a valid device at function 0. */
558 	value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
559 	if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
560 		return (PCI_INVALID_IRQ);
561 	if (value & PCIM_MFDEV)
562 		maxfunc = PCI_FUNCMAX;
563 	else
564 		maxfunc = 0;
565 
566 	/* Scan all possible functions at this device. */
567 	for (func = 0; func <= maxfunc; func++) {
568 		value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
569 		if (value == 0xffffffff)
570 			continue;
571 		value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
572 
573 		/*
574 		 * See if it uses the pin in question.  Note that the passed
575 		 * in pin uses 0 for A, .. 3 for D whereas the intpin
576 		 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
577 		 */
578 		if (value != pin + 1)
579 			continue;
580 		value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
581 		if (bootverbose)
582 			printf(
583 		"ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
584 			    bus, device, pin + 'A', func, value);
585 		if (value != PCI_INVALID_IRQ)
586 			return (value);
587 	}
588 	return (PCI_INVALID_IRQ);
589 }
590 
591 /*
592  * Find the link structure that corresponds to the resource index passed in
593  * via 'source_index'.
594  */
595 static struct link *
596 acpi_pci_link_lookup(device_t dev, int source_index)
597 {
598 	struct acpi_pci_link_softc *sc;
599 	int i;
600 
601 	ACPI_SERIAL_ASSERT(pci_link);
602 	sc = device_get_softc(dev);
603 	for (i = 0; i < sc->pl_num_links; i++)
604 		if (sc->pl_links[i].l_res_index == source_index)
605 			return (&sc->pl_links[i]);
606 	return (NULL);
607 }
608 
609 void
610 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
611     int pin)
612 {
613 	struct link *link;
614 	uint8_t bios_irq;
615 	uintptr_t bus;
616 
617 	/*
618 	 * Look up the PCI bus for the specified PCI bridge device.  Note
619 	 * that the PCI bridge device might not have any children yet.
620 	 * However, looking up its bus number doesn't require a valid child
621 	 * device, so we just pass NULL.
622 	 */
623 	if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
624 		device_printf(pcib, "Unable to read PCI bus number");
625 		panic("PCI bridge without a bus number");
626 	}
627 
628 	/* Bump the reference count. */
629 	ACPI_SERIAL_BEGIN(pci_link);
630 	link = acpi_pci_link_lookup(dev, index);
631 	if (link == NULL) {
632 		device_printf(dev, "apparently invalid index %d\n", index);
633 		ACPI_SERIAL_END(pci_link);
634 		return;
635 	}
636 	link->l_references++;
637 	if (link->l_routed)
638 		pci_link_interrupt_weights[link->l_irq]++;
639 
640 	/*
641 	 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
642 	 * (8259As).  Thus, if this link is routed via an ISA IRQ, go
643 	 * look to see if the BIOS routed an IRQ for this link at the
644 	 * indicated (bus, slot, pin).  If so, we prefer that IRQ for
645 	 * this link and add that IRQ to our list of known-good IRQs.
646 	 * This provides a good work-around for link devices whose _CRS
647 	 * method is either broken or bogus.  We only use the value
648 	 * returned by _CRS if we can't find a valid IRQ via this method
649 	 * in fact.
650 	 *
651 	 * If this link is not routed via an ISA IRQ (because we are using
652 	 * APIC for example), then don't bother looking up the BIOS IRQ
653 	 * as if we find one it won't be valid anyway.
654 	 */
655 	if (!link->l_isa_irq) {
656 		ACPI_SERIAL_END(pci_link);
657 		return;
658 	}
659 
660 	/* Try to find a BIOS IRQ setting from any matching devices. */
661 	bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
662 	if (!PCI_INTERRUPT_VALID(bios_irq)) {
663 		ACPI_SERIAL_END(pci_link);
664 		return;
665 	}
666 
667 	/* Validate the BIOS IRQ. */
668 	if (!link_valid_irq(link, bios_irq)) {
669 		device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
670 		    bios_irq, (int)bus, slot, pin + 'A');
671 	} else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
672 		link->l_bios_irq = bios_irq;
673 		if (bios_irq < NUM_ISA_INTERRUPTS)
674 			pci_link_bios_isa_irqs |= (1 << bios_irq);
675 		if (bios_irq != link->l_initial_irq &&
676 		    PCI_INTERRUPT_VALID(link->l_initial_irq))
677 			device_printf(dev,
678 			    "BIOS IRQ %u does not match initial IRQ %u\n",
679 			    bios_irq, link->l_initial_irq);
680 	} else if (bios_irq != link->l_bios_irq)
681 		device_printf(dev,
682 	    "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
683 		    bios_irq, (int)bus, slot, pin + 'A',
684 		    link->l_bios_irq);
685 	ACPI_SERIAL_END(pci_link);
686 }
687 
688 static ACPI_STATUS
689 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
690 {
691 	ACPI_RESOURCE *resource, *end, newres, *resptr;
692 	ACPI_BUFFER crsbuf;
693 	ACPI_STATUS status;
694 	struct link *link;
695 	int i, in_dpf;
696 
697 	/* Fetch the _CRS. */
698 	ACPI_SERIAL_ASSERT(pci_link);
699 	crsbuf.Pointer = NULL;
700 	crsbuf.Length = ACPI_ALLOCATE_BUFFER;
701 	status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), &crsbuf);
702 	if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL)
703 		status = AE_NO_MEMORY;
704 	if (ACPI_FAILURE(status)) {
705 		if (bootverbose)
706 			device_printf(sc->pl_dev,
707 			    "Unable to fetch current resources: %s\n",
708 			    AcpiFormatException(status));
709 		return (status);
710 	}
711 
712 	/* Fill in IRQ resources via link structures. */
713 	srsbuf->Pointer = NULL;
714 	link = sc->pl_links;
715 	i = 0;
716 	in_dpf = DPF_OUTSIDE;
717 	resource = (ACPI_RESOURCE *)crsbuf.Pointer;
718 	end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length);
719 	for (;;) {
720 		switch (resource->Type) {
721 		case ACPI_RESOURCE_TYPE_START_DEPENDENT:
722 			switch (in_dpf) {
723 			case DPF_OUTSIDE:
724 				/* We've started the first DPF. */
725 				in_dpf = DPF_FIRST;
726 				break;
727 			case DPF_FIRST:
728 				/* We've started the second DPF. */
729 				panic(
730 		"%s: Multiple dependent functions within a current resource",
731 				    __func__);
732 				break;
733 			}
734 			resptr = NULL;
735 			break;
736 		case ACPI_RESOURCE_TYPE_END_DEPENDENT:
737 			/* We are finished with DPF parsing. */
738 			KASSERT(in_dpf != DPF_OUTSIDE,
739 			    ("%s: end dpf when not parsing a dpf", __func__));
740 			in_dpf = DPF_OUTSIDE;
741 			resptr = NULL;
742 			break;
743 		case ACPI_RESOURCE_TYPE_IRQ:
744 			MPASS(i < sc->pl_num_links);
745 			MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ);
746 			newres = link->l_prs_template;
747 			resptr = &newres;
748 			resptr->Data.Irq.InterruptCount = 1;
749 			if (PCI_INTERRUPT_VALID(link->l_irq)) {
750 				KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
751 		("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
752 				    __func__, link->l_irq));
753 				resptr->Data.Irq.Interrupts[0] = link->l_irq;
754 			} else
755 				resptr->Data.Irq.Interrupts[0] = 0;
756 			link++;
757 			i++;
758 			break;
759 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
760 			MPASS(i < sc->pl_num_links);
761 			MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ);
762 			newres = link->l_prs_template;
763 			resptr = &newres;
764 			resptr->Data.ExtendedIrq.InterruptCount = 1;
765 			if (PCI_INTERRUPT_VALID(link->l_irq))
766 				resptr->Data.ExtendedIrq.Interrupts[0] =
767 				    link->l_irq;
768 			else
769 				resptr->Data.ExtendedIrq.Interrupts[0] = 0;
770 			link++;
771 			i++;
772 			break;
773 		default:
774 			resptr = resource;
775 		}
776 		if (resptr != NULL) {
777 			status = acpi_AppendBufferResource(srsbuf, resptr);
778 			if (ACPI_FAILURE(status)) {
779 				device_printf(sc->pl_dev,
780 				    "Unable to build resources: %s\n",
781 				    AcpiFormatException(status));
782 				if (srsbuf->Pointer != NULL)
783 					AcpiOsFree(srsbuf->Pointer);
784 				AcpiOsFree(crsbuf.Pointer);
785 				return (status);
786 			}
787 		}
788 		if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
789 			break;
790 		resource = ACPI_NEXT_RESOURCE(resource);
791 		if (resource >= end)
792 			break;
793 	}
794 	AcpiOsFree(crsbuf.Pointer);
795 	return (AE_OK);
796 }
797 
798 static ACPI_STATUS
799 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
800     ACPI_BUFFER *srsbuf)
801 {
802 	ACPI_RESOURCE newres;
803 	ACPI_STATUS status;
804 	struct link *link;
805 	int i;
806 
807 	/* Start off with an empty buffer. */
808 	srsbuf->Pointer = NULL;
809 	link = sc->pl_links;
810 	for (i = 0; i < sc->pl_num_links; i++) {
811 
812 		/* Add a new IRQ resource from each link. */
813 		link = &sc->pl_links[i];
814 		newres = link->l_prs_template;
815 		if (newres.Type == ACPI_RESOURCE_TYPE_IRQ) {
816 
817 			/* Build an IRQ resource. */
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 
828 			/* Build an ExtIRQ resuorce. */
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 	{0, 0}
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