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