Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ACPI PCI HotPlug glue functions to ACPI CA subsystem |
| 3 | * |
| 4 | * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com) |
| 5 | * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com) |
| 6 | * Copyright (C) 2002,2003 NEC Corporation |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 7 | * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com) |
| 8 | * Copyright (C) 2003-2005 Hewlett Packard |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 9 | * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com) |
| 10 | * Copyright (C) 2005 Intel Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * All rights reserved. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or (at |
| 17 | * your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, but |
| 20 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 22 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 23 | * details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 28 | * |
Kristen Carlson Accardi | 998be20 | 2006-07-26 10:52:33 -0700 | [diff] [blame] | 29 | * Send feedback to <kristen.c.accardi@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * |
| 31 | */ |
| 32 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 33 | /* |
| 34 | * Lifetime rules for pci_dev: |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 35 | * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot() |
| 36 | * when the bridge is scanned and it loses a refcount when the bridge |
| 37 | * is removed. |
Alex Chiang | 5d4a4b2 | 2009-03-30 10:50:14 -0600 | [diff] [blame] | 38 | * - When a P2P bridge is present, we elevate the refcount on the subordinate |
| 39 | * bus. It loses the refcount when the the driver unloads. |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 40 | */ |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/init.h> |
| 43 | #include <linux/module.h> |
| 44 | |
| 45 | #include <linux/kernel.h> |
| 46 | #include <linux/pci.h> |
Greg Kroah-Hartman | 7a54f25 | 2006-10-13 20:05:19 -0700 | [diff] [blame] | 47 | #include <linux/pci_hotplug.h> |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 48 | #include <linux/pci-acpi.h> |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 49 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 50 | #include <linux/slab.h> |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 51 | #include <linux/acpi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | #include "../pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include "acpiphp.h" |
| 55 | |
| 56 | static LIST_HEAD(bridge_list); |
| 57 | |
| 58 | #define MY_NAME "acpiphp_glue" |
| 59 | |
| 60 | static void handle_hotplug_event_bridge (acpi_handle, u32, void *); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 61 | static void acpiphp_sanitize_bus(struct pci_bus *bus); |
Bjorn Helgaas | fca6825 | 2009-09-14 16:35:10 -0600 | [diff] [blame] | 62 | static void acpiphp_set_hpp_values(struct pci_bus *bus); |
Len Brown | 2b85e13 | 2006-06-27 01:50:14 -0400 | [diff] [blame] | 63 | static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 64 | |
MUNEDA Takahiro | 5a340ed | 2007-11-09 19:07:02 +0900 | [diff] [blame] | 65 | /* callback routine to check for the existence of a pci dock device */ |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 66 | static acpi_status |
| 67 | is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 68 | { |
| 69 | int *count = (int *)context; |
| 70 | |
| 71 | if (is_dock_device(handle)) { |
| 72 | (*count)++; |
| 73 | return AE_CTRL_TERMINATE; |
| 74 | } else { |
| 75 | return AE_OK; |
| 76 | } |
| 77 | } |
| 78 | |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 79 | /* |
| 80 | * the _DCK method can do funny things... and sometimes not |
| 81 | * hah-hah funny. |
| 82 | * |
| 83 | * TBD - figure out a way to only call fixups for |
| 84 | * systems that require them. |
| 85 | */ |
| 86 | static int post_dock_fixups(struct notifier_block *nb, unsigned long val, |
| 87 | void *v) |
| 88 | { |
| 89 | struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb); |
| 90 | struct pci_bus *bus = func->slot->bridge->pci_bus; |
| 91 | u32 buses; |
| 92 | |
| 93 | if (!bus->self) |
| 94 | return NOTIFY_OK; |
| 95 | |
| 96 | /* fixup bad _DCK function that rewrites |
| 97 | * secondary bridge on slot |
| 98 | */ |
| 99 | pci_read_config_dword(bus->self, |
| 100 | PCI_PRIMARY_BUS, |
| 101 | &buses); |
| 102 | |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 103 | if (((buses >> 8) & 0xff) != bus->busn_res.start) { |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 104 | buses = (buses & 0xff000000) |
Alex Chiang | 2a9d352 | 2008-12-11 11:17:55 -0700 | [diff] [blame] | 105 | | ((unsigned int)(bus->primary) << 0) |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 106 | | ((unsigned int)(bus->busn_res.start) << 8) |
| 107 | | ((unsigned int)(bus->busn_res.end) << 16); |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 108 | pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses); |
| 109 | } |
| 110 | return NOTIFY_OK; |
| 111 | } |
| 112 | |
| 113 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 114 | static const struct acpi_dock_ops acpiphp_dock_ops = { |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 115 | .handler = handle_hotplug_event_func, |
| 116 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Jiang Liu | 5ba113f | 2012-08-22 23:16:45 +0800 | [diff] [blame] | 118 | /* Check whether the PCI device is managed by native PCIe hotplug driver */ |
| 119 | static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev) |
| 120 | { |
| 121 | u32 reg32; |
| 122 | acpi_handle tmp; |
| 123 | struct acpi_pci_root *root; |
| 124 | |
| 125 | /* Check whether the PCIe port supports native PCIe hotplug */ |
| 126 | if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32)) |
| 127 | return false; |
| 128 | if (!(reg32 & PCI_EXP_SLTCAP_HPC)) |
| 129 | return false; |
| 130 | |
| 131 | /* |
| 132 | * Check whether native PCIe hotplug has been enabled for |
| 133 | * this PCIe hierarchy. |
| 134 | */ |
| 135 | tmp = acpi_find_root_bridge_handle(pdev); |
| 136 | if (!tmp) |
| 137 | return false; |
| 138 | root = acpi_pci_find_root(tmp); |
| 139 | if (!root) |
| 140 | return false; |
| 141 | if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL)) |
| 142 | return false; |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* callback routine to register each ACPI PCI slot object */ |
| 148 | static acpi_status |
| 149 | register_slot(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 150 | { |
| 151 | struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context; |
| 152 | struct acpiphp_slot *slot; |
| 153 | struct acpiphp_func *newfunc; |
| 154 | acpi_handle tmp; |
| 155 | acpi_status status = AE_OK; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 156 | unsigned long long adr, sun; |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 157 | int device, function, retval, found = 0; |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 158 | struct pci_bus *pbus = bridge->pci_bus; |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 159 | struct pci_dev *pdev; |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 160 | u32 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 162 | if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return AE_OK; |
| 164 | |
Bjorn Helgaas | dfb117b | 2012-06-20 16:18:29 -0600 | [diff] [blame] | 165 | status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr); |
| 166 | if (ACPI_FAILURE(status)) { |
| 167 | warn("can't evaluate _ADR (%#x)\n", status); |
| 168 | return AE_OK; |
| 169 | } |
| 170 | |
| 171 | device = (adr >> 16) & 0xffff; |
| 172 | function = adr & 0xffff; |
| 173 | |
Rafael J. Wysocki | 619a518 | 2011-12-13 00:02:28 +0100 | [diff] [blame] | 174 | pdev = pbus->self; |
Jiang Liu | 5ba113f | 2012-08-22 23:16:45 +0800 | [diff] [blame] | 175 | if (pdev && device_is_managed_by_native_pciehp(pdev)) |
| 176 | return AE_OK; |
Rafael J. Wysocki | 619a518 | 2011-12-13 00:02:28 +0100 | [diff] [blame] | 177 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 178 | newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | if (!newfunc) |
| 180 | return AE_NO_MEMORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | INIT_LIST_HEAD(&newfunc->sibling); |
| 183 | newfunc->handle = handle; |
| 184 | newfunc->function = function; |
Matthew Garrett | 56ee325 | 2008-11-25 21:48:14 +0000 | [diff] [blame] | 185 | |
| 186 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp))) |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 187 | newfunc->flags = FUNC_HAS_EJ0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp))) |
| 190 | newfunc->flags |= FUNC_HAS_STA; |
| 191 | |
| 192 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp))) |
| 193 | newfunc->flags |= FUNC_HAS_PS0; |
| 194 | |
| 195 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp))) |
| 196 | newfunc->flags |= FUNC_HAS_PS3; |
| 197 | |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 198 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 199 | newfunc->flags |= FUNC_HAS_DCK; |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun); |
Kristen Accardi | 95b38b3 | 2006-06-28 03:09:54 -0400 | [diff] [blame] | 202 | if (ACPI_FAILURE(status)) { |
| 203 | /* |
| 204 | * use the count of the number of slots we've found |
| 205 | * for the number of the slot |
| 206 | */ |
| 207 | sun = bridge->nr_slots+1; |
| 208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | /* search for objects that share the same slot */ |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 211 | list_for_each_entry(slot, &bridge->slots, node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (slot->device == device) { |
| 213 | if (slot->sun != sun) |
| 214 | warn("sibling found, but _SUN doesn't match!\n"); |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 215 | found = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 219 | if (!found) { |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 220 | slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (!slot) { |
| 222 | kfree(newfunc); |
| 223 | return AE_NO_MEMORY; |
| 224 | } |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | slot->bridge = bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | slot->device = device; |
| 228 | slot->sun = sun; |
| 229 | INIT_LIST_HEAD(&slot->funcs); |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 230 | mutex_init(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 232 | list_add_tail(&slot->node, &bridge->slots); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | bridge->nr_slots++; |
| 234 | |
Justin Chen | b6adc19 | 2008-12-11 11:16:44 -0700 | [diff] [blame] | 235 | dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n", |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 236 | slot->sun, pci_domain_nr(pbus), pbus->number, device); |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 237 | retval = acpiphp_register_hotplug_slot(slot); |
| 238 | if (retval) { |
Alex Chiang | f46753c | 2008-06-10 15:28:50 -0600 | [diff] [blame] | 239 | if (retval == -EBUSY) |
Justin Chen | b6adc19 | 2008-12-11 11:16:44 -0700 | [diff] [blame] | 240 | warn("Slot %llu already registered by another " |
Alex Chiang | f46753c | 2008-06-10 15:28:50 -0600 | [diff] [blame] | 241 | "hotplug driver\n", slot->sun); |
| 242 | else |
| 243 | warn("acpiphp_register_hotplug_slot failed " |
| 244 | "(err code = 0x%x)\n", retval); |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 245 | goto err_exit; |
| 246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | newfunc->slot = slot; |
| 250 | list_add_tail(&newfunc->sibling, &slot->funcs); |
| 251 | |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 252 | if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function), |
| 253 | &val, 60*1000)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 256 | if (is_dock_device(handle)) { |
| 257 | /* we don't want to call this device's _EJ0 |
| 258 | * because we want the dock notify handler |
| 259 | * to call it after it calls _DCK |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 260 | */ |
| 261 | newfunc->flags &= ~FUNC_HAS_EJ0; |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 262 | if (register_hotplug_dock_device(handle, |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 263 | &acpiphp_dock_ops, newfunc)) |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 264 | dbg("failed to register dock device\n"); |
| 265 | |
| 266 | /* we need to be notified when dock events happen |
| 267 | * outside of the hotplug operation, since we may |
| 268 | * need to do fixups before we can hotplug. |
| 269 | */ |
| 270 | newfunc->nb.notifier_call = post_dock_fixups; |
| 271 | if (register_dock_notifier(&newfunc->nb)) |
| 272 | dbg("failed to register a dock notifier"); |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | /* install notify handler */ |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 276 | if (!(newfunc->flags & FUNC_HAS_DCK)) { |
| 277 | status = acpi_install_notify_handler(handle, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | ACPI_SYSTEM_NOTIFY, |
| 279 | handle_hotplug_event_func, |
| 280 | newfunc); |
| 281 | |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 282 | if (ACPI_FAILURE(status)) |
| 283 | err("failed to register interrupt notify handler\n"); |
| 284 | } else |
| 285 | status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 287 | return status; |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 288 | |
| 289 | err_exit: |
| 290 | bridge->nr_slots--; |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 291 | list_del(&slot->node); |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 292 | kfree(slot); |
| 293 | kfree(newfunc); |
| 294 | |
| 295 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
| 299 | /* see if it's worth looking at this bridge */ |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 300 | static int detect_ejectable_slots(acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Alex Chiang | 7f53866 | 2009-09-10 12:34:09 -0600 | [diff] [blame] | 302 | int found = acpi_pci_detect_ejectable(handle); |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 303 | if (!found) { |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 304 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 305 | is_pci_dock_device, NULL, (void *)&found, NULL); |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 306 | } |
| 307 | return found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */ |
| 311 | static void init_bridge_misc(struct acpiphp_bridge *bridge) |
| 312 | { |
| 313 | acpi_status status; |
| 314 | |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 315 | /* must be added to the list prior to calling register_slot */ |
| 316 | list_add(&bridge->list, &bridge_list); |
| 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | /* register all slot objects under this bridge */ |
| 319 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 320 | register_slot, NULL, bridge, NULL); |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 321 | if (ACPI_FAILURE(status)) { |
| 322 | list_del(&bridge->list); |
| 323 | return; |
| 324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 326 | /* install notify handler for P2P bridges */ |
| 327 | if (!pci_is_root_bus(bridge->pci_bus)) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 328 | if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) { |
| 329 | status = acpi_remove_notify_handler(bridge->func->handle, |
| 330 | ACPI_SYSTEM_NOTIFY, |
| 331 | handle_hotplug_event_func); |
| 332 | if (ACPI_FAILURE(status)) |
| 333 | err("failed to remove notify handler\n"); |
| 334 | } |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 335 | status = acpi_install_notify_handler(bridge->handle, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | ACPI_SYSTEM_NOTIFY, |
| 337 | handle_hotplug_event_bridge, |
| 338 | bridge); |
| 339 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 340 | if (ACPI_FAILURE(status)) { |
| 341 | err("failed to register interrupt notify handler\n"); |
| 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 347 | /* find acpiphp_func from acpiphp_bridge */ |
| 348 | static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle) |
| 349 | { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 350 | struct acpiphp_bridge *bridge; |
| 351 | struct acpiphp_slot *slot; |
| 352 | struct acpiphp_func *func; |
| 353 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 354 | list_for_each_entry(bridge, &bridge_list, list) { |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 355 | list_for_each_entry(slot, &bridge->slots, node) { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 356 | list_for_each_entry(func, &slot->funcs, sibling) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 357 | if (func->handle == handle) |
| 358 | return func; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 367 | static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle) |
| 368 | { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 369 | struct acpiphp_bridge *bridge; |
| 370 | |
| 371 | list_for_each_entry(bridge, &bridge_list, list) |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 372 | if (bridge->handle == handle) |
| 373 | return bridge; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 374 | |
| 375 | return NULL; |
| 376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 378 | static void cleanup_bridge(struct acpiphp_bridge *bridge) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 380 | struct acpiphp_slot *slot, *next; |
| 381 | struct acpiphp_func *func, *tmp; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 382 | acpi_status status; |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 383 | acpi_handle handle = bridge->handle; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 384 | |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 385 | if (!pci_is_root_bus(bridge->pci_bus)) { |
Yinghai Lu | 668192b | 2013-01-21 13:20:48 -0800 | [diff] [blame] | 386 | status = acpi_remove_notify_handler(handle, |
| 387 | ACPI_SYSTEM_NOTIFY, |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 388 | handle_hotplug_event_bridge); |
Yinghai Lu | 668192b | 2013-01-21 13:20:48 -0800 | [diff] [blame] | 389 | if (ACPI_FAILURE(status)) |
| 390 | err("failed to remove notify handler\n"); |
| 391 | } |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 392 | |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 393 | if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 394 | status = acpi_install_notify_handler(bridge->func->handle, |
| 395 | ACPI_SYSTEM_NOTIFY, |
| 396 | handle_hotplug_event_func, |
| 397 | bridge->func); |
| 398 | if (ACPI_FAILURE(status)) |
| 399 | err("failed to install interrupt notify handler\n"); |
| 400 | } |
| 401 | |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 402 | list_for_each_entry_safe(slot, next, &bridge->slots, node) { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 403 | list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) { |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 404 | if (is_dock_device(func->handle)) { |
| 405 | unregister_hotplug_dock_device(func->handle); |
| 406 | unregister_dock_notifier(&func->nb); |
| 407 | } |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 408 | if (!(func->flags & FUNC_HAS_DCK)) { |
| 409 | status = acpi_remove_notify_handler(func->handle, |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 410 | ACPI_SYSTEM_NOTIFY, |
| 411 | handle_hotplug_event_func); |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 412 | if (ACPI_FAILURE(status)) |
| 413 | err("failed to remove notify handler\n"); |
| 414 | } |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 415 | list_del(&func->sibling); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 416 | kfree(func); |
| 417 | } |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 418 | acpiphp_unregister_hotplug_slot(slot); |
| 419 | list_del(&slot->funcs); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 420 | kfree(slot); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 423 | put_device(&bridge->pci_bus->dev); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 424 | pci_dev_put(bridge->pci_dev); |
| 425 | list_del(&bridge->list); |
| 426 | kfree(bridge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | static int power_on_slot(struct acpiphp_slot *slot) |
| 430 | { |
| 431 | acpi_status status; |
| 432 | struct acpiphp_func *func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | int retval = 0; |
| 434 | |
| 435 | /* if already enabled, just skip */ |
| 436 | if (slot->flags & SLOT_POWEREDON) |
| 437 | goto err_exit; |
| 438 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 439 | list_for_each_entry(func, &slot->funcs, sibling) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (func->flags & FUNC_HAS_PS0) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 441 | dbg("%s: executing _PS0\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL); |
| 443 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 444 | warn("%s: _PS0 failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | retval = -1; |
| 446 | goto err_exit; |
| 447 | } else |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /* TBD: evaluate _STA to check if the slot is enabled */ |
| 453 | |
| 454 | slot->flags |= SLOT_POWEREDON; |
| 455 | |
| 456 | err_exit: |
| 457 | return retval; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | static int power_off_slot(struct acpiphp_slot *slot) |
| 462 | { |
| 463 | acpi_status status; |
| 464 | struct acpiphp_func *func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
| 466 | int retval = 0; |
| 467 | |
| 468 | /* if already disabled, just skip */ |
| 469 | if ((slot->flags & SLOT_POWEREDON) == 0) |
| 470 | goto err_exit; |
| 471 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 472 | list_for_each_entry(func, &slot->funcs, sibling) { |
Rajesh Shah | 2f523b1 | 2005-04-28 00:25:55 -0700 | [diff] [blame] | 473 | if (func->flags & FUNC_HAS_PS3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL); |
| 475 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 476 | warn("%s: _PS3 failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | retval = -1; |
| 478 | goto err_exit; |
| 479 | } else |
| 480 | break; |
| 481 | } |
| 482 | } |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /* TBD: evaluate _STA to check if the slot is disabled */ |
| 485 | |
| 486 | slot->flags &= (~SLOT_POWEREDON); |
| 487 | |
| 488 | err_exit: |
| 489 | return retval; |
| 490 | } |
| 491 | |
| 492 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 493 | |
| 494 | /** |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 495 | * acpiphp_max_busnr - return the highest reserved bus number under the given bus. |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 496 | * @bus: bus to start search with |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 497 | */ |
| 498 | static unsigned char acpiphp_max_busnr(struct pci_bus *bus) |
| 499 | { |
| 500 | struct list_head *tmp; |
| 501 | unsigned char max, n; |
| 502 | |
| 503 | /* |
| 504 | * pci_bus_max_busnr will return the highest |
| 505 | * reserved busnr for all these children. |
| 506 | * that is equivalent to the bus->subordinate |
| 507 | * value. We don't want to use the parent's |
| 508 | * bus->subordinate value because it could have |
| 509 | * padding in it. |
| 510 | */ |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 511 | max = bus->busn_res.start; |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 512 | |
| 513 | list_for_each(tmp, &bus->children) { |
| 514 | n = pci_bus_max_busnr(pci_bus_b(tmp)); |
| 515 | if (n > max) |
| 516 | max = n; |
| 517 | } |
| 518 | return max; |
| 519 | } |
| 520 | |
| 521 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 522 | /** |
| 523 | * acpiphp_bus_add - add a new bus to acpi subsystem |
| 524 | * @func: acpiphp_func of the bridge |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 525 | */ |
| 526 | static int acpiphp_bus_add(struct acpiphp_func *func) |
| 527 | { |
Rafael J. Wysocki | 636458d | 2012-12-21 00:36:47 +0100 | [diff] [blame] | 528 | struct acpi_device *device; |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 529 | int ret_val; |
| 530 | |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 531 | if (!acpi_bus_get_device(func->handle, &device)) { |
| 532 | dbg("bus exists... trim\n"); |
| 533 | /* this shouldn't be in here, so remove |
| 534 | * the bus then re-add it... |
| 535 | */ |
Rafael J. Wysocki | bfee26d | 2013-01-26 00:27:44 +0100 | [diff] [blame] | 536 | acpi_bus_trim(device); |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 539 | ret_val = acpi_bus_scan(func->handle); |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 540 | if (!ret_val) |
| 541 | ret_val = acpi_bus_get_device(func->handle, &device); |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 542 | |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 543 | if (ret_val) |
| 544 | dbg("error adding bus, %x\n", -ret_val); |
| 545 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 546 | return ret_val; |
| 547 | } |
| 548 | |
| 549 | |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 550 | /** |
| 551 | * acpiphp_bus_trim - trim a bus from acpi subsystem |
| 552 | * @handle: handle to acpi namespace |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 553 | */ |
Adrian Bunk | 6d47a5e | 2006-08-14 23:07:38 -0700 | [diff] [blame] | 554 | static int acpiphp_bus_trim(acpi_handle handle) |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 555 | { |
| 556 | struct acpi_device *device; |
| 557 | int retval; |
| 558 | |
| 559 | retval = acpi_bus_get_device(handle, &device); |
| 560 | if (retval) { |
| 561 | dbg("acpi_device not found\n"); |
| 562 | return retval; |
| 563 | } |
| 564 | |
Rafael J. Wysocki | bfee26d | 2013-01-26 00:27:44 +0100 | [diff] [blame] | 565 | acpi_bus_trim(device); |
| 566 | return 0; |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 567 | } |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 568 | |
Shaohua Li | d060705 | 2010-02-25 10:59:34 +0800 | [diff] [blame] | 569 | static void acpiphp_set_acpi_region(struct acpiphp_slot *slot) |
| 570 | { |
| 571 | struct acpiphp_func *func; |
| 572 | union acpi_object params[2]; |
| 573 | struct acpi_object_list arg_list; |
| 574 | |
| 575 | list_for_each_entry(func, &slot->funcs, sibling) { |
| 576 | arg_list.count = 2; |
| 577 | arg_list.pointer = params; |
| 578 | params[0].type = ACPI_TYPE_INTEGER; |
| 579 | params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG; |
| 580 | params[1].type = ACPI_TYPE_INTEGER; |
| 581 | params[1].integer.value = 1; |
| 582 | /* _REG is optional, we don't care about if there is failure */ |
| 583 | acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL); |
| 584 | } |
| 585 | } |
| 586 | |
Yinghai Lu | 1f96a96 | 2013-01-21 13:20:42 -0800 | [diff] [blame] | 587 | static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev) |
| 588 | { |
| 589 | struct acpiphp_func *func; |
| 590 | |
| 591 | if (!dev->subordinate) |
| 592 | return; |
| 593 | |
| 594 | /* quirk, or pcie could set it already */ |
| 595 | if (dev->is_hotplug_bridge) |
| 596 | return; |
| 597 | |
| 598 | if (PCI_SLOT(dev->devfn) != slot->device) |
| 599 | return; |
| 600 | |
| 601 | list_for_each_entry(func, &slot->funcs, sibling) { |
| 602 | if (PCI_FUNC(dev->devfn) == func->function) { |
| 603 | /* check if this bridge has ejectable slots */ |
| 604 | if ((detect_ejectable_slots(func->handle) > 0)) |
| 605 | dev->is_hotplug_bridge = 1; |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | } |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /** |
| 612 | * enable_device - enable, configure a slot |
| 613 | * @slot: slot to be enabled |
| 614 | * |
| 615 | * This function should be called per *physical slot*, |
| 616 | * not per each slot object in ACPI namespace. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | */ |
Sam Ravnborg | 0ab2b57 | 2008-02-17 10:45:28 +0100 | [diff] [blame] | 618 | static int __ref enable_device(struct acpiphp_slot *slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | struct pci_dev *dev; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 621 | struct pci_bus *bus = slot->bridge->pci_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | struct acpiphp_func *func; |
| 623 | int retval = 0; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 624 | int num, max, pass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | if (slot->flags & SLOT_ENABLED) |
| 627 | goto err_exit; |
| 628 | |
Jiang Liu | 2ca344e | 2013-01-31 00:10:09 +0800 | [diff] [blame] | 629 | list_for_each_entry(func, &slot->funcs, sibling) |
| 630 | acpiphp_bus_add(func); |
| 631 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 632 | num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0)); |
| 633 | if (num == 0) { |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 634 | /* Maybe only part of funcs are added. */ |
| 635 | dbg("No new device found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | goto err_exit; |
| 637 | } |
| 638 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 639 | max = acpiphp_max_busnr(bus); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 640 | for (pass = 0; pass < 2; pass++) { |
| 641 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 642 | if (PCI_SLOT(dev->devfn) != slot->device) |
| 643 | continue; |
| 644 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 645 | dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 646 | max = pci_scan_bridge(bus, dev, max, pass); |
Yinghai Lu | 1f96a96 | 2013-01-21 13:20:42 -0800 | [diff] [blame] | 647 | if (pass && dev->subordinate) { |
| 648 | check_hotplug_bridge(slot, dev); |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 649 | pci_bus_size_bridges(dev->subordinate); |
Yinghai Lu | 1f96a96 | 2013-01-21 13:20:42 -0800 | [diff] [blame] | 650 | } |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 651 | } |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 652 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 655 | pci_bus_assign_resources(bus); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 656 | acpiphp_sanitize_bus(bus); |
Bjorn Helgaas | fca6825 | 2009-09-14 16:35:10 -0600 | [diff] [blame] | 657 | acpiphp_set_hpp_values(bus); |
Shaohua Li | d060705 | 2010-02-25 10:59:34 +0800 | [diff] [blame] | 658 | acpiphp_set_acpi_region(slot); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 659 | pci_enable_bridges(bus); |
Ian Campbell | 69643e4 | 2011-05-11 17:00:32 +0100 | [diff] [blame] | 660 | |
| 661 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 662 | /* Assume that newly added devices are powered on already. */ |
| 663 | if (!dev->is_added) |
| 664 | dev->current_state = PCI_D0; |
| 665 | } |
| 666 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 667 | pci_bus_add_devices(bus); |
| 668 | |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 669 | slot->flags |= SLOT_ENABLED; |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 670 | list_for_each_entry(func, &slot->funcs, sibling) { |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 671 | dev = pci_get_slot(bus, PCI_DEVFN(slot->device, |
| 672 | func->function)); |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 673 | if (!dev) { |
| 674 | /* Do not set SLOT_ENABLED flag if some funcs |
| 675 | are not added. */ |
| 676 | slot->flags &= (~SLOT_ENABLED); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 677 | continue; |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | err_exit: |
| 683 | return retval; |
| 684 | } |
| 685 | |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 686 | /* return first device in slot, acquiring a reference on it */ |
| 687 | static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot) |
| 688 | { |
| 689 | struct pci_bus *bus = slot->bridge->pci_bus; |
| 690 | struct pci_dev *dev; |
| 691 | struct pci_dev *ret = NULL; |
| 692 | |
| 693 | down_read(&pci_bus_sem); |
| 694 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 695 | if (PCI_SLOT(dev->devfn) == slot->device) { |
| 696 | ret = pci_dev_get(dev); |
| 697 | break; |
| 698 | } |
| 699 | up_read(&pci_bus_sem); |
| 700 | |
| 701 | return ret; |
| 702 | } |
| 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | /** |
| 705 | * disable_device - disable a slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 706 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | */ |
| 708 | static int disable_device(struct acpiphp_slot *slot) |
| 709 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | struct acpiphp_func *func; |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 711 | struct pci_dev *pdev; |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 712 | |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 713 | /* |
| 714 | * enable_device() enumerates all functions in this device via |
| 715 | * pci_scan_slot(), whether they have associated ACPI hotplug |
| 716 | * methods (_EJ0, etc.) or not. Therefore, we remove all functions |
| 717 | * here. |
| 718 | */ |
| 719 | while ((pdev = dev_in_slot(slot))) { |
Bjorn Helgaas | 34e5484 | 2012-08-17 10:03:47 -0600 | [diff] [blame] | 720 | pci_stop_and_remove_bus_device(pdev); |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 721 | pci_dev_put(pdev); |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 722 | } |
Satoru Takeuchi | 0dad351 | 2006-09-12 10:17:46 -0700 | [diff] [blame] | 723 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 724 | list_for_each_entry(func, &slot->funcs, sibling) { |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 725 | acpiphp_bus_trim(func->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | slot->flags &= (~SLOT_ENABLED); |
| 729 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 730 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | |
| 734 | /** |
| 735 | * get_slot_status - get ACPI slot status |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 736 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 738 | * If a slot has _STA for each function and if any one of them |
| 739 | * returned non-zero status, return it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 741 | * If a slot doesn't have _STA and if any one of its functions' |
| 742 | * configuration space is configured, return 0x0f as a _STA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 744 | * Otherwise return 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | */ |
| 746 | static unsigned int get_slot_status(struct acpiphp_slot *slot) |
| 747 | { |
| 748 | acpi_status status; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 749 | unsigned long long sta = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | u32 dvid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | struct acpiphp_func *func; |
| 752 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 753 | list_for_each_entry(func, &slot->funcs, sibling) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | if (func->flags & FUNC_HAS_STA) { |
| 755 | status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta); |
| 756 | if (ACPI_SUCCESS(status) && sta) |
| 757 | break; |
| 758 | } else { |
| 759 | pci_bus_read_config_dword(slot->bridge->pci_bus, |
| 760 | PCI_DEVFN(slot->device, |
| 761 | func->function), |
| 762 | PCI_VENDOR_ID, &dvid); |
| 763 | if (dvid != 0xffffffff) { |
| 764 | sta = ACPI_STA_ALL; |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | return (unsigned int)sta; |
| 771 | } |
| 772 | |
| 773 | /** |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 774 | * acpiphp_eject_slot - physically eject the slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 775 | * @slot: ACPI PHP slot |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 776 | */ |
Gary Hade | bfceafc | 2007-07-05 11:10:46 -0700 | [diff] [blame] | 777 | int acpiphp_eject_slot(struct acpiphp_slot *slot) |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 778 | { |
| 779 | acpi_status status; |
| 780 | struct acpiphp_func *func; |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 781 | struct acpi_object_list arg_list; |
| 782 | union acpi_object arg; |
| 783 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 784 | list_for_each_entry(func, &slot->funcs, sibling) { |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 785 | /* We don't want to call _EJ0 on non-existing functions. */ |
| 786 | if ((func->flags & FUNC_HAS_EJ0)) { |
| 787 | /* _EJ0 method take one argument */ |
| 788 | arg_list.count = 1; |
| 789 | arg_list.pointer = &arg; |
| 790 | arg.type = ACPI_TYPE_INTEGER; |
| 791 | arg.integer.value = 1; |
| 792 | |
| 793 | status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL); |
| 794 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 795 | warn("%s: _EJ0 failed\n", __func__); |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 796 | return -1; |
| 797 | } else |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | * acpiphp_check_bridge - re-enumerate devices |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 806 | * @bridge: where to begin re-enumeration |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | * |
| 808 | * Iterate over all slots under this bridge and make sure that if a |
| 809 | * card is present they are enabled, and if not they are disabled. |
| 810 | */ |
| 811 | static int acpiphp_check_bridge(struct acpiphp_bridge *bridge) |
| 812 | { |
| 813 | struct acpiphp_slot *slot; |
| 814 | int retval = 0; |
| 815 | int enabled, disabled; |
| 816 | |
| 817 | enabled = disabled = 0; |
| 818 | |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 819 | list_for_each_entry(slot, &bridge->slots, node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | unsigned int status = get_slot_status(slot); |
| 821 | if (slot->flags & SLOT_ENABLED) { |
| 822 | if (status == ACPI_STA_ALL) |
| 823 | continue; |
| 824 | retval = acpiphp_disable_slot(slot); |
| 825 | if (retval) { |
| 826 | err("Error occurred in disabling\n"); |
| 827 | goto err_exit; |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 828 | } else { |
| 829 | acpiphp_eject_slot(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | disabled++; |
| 832 | } else { |
| 833 | if (status != ACPI_STA_ALL) |
| 834 | continue; |
| 835 | retval = acpiphp_enable_slot(slot); |
| 836 | if (retval) { |
| 837 | err("Error occurred in enabling\n"); |
| 838 | goto err_exit; |
| 839 | } |
| 840 | enabled++; |
| 841 | } |
| 842 | } |
| 843 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 844 | dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
| 846 | err_exit: |
| 847 | return retval; |
| 848 | } |
| 849 | |
Bjorn Helgaas | fca6825 | 2009-09-14 16:35:10 -0600 | [diff] [blame] | 850 | static void acpiphp_set_hpp_values(struct pci_bus *bus) |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 851 | { |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 852 | struct pci_dev *dev; |
| 853 | |
Bjorn Helgaas | e81995b | 2009-09-14 16:35:35 -0600 | [diff] [blame] | 854 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 855 | pci_configure_slot(dev); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* |
| 859 | * Remove devices for which we could not assign resources, call |
| 860 | * arch specific code to fix-up the bus |
| 861 | */ |
| 862 | static void acpiphp_sanitize_bus(struct pci_bus *bus) |
| 863 | { |
Yijing Wang | d65eba6 | 2013-04-12 05:44:17 +0000 | [diff] [blame] | 864 | struct pci_dev *dev, *tmp; |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 865 | int i; |
| 866 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM; |
| 867 | |
Yijing Wang | d65eba6 | 2013-04-12 05:44:17 +0000 | [diff] [blame] | 868 | list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) { |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 869 | for (i=0; i<PCI_BRIDGE_RESOURCES; i++) { |
| 870 | struct resource *res = &dev->resource[i]; |
| 871 | if ((res->flags & type_mask) && !res->start && |
| 872 | res->end) { |
| 873 | /* Could not assign a required resources |
| 874 | * for this device, remove it */ |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 875 | pci_stop_and_remove_bus_device(dev); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | /* |
| 883 | * ACPI event handlers |
| 884 | */ |
| 885 | |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 886 | static acpi_status |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 887 | check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 888 | { |
| 889 | struct acpiphp_bridge *bridge; |
| 890 | char objname[64]; |
| 891 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 892 | .pointer = objname }; |
| 893 | |
| 894 | bridge = acpiphp_handle_to_bridge(handle); |
| 895 | if (bridge) { |
| 896 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 897 | dbg("%s: re-enumerating slots under %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 898 | __func__, objname); |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 899 | acpiphp_check_bridge(bridge); |
| 900 | } |
| 901 | return AE_OK ; |
| 902 | } |
| 903 | |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 904 | static void _handle_hotplug_event_bridge(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
| 906 | struct acpiphp_bridge *bridge; |
| 907 | char objname[64]; |
| 908 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 909 | .pointer = objname }; |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 910 | struct acpi_hp_work *hp_work; |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 911 | acpi_handle handle; |
| 912 | u32 type; |
| 913 | |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 914 | hp_work = container_of(work, struct acpi_hp_work, work); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 915 | handle = hp_work->handle; |
| 916 | type = hp_work->type; |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 917 | bridge = (struct acpiphp_bridge *)hp_work->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 919 | acpi_scan_lock_acquire(); |
| 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 922 | |
| 923 | switch (type) { |
| 924 | case ACPI_NOTIFY_BUS_CHECK: |
| 925 | /* bus re-enumerate */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 926 | dbg("%s: Bus check notify on %s\n", __func__, objname); |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 927 | dbg("%s: re-enumerating slots under %s\n", __func__, objname); |
| 928 | acpiphp_check_bridge(bridge); |
| 929 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, |
| 930 | ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | break; |
| 932 | |
| 933 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 934 | /* device check */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 935 | dbg("%s: Device check notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | acpiphp_check_bridge(bridge); |
| 937 | break; |
| 938 | |
| 939 | case ACPI_NOTIFY_DEVICE_WAKE: |
| 940 | /* wake event */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 941 | dbg("%s: Device wake notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | break; |
| 943 | |
| 944 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 945 | /* request device eject */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 946 | dbg("%s: Device eject notify on %s\n", __func__, objname); |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 947 | if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 948 | struct acpiphp_slot *slot; |
| 949 | slot = bridge->func->slot; |
| 950 | if (!acpiphp_disable_slot(slot)) |
| 951 | acpiphp_eject_slot(slot); |
| 952 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | break; |
| 954 | |
| 955 | case ACPI_NOTIFY_FREQUENCY_MISMATCH: |
| 956 | printk(KERN_ERR "Device %s cannot be configured due" |
| 957 | " to a frequency mismatch\n", objname); |
| 958 | break; |
| 959 | |
| 960 | case ACPI_NOTIFY_BUS_MODE_MISMATCH: |
| 961 | printk(KERN_ERR "Device %s cannot be configured due" |
| 962 | " to a bus mode mismatch\n", objname); |
| 963 | break; |
| 964 | |
| 965 | case ACPI_NOTIFY_POWER_FAULT: |
| 966 | printk(KERN_ERR "Device %s has suffered a power fault\n", |
| 967 | objname); |
| 968 | break; |
| 969 | |
| 970 | default: |
| 971 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); |
| 972 | break; |
| 973 | } |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 974 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 975 | acpi_scan_lock_release(); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 976 | kfree(hp_work); /* allocated in handle_hotplug_event_bridge */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | /** |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 980 | * handle_hotplug_event_bridge - handle ACPI event on bridges |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | * @handle: Notify()'ed acpi_handle |
| 982 | * @type: Notify code |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 983 | * @context: pointer to acpiphp_bridge structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | * |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 985 | * Handles ACPI event notification on {host,p2p} bridges. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | */ |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 987 | static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, |
| 988 | void *context) |
| 989 | { |
| 990 | /* |
| 991 | * Currently the code adds all hotplug events to the kacpid_wq |
| 992 | * queue when it should add hotplug events to the kacpi_hotplug_wq. |
| 993 | * The proper way to fix this is to reorganize the code so that |
| 994 | * drivers (dock, etc.) do not call acpi_os_execute(), etc. |
| 995 | * For now just re-add this work to the kacpi_hotplug_wq so we |
| 996 | * don't deadlock on hotplug actions. |
| 997 | */ |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 998 | alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | static void _handle_hotplug_event_func(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | { |
| 1003 | struct acpiphp_func *func; |
| 1004 | char objname[64]; |
| 1005 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 1006 | .pointer = objname }; |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 1007 | struct acpi_hp_work *hp_work; |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1008 | acpi_handle handle; |
| 1009 | u32 type; |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1010 | |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 1011 | hp_work = container_of(work, struct acpi_hp_work, work); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1012 | handle = hp_work->handle; |
| 1013 | type = hp_work->type; |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 1014 | func = (struct acpiphp_func *)hp_work->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1016 | acpi_scan_lock_acquire(); |
| 1017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | switch (type) { |
| 1021 | case ACPI_NOTIFY_BUS_CHECK: |
| 1022 | /* bus re-enumerate */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1023 | dbg("%s: Bus check notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | acpiphp_enable_slot(func->slot); |
| 1025 | break; |
| 1026 | |
| 1027 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 1028 | /* device check : re-enumerate from parent bus */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1029 | dbg("%s: Device check notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | acpiphp_check_bridge(func->slot->bridge); |
| 1031 | break; |
| 1032 | |
| 1033 | case ACPI_NOTIFY_DEVICE_WAKE: |
| 1034 | /* wake event */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1035 | dbg("%s: Device wake notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | break; |
| 1037 | |
| 1038 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 1039 | /* request device eject */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1040 | dbg("%s: Device eject notify on %s\n", __func__, objname); |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1041 | if (!(acpiphp_disable_slot(func->slot))) |
| 1042 | acpiphp_eject_slot(func->slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | break; |
| 1044 | |
| 1045 | default: |
| 1046 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); |
| 1047 | break; |
| 1048 | } |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1049 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1050 | acpi_scan_lock_release(); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1051 | kfree(hp_work); /* allocated in handle_hotplug_event_func */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1054 | /** |
| 1055 | * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots) |
| 1056 | * @handle: Notify()'ed acpi_handle |
| 1057 | * @type: Notify code |
| 1058 | * @context: pointer to acpiphp_func structure |
| 1059 | * |
| 1060 | * Handles ACPI event notification on slots. |
| 1061 | */ |
| 1062 | static void handle_hotplug_event_func(acpi_handle handle, u32 type, |
| 1063 | void *context) |
| 1064 | { |
| 1065 | /* |
| 1066 | * Currently the code adds all hotplug events to the kacpid_wq |
| 1067 | * queue when it should add hotplug events to the kacpi_hotplug_wq. |
| 1068 | * The proper way to fix this is to reorganize the code so that |
| 1069 | * drivers (dock, etc.) do not call acpi_os_execute(), etc. |
| 1070 | * For now just re-add this work to the kacpi_hotplug_wq so we |
| 1071 | * don't deadlock on hotplug actions. |
| 1072 | */ |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 1073 | alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1074 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 1076 | /* |
| 1077 | * Create hotplug slots for the PCI bus. |
| 1078 | * It should always return 0 to avoid skipping following notifiers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | */ |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 1080 | void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | { |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 1082 | acpi_handle dummy_handle; |
| 1083 | struct acpiphp_bridge *bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 1085 | if (acpiphp_disabled) |
| 1086 | return; |
| 1087 | |
| 1088 | if (detect_ejectable_slots(handle) <= 0) |
| 1089 | return; |
| 1090 | |
| 1091 | bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); |
| 1092 | if (bridge == NULL) { |
| 1093 | err("out of memory\n"); |
| 1094 | return; |
| 1095 | } |
| 1096 | |
Yijing Wang | ad41dd9 | 2013-04-12 05:44:27 +0000 | [diff] [blame^] | 1097 | INIT_LIST_HEAD(&bridge->slots); |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 1098 | bridge->handle = handle; |
| 1099 | bridge->pci_dev = pci_dev_get(bus->self); |
| 1100 | bridge->pci_bus = bus; |
| 1101 | |
| 1102 | /* |
| 1103 | * Grab a ref to the subordinate PCI bus in case the bus is |
| 1104 | * removed via PCI core logical hotplug. The ref pins the bus |
| 1105 | * (which we access during module unload). |
| 1106 | */ |
| 1107 | get_device(&bus->dev); |
| 1108 | |
| 1109 | if (!pci_is_root_bus(bridge->pci_bus) && |
| 1110 | ACPI_SUCCESS(acpi_get_handle(bridge->handle, |
| 1111 | "_EJ0", &dummy_handle))) { |
| 1112 | dbg("found ejectable p2p bridge\n"); |
| 1113 | bridge->flags |= BRIDGE_HAS_EJ0; |
| 1114 | bridge->func = acpiphp_bridge_handle_to_function(handle); |
| 1115 | } |
| 1116 | |
| 1117 | init_bridge_misc(bridge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 1120 | /* Destroy hotplug slots associated with the PCI bus */ |
| 1121 | void acpiphp_remove_slots(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | { |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 1123 | struct acpiphp_bridge *bridge, *tmp; |
| 1124 | |
| 1125 | if (acpiphp_disabled) |
| 1126 | return; |
| 1127 | |
| 1128 | list_for_each_entry_safe(bridge, tmp, &bridge_list, list) |
| 1129 | if (bridge->pci_bus == bus) { |
| 1130 | cleanup_bridge(bridge); |
| 1131 | break; |
| 1132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | /** |
| 1136 | * acpiphp_enable_slot - power on slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1137 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | */ |
| 1139 | int acpiphp_enable_slot(struct acpiphp_slot *slot) |
| 1140 | { |
| 1141 | int retval; |
| 1142 | |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1143 | mutex_lock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
| 1145 | /* wake up all functions */ |
| 1146 | retval = power_on_slot(slot); |
| 1147 | if (retval) |
| 1148 | goto err_exit; |
| 1149 | |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1150 | if (get_slot_status(slot) == ACPI_STA_ALL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | /* configure all functions */ |
| 1152 | retval = enable_device(slot); |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1153 | if (retval) |
| 1154 | power_off_slot(slot); |
| 1155 | } else { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1156 | dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__); |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1157 | power_off_slot(slot); |
| 1158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
| 1160 | err_exit: |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1161 | mutex_unlock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | return retval; |
| 1163 | } |
| 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | /** |
| 1166 | * acpiphp_disable_slot - power off slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1167 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | */ |
| 1169 | int acpiphp_disable_slot(struct acpiphp_slot *slot) |
| 1170 | { |
| 1171 | int retval = 0; |
| 1172 | |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1173 | mutex_lock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
| 1175 | /* unconfigure all functions */ |
| 1176 | retval = disable_device(slot); |
| 1177 | if (retval) |
| 1178 | goto err_exit; |
| 1179 | |
| 1180 | /* power off all functions */ |
| 1181 | retval = power_off_slot(slot); |
| 1182 | if (retval) |
| 1183 | goto err_exit; |
| 1184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | err_exit: |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1186 | mutex_unlock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | return retval; |
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | /* |
| 1192 | * slot enabled: 1 |
| 1193 | * slot disabled: 0 |
| 1194 | */ |
| 1195 | u8 acpiphp_get_power_status(struct acpiphp_slot *slot) |
| 1196 | { |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1197 | return (slot->flags & SLOT_POWEREDON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | /* |
MUNEDA Takahiro | 35ae61a | 2006-10-25 11:44:57 -0700 | [diff] [blame] | 1202 | * latch open: 1 |
| 1203 | * latch closed: 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | */ |
| 1205 | u8 acpiphp_get_latch_status(struct acpiphp_slot *slot) |
| 1206 | { |
| 1207 | unsigned int sta; |
| 1208 | |
| 1209 | sta = get_slot_status(slot); |
| 1210 | |
Jiang Liu | ce15d87 | 2013-04-12 05:44:19 +0000 | [diff] [blame] | 1211 | return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | /* |
| 1216 | * adapter presence : 1 |
| 1217 | * absence : 0 |
| 1218 | */ |
| 1219 | u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot) |
| 1220 | { |
| 1221 | unsigned int sta; |
| 1222 | |
| 1223 | sta = get_slot_status(slot); |
| 1224 | |
| 1225 | return (sta == 0) ? 0 : 1; |
| 1226 | } |