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