2 * PCI Hotplug Driver for PowerPC PowerNV platform.
4 * Copyright Gavin Shan, IBM Corporation 2016.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/libfdt.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/pci_hotplug.h>
18 #include <asm/pnv-pci.h>
19 #include <asm/ppc-pci.h>
21 #define DRIVER_VERSION "0.1"
22 #define DRIVER_AUTHOR "Gavin Shan, IBM Corporation"
23 #define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver"
25 struct pnv_php_event {
27 struct pnv_php_slot *php_slot;
28 struct work_struct work;
31 static LIST_HEAD(pnv_php_slot_list);
32 static DEFINE_SPINLOCK(pnv_php_lock);
34 static void pnv_php_register(struct device_node *dn);
35 static void pnv_php_unregister_one(struct device_node *dn);
36 static void pnv_php_unregister(struct device_node *dn);
38 static void pnv_php_disable_irq(struct pnv_php_slot *php_slot)
40 struct pci_dev *pdev = php_slot->pdev;
43 if (php_slot->irq > 0) {
44 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
45 ctrl &= ~(PCI_EXP_SLTCTL_HPIE |
47 PCI_EXP_SLTCTL_DLLSCE);
48 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
50 free_irq(php_slot->irq, php_slot);
55 destroy_workqueue(php_slot->wq);
59 if (pdev->msix_enabled)
60 pci_disable_msix(pdev);
61 else if (pdev->msi_enabled)
62 pci_disable_msi(pdev);
65 static void pnv_php_free_slot(struct kref *kref)
67 struct pnv_php_slot *php_slot = container_of(kref,
68 struct pnv_php_slot, kref);
70 WARN_ON(!list_empty(&php_slot->children));
71 pnv_php_disable_irq(php_slot);
72 kfree(php_slot->name);
76 static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot)
79 if (WARN_ON(!php_slot))
82 kref_put(&php_slot->kref, pnv_php_free_slot);
85 static struct pnv_php_slot *pnv_php_match(struct device_node *dn,
86 struct pnv_php_slot *php_slot)
88 struct pnv_php_slot *target, *tmp;
90 if (php_slot->dn == dn) {
91 kref_get(&php_slot->kref);
95 list_for_each_entry(tmp, &php_slot->children, link) {
96 target = pnv_php_match(dn, tmp);
104 struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn)
106 struct pnv_php_slot *php_slot, *tmp;
109 spin_lock_irqsave(&pnv_php_lock, flags);
110 list_for_each_entry(tmp, &pnv_php_slot_list, link) {
111 php_slot = pnv_php_match(dn, tmp);
113 spin_unlock_irqrestore(&pnv_php_lock, flags);
117 spin_unlock_irqrestore(&pnv_php_lock, flags);
121 EXPORT_SYMBOL_GPL(pnv_php_find_slot);
124 * Remove pdn for all children of the indicated device node.
125 * The function should remove pdn in a depth-first manner.
127 static void pnv_php_rmv_pdns(struct device_node *dn)
129 struct device_node *child;
131 for_each_child_of_node(dn, child) {
132 pnv_php_rmv_pdns(child);
134 pci_remove_device_node_info(child);
139 * Detach all child nodes of the indicated device nodes. The
140 * function should handle device nodes in depth-first manner.
142 * We should not invoke of_node_release() as the memory for
143 * individual device node is part of large memory block. The
144 * large block is allocated from memblock (system bootup) or
145 * kmalloc() when unflattening the device tree by OF changeset.
146 * We can not free the large block allocated from memblock. For
147 * later case, it should be released at once.
149 static void pnv_php_detach_device_nodes(struct device_node *parent)
151 struct device_node *dn;
154 for_each_child_of_node(parent, dn) {
155 pnv_php_detach_device_nodes(dn);
158 refcount = atomic_read(&dn->kobj.kref.refcount);
160 pr_warn("Invalid refcount %d on <%s>\n",
161 refcount, of_node_full_name(dn));
167 static void pnv_php_rmv_devtree(struct pnv_php_slot *php_slot)
169 pnv_php_rmv_pdns(php_slot->dn);
172 * Decrease the refcount if the device nodes were created
173 * through OF changeset before detaching them.
176 of_changeset_destroy(&php_slot->ocs);
177 pnv_php_detach_device_nodes(php_slot->dn);
181 kfree(php_slot->fdt);
183 php_slot->dn->child = NULL;
184 php_slot->fdt = NULL;
189 * As the nodes in OF changeset are applied in reverse order, we
190 * need revert the nodes in advance so that we have correct node
191 * order after the changeset is applied.
193 static void pnv_php_reverse_nodes(struct device_node *parent)
195 struct device_node *child, *next;
198 for_each_child_of_node(parent, child)
199 pnv_php_reverse_nodes(child);
201 /* Reverse the nodes in the child list */
202 child = parent->child;
203 parent->child = NULL;
205 next = child->sibling;
207 child->sibling = parent->child;
208 parent->child = child;
213 static int pnv_php_populate_changeset(struct of_changeset *ocs,
214 struct device_node *dn)
216 struct device_node *child;
219 for_each_child_of_node(dn, child) {
220 ret = of_changeset_attach_node(ocs, child);
224 ret = pnv_php_populate_changeset(ocs, child);
232 static void *pnv_php_add_one_pdn(struct device_node *dn, void *data)
234 struct pci_controller *hose = (struct pci_controller *)data;
237 pdn = pci_add_device_node_info(hose, dn);
239 return ERR_PTR(-ENOMEM);
244 static void pnv_php_add_pdns(struct pnv_php_slot *slot)
246 struct pci_controller *hose = pci_bus_to_host(slot->bus);
248 pci_traverse_device_nodes(slot->dn, pnv_php_add_one_pdn, hose);
251 static int pnv_php_add_devtree(struct pnv_php_slot *php_slot)
253 void *fdt, *fdt1, *dt;
256 /* We don't know the FDT blob size. We try to get it through
257 * maximal memory chunk and then copy it to another chunk that
258 * fits the real size.
260 fdt1 = kzalloc(0x10000, GFP_KERNEL);
263 dev_warn(&php_slot->pdev->dev, "Cannot alloc FDT blob\n");
267 ret = pnv_pci_get_device_tree(php_slot->dn->phandle, fdt1, 0x10000);
269 dev_warn(&php_slot->pdev->dev, "Error %d getting FDT blob\n",
274 fdt = kzalloc(fdt_totalsize(fdt1), GFP_KERNEL);
277 dev_warn(&php_slot->pdev->dev, "Cannot %d bytes memory\n",
278 fdt_totalsize(fdt1));
282 /* Unflatten device tree blob */
283 memcpy(fdt, fdt1, fdt_totalsize(fdt1));
284 dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL);
287 dev_warn(&php_slot->pdev->dev, "Cannot unflatten FDT\n");
291 /* Initialize and apply the changeset */
292 of_changeset_init(&php_slot->ocs);
293 pnv_php_reverse_nodes(php_slot->dn);
294 ret = pnv_php_populate_changeset(&php_slot->ocs, php_slot->dn);
296 pnv_php_reverse_nodes(php_slot->dn);
297 dev_warn(&php_slot->pdev->dev, "Error %d populating changeset\n",
302 php_slot->dn->child = NULL;
303 ret = of_changeset_apply(&php_slot->ocs);
305 dev_warn(&php_slot->pdev->dev, "Error %d applying changeset\n",
307 goto destroy_changeset;
310 /* Add device node firmware data */
311 pnv_php_add_pdns(php_slot);
318 of_changeset_destroy(&php_slot->ocs);
321 php_slot->dn->child = NULL;
330 int pnv_php_set_slot_power_state(struct hotplug_slot *slot,
333 struct pnv_php_slot *php_slot = slot->private;
337 ret = pnv_pci_set_power_state(php_slot->id, state, &msg);
339 if (be64_to_cpu(msg.params[1]) != php_slot->dn->phandle ||
340 be64_to_cpu(msg.params[2]) != state ||
341 be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) {
342 dev_warn(&php_slot->pdev->dev, "Wrong msg (%lld, %lld, %lld)\n",
343 be64_to_cpu(msg.params[1]),
344 be64_to_cpu(msg.params[2]),
345 be64_to_cpu(msg.params[3]));
348 } else if (ret < 0) {
349 dev_warn(&php_slot->pdev->dev, "Error %d powering %s\n",
350 ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off");
354 if (state == OPAL_PCI_SLOT_POWER_OFF || state == OPAL_PCI_SLOT_OFFLINE)
355 pnv_php_rmv_devtree(php_slot);
357 ret = pnv_php_add_devtree(php_slot);
361 EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state);
363 static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state)
365 struct pnv_php_slot *php_slot = slot->private;
366 uint8_t power_state = OPAL_PCI_SLOT_POWER_ON;
370 * Retrieve power status from firmware. If we fail
371 * getting that, the power status fails back to
374 ret = pnv_pci_get_power_state(php_slot->id, &power_state);
376 dev_warn(&php_slot->pdev->dev, "Error %d getting power status\n",
379 *state = power_state;
380 slot->info->power_status = power_state;
386 static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state)
388 struct pnv_php_slot *php_slot = slot->private;
389 uint8_t presence = OPAL_PCI_SLOT_EMPTY;
393 * Retrieve presence status from firmware. If we can't
394 * get that, it will fail back to be empty.
396 ret = pnv_pci_get_presence_state(php_slot->id, &presence);
399 slot->info->adapter_status = presence;
402 dev_warn(&php_slot->pdev->dev, "Error %d getting presence\n",
409 static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state)
411 /* FIXME: Make it real once firmware supports it */
412 slot->info->attention_status = state;
417 static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
419 struct hotplug_slot *slot = &php_slot->slot;
420 uint8_t presence = OPAL_PCI_SLOT_EMPTY;
421 uint8_t power_status = OPAL_PCI_SLOT_POWER_ON;
424 /* Check if the slot has been configured */
425 if (php_slot->state != PNV_PHP_STATE_REGISTERED)
428 /* Retrieve slot presence status */
429 ret = pnv_php_get_adapter_state(slot, &presence);
433 /* Proceed if there have nothing behind the slot */
434 if (presence == OPAL_PCI_SLOT_EMPTY)
438 * If the power supply to the slot is off, we can't detect
439 * adapter presence state. That means we have to turn the
440 * slot on before going to probe slot's presence state.
442 * On the first time, we don't change the power status to
443 * boost system boot with assumption that the firmware
444 * supplies consistent slot power status: empty slot always
445 * has its power off and non-empty slot has its power on.
447 if (!php_slot->power_state_check) {
448 php_slot->power_state_check = true;
450 ret = pnv_php_get_power_state(slot, &power_status);
454 if (power_status != OPAL_PCI_SLOT_POWER_ON)
458 /* Check the power status. Scan the slot if it is already on */
459 ret = pnv_php_get_power_state(slot, &power_status);
463 if (power_status == OPAL_PCI_SLOT_POWER_ON)
466 /* Power is off, turn it on and then scan the slot */
467 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
472 if (presence == OPAL_PCI_SLOT_PRESENT) {
474 pci_lock_rescan_remove();
475 pci_hp_add_devices(php_slot->bus);
476 pci_unlock_rescan_remove();
479 /* Rescan for child hotpluggable slots */
480 php_slot->state = PNV_PHP_STATE_POPULATED;
482 pnv_php_register(php_slot->dn);
484 php_slot->state = PNV_PHP_STATE_POPULATED;
490 static int pnv_php_enable_slot(struct hotplug_slot *slot)
492 struct pnv_php_slot *php_slot = container_of(slot,
493 struct pnv_php_slot, slot);
495 return pnv_php_enable(php_slot, true);
498 static int pnv_php_disable_slot(struct hotplug_slot *slot)
500 struct pnv_php_slot *php_slot = slot->private;
503 if (php_slot->state != PNV_PHP_STATE_POPULATED)
506 /* Remove all devices behind the slot */
507 pci_lock_rescan_remove();
508 pci_hp_remove_devices(php_slot->bus);
509 pci_unlock_rescan_remove();
511 /* Detach the child hotpluggable slots */
512 pnv_php_unregister(php_slot->dn);
514 /* Notify firmware and remove device nodes */
515 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF);
517 php_slot->state = PNV_PHP_STATE_REGISTERED;
521 static struct hotplug_slot_ops php_slot_ops = {
522 .get_power_status = pnv_php_get_power_state,
523 .get_adapter_status = pnv_php_get_adapter_state,
524 .set_attention_status = pnv_php_set_attention_state,
525 .enable_slot = pnv_php_enable_slot,
526 .disable_slot = pnv_php_disable_slot,
529 static void pnv_php_release(struct hotplug_slot *slot)
531 struct pnv_php_slot *php_slot = slot->private;
534 /* Remove from global or child list */
535 spin_lock_irqsave(&pnv_php_lock, flags);
536 list_del(&php_slot->link);
537 spin_unlock_irqrestore(&pnv_php_lock, flags);
539 /* Detach from parent */
540 pnv_php_put_slot(php_slot);
541 pnv_php_put_slot(php_slot->parent);
544 static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
546 struct pnv_php_slot *php_slot;
552 ret = of_property_read_string(dn, "ibm,slot-label", &label);
556 if (pnv_pci_get_slot_id(dn, &id))
559 bus = pci_find_bus_by_node(dn);
563 php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL);
567 php_slot->name = kstrdup(label, GFP_KERNEL);
568 if (!php_slot->name) {
573 if (dn->child && PCI_DN(dn->child))
574 php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn);
576 php_slot->slot_no = -1; /* Placeholder slot */
578 kref_init(&php_slot->kref);
579 php_slot->state = PNV_PHP_STATE_INITIALIZED;
581 php_slot->pdev = bus->self;
584 php_slot->power_state_check = false;
585 php_slot->slot.ops = &php_slot_ops;
586 php_slot->slot.info = &php_slot->slot_info;
587 php_slot->slot.release = pnv_php_release;
588 php_slot->slot.private = php_slot;
590 INIT_LIST_HEAD(&php_slot->children);
591 INIT_LIST_HEAD(&php_slot->link);
596 static int pnv_php_register_slot(struct pnv_php_slot *php_slot)
598 struct pnv_php_slot *parent;
599 struct device_node *dn = php_slot->dn;
603 /* Check if the slot is registered or not */
604 parent = pnv_php_find_slot(php_slot->dn);
606 pnv_php_put_slot(parent);
610 /* Register PCI slot */
611 ret = pci_hp_register(&php_slot->slot, php_slot->bus,
612 php_slot->slot_no, php_slot->name);
614 dev_warn(&php_slot->pdev->dev, "Error %d registering slot\n",
619 /* Attach to the parent's child list or global list */
620 while ((dn = of_get_parent(dn))) {
626 parent = pnv_php_find_slot(dn);
635 spin_lock_irqsave(&pnv_php_lock, flags);
636 php_slot->parent = parent;
638 list_add_tail(&php_slot->link, &parent->children);
640 list_add_tail(&php_slot->link, &pnv_php_slot_list);
641 spin_unlock_irqrestore(&pnv_php_lock, flags);
643 php_slot->state = PNV_PHP_STATE_REGISTERED;
647 static int pnv_php_enable_msix(struct pnv_php_slot *php_slot)
649 struct pci_dev *pdev = php_slot->pdev;
650 struct msix_entry entry;
654 /* Get total number of MSIx entries */
655 nr_entries = pci_msix_vec_count(pdev);
659 /* Check hotplug MSIx entry is in range */
660 pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag);
661 entry.entry = (pcie_flag & PCI_EXP_FLAGS_IRQ) >> 9;
662 if (entry.entry >= nr_entries)
666 ret = pci_enable_msix_exact(pdev, &entry, 1);
668 dev_warn(&pdev->dev, "Error %d enabling MSIx\n", ret);
675 static void pnv_php_event_handler(struct work_struct *work)
677 struct pnv_php_event *event =
678 container_of(work, struct pnv_php_event, work);
679 struct pnv_php_slot *php_slot = event->php_slot;
682 pnv_php_enable_slot(&php_slot->slot);
684 pnv_php_disable_slot(&php_slot->slot);
689 static irqreturn_t pnv_php_interrupt(int irq, void *data)
691 struct pnv_php_slot *php_slot = data;
692 struct pci_dev *pchild, *pdev = php_slot->pdev;
693 struct eeh_dev *edev;
695 struct pnv_php_event *event;
702 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
703 sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
704 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
705 if (sts & PCI_EXP_SLTSTA_DLLSC) {
706 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts);
707 added = !!(lsts & PCI_EXP_LNKSTA_DLLLA);
708 } else if (sts & PCI_EXP_SLTSTA_PDC) {
709 ret = pnv_pci_get_presence_state(php_slot->id, &presence);
712 added = !!(presence == OPAL_PCI_SLOT_PRESENT);
717 /* Freeze the removed PE to avoid unexpected error reporting */
719 pchild = list_first_entry_or_null(&php_slot->bus->devices,
720 struct pci_dev, bus_list);
721 edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL;
722 pe = edev ? edev->pe : NULL;
724 eeh_serialize_lock(&flags);
725 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
726 eeh_serialize_unlock(flags);
727 eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
732 * The PE is left in frozen state if the event is missed. It's
733 * fine as the PCI devices (PE) aren't functional any more.
735 event = kzalloc(sizeof(*event), GFP_ATOMIC);
737 dev_warn(&pdev->dev, "PCI slot [%s] missed hotplug event 0x%04x\n",
738 php_slot->name, sts);
742 dev_info(&pdev->dev, "PCI slot [%s] %s (IRQ: %d)\n",
743 php_slot->name, added ? "added" : "removed", irq);
744 INIT_WORK(&event->work, pnv_php_event_handler);
745 event->added = added;
746 event->php_slot = php_slot;
747 queue_work(php_slot->wq, &event->work);
752 static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq)
754 struct pci_dev *pdev = php_slot->pdev;
758 /* Allocate workqueue */
759 php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name);
761 dev_warn(&pdev->dev, "Cannot alloc workqueue\n");
762 pnv_php_disable_irq(php_slot);
766 /* Clear pending interrupts */
767 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
768 sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
769 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
771 /* Request the interrupt */
772 ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED,
773 php_slot->name, php_slot);
775 pnv_php_disable_irq(php_slot);
776 dev_warn(&pdev->dev, "Error %d enabling IRQ %d\n", ret, irq);
780 /* Enable the interrupts */
781 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
782 ctrl |= (PCI_EXP_SLTCTL_HPIE |
783 PCI_EXP_SLTCTL_PDCE |
784 PCI_EXP_SLTCTL_DLLSCE);
785 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
787 /* The interrupt is initialized successfully when @irq is valid */
791 static void pnv_php_enable_irq(struct pnv_php_slot *php_slot)
793 struct pci_dev *pdev = php_slot->pdev;
796 ret = pci_enable_device(pdev);
798 dev_warn(&pdev->dev, "Error %d enabling device\n", ret);
802 pci_set_master(pdev);
804 /* Enable MSIx interrupt */
805 irq = pnv_php_enable_msix(php_slot);
807 pnv_php_init_irq(php_slot, irq);
812 * Use MSI if MSIx doesn't work. Fail back to legacy INTx
813 * if MSI doesn't work either
815 ret = pci_enable_msi(pdev);
816 if (!ret || pdev->irq) {
818 pnv_php_init_irq(php_slot, irq);
822 static int pnv_php_register_one(struct device_node *dn)
824 struct pnv_php_slot *php_slot;
828 /* Check if it's hotpluggable slot */
829 ret = of_property_read_u32(dn, "ibm,slot-pluggable", &prop32);
833 ret = of_property_read_u32(dn, "ibm,reset-by-firmware", &prop32);
837 php_slot = pnv_php_alloc_slot(dn);
841 ret = pnv_php_register_slot(php_slot);
845 ret = pnv_php_enable(php_slot, false);
847 goto unregister_slot;
849 /* Enable interrupt if the slot supports surprise hotplug */
850 ret = of_property_read_u32(dn, "ibm,slot-surprise-pluggable", &prop32);
852 pnv_php_enable_irq(php_slot);
857 pnv_php_unregister_one(php_slot->dn);
859 pnv_php_put_slot(php_slot);
863 static void pnv_php_register(struct device_node *dn)
865 struct device_node *child;
868 * The parent slots should be registered before their
871 for_each_child_of_node(dn, child) {
872 pnv_php_register_one(child);
873 pnv_php_register(child);
877 static void pnv_php_unregister_one(struct device_node *dn)
879 struct pnv_php_slot *php_slot;
881 php_slot = pnv_php_find_slot(dn);
885 php_slot->state = PNV_PHP_STATE_OFFLINE;
886 pnv_php_put_slot(php_slot);
887 pci_hp_deregister(&php_slot->slot);
890 static void pnv_php_unregister(struct device_node *dn)
892 struct device_node *child;
894 /* The child slots should go before their parent slots */
895 for_each_child_of_node(dn, child) {
896 pnv_php_unregister(child);
897 pnv_php_unregister_one(child);
901 static int __init pnv_php_init(void)
903 struct device_node *dn;
905 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
906 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
907 pnv_php_register(dn);
912 static void __exit pnv_php_exit(void)
914 struct device_node *dn;
916 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
917 pnv_php_unregister(dn);
920 module_init(pnv_php_init);
921 module_exit(pnv_php_exit);
923 MODULE_VERSION(DRIVER_VERSION);
924 MODULE_LICENSE("GPL v2");
925 MODULE_AUTHOR(DRIVER_AUTHOR);
926 MODULE_DESCRIPTION(DRIVER_DESC);