]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/pci_root.c
ACPI: use PNPID:instance_no as bus_id of ACPI device
[mirror_ubuntu-zesty-kernel.git] / drivers / acpi / pci_root.c
CommitLineData
1da177e4
LT
1/*
2 * pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
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
1da177e4 38#define _COMPONENT ACPI_PCI_COMPONENT
4be44fcd 39ACPI_MODULE_NAME("pci_root")
1da177e4
LT
40#define ACPI_PCI_ROOT_CLASS "pci_bridge"
41#define ACPI_PCI_ROOT_HID "PNP0A03"
42#define ACPI_PCI_ROOT_DRIVER_NAME "ACPI PCI Root Bridge Driver"
43#define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
4be44fcd
LB
44static int acpi_pci_root_add(struct acpi_device *device);
45static int acpi_pci_root_remove(struct acpi_device *device, int type);
46static int acpi_pci_root_start(struct acpi_device *device);
1da177e4
LT
47
48static struct acpi_driver acpi_pci_root_driver = {
4be44fcd
LB
49 .name = ACPI_PCI_ROOT_DRIVER_NAME,
50 .class = ACPI_PCI_ROOT_CLASS,
51 .ids = ACPI_PCI_ROOT_HID,
52 .ops = {
53 .add = acpi_pci_root_add,
54 .remove = acpi_pci_root_remove,
55 .start = acpi_pci_root_start,
56 },
1da177e4
LT
57};
58
59struct acpi_pci_root {
4be44fcd 60 struct list_head node;
32917e5b 61 struct acpi_device * device;
4be44fcd
LB
62 struct acpi_pci_id id;
63 struct pci_bus *bus;
1da177e4
LT
64};
65
66static LIST_HEAD(acpi_pci_roots);
67
68static struct acpi_pci_driver *sub_driver;
69
70int acpi_pci_register_driver(struct acpi_pci_driver *driver)
71{
72 int n = 0;
73 struct list_head *entry;
74
75 struct acpi_pci_driver **pptr = &sub_driver;
76 while (*pptr)
77 pptr = &(*pptr)->next;
78 *pptr = driver;
79
80 if (!driver->add)
81 return 0;
82
83 list_for_each(entry, &acpi_pci_roots) {
84 struct acpi_pci_root *root;
85 root = list_entry(entry, struct acpi_pci_root, node);
2d1e0a02 86 driver->add(root->device->handle);
1da177e4
LT
87 n++;
88 }
89
90 return n;
91}
4be44fcd 92
1da177e4
LT
93EXPORT_SYMBOL(acpi_pci_register_driver);
94
95void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
96{
97 struct list_head *entry;
98
99 struct acpi_pci_driver **pptr = &sub_driver;
100 while (*pptr) {
101 if (*pptr != driver)
102 continue;
103 *pptr = (*pptr)->next;
104 break;
105 }
106
107 if (!driver->remove)
108 return;
109
110 list_for_each(entry, &acpi_pci_roots) {
111 struct acpi_pci_root *root;
112 root = list_entry(entry, struct acpi_pci_root, node);
2d1e0a02 113 driver->remove(root->device->handle);
1da177e4
LT
114 }
115}
4be44fcd 116
1da177e4
LT
117EXPORT_SYMBOL(acpi_pci_unregister_driver);
118
119static acpi_status
4be44fcd 120get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
1da177e4
LT
121{
122 int *busnr = (int *)data;
123 struct acpi_resource_address64 address;
124
50eca3eb
BM
125 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
126 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
127 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
1da177e4
LT
128 return AE_OK;
129
130 acpi_resource_to_address64(resource, &address);
4be44fcd
LB
131 if ((address.address_length > 0) &&
132 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
50eca3eb 133 *busnr = address.minimum;
1da177e4
LT
134
135 return AE_OK;
136}
137
4be44fcd 138static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
1da177e4
LT
139{
140 acpi_status status;
141
142 *busnum = -1;
4be44fcd
LB
143 status =
144 acpi_walk_resources(handle, METHOD_NAME__CRS,
145 get_root_bridge_busnr_callback, busnum);
1da177e4
LT
146 if (ACPI_FAILURE(status))
147 return status;
148 /* Check if we really get a bus number from _CRS */
149 if (*busnum == -1)
150 return AE_ERROR;
151 return AE_OK;
152}
153
4be44fcd 154static int acpi_pci_root_add(struct acpi_device *device)
1da177e4 155{
4be44fcd
LB
156 int result = 0;
157 struct acpi_pci_root *root = NULL;
158 struct acpi_pci_root *tmp;
159 acpi_status status = AE_OK;
160 unsigned long value = 0;
161 acpi_handle handle = NULL;
1da177e4 162
1da177e4
LT
163
164 if (!device)
d550d98d 165 return -EINVAL;
1da177e4
LT
166
167 root = kmalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
168 if (!root)
d550d98d 169 return -ENOMEM;
1da177e4 170 memset(root, 0, sizeof(struct acpi_pci_root));
c431ada4 171 INIT_LIST_HEAD(&root->node);
1da177e4 172
32917e5b 173 root->device = device;
1da177e4
LT
174 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
175 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
176 acpi_driver_data(device) = root;
177
1da177e4
LT
178 /*
179 * Segment
180 * -------
181 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
182 */
2d1e0a02 183 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
4be44fcd 184 &value);
1da177e4
LT
185 switch (status) {
186 case AE_OK:
187 root->id.segment = (u16) value;
188 break;
189 case AE_NOT_FOUND:
4be44fcd
LB
190 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
191 "Assuming segment 0 (no _SEG)\n"));
1da177e4
LT
192 root->id.segment = 0;
193 break;
194 default:
a6fc6720 195 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
1da177e4
LT
196 result = -ENODEV;
197 goto end;
198 }
199
200 /*
201 * Bus
202 * ---
203 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
204 */
2d1e0a02 205 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
4be44fcd 206 &value);
1da177e4
LT
207 switch (status) {
208 case AE_OK:
209 root->id.bus = (u16) value;
210 break;
211 case AE_NOT_FOUND:
212 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
213 root->id.bus = 0;
214 break;
215 default:
a6fc6720 216 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
1da177e4
LT
217 result = -ENODEV;
218 goto end;
219 }
220
221 /* Some systems have wrong _BBN */
222 list_for_each_entry(tmp, &acpi_pci_roots, node) {
223 if ((tmp->id.segment == root->id.segment)
4be44fcd 224 && (tmp->id.bus == root->id.bus)) {
1da177e4
LT
225 int bus = 0;
226 acpi_status status;
227
6468463a 228 printk(KERN_ERR PREFIX
a6fc6720 229 "Wrong _BBN value, reboot"
6468463a 230 " and use option 'pci=noacpi'\n");
1da177e4 231
2d1e0a02 232 status = try_get_root_bridge_busnr(device->handle, &bus);
1da177e4
LT
233 if (ACPI_FAILURE(status))
234 break;
235 if (bus != root->id.bus) {
4be44fcd
LB
236 printk(KERN_INFO PREFIX
237 "PCI _CRS %d overrides _BBN 0\n", bus);
1da177e4
LT
238 root->id.bus = bus;
239 }
240 break;
241 }
242 }
243 /*
244 * Device & Function
245 * -----------------
246 * Obtained from _ADR (which has already been evaluated for us).
247 */
248 root->id.device = device->pnp.bus_address >> 16;
249 root->id.function = device->pnp.bus_address & 0xFFFF;
250
251 /*
252 * TBD: Need PCI interface for enumeration/configuration of roots.
253 */
254
4be44fcd
LB
255 /* TBD: Locking */
256 list_add_tail(&root->node, &acpi_pci_roots);
1da177e4 257
4be44fcd
LB
258 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
259 acpi_device_name(device), acpi_device_bid(device),
260 root->id.segment, root->id.bus);
1da177e4
LT
261
262 /*
263 * Scan the Root Bridge
264 * --------------------
265 * Must do this prior to any attempt to bind the root device, as the
266 * PCI namespace does not get created until this call is made (and
267 * thus the root bridge's pci_dev does not exist).
268 */
269 root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
270 if (!root->bus) {
6468463a
LB
271 printk(KERN_ERR PREFIX
272 "Bus %04x:%02x not present in PCI namespace\n",
273 root->id.segment, root->id.bus);
1da177e4
LT
274 result = -ENODEV;
275 goto end;
276 }
277
278 /*
279 * Attach ACPI-PCI Context
280 * -----------------------
281 * Thus binding the ACPI and PCI devices.
282 */
283 result = acpi_pci_bind_root(device, &root->id, root->bus);
284 if (result)
285 goto end;
286
287 /*
288 * PCI Routing Table
289 * -----------------
290 * Evaluate and parse _PRT, if exists.
291 */
2d1e0a02 292 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
1da177e4 293 if (ACPI_SUCCESS(status))
2d1e0a02 294 result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
4be44fcd 295 root->id.bus);
1da177e4 296
4be44fcd 297 end:
c431ada4
RS
298 if (result) {
299 if (!list_empty(&root->node))
300 list_del(&root->node);
1da177e4 301 kfree(root);
c431ada4 302 }
1da177e4 303
d550d98d 304 return result;
1da177e4
LT
305}
306
4be44fcd 307static int acpi_pci_root_start(struct acpi_device *device)
c431ada4 308{
4be44fcd 309 struct acpi_pci_root *root;
c431ada4 310
c431ada4
RS
311
312 list_for_each_entry(root, &acpi_pci_roots, node) {
432bfaba 313 if (root->device == device) {
c431ada4 314 pci_bus_add_devices(root->bus);
d550d98d 315 return 0;
c431ada4
RS
316 }
317 }
d550d98d 318 return -ENODEV;
c431ada4 319}
1da177e4 320
4be44fcd 321static int acpi_pci_root_remove(struct acpi_device *device, int type)
1da177e4 322{
4be44fcd 323 struct acpi_pci_root *root = NULL;
1da177e4 324
1da177e4
LT
325
326 if (!device || !acpi_driver_data(device))
d550d98d 327 return -EINVAL;
1da177e4 328
4be44fcd 329 root = (struct acpi_pci_root *)acpi_driver_data(device);
1da177e4
LT
330
331 kfree(root);
332
d550d98d 333 return 0;
1da177e4
LT
334}
335
4be44fcd 336static int __init acpi_pci_root_init(void)
1da177e4 337{
1da177e4
LT
338
339 if (acpi_pci_disabled)
d550d98d 340 return 0;
1da177e4
LT
341
342 /* DEBUG:
4be44fcd
LB
343 acpi_dbg_layer = ACPI_PCI_COMPONENT;
344 acpi_dbg_level = 0xFFFFFFFF;
1da177e4
LT
345 */
346
347 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
d550d98d 348 return -ENODEV;
1da177e4 349
d550d98d 350 return 0;
1da177e4
LT
351}
352
353subsys_initcall(acpi_pci_root_init);