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