1 /*
2  *  pci_bind.c - ACPI PCI Device Binding ($Revision: 3 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25 
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/pm.h>
33 #include <linux/pci.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37 
38 
39 #define _COMPONENT		ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME		("pci_bind")
41 
42 #define PREFIX			"ACPI: "
43 
44 
45 struct acpi_pci_data {
46 	struct acpi_pci_id	id;
47 	struct pci_bus		*bus;
48 	struct pci_dev		*dev;
49 };
50 
51 
52 void
acpi_pci_data_handler(acpi_handle handle,u32 function,void * context)53 acpi_pci_data_handler (
54 	acpi_handle		handle,
55 	u32			function,
56 	void			*context)
57 {
58 	ACPI_FUNCTION_TRACE("acpi_pci_data_handler");
59 
60 	/* TBD: Anything we need to do here? */
61 
62 	return_VOID;
63 }
64 
65 
66 /**
67  * acpi_os_get_pci_id
68  * ------------------
69  * This function is used by the ACPI Interpreter (a.k.a. Core Subsystem)
70  * to resolve PCI information for ACPI-PCI devices defined in the namespace.
71  * This typically occurs when resolving PCI operation region information.
72  */
73 acpi_status
acpi_os_get_pci_id(acpi_handle handle,struct acpi_pci_id * id)74 acpi_os_get_pci_id (
75 	acpi_handle		handle,
76 	struct acpi_pci_id	*id)
77 {
78 	int			result = 0;
79 	acpi_status		status = AE_OK;
80 	struct acpi_device	*device = NULL;
81 	struct acpi_pci_data	*data = NULL;
82 
83 	ACPI_FUNCTION_TRACE("acpi_os_get_pci_id");
84 
85 	if (!id)
86 		return_ACPI_STATUS(AE_BAD_PARAMETER);
87 
88 	result = acpi_bus_get_device(handle, &device);
89 	if (result) {
90 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
91 			"Invalid ACPI Bus context for device %s\n",
92 			acpi_device_bid(device)));
93 		return_ACPI_STATUS(AE_NOT_EXIST);
94 	}
95 
96 	status = acpi_get_data(handle, acpi_pci_data_handler, (void**) &data);
97 	if (ACPI_FAILURE(status) || !data || !data->dev) {
98 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
99 			"Invalid ACPI-PCI context for device %s\n",
100 			acpi_device_bid(device)));
101 		return_ACPI_STATUS(status);
102 	}
103 
104 	*id = data->id;
105 
106 	/*
107 	id->segment = data->id.segment;
108 	id->bus = data->id.bus;
109 	id->device = data->id.device;
110 	id->function = data->id.function;
111 	*/
112 
113 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
114 		"Device %s has PCI address %02x:%02x:%02x.%02x\n",
115 		acpi_device_bid(device), id->segment, id->bus,
116 		id->device, id->function));
117 
118 	return_ACPI_STATUS(AE_OK);
119 }
120 
121 
122 int
acpi_pci_bind(struct acpi_device * device)123 acpi_pci_bind (
124 	struct acpi_device	*device)
125 {
126 	int			result = 0;
127 	acpi_status		status = AE_OK;
128 	struct acpi_pci_data	*data = NULL;
129 	struct acpi_pci_data	*pdata = NULL;
130 	char			pathname[ACPI_PATHNAME_MAX] = {0};
131 	struct acpi_buffer	buffer = {ACPI_PATHNAME_MAX, pathname};
132 	acpi_handle		handle = NULL;
133 
134 	ACPI_FUNCTION_TRACE("acpi_pci_bind");
135 
136 	if (!device || !device->parent)
137 		return_VALUE(-EINVAL);
138 
139 	data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
140 	if (!data)
141 		return_VALUE(-ENOMEM);
142 	memset(data, 0, sizeof(struct acpi_pci_data));
143 
144 	acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
145 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
146 		pathname));
147 
148 	/*
149 	 * Segment & Bus
150 	 * -------------
151 	 * These are obtained via the parent device's ACPI-PCI context.
152 	 */
153 	status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
154 		(void**) &pdata);
155 	if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
156 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
157 			"Invalid ACPI-PCI context for parent device %s\n",
158 			acpi_device_bid(device->parent)));
159 		result = -ENODEV;
160 		goto end;
161 	}
162 	data->id.segment = pdata->id.segment;
163 	data->id.bus = pdata->bus->number;
164 
165 	/*
166 	 * Device & Function
167 	 * -----------------
168 	 * These are simply obtained from the device's _ADR method.  Note
169 	 * that a value of zero is valid.
170 	 */
171 	data->id.device = device->pnp.bus_address >> 16;
172 	data->id.function = device->pnp.bus_address & 0xFFFF;
173 
174 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
175 		data->id.segment, data->id.bus, data->id.device,
176 		data->id.function));
177 
178 	/*
179 	 * TBD: Support slot devices (e.g. function=0xFFFF).
180 	 */
181 
182 	/*
183 	 * Locate PCI Device
184 	 * -----------------
185 	 * Locate matching device in PCI namespace.  If it doesn't exist
186 	 * this typically means that the device isn't currently inserted
187 	 * (e.g. docking station, port replicator, etc.).
188 	 */
189 	data->dev = pci_find_slot(data->id.bus, PCI_DEVFN(data->id.device, data->id.function));
190 	if (!data->dev) {
191 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
192 			"Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
193 			data->id.segment, data->id.bus,
194 			data->id.device, data->id.function));
195 		result = -ENODEV;
196 		goto end;
197 	}
198 	if (!data->dev->bus) {
199 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
200 			"Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
201 			data->id.segment, data->id.bus,
202 			data->id.device, data->id.function));
203 		result = -ENODEV;
204 		goto end;
205 	}
206 
207 	/*
208 	 * PCI Bridge?
209 	 * -----------
210 	 * If so, set the 'bus' field and install the 'bind' function to
211 	 * facilitate callbacks for all of its children.
212 	 */
213 	if (data->dev->subordinate) {
214 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
215 			"Device %02x:%02x:%02x.%02x is a PCI bridge\n",
216 			data->id.segment, data->id.bus,
217 			data->id.device, data->id.function));
218 		data->bus = data->dev->subordinate;
219 		device->ops.bind = acpi_pci_bind;
220 	}
221 
222 	/*
223 	 * Attach ACPI-PCI Context
224 	 * -----------------------
225 	 * Thus binding the ACPI and PCI devices.
226 	 */
227 	status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
228 	if (ACPI_FAILURE(status)) {
229 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
230 			"Unable to attach ACPI-PCI context to device %s\n",
231 			acpi_device_bid(device)));
232 		result = -ENODEV;
233 		goto end;
234 	}
235 
236 	/*
237 	 * PCI Routing Table
238 	 * -----------------
239 	 * Evaluate and parse _PRT, if exists.  This code is independent of
240 	 * PCI bridges (above) to allow parsing of _PRT objects within the
241 	 * scope of non-bridge devices.  Note that _PRTs within the scope of
242 	 * a PCI bridge assume the bridge's subordinate bus number.
243 	 *
244 	 * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
245 	 */
246 	status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
247 	if (ACPI_SUCCESS(status)) {
248 		if (data->bus)				    /* PCI-PCI bridge */
249 			acpi_pci_irq_add_prt(device->handle, data->id.segment,
250 				data->bus->number);
251 		else				     /* non-bridge PCI device */
252 			acpi_pci_irq_add_prt(device->handle, data->id.segment,
253 				data->id.bus);
254 	}
255 
256 end:
257 	if (result)
258 		kfree(data);
259 
260 	return_VALUE(result);
261 }
262 
263 
264 int
acpi_pci_bind_root(struct acpi_device * device,struct acpi_pci_id * id,struct pci_bus * bus)265 acpi_pci_bind_root (
266 	struct acpi_device	*device,
267 	struct acpi_pci_id	*id,
268 	struct pci_bus		*bus)
269 {
270 	int			result = 0;
271 	acpi_status		status = AE_OK;
272 	struct acpi_pci_data	*data = NULL;
273 	char			pathname[ACPI_PATHNAME_MAX] = {0};
274 	struct acpi_buffer	buffer = {ACPI_PATHNAME_MAX, pathname};
275 
276 	ACPI_FUNCTION_TRACE("acpi_pci_bind_root");
277 
278 	if (!device || !id || !bus)
279 		return_VALUE(-EINVAL);
280 
281 	data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
282 	if (!data)
283 		return_VALUE(-ENOMEM);
284 	memset(data, 0, sizeof(struct acpi_pci_data));
285 
286 	data->id = *id;
287 	data->bus = bus;
288 	device->ops.bind = acpi_pci_bind;
289 
290 	acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
291 
292 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
293 		"%02x:%02x\n", pathname, id->segment, id->bus));
294 
295 	status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
296 	if (ACPI_FAILURE(status)) {
297 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
298 			"Unable to attach ACPI-PCI context to device %s\n",
299 			pathname));
300 		result = -ENODEV;
301 		goto end;
302 	}
303 
304 end:
305 	if (result != 0)
306 		kfree(data);
307 
308 	return_VALUE(result);
309 }
310