xref: /freebsd/sys/dev/acpica/acpi_pcib.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 
35 #include <contrib/dev/acpica/include/acpi.h>
36 #include <contrib/dev/acpica/include/accommon.h>
37 
38 #include <dev/acpica/acpivar.h>
39 #include <dev/acpica/acpi_pcibvar.h>
40 
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pcivar.h>
43 #include "pcib_if.h"
44 
45 /* Hooks for the ACPI CA debugging infrastructure. */
46 #define _COMPONENT	ACPI_BUS
47 ACPI_MODULE_NAME("PCI")
48 
49 ACPI_SERIAL_DECL(pcib, "ACPI PCI bus methods");
50 
51 /*
52  * For locking, we assume the caller is not concurrent since this is
53  * triggered by newbus methods.
54  */
55 
56 struct prt_lookup_request {
57     ACPI_PCI_ROUTING_TABLE *pr_entry;
58     u_int	pr_pin;
59     u_int	pr_slot;
60 };
61 
62 typedef	void prt_entry_handler(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
63 
64 static void	prt_attach_devices(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
65 static void	prt_lookup_device(ACPI_PCI_ROUTING_TABLE *entry, void *arg);
66 static void	prt_walk_table(ACPI_BUFFER *prt, prt_entry_handler *handler,
67 		    void *arg);
68 
69 static void
70 prt_walk_table(ACPI_BUFFER *prt, prt_entry_handler *handler, void *arg)
71 {
72     ACPI_PCI_ROUTING_TABLE *entry;
73     char *prtptr;
74 
75     /* First check to see if there is a table to walk. */
76     if (prt == NULL || prt->Pointer == NULL)
77 	return;
78 
79     /* Walk the table executing the handler function for each entry. */
80     prtptr = prt->Pointer;
81     entry = (ACPI_PCI_ROUTING_TABLE *)prtptr;
82     while (entry->Length != 0) {
83 	handler(entry, arg);
84 	prtptr += entry->Length;
85 	entry = (ACPI_PCI_ROUTING_TABLE *)prtptr;
86     }
87 }
88 
89 static void
90 prt_attach_devices(ACPI_PCI_ROUTING_TABLE *entry, void *arg)
91 {
92     ACPI_HANDLE handle;
93     device_t child, pcib;
94     int error;
95 
96     /* We only care about entries that reference a link device. */
97     if (entry->Source[0] == '\0')
98 	return;
99 
100     /*
101      * In practice, we only see SourceIndex's of 0 out in the wild.
102      * When indices != 0 have been found, they've been bugs in the ASL.
103      */
104     if (entry->SourceIndex != 0)
105 	return;
106 
107     /* Lookup the associated handle and device. */
108     pcib = (device_t)arg;
109     if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, entry->Source, &handle)))
110 	return;
111     child = acpi_get_device(handle);
112     if (child == NULL)
113 	return;
114 
115     /* If the device hasn't been probed yet, force it to do so. */
116     error = device_probe_and_attach(child);
117     if (error != 0) {
118 	device_printf(pcib, "failed to force attach of %s\n",
119 	    acpi_name(handle));
120 	return;
121     }
122 
123     /* Add a reference for a specific bus/device/pin tuple. */
124     acpi_pci_link_add_reference(child, entry->SourceIndex, pcib,
125 	ACPI_ADR_PCI_SLOT(entry->Address), entry->Pin);
126 }
127 
128 void
129 acpi_pcib_fetch_prt(device_t dev, ACPI_BUFFER *prt)
130 {
131     ACPI_STATUS status;
132 
133     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
134 
135     /*
136      * Get the PCI interrupt routing table for this bus.  If we can't
137      * get it, this is not an error but may reduce functionality.  There
138      * are several valid bridges in the field that do not have a _PRT, so
139      * only warn about missing tables if bootverbose is set.
140      */
141     prt->Length = ACPI_ALLOCATE_BUFFER;
142     status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
143     if (ACPI_FAILURE(status) && (bootverbose || status != AE_NOT_FOUND))
144 	device_printf(dev,
145 	    "could not get PCI interrupt routing table for %s - %s\n",
146 	    acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
147 
148     /*
149      * Ensure all the link devices are attached.
150      */
151     prt_walk_table(prt, prt_attach_devices, dev);
152 }
153 
154 static void
155 prt_lookup_device(ACPI_PCI_ROUTING_TABLE *entry, void *arg)
156 {
157     struct prt_lookup_request *pr;
158 
159     pr = (struct prt_lookup_request *)arg;
160     if (pr->pr_entry != NULL)
161 	return;
162 
163     /*
164      * Compare the slot number (high word of Address) and pin number
165      * (note that ACPI uses 0 for INTA) to check for a match.
166      *
167      * Note that the low word of the Address field (function number)
168      * is required by the specification to be 0xffff.  We don't risk
169      * checking it here.
170      */
171     if (ACPI_ADR_PCI_SLOT(entry->Address) == pr->pr_slot &&
172 	entry->Pin == pr->pr_pin)
173 	    pr->pr_entry = entry;
174 }
175 
176 /*
177  * Route an interrupt for a child of the bridge.
178  */
179 int
180 acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin,
181     ACPI_BUFFER *prtbuf)
182 {
183     ACPI_PCI_ROUTING_TABLE *prt;
184     struct prt_lookup_request pr;
185     ACPI_HANDLE lnkdev;
186     int interrupt;
187 
188     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
189 
190     lnkdev = NULL;
191     interrupt = PCI_INVALID_IRQ;
192 
193     /* ACPI numbers pins 0-3, not 1-4 like the BIOS. */
194     pin--;
195 
196     ACPI_SERIAL_BEGIN(pcib);
197 
198     /* Search for a matching entry in the routing table. */
199     pr.pr_entry = NULL;
200     pr.pr_pin = pin;
201     pr.pr_slot = pci_get_slot(dev);
202     prt_walk_table(prtbuf, prt_lookup_device, &pr);
203     if (pr.pr_entry == NULL) {
204 	device_printf(pcib, "no PRT entry for %d.%d.INT%c\n", pci_get_bus(dev),
205 	    pci_get_slot(dev), 'A' + pin);
206 	goto out;
207     }
208     prt = pr.pr_entry;
209 
210     if (bootverbose) {
211 	device_printf(pcib, "matched entry for %d.%d.INT%c",
212 	    pci_get_bus(dev), pci_get_slot(dev), 'A' + pin);
213 	if (prt->Source[0] != '\0')
214 		printf(" (src %s:%u)", prt->Source, prt->SourceIndex);
215 	printf("\n");
216     }
217 
218     /*
219      * If source is empty/NULL, the source index is a global IRQ number
220      * and it's hard-wired so we're done.
221      *
222      * XXX: If the source index is non-zero, ignore the source device and
223      * assume that this is a hard-wired entry.
224      */
225     if (prt->Source[0] == '\0' || prt->SourceIndex != 0) {
226 	if (bootverbose)
227 	    device_printf(pcib, "slot %d INT%c hardwired to IRQ %d\n",
228 		pci_get_slot(dev), 'A' + pin, prt->SourceIndex);
229 	if (prt->SourceIndex) {
230 	    interrupt = prt->SourceIndex;
231 	    BUS_CONFIG_INTR(dev, interrupt, INTR_TRIGGER_LEVEL,
232 		INTR_POLARITY_LOW);
233 	} else
234 	    device_printf(pcib, "error: invalid hard-wired IRQ of 0\n");
235 	goto out;
236     }
237 
238     /*
239      * We have to find the source device (PCI interrupt link device).
240      */
241     if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
242 	device_printf(pcib, "couldn't find PCI interrupt link device %s\n",
243 	    prt->Source);
244 	goto out;
245     }
246     interrupt = acpi_pci_link_route_interrupt(acpi_get_device(lnkdev),
247 	prt->SourceIndex);
248 
249     if (bootverbose && PCI_INTERRUPT_VALID(interrupt))
250 	device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n",
251 	    pci_get_slot(dev), 'A' + pin, interrupt, acpi_name(lnkdev));
252 
253 out:
254     ACPI_SERIAL_END(pcib);
255 #ifdef INTRNG
256     if (PCI_INTERRUPT_VALID(interrupt)) {
257 	interrupt  = acpi_map_intr(dev, interrupt, lnkdev);
258 	KASSERT(PCI_INTERRUPT_VALID(interrupt), ("mapping fail"));
259     }
260 #endif
261     return_VALUE(interrupt);
262 }
263 
264 int
265 acpi_pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
266 {
267     device_t acpi_dev;
268 
269     acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
270     acpi_device_pwr_for_sleep(acpi_dev, dev, pstate);
271     return (0);
272 }
273 
274 int
275 acpi_pcib_get_cpus(device_t pcib, device_t dev, enum cpu_sets op,
276     size_t setsize, cpuset_t *cpuset)
277 {
278 
279 	return (bus_get_cpus(pcib, op, setsize, cpuset));
280 }
281 
282 int
283 acpi_pcib_osc(device_t pcib, uint32_t *ap_osc_ctl, uint32_t osc_ctl)
284 {
285 	ACPI_STATUS status;
286 	ACPI_HANDLE handle;
287 	uint32_t cap_set[3];
288 
289 	static uint8_t pci_host_bridge_uuid[ACPI_UUID_LENGTH] = {
290 		0x5b, 0x4d, 0xdb, 0x33, 0xf7, 0x1f, 0x1c, 0x40,
291 		0x96, 0x57, 0x74, 0x41, 0xc0, 0x3d, 0xd7, 0x66
292 	};
293 
294 	/*
295 	 * Don't invoke _OSC if a control is already granted.
296 	 * However, always invoke _OSC during attach when 0 is passed.
297 	 */
298 	if (osc_ctl != 0 && (*ap_osc_ctl & osc_ctl) == osc_ctl)
299 		return (0);
300 
301 	/* Support Field: Extended PCI Config Space, PCI Segment Groups, MSI */
302 	cap_set[PCI_OSC_SUPPORT] = PCIM_OSC_SUPPORT_EXT_PCI_CONF |
303 	    PCIM_OSC_SUPPORT_SEG_GROUP | PCIM_OSC_SUPPORT_MSI;
304 	/* Active State Power Management, Clock Power Management Capability */
305 	if (pci_enable_aspm)
306 		cap_set[PCI_OSC_SUPPORT] |= PCIM_OSC_SUPPORT_ASPM |
307 		    PCIM_OSC_SUPPORT_CPMC;
308 
309 	/* Control Field */
310 	cap_set[PCI_OSC_CTL] = *ap_osc_ctl | osc_ctl;
311 
312 	handle = acpi_get_handle(pcib);
313 	status = acpi_EvaluateOSC(handle, pci_host_bridge_uuid, 1,
314 	    nitems(cap_set), cap_set, cap_set, false);
315 	if (ACPI_FAILURE(status)) {
316 		if (status == AE_NOT_FOUND) {
317 			*ap_osc_ctl |= osc_ctl;
318 			return (0);
319 		}
320 		device_printf(pcib, "_OSC failed: %s\n",
321 		    AcpiFormatException(status));
322 		return (EIO);
323 	}
324 
325 	/*
326 	 * _OSC may return an error in the status word, but will
327 	 * update the control mask always.  _OSC should not revoke
328 	 * previously-granted controls.
329 	 */
330 	if ((cap_set[PCI_OSC_CTL] & *ap_osc_ctl) != *ap_osc_ctl)
331 		device_printf(pcib, "_OSC revoked %#x\n",
332 		    (cap_set[PCI_OSC_CTL] & *ap_osc_ctl) ^ *ap_osc_ctl);
333 	*ap_osc_ctl = cap_set[PCI_OSC_CTL];
334 	if ((*ap_osc_ctl & osc_ctl) != osc_ctl)
335 		return (EIO);
336 
337 	return (0);
338 }
339