xref: /freebsd/sys/dev/acpica/acpi_pci.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 2000, BSDi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_bus.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 
41 #include "acpi.h"
42 
43 #include <dev/acpica/acpivar.h>
44 
45 #include <sys/pciio.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pci_private.h>
49 
50 #include "pcib_if.h"
51 #include "pci_if.h"
52 
53 /*
54  * Hooks for the ACPI CA debugging infrastructure
55  */
56 #define _COMPONENT	ACPI_BUS
57 ACPI_MODULE_NAME("PCI")
58 
59 struct acpi_pci_devinfo {
60 	struct pci_devinfo ap_dinfo;
61 	ACPI_HANDLE	ap_handle;
62 };
63 
64 static int	acpi_pci_probe(device_t dev);
65 static int	acpi_pci_attach(device_t dev);
66 static int	acpi_pci_read_ivar(device_t dev, device_t child, int which,
67     uintptr_t *result);
68 #if 0
69 static int	acpi_pci_set_powerstate_method(device_t dev, device_t child,
70     int state);
71 static int	acpi_pci_get_powerstate_method(device_t dev, device_t child);
72 #endif
73 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level,
74     void *context, void **status);
75 
76 static device_method_t acpi_pci_methods[] = {
77 	/* Device interface */
78 	DEVMETHOD(device_probe,		acpi_pci_probe),
79 	DEVMETHOD(device_attach,	acpi_pci_attach),
80 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
81 	DEVMETHOD(device_suspend,	bus_generic_suspend),
82 	DEVMETHOD(device_resume,	pci_resume),
83 
84 	/* Bus interface */
85 	DEVMETHOD(bus_print_child,	pci_print_child),
86 	DEVMETHOD(bus_probe_nomatch,	pci_probe_nomatch),
87 	DEVMETHOD(bus_read_ivar,	acpi_pci_read_ivar),
88 	DEVMETHOD(bus_write_ivar,	pci_write_ivar),
89 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
90 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
91 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
92 
93 	DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
94 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
95 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
96 	DEVMETHOD(bus_delete_resource,	pci_delete_resource),
97 	DEVMETHOD(bus_alloc_resource,	pci_alloc_resource),
98 	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
99 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
100 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
101 	DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method),
102 	DEVMETHOD(bus_child_location_str, pci_child_location_str_method),
103 
104 	/* PCI interface */
105 	DEVMETHOD(pci_read_config,	pci_read_config_method),
106 	DEVMETHOD(pci_write_config,	pci_write_config_method),
107 	DEVMETHOD(pci_enable_busmaster,	pci_enable_busmaster_method),
108 	DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
109 	DEVMETHOD(pci_enable_io,	pci_enable_io_method),
110 	DEVMETHOD(pci_disable_io,	pci_disable_io_method),
111 	/* XXX: We should override these two. */
112 	DEVMETHOD(pci_get_powerstate,	pci_get_powerstate_method),
113 	DEVMETHOD(pci_set_powerstate,	pci_set_powerstate_method),
114 	DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method),
115 
116 	{ 0, 0 }
117 };
118 
119 static driver_t acpi_pci_driver = {
120 	"pci",
121 	acpi_pci_methods,
122 	0,			/* no softc */
123 };
124 
125 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0);
126 MODULE_VERSION(acpi_pci, 1);
127 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1);
128 
129 static int
130 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
131 {
132     struct acpi_pci_devinfo *dinfo;
133 
134     switch (which) {
135     case  ACPI_IVAR_HANDLE:
136 	dinfo = device_get_ivars(child);
137 	*result = (uintptr_t)dinfo->ap_handle;
138 	return(0);
139     }
140     return(pci_read_ivar(dev, child, which, result));
141 }
142 
143 #if 0
144 /*
145  * PCI power manangement
146  */
147 static int
148 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
149 {
150 	/* XXX: TODO */
151 	return (ENXIO);
152 }
153 
154 static int
155 acpi_pci_get_powerstate_method(device_t dev, device_t child)
156 {
157 	/* XXX: TODO */
158 	return (ENXIO);
159 }
160 #endif
161 
162 static ACPI_STATUS
163 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
164     void **status)
165 {
166 	struct acpi_pci_devinfo *dinfo;
167 	device_t *devlist;
168 	int devcount, i, func, slot;
169 	UINT32 address;
170 
171 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
172 
173 	if (ACPI_FAILURE(acpi_EvaluateInteger(handle, "_ADR", &address)))
174 		return_ACPI_STATUS(AE_OK);
175 	slot = address >> 16;
176 	func = address & 0xffff;
177 	if (device_get_children((device_t)context, &devlist, &devcount) != 0)
178 		return_ACPI_STATUS(AE_OK);
179 	for (i = 0; i < devcount; i++) {
180 		dinfo = device_get_ivars(devlist[i]);
181 		if (dinfo->ap_dinfo.cfg.func == func &&
182 		    dinfo->ap_dinfo.cfg.slot == slot) {
183 			dinfo->ap_handle = handle;
184 			free(devlist, M_TEMP);
185 			return_ACPI_STATUS(AE_OK);
186 		}
187 	}
188 	free(devlist, M_TEMP);
189 	return_ACPI_STATUS(AE_OK);
190 }
191 
192 static int
193 acpi_pci_probe(device_t dev)
194 {
195 
196 	if (pcib_get_bus(dev) < 0)
197 		return (ENXIO);
198 
199 	device_set_desc(dev, "ACPI PCI bus");
200 
201 	if (acpi_get_handle(dev) == NULL)
202 		return (ENXIO);
203 	return (0);
204 }
205 
206 static int
207 acpi_pci_attach(device_t dev)
208 {
209 	int busno;
210 
211 	/*
212 	 * Since there can be multiple independantly numbered PCI
213 	 * busses on some large alpha systems, we can't use the unit
214 	 * number to decide what bus we are probing. We ask the parent
215 	 * pcib what our bus number is.
216 	 */
217 	busno = pcib_get_bus(dev);
218 	if (bootverbose)
219 		device_printf(dev, "physical bus=%d\n", busno);
220 
221 	/*
222 	 * First, PCI devices are added as in the normal PCI bus driver.
223 	 * Afterwards, the ACPI namespace under the bridge driver is
224 	 * walked to save ACPI handles to all the devices that appear in
225 	 * the ACPI namespace as immediate descendants of the bridge.
226 	 *
227 	 * XXX: Sometimes PCI devices show up in the ACPI namespace that
228 	 * pci_add_children() doesn't find.  We currently just ignore
229 	 * these devices.
230 	 */
231 	pci_add_children(dev, busno, sizeof(struct acpi_pci_devinfo));
232 	(void) AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
233 	    acpi_pci_save_handle, dev, NULL);
234 
235 	return (bus_generic_attach(dev));
236 }
237