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 static LIST_HEAD(pnv_php_slot_list);
26 static DEFINE_SPINLOCK(pnv_php_lock);
28 static void pnv_php_register(struct device_node *dn);
29 static void pnv_php_unregister_one(struct device_node *dn);
30 static void pnv_php_unregister(struct device_node *dn);
32 static void pnv_php_free_slot(struct kref *kref)
34 struct pnv_php_slot *php_slot = container_of(kref,
35 struct pnv_php_slot, kref);
37 WARN_ON(!list_empty(&php_slot->children));
38 kfree(php_slot->name);
42 static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot)
45 if (WARN_ON(!php_slot))
48 kref_put(&php_slot->kref, pnv_php_free_slot);
51 static struct pnv_php_slot *pnv_php_match(struct device_node *dn,
52 struct pnv_php_slot *php_slot)
54 struct pnv_php_slot *target, *tmp;
56 if (php_slot->dn == dn) {
57 kref_get(&php_slot->kref);
61 list_for_each_entry(tmp, &php_slot->children, link) {
62 target = pnv_php_match(dn, tmp);
70 struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn)
72 struct pnv_php_slot *php_slot, *tmp;
75 spin_lock_irqsave(&pnv_php_lock, flags);
76 list_for_each_entry(tmp, &pnv_php_slot_list, link) {
77 php_slot = pnv_php_match(dn, tmp);
79 spin_unlock_irqrestore(&pnv_php_lock, flags);
83 spin_unlock_irqrestore(&pnv_php_lock, flags);
87 EXPORT_SYMBOL_GPL(pnv_php_find_slot);
90 * Remove pdn for all children of the indicated device node.
91 * The function should remove pdn in a depth-first manner.
93 static void pnv_php_rmv_pdns(struct device_node *dn)
95 struct device_node *child;
97 for_each_child_of_node(dn, child) {
98 pnv_php_rmv_pdns(child);
100 pci_remove_device_node_info(child);
105 * Detach all child nodes of the indicated device nodes. The
106 * function should handle device nodes in depth-first manner.
108 * We should not invoke of_node_release() as the memory for
109 * individual device node is part of large memory block. The
110 * large block is allocated from memblock (system bootup) or
111 * kmalloc() when unflattening the device tree by OF changeset.
112 * We can not free the large block allocated from memblock. For
113 * later case, it should be released at once.
115 static void pnv_php_detach_device_nodes(struct device_node *parent)
117 struct device_node *dn;
120 for_each_child_of_node(parent, dn) {
121 pnv_php_detach_device_nodes(dn);
124 refcount = atomic_read(&dn->kobj.kref.refcount);
125 if (unlikely(refcount != 1))
126 pr_warn("Invalid refcount %d on <%s>\n",
127 refcount, of_node_full_name(dn));
133 static void pnv_php_rmv_devtree(struct pnv_php_slot *php_slot)
135 pnv_php_rmv_pdns(php_slot->dn);
138 * Decrease the refcount if the device nodes were created
139 * through OF changeset before detaching them.
142 of_changeset_destroy(&php_slot->ocs);
143 pnv_php_detach_device_nodes(php_slot->dn);
147 kfree(php_slot->fdt);
149 php_slot->dn->child = NULL;
150 php_slot->fdt = NULL;
155 * As the nodes in OF changeset are applied in reverse order, we
156 * need revert the nodes in advance so that we have correct node
157 * order after the changeset is applied.
159 static void pnv_php_reverse_nodes(struct device_node *parent)
161 struct device_node *child, *next;
164 for_each_child_of_node(parent, child)
165 pnv_php_reverse_nodes(child);
167 /* Reverse the nodes in the child list */
168 child = parent->child;
169 parent->child = NULL;
171 next = child->sibling;
173 child->sibling = parent->child;
174 parent->child = child;
179 static int pnv_php_populate_changeset(struct of_changeset *ocs,
180 struct device_node *dn)
182 struct device_node *child;
185 for_each_child_of_node(dn, child) {
186 ret = of_changeset_attach_node(ocs, child);
190 ret = pnv_php_populate_changeset(ocs, child);
198 static void *pnv_php_add_one_pdn(struct device_node *dn, void *data)
200 struct pci_controller *hose = (struct pci_controller *)data;
203 pdn = pci_add_device_node_info(hose, dn);
205 return ERR_PTR(-ENOMEM);
210 static void pnv_php_add_pdns(struct pnv_php_slot *slot)
212 struct pci_controller *hose = pci_bus_to_host(slot->bus);
214 pci_traverse_device_nodes(slot->dn, pnv_php_add_one_pdn, hose);
217 static int pnv_php_add_devtree(struct pnv_php_slot *php_slot)
219 void *fdt, *fdt1, *dt;
222 /* We don't know the FDT blob size. We try to get it through
223 * maximal memory chunk and then copy it to another chunk that
224 * fits the real size.
226 fdt1 = kzalloc(0x10000, GFP_KERNEL);
227 if (unlikely(!fdt1)) {
229 dev_warn(&php_slot->pdev->dev, "Cannot alloc FDT blob\n");
233 ret = pnv_pci_get_device_tree(php_slot->dn->phandle, fdt1, 0x10000);
235 dev_warn(&php_slot->pdev->dev, "Error %d getting FDT blob\n",
240 fdt = kzalloc(fdt_totalsize(fdt1), GFP_KERNEL);
241 if (unlikely(!fdt)) {
243 dev_warn(&php_slot->pdev->dev, "Cannot %d bytes memory\n",
244 fdt_totalsize(fdt1));
248 /* Unflatten device tree blob */
249 memcpy(fdt, fdt1, fdt_totalsize(fdt1));
250 dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL);
253 dev_warn(&php_slot->pdev->dev, "Cannot unflatten FDT\n");
257 /* Initialize and apply the changeset */
258 of_changeset_init(&php_slot->ocs);
259 pnv_php_reverse_nodes(php_slot->dn);
260 ret = pnv_php_populate_changeset(&php_slot->ocs, php_slot->dn);
262 pnv_php_reverse_nodes(php_slot->dn);
263 dev_warn(&php_slot->pdev->dev, "Error %d populating changeset\n",
268 php_slot->dn->child = NULL;
269 ret = of_changeset_apply(&php_slot->ocs);
271 dev_warn(&php_slot->pdev->dev, "Error %d applying changeset\n",
273 goto destroy_changeset;
276 /* Add device node firmware data */
277 pnv_php_add_pdns(php_slot);
284 of_changeset_destroy(&php_slot->ocs);
287 php_slot->dn->child = NULL;
296 int pnv_php_set_slot_power_state(struct hotplug_slot *slot,
299 struct pnv_php_slot *php_slot = slot->private;
303 ret = pnv_pci_set_power_state(php_slot->id, state, &msg);
304 if (likely(ret > 0)) {
305 if (be64_to_cpu(msg.params[1]) != php_slot->dn->phandle ||
306 be64_to_cpu(msg.params[2]) != state ||
307 be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) {
308 dev_warn(&php_slot->pdev->dev, "Wrong msg (%lld, %lld, %lld)\n",
309 be64_to_cpu(msg.params[1]),
310 be64_to_cpu(msg.params[2]),
311 be64_to_cpu(msg.params[3]));
314 } else if (unlikely(ret < 0)) {
315 dev_warn(&php_slot->pdev->dev, "Error %d powering %s\n",
316 ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off");
320 if (state == OPAL_PCI_SLOT_POWER_OFF || state == OPAL_PCI_SLOT_OFFLINE)
321 pnv_php_rmv_devtree(php_slot);
323 ret = pnv_php_add_devtree(php_slot);
327 EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state);
329 static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state)
331 struct pnv_php_slot *php_slot = slot->private;
332 uint8_t power_state = OPAL_PCI_SLOT_POWER_ON;
336 * Retrieve power status from firmware. If we fail
337 * getting that, the power status fails back to
340 ret = pnv_pci_get_power_state(php_slot->id, &power_state);
342 dev_warn(&php_slot->pdev->dev, "Error %d getting power status\n",
345 *state = power_state;
346 slot->info->power_status = power_state;
352 static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state)
354 struct pnv_php_slot *php_slot = slot->private;
355 uint8_t presence = OPAL_PCI_SLOT_EMPTY;
359 * Retrieve presence status from firmware. If we can't
360 * get that, it will fail back to be empty.
362 ret = pnv_pci_get_presence_state(php_slot->id, &presence);
363 if (likely(ret >= 0)) {
365 slot->info->adapter_status = presence;
368 dev_warn(&php_slot->pdev->dev, "Error %d getting presence\n",
375 static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state)
377 /* FIXME: Make it real once firmware supports it */
378 slot->info->attention_status = state;
383 static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
385 struct hotplug_slot *slot = &php_slot->slot;
386 uint8_t presence = OPAL_PCI_SLOT_EMPTY;
387 uint8_t power_status = OPAL_PCI_SLOT_POWER_ON;
390 /* Check if the slot has been configured */
391 if (php_slot->state != PNV_PHP_STATE_REGISTERED)
394 /* Retrieve slot presence status */
395 ret = pnv_php_get_adapter_state(slot, &presence);
399 /* Proceed if there have nothing behind the slot */
400 if (presence == OPAL_PCI_SLOT_EMPTY)
404 * If the power supply to the slot is off, we can't detect
405 * adapter presence state. That means we have to turn the
406 * slot on before going to probe slot's presence state.
408 * On the first time, we don't change the power status to
409 * boost system boot with assumption that the firmware
410 * supplies consistent slot power status: empty slot always
411 * has its power off and non-empty slot has its power on.
413 if (!php_slot->power_state_check) {
414 php_slot->power_state_check = true;
416 ret = pnv_php_get_power_state(slot, &power_status);
420 if (power_status != OPAL_PCI_SLOT_POWER_ON)
424 /* Check the power status. Scan the slot if it is already on */
425 ret = pnv_php_get_power_state(slot, &power_status);
429 if (power_status == OPAL_PCI_SLOT_POWER_ON)
432 /* Power is off, turn it on and then scan the slot */
433 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
438 if (presence == OPAL_PCI_SLOT_PRESENT) {
440 pci_lock_rescan_remove();
441 pci_hp_add_devices(php_slot->bus);
442 pci_unlock_rescan_remove();
445 /* Rescan for child hotpluggable slots */
446 php_slot->state = PNV_PHP_STATE_POPULATED;
448 pnv_php_register(php_slot->dn);
450 php_slot->state = PNV_PHP_STATE_POPULATED;
456 static int pnv_php_enable_slot(struct hotplug_slot *slot)
458 struct pnv_php_slot *php_slot = container_of(slot,
459 struct pnv_php_slot, slot);
461 return pnv_php_enable(php_slot, true);
464 static int pnv_php_disable_slot(struct hotplug_slot *slot)
466 struct pnv_php_slot *php_slot = slot->private;
469 if (php_slot->state != PNV_PHP_STATE_POPULATED)
472 /* Remove all devices behind the slot */
473 pci_lock_rescan_remove();
474 pci_hp_remove_devices(php_slot->bus);
475 pci_unlock_rescan_remove();
477 /* Detach the child hotpluggable slots */
478 pnv_php_unregister(php_slot->dn);
480 /* Notify firmware and remove device nodes */
481 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF);
483 php_slot->state = PNV_PHP_STATE_REGISTERED;
487 static struct hotplug_slot_ops php_slot_ops = {
488 .get_power_status = pnv_php_get_power_state,
489 .get_adapter_status = pnv_php_get_adapter_state,
490 .set_attention_status = pnv_php_set_attention_state,
491 .enable_slot = pnv_php_enable_slot,
492 .disable_slot = pnv_php_disable_slot,
495 static void pnv_php_release(struct hotplug_slot *slot)
497 struct pnv_php_slot *php_slot = slot->private;
500 /* Remove from global or child list */
501 spin_lock_irqsave(&pnv_php_lock, flags);
502 list_del(&php_slot->link);
503 spin_unlock_irqrestore(&pnv_php_lock, flags);
505 /* Detach from parent */
506 pnv_php_put_slot(php_slot);
507 pnv_php_put_slot(php_slot->parent);
510 static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
512 struct pnv_php_slot *php_slot;
517 label = of_get_property(dn, "ibm,slot-label", NULL);
518 if (unlikely(!label))
521 if (pnv_pci_get_slot_id(dn, &id))
524 bus = pci_find_bus_by_node(dn);
528 php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL);
529 if (unlikely(!php_slot))
532 php_slot->name = kstrdup(label, GFP_KERNEL);
533 if (unlikely(!php_slot->name)) {
538 if (likely(dn->child && PCI_DN(dn->child)))
539 php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn);
541 php_slot->slot_no = -1; /* Placeholder slot */
543 kref_init(&php_slot->kref);
544 php_slot->state = PNV_PHP_STATE_INITIALIZED;
546 php_slot->pdev = bus->self;
549 php_slot->power_state_check = false;
550 php_slot->slot.ops = &php_slot_ops;
551 php_slot->slot.info = &php_slot->slot_info;
552 php_slot->slot.release = pnv_php_release;
553 php_slot->slot.private = php_slot;
555 INIT_LIST_HEAD(&php_slot->children);
556 INIT_LIST_HEAD(&php_slot->link);
561 static int pnv_php_register_slot(struct pnv_php_slot *php_slot)
563 struct pnv_php_slot *parent;
564 struct device_node *dn = php_slot->dn;
568 /* Check if the slot is registered or not */
569 parent = pnv_php_find_slot(php_slot->dn);
570 if (unlikely(parent)) {
571 pnv_php_put_slot(parent);
575 /* Register PCI slot */
576 ret = pci_hp_register(&php_slot->slot, php_slot->bus,
577 php_slot->slot_no, php_slot->name);
579 dev_warn(&php_slot->pdev->dev, "Error %d registering slot\n",
584 /* Attach to the parent's child list or global list */
585 while ((dn = of_get_parent(dn))) {
591 parent = pnv_php_find_slot(dn);
600 spin_lock_irqsave(&pnv_php_lock, flags);
601 php_slot->parent = parent;
603 list_add_tail(&php_slot->link, &parent->children);
605 list_add_tail(&php_slot->link, &pnv_php_slot_list);
606 spin_unlock_irqrestore(&pnv_php_lock, flags);
608 php_slot->state = PNV_PHP_STATE_REGISTERED;
612 static int pnv_php_register_one(struct device_node *dn)
614 struct pnv_php_slot *php_slot;
615 const __be32 *prop32;
618 /* Check if it's hotpluggable slot */
619 prop32 = of_get_property(dn, "ibm,slot-pluggable", NULL);
620 if (!prop32 || !of_read_number(prop32, 1))
623 prop32 = of_get_property(dn, "ibm,reset-by-firmware", NULL);
624 if (!prop32 || !of_read_number(prop32, 1))
627 php_slot = pnv_php_alloc_slot(dn);
628 if (unlikely(!php_slot))
631 ret = pnv_php_register_slot(php_slot);
635 ret = pnv_php_enable(php_slot, false);
637 goto unregister_slot;
642 pnv_php_unregister_one(php_slot->dn);
644 pnv_php_put_slot(php_slot);
648 static void pnv_php_register(struct device_node *dn)
650 struct device_node *child;
653 * The parent slots should be registered before their
656 for_each_child_of_node(dn, child) {
657 pnv_php_register_one(child);
658 pnv_php_register(child);
662 static void pnv_php_unregister_one(struct device_node *dn)
664 struct pnv_php_slot *php_slot;
666 php_slot = pnv_php_find_slot(dn);
670 php_slot->state = PNV_PHP_STATE_OFFLINE;
671 pnv_php_put_slot(php_slot);
672 pci_hp_deregister(&php_slot->slot);
675 static void pnv_php_unregister(struct device_node *dn)
677 struct device_node *child;
679 /* The child slots should go before their parent slots */
680 for_each_child_of_node(dn, child) {
681 pnv_php_unregister(child);
682 pnv_php_unregister_one(child);
686 static int __init pnv_php_init(void)
688 struct device_node *dn;
690 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
691 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
692 pnv_php_register(dn);
697 static void __exit pnv_php_exit(void)
699 struct device_node *dn;
701 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
702 pnv_php_unregister(dn);
705 module_init(pnv_php_init);
706 module_exit(pnv_php_exit);
708 MODULE_VERSION(DRIVER_VERSION);
709 MODULE_LICENSE("GPL v2");
710 MODULE_AUTHOR(DRIVER_AUTHOR);
711 MODULE_DESCRIPTION(DRIVER_DESC);