blob: dee916707a7d1b550d88636ebe60528faa36a035 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Andrew Patterson990a7ac2008-11-10 15:30:45 -070034#include <linux/pci-acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/acpi.h>
36#include <acpi/acpi_bus.h>
37#include <acpi/acpi_drivers.h>
38
Len Browna192a952009-07-28 16:45:54 -040039#define PREFIX "ACPI: "
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define _COMPONENT ACPI_PCI_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050042ACPI_MODULE_NAME("pci_root");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define ACPI_PCI_ROOT_CLASS "pci_bridge"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
Len Brown4be44fc2005-08-05 00:44:28 -040045static int acpi_pci_root_add(struct acpi_device *device);
46static int acpi_pci_root_remove(struct acpi_device *device, int type);
47static int acpi_pci_root_start(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Thomas Renninger1ba90e32007-07-23 14:44:41 +020049static struct acpi_device_id root_device_ids[] = {
50 {"PNP0A03", 0},
51 {"", 0},
52};
53MODULE_DEVICE_TABLE(acpi, root_device_ids);
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static struct acpi_driver acpi_pci_root_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050056 .name = "pci_root",
Len Brown4be44fc2005-08-05 00:44:28 -040057 .class = ACPI_PCI_ROOT_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020058 .ids = root_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040059 .ops = {
60 .add = acpi_pci_root_add,
61 .remove = acpi_pci_root_remove,
62 .start = acpi_pci_root_start,
63 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66struct acpi_pci_root {
Len Brown4be44fc2005-08-05 00:44:28 -040067 struct list_head node;
Bjorn Helgaas07054952009-06-18 14:47:07 -060068 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -040069 struct pci_bus *bus;
Bjorn Helgaas07054952009-06-18 14:47:07 -060070 u16 segment;
71 u8 bus_nr;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +090072
73 u32 osc_support_set; /* _OSC state of support bits */
74 u32 osc_control_set; /* _OSC state of control bits */
75 u32 osc_control_qry; /* the latest _OSC query result */
76
77 u32 osc_queried:1; /* has _OSC control been queried? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
80static LIST_HEAD(acpi_pci_roots);
81
82static struct acpi_pci_driver *sub_driver;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +090083static DEFINE_MUTEX(osc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85int acpi_pci_register_driver(struct acpi_pci_driver *driver)
86{
87 int n = 0;
Bjorn Helgaasc1aec832009-06-18 14:47:02 -060088 struct acpi_pci_root *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 struct acpi_pci_driver **pptr = &sub_driver;
91 while (*pptr)
92 pptr = &(*pptr)->next;
93 *pptr = driver;
94
95 if (!driver->add)
96 return 0;
97
Bjorn Helgaasc1aec832009-06-18 14:47:02 -060098 list_for_each_entry(root, &acpi_pci_roots, node) {
Patrick Mochel2d1e0a02006-05-19 16:54:43 -040099 driver->add(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 n++;
101 }
102
103 return n;
104}
Len Brown4be44fc2005-08-05 00:44:28 -0400105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106EXPORT_SYMBOL(acpi_pci_register_driver);
107
108void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
109{
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600110 struct acpi_pci_root *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 struct acpi_pci_driver **pptr = &sub_driver;
113 while (*pptr) {
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800114 if (*pptr == driver)
115 break;
116 pptr = &(*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Akinobu Mitaf10bb252006-12-19 12:56:09 -0800118 BUG_ON(!*pptr);
119 *pptr = (*pptr)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (!driver->remove)
122 return;
123
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600124 list_for_each_entry(root, &acpi_pci_roots, node)
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400125 driver->remove(root->device->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
Len Brown4be44fc2005-08-05 00:44:28 -0400127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128EXPORT_SYMBOL(acpi_pci_unregister_driver);
129
Justin Chend91a0072006-12-06 10:17:10 -0700130acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
131{
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600132 struct acpi_pci_root *root;
Justin Chend91a0072006-12-06 10:17:10 -0700133
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600134 list_for_each_entry(root, &acpi_pci_roots, node)
Bjorn Helgaas07054952009-06-18 14:47:07 -0600135 if ((root->segment == (u16) seg) && (root->bus_nr == (u16) bus))
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600136 return root->device->handle;
Justin Chend91a0072006-12-06 10:17:10 -0700137 return NULL;
138}
139
140EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
141
Alexander Chiang27558202009-06-10 19:55:14 +0000142/**
143 * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
144 * @handle - the ACPI CA node in question.
145 *
146 * Note: we could make this API take a struct acpi_device * instead, but
147 * for now, it's more convenient to operate on an acpi_handle.
148 */
149int acpi_is_root_bridge(acpi_handle handle)
150{
151 int ret;
152 struct acpi_device *device;
153
154 ret = acpi_bus_get_device(handle, &device);
155 if (ret)
156 return 0;
157
158 ret = acpi_match_device_ids(device, root_device_ids);
159 if (ret)
160 return 0;
161 else
162 return 1;
163}
164EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400167get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200169 int *busnr = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct acpi_resource_address64 address;
171
Bob Moore50eca3e2005-09-30 19:03:00 -0400172 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
173 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
174 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return AE_OK;
176
177 acpi_resource_to_address64(resource, &address);
Len Brown4be44fc2005-08-05 00:44:28 -0400178 if ((address.address_length > 0) &&
179 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
Bob Moore50eca3e2005-09-30 19:03:00 -0400180 *busnr = address.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 return AE_OK;
183}
184
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600185static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
186 unsigned long long *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 acpi_status status;
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600189 int busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600191 busnum = -1;
Len Brown4be44fc2005-08-05 00:44:28 -0400192 status =
193 acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600194 get_root_bridge_busnr_callback, &busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (ACPI_FAILURE(status))
196 return status;
197 /* Check if we really get a bus number from _CRS */
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600198 if (busnum == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return AE_ERROR;
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600200 *bus = busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return AE_OK;
202}
203
Rui Zhang2786f6e2006-12-21 02:21:13 -0500204static void acpi_pci_bridge_scan(struct acpi_device *device)
205{
206 int status;
207 struct acpi_device *child = NULL;
208
209 if (device->flags.bus_address)
210 if (device->parent && device->parent->ops.bind) {
211 status = device->parent->ops.bind(device);
212 if (!status) {
213 list_for_each_entry(child, &device->children, node)
214 acpi_pci_bridge_scan(child);
215 }
216 }
217}
218
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900219static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40,
220 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
221
222static acpi_status acpi_pci_run_osc(acpi_handle handle,
223 const u32 *capbuf, u32 *retval)
224{
225 acpi_status status;
226 struct acpi_object_list input;
227 union acpi_object in_params[4];
228 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
229 union acpi_object *out_obj;
230 u32 errors;
231
232 /* Setting up input parameters */
233 input.count = 4;
234 input.pointer = in_params;
235 in_params[0].type = ACPI_TYPE_BUFFER;
236 in_params[0].buffer.length = 16;
237 in_params[0].buffer.pointer = OSC_UUID;
238 in_params[1].type = ACPI_TYPE_INTEGER;
239 in_params[1].integer.value = 1;
240 in_params[2].type = ACPI_TYPE_INTEGER;
241 in_params[2].integer.value = 3;
242 in_params[3].type = ACPI_TYPE_BUFFER;
243 in_params[3].buffer.length = 12;
244 in_params[3].buffer.pointer = (u8 *)capbuf;
245
246 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
247 if (ACPI_FAILURE(status))
248 return status;
249
250 if (!output.length)
251 return AE_NULL_OBJECT;
252
253 out_obj = output.pointer;
254 if (out_obj->type != ACPI_TYPE_BUFFER) {
255 printk(KERN_DEBUG "_OSC evaluation returned wrong type\n");
256 status = AE_TYPE;
257 goto out_kfree;
258 }
259 /* Need to ignore the bit0 in result code */
260 errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
261 if (errors) {
262 if (errors & OSC_REQUEST_ERROR)
263 printk(KERN_DEBUG "_OSC request failed\n");
264 if (errors & OSC_INVALID_UUID_ERROR)
265 printk(KERN_DEBUG "_OSC invalid UUID\n");
266 if (errors & OSC_INVALID_REVISION_ERROR)
267 printk(KERN_DEBUG "_OSC invalid revision\n");
268 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
269 if (capbuf[OSC_QUERY_TYPE] & OSC_QUERY_ENABLE)
270 goto out_success;
271 printk(KERN_DEBUG
272 "Firmware did not grant requested _OSC control\n");
273 status = AE_SUPPORT;
274 goto out_kfree;
275 }
276 status = AE_ERROR;
277 goto out_kfree;
278 }
279out_success:
280 *retval = *((u32 *)(out_obj->buffer.pointer + 8));
281 status = AE_OK;
282
283out_kfree:
284 kfree(output.pointer);
285 return status;
286}
287
288static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root, u32 flags)
289{
290 acpi_status status;
291 u32 support_set, result, capbuf[3];
292
293 /* do _OSC query for all possible controls */
294 support_set = root->osc_support_set | (flags & OSC_SUPPORT_MASKS);
295 capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
296 capbuf[OSC_SUPPORT_TYPE] = support_set;
297 capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
298
299 status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
300 if (ACPI_SUCCESS(status)) {
301 root->osc_support_set = support_set;
302 root->osc_control_qry = result;
303 root->osc_queried = 1;
304 }
305 return status;
306}
307
308static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
309{
310 acpi_status status;
311 acpi_handle tmp;
312
313 status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
314 if (ACPI_FAILURE(status))
315 return status;
316 mutex_lock(&osc_lock);
317 status = acpi_pci_query_osc(root, flags);
318 mutex_unlock(&osc_lock);
319 return status;
320}
321
322static struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
323{
324 struct acpi_pci_root *root;
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600325
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900326 list_for_each_entry(root, &acpi_pci_roots, node) {
327 if (root->device->handle == handle)
328 return root;
329 }
330 return NULL;
331}
332
Alexander Chiang2f7bbce2009-06-10 19:55:20 +0000333struct acpi_handle_node {
334 struct list_head node;
335 acpi_handle handle;
336};
337
338/**
339 * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
340 * @handle: the handle in question
341 *
342 * Given an ACPI CA handle, the desired PCI device is located in the
343 * list of PCI devices.
344 *
345 * If the device is found, its reference count is increased and this
346 * function returns a pointer to its data structure. The caller must
347 * decrement the reference count by calling pci_dev_put().
348 * If no device is found, %NULL is returned.
349 */
350struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
351{
352 int dev, fn;
353 unsigned long long adr;
354 acpi_status status;
355 acpi_handle phandle;
356 struct pci_bus *pbus;
357 struct pci_dev *pdev = NULL;
358 struct acpi_handle_node *node, *tmp;
359 struct acpi_pci_root *root;
360 LIST_HEAD(device_list);
361
362 /*
363 * Walk up the ACPI CA namespace until we reach a PCI root bridge.
364 */
365 phandle = handle;
366 while (!acpi_is_root_bridge(phandle)) {
367 node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
368 if (!node)
369 goto out;
370
371 INIT_LIST_HEAD(&node->node);
372 node->handle = phandle;
373 list_add(&node->node, &device_list);
374
375 status = acpi_get_parent(phandle, &phandle);
376 if (ACPI_FAILURE(status))
377 goto out;
378 }
379
380 root = acpi_pci_find_root(phandle);
381 if (!root)
382 goto out;
383
384 pbus = root->bus;
385
386 /*
387 * Now, walk back down the PCI device tree until we return to our
388 * original handle. Assumes that everything between the PCI root
389 * bridge and the device we're looking for must be a P2P bridge.
390 */
391 list_for_each_entry(node, &device_list, node) {
392 acpi_handle hnd = node->handle;
393 status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
394 if (ACPI_FAILURE(status))
395 goto out;
396 dev = (adr >> 16) & 0xffff;
397 fn = adr & 0xffff;
398
399 pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
Troy Moure412af972009-06-25 17:05:35 -0600400 if (!pdev || hnd == handle)
Alexander Chiang2f7bbce2009-06-10 19:55:20 +0000401 break;
402
403 pbus = pdev->subordinate;
404 pci_dev_put(pdev);
405 }
406out:
407 list_for_each_entry_safe(node, tmp, &device_list, node)
408 kfree(node);
409
410 return pdev;
411}
412EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
413
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900414/**
Kenji Kaneshige9f5404d2009-02-09 16:00:04 +0900415 * acpi_pci_osc_control_set - commit requested control to Firmware
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900416 * @handle: acpi_handle for the target ACPI object
417 * @flags: driver's requested control bits
418 *
419 * Attempt to take control from Firmware on requested control bits.
420 **/
Kenji Kaneshige9f5404d2009-02-09 16:00:04 +0900421acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 flags)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900422{
423 acpi_status status;
424 u32 control_req, result, capbuf[3];
425 acpi_handle tmp;
426 struct acpi_pci_root *root;
427
428 status = acpi_get_handle(handle, "_OSC", &tmp);
429 if (ACPI_FAILURE(status))
430 return status;
431
432 control_req = (flags & OSC_CONTROL_MASKS);
433 if (!control_req)
434 return AE_TYPE;
435
436 root = acpi_pci_find_root(handle);
437 if (!root)
438 return AE_NOT_EXIST;
439
440 mutex_lock(&osc_lock);
441 /* No need to evaluate _OSC if the control was already granted. */
442 if ((root->osc_control_set & control_req) == control_req)
443 goto out;
444
445 /* Need to query controls first before requesting them */
446 if (!root->osc_queried) {
447 status = acpi_pci_query_osc(root, root->osc_support_set);
448 if (ACPI_FAILURE(status))
449 goto out;
450 }
451 if ((root->osc_control_qry & control_req) != control_req) {
452 printk(KERN_DEBUG
453 "Firmware did not grant requested _OSC control\n");
454 status = AE_SUPPORT;
455 goto out;
456 }
457
458 capbuf[OSC_QUERY_TYPE] = 0;
459 capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
460 capbuf[OSC_CONTROL_TYPE] = root->osc_control_set | control_req;
461 status = acpi_pci_run_osc(handle, capbuf, &result);
462 if (ACPI_SUCCESS(status))
463 root->osc_control_set = result;
464out:
465 mutex_unlock(&osc_lock);
466 return status;
467}
Kenji Kaneshige9f5404d2009-02-09 16:00:04 +0900468EXPORT_SYMBOL(acpi_pci_osc_control_set);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900469
Sam Ravnborgb5678a32008-02-17 13:23:03 +0100470static int __devinit acpi_pci_root_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600472 unsigned long long segment, bus;
473 acpi_status status;
474 int result;
475 struct acpi_pci_root *root;
476 acpi_handle handle;
Rui Zhang2786f6e2006-12-21 02:21:13 -0500477 struct acpi_device *child;
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700478 u32 flags, base_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600480 segment = 0;
481 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
482 &segment);
483 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
484 printk(KERN_ERR PREFIX "can't evaluate _SEG\n");
485 return -ENODEV;
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600488 /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
489 bus = 0;
490 status = try_get_root_bridge_busnr(device->handle, &bus);
491 if (ACPI_FAILURE(status)) {
492 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL, &bus);
493 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
494 printk(KERN_ERR PREFIX
495 "no bus number in _CRS and can't evaluate _BBN\n");
496 return -ENODEV;
497 }
498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Burman Yan36bcbec2006-12-19 12:56:11 -0800500 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -0400502 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600504 INIT_LIST_HEAD(&root->node);
Patrick Mochel32917e52006-05-19 16:54:39 -0400505 root->device = device;
Bjorn Helgaas07054952009-06-18 14:47:07 -0600506 root->segment = segment & 0xFFFF;
507 root->bus_nr = bus & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
509 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700510 device->driver_data = root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Andrew Patterson990a7ac2008-11-10 15:30:45 -0700512 /*
513 * All supported architectures that use ACPI have support for
514 * PCI domains, so we indicate this in _OSC support capabilities.
515 */
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700516 flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900517 acpi_pci_osc_support(root, flags);
Andrew Patterson990a7ac2008-11-10 15:30:45 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /*
520 * TBD: Need PCI interface for enumeration/configuration of roots.
521 */
522
Len Brown4be44fc2005-08-05 00:44:28 -0400523 /* TBD: Locking */
524 list_add_tail(&root->node, &acpi_pci_roots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Len Brown4be44fc2005-08-05 00:44:28 -0400526 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
527 acpi_device_name(device), acpi_device_bid(device),
Bjorn Helgaas07054952009-06-18 14:47:07 -0600528 root->segment, root->bus_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /*
531 * Scan the Root Bridge
532 * --------------------
533 * Must do this prior to any attempt to bind the root device, as the
534 * PCI namespace does not get created until this call is made (and
535 * thus the root bridge's pci_dev does not exist).
536 */
Bjorn Helgaas07054952009-06-18 14:47:07 -0600537 root->bus = pci_acpi_scan_root(device, segment, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (!root->bus) {
Len Brown64684632006-06-26 23:41:38 -0400539 printk(KERN_ERR PREFIX
540 "Bus %04x:%02x not present in PCI namespace\n",
Bjorn Helgaas07054952009-06-18 14:47:07 -0600541 root->segment, root->bus_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 result = -ENODEV;
543 goto end;
544 }
545
546 /*
547 * Attach ACPI-PCI Context
548 * -----------------------
549 * Thus binding the ACPI and PCI devices.
550 */
Alexander Chiang499650d2009-06-10 19:55:30 +0000551 result = acpi_pci_bind_root(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (result)
553 goto end;
554
555 /*
556 * PCI Routing Table
557 * -----------------
558 * Evaluate and parse _PRT, if exists.
559 */
Patrick Mochel2d1e0a02006-05-19 16:54:43 -0400560 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (ACPI_SUCCESS(status))
Alexander Chiang859a3f82009-06-10 19:55:35 +0000562 result = acpi_pci_irq_add_prt(device->handle, root->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Rui Zhang2786f6e2006-12-21 02:21:13 -0500564 /*
565 * Scan and bind all _ADR-Based Devices
566 */
567 list_for_each_entry(child, &device->children, node)
568 acpi_pci_bridge_scan(child);
569
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700570 /* Indicate support for various _OSC capabilities. */
571 if (pci_ext_cfg_avail(root->bus->self))
572 flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700573 if (pcie_aspm_enabled())
574 flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
575 OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
Andrew Patterson07ae95f2008-11-10 15:31:05 -0700576 if (pci_msi_enabled())
577 flags |= OSC_MSI_SUPPORT;
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700578 if (flags != base_flags)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900579 acpi_pci_osc_support(root, flags);
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700580
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600583end:
584 if (!list_empty(&root->node))
585 list_del(&root->node);
586 kfree(root);
Patrick Mocheld550d982006-06-27 00:41:40 -0400587 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Len Brown4be44fc2005-08-05 00:44:28 -0400590static int acpi_pci_root_start(struct acpi_device *device)
Rajesh Shahc431ada2005-04-28 00:25:45 -0700591{
Bjorn Helgaascaf420c2009-06-18 14:46:57 -0600592 struct acpi_pci_root *root = acpi_driver_data(device);
Rajesh Shahc431ada2005-04-28 00:25:45 -0700593
Bjorn Helgaascaf420c2009-06-18 14:46:57 -0600594 pci_bus_add_devices(root->bus);
595 return 0;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Len Brown4be44fc2005-08-05 00:44:28 -0400598static int acpi_pci_root_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Bjorn Helgaascaf420c2009-06-18 14:46:57 -0600600 struct acpi_pci_root *root = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 kfree(root);
Patrick Mocheld550d982006-06-27 00:41:40 -0400603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Len Brown4be44fc2005-08-05 00:44:28 -0400606static int __init acpi_pci_root_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (acpi_pci_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400612 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Patrick Mocheld550d982006-06-27 00:41:40 -0400614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617subsys_initcall(acpi_pci_root_init);