blob: 417487a201fbe0aa43f2e241407b3b8fe8139dcd [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>
Taku Izumid0020f62012-09-18 15:21:31 +090030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/pm.h>
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010032#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/pci.h>
Andrew Patterson990a7ac2008-11-10 15:30:45 -070034#include <linux/pci-acpi.h>
Naga Chumbalkareca67312011-03-21 03:29:20 +000035#include <linux/pci-aspm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <acpi/acpi_bus.h>
39#include <acpi/acpi_drivers.h>
Rafael J. Wysocki415e12b2011-01-07 00:55:09 +010040#include <acpi/apei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Len Browna192a952009-07-28 16:45:54 -040042#define PREFIX "ACPI: "
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define _COMPONENT ACPI_PCI_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050045ACPI_MODULE_NAME("pci_root");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ACPI_PCI_ROOT_CLASS "pci_bridge"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
Len Brown4be44fc2005-08-05 00:44:28 -040048static int acpi_pci_root_add(struct acpi_device *device);
49static int acpi_pci_root_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Rafael J. Wysocki415e12b2011-01-07 00:55:09 +010051#define ACPI_PCIE_REQ_SUPPORT (OSC_EXT_PCI_CONFIG_SUPPORT \
52 | OSC_ACTIVE_STATE_PWR_SUPPORT \
53 | OSC_CLOCK_PWR_CAPABILITY_SUPPORT \
54 | OSC_MSI_SUPPORT)
55
Márton Némethc97adf92010-01-10 17:15:36 +010056static const struct acpi_device_id root_device_ids[] = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020057 {"PNP0A03", 0},
58 {"", 0},
59};
60MODULE_DEVICE_TABLE(acpi, root_device_ids);
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static struct acpi_driver acpi_pci_root_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050063 .name = "pci_root",
Len Brown4be44fc2005-08-05 00:44:28 -040064 .class = ACPI_PCI_ROOT_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020065 .ids = root_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040066 .ops = {
67 .add = acpi_pci_root_add,
68 .remove = acpi_pci_root_remove,
Len Brown4be44fc2005-08-05 00:44:28 -040069 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Taku Izumid0020f62012-09-18 15:21:31 +090072/* Lock to protect both acpi_pci_roots and acpi_pci_drivers lists */
73static DEFINE_MUTEX(acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static LIST_HEAD(acpi_pci_roots);
Jiang Liu8ee5bdf2012-09-18 15:19:49 +090075static LIST_HEAD(acpi_pci_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Kenji Kaneshige63f10f02009-02-09 15:59:29 +090077static DEFINE_MUTEX(osc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79int acpi_pci_register_driver(struct acpi_pci_driver *driver)
80{
81 int n = 0;
Bjorn Helgaasc1aec832009-06-18 14:47:02 -060082 struct acpi_pci_root *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Taku Izumid0020f62012-09-18 15:21:31 +090084 mutex_lock(&acpi_pci_root_lock);
Jiang Liu8ee5bdf2012-09-18 15:19:49 +090085 list_add_tail(&driver->node, &acpi_pci_drivers);
Taku Izumid0020f62012-09-18 15:21:31 +090086 if (driver->add)
87 list_for_each_entry(root, &acpi_pci_roots, node) {
Taku Izumi55bfe3c2012-09-18 15:22:35 +090088 driver->add(root);
Taku Izumid0020f62012-09-18 15:21:31 +090089 n++;
90 }
91 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 return n;
94}
95EXPORT_SYMBOL(acpi_pci_register_driver);
96
97void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
98{
Bjorn Helgaasc1aec832009-06-18 14:47:02 -060099 struct acpi_pci_root *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Taku Izumid0020f62012-09-18 15:21:31 +0900101 mutex_lock(&acpi_pci_root_lock);
Jiang Liu8ee5bdf2012-09-18 15:19:49 +0900102 list_del(&driver->node);
Taku Izumid0020f62012-09-18 15:21:31 +0900103 if (driver->remove)
104 list_for_each_entry(root, &acpi_pci_roots, node)
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900105 driver->remove(root);
Taku Izumid0020f62012-09-18 15:21:31 +0900106 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108EXPORT_SYMBOL(acpi_pci_unregister_driver);
109
Alexander Chiang27558202009-06-10 19:55:14 +0000110/**
111 * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
112 * @handle - the ACPI CA node in question.
113 *
114 * Note: we could make this API take a struct acpi_device * instead, but
115 * for now, it's more convenient to operate on an acpi_handle.
116 */
117int acpi_is_root_bridge(acpi_handle handle)
118{
119 int ret;
120 struct acpi_device *device;
121
122 ret = acpi_bus_get_device(handle, &device);
123 if (ret)
124 return 0;
125
126 ret = acpi_match_device_ids(device, root_device_ids);
127 if (ret)
128 return 0;
129 else
130 return 1;
131}
132EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400135get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700137 struct resource *res = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct acpi_resource_address64 address;
139
Bob Moore50eca3e2005-09-30 19:03:00 -0400140 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
141 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
142 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return AE_OK;
144
145 acpi_resource_to_address64(resource, &address);
Len Brown4be44fc2005-08-05 00:44:28 -0400146 if ((address.address_length > 0) &&
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700147 (address.resource_type == ACPI_BUS_NUMBER_RANGE)) {
148 res->start = address.minimum;
149 res->end = address.minimum + address.address_length - 1;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 return AE_OK;
153}
154
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600155static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700156 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 acpi_status status;
159
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700160 res->start = -1;
Len Brown4be44fc2005-08-05 00:44:28 -0400161 status =
162 acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700163 get_root_bridge_busnr_callback, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (ACPI_FAILURE(status))
165 return status;
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700166 if (res->start == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return AE_ERROR;
168 return AE_OK;
169}
170
Shaohua Li3a9622d2009-10-29 11:04:50 +0800171static u8 pci_osc_uuid_str[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900172
173static acpi_status acpi_pci_run_osc(acpi_handle handle,
174 const u32 *capbuf, u32 *retval)
175{
Shaohua Li3a9622d2009-10-29 11:04:50 +0800176 struct acpi_osc_context context = {
177 .uuid_str = pci_osc_uuid_str,
178 .rev = 1,
179 .cap.length = 12,
180 .cap.pointer = (void *)capbuf,
181 };
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900182 acpi_status status;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900183
Shaohua Li3a9622d2009-10-29 11:04:50 +0800184 status = acpi_run_osc(handle, &context);
185 if (ACPI_SUCCESS(status)) {
186 *retval = *((u32 *)(context.ret.pointer + 8));
187 kfree(context.ret.pointer);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900188 }
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900189 return status;
190}
191
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200192static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root,
193 u32 support,
194 u32 *control)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900195{
196 acpi_status status;
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200197 u32 result, capbuf[3];
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900198
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200199 support &= OSC_PCI_SUPPORT_MASKS;
200 support |= root->osc_support_set;
201
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900202 capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200203 capbuf[OSC_SUPPORT_TYPE] = support;
204 if (control) {
205 *control &= OSC_PCI_CONTROL_MASKS;
206 capbuf[OSC_CONTROL_TYPE] = *control | root->osc_control_set;
207 } else {
208 /* Run _OSC query for all possible controls. */
209 capbuf[OSC_CONTROL_TYPE] = OSC_PCI_CONTROL_MASKS;
210 }
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900211
212 status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
213 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200214 root->osc_support_set = support;
Rafael J. Wysocki2b8fd912010-08-23 23:55:59 +0200215 if (control)
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200216 *control = result;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900217 }
218 return status;
219}
220
221static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
222{
223 acpi_status status;
224 acpi_handle tmp;
225
226 status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
227 if (ACPI_FAILURE(status))
228 return status;
229 mutex_lock(&osc_lock);
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200230 status = acpi_pci_query_osc(root, flags, NULL);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900231 mutex_unlock(&osc_lock);
232 return status;
233}
234
Alex Chiang76d56de2009-07-23 17:03:00 -0600235struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900236{
237 struct acpi_pci_root *root;
Taku Izumicd4faf92012-09-18 15:23:23 +0900238 struct acpi_device *device;
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600239
Taku Izumicd4faf92012-09-18 15:23:23 +0900240 if (acpi_bus_get_device(handle, &device) ||
241 acpi_match_device_ids(device, root_device_ids))
242 return NULL;
243
244 root = acpi_driver_data(device);
245
246 return root;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900247}
Alex Chiang76d56de2009-07-23 17:03:00 -0600248EXPORT_SYMBOL_GPL(acpi_pci_find_root);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900249
Alexander Chiang2f7bbce2009-06-10 19:55:20 +0000250struct acpi_handle_node {
251 struct list_head node;
252 acpi_handle handle;
253};
254
255/**
256 * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
257 * @handle: the handle in question
258 *
259 * Given an ACPI CA handle, the desired PCI device is located in the
260 * list of PCI devices.
261 *
262 * If the device is found, its reference count is increased and this
263 * function returns a pointer to its data structure. The caller must
264 * decrement the reference count by calling pci_dev_put().
265 * If no device is found, %NULL is returned.
266 */
267struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
268{
269 int dev, fn;
270 unsigned long long adr;
271 acpi_status status;
272 acpi_handle phandle;
273 struct pci_bus *pbus;
274 struct pci_dev *pdev = NULL;
275 struct acpi_handle_node *node, *tmp;
276 struct acpi_pci_root *root;
277 LIST_HEAD(device_list);
278
279 /*
280 * Walk up the ACPI CA namespace until we reach a PCI root bridge.
281 */
282 phandle = handle;
283 while (!acpi_is_root_bridge(phandle)) {
284 node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
285 if (!node)
286 goto out;
287
288 INIT_LIST_HEAD(&node->node);
289 node->handle = phandle;
290 list_add(&node->node, &device_list);
291
292 status = acpi_get_parent(phandle, &phandle);
293 if (ACPI_FAILURE(status))
294 goto out;
295 }
296
297 root = acpi_pci_find_root(phandle);
298 if (!root)
299 goto out;
300
301 pbus = root->bus;
302
303 /*
304 * Now, walk back down the PCI device tree until we return to our
305 * original handle. Assumes that everything between the PCI root
306 * bridge and the device we're looking for must be a P2P bridge.
307 */
308 list_for_each_entry(node, &device_list, node) {
309 acpi_handle hnd = node->handle;
310 status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
311 if (ACPI_FAILURE(status))
312 goto out;
313 dev = (adr >> 16) & 0xffff;
314 fn = adr & 0xffff;
315
316 pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
Troy Moure412af972009-06-25 17:05:35 -0600317 if (!pdev || hnd == handle)
Alexander Chiang2f7bbce2009-06-10 19:55:20 +0000318 break;
319
320 pbus = pdev->subordinate;
321 pci_dev_put(pdev);
Rafael J. Wysocki497fb542009-10-13 01:01:57 +0200322
323 /*
324 * This function may be called for a non-PCI device that has a
325 * PCI parent (eg. a disk under a PCI SATA controller). In that
326 * case pdev->subordinate will be NULL for the parent.
327 */
328 if (!pbus) {
329 dev_dbg(&pdev->dev, "Not a PCI-to-PCI bridge\n");
330 pdev = NULL;
331 break;
332 }
Alexander Chiang2f7bbce2009-06-10 19:55:20 +0000333 }
334out:
335 list_for_each_entry_safe(node, tmp, &device_list, node)
336 kfree(node);
337
338 return pdev;
339}
340EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
341
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900342/**
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200343 * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
344 * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
345 * @mask: Mask of _OSC bits to request control of, place to store control mask.
346 * @req: Mask of _OSC bits the control of is essential to the caller.
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900347 *
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200348 * Run _OSC query for @mask and if that is successful, compare the returned
349 * mask of control bits with @req. If all of the @req bits are set in the
350 * returned mask, run _OSC request for it.
351 *
352 * The variable at the @mask address may be modified regardless of whether or
353 * not the function returns success. On success it will contain the mask of
354 * _OSC bits the BIOS has granted control of, but its contents are meaningless
355 * on failure.
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900356 **/
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200357acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 *mask, u32 req)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900358{
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900359 struct acpi_pci_root *root;
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200360 acpi_status status;
361 u32 ctrl, capbuf[3];
362 acpi_handle tmp;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900363
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200364 if (!mask)
365 return AE_BAD_PARAMETER;
366
367 ctrl = *mask & OSC_PCI_CONTROL_MASKS;
368 if ((ctrl & req) != req)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900369 return AE_TYPE;
370
371 root = acpi_pci_find_root(handle);
372 if (!root)
373 return AE_NOT_EXIST;
374
Rafael J. Wysockib879dc42010-08-21 01:52:37 +0200375 status = acpi_get_handle(handle, "_OSC", &tmp);
376 if (ACPI_FAILURE(status))
377 return status;
378
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900379 mutex_lock(&osc_lock);
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200380
381 *mask = ctrl | root->osc_control_set;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900382 /* No need to evaluate _OSC if the control was already granted. */
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200383 if ((root->osc_control_set & ctrl) == ctrl)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900384 goto out;
385
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200386 /* Need to check the available controls bits before requesting them. */
387 while (*mask) {
388 status = acpi_pci_query_osc(root, root->osc_support_set, mask);
389 if (ACPI_FAILURE(status))
390 goto out;
391 if (ctrl == *mask)
392 break;
393 ctrl = *mask;
394 }
Rafael J. Wysocki2b8fd912010-08-23 23:55:59 +0200395
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200396 if ((ctrl & req) != req) {
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900397 status = AE_SUPPORT;
398 goto out;
399 }
400
401 capbuf[OSC_QUERY_TYPE] = 0;
402 capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200403 capbuf[OSC_CONTROL_TYPE] = ctrl;
404 status = acpi_pci_run_osc(handle, capbuf, mask);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900405 if (ACPI_SUCCESS(status))
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200406 root->osc_control_set = *mask;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900407out:
408 mutex_unlock(&osc_lock);
409 return status;
410}
Kenji Kaneshige9f5404d2009-02-09 16:00:04 +0900411EXPORT_SYMBOL(acpi_pci_osc_control_set);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900412
Bill Pembertonda095fd2012-11-19 13:22:46 -0500413static int acpi_pci_root_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600415 unsigned long long segment, bus;
416 acpi_status status;
417 int result;
418 struct acpi_pci_root *root;
419 acpi_handle handle;
Rafael J. Wysocki47525cd2012-12-21 00:36:45 +0100420 struct acpi_pci_driver *driver;
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700421 u32 flags, base_flags;
Taku Izumi8c33f512012-10-30 15:27:13 +0900422 bool is_osc_granted = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700424 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
425 if (!root)
426 return -ENOMEM;
427
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600428 segment = 0;
429 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
430 &segment);
431 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
432 printk(KERN_ERR PREFIX "can't evaluate _SEG\n");
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700433 result = -ENODEV;
434 goto end;
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600437 /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700438 root->secondary.flags = IORESOURCE_BUS;
439 status = try_get_root_bridge_busnr(device->handle, &root->secondary);
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600440 if (ACPI_FAILURE(status)) {
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700441 /*
442 * We need both the start and end of the downstream bus range
443 * to interpret _CBA (MMCONFIG base address), so it really is
444 * supposed to be in _CRS. If we don't find it there, all we
445 * can do is assume [_BBN-0xFF] or [0-0xFF].
446 */
447 root->secondary.end = 0xFF;
448 printk(KERN_WARNING FW_BUG PREFIX
449 "no secondary bus range in _CRS\n");
Jon Masone545b552011-06-19 18:51:37 -0500450 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN,
451 NULL, &bus);
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700452 if (ACPI_SUCCESS(status))
453 root->secondary.start = bus;
454 else if (status == AE_NOT_FOUND)
455 root->secondary.start = 0;
456 else {
457 printk(KERN_ERR PREFIX "can't evaluate _BBN\n");
458 result = -ENODEV;
459 goto end;
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600460 }
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600463 INIT_LIST_HEAD(&root->node);
Patrick Mochel32917e52006-05-19 16:54:39 -0400464 root->device = device;
Bjorn Helgaas07054952009-06-18 14:47:07 -0600465 root->segment = segment & 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
467 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700468 device->driver_data = root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Bjorn Helgaasd4761ba2012-10-30 15:24:50 +0900470 printk(KERN_INFO PREFIX "%s [%s] (domain %04x %pR)\n",
471 acpi_device_name(device), acpi_device_bid(device),
472 root->segment, &root->secondary);
473
474 /*
475 * PCI Routing Table
476 * -----------------
477 * Evaluate and parse _PRT, if exists.
478 */
479 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
480 if (ACPI_SUCCESS(status))
481 result = acpi_pci_irq_add_prt(device->handle, root->segment,
482 root->secondary.start);
483
Jiang Liuf4b57a32012-06-22 14:55:16 +0800484 root->mcfg_addr = acpi_pci_root_get_mcfg_addr(device->handle);
485
Andrew Patterson990a7ac2008-11-10 15:30:45 -0700486 /*
487 * All supported architectures that use ACPI have support for
488 * PCI domains, so we indicate this in _OSC support capabilities.
489 */
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700490 flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900491 acpi_pci_osc_support(root, flags);
Andrew Patterson990a7ac2008-11-10 15:30:45 -0700492
Taku Izumi8c33f512012-10-30 15:27:13 +0900493 /* Indicate support for various _OSC capabilities. */
494 if (pci_ext_cfg_avail())
495 flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
496 if (pcie_aspm_support_enabled()) {
497 flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
498 OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
499 }
500 if (pci_msi_enabled())
501 flags |= OSC_MSI_SUPPORT;
502 if (flags != base_flags) {
503 status = acpi_pci_osc_support(root, flags);
504 if (ACPI_FAILURE(status)) {
505 dev_info(&device->dev, "ACPI _OSC support "
506 "notification failed, disabling PCIe ASPM\n");
507 pcie_no_aspm();
508 flags = base_flags;
509 }
510 }
511 if (!pcie_ports_disabled
512 && (flags & ACPI_PCIE_REQ_SUPPORT) == ACPI_PCIE_REQ_SUPPORT) {
513 flags = OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL
514 | OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
515 | OSC_PCI_EXPRESS_PME_CONTROL;
516
517 if (pci_aer_available()) {
518 if (aer_acpi_firmware_first())
519 dev_dbg(&device->dev,
520 "PCIe errors handled by BIOS.\n");
521 else
522 flags |= OSC_PCI_EXPRESS_AER_CONTROL;
523 }
524
525 dev_info(&device->dev,
526 "Requesting ACPI _OSC control (0x%02x)\n", flags);
527
528 status = acpi_pci_osc_control_set(device->handle, &flags,
529 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
530 if (ACPI_SUCCESS(status)) {
531 is_osc_granted = true;
532 dev_info(&device->dev,
533 "ACPI _OSC control (0x%02x) granted\n", flags);
534 } else {
535 is_osc_granted = false;
536 dev_info(&device->dev,
537 "ACPI _OSC request failed (%s), "
538 "returned control mask: 0x%02x\n",
539 acpi_format_exception(status), flags);
540 }
541 } else {
542 dev_info(&device->dev,
543 "Unable to request _OSC control "
544 "(_OSC support mask: 0x%02x)\n", flags);
545 }
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /*
548 * TBD: Need PCI interface for enumeration/configuration of roots.
549 */
550
Taku Izumi6507e6e2012-09-18 15:24:40 +0900551 mutex_lock(&acpi_pci_root_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400552 list_add_tail(&root->node, &acpi_pci_roots);
Taku Izumi6507e6e2012-09-18 15:24:40 +0900553 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
556 * Scan the Root Bridge
557 * --------------------
558 * Must do this prior to any attempt to bind the root device, as the
559 * PCI namespace does not get created until this call is made (and
560 * thus the root bridge's pci_dev does not exist).
561 */
Bjorn Helgaas57283772010-03-11 12:20:11 -0700562 root->bus = pci_acpi_scan_root(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!root->bus) {
Len Brown64684632006-06-26 23:41:38 -0400564 printk(KERN_ERR PREFIX
565 "Bus %04x:%02x not present in PCI namespace\n",
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700566 root->segment, (unsigned int)root->secondary.start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 result = -ENODEV;
Taku Izumi6507e6e2012-09-18 15:24:40 +0900568 goto out_del_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
Taku Izumi8c33f512012-10-30 15:27:13 +0900571 /* ASPM setting */
572 if (is_osc_granted) {
573 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM)
574 pcie_clear_aspm(root->bus);
Rafael J. Wysockia2466702011-04-30 00:21:38 +0200575 } else {
Taku Izumi8c33f512012-10-30 15:27:13 +0900576 pr_info("ACPI _OSC control for PCIe not granted, "
577 "disabling ASPM\n");
578 pcie_no_aspm();
Rafael J. Wysocki415e12b2011-01-07 00:55:09 +0100579 }
580
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100581 pci_acpi_add_bus_pm_notifier(device, root->bus);
582 if (device->wakeup.flags.run_wake)
583 device_set_run_wake(root->bus->bridge, true);
584
Yinghai Lu3c449ed2012-11-03 21:39:31 -0700585 if (system_state != SYSTEM_BOOTING) {
586 pcibios_resource_survey_bus(root->bus);
Yinghai Lu62a08c52012-10-30 14:31:32 -0600587 pci_assign_unassigned_bus_resources(root->bus);
Yinghai Lu3c449ed2012-11-03 21:39:31 -0700588 }
Yinghai Lu62a08c52012-10-30 14:31:32 -0600589
Taku Izumid0020f62012-09-18 15:21:31 +0900590 mutex_lock(&acpi_pci_root_lock);
Jiang Liuc8e9afb2012-09-18 15:20:34 +0900591 list_for_each_entry(driver, &acpi_pci_drivers, node)
592 if (driver->add)
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900593 driver->add(root);
Taku Izumid0020f62012-09-18 15:21:31 +0900594 mutex_unlock(&acpi_pci_root_lock);
Rajesh Shahc431ada2005-04-28 00:25:45 -0700595
Yinghai Lu62a08c52012-10-30 14:31:32 -0600596 /* need to after hot-added ioapic is registered */
597 if (system_state != SYSTEM_BOOTING)
598 pci_enable_bridges(root->bus);
599
Bjorn Helgaascaf420c2009-06-18 14:46:57 -0600600 pci_bus_add_devices(root->bus);
601 return 0;
Rafael J. Wysocki47525cd2012-12-21 00:36:45 +0100602
603out_del_root:
604 mutex_lock(&acpi_pci_root_lock);
605 list_del(&root->node);
606 mutex_unlock(&acpi_pci_root_lock);
607
608 acpi_pci_irq_del_prt(root->segment, root->secondary.start);
609end:
610 kfree(root);
611 return result;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Len Brown4be44fc2005-08-05 00:44:28 -0400614static int acpi_pci_root_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Yinghai Lu13b6a912012-10-30 14:31:48 -0600616 acpi_status status;
617 acpi_handle handle;
Bjorn Helgaascaf420c2009-06-18 14:46:57 -0600618 struct acpi_pci_root *root = acpi_driver_data(device);
Jiang Liuc8e9afb2012-09-18 15:20:34 +0900619 struct acpi_pci_driver *driver;
620
Yinghai Lu9738a1f2012-10-30 14:31:43 -0600621 pci_stop_root_bus(root->bus);
622
Taku Izumid0020f62012-09-18 15:21:31 +0900623 mutex_lock(&acpi_pci_root_lock);
Yinghai Luf426cef2012-10-30 14:31:52 -0600624 list_for_each_entry_reverse(driver, &acpi_pci_drivers, node)
Jiang Liuc8e9afb2012-09-18 15:20:34 +0900625 if (driver->remove)
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900626 driver->remove(root);
Yinghai Lu9738a1f2012-10-30 14:31:43 -0600627 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100629 device_set_run_wake(root->bus->bridge, false);
630 pci_acpi_remove_bus_pm_notifier(device);
631
Yinghai Lu13b6a912012-10-30 14:31:48 -0600632 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
633 if (ACPI_SUCCESS(status))
Bjorn Helgaas79c44122012-10-30 15:24:06 +0900634 acpi_pci_irq_del_prt(root->segment, root->secondary.start);
Yinghai Lu13b6a912012-10-30 14:31:48 -0600635
Yinghai Lu9738a1f2012-10-30 14:31:43 -0600636 pci_remove_root_bus(root->bus);
637
638 mutex_lock(&acpi_pci_root_lock);
Taku Izumi6507e6e2012-09-18 15:24:40 +0900639 list_del(&root->node);
640 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 kfree(root);
Patrick Mocheld550d982006-06-27 00:41:40 -0400642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +0100645int __init acpi_pci_root_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Rafael J. Wysockid3072e62011-01-16 20:44:22 +0100647 acpi_hest_init();
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (acpi_pci_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Bjorn Helgaas7bc5e3f2010-02-23 10:24:41 -0700652 pci_acpi_crs_quirks();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -0400654 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Patrick Mocheld550d982006-06-27 00:41:40 -0400656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
Yinghai Lu668192b2013-01-21 13:20:48 -0800658/* Support root bridge hotplug */
659
660static void handle_root_bridge_insertion(acpi_handle handle)
661{
662 struct acpi_device *device;
663
664 if (!acpi_bus_get_device(handle, &device)) {
665 printk(KERN_DEBUG "acpi device exists...\n");
666 return;
667 }
668
669 if (acpi_bus_scan(handle))
670 printk(KERN_ERR "cannot add bridge to acpi list\n");
671}
672
673static void handle_root_bridge_removal(struct acpi_device *device)
674{
675 struct acpi_eject_event *ej_event;
676
677 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
678 if (!ej_event) {
679 /* Inform firmware the hot-remove operation has error */
680 (void) acpi_evaluate_hotplug_ost(device->handle,
681 ACPI_NOTIFY_EJECT_REQUEST,
682 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
683 NULL);
684 return;
685 }
686
687 ej_event->device = device;
688 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
689
690 acpi_bus_hot_remove_device(ej_event);
691}
692
693static void _handle_hotplug_event_root(struct work_struct *work)
694{
695 struct acpi_pci_root *root;
696 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER };
697 struct acpi_hp_work *hp_work;
698 acpi_handle handle;
699 u32 type;
700
701 hp_work = container_of(work, struct acpi_hp_work, work);
702 handle = hp_work->handle;
703 type = hp_work->type;
704
705 root = acpi_pci_find_root(handle);
706
707 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
708
709 switch (type) {
710 case ACPI_NOTIFY_BUS_CHECK:
711 /* bus enumerate */
712 printk(KERN_DEBUG "%s: Bus check notify on %s\n", __func__,
713 (char *)buffer.pointer);
714 if (!root)
715 handle_root_bridge_insertion(handle);
716
717 break;
718
719 case ACPI_NOTIFY_DEVICE_CHECK:
720 /* device check */
721 printk(KERN_DEBUG "%s: Device check notify on %s\n", __func__,
722 (char *)buffer.pointer);
723 if (!root)
724 handle_root_bridge_insertion(handle);
725 break;
726
727 case ACPI_NOTIFY_EJECT_REQUEST:
728 /* request device eject */
729 printk(KERN_DEBUG "%s: Device eject notify on %s\n", __func__,
730 (char *)buffer.pointer);
731 if (root)
732 handle_root_bridge_removal(root->device);
733 break;
734 default:
735 printk(KERN_WARNING "notify_handler: unknown event type 0x%x for %s\n",
736 type, (char *)buffer.pointer);
737 break;
738 }
739
740 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
741 kfree(buffer.pointer);
742}
743
744static void handle_hotplug_event_root(acpi_handle handle, u32 type,
745 void *context)
746{
747 alloc_acpi_hp_work(handle, type, context,
748 _handle_hotplug_event_root);
749}
750
751static acpi_status __init
752find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
753{
Tang Chen121b0902013-01-21 13:20:49 -0800754 acpi_status status;
Yinghai Lu668192b2013-01-21 13:20:48 -0800755 char objname[64];
756 struct acpi_buffer buffer = { .length = sizeof(objname),
757 .pointer = objname };
758 int *count = (int *)context;
759
760 if (!acpi_is_root_bridge(handle))
761 return AE_OK;
762
763 (*count)++;
764
765 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
766
Tang Chen121b0902013-01-21 13:20:49 -0800767 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
768 handle_hotplug_event_root, NULL);
769 if (ACPI_FAILURE(status))
770 printk(KERN_DEBUG "acpi root: %s notify handler is not installed, exit status: %u\n",
771 objname, (unsigned int)status);
772 else
773 printk(KERN_DEBUG "acpi root: %s notify handler is installed\n",
774 objname);
Yinghai Lu668192b2013-01-21 13:20:48 -0800775
776 return AE_OK;
777}
778
779void __init acpi_pci_root_hp_init(void)
780{
781 int num = 0;
782
783 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
784 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
785
786 printk(KERN_DEBUG "Found %d acpi root devices\n", num);
787}