blob: b3cc69c5caf1e07bd93aa4111bfcbb0a0d4293b5 [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"
Rafael J. Wysocki00c43b92013-01-30 14:27:33 +010048static int acpi_pci_root_add(struct acpi_device *device,
49 const struct acpi_device_id *not_used);
50static void acpi_pci_root_remove(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Rafael J. Wysocki415e12b2011-01-07 00:55:09 +010052#define ACPI_PCIE_REQ_SUPPORT (OSC_EXT_PCI_CONFIG_SUPPORT \
53 | OSC_ACTIVE_STATE_PWR_SUPPORT \
54 | OSC_CLOCK_PWR_CAPABILITY_SUPPORT \
55 | OSC_MSI_SUPPORT)
56
Márton Némethc97adf92010-01-10 17:15:36 +010057static const struct acpi_device_id root_device_ids[] = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020058 {"PNP0A03", 0},
59 {"", 0},
60};
Thomas Renninger1ba90e32007-07-23 14:44:41 +020061
Rafael J. Wysocki00c43b92013-01-30 14:27:33 +010062static struct acpi_scan_handler pci_root_handler = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020063 .ids = root_device_ids,
Rafael J. Wysocki00c43b92013-01-30 14:27:33 +010064 .attach = acpi_pci_root_add,
65 .detach = acpi_pci_root_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Taku Izumid0020f62012-09-18 15:21:31 +090068/* Lock to protect both acpi_pci_roots and acpi_pci_drivers lists */
69static DEFINE_MUTEX(acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static LIST_HEAD(acpi_pci_roots);
Jiang Liu8ee5bdf2012-09-18 15:19:49 +090071static LIST_HEAD(acpi_pci_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Kenji Kaneshige63f10f02009-02-09 15:59:29 +090073static DEFINE_MUTEX(osc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75int acpi_pci_register_driver(struct acpi_pci_driver *driver)
76{
77 int n = 0;
Bjorn Helgaasc1aec832009-06-18 14:47:02 -060078 struct acpi_pci_root *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Taku Izumid0020f62012-09-18 15:21:31 +090080 mutex_lock(&acpi_pci_root_lock);
Jiang Liu8ee5bdf2012-09-18 15:19:49 +090081 list_add_tail(&driver->node, &acpi_pci_drivers);
Taku Izumid0020f62012-09-18 15:21:31 +090082 if (driver->add)
83 list_for_each_entry(root, &acpi_pci_roots, node) {
Taku Izumi55bfe3c2012-09-18 15:22:35 +090084 driver->add(root);
Taku Izumid0020f62012-09-18 15:21:31 +090085 n++;
86 }
87 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 return n;
90}
91EXPORT_SYMBOL(acpi_pci_register_driver);
92
93void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
94{
Bjorn Helgaasc1aec832009-06-18 14:47:02 -060095 struct acpi_pci_root *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Taku Izumid0020f62012-09-18 15:21:31 +090097 mutex_lock(&acpi_pci_root_lock);
Jiang Liu8ee5bdf2012-09-18 15:19:49 +090098 list_del(&driver->node);
Taku Izumid0020f62012-09-18 15:21:31 +090099 if (driver->remove)
100 list_for_each_entry(root, &acpi_pci_roots, node)
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900101 driver->remove(root);
Taku Izumid0020f62012-09-18 15:21:31 +0900102 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104EXPORT_SYMBOL(acpi_pci_unregister_driver);
105
Justin Chend91a0072006-12-06 10:17:10 -0700106acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
107{
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600108 struct acpi_pci_root *root;
Taku Izumi6507e6e2012-09-18 15:24:40 +0900109 acpi_handle handle = NULL;
Justin Chend91a0072006-12-06 10:17:10 -0700110
Taku Izumi6507e6e2012-09-18 15:24:40 +0900111 mutex_lock(&acpi_pci_root_lock);
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600112 list_for_each_entry(root, &acpi_pci_roots, node)
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700113 if ((root->segment == (u16) seg) &&
Taku Izumi6507e6e2012-09-18 15:24:40 +0900114 (root->secondary.start == (u16) bus)) {
115 handle = root->device->handle;
116 break;
117 }
118 mutex_unlock(&acpi_pci_root_lock);
119 return handle;
Justin Chend91a0072006-12-06 10:17:10 -0700120}
121
122EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
123
Alexander Chiang27558202009-06-10 19:55:14 +0000124/**
125 * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
126 * @handle - the ACPI CA node in question.
127 *
128 * Note: we could make this API take a struct acpi_device * instead, but
129 * for now, it's more convenient to operate on an acpi_handle.
130 */
131int acpi_is_root_bridge(acpi_handle handle)
132{
133 int ret;
134 struct acpi_device *device;
135
136 ret = acpi_bus_get_device(handle, &device);
137 if (ret)
138 return 0;
139
140 ret = acpi_match_device_ids(device, root_device_ids);
141 if (ret)
142 return 0;
143 else
144 return 1;
145}
146EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400149get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700151 struct resource *res = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct acpi_resource_address64 address;
153
Bob Moore50eca3e2005-09-30 19:03:00 -0400154 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
155 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
156 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return AE_OK;
158
159 acpi_resource_to_address64(resource, &address);
Len Brown4be44fc2005-08-05 00:44:28 -0400160 if ((address.address_length > 0) &&
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700161 (address.resource_type == ACPI_BUS_NUMBER_RANGE)) {
162 res->start = address.minimum;
163 res->end = address.minimum + address.address_length - 1;
164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return AE_OK;
167}
168
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600169static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700170 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 acpi_status status;
173
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700174 res->start = -1;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 status =
176 acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700177 get_root_bridge_busnr_callback, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (ACPI_FAILURE(status))
179 return status;
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700180 if (res->start == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return AE_ERROR;
182 return AE_OK;
183}
184
Shaohua Li3a9622d2009-10-29 11:04:50 +0800185static u8 pci_osc_uuid_str[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900186
187static acpi_status acpi_pci_run_osc(acpi_handle handle,
188 const u32 *capbuf, u32 *retval)
189{
Shaohua Li3a9622d2009-10-29 11:04:50 +0800190 struct acpi_osc_context context = {
191 .uuid_str = pci_osc_uuid_str,
192 .rev = 1,
193 .cap.length = 12,
194 .cap.pointer = (void *)capbuf,
195 };
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900196 acpi_status status;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900197
Shaohua Li3a9622d2009-10-29 11:04:50 +0800198 status = acpi_run_osc(handle, &context);
199 if (ACPI_SUCCESS(status)) {
200 *retval = *((u32 *)(context.ret.pointer + 8));
201 kfree(context.ret.pointer);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900202 }
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900203 return status;
204}
205
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200206static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root,
207 u32 support,
208 u32 *control)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900209{
210 acpi_status status;
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200211 u32 result, capbuf[3];
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900212
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200213 support &= OSC_PCI_SUPPORT_MASKS;
214 support |= root->osc_support_set;
215
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900216 capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200217 capbuf[OSC_SUPPORT_TYPE] = support;
218 if (control) {
219 *control &= OSC_PCI_CONTROL_MASKS;
220 capbuf[OSC_CONTROL_TYPE] = *control | root->osc_control_set;
221 } else {
222 /* Run _OSC query for all possible controls. */
223 capbuf[OSC_CONTROL_TYPE] = OSC_PCI_CONTROL_MASKS;
224 }
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900225
226 status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
227 if (ACPI_SUCCESS(status)) {
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200228 root->osc_support_set = support;
Rafael J. Wysocki2b8fd912010-08-23 23:55:59 +0200229 if (control)
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200230 *control = result;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900231 }
232 return status;
233}
234
235static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
236{
237 acpi_status status;
238 acpi_handle tmp;
239
240 status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
241 if (ACPI_FAILURE(status))
242 return status;
243 mutex_lock(&osc_lock);
Rafael J. Wysockiab8e8952010-08-21 01:53:27 +0200244 status = acpi_pci_query_osc(root, flags, NULL);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900245 mutex_unlock(&osc_lock);
246 return status;
247}
248
Alex Chiang76d56de2009-07-23 17:03:00 -0600249struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900250{
251 struct acpi_pci_root *root;
Taku Izumicd4faf92012-09-18 15:23:23 +0900252 struct acpi_device *device;
Bjorn Helgaasc1aec832009-06-18 14:47:02 -0600253
Taku Izumicd4faf92012-09-18 15:23:23 +0900254 if (acpi_bus_get_device(handle, &device) ||
255 acpi_match_device_ids(device, root_device_ids))
256 return NULL;
257
258 root = acpi_driver_data(device);
259
260 return root;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900261}
Alex Chiang76d56de2009-07-23 17:03:00 -0600262EXPORT_SYMBOL_GPL(acpi_pci_find_root);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900263
Alexander Chiang2f7bbce2009-06-10 19:55:20 +0000264struct acpi_handle_node {
265 struct list_head node;
266 acpi_handle handle;
267};
268
269/**
270 * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
271 * @handle: the handle in question
272 *
273 * Given an ACPI CA handle, the desired PCI device is located in the
274 * list of PCI devices.
275 *
276 * If the device is found, its reference count is increased and this
277 * function returns a pointer to its data structure. The caller must
278 * decrement the reference count by calling pci_dev_put().
279 * If no device is found, %NULL is returned.
280 */
281struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
282{
283 int dev, fn;
284 unsigned long long adr;
285 acpi_status status;
286 acpi_handle phandle;
287 struct pci_bus *pbus;
288 struct pci_dev *pdev = NULL;
289 struct acpi_handle_node *node, *tmp;
290 struct acpi_pci_root *root;
291 LIST_HEAD(device_list);
292
293 /*
294 * Walk up the ACPI CA namespace until we reach a PCI root bridge.
295 */
296 phandle = handle;
297 while (!acpi_is_root_bridge(phandle)) {
298 node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
299 if (!node)
300 goto out;
301
302 INIT_LIST_HEAD(&node->node);
303 node->handle = phandle;
304 list_add(&node->node, &device_list);
305
306 status = acpi_get_parent(phandle, &phandle);
307 if (ACPI_FAILURE(status))
308 goto out;
309 }
310
311 root = acpi_pci_find_root(phandle);
312 if (!root)
313 goto out;
314
315 pbus = root->bus;
316
317 /*
318 * Now, walk back down the PCI device tree until we return to our
319 * original handle. Assumes that everything between the PCI root
320 * bridge and the device we're looking for must be a P2P bridge.
321 */
322 list_for_each_entry(node, &device_list, node) {
323 acpi_handle hnd = node->handle;
324 status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
325 if (ACPI_FAILURE(status))
326 goto out;
327 dev = (adr >> 16) & 0xffff;
328 fn = adr & 0xffff;
329
330 pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
Troy Moure412af972009-06-25 17:05:35 -0600331 if (!pdev || hnd == handle)
Alexander Chiang2f7bbce2009-06-10 19:55:20 +0000332 break;
333
334 pbus = pdev->subordinate;
335 pci_dev_put(pdev);
Rafael J. Wysocki497fb542009-10-13 01:01:57 +0200336
337 /*
338 * This function may be called for a non-PCI device that has a
339 * PCI parent (eg. a disk under a PCI SATA controller). In that
340 * case pdev->subordinate will be NULL for the parent.
341 */
342 if (!pbus) {
343 dev_dbg(&pdev->dev, "Not a PCI-to-PCI bridge\n");
344 pdev = NULL;
345 break;
346 }
Alexander Chiang2f7bbce2009-06-10 19:55:20 +0000347 }
348out:
349 list_for_each_entry_safe(node, tmp, &device_list, node)
350 kfree(node);
351
352 return pdev;
353}
354EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
355
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900356/**
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200357 * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
358 * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
359 * @mask: Mask of _OSC bits to request control of, place to store control mask.
360 * @req: Mask of _OSC bits the control of is essential to the caller.
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900361 *
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200362 * Run _OSC query for @mask and if that is successful, compare the returned
363 * mask of control bits with @req. If all of the @req bits are set in the
364 * returned mask, run _OSC request for it.
365 *
366 * The variable at the @mask address may be modified regardless of whether or
367 * not the function returns success. On success it will contain the mask of
368 * _OSC bits the BIOS has granted control of, but its contents are meaningless
369 * on failure.
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900370 **/
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200371acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 *mask, u32 req)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900372{
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900373 struct acpi_pci_root *root;
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200374 acpi_status status;
375 u32 ctrl, capbuf[3];
376 acpi_handle tmp;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900377
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200378 if (!mask)
379 return AE_BAD_PARAMETER;
380
381 ctrl = *mask & OSC_PCI_CONTROL_MASKS;
382 if ((ctrl & req) != req)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900383 return AE_TYPE;
384
385 root = acpi_pci_find_root(handle);
386 if (!root)
387 return AE_NOT_EXIST;
388
Rafael J. Wysockib879dc42010-08-21 01:52:37 +0200389 status = acpi_get_handle(handle, "_OSC", &tmp);
390 if (ACPI_FAILURE(status))
391 return status;
392
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900393 mutex_lock(&osc_lock);
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200394
395 *mask = ctrl | root->osc_control_set;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900396 /* No need to evaluate _OSC if the control was already granted. */
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200397 if ((root->osc_control_set & ctrl) == ctrl)
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900398 goto out;
399
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200400 /* Need to check the available controls bits before requesting them. */
401 while (*mask) {
402 status = acpi_pci_query_osc(root, root->osc_support_set, mask);
403 if (ACPI_FAILURE(status))
404 goto out;
405 if (ctrl == *mask)
406 break;
407 ctrl = *mask;
408 }
Rafael J. Wysocki2b8fd912010-08-23 23:55:59 +0200409
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200410 if ((ctrl & req) != req) {
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900411 status = AE_SUPPORT;
412 goto out;
413 }
414
415 capbuf[OSC_QUERY_TYPE] = 0;
416 capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200417 capbuf[OSC_CONTROL_TYPE] = ctrl;
418 status = acpi_pci_run_osc(handle, capbuf, mask);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900419 if (ACPI_SUCCESS(status))
Rafael J. Wysocki75fb60f2010-08-23 23:53:11 +0200420 root->osc_control_set = *mask;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900421out:
422 mutex_unlock(&osc_lock);
423 return status;
424}
Kenji Kaneshige9f5404d2009-02-09 16:00:04 +0900425EXPORT_SYMBOL(acpi_pci_osc_control_set);
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900426
Rafael J. Wysocki00c43b92013-01-30 14:27:33 +0100427static int acpi_pci_root_add(struct acpi_device *device,
428 const struct acpi_device_id *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600430 unsigned long long segment, bus;
431 acpi_status status;
432 int result;
433 struct acpi_pci_root *root;
434 acpi_handle handle;
Rafael J. Wysocki47525cd2012-12-21 00:36:45 +0100435 struct acpi_pci_driver *driver;
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700436 u32 flags, base_flags;
Taku Izumi8c33f512012-10-30 15:27:13 +0900437 bool is_osc_granted = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700439 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
440 if (!root)
441 return -ENOMEM;
442
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600443 segment = 0;
444 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
445 &segment);
446 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
447 printk(KERN_ERR PREFIX "can't evaluate _SEG\n");
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700448 result = -ENODEV;
449 goto end;
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600452 /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700453 root->secondary.flags = IORESOURCE_BUS;
454 status = try_get_root_bridge_busnr(device->handle, &root->secondary);
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600455 if (ACPI_FAILURE(status)) {
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700456 /*
457 * We need both the start and end of the downstream bus range
458 * to interpret _CBA (MMCONFIG base address), so it really is
459 * supposed to be in _CRS. If we don't find it there, all we
460 * can do is assume [_BBN-0xFF] or [0-0xFF].
461 */
462 root->secondary.end = 0xFF;
463 printk(KERN_WARNING FW_BUG PREFIX
464 "no secondary bus range in _CRS\n");
Jon Masone545b552011-06-19 18:51:37 -0500465 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN,
466 NULL, &bus);
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700467 if (ACPI_SUCCESS(status))
468 root->secondary.start = bus;
469 else if (status == AE_NOT_FOUND)
470 root->secondary.start = 0;
471 else {
472 printk(KERN_ERR PREFIX "can't evaluate _BBN\n");
473 result = -ENODEV;
474 goto end;
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600475 }
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Bjorn Helgaasf5eebbe2009-06-18 14:46:52 -0600478 INIT_LIST_HEAD(&root->node);
Patrick Mochel32917e52006-05-19 16:54:39 -0400479 root->device = device;
Bjorn Helgaas07054952009-06-18 14:47:07 -0600480 root->segment = segment & 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
482 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700483 device->driver_data = root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Bjorn Helgaasd4761ba2012-10-30 15:24:50 +0900485 printk(KERN_INFO PREFIX "%s [%s] (domain %04x %pR)\n",
486 acpi_device_name(device), acpi_device_bid(device),
487 root->segment, &root->secondary);
488
489 /*
490 * PCI Routing Table
491 * -----------------
492 * Evaluate and parse _PRT, if exists.
493 */
494 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
495 if (ACPI_SUCCESS(status))
496 result = acpi_pci_irq_add_prt(device->handle, root->segment,
497 root->secondary.start);
498
Jiang Liuf4b57a32012-06-22 14:55:16 +0800499 root->mcfg_addr = acpi_pci_root_get_mcfg_addr(device->handle);
500
Andrew Patterson990a7ac2008-11-10 15:30:45 -0700501 /*
502 * All supported architectures that use ACPI have support for
503 * PCI domains, so we indicate this in _OSC support capabilities.
504 */
Andrew Patterson0ef5f8f2008-11-10 15:30:50 -0700505 flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
Kenji Kaneshige63f10f02009-02-09 15:59:29 +0900506 acpi_pci_osc_support(root, flags);
Andrew Patterson990a7ac2008-11-10 15:30:45 -0700507
Taku Izumi8c33f512012-10-30 15:27:13 +0900508 /* Indicate support for various _OSC capabilities. */
509 if (pci_ext_cfg_avail())
510 flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
511 if (pcie_aspm_support_enabled()) {
512 flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
513 OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
514 }
515 if (pci_msi_enabled())
516 flags |= OSC_MSI_SUPPORT;
517 if (flags != base_flags) {
518 status = acpi_pci_osc_support(root, flags);
519 if (ACPI_FAILURE(status)) {
520 dev_info(&device->dev, "ACPI _OSC support "
521 "notification failed, disabling PCIe ASPM\n");
522 pcie_no_aspm();
523 flags = base_flags;
524 }
525 }
526 if (!pcie_ports_disabled
527 && (flags & ACPI_PCIE_REQ_SUPPORT) == ACPI_PCIE_REQ_SUPPORT) {
528 flags = OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL
529 | OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
530 | OSC_PCI_EXPRESS_PME_CONTROL;
531
532 if (pci_aer_available()) {
533 if (aer_acpi_firmware_first())
534 dev_dbg(&device->dev,
535 "PCIe errors handled by BIOS.\n");
536 else
537 flags |= OSC_PCI_EXPRESS_AER_CONTROL;
538 }
539
540 dev_info(&device->dev,
541 "Requesting ACPI _OSC control (0x%02x)\n", flags);
542
543 status = acpi_pci_osc_control_set(device->handle, &flags,
544 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
545 if (ACPI_SUCCESS(status)) {
546 is_osc_granted = true;
547 dev_info(&device->dev,
548 "ACPI _OSC control (0x%02x) granted\n", flags);
549 } else {
550 is_osc_granted = false;
551 dev_info(&device->dev,
552 "ACPI _OSC request failed (%s), "
553 "returned control mask: 0x%02x\n",
554 acpi_format_exception(status), flags);
555 }
556 } else {
557 dev_info(&device->dev,
558 "Unable to request _OSC control "
559 "(_OSC support mask: 0x%02x)\n", flags);
560 }
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /*
563 * TBD: Need PCI interface for enumeration/configuration of roots.
564 */
565
Taku Izumi6507e6e2012-09-18 15:24:40 +0900566 mutex_lock(&acpi_pci_root_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400567 list_add_tail(&root->node, &acpi_pci_roots);
Taku Izumi6507e6e2012-09-18 15:24:40 +0900568 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
571 * Scan the Root Bridge
572 * --------------------
573 * Must do this prior to any attempt to bind the root device, as the
574 * PCI namespace does not get created until this call is made (and
575 * thus the root bridge's pci_dev does not exist).
576 */
Bjorn Helgaas57283772010-03-11 12:20:11 -0700577 root->bus = pci_acpi_scan_root(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!root->bus) {
Len Brown64684632006-06-26 23:41:38 -0400579 printk(KERN_ERR PREFIX
580 "Bus %04x:%02x not present in PCI namespace\n",
Bjorn Helgaas6ad95512010-03-11 12:20:06 -0700581 root->segment, (unsigned int)root->secondary.start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 result = -ENODEV;
Taku Izumi6507e6e2012-09-18 15:24:40 +0900583 goto out_del_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Taku Izumi8c33f512012-10-30 15:27:13 +0900586 /* ASPM setting */
587 if (is_osc_granted) {
588 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM)
589 pcie_clear_aspm(root->bus);
Rafael J. Wysockia2466702011-04-30 00:21:38 +0200590 } else {
Taku Izumi8c33f512012-10-30 15:27:13 +0900591 pr_info("ACPI _OSC control for PCIe not granted, "
592 "disabling ASPM\n");
593 pcie_no_aspm();
Rafael J. Wysocki415e12b2011-01-07 00:55:09 +0100594 }
595
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100596 pci_acpi_add_bus_pm_notifier(device, root->bus);
597 if (device->wakeup.flags.run_wake)
598 device_set_run_wake(root->bus->bridge, true);
599
Yinghai Lu62a08c52012-10-30 14:31:32 -0600600 if (system_state != SYSTEM_BOOTING)
601 pci_assign_unassigned_bus_resources(root->bus);
602
Taku Izumid0020f62012-09-18 15:21:31 +0900603 mutex_lock(&acpi_pci_root_lock);
Jiang Liuc8e9afb2012-09-18 15:20:34 +0900604 list_for_each_entry(driver, &acpi_pci_drivers, node)
605 if (driver->add)
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900606 driver->add(root);
Taku Izumid0020f62012-09-18 15:21:31 +0900607 mutex_unlock(&acpi_pci_root_lock);
Rajesh Shahc431ada2005-04-28 00:25:45 -0700608
Yinghai Lu62a08c52012-10-30 14:31:32 -0600609 /* need to after hot-added ioapic is registered */
610 if (system_state != SYSTEM_BOOTING)
611 pci_enable_bridges(root->bus);
612
Bjorn Helgaascaf420c2009-06-18 14:46:57 -0600613 pci_bus_add_devices(root->bus);
Rafael J. Wysocki00c43b92013-01-30 14:27:33 +0100614 return 1;
Rafael J. Wysocki47525cd2012-12-21 00:36:45 +0100615
616out_del_root:
617 mutex_lock(&acpi_pci_root_lock);
618 list_del(&root->node);
619 mutex_unlock(&acpi_pci_root_lock);
620
621 acpi_pci_irq_del_prt(root->segment, root->secondary.start);
622end:
623 kfree(root);
624 return result;
Rajesh Shahc431ada2005-04-28 00:25:45 -0700625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Rafael J. Wysocki00c43b92013-01-30 14:27:33 +0100627static void acpi_pci_root_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Yinghai Lu13b6a912012-10-30 14:31:48 -0600629 acpi_status status;
630 acpi_handle handle;
Bjorn Helgaascaf420c2009-06-18 14:46:57 -0600631 struct acpi_pci_root *root = acpi_driver_data(device);
Jiang Liuc8e9afb2012-09-18 15:20:34 +0900632 struct acpi_pci_driver *driver;
633
Yinghai Lu9738a1f2012-10-30 14:31:43 -0600634 pci_stop_root_bus(root->bus);
635
Taku Izumid0020f62012-09-18 15:21:31 +0900636 mutex_lock(&acpi_pci_root_lock);
Yinghai Luf426cef2012-10-30 14:31:52 -0600637 list_for_each_entry_reverse(driver, &acpi_pci_drivers, node)
Jiang Liuc8e9afb2012-09-18 15:20:34 +0900638 if (driver->remove)
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900639 driver->remove(root);
Yinghai Lu9738a1f2012-10-30 14:31:43 -0600640 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100642 device_set_run_wake(root->bus->bridge, false);
643 pci_acpi_remove_bus_pm_notifier(device);
644
Yinghai Lu13b6a912012-10-30 14:31:48 -0600645 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
646 if (ACPI_SUCCESS(status))
Bjorn Helgaas79c44122012-10-30 15:24:06 +0900647 acpi_pci_irq_del_prt(root->segment, root->secondary.start);
Yinghai Lu13b6a912012-10-30 14:31:48 -0600648
Yinghai Lu9738a1f2012-10-30 14:31:43 -0600649 pci_remove_root_bus(root->bus);
650
651 mutex_lock(&acpi_pci_root_lock);
Taku Izumi6507e6e2012-09-18 15:24:40 +0900652 list_del(&root->node);
653 mutex_unlock(&acpi_pci_root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 kfree(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Rafael J. Wysocki00c43b92013-01-30 14:27:33 +0100657void __init acpi_pci_root_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Rafael J. Wysockid3072e62011-01-16 20:44:22 +0100659 acpi_hest_init();
660
Rafael J. Wysocki00c43b92013-01-30 14:27:33 +0100661 if (!acpi_pci_disabled) {
662 pci_acpi_crs_quirks();
663 acpi_scan_add_handler(&pci_root_handler);
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}