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: |
| 35 | * - The one in acpiphp_func has its refcount elevated by pci_get_slot() |
| 36 | * when the driver is loaded or when an insertion event occurs. It loses |
| 37 | * a refcount when its ejected or the driver unloads. |
| 38 | * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot() |
| 39 | * when the bridge is scanned and it loses a refcount when the bridge |
| 40 | * is removed. |
| 41 | */ |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/init.h> |
| 44 | #include <linux/module.h> |
| 45 | |
| 46 | #include <linux/kernel.h> |
| 47 | #include <linux/pci.h> |
Greg Kroah-Hartman | 7a54f25 | 2006-10-13 20:05:19 -0700 | [diff] [blame] | 48 | #include <linux/pci_hotplug.h> |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 49 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #include "../pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include "acpiphp.h" |
| 53 | |
| 54 | static LIST_HEAD(bridge_list); |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 55 | static LIST_HEAD(ioapic_list); |
| 56 | static DEFINE_SPINLOCK(ioapic_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 62 | static void acpiphp_set_hpp_values(acpi_handle handle, 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * initialization & terminatation routines |
| 68 | */ |
| 69 | |
| 70 | /** |
| 71 | * is_ejectable - determine if a slot is ejectable |
| 72 | * @handle: handle to acpi namespace |
| 73 | * |
| 74 | * Ejectable slot should satisfy at least these conditions: |
| 75 | * |
| 76 | * 1. has _ADR method |
| 77 | * 2. has _EJ0 method |
| 78 | * |
| 79 | * optionally |
| 80 | * |
| 81 | * 1. has _STA method |
| 82 | * 2. has _PS0 method |
| 83 | * 3. has _PS3 method |
| 84 | * 4. .. |
| 85 | * |
| 86 | */ |
| 87 | static int is_ejectable(acpi_handle handle) |
| 88 | { |
| 89 | acpi_status status; |
| 90 | acpi_handle tmp; |
| 91 | |
| 92 | status = acpi_get_handle(handle, "_ADR", &tmp); |
| 93 | if (ACPI_FAILURE(status)) { |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | status = acpi_get_handle(handle, "_EJ0", &tmp); |
| 98 | if (ACPI_FAILURE(status)) { |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /* callback routine to check the existence of ejectable slots */ |
| 107 | static acpi_status |
| 108 | is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 109 | { |
| 110 | int *count = (int *)context; |
| 111 | |
| 112 | if (is_ejectable(handle)) { |
| 113 | (*count)++; |
| 114 | /* only one ejectable slot is enough */ |
| 115 | return AE_CTRL_TERMINATE; |
| 116 | } else { |
| 117 | return AE_OK; |
| 118 | } |
| 119 | } |
| 120 | |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 121 | /* callback routine to check for the existance of a pci dock device */ |
| 122 | static acpi_status |
| 123 | is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 124 | { |
| 125 | int *count = (int *)context; |
| 126 | |
| 127 | if (is_dock_device(handle)) { |
| 128 | (*count)++; |
| 129 | return AE_CTRL_TERMINATE; |
| 130 | } else { |
| 131 | return AE_OK; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | /* |
| 139 | * the _DCK method can do funny things... and sometimes not |
| 140 | * hah-hah funny. |
| 141 | * |
| 142 | * TBD - figure out a way to only call fixups for |
| 143 | * systems that require them. |
| 144 | */ |
| 145 | static int post_dock_fixups(struct notifier_block *nb, unsigned long val, |
| 146 | void *v) |
| 147 | { |
| 148 | struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb); |
| 149 | struct pci_bus *bus = func->slot->bridge->pci_bus; |
| 150 | u32 buses; |
| 151 | |
| 152 | if (!bus->self) |
| 153 | return NOTIFY_OK; |
| 154 | |
| 155 | /* fixup bad _DCK function that rewrites |
| 156 | * secondary bridge on slot |
| 157 | */ |
| 158 | pci_read_config_dword(bus->self, |
| 159 | PCI_PRIMARY_BUS, |
| 160 | &buses); |
| 161 | |
| 162 | if (((buses >> 8) & 0xff) != bus->secondary) { |
| 163 | buses = (buses & 0xff000000) |
| 164 | | ((unsigned int)(bus->primary) << 0) |
| 165 | | ((unsigned int)(bus->secondary) << 8) |
| 166 | | ((unsigned int)(bus->subordinate) << 16); |
| 167 | pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses); |
| 168 | } |
| 169 | return NOTIFY_OK; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | /* callback routine to register each ACPI PCI slot object */ |
| 176 | static acpi_status |
| 177 | register_slot(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 178 | { |
| 179 | struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context; |
| 180 | struct acpiphp_slot *slot; |
| 181 | struct acpiphp_func *newfunc; |
| 182 | acpi_handle tmp; |
| 183 | acpi_status status = AE_OK; |
| 184 | unsigned long adr, sun; |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 185 | int device, function, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr); |
| 188 | |
| 189 | if (ACPI_FAILURE(status)) |
| 190 | return AE_OK; |
| 191 | |
| 192 | status = acpi_get_handle(handle, "_EJ0", &tmp); |
| 193 | |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 194 | if (ACPI_FAILURE(status) && !(is_dock_device(handle))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return AE_OK; |
| 196 | |
| 197 | device = (adr >> 16) & 0xffff; |
| 198 | function = adr & 0xffff; |
| 199 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 200 | newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | if (!newfunc) |
| 202 | return AE_NO_MEMORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | INIT_LIST_HEAD(&newfunc->sibling); |
| 205 | newfunc->handle = handle; |
| 206 | newfunc->function = function; |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 207 | if (ACPI_SUCCESS(status)) |
| 208 | newfunc->flags = FUNC_HAS_EJ0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp))) |
| 211 | newfunc->flags |= FUNC_HAS_STA; |
| 212 | |
| 213 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp))) |
| 214 | newfunc->flags |= FUNC_HAS_PS0; |
| 215 | |
| 216 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp))) |
| 217 | newfunc->flags |= FUNC_HAS_PS3; |
| 218 | |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 219 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 220 | newfunc->flags |= FUNC_HAS_DCK; |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun); |
Kristen Accardi | 95b38b3 | 2006-06-28 03:09:54 -0400 | [diff] [blame] | 223 | if (ACPI_FAILURE(status)) { |
| 224 | /* |
| 225 | * use the count of the number of slots we've found |
| 226 | * for the number of the slot |
| 227 | */ |
| 228 | sun = bridge->nr_slots+1; |
| 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | /* search for objects that share the same slot */ |
| 232 | for (slot = bridge->slots; slot; slot = slot->next) |
| 233 | if (slot->device == device) { |
| 234 | if (slot->sun != sun) |
| 235 | warn("sibling found, but _SUN doesn't match!\n"); |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | if (!slot) { |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 240 | slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if (!slot) { |
| 242 | kfree(newfunc); |
| 243 | return AE_NO_MEMORY; |
| 244 | } |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | slot->bridge = bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | slot->device = device; |
| 248 | slot->sun = sun; |
| 249 | INIT_LIST_HEAD(&slot->funcs); |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 250 | mutex_init(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | slot->next = bridge->slots; |
| 253 | bridge->slots = slot; |
| 254 | |
| 255 | bridge->nr_slots++; |
| 256 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 257 | dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n", |
| 258 | slot->sun, pci_domain_nr(bridge->pci_bus), |
| 259 | bridge->pci_bus->number, slot->device); |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 260 | retval = acpiphp_register_hotplug_slot(slot); |
| 261 | if (retval) { |
| 262 | warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval); |
| 263 | goto err_exit; |
| 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | newfunc->slot = slot; |
| 268 | list_add_tail(&newfunc->sibling, &slot->funcs); |
| 269 | |
| 270 | /* associate corresponding pci_dev */ |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 271 | newfunc->pci_dev = pci_get_slot(bridge->pci_bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | PCI_DEVFN(device, function)); |
| 273 | if (newfunc->pci_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON); |
| 275 | } |
| 276 | |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 277 | if (is_dock_device(handle)) { |
| 278 | /* we don't want to call this device's _EJ0 |
| 279 | * because we want the dock notify handler |
| 280 | * to call it after it calls _DCK |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 281 | */ |
| 282 | newfunc->flags &= ~FUNC_HAS_EJ0; |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 283 | if (register_hotplug_dock_device(handle, |
| 284 | handle_hotplug_event_func, newfunc)) |
| 285 | dbg("failed to register dock device\n"); |
| 286 | |
| 287 | /* we need to be notified when dock events happen |
| 288 | * outside of the hotplug operation, since we may |
| 289 | * need to do fixups before we can hotplug. |
| 290 | */ |
| 291 | newfunc->nb.notifier_call = post_dock_fixups; |
| 292 | if (register_dock_notifier(&newfunc->nb)) |
| 293 | dbg("failed to register a dock notifier"); |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | /* install notify handler */ |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 297 | if (!(newfunc->flags & FUNC_HAS_DCK)) { |
| 298 | status = acpi_install_notify_handler(handle, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | ACPI_SYSTEM_NOTIFY, |
| 300 | handle_hotplug_event_func, |
| 301 | newfunc); |
| 302 | |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 303 | if (ACPI_FAILURE(status)) |
| 304 | err("failed to register interrupt notify handler\n"); |
| 305 | } else |
| 306 | status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 308 | return status; |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 309 | |
| 310 | err_exit: |
| 311 | bridge->nr_slots--; |
| 312 | bridge->slots = slot->next; |
| 313 | kfree(slot); |
| 314 | kfree(newfunc); |
| 315 | |
| 316 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
| 320 | /* see if it's worth looking at this bridge */ |
| 321 | static int detect_ejectable_slots(acpi_handle *bridge_handle) |
| 322 | { |
| 323 | acpi_status status; |
| 324 | int count; |
| 325 | |
| 326 | count = 0; |
| 327 | |
| 328 | /* only check slots defined directly below bridge object */ |
| 329 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1, |
| 330 | is_ejectable_slot, (void *)&count, NULL); |
| 331 | |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 332 | /* |
| 333 | * we also need to add this bridge if there is a dock bridge or |
| 334 | * other pci device on a dock station (removable) |
| 335 | */ |
| 336 | if (!count) |
| 337 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, |
| 338 | (u32)1, is_pci_dock_device, (void *)&count, |
| 339 | NULL); |
| 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return count; |
| 342 | } |
| 343 | |
| 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | /* decode ACPI 2.0 _HPP hot plug parameters */ |
| 346 | static void decode_hpp(struct acpiphp_bridge *bridge) |
| 347 | { |
| 348 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Kenji Kaneshige | 7430e34 | 2006-05-02 10:54:50 +0900 | [diff] [blame] | 350 | status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp); |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 351 | if (ACPI_FAILURE(status) || |
| 352 | !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) { |
Kristen Accardi | 783c49f | 2006-03-03 10:16:05 -0800 | [diff] [blame] | 353 | /* use default numbers */ |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 354 | printk(KERN_WARNING |
| 355 | "%s: Could not get hotplug parameters. Use defaults\n", |
| 356 | __FUNCTION__); |
| 357 | bridge->hpp.t0 = &bridge->hpp.type0_data; |
| 358 | bridge->hpp.t0->revision = 0; |
| 359 | bridge->hpp.t0->cache_line_size = 0x10; |
| 360 | bridge->hpp.t0->latency_timer = 0x40; |
| 361 | bridge->hpp.t0->enable_serr = 0; |
| 362 | bridge->hpp.t0->enable_perr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
Kristen Accardi | 783c49f | 2006-03-03 10:16:05 -0800 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */ |
| 369 | static void init_bridge_misc(struct acpiphp_bridge *bridge) |
| 370 | { |
| 371 | acpi_status status; |
| 372 | |
| 373 | /* decode ACPI 2.0 _HPP (hot plug parameters) */ |
| 374 | decode_hpp(bridge); |
| 375 | |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 376 | /* must be added to the list prior to calling register_slot */ |
| 377 | list_add(&bridge->list, &bridge_list); |
| 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | /* register all slot objects under this bridge */ |
| 380 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1, |
| 381 | register_slot, bridge, NULL); |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 382 | if (ACPI_FAILURE(status)) { |
| 383 | list_del(&bridge->list); |
| 384 | return; |
| 385 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | /* install notify handler */ |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 388 | if (bridge->type != BRIDGE_TYPE_HOST) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 389 | if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) { |
| 390 | status = acpi_remove_notify_handler(bridge->func->handle, |
| 391 | ACPI_SYSTEM_NOTIFY, |
| 392 | handle_hotplug_event_func); |
| 393 | if (ACPI_FAILURE(status)) |
| 394 | err("failed to remove notify handler\n"); |
| 395 | } |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 396 | status = acpi_install_notify_handler(bridge->handle, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | ACPI_SYSTEM_NOTIFY, |
| 398 | handle_hotplug_event_bridge, |
| 399 | bridge); |
| 400 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 401 | if (ACPI_FAILURE(status)) { |
| 402 | err("failed to register interrupt notify handler\n"); |
| 403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 408 | /* find acpiphp_func from acpiphp_bridge */ |
| 409 | static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle) |
| 410 | { |
| 411 | struct list_head *node, *l; |
| 412 | struct acpiphp_bridge *bridge; |
| 413 | struct acpiphp_slot *slot; |
| 414 | struct acpiphp_func *func; |
| 415 | |
| 416 | list_for_each(node, &bridge_list) { |
| 417 | bridge = list_entry(node, struct acpiphp_bridge, list); |
| 418 | for (slot = bridge->slots; slot; slot = slot->next) { |
| 419 | list_for_each(l, &slot->funcs) { |
| 420 | func = list_entry(l, struct acpiphp_func, |
| 421 | sibling); |
| 422 | if (func->handle == handle) |
| 423 | return func; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge) |
| 433 | { |
| 434 | acpi_handle dummy_handle; |
| 435 | |
| 436 | if (ACPI_SUCCESS(acpi_get_handle(bridge->handle, |
| 437 | "_STA", &dummy_handle))) |
| 438 | bridge->flags |= BRIDGE_HAS_STA; |
| 439 | |
| 440 | if (ACPI_SUCCESS(acpi_get_handle(bridge->handle, |
| 441 | "_EJ0", &dummy_handle))) |
| 442 | bridge->flags |= BRIDGE_HAS_EJ0; |
| 443 | |
| 444 | if (ACPI_SUCCESS(acpi_get_handle(bridge->handle, |
| 445 | "_PS0", &dummy_handle))) |
| 446 | bridge->flags |= BRIDGE_HAS_PS0; |
| 447 | |
| 448 | if (ACPI_SUCCESS(acpi_get_handle(bridge->handle, |
| 449 | "_PS3", &dummy_handle))) |
| 450 | bridge->flags |= BRIDGE_HAS_PS3; |
| 451 | |
| 452 | /* is this ejectable p2p bridge? */ |
| 453 | if (bridge->flags & BRIDGE_HAS_EJ0) { |
| 454 | struct acpiphp_func *func; |
| 455 | |
| 456 | dbg("found ejectable p2p bridge\n"); |
| 457 | |
| 458 | /* make link between PCI bridge and PCI function */ |
| 459 | func = acpiphp_bridge_handle_to_function(bridge->handle); |
| 460 | if (!func) |
| 461 | return; |
| 462 | bridge->func = func; |
| 463 | func->bridge = bridge; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | /* allocate and initialize host bridge data structure */ |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 469 | static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | struct acpiphp_bridge *bridge; |
| 472 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 473 | bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (bridge == NULL) |
| 475 | return; |
| 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | bridge->type = BRIDGE_TYPE_HOST; |
| 478 | bridge->handle = handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 480 | bridge->pci_bus = pci_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | spin_lock_init(&bridge->res_lock); |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | init_bridge_misc(bridge); |
| 485 | } |
| 486 | |
| 487 | |
| 488 | /* allocate and initialize PCI-to-PCI bridge data structure */ |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 489 | static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
| 491 | struct acpiphp_bridge *bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 493 | bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | if (bridge == NULL) { |
| 495 | err("out of memory\n"); |
| 496 | return; |
| 497 | } |
| 498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | bridge->type = BRIDGE_TYPE_P2P; |
| 500 | bridge->handle = handle; |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 501 | config_p2p_bridge_flags(bridge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 503 | bridge->pci_dev = pci_dev_get(pci_dev); |
| 504 | bridge->pci_bus = pci_dev->subordinate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if (!bridge->pci_bus) { |
| 506 | err("This is not a PCI-to-PCI bridge!\n"); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 507 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | spin_lock_init(&bridge->res_lock); |
| 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | init_bridge_misc(bridge); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 513 | return; |
| 514 | err: |
| 515 | pci_dev_put(pci_dev); |
| 516 | kfree(bridge); |
| 517 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | |
| 521 | /* callback routine to find P2P bridges */ |
| 522 | static acpi_status |
| 523 | find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 524 | { |
| 525 | acpi_status status; |
| 526 | acpi_handle dummy_handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | unsigned long tmp; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 528 | int device, function; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | struct pci_dev *dev; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 530 | struct pci_bus *pci_bus = context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
| 532 | status = acpi_get_handle(handle, "_ADR", &dummy_handle); |
| 533 | if (ACPI_FAILURE(status)) |
| 534 | return AE_OK; /* continue */ |
| 535 | |
| 536 | status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp); |
| 537 | if (ACPI_FAILURE(status)) { |
| 538 | dbg("%s: _ADR evaluation failure\n", __FUNCTION__); |
| 539 | return AE_OK; |
| 540 | } |
| 541 | |
| 542 | device = (tmp >> 16) & 0xffff; |
| 543 | function = tmp & 0xffff; |
| 544 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 545 | dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 547 | if (!dev || !dev->subordinate) |
| 548 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | /* check if this bridge has ejectable slots */ |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 551 | if ((detect_ejectable_slots(handle) > 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev)); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 553 | add_p2p_bridge(handle, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Kenji Kaneshige | 7c8f25d | 2006-02-27 22:15:49 +0900 | [diff] [blame] | 556 | /* search P2P bridges under this p2p bridge */ |
| 557 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
| 558 | find_p2p_bridge, dev->subordinate, NULL); |
| 559 | if (ACPI_FAILURE(status)) |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 560 | warn("find_p2p_bridge failed (error code = 0x%x)\n", status); |
Kenji Kaneshige | 7c8f25d | 2006-02-27 22:15:49 +0900 | [diff] [blame] | 561 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 562 | out: |
| 563 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | return AE_OK; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | /* find hot-pluggable slots, and then find P2P bridge */ |
| 569 | static int add_bridge(acpi_handle handle) |
| 570 | { |
| 571 | acpi_status status; |
| 572 | unsigned long tmp; |
| 573 | int seg, bus; |
| 574 | acpi_handle dummy_handle; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 575 | struct pci_bus *pci_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | /* if the bridge doesn't have _STA, we assume it is always there */ |
| 578 | status = acpi_get_handle(handle, "_STA", &dummy_handle); |
| 579 | if (ACPI_SUCCESS(status)) { |
| 580 | status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp); |
| 581 | if (ACPI_FAILURE(status)) { |
| 582 | dbg("%s: _STA evaluation failure\n", __FUNCTION__); |
| 583 | return 0; |
| 584 | } |
| 585 | if ((tmp & ACPI_STA_FUNCTIONING) == 0) |
| 586 | /* don't register this object */ |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | /* get PCI segment number */ |
| 591 | status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp); |
| 592 | |
| 593 | seg = ACPI_SUCCESS(status) ? tmp : 0; |
| 594 | |
| 595 | /* get PCI bus number */ |
| 596 | status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp); |
| 597 | |
| 598 | if (ACPI_SUCCESS(status)) { |
| 599 | bus = tmp; |
| 600 | } else { |
| 601 | warn("can't get bus number, assuming 0\n"); |
| 602 | bus = 0; |
| 603 | } |
| 604 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 605 | pci_bus = pci_find_bus(seg, bus); |
| 606 | if (!pci_bus) { |
| 607 | err("Can't find bus %04x:%02x\n", seg, bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | return 0; |
| 609 | } |
| 610 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 611 | /* check if this bridge has ejectable slots */ |
| 612 | if (detect_ejectable_slots(handle) > 0) { |
| 613 | dbg("found PCI host-bus bridge with hot-pluggable slots\n"); |
| 614 | add_host_bridge(handle, pci_bus); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 615 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
| 617 | /* search P2P bridges under this host bridge */ |
| 618 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 619 | find_p2p_bridge, pci_bus, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
| 621 | if (ACPI_FAILURE(status)) |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 622 | warn("find_p2p_bridge failed (error code = 0x%x)\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 627 | static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle) |
| 628 | { |
| 629 | struct list_head *head; |
| 630 | list_for_each(head, &bridge_list) { |
| 631 | struct acpiphp_bridge *bridge = list_entry(head, |
| 632 | struct acpiphp_bridge, list); |
| 633 | if (bridge->handle == handle) |
| 634 | return bridge; |
| 635 | } |
| 636 | |
| 637 | return NULL; |
| 638 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 640 | static void cleanup_bridge(struct acpiphp_bridge *bridge) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 642 | struct list_head *list, *tmp; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 643 | struct acpiphp_slot *slot; |
| 644 | acpi_status status; |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 645 | acpi_handle handle = bridge->handle; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 646 | |
| 647 | status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
| 648 | handle_hotplug_event_bridge); |
| 649 | if (ACPI_FAILURE(status)) |
| 650 | err("failed to remove notify handler\n"); |
| 651 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 652 | if ((bridge->type != BRIDGE_TYPE_HOST) && |
| 653 | ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) { |
| 654 | status = acpi_install_notify_handler(bridge->func->handle, |
| 655 | ACPI_SYSTEM_NOTIFY, |
| 656 | handle_hotplug_event_func, |
| 657 | bridge->func); |
| 658 | if (ACPI_FAILURE(status)) |
| 659 | err("failed to install interrupt notify handler\n"); |
| 660 | } |
| 661 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 662 | slot = bridge->slots; |
| 663 | while (slot) { |
| 664 | struct acpiphp_slot *next = slot->next; |
| 665 | list_for_each_safe (list, tmp, &slot->funcs) { |
| 666 | struct acpiphp_func *func; |
| 667 | func = list_entry(list, struct acpiphp_func, sibling); |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 668 | if (is_dock_device(func->handle)) { |
| 669 | unregister_hotplug_dock_device(func->handle); |
| 670 | unregister_dock_notifier(&func->nb); |
| 671 | } |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 672 | if (!(func->flags & FUNC_HAS_DCK)) { |
| 673 | status = acpi_remove_notify_handler(func->handle, |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 674 | ACPI_SYSTEM_NOTIFY, |
| 675 | handle_hotplug_event_func); |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 676 | if (ACPI_FAILURE(status)) |
| 677 | err("failed to remove notify handler\n"); |
| 678 | } |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 679 | pci_dev_put(func->pci_dev); |
| 680 | list_del(list); |
| 681 | kfree(func); |
| 682 | } |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 683 | acpiphp_unregister_hotplug_slot(slot); |
| 684 | list_del(&slot->funcs); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 685 | kfree(slot); |
| 686 | slot = next; |
| 687 | } |
| 688 | |
| 689 | pci_dev_put(bridge->pci_dev); |
| 690 | list_del(&bridge->list); |
| 691 | kfree(bridge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 694 | static acpi_status |
| 695 | cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 696 | { |
| 697 | struct acpiphp_bridge *bridge; |
| 698 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 699 | /* cleanup p2p bridges under this P2P bridge |
| 700 | in a depth-first manner */ |
| 701 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
| 702 | cleanup_p2p_bridge, NULL, NULL); |
| 703 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 704 | if (!(bridge = acpiphp_handle_to_bridge(handle))) |
| 705 | return AE_OK; |
| 706 | cleanup_bridge(bridge); |
| 707 | return AE_OK; |
| 708 | } |
| 709 | |
| 710 | static void remove_bridge(acpi_handle handle) |
| 711 | { |
| 712 | struct acpiphp_bridge *bridge; |
| 713 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 714 | /* cleanup p2p bridges under this host bridge |
| 715 | in a depth-first manner */ |
| 716 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, |
| 717 | (u32)1, cleanup_p2p_bridge, NULL, NULL); |
| 718 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 719 | bridge = acpiphp_handle_to_bridge(handle); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 720 | if (bridge) |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 721 | cleanup_bridge(bridge); |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 724 | static struct pci_dev * get_apic_pci_info(acpi_handle handle) |
| 725 | { |
| 726 | struct acpi_pci_id id; |
| 727 | struct pci_bus *bus; |
| 728 | struct pci_dev *dev; |
| 729 | |
| 730 | if (ACPI_FAILURE(acpi_get_pci_id(handle, &id))) |
| 731 | return NULL; |
| 732 | |
| 733 | bus = pci_find_bus(id.segment, id.bus); |
| 734 | if (!bus) |
| 735 | return NULL; |
| 736 | |
| 737 | dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function)); |
| 738 | if (!dev) |
| 739 | return NULL; |
| 740 | |
| 741 | if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) && |
| 742 | (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC)) |
| 743 | { |
| 744 | pci_dev_put(dev); |
| 745 | return NULL; |
| 746 | } |
| 747 | |
| 748 | return dev; |
| 749 | } |
| 750 | |
| 751 | static int get_gsi_base(acpi_handle handle, u32 *gsi_base) |
| 752 | { |
| 753 | acpi_status status; |
| 754 | int result = -1; |
| 755 | unsigned long gsb; |
| 756 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 757 | union acpi_object *obj; |
| 758 | void *table; |
| 759 | |
| 760 | status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb); |
| 761 | if (ACPI_SUCCESS(status)) { |
| 762 | *gsi_base = (u32)gsb; |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer); |
| 767 | if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer) |
| 768 | return -1; |
| 769 | |
| 770 | obj = buffer.pointer; |
| 771 | if (obj->type != ACPI_TYPE_BUFFER) |
| 772 | goto out; |
| 773 | |
| 774 | table = obj->buffer.pointer; |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 775 | switch (((struct acpi_subtable_header *)table)->type) { |
| 776 | case ACPI_MADT_TYPE_IO_SAPIC: |
| 777 | *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base; |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 778 | result = 0; |
| 779 | break; |
Alexey Starikovskiy | 15a58ed | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 780 | case ACPI_MADT_TYPE_IO_APIC: |
| 781 | *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base; |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 782 | result = 0; |
| 783 | break; |
| 784 | default: |
| 785 | break; |
| 786 | } |
| 787 | out: |
Kristen Accardi | 81b26bc | 2006-04-18 14:36:43 -0700 | [diff] [blame] | 788 | kfree(buffer.pointer); |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 789 | return result; |
| 790 | } |
| 791 | |
| 792 | static acpi_status |
| 793 | ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 794 | { |
| 795 | acpi_status status; |
| 796 | unsigned long sta; |
| 797 | acpi_handle tmp; |
| 798 | struct pci_dev *pdev; |
| 799 | u32 gsi_base; |
| 800 | u64 phys_addr; |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 801 | struct acpiphp_ioapic *ioapic; |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 802 | |
| 803 | /* Evaluate _STA if present */ |
| 804 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
| 805 | if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL) |
| 806 | return AE_CTRL_DEPTH; |
| 807 | |
| 808 | /* Scan only PCI bus scope */ |
| 809 | status = acpi_get_handle(handle, "_HID", &tmp); |
| 810 | if (ACPI_SUCCESS(status)) |
| 811 | return AE_CTRL_DEPTH; |
| 812 | |
| 813 | if (get_gsi_base(handle, &gsi_base)) |
| 814 | return AE_OK; |
| 815 | |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 816 | ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL); |
| 817 | if (!ioapic) |
| 818 | return AE_NO_MEMORY; |
| 819 | |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 820 | pdev = get_apic_pci_info(handle); |
| 821 | if (!pdev) |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 822 | goto exit_kfree; |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 823 | |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 824 | if (pci_enable_device(pdev)) |
| 825 | goto exit_pci_dev_put; |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 826 | |
| 827 | pci_set_master(pdev); |
| 828 | |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 829 | if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) |
| 830 | goto exit_pci_disable_device; |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 831 | |
| 832 | phys_addr = pci_resource_start(pdev, 0); |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 833 | if (acpi_register_ioapic(handle, phys_addr, gsi_base)) |
| 834 | goto exit_pci_release_region; |
| 835 | |
| 836 | ioapic->gsi_base = gsi_base; |
| 837 | ioapic->dev = pdev; |
| 838 | spin_lock(&ioapic_list_lock); |
| 839 | list_add_tail(&ioapic->list, &ioapic_list); |
| 840 | spin_unlock(&ioapic_list_lock); |
| 841 | |
| 842 | return AE_OK; |
| 843 | |
| 844 | exit_pci_release_region: |
| 845 | pci_release_region(pdev, 0); |
| 846 | exit_pci_disable_device: |
| 847 | pci_disable_device(pdev); |
| 848 | exit_pci_dev_put: |
| 849 | pci_dev_put(pdev); |
| 850 | exit_kfree: |
| 851 | kfree(ioapic); |
| 852 | |
| 853 | return AE_OK; |
| 854 | } |
| 855 | |
| 856 | static acpi_status |
| 857 | ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 858 | { |
| 859 | acpi_status status; |
| 860 | unsigned long sta; |
| 861 | acpi_handle tmp; |
| 862 | u32 gsi_base; |
| 863 | struct acpiphp_ioapic *pos, *n, *ioapic = NULL; |
| 864 | |
| 865 | /* Evaluate _STA if present */ |
| 866 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
| 867 | if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL) |
| 868 | return AE_CTRL_DEPTH; |
| 869 | |
| 870 | /* Scan only PCI bus scope */ |
| 871 | status = acpi_get_handle(handle, "_HID", &tmp); |
| 872 | if (ACPI_SUCCESS(status)) |
| 873 | return AE_CTRL_DEPTH; |
| 874 | |
| 875 | if (get_gsi_base(handle, &gsi_base)) |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 876 | return AE_OK; |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 877 | |
| 878 | acpi_unregister_ioapic(handle, gsi_base); |
| 879 | |
| 880 | spin_lock(&ioapic_list_lock); |
| 881 | list_for_each_entry_safe(pos, n, &ioapic_list, list) { |
| 882 | if (pos->gsi_base != gsi_base) |
| 883 | continue; |
| 884 | ioapic = pos; |
| 885 | list_del(&ioapic->list); |
| 886 | break; |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 887 | } |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 888 | spin_unlock(&ioapic_list_lock); |
| 889 | |
| 890 | if (!ioapic) |
| 891 | return AE_OK; |
| 892 | |
| 893 | pci_release_region(ioapic->dev, 0); |
| 894 | pci_disable_device(ioapic->dev); |
| 895 | pci_dev_put(ioapic->dev); |
| 896 | kfree(ioapic); |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 897 | |
| 898 | return AE_OK; |
| 899 | } |
| 900 | |
| 901 | static int acpiphp_configure_ioapics(acpi_handle handle) |
| 902 | { |
Satoru Takeuchi | 9b1d19e | 2006-09-12 10:15:10 -0700 | [diff] [blame] | 903 | ioapic_add(handle, 0, NULL, NULL); |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 904 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, |
| 905 | ACPI_UINT32_MAX, ioapic_add, NULL, NULL); |
| 906 | return 0; |
| 907 | } |
| 908 | |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 909 | static int acpiphp_unconfigure_ioapics(acpi_handle handle) |
| 910 | { |
| 911 | ioapic_remove(handle, 0, NULL, NULL); |
| 912 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, |
| 913 | ACPI_UINT32_MAX, ioapic_remove, NULL, NULL); |
| 914 | return 0; |
| 915 | } |
| 916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | static int power_on_slot(struct acpiphp_slot *slot) |
| 918 | { |
| 919 | acpi_status status; |
| 920 | struct acpiphp_func *func; |
| 921 | struct list_head *l; |
| 922 | int retval = 0; |
| 923 | |
| 924 | /* if already enabled, just skip */ |
| 925 | if (slot->flags & SLOT_POWEREDON) |
| 926 | goto err_exit; |
| 927 | |
| 928 | list_for_each (l, &slot->funcs) { |
| 929 | func = list_entry(l, struct acpiphp_func, sibling); |
| 930 | |
| 931 | if (func->flags & FUNC_HAS_PS0) { |
| 932 | dbg("%s: executing _PS0\n", __FUNCTION__); |
| 933 | status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL); |
| 934 | if (ACPI_FAILURE(status)) { |
| 935 | warn("%s: _PS0 failed\n", __FUNCTION__); |
| 936 | retval = -1; |
| 937 | goto err_exit; |
| 938 | } else |
| 939 | break; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /* TBD: evaluate _STA to check if the slot is enabled */ |
| 944 | |
| 945 | slot->flags |= SLOT_POWEREDON; |
| 946 | |
| 947 | err_exit: |
| 948 | return retval; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | static int power_off_slot(struct acpiphp_slot *slot) |
| 953 | { |
| 954 | acpi_status status; |
| 955 | struct acpiphp_func *func; |
| 956 | struct list_head *l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
| 958 | int retval = 0; |
| 959 | |
| 960 | /* if already disabled, just skip */ |
| 961 | if ((slot->flags & SLOT_POWEREDON) == 0) |
| 962 | goto err_exit; |
| 963 | |
| 964 | list_for_each (l, &slot->funcs) { |
| 965 | func = list_entry(l, struct acpiphp_func, sibling); |
| 966 | |
Rajesh Shah | 2f523b1 | 2005-04-28 00:25:55 -0700 | [diff] [blame] | 967 | if (func->flags & FUNC_HAS_PS3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL); |
| 969 | if (ACPI_FAILURE(status)) { |
| 970 | warn("%s: _PS3 failed\n", __FUNCTION__); |
| 971 | retval = -1; |
| 972 | goto err_exit; |
| 973 | } else |
| 974 | break; |
| 975 | } |
| 976 | } |
| 977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | /* TBD: evaluate _STA to check if the slot is disabled */ |
| 979 | |
| 980 | slot->flags &= (~SLOT_POWEREDON); |
| 981 | |
| 982 | err_exit: |
| 983 | return retval; |
| 984 | } |
| 985 | |
| 986 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 987 | |
| 988 | /** |
| 989 | * acpiphp_max_busnr - return the highest reserved bus number under |
| 990 | * the given bus. |
| 991 | * @bus: bus to start search with |
| 992 | * |
| 993 | */ |
| 994 | static unsigned char acpiphp_max_busnr(struct pci_bus *bus) |
| 995 | { |
| 996 | struct list_head *tmp; |
| 997 | unsigned char max, n; |
| 998 | |
| 999 | /* |
| 1000 | * pci_bus_max_busnr will return the highest |
| 1001 | * reserved busnr for all these children. |
| 1002 | * that is equivalent to the bus->subordinate |
| 1003 | * value. We don't want to use the parent's |
| 1004 | * bus->subordinate value because it could have |
| 1005 | * padding in it. |
| 1006 | */ |
| 1007 | max = bus->secondary; |
| 1008 | |
| 1009 | list_for_each(tmp, &bus->children) { |
| 1010 | n = pci_bus_max_busnr(pci_bus_b(tmp)); |
| 1011 | if (n > max) |
| 1012 | max = n; |
| 1013 | } |
| 1014 | return max; |
| 1015 | } |
| 1016 | |
| 1017 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 1018 | /** |
| 1019 | * acpiphp_bus_add - add a new bus to acpi subsystem |
| 1020 | * @func: acpiphp_func of the bridge |
| 1021 | * |
| 1022 | */ |
| 1023 | static int acpiphp_bus_add(struct acpiphp_func *func) |
| 1024 | { |
| 1025 | acpi_handle phandle; |
| 1026 | struct acpi_device *device, *pdevice; |
| 1027 | int ret_val; |
| 1028 | |
| 1029 | acpi_get_parent(func->handle, &phandle); |
| 1030 | if (acpi_bus_get_device(phandle, &pdevice)) { |
| 1031 | dbg("no parent device, assuming NULL\n"); |
| 1032 | pdevice = NULL; |
| 1033 | } |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 1034 | if (!acpi_bus_get_device(func->handle, &device)) { |
| 1035 | dbg("bus exists... trim\n"); |
| 1036 | /* this shouldn't be in here, so remove |
| 1037 | * the bus then re-add it... |
| 1038 | */ |
| 1039 | ret_val = acpi_bus_trim(device, 1); |
| 1040 | dbg("acpi_bus_trim return %x\n", ret_val); |
| 1041 | } |
| 1042 | |
| 1043 | ret_val = acpi_bus_add(&device, pdevice, func->handle, |
| 1044 | ACPI_BUS_TYPE_DEVICE); |
| 1045 | if (ret_val) { |
| 1046 | dbg("error adding bus, %x\n", |
| 1047 | -ret_val); |
| 1048 | goto acpiphp_bus_add_out; |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 1049 | } |
| 1050 | /* |
| 1051 | * try to start anyway. We could have failed to add |
| 1052 | * simply because this bus had previously been added |
| 1053 | * on another add. Don't bother with the return value |
| 1054 | * we just keep going. |
| 1055 | */ |
| 1056 | ret_val = acpi_bus_start(device); |
| 1057 | |
| 1058 | acpiphp_bus_add_out: |
| 1059 | return ret_val; |
| 1060 | } |
| 1061 | |
| 1062 | |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 1063 | /** |
| 1064 | * acpiphp_bus_trim - trim a bus from acpi subsystem |
| 1065 | * @handle: handle to acpi namespace |
| 1066 | * |
| 1067 | */ |
Adrian Bunk | 6d47a5e | 2006-08-14 23:07:38 -0700 | [diff] [blame] | 1068 | static int acpiphp_bus_trim(acpi_handle handle) |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 1069 | { |
| 1070 | struct acpi_device *device; |
| 1071 | int retval; |
| 1072 | |
| 1073 | retval = acpi_bus_get_device(handle, &device); |
| 1074 | if (retval) { |
| 1075 | dbg("acpi_device not found\n"); |
| 1076 | return retval; |
| 1077 | } |
| 1078 | |
| 1079 | retval = acpi_bus_trim(device, 1); |
| 1080 | if (retval) |
| 1081 | err("cannot remove from acpi list\n"); |
| 1082 | |
| 1083 | return retval; |
| 1084 | } |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 1085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | /** |
| 1087 | * enable_device - enable, configure a slot |
| 1088 | * @slot: slot to be enabled |
| 1089 | * |
| 1090 | * This function should be called per *physical slot*, |
| 1091 | * not per each slot object in ACPI namespace. |
| 1092 | * |
| 1093 | */ |
| 1094 | static int enable_device(struct acpiphp_slot *slot) |
| 1095 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | struct pci_dev *dev; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1097 | struct pci_bus *bus = slot->bridge->pci_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | struct list_head *l; |
| 1099 | struct acpiphp_func *func; |
| 1100 | int retval = 0; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1101 | int num, max, pass; |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 1102 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
| 1104 | if (slot->flags & SLOT_ENABLED) |
| 1105 | goto err_exit; |
| 1106 | |
| 1107 | /* sanity check: dev should be NULL when hot-plugged in */ |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1108 | dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | if (dev) { |
| 1110 | /* This case shouldn't happen */ |
| 1111 | err("pci_dev structure already exists.\n"); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1112 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | retval = -1; |
| 1114 | goto err_exit; |
| 1115 | } |
| 1116 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1117 | num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0)); |
| 1118 | if (num == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | err("No new device found\n"); |
| 1120 | retval = -1; |
| 1121 | goto err_exit; |
| 1122 | } |
| 1123 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 1124 | max = acpiphp_max_busnr(bus); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1125 | for (pass = 0; pass < 2; pass++) { |
| 1126 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1127 | if (PCI_SLOT(dev->devfn) != slot->device) |
| 1128 | continue; |
| 1129 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 1130 | dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1131 | max = pci_scan_bridge(bus, dev, max, pass); |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 1132 | if (pass && dev->subordinate) |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 1133 | pci_bus_size_bridges(dev->subordinate); |
| 1134 | } |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 1138 | list_for_each (l, &slot->funcs) { |
| 1139 | func = list_entry(l, struct acpiphp_func, sibling); |
| 1140 | acpiphp_bus_add(func); |
| 1141 | } |
| 1142 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1143 | pci_bus_assign_resources(bus); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 1144 | acpiphp_sanitize_bus(bus); |
Satoru Takeuchi | 287af2f | 2006-09-12 10:12:16 -0700 | [diff] [blame] | 1145 | acpiphp_set_hpp_values(slot->bridge->handle, bus); |
Satoru Takeuchi | 9b1d19e | 2006-09-12 10:15:10 -0700 | [diff] [blame] | 1146 | list_for_each_entry(func, &slot->funcs, sibling) |
| 1147 | acpiphp_configure_ioapics(func->handle); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 1148 | pci_enable_bridges(bus); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1149 | pci_bus_add_devices(bus); |
| 1150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | /* associate pci_dev to our representation */ |
| 1152 | list_for_each (l, &slot->funcs) { |
| 1153 | func = list_entry(l, struct acpiphp_func, sibling); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1154 | func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | func->function)); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 1156 | if (!func->pci_dev) |
| 1157 | continue; |
| 1158 | |
| 1159 | if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE && |
| 1160 | func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) |
| 1161 | continue; |
| 1162 | |
| 1163 | status = find_p2p_bridge(func->handle, (u32)1, bus, NULL); |
| 1164 | if (ACPI_FAILURE(status)) |
| 1165 | warn("find_p2p_bridge failed (error code = 0x%x)\n", |
| 1166 | status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | slot->flags |= SLOT_ENABLED; |
| 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | err_exit: |
| 1172 | return retval; |
| 1173 | } |
| 1174 | |
Satoru Takeuchi | d5cdb67 | 2006-09-12 10:19:00 -0700 | [diff] [blame] | 1175 | static void disable_bridges(struct pci_bus *bus) |
| 1176 | { |
| 1177 | struct pci_dev *dev; |
| 1178 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1179 | if (dev->subordinate) { |
| 1180 | disable_bridges(dev->subordinate); |
| 1181 | pci_disable_device(dev); |
| 1182 | } |
| 1183 | } |
| 1184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
| 1186 | /** |
| 1187 | * disable_device - disable a slot |
| 1188 | */ |
| 1189 | static int disable_device(struct acpiphp_slot *slot) |
| 1190 | { |
| 1191 | int retval = 0; |
| 1192 | struct acpiphp_func *func; |
| 1193 | struct list_head *l; |
| 1194 | |
| 1195 | /* is this slot already disabled? */ |
| 1196 | if (!(slot->flags & SLOT_ENABLED)) |
| 1197 | goto err_exit; |
| 1198 | |
| 1199 | list_for_each (l, &slot->funcs) { |
| 1200 | func = list_entry(l, struct acpiphp_func, sibling); |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 1201 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 1202 | if (func->bridge) { |
| 1203 | /* cleanup p2p bridges under this P2P bridge */ |
| 1204 | cleanup_p2p_bridge(func->bridge->handle, |
| 1205 | (u32)1, NULL, NULL); |
| 1206 | func->bridge = NULL; |
| 1207 | } |
| 1208 | |
Satoru Takeuchi | d5cdb67 | 2006-09-12 10:19:00 -0700 | [diff] [blame] | 1209 | if (func->pci_dev) { |
Satoru Takeuchi | 0dad351 | 2006-09-12 10:17:46 -0700 | [diff] [blame] | 1210 | pci_stop_bus_device(func->pci_dev); |
Satoru Takeuchi | d5cdb67 | 2006-09-12 10:19:00 -0700 | [diff] [blame] | 1211 | if (func->pci_dev->subordinate) { |
| 1212 | disable_bridges(func->pci_dev->subordinate); |
| 1213 | pci_disable_device(func->pci_dev); |
| 1214 | } |
| 1215 | } |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 1216 | } |
Satoru Takeuchi | 0dad351 | 2006-09-12 10:17:46 -0700 | [diff] [blame] | 1217 | |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 1218 | list_for_each (l, &slot->funcs) { |
| 1219 | func = list_entry(l, struct acpiphp_func, sibling); |
| 1220 | |
| 1221 | acpiphp_unconfigure_ioapics(func->handle); |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 1222 | acpiphp_bus_trim(func->handle); |
| 1223 | /* try to remove anyway. |
| 1224 | * acpiphp_bus_add might have been failed */ |
| 1225 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1226 | if (!func->pci_dev) |
| 1227 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1229 | pci_remove_bus_device(func->pci_dev); |
| 1230 | pci_dev_put(func->pci_dev); |
| 1231 | func->pci_dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | slot->flags &= (~SLOT_ENABLED); |
| 1235 | |
| 1236 | err_exit: |
| 1237 | return retval; |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | /** |
| 1242 | * get_slot_status - get ACPI slot status |
| 1243 | * |
| 1244 | * if a slot has _STA for each function and if any one of them |
| 1245 | * returned non-zero status, return it |
| 1246 | * |
| 1247 | * if a slot doesn't have _STA and if any one of its functions' |
| 1248 | * configuration space is configured, return 0x0f as a _STA |
| 1249 | * |
| 1250 | * otherwise return 0 |
| 1251 | */ |
| 1252 | static unsigned int get_slot_status(struct acpiphp_slot *slot) |
| 1253 | { |
| 1254 | acpi_status status; |
| 1255 | unsigned long sta = 0; |
| 1256 | u32 dvid; |
| 1257 | struct list_head *l; |
| 1258 | struct acpiphp_func *func; |
| 1259 | |
| 1260 | list_for_each (l, &slot->funcs) { |
| 1261 | func = list_entry(l, struct acpiphp_func, sibling); |
| 1262 | |
| 1263 | if (func->flags & FUNC_HAS_STA) { |
| 1264 | status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta); |
| 1265 | if (ACPI_SUCCESS(status) && sta) |
| 1266 | break; |
| 1267 | } else { |
| 1268 | pci_bus_read_config_dword(slot->bridge->pci_bus, |
| 1269 | PCI_DEVFN(slot->device, |
| 1270 | func->function), |
| 1271 | PCI_VENDOR_ID, &dvid); |
| 1272 | if (dvid != 0xffffffff) { |
| 1273 | sta = ACPI_STA_ALL; |
| 1274 | break; |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | return (unsigned int)sta; |
| 1280 | } |
| 1281 | |
| 1282 | /** |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1283 | * acpiphp_eject_slot - physically eject the slot |
| 1284 | */ |
Gary Hade | bfceafc | 2007-07-05 11:10:46 -0700 | [diff] [blame] | 1285 | int acpiphp_eject_slot(struct acpiphp_slot *slot) |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1286 | { |
| 1287 | acpi_status status; |
| 1288 | struct acpiphp_func *func; |
| 1289 | struct list_head *l; |
| 1290 | struct acpi_object_list arg_list; |
| 1291 | union acpi_object arg; |
| 1292 | |
| 1293 | list_for_each (l, &slot->funcs) { |
| 1294 | func = list_entry(l, struct acpiphp_func, sibling); |
| 1295 | |
| 1296 | /* We don't want to call _EJ0 on non-existing functions. */ |
| 1297 | if ((func->flags & FUNC_HAS_EJ0)) { |
| 1298 | /* _EJ0 method take one argument */ |
| 1299 | arg_list.count = 1; |
| 1300 | arg_list.pointer = &arg; |
| 1301 | arg.type = ACPI_TYPE_INTEGER; |
| 1302 | arg.integer.value = 1; |
| 1303 | |
| 1304 | status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL); |
| 1305 | if (ACPI_FAILURE(status)) { |
| 1306 | warn("%s: _EJ0 failed\n", __FUNCTION__); |
| 1307 | return -1; |
| 1308 | } else |
| 1309 | break; |
| 1310 | } |
| 1311 | } |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | * acpiphp_check_bridge - re-enumerate devices |
| 1317 | * |
| 1318 | * Iterate over all slots under this bridge and make sure that if a |
| 1319 | * card is present they are enabled, and if not they are disabled. |
| 1320 | */ |
| 1321 | static int acpiphp_check_bridge(struct acpiphp_bridge *bridge) |
| 1322 | { |
| 1323 | struct acpiphp_slot *slot; |
| 1324 | int retval = 0; |
| 1325 | int enabled, disabled; |
| 1326 | |
| 1327 | enabled = disabled = 0; |
| 1328 | |
| 1329 | for (slot = bridge->slots; slot; slot = slot->next) { |
| 1330 | unsigned int status = get_slot_status(slot); |
| 1331 | if (slot->flags & SLOT_ENABLED) { |
| 1332 | if (status == ACPI_STA_ALL) |
| 1333 | continue; |
| 1334 | retval = acpiphp_disable_slot(slot); |
| 1335 | if (retval) { |
| 1336 | err("Error occurred in disabling\n"); |
| 1337 | goto err_exit; |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1338 | } else { |
| 1339 | acpiphp_eject_slot(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | } |
| 1341 | disabled++; |
| 1342 | } else { |
| 1343 | if (status != ACPI_STA_ALL) |
| 1344 | continue; |
| 1345 | retval = acpiphp_enable_slot(slot); |
| 1346 | if (retval) { |
| 1347 | err("Error occurred in enabling\n"); |
| 1348 | goto err_exit; |
| 1349 | } |
| 1350 | enabled++; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled); |
| 1355 | |
| 1356 | err_exit: |
| 1357 | return retval; |
| 1358 | } |
| 1359 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1360 | static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge) |
| 1361 | { |
| 1362 | u16 pci_cmd, pci_bctl; |
| 1363 | struct pci_dev *cdev; |
| 1364 | |
| 1365 | /* Program hpp values for this device */ |
| 1366 | if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL || |
| 1367 | (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE && |
| 1368 | (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI))) |
| 1369 | return; |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 1370 | |
Gary Hade | 9ef2241 | 2007-07-05 11:10:47 -0700 | [diff] [blame^] | 1371 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) |
| 1372 | return; |
| 1373 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1374 | pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 1375 | bridge->hpp.t0->cache_line_size); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1376 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 1377 | bridge->hpp.t0->latency_timer); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1378 | pci_read_config_word(dev, PCI_COMMAND, &pci_cmd); |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 1379 | if (bridge->hpp.t0->enable_serr) |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1380 | pci_cmd |= PCI_COMMAND_SERR; |
| 1381 | else |
| 1382 | pci_cmd &= ~PCI_COMMAND_SERR; |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 1383 | if (bridge->hpp.t0->enable_perr) |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1384 | pci_cmd |= PCI_COMMAND_PARITY; |
| 1385 | else |
| 1386 | pci_cmd &= ~PCI_COMMAND_PARITY; |
| 1387 | pci_write_config_word(dev, PCI_COMMAND, pci_cmd); |
| 1388 | |
| 1389 | /* Program bridge control value and child devices */ |
| 1390 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) { |
| 1391 | pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 1392 | bridge->hpp.t0->latency_timer); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1393 | pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl); |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 1394 | if (bridge->hpp.t0->enable_serr) |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1395 | pci_bctl |= PCI_BRIDGE_CTL_SERR; |
| 1396 | else |
| 1397 | pci_bctl &= ~PCI_BRIDGE_CTL_SERR; |
Kenji Kaneshige | e22b7350 | 2006-05-02 10:57:14 +0900 | [diff] [blame] | 1398 | if (bridge->hpp.t0->enable_perr) |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1399 | pci_bctl |= PCI_BRIDGE_CTL_PARITY; |
| 1400 | else |
| 1401 | pci_bctl &= ~PCI_BRIDGE_CTL_PARITY; |
| 1402 | pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl); |
| 1403 | if (dev->subordinate) { |
| 1404 | list_for_each_entry(cdev, &dev->subordinate->devices, |
| 1405 | bus_list) |
| 1406 | program_hpp(cdev, bridge); |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus) |
| 1412 | { |
| 1413 | struct acpiphp_bridge bridge; |
| 1414 | struct pci_dev *dev; |
| 1415 | |
| 1416 | memset(&bridge, 0, sizeof(bridge)); |
| 1417 | bridge.handle = handle; |
Kenji Kaneshige | 7430e34 | 2006-05-02 10:54:50 +0900 | [diff] [blame] | 1418 | bridge.pci_bus = bus; |
Kristen Accardi | 783c49f | 2006-03-03 10:16:05 -0800 | [diff] [blame] | 1419 | bridge.pci_dev = bus->self; |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1420 | decode_hpp(&bridge); |
| 1421 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 1422 | program_hpp(dev, &bridge); |
| 1423 | |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Remove devices for which we could not assign resources, call |
| 1428 | * arch specific code to fix-up the bus |
| 1429 | */ |
| 1430 | static void acpiphp_sanitize_bus(struct pci_bus *bus) |
| 1431 | { |
| 1432 | struct pci_dev *dev; |
| 1433 | int i; |
| 1434 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM; |
| 1435 | |
| 1436 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1437 | for (i=0; i<PCI_BRIDGE_RESOURCES; i++) { |
| 1438 | struct resource *res = &dev->resource[i]; |
| 1439 | if ((res->flags & type_mask) && !res->start && |
| 1440 | res->end) { |
| 1441 | /* Could not assign a required resources |
| 1442 | * for this device, remove it */ |
| 1443 | pci_remove_bus_device(dev); |
| 1444 | break; |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | /* Program resources in newly inserted bridge */ |
| 1451 | static int acpiphp_configure_bridge (acpi_handle handle) |
| 1452 | { |
| 1453 | struct acpi_pci_id pci_id; |
| 1454 | struct pci_bus *bus; |
| 1455 | |
| 1456 | if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) { |
| 1457 | err("cannot get PCI domain and bus number for bridge\n"); |
| 1458 | return -EINVAL; |
| 1459 | } |
| 1460 | bus = pci_find_bus(pci_id.segment, pci_id.bus); |
| 1461 | if (!bus) { |
| 1462 | err("cannot find bus %d:%d\n", |
| 1463 | pci_id.segment, pci_id.bus); |
| 1464 | return -EINVAL; |
| 1465 | } |
| 1466 | |
| 1467 | pci_bus_size_bridges(bus); |
| 1468 | pci_bus_assign_resources(bus); |
| 1469 | acpiphp_sanitize_bus(bus); |
| 1470 | acpiphp_set_hpp_values(handle, bus); |
| 1471 | pci_enable_bridges(bus); |
Kenji Kaneshige | a0d399a | 2005-04-28 00:25:59 -0700 | [diff] [blame] | 1472 | acpiphp_configure_ioapics(handle); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
| 1475 | |
| 1476 | static void handle_bridge_insertion(acpi_handle handle, u32 type) |
| 1477 | { |
| 1478 | struct acpi_device *device, *pdevice; |
| 1479 | acpi_handle phandle; |
| 1480 | |
| 1481 | if ((type != ACPI_NOTIFY_BUS_CHECK) && |
| 1482 | (type != ACPI_NOTIFY_DEVICE_CHECK)) { |
| 1483 | err("unexpected notification type %d\n", type); |
| 1484 | return; |
| 1485 | } |
| 1486 | |
| 1487 | acpi_get_parent(handle, &phandle); |
| 1488 | if (acpi_bus_get_device(phandle, &pdevice)) { |
| 1489 | dbg("no parent device, assuming NULL\n"); |
| 1490 | pdevice = NULL; |
| 1491 | } |
| 1492 | if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) { |
| 1493 | err("cannot add bridge to acpi list\n"); |
| 1494 | return; |
| 1495 | } |
| 1496 | if (!acpiphp_configure_bridge(handle) && |
| 1497 | !acpi_bus_start(device)) |
| 1498 | add_bridge(handle); |
| 1499 | else |
| 1500 | err("cannot configure and start bridge\n"); |
| 1501 | |
| 1502 | } |
| 1503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | /* |
| 1505 | * ACPI event handlers |
| 1506 | */ |
| 1507 | |
| 1508 | /** |
| 1509 | * handle_hotplug_event_bridge - handle ACPI event on bridges |
| 1510 | * |
| 1511 | * @handle: Notify()'ed acpi_handle |
| 1512 | * @type: Notify code |
| 1513 | * @context: pointer to acpiphp_bridge structure |
| 1514 | * |
| 1515 | * handles ACPI event notification on {host,p2p} bridges |
| 1516 | * |
| 1517 | */ |
| 1518 | static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context) |
| 1519 | { |
| 1520 | struct acpiphp_bridge *bridge; |
| 1521 | char objname[64]; |
| 1522 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 1523 | .pointer = objname }; |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1524 | struct acpi_device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1526 | if (acpi_bus_get_device(handle, &device)) { |
| 1527 | /* This bridge must have just been physically inserted */ |
| 1528 | handle_bridge_insertion(handle, type); |
| 1529 | return; |
| 1530 | } |
| 1531 | |
| 1532 | bridge = acpiphp_handle_to_bridge(handle); |
| 1533 | if (!bridge) { |
| 1534 | err("cannot get bridge info\n"); |
| 1535 | return; |
| 1536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
| 1538 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1539 | |
| 1540 | switch (type) { |
| 1541 | case ACPI_NOTIFY_BUS_CHECK: |
| 1542 | /* bus re-enumerate */ |
| 1543 | dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname); |
| 1544 | acpiphp_check_bridge(bridge); |
| 1545 | break; |
| 1546 | |
| 1547 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 1548 | /* device check */ |
| 1549 | dbg("%s: Device check notify on %s\n", __FUNCTION__, objname); |
| 1550 | acpiphp_check_bridge(bridge); |
| 1551 | break; |
| 1552 | |
| 1553 | case ACPI_NOTIFY_DEVICE_WAKE: |
| 1554 | /* wake event */ |
| 1555 | dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname); |
| 1556 | break; |
| 1557 | |
| 1558 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 1559 | /* request device eject */ |
| 1560 | dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 1561 | if ((bridge->type != BRIDGE_TYPE_HOST) && |
| 1562 | (bridge->flags & BRIDGE_HAS_EJ0)) { |
| 1563 | struct acpiphp_slot *slot; |
| 1564 | slot = bridge->func->slot; |
| 1565 | if (!acpiphp_disable_slot(slot)) |
| 1566 | acpiphp_eject_slot(slot); |
| 1567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | break; |
| 1569 | |
| 1570 | case ACPI_NOTIFY_FREQUENCY_MISMATCH: |
| 1571 | printk(KERN_ERR "Device %s cannot be configured due" |
| 1572 | " to a frequency mismatch\n", objname); |
| 1573 | break; |
| 1574 | |
| 1575 | case ACPI_NOTIFY_BUS_MODE_MISMATCH: |
| 1576 | printk(KERN_ERR "Device %s cannot be configured due" |
| 1577 | " to a bus mode mismatch\n", objname); |
| 1578 | break; |
| 1579 | |
| 1580 | case ACPI_NOTIFY_POWER_FAULT: |
| 1581 | printk(KERN_ERR "Device %s has suffered a power fault\n", |
| 1582 | objname); |
| 1583 | break; |
| 1584 | |
| 1585 | default: |
| 1586 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); |
| 1587 | break; |
| 1588 | } |
| 1589 | } |
| 1590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | /** |
| 1592 | * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots) |
| 1593 | * |
| 1594 | * @handle: Notify()'ed acpi_handle |
| 1595 | * @type: Notify code |
| 1596 | * @context: pointer to acpiphp_func structure |
| 1597 | * |
| 1598 | * handles ACPI event notification on slots |
| 1599 | * |
| 1600 | */ |
Len Brown | 2b85e13 | 2006-06-27 01:50:14 -0400 | [diff] [blame] | 1601 | static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | { |
| 1603 | struct acpiphp_func *func; |
| 1604 | char objname[64]; |
| 1605 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 1606 | .pointer = objname }; |
| 1607 | |
| 1608 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1609 | |
| 1610 | func = (struct acpiphp_func *)context; |
| 1611 | |
| 1612 | switch (type) { |
| 1613 | case ACPI_NOTIFY_BUS_CHECK: |
| 1614 | /* bus re-enumerate */ |
| 1615 | dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname); |
| 1616 | acpiphp_enable_slot(func->slot); |
| 1617 | break; |
| 1618 | |
| 1619 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 1620 | /* device check : re-enumerate from parent bus */ |
| 1621 | dbg("%s: Device check notify on %s\n", __FUNCTION__, objname); |
| 1622 | acpiphp_check_bridge(func->slot->bridge); |
| 1623 | break; |
| 1624 | |
| 1625 | case ACPI_NOTIFY_DEVICE_WAKE: |
| 1626 | /* wake event */ |
| 1627 | dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname); |
| 1628 | break; |
| 1629 | |
| 1630 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 1631 | /* request device eject */ |
| 1632 | dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname); |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1633 | if (!(acpiphp_disable_slot(func->slot))) |
| 1634 | acpiphp_eject_slot(func->slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | break; |
| 1636 | |
| 1637 | default: |
| 1638 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); |
| 1639 | break; |
| 1640 | } |
| 1641 | } |
| 1642 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1643 | |
| 1644 | static acpi_status |
| 1645 | find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 1646 | { |
| 1647 | int *count = (int *)context; |
| 1648 | |
Kristen Accardi | 783c49f | 2006-03-03 10:16:05 -0800 | [diff] [blame] | 1649 | if (acpi_root_bridge(handle)) { |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1650 | acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
| 1651 | handle_hotplug_event_bridge, NULL); |
| 1652 | (*count)++; |
| 1653 | } |
| 1654 | return AE_OK ; |
| 1655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
| 1657 | static struct acpi_pci_driver acpi_pci_hp_driver = { |
| 1658 | .add = add_bridge, |
| 1659 | .remove = remove_bridge, |
| 1660 | }; |
| 1661 | |
| 1662 | /** |
| 1663 | * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures |
| 1664 | * |
| 1665 | */ |
| 1666 | int __init acpiphp_glue_init(void) |
| 1667 | { |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1668 | int num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1670 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
| 1671 | ACPI_UINT32_MAX, find_root_bridges, &num, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | |
| 1673 | if (num <= 0) |
| 1674 | return -1; |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1675 | else |
| 1676 | acpi_pci_register_driver(&acpi_pci_hp_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | |
| 1682 | /** |
| 1683 | * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures |
| 1684 | * |
| 1685 | * This function frees all data allocated in acpiphp_glue_init() |
| 1686 | */ |
Kristen Carlson Accardi | 031f30d | 2006-12-16 15:26:04 -0800 | [diff] [blame] | 1687 | void acpiphp_glue_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | acpi_pci_unregister_driver(&acpi_pci_hp_driver); |
| 1690 | } |
| 1691 | |
| 1692 | |
| 1693 | /** |
| 1694 | * acpiphp_get_num_slots - count number of slots in a system |
| 1695 | */ |
| 1696 | int __init acpiphp_get_num_slots(void) |
| 1697 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | struct acpiphp_bridge *bridge; |
Akinobu Mita | 467c442 | 2006-10-30 13:07:58 -0800 | [diff] [blame] | 1699 | int num_slots = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
Akinobu Mita | 467c442 | 2006-10-30 13:07:58 -0800 | [diff] [blame] | 1701 | list_for_each_entry (bridge, &bridge_list, list) { |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1702 | dbg("Bus %04x:%02x has %d slot%s\n", |
| 1703 | pci_domain_nr(bridge->pci_bus), |
| 1704 | bridge->pci_bus->number, bridge->nr_slots, |
| 1705 | bridge->nr_slots == 1 ? "" : "s"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | num_slots += bridge->nr_slots; |
| 1707 | } |
| 1708 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1709 | dbg("Total %d slots\n", num_slots); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | return num_slots; |
| 1711 | } |
| 1712 | |
| 1713 | |
| 1714 | #if 0 |
| 1715 | /** |
| 1716 | * acpiphp_for_each_slot - call function for each slot |
| 1717 | * @fn: callback function |
| 1718 | * @data: context to be passed to callback function |
| 1719 | * |
| 1720 | */ |
| 1721 | static int acpiphp_for_each_slot(acpiphp_callback fn, void *data) |
| 1722 | { |
| 1723 | struct list_head *node; |
| 1724 | struct acpiphp_bridge *bridge; |
| 1725 | struct acpiphp_slot *slot; |
| 1726 | int retval = 0; |
| 1727 | |
| 1728 | list_for_each (node, &bridge_list) { |
| 1729 | bridge = (struct acpiphp_bridge *)node; |
| 1730 | for (slot = bridge->slots; slot; slot = slot->next) { |
| 1731 | retval = fn(slot, data); |
| 1732 | if (!retval) |
| 1733 | goto err_exit; |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | err_exit: |
| 1738 | return retval; |
| 1739 | } |
| 1740 | #endif |
| 1741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | |
| 1743 | /** |
| 1744 | * acpiphp_enable_slot - power on slot |
| 1745 | */ |
| 1746 | int acpiphp_enable_slot(struct acpiphp_slot *slot) |
| 1747 | { |
| 1748 | int retval; |
| 1749 | |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1750 | mutex_lock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | |
| 1752 | /* wake up all functions */ |
| 1753 | retval = power_on_slot(slot); |
| 1754 | if (retval) |
| 1755 | goto err_exit; |
| 1756 | |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1757 | if (get_slot_status(slot) == ACPI_STA_ALL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | /* configure all functions */ |
| 1759 | retval = enable_device(slot); |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1760 | if (retval) |
| 1761 | power_off_slot(slot); |
| 1762 | } else { |
| 1763 | dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__); |
| 1764 | power_off_slot(slot); |
| 1765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
| 1767 | err_exit: |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1768 | mutex_unlock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | return retval; |
| 1770 | } |
| 1771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | /** |
| 1773 | * acpiphp_disable_slot - power off slot |
| 1774 | */ |
| 1775 | int acpiphp_disable_slot(struct acpiphp_slot *slot) |
| 1776 | { |
| 1777 | int retval = 0; |
| 1778 | |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1779 | mutex_lock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | |
| 1781 | /* unconfigure all functions */ |
| 1782 | retval = disable_device(slot); |
| 1783 | if (retval) |
| 1784 | goto err_exit; |
| 1785 | |
| 1786 | /* power off all functions */ |
| 1787 | retval = power_off_slot(slot); |
| 1788 | if (retval) |
| 1789 | goto err_exit; |
| 1790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | err_exit: |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1792 | mutex_unlock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | return retval; |
| 1794 | } |
| 1795 | |
| 1796 | |
| 1797 | /* |
| 1798 | * slot enabled: 1 |
| 1799 | * slot disabled: 0 |
| 1800 | */ |
| 1801 | u8 acpiphp_get_power_status(struct acpiphp_slot *slot) |
| 1802 | { |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1803 | return (slot->flags & SLOT_POWEREDON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | |
| 1807 | /* |
MUNEDA Takahiro | 35ae61a | 2006-10-25 11:44:57 -0700 | [diff] [blame] | 1808 | * latch open: 1 |
| 1809 | * latch closed: 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | */ |
| 1811 | u8 acpiphp_get_latch_status(struct acpiphp_slot *slot) |
| 1812 | { |
| 1813 | unsigned int sta; |
| 1814 | |
| 1815 | sta = get_slot_status(slot); |
| 1816 | |
MUNEDA Takahiro | 35ae61a | 2006-10-25 11:44:57 -0700 | [diff] [blame] | 1817 | return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | /* |
| 1822 | * adapter presence : 1 |
| 1823 | * absence : 0 |
| 1824 | */ |
| 1825 | u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot) |
| 1826 | { |
| 1827 | unsigned int sta; |
| 1828 | |
| 1829 | sta = get_slot_status(slot); |
| 1830 | |
| 1831 | return (sta == 0) ? 0 : 1; |
| 1832 | } |
| 1833 | |
| 1834 | |
| 1835 | /* |
| 1836 | * pci address (seg/bus/dev) |
| 1837 | */ |
| 1838 | u32 acpiphp_get_address(struct acpiphp_slot *slot) |
| 1839 | { |
| 1840 | u32 address; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1841 | struct pci_bus *pci_bus = slot->bridge->pci_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1843 | address = (pci_domain_nr(pci_bus) << 16) | |
| 1844 | (pci_bus->number << 8) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | slot->device; |
| 1846 | |
| 1847 | return address; |
| 1848 | } |