Writing a new value to the PCI_SECONDARY_BUS register of the bridge means
that its children will become addressable on another address (new B in BDF)
or even un-addressable if the secondary bus is set to zero.

On PowerNV, device PEs are heavily BDF-dependent, so they must be updated
on every such change of its address.

Signed-off-by: Sergey Miroshnichenko <s.miroshniche...@yadro.com>
---
 arch/powerpc/platforms/powernv/pci.c | 118 ++++++++++++++++++++++++++-
 1 file changed, 116 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/platforms/powernv/pci.c 
b/arch/powerpc/platforms/powernv/pci.c
index 73c2d0aed996..52ce717a7905 100644
--- a/arch/powerpc/platforms/powernv/pci.c
+++ b/arch/powerpc/platforms/powernv/pci.c
@@ -722,13 +722,127 @@ int pnv_pci_cfg_read(struct pci_dn *pdn,
                                    where, size, val);
 }
 
+static void invalidate_children_pes(struct pci_dn *pdn)
+{
+       struct pnv_phb *phb = pdn->phb->private_data;
+       struct pci_dn *child;
+       bool found_pe = false;
+       int pe_num;
+       int pe_bus;
+
+       list_for_each_entry(child, &pdn->child_list, list) {
+               struct pnv_ioda_pe *pe = (child->pe_number != IODA_INVALID_PE) ?
+                       &phb->ioda.pe_array[child->pe_number] :
+                       NULL;
+
+               if (!child->busno)
+                       continue;
+
+               if ((child->class_code >> 8) == PCI_CLASS_BRIDGE_PCI)
+                       invalidate_children_pes(child);
+
+               if (pe) {
+                       u8 rid_bus = (pe->rid >> 8) & 0xff;
+
+                       if (rid_bus) {
+                               pe_num = child->pe_number;
+                               pe_bus = rid_bus;
+                               found_pe = true;
+                       }
+
+                       pe->rid &= 0xff;
+               }
+
+               child->busno = 0;
+       }
+
+       if (found_pe) {
+               u16 rid = pe_bus << 8;
+
+               opal_pci_set_pe(phb->opal_id, pe_num, rid, 7, 0, 0, 
OPAL_UNMAP_PE);
+       }
+}
+
+static u8 pre_hook_new_sec_bus(struct pci_dn *pdn, u8 new_secondary_bus)
+{
+       u32 old_secondary_bus = 0;
+
+       if ((pdn->class_code >> 8) != PCI_CLASS_BRIDGE_PCI)
+               return 0;
+
+       pnv_pci_cfg_read(pdn, PCI_SECONDARY_BUS, 1, &old_secondary_bus);
+       old_secondary_bus &= 0xff;
+
+       if (old_secondary_bus != new_secondary_bus)
+               invalidate_children_pes(pdn);
+
+       return old_secondary_bus;
+}
+
+static void update_children_pes(struct pci_dn *pdn, u8 new_secondary_bus)
+{
+       struct pnv_phb *phb = pdn->phb->private_data;
+       struct pci_dn *child;
+       bool found_pe = false;
+       int pe_num;
+
+       if (!new_secondary_bus)
+               return;
+
+       list_for_each_entry(child, &pdn->child_list, list) {
+               struct pnv_ioda_pe *pe = (child->pe_number != IODA_INVALID_PE) ?
+                       &phb->ioda.pe_array[child->pe_number] :
+                       NULL;
+
+               if (child->busno)
+                       continue;
+
+               child->busno = new_secondary_bus;
+
+               if (pe) {
+                       pe->rid |= (child->busno << 8);
+                       pe_num = child->pe_number;
+                       found_pe = true;
+               }
+       }
+
+       if (found_pe) {
+               u16 rid = new_secondary_bus << 8;
+
+               opal_pci_set_pe(phb->opal_id, pe_num, rid, 7, 0, 0, 
OPAL_MAP_PE);
+       }
+}
+
+static void post_hook_new_sec_bus(struct pci_dn *pdn, u8 new_secondary_bus)
+{
+       if ((pdn->class_code >> 8) != PCI_CLASS_BRIDGE_PCI)
+               return;
+
+       update_children_pes(pdn, new_secondary_bus);
+}
+
 int pnv_pci_cfg_write(struct pci_dn *pdn,
                      int where, int size, u32 val)
 {
        struct pnv_phb *phb = pdn->phb->private_data;
+       u8 old_secondary_bus = 0, new_secondary_bus = 0;
+       int rc;
+
+       if (where == PCI_SECONDARY_BUS) {
+               new_secondary_bus = val & 0xff;
+               old_secondary_bus = pre_hook_new_sec_bus(pdn, 
new_secondary_bus);
+       } else if (where == PCI_PRIMARY_BUS && size > 1) {
+               new_secondary_bus = (val >> 8) & 0xff;
+               old_secondary_bus = pre_hook_new_sec_bus(pdn, 
new_secondary_bus);
+       }
 
-       return pnv_pci_cfg_write_raw(phb->opal_id, pdn->busno, pdn->devfn,
-                                    where, size, val);
+       rc = pnv_pci_cfg_write_raw(phb->opal_id, pdn->busno, pdn->devfn,
+                                  where, size, val);
+
+       if (new_secondary_bus && old_secondary_bus != new_secondary_bus)
+               post_hook_new_sec_bus(pdn, new_secondary_bus);
+
+       return rc;
 }
 
 #if CONFIG_EEH
-- 
2.20.1

Reply via email to