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>
33 #include <linux/pci.h>
34 #include <linux/pci-acpi.h>
35 #include <linux/acpi.h>
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
39 #define PREFIX "ACPI: "
41 #define _COMPONENT ACPI_PCI_COMPONENT
42 ACPI_MODULE_NAME("pci_root");
43 #define ACPI_PCI_ROOT_CLASS "pci_bridge"
44 #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
45 static int acpi_pci_root_add(struct acpi_device
*device
);
46 static int acpi_pci_root_remove(struct acpi_device
*device
, int type
);
47 static int acpi_pci_root_start(struct acpi_device
*device
);
49 static struct acpi_device_id root_device_ids
[] = {
53 MODULE_DEVICE_TABLE(acpi
, root_device_ids
);
55 static struct acpi_driver acpi_pci_root_driver
= {
57 .class = ACPI_PCI_ROOT_CLASS
,
58 .ids
= root_device_ids
,
60 .add
= acpi_pci_root_add
,
61 .remove
= acpi_pci_root_remove
,
62 .start
= acpi_pci_root_start
,
66 struct acpi_pci_root
{
67 struct list_head node
;
68 struct acpi_device
*device
;
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 */
77 u32 osc_queried
:1; /* has _OSC control been queried? */
80 static LIST_HEAD(acpi_pci_roots
);
82 static struct acpi_pci_driver
*sub_driver
;
83 static DEFINE_MUTEX(osc_lock
);
85 int acpi_pci_register_driver(struct acpi_pci_driver
*driver
)
88 struct acpi_pci_root
*root
;
90 struct acpi_pci_driver
**pptr
= &sub_driver
;
92 pptr
= &(*pptr
)->next
;
98 list_for_each_entry(root
, &acpi_pci_roots
, node
) {
99 driver
->add(root
->device
->handle
);
106 EXPORT_SYMBOL(acpi_pci_register_driver
);
108 void acpi_pci_unregister_driver(struct acpi_pci_driver
*driver
)
110 struct acpi_pci_root
*root
;
112 struct acpi_pci_driver
**pptr
= &sub_driver
;
116 pptr
= &(*pptr
)->next
;
119 *pptr
= (*pptr
)->next
;
124 list_for_each_entry(root
, &acpi_pci_roots
, node
)
125 driver
->remove(root
->device
->handle
);
128 EXPORT_SYMBOL(acpi_pci_unregister_driver
);
130 acpi_handle
acpi_get_pci_rootbridge_handle(unsigned int seg
, unsigned int bus
)
132 struct acpi_pci_root
*root
;
134 list_for_each_entry(root
, &acpi_pci_roots
, node
)
135 if ((root
->segment
== (u16
) seg
) && (root
->bus_nr
== (u16
) bus
))
136 return root
->device
->handle
;
140 EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle
);
143 * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
144 * @handle - the ACPI CA node in question.
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.
149 int acpi_is_root_bridge(acpi_handle handle
)
152 struct acpi_device
*device
;
154 ret
= acpi_bus_get_device(handle
, &device
);
158 ret
= acpi_match_device_ids(device
, root_device_ids
);
164 EXPORT_SYMBOL_GPL(acpi_is_root_bridge
);
167 get_root_bridge_busnr_callback(struct acpi_resource
*resource
, void *data
)
170 struct acpi_resource_address64 address
;
172 if (resource
->type
!= ACPI_RESOURCE_TYPE_ADDRESS16
&&
173 resource
->type
!= ACPI_RESOURCE_TYPE_ADDRESS32
&&
174 resource
->type
!= ACPI_RESOURCE_TYPE_ADDRESS64
)
177 acpi_resource_to_address64(resource
, &address
);
178 if ((address
.address_length
> 0) &&
179 (address
.resource_type
== ACPI_BUS_NUMBER_RANGE
))
180 *busnr
= address
.minimum
;
185 static acpi_status
try_get_root_bridge_busnr(acpi_handle handle
,
186 unsigned long long *bus
)
193 acpi_walk_resources(handle
, METHOD_NAME__CRS
,
194 get_root_bridge_busnr_callback
, &busnum
);
195 if (ACPI_FAILURE(status
))
197 /* Check if we really get a bus number from _CRS */
204 static void acpi_pci_bridge_scan(struct acpi_device
*device
)
207 struct acpi_device
*child
= NULL
;
209 if (device
->flags
.bus_address
)
210 if (device
->parent
&& device
->parent
->ops
.bind
) {
211 status
= device
->parent
->ops
.bind(device
);
213 list_for_each_entry(child
, &device
->children
, node
)
214 acpi_pci_bridge_scan(child
);
219 static u8 OSC_UUID
[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40,
220 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
222 static acpi_status
acpi_pci_run_osc(acpi_handle handle
,
223 const u32
*capbuf
, u32
*retval
)
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
;
232 /* Setting up input parameters */
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
;
246 status
= acpi_evaluate_object(handle
, "_OSC", &input
, &output
);
247 if (ACPI_FAILURE(status
))
251 return AE_NULL_OBJECT
;
253 out_obj
= output
.pointer
;
254 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
255 printk(KERN_DEBUG
"_OSC evaluation returned wrong type\n");
259 /* Need to ignore the bit0 in result code */
260 errors
= *((u32
*)out_obj
->buffer
.pointer
) & ~(1 << 0);
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
)
272 "Firmware did not grant requested _OSC control\n");
280 *retval
= *((u32
*)(out_obj
->buffer
.pointer
+ 8));
284 kfree(output
.pointer
);
288 static acpi_status
acpi_pci_query_osc(struct acpi_pci_root
*root
, u32 flags
)
291 u32 support_set
, result
, capbuf
[3];
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
;
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;
308 static acpi_status
acpi_pci_osc_support(struct acpi_pci_root
*root
, u32 flags
)
313 status
= acpi_get_handle(root
->device
->handle
, "_OSC", &tmp
);
314 if (ACPI_FAILURE(status
))
316 mutex_lock(&osc_lock
);
317 status
= acpi_pci_query_osc(root
, flags
);
318 mutex_unlock(&osc_lock
);
322 static struct acpi_pci_root
*acpi_pci_find_root(acpi_handle handle
)
324 struct acpi_pci_root
*root
;
326 list_for_each_entry(root
, &acpi_pci_roots
, node
) {
327 if (root
->device
->handle
== handle
)
333 struct acpi_handle_node
{
334 struct list_head node
;
339 * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
340 * @handle: the handle in question
342 * Given an ACPI CA handle, the desired PCI device is located in the
343 * list of PCI devices.
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.
350 struct pci_dev
*acpi_get_pci_dev(acpi_handle handle
)
353 unsigned long long adr
;
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
);
363 * Walk up the ACPI CA namespace until we reach a PCI root bridge.
366 while (!acpi_is_root_bridge(phandle
)) {
367 node
= kzalloc(sizeof(struct acpi_handle_node
), GFP_KERNEL
);
371 INIT_LIST_HEAD(&node
->node
);
372 node
->handle
= phandle
;
373 list_add(&node
->node
, &device_list
);
375 status
= acpi_get_parent(phandle
, &phandle
);
376 if (ACPI_FAILURE(status
))
380 root
= acpi_pci_find_root(phandle
);
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.
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
))
396 dev
= (adr
>> 16) & 0xffff;
399 pdev
= pci_get_slot(pbus
, PCI_DEVFN(dev
, fn
));
400 if (!pdev
|| hnd
== handle
)
403 pbus
= pdev
->subordinate
;
407 list_for_each_entry_safe(node
, tmp
, &device_list
, node
)
412 EXPORT_SYMBOL_GPL(acpi_get_pci_dev
);
415 * acpi_pci_osc_control_set - commit requested control to Firmware
416 * @handle: acpi_handle for the target ACPI object
417 * @flags: driver's requested control bits
419 * Attempt to take control from Firmware on requested control bits.
421 acpi_status
acpi_pci_osc_control_set(acpi_handle handle
, u32 flags
)
424 u32 control_req
, result
, capbuf
[3];
426 struct acpi_pci_root
*root
;
428 status
= acpi_get_handle(handle
, "_OSC", &tmp
);
429 if (ACPI_FAILURE(status
))
432 control_req
= (flags
& OSC_CONTROL_MASKS
);
436 root
= acpi_pci_find_root(handle
);
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
)
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
))
451 if ((root
->osc_control_qry
& control_req
) != control_req
) {
453 "Firmware did not grant requested _OSC control\n");
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
;
465 mutex_unlock(&osc_lock
);
468 EXPORT_SYMBOL(acpi_pci_osc_control_set
);
470 static int __devinit
acpi_pci_root_add(struct acpi_device
*device
)
472 unsigned long long segment
, bus
;
475 struct acpi_pci_root
*root
;
477 struct acpi_device
*child
;
478 u32 flags
, base_flags
;
481 status
= acpi_evaluate_integer(device
->handle
, METHOD_NAME__SEG
, NULL
,
483 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
484 printk(KERN_ERR PREFIX
"can't evaluate _SEG\n");
488 /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
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");
500 root
= kzalloc(sizeof(struct acpi_pci_root
), GFP_KERNEL
);
504 INIT_LIST_HEAD(&root
->node
);
505 root
->device
= device
;
506 root
->segment
= segment
& 0xFFFF;
507 root
->bus_nr
= bus
& 0xFF;
508 strcpy(acpi_device_name(device
), ACPI_PCI_ROOT_DEVICE_NAME
);
509 strcpy(acpi_device_class(device
), ACPI_PCI_ROOT_CLASS
);
510 device
->driver_data
= root
;
513 * All supported architectures that use ACPI have support for
514 * PCI domains, so we indicate this in _OSC support capabilities.
516 flags
= base_flags
= OSC_PCI_SEGMENT_GROUPS_SUPPORT
;
517 acpi_pci_osc_support(root
, flags
);
520 * TBD: Need PCI interface for enumeration/configuration of roots.
524 list_add_tail(&root
->node
, &acpi_pci_roots
);
526 printk(KERN_INFO PREFIX
"%s [%s] (%04x:%02x)\n",
527 acpi_device_name(device
), acpi_device_bid(device
),
528 root
->segment
, root
->bus_nr
);
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).
537 root
->bus
= pci_acpi_scan_root(device
, segment
, bus
);
539 printk(KERN_ERR PREFIX
540 "Bus %04x:%02x not present in PCI namespace\n",
541 root
->segment
, root
->bus_nr
);
547 * Attach ACPI-PCI Context
548 * -----------------------
549 * Thus binding the ACPI and PCI devices.
551 result
= acpi_pci_bind_root(device
);
558 * Evaluate and parse _PRT, if exists.
560 status
= acpi_get_handle(device
->handle
, METHOD_NAME__PRT
, &handle
);
561 if (ACPI_SUCCESS(status
))
562 result
= acpi_pci_irq_add_prt(device
->handle
, root
->bus
);
565 * Scan and bind all _ADR-Based Devices
567 list_for_each_entry(child
, &device
->children
, node
)
568 acpi_pci_bridge_scan(child
);
570 /* Indicate support for various _OSC capabilities. */
571 if (pci_ext_cfg_avail(root
->bus
->self
))
572 flags
|= OSC_EXT_PCI_CONFIG_SUPPORT
;
573 if (pcie_aspm_enabled())
574 flags
|= OSC_ACTIVE_STATE_PWR_SUPPORT
|
575 OSC_CLOCK_PWR_CAPABILITY_SUPPORT
;
576 if (pci_msi_enabled())
577 flags
|= OSC_MSI_SUPPORT
;
578 if (flags
!= base_flags
)
579 acpi_pci_osc_support(root
, flags
);
584 if (!list_empty(&root
->node
))
585 list_del(&root
->node
);
590 static int acpi_pci_root_start(struct acpi_device
*device
)
592 struct acpi_pci_root
*root
= acpi_driver_data(device
);
594 pci_bus_add_devices(root
->bus
);
598 static int acpi_pci_root_remove(struct acpi_device
*device
, int type
)
600 struct acpi_pci_root
*root
= acpi_driver_data(device
);
606 static int __init
acpi_pci_root_init(void)
608 if (acpi_pci_disabled
)
611 if (acpi_bus_register_driver(&acpi_pci_root_driver
) < 0)
617 subsys_initcall(acpi_pci_root_init
);