ACPI/PCI: Negotiate _OSC control bits before requesting them
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / pci_root.c
blobc34713112520a06fc878243cde5aabb43a1861ce
1 /*
2 * pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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/pm_runtime.h>
34 #include <linux/pci.h>
35 #include <linux/pci-acpi.h>
36 #include <linux/pci-aspm.h>
37 #include <linux/acpi.h>
38 #include <linux/slab.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
42 #define PREFIX "ACPI: "
44 #define _COMPONENT ACPI_PCI_COMPONENT
45 ACPI_MODULE_NAME("pci_root");
46 #define ACPI_PCI_ROOT_CLASS "pci_bridge"
47 #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
48 static int acpi_pci_root_add(struct acpi_device *device);
49 static int acpi_pci_root_remove(struct acpi_device *device, int type);
50 static int acpi_pci_root_start(struct acpi_device *device);
52 static const struct acpi_device_id root_device_ids[] = {
53 {"PNP0A03", 0},
54 {"", 0},
56 MODULE_DEVICE_TABLE(acpi, root_device_ids);
58 static struct acpi_driver acpi_pci_root_driver = {
59 .name = "pci_root",
60 .class = ACPI_PCI_ROOT_CLASS,
61 .ids = root_device_ids,
62 .ops = {
63 .add = acpi_pci_root_add,
64 .remove = acpi_pci_root_remove,
65 .start = acpi_pci_root_start,
69 static LIST_HEAD(acpi_pci_roots);
71 static struct acpi_pci_driver *sub_driver;
72 static DEFINE_MUTEX(osc_lock);
74 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
76 int n = 0;
77 struct acpi_pci_root *root;
79 struct acpi_pci_driver **pptr = &sub_driver;
80 while (*pptr)
81 pptr = &(*pptr)->next;
82 *pptr = driver;
84 if (!driver->add)
85 return 0;
87 list_for_each_entry(root, &acpi_pci_roots, node) {
88 driver->add(root->device->handle);
89 n++;
92 return n;
95 EXPORT_SYMBOL(acpi_pci_register_driver);
97 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
99 struct acpi_pci_root *root;
101 struct acpi_pci_driver **pptr = &sub_driver;
102 while (*pptr) {
103 if (*pptr == driver)
104 break;
105 pptr = &(*pptr)->next;
107 BUG_ON(!*pptr);
108 *pptr = (*pptr)->next;
110 if (!driver->remove)
111 return;
113 list_for_each_entry(root, &acpi_pci_roots, node)
114 driver->remove(root->device->handle);
117 EXPORT_SYMBOL(acpi_pci_unregister_driver);
119 acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
121 struct acpi_pci_root *root;
123 list_for_each_entry(root, &acpi_pci_roots, node)
124 if ((root->segment == (u16) seg) &&
125 (root->secondary.start == (u16) bus))
126 return root->device->handle;
127 return NULL;
130 EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
133 * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
134 * @handle - the ACPI CA node in question.
136 * Note: we could make this API take a struct acpi_device * instead, but
137 * for now, it's more convenient to operate on an acpi_handle.
139 int acpi_is_root_bridge(acpi_handle handle)
141 int ret;
142 struct acpi_device *device;
144 ret = acpi_bus_get_device(handle, &device);
145 if (ret)
146 return 0;
148 ret = acpi_match_device_ids(device, root_device_ids);
149 if (ret)
150 return 0;
151 else
152 return 1;
154 EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
156 static acpi_status
157 get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
159 struct resource *res = data;
160 struct acpi_resource_address64 address;
162 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
163 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
164 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
165 return AE_OK;
167 acpi_resource_to_address64(resource, &address);
168 if ((address.address_length > 0) &&
169 (address.resource_type == ACPI_BUS_NUMBER_RANGE)) {
170 res->start = address.minimum;
171 res->end = address.minimum + address.address_length - 1;
174 return AE_OK;
177 static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
178 struct resource *res)
180 acpi_status status;
182 res->start = -1;
183 status =
184 acpi_walk_resources(handle, METHOD_NAME__CRS,
185 get_root_bridge_busnr_callback, res);
186 if (ACPI_FAILURE(status))
187 return status;
188 if (res->start == -1)
189 return AE_ERROR;
190 return AE_OK;
193 static void acpi_pci_bridge_scan(struct acpi_device *device)
195 int status;
196 struct acpi_device *child = NULL;
198 if (device->flags.bus_address)
199 if (device->parent && device->parent->ops.bind) {
200 status = device->parent->ops.bind(device);
201 if (!status) {
202 list_for_each_entry(child, &device->children, node)
203 acpi_pci_bridge_scan(child);
208 static u8 pci_osc_uuid_str[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
210 static acpi_status acpi_pci_run_osc(acpi_handle handle,
211 const u32 *capbuf, u32 *retval)
213 struct acpi_osc_context context = {
214 .uuid_str = pci_osc_uuid_str,
215 .rev = 1,
216 .cap.length = 12,
217 .cap.pointer = (void *)capbuf,
219 acpi_status status;
221 status = acpi_run_osc(handle, &context);
222 if (ACPI_SUCCESS(status)) {
223 *retval = *((u32 *)(context.ret.pointer + 8));
224 kfree(context.ret.pointer);
226 return status;
229 static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root,
230 u32 support,
231 u32 *control)
233 acpi_status status;
234 u32 result, capbuf[3];
236 support &= OSC_PCI_SUPPORT_MASKS;
237 support |= root->osc_support_set;
239 capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
240 capbuf[OSC_SUPPORT_TYPE] = support;
241 if (control) {
242 *control &= OSC_PCI_CONTROL_MASKS;
243 capbuf[OSC_CONTROL_TYPE] = *control | root->osc_control_set;
244 } else {
245 /* Run _OSC query for all possible controls. */
246 capbuf[OSC_CONTROL_TYPE] = OSC_PCI_CONTROL_MASKS;
249 status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
250 if (ACPI_SUCCESS(status)) {
251 root->osc_support_set = support;
252 if (control)
253 *control = result;
255 return status;
258 static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
260 acpi_status status;
261 acpi_handle tmp;
263 status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
264 if (ACPI_FAILURE(status))
265 return status;
266 mutex_lock(&osc_lock);
267 status = acpi_pci_query_osc(root, flags, NULL);
268 mutex_unlock(&osc_lock);
269 return status;
272 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
274 struct acpi_pci_root *root;
276 list_for_each_entry(root, &acpi_pci_roots, node) {
277 if (root->device->handle == handle)
278 return root;
280 return NULL;
282 EXPORT_SYMBOL_GPL(acpi_pci_find_root);
284 struct acpi_handle_node {
285 struct list_head node;
286 acpi_handle handle;
290 * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
291 * @handle: the handle in question
293 * Given an ACPI CA handle, the desired PCI device is located in the
294 * list of PCI devices.
296 * If the device is found, its reference count is increased and this
297 * function returns a pointer to its data structure. The caller must
298 * decrement the reference count by calling pci_dev_put().
299 * If no device is found, %NULL is returned.
301 struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
303 int dev, fn;
304 unsigned long long adr;
305 acpi_status status;
306 acpi_handle phandle;
307 struct pci_bus *pbus;
308 struct pci_dev *pdev = NULL;
309 struct acpi_handle_node *node, *tmp;
310 struct acpi_pci_root *root;
311 LIST_HEAD(device_list);
314 * Walk up the ACPI CA namespace until we reach a PCI root bridge.
316 phandle = handle;
317 while (!acpi_is_root_bridge(phandle)) {
318 node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
319 if (!node)
320 goto out;
322 INIT_LIST_HEAD(&node->node);
323 node->handle = phandle;
324 list_add(&node->node, &device_list);
326 status = acpi_get_parent(phandle, &phandle);
327 if (ACPI_FAILURE(status))
328 goto out;
331 root = acpi_pci_find_root(phandle);
332 if (!root)
333 goto out;
335 pbus = root->bus;
338 * Now, walk back down the PCI device tree until we return to our
339 * original handle. Assumes that everything between the PCI root
340 * bridge and the device we're looking for must be a P2P bridge.
342 list_for_each_entry(node, &device_list, node) {
343 acpi_handle hnd = node->handle;
344 status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
345 if (ACPI_FAILURE(status))
346 goto out;
347 dev = (adr >> 16) & 0xffff;
348 fn = adr & 0xffff;
350 pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
351 if (!pdev || hnd == handle)
352 break;
354 pbus = pdev->subordinate;
355 pci_dev_put(pdev);
358 * This function may be called for a non-PCI device that has a
359 * PCI parent (eg. a disk under a PCI SATA controller). In that
360 * case pdev->subordinate will be NULL for the parent.
362 if (!pbus) {
363 dev_dbg(&pdev->dev, "Not a PCI-to-PCI bridge\n");
364 pdev = NULL;
365 break;
368 out:
369 list_for_each_entry_safe(node, tmp, &device_list, node)
370 kfree(node);
372 return pdev;
374 EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
377 * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
378 * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
379 * @mask: Mask of _OSC bits to request control of, place to store control mask.
380 * @req: Mask of _OSC bits the control of is essential to the caller.
382 * Run _OSC query for @mask and if that is successful, compare the returned
383 * mask of control bits with @req. If all of the @req bits are set in the
384 * returned mask, run _OSC request for it.
386 * The variable at the @mask address may be modified regardless of whether or
387 * not the function returns success. On success it will contain the mask of
388 * _OSC bits the BIOS has granted control of, but its contents are meaningless
389 * on failure.
391 acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 *mask, u32 req)
393 struct acpi_pci_root *root;
394 acpi_status status;
395 u32 ctrl, capbuf[3];
396 acpi_handle tmp;
398 if (!mask)
399 return AE_BAD_PARAMETER;
401 ctrl = *mask & OSC_PCI_CONTROL_MASKS;
402 if ((ctrl & req) != req)
403 return AE_TYPE;
405 root = acpi_pci_find_root(handle);
406 if (!root)
407 return AE_NOT_EXIST;
409 status = acpi_get_handle(handle, "_OSC", &tmp);
410 if (ACPI_FAILURE(status))
411 return status;
413 mutex_lock(&osc_lock);
415 *mask = ctrl | root->osc_control_set;
416 /* No need to evaluate _OSC if the control was already granted. */
417 if ((root->osc_control_set & ctrl) == ctrl)
418 goto out;
420 /* Need to check the available controls bits before requesting them. */
421 while (*mask) {
422 status = acpi_pci_query_osc(root, root->osc_support_set, mask);
423 if (ACPI_FAILURE(status))
424 goto out;
425 if (ctrl == *mask)
426 break;
427 ctrl = *mask;
430 if ((ctrl & req) != req) {
431 status = AE_SUPPORT;
432 goto out;
435 capbuf[OSC_QUERY_TYPE] = 0;
436 capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
437 capbuf[OSC_CONTROL_TYPE] = ctrl;
438 status = acpi_pci_run_osc(handle, capbuf, mask);
439 if (ACPI_SUCCESS(status))
440 root->osc_control_set = *mask;
441 out:
442 mutex_unlock(&osc_lock);
443 return status;
445 EXPORT_SYMBOL(acpi_pci_osc_control_set);
447 static int __devinit acpi_pci_root_add(struct acpi_device *device)
449 unsigned long long segment, bus;
450 acpi_status status;
451 int result;
452 struct acpi_pci_root *root;
453 acpi_handle handle;
454 struct acpi_device *child;
455 u32 flags, base_flags;
457 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
458 if (!root)
459 return -ENOMEM;
461 segment = 0;
462 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
463 &segment);
464 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
465 printk(KERN_ERR PREFIX "can't evaluate _SEG\n");
466 result = -ENODEV;
467 goto end;
470 /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
471 root->secondary.flags = IORESOURCE_BUS;
472 status = try_get_root_bridge_busnr(device->handle, &root->secondary);
473 if (ACPI_FAILURE(status)) {
475 * We need both the start and end of the downstream bus range
476 * to interpret _CBA (MMCONFIG base address), so it really is
477 * supposed to be in _CRS. If we don't find it there, all we
478 * can do is assume [_BBN-0xFF] or [0-0xFF].
480 root->secondary.end = 0xFF;
481 printk(KERN_WARNING FW_BUG PREFIX
482 "no secondary bus range in _CRS\n");
483 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL, &bus);
484 if (ACPI_SUCCESS(status))
485 root->secondary.start = bus;
486 else if (status == AE_NOT_FOUND)
487 root->secondary.start = 0;
488 else {
489 printk(KERN_ERR PREFIX "can't evaluate _BBN\n");
490 result = -ENODEV;
491 goto end;
495 INIT_LIST_HEAD(&root->node);
496 root->device = device;
497 root->segment = segment & 0xFFFF;
498 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
499 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
500 device->driver_data = root;
503 * All supported architectures that use ACPI have support for
504 * PCI domains, so we indicate this in _OSC support capabilities.
506 flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
507 acpi_pci_osc_support(root, flags);
510 * TBD: Need PCI interface for enumeration/configuration of roots.
513 /* TBD: Locking */
514 list_add_tail(&root->node, &acpi_pci_roots);
516 printk(KERN_INFO PREFIX "%s [%s] (domain %04x %pR)\n",
517 acpi_device_name(device), acpi_device_bid(device),
518 root->segment, &root->secondary);
521 * Scan the Root Bridge
522 * --------------------
523 * Must do this prior to any attempt to bind the root device, as the
524 * PCI namespace does not get created until this call is made (and
525 * thus the root bridge's pci_dev does not exist).
527 root->bus = pci_acpi_scan_root(root);
528 if (!root->bus) {
529 printk(KERN_ERR PREFIX
530 "Bus %04x:%02x not present in PCI namespace\n",
531 root->segment, (unsigned int)root->secondary.start);
532 result = -ENODEV;
533 goto end;
537 * Attach ACPI-PCI Context
538 * -----------------------
539 * Thus binding the ACPI and PCI devices.
541 result = acpi_pci_bind_root(device);
542 if (result)
543 goto end;
546 * PCI Routing Table
547 * -----------------
548 * Evaluate and parse _PRT, if exists.
550 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
551 if (ACPI_SUCCESS(status))
552 result = acpi_pci_irq_add_prt(device->handle, root->bus);
555 * Scan and bind all _ADR-Based Devices
557 list_for_each_entry(child, &device->children, node)
558 acpi_pci_bridge_scan(child);
560 /* Indicate support for various _OSC capabilities. */
561 if (pci_ext_cfg_avail(root->bus->self))
562 flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
563 if (pcie_aspm_enabled())
564 flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
565 OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
566 if (pci_msi_enabled())
567 flags |= OSC_MSI_SUPPORT;
568 if (flags != base_flags)
569 acpi_pci_osc_support(root, flags);
571 flags = OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL;
572 status = acpi_pci_osc_control_set(root->device->handle, &flags, flags);
574 if (ACPI_FAILURE(status)) {
575 printk(KERN_INFO "Unable to assume PCIe control: Disabling ASPM\n");
576 pcie_no_aspm();
579 pci_acpi_add_bus_pm_notifier(device, root->bus);
580 if (device->wakeup.flags.run_wake)
581 device_set_run_wake(root->bus->bridge, true);
583 return 0;
585 end:
586 if (!list_empty(&root->node))
587 list_del(&root->node);
588 kfree(root);
589 return result;
592 static int acpi_pci_root_start(struct acpi_device *device)
594 struct acpi_pci_root *root = acpi_driver_data(device);
596 pci_bus_add_devices(root->bus);
597 return 0;
600 static int acpi_pci_root_remove(struct acpi_device *device, int type)
602 struct acpi_pci_root *root = acpi_driver_data(device);
604 device_set_run_wake(root->bus->bridge, false);
605 pci_acpi_remove_bus_pm_notifier(device);
607 kfree(root);
608 return 0;
611 static int __init acpi_pci_root_init(void)
613 if (acpi_pci_disabled)
614 return 0;
616 pci_acpi_crs_quirks();
617 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
618 return -ENODEV;
620 return 0;
623 subsys_initcall(acpi_pci_root_init);