Path: blob/master/arch/powerpc/platforms/powernv/pci-ioda.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Support PCI/PCIe on PowerNV platforms3*4* Copyright 2011 Benjamin Herrenschmidt, IBM Corp.5*/67#undef DEBUG89#include <linux/kernel.h>10#include <linux/pci.h>11#include <linux/crash_dump.h>12#include <linux/delay.h>13#include <linux/string.h>14#include <linux/init.h>15#include <linux/memblock.h>16#include <linux/irq.h>17#include <linux/io.h>18#include <linux/msi.h>19#include <linux/iommu.h>20#include <linux/rculist.h>21#include <linux/sizes.h>22#include <linux/debugfs.h>23#include <linux/of_address.h>24#include <linux/of_irq.h>2526#include <asm/sections.h>27#include <asm/io.h>28#include <asm/pci-bridge.h>29#include <asm/machdep.h>30#include <asm/msi_bitmap.h>31#include <asm/ppc-pci.h>32#include <asm/opal.h>33#include <asm/iommu.h>34#include <asm/tce.h>35#include <asm/xics.h>36#include <asm/firmware.h>37#include <asm/pnv-pci.h>38#include <asm/mmzone.h>39#include <asm/xive.h>4041#include "powernv.h"42#include "pci.h"43#include "../../../../drivers/pci/pci.h"4445/* This array is indexed with enum pnv_phb_type */46static const char * const pnv_phb_names[] = { "IODA2", "NPU_OCAPI" };4748static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);49static void pnv_pci_configure_bus(struct pci_bus *bus);5051void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,52const char *fmt, ...)53{54struct va_format vaf;55va_list args;56char pfix[32];5758va_start(args, fmt);5960vaf.fmt = fmt;61vaf.va = &args;6263if (pe->flags & PNV_IODA_PE_DEV)64strscpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));65else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))66sprintf(pfix, "%04x:%02x ",67pci_domain_nr(pe->pbus), pe->pbus->number);68#ifdef CONFIG_PCI_IOV69else if (pe->flags & PNV_IODA_PE_VF)70sprintf(pfix, "%04x:%02x:%2x.%d",71pci_domain_nr(pe->parent_dev->bus),72(pe->rid & 0xff00) >> 8,73PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));74#endif /* CONFIG_PCI_IOV*/7576printk("%spci %s: [PE# %.2x] %pV",77level, pfix, pe->pe_number, &vaf);7879va_end(args);80}8182static bool pnv_iommu_bypass_disabled __read_mostly;83static bool pci_reset_phbs __read_mostly;8485static int __init iommu_setup(char *str)86{87if (!str)88return -EINVAL;8990while (*str) {91if (!strncmp(str, "nobypass", 8)) {92pnv_iommu_bypass_disabled = true;93pr_info("PowerNV: IOMMU bypass window disabled.\n");94break;95}96str += strcspn(str, ",");97if (*str == ',')98str++;99}100101return 0;102}103early_param("iommu", iommu_setup);104105static int __init pci_reset_phbs_setup(char *str)106{107pci_reset_phbs = true;108return 0;109}110111early_param("ppc_pci_reset_phbs", pci_reset_phbs_setup);112113static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)114{115s64 rc;116117phb->ioda.pe_array[pe_no].phb = phb;118phb->ioda.pe_array[pe_no].pe_number = pe_no;119phb->ioda.pe_array[pe_no].dma_setup_done = false;120121/*122* Clear the PE frozen state as it might be put into frozen state123* in the last PCI remove path. It's not harmful to do so when the124* PE is already in unfrozen state.125*/126rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,127OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);128if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)129pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n",130__func__, rc, phb->hose->global_number, pe_no);131132return &phb->ioda.pe_array[pe_no];133}134135static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)136{137if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {138pr_warn("%s: Invalid PE %x on PHB#%x\n",139__func__, pe_no, phb->hose->global_number);140return;141}142143mutex_lock(&phb->ioda.pe_alloc_mutex);144if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))145pr_debug("%s: PE %x was reserved on PHB#%x\n",146__func__, pe_no, phb->hose->global_number);147mutex_unlock(&phb->ioda.pe_alloc_mutex);148149pnv_ioda_init_pe(phb, pe_no);150}151152struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb, int count)153{154struct pnv_ioda_pe *ret = NULL;155int run = 0, pe, i;156157mutex_lock(&phb->ioda.pe_alloc_mutex);158159/* scan backwards for a run of @count cleared bits */160for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {161if (test_bit(pe, phb->ioda.pe_alloc)) {162run = 0;163continue;164}165166run++;167if (run == count)168break;169}170if (run != count)171goto out;172173for (i = pe; i < pe + count; i++) {174set_bit(i, phb->ioda.pe_alloc);175pnv_ioda_init_pe(phb, i);176}177ret = &phb->ioda.pe_array[pe];178179out:180mutex_unlock(&phb->ioda.pe_alloc_mutex);181return ret;182}183184void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)185{186struct pnv_phb *phb = pe->phb;187unsigned int pe_num = pe->pe_number;188189WARN_ON(pe->pdev);190memset(pe, 0, sizeof(struct pnv_ioda_pe));191192mutex_lock(&phb->ioda.pe_alloc_mutex);193clear_bit(pe_num, phb->ioda.pe_alloc);194mutex_unlock(&phb->ioda.pe_alloc_mutex);195}196197/* The default M64 BAR is shared by all PEs */198static int pnv_ioda2_init_m64(struct pnv_phb *phb)199{200const char *desc;201struct resource *r;202s64 rc;203204/* Configure the default M64 BAR */205rc = opal_pci_set_phb_mem_window(phb->opal_id,206OPAL_M64_WINDOW_TYPE,207phb->ioda.m64_bar_idx,208phb->ioda.m64_base,2090, /* unused */210phb->ioda.m64_size);211if (rc != OPAL_SUCCESS) {212desc = "configuring";213goto fail;214}215216/* Enable the default M64 BAR */217rc = opal_pci_phb_mmio_enable(phb->opal_id,218OPAL_M64_WINDOW_TYPE,219phb->ioda.m64_bar_idx,220OPAL_ENABLE_M64_SPLIT);221if (rc != OPAL_SUCCESS) {222desc = "enabling";223goto fail;224}225226/*227* Exclude the segments for reserved and root bus PE, which228* are first or last two PEs.229*/230r = &phb->hose->mem_resources[1];231if (phb->ioda.reserved_pe_idx == 0)232r->start += (2 * phb->ioda.m64_segsize);233else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))234r->end -= (2 * phb->ioda.m64_segsize);235else236pr_warn(" Cannot strip M64 segment for reserved PE#%x\n",237phb->ioda.reserved_pe_idx);238239return 0;240241fail:242pr_warn(" Failure %lld %s M64 BAR#%d\n",243rc, desc, phb->ioda.m64_bar_idx);244opal_pci_phb_mmio_enable(phb->opal_id,245OPAL_M64_WINDOW_TYPE,246phb->ioda.m64_bar_idx,247OPAL_DISABLE_M64);248return -EIO;249}250251static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,252unsigned long *pe_bitmap)253{254struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);255struct resource *r;256resource_size_t base, sgsz, start, end;257int segno, i;258259base = phb->ioda.m64_base;260sgsz = phb->ioda.m64_segsize;261for (i = 0; i <= PCI_ROM_RESOURCE; i++) {262r = &pdev->resource[i];263if (!r->parent || !pnv_pci_is_m64(phb, r))264continue;265266start = ALIGN_DOWN(r->start - base, sgsz);267end = ALIGN(r->end - base, sgsz);268for (segno = start / sgsz; segno < end / sgsz; segno++) {269if (pe_bitmap)270set_bit(segno, pe_bitmap);271else272pnv_ioda_reserve_pe(phb, segno);273}274}275}276277static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,278unsigned long *pe_bitmap,279bool all)280{281struct pci_dev *pdev;282283list_for_each_entry(pdev, &bus->devices, bus_list) {284pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);285286if (all && pdev->subordinate)287pnv_ioda_reserve_m64_pe(pdev->subordinate,288pe_bitmap, all);289}290}291292static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)293{294struct pnv_phb *phb = pci_bus_to_pnvhb(bus);295struct pnv_ioda_pe *master_pe, *pe;296unsigned long size, *pe_alloc;297int i;298299/* Root bus shouldn't use M64 */300if (pci_is_root_bus(bus))301return NULL;302303/* Allocate bitmap */304size = ALIGN(phb->ioda.total_pe_num / 8, sizeof(unsigned long));305pe_alloc = kzalloc(size, GFP_KERNEL);306if (!pe_alloc) {307pr_warn("%s: Out of memory !\n",308__func__);309return NULL;310}311312/* Figure out reserved PE numbers by the PE */313pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);314315/*316* the current bus might not own M64 window and that's all317* contributed by its child buses. For the case, we needn't318* pick M64 dependent PE#.319*/320if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {321kfree(pe_alloc);322return NULL;323}324325/*326* Figure out the master PE and put all slave PEs to master327* PE's list to form compound PE.328*/329master_pe = NULL;330i = -1;331while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <332phb->ioda.total_pe_num) {333pe = &phb->ioda.pe_array[i];334335phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;336if (!master_pe) {337pe->flags |= PNV_IODA_PE_MASTER;338INIT_LIST_HEAD(&pe->slaves);339master_pe = pe;340} else {341pe->flags |= PNV_IODA_PE_SLAVE;342pe->master = master_pe;343list_add_tail(&pe->list, &master_pe->slaves);344}345}346347kfree(pe_alloc);348return master_pe;349}350351static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)352{353struct pci_controller *hose = phb->hose;354struct device_node *dn = hose->dn;355struct resource *res;356u32 m64_range[2], i;357const __be32 *r;358u64 pci_addr;359360if (phb->type != PNV_PHB_IODA2) {361pr_info(" Not support M64 window\n");362return;363}364365if (!firmware_has_feature(FW_FEATURE_OPAL)) {366pr_info(" Firmware too old to support M64 window\n");367return;368}369370r = of_get_property(dn, "ibm,opal-m64-window", NULL);371if (!r) {372pr_info(" No <ibm,opal-m64-window> on %pOF\n",373dn);374return;375}376377/*378* Find the available M64 BAR range and pickup the last one for379* covering the whole 64-bits space. We support only one range.380*/381if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges",382m64_range, 2)) {383/* In absence of the property, assume 0..15 */384m64_range[0] = 0;385m64_range[1] = 16;386}387/* We only support 64 bits in our allocator */388if (m64_range[1] > 63) {389pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",390__func__, m64_range[1], phb->hose->global_number);391m64_range[1] = 63;392}393/* Empty range, no m64 */394if (m64_range[1] <= m64_range[0]) {395pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",396__func__, phb->hose->global_number);397return;398}399400/* Configure M64 informations */401res = &hose->mem_resources[1];402res->name = dn->full_name;403res->start = of_translate_address(dn, r + 2);404res->end = res->start + of_read_number(r + 4, 2) - 1;405res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);406pci_addr = of_read_number(r, 2);407hose->mem_offset[1] = res->start - pci_addr;408409phb->ioda.m64_size = resource_size(res);410phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;411phb->ioda.m64_base = pci_addr;412413/* This lines up nicely with the display from processing OF ranges */414pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",415res->start, res->end, pci_addr, m64_range[0],416m64_range[0] + m64_range[1] - 1);417418/* Mark all M64 used up by default */419phb->ioda.m64_bar_alloc = (unsigned long)-1;420421/* Use last M64 BAR to cover M64 window */422m64_range[1]--;423phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1];424425pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx);426427/* Mark remaining ones free */428for (i = m64_range[0]; i < m64_range[1]; i++)429clear_bit(i, &phb->ioda.m64_bar_alloc);430431/*432* Setup init functions for M64 based on IODA version, IODA3 uses433* the IODA2 code.434*/435phb->init_m64 = pnv_ioda2_init_m64;436}437438static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)439{440struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];441struct pnv_ioda_pe *slave;442s64 rc;443444/* Fetch master PE */445if (pe->flags & PNV_IODA_PE_SLAVE) {446pe = pe->master;447if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))448return;449450pe_no = pe->pe_number;451}452453/* Freeze master PE */454rc = opal_pci_eeh_freeze_set(phb->opal_id,455pe_no,456OPAL_EEH_ACTION_SET_FREEZE_ALL);457if (rc != OPAL_SUCCESS) {458pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",459__func__, rc, phb->hose->global_number, pe_no);460return;461}462463/* Freeze slave PEs */464if (!(pe->flags & PNV_IODA_PE_MASTER))465return;466467list_for_each_entry(slave, &pe->slaves, list) {468rc = opal_pci_eeh_freeze_set(phb->opal_id,469slave->pe_number,470OPAL_EEH_ACTION_SET_FREEZE_ALL);471if (rc != OPAL_SUCCESS)472pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",473__func__, rc, phb->hose->global_number,474slave->pe_number);475}476}477478static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)479{480struct pnv_ioda_pe *pe, *slave;481s64 rc;482483/* Find master PE */484pe = &phb->ioda.pe_array[pe_no];485if (pe->flags & PNV_IODA_PE_SLAVE) {486pe = pe->master;487WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));488pe_no = pe->pe_number;489}490491/* Clear frozen state for master PE */492rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);493if (rc != OPAL_SUCCESS) {494pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",495__func__, rc, opt, phb->hose->global_number, pe_no);496return -EIO;497}498499if (!(pe->flags & PNV_IODA_PE_MASTER))500return 0;501502/* Clear frozen state for slave PEs */503list_for_each_entry(slave, &pe->slaves, list) {504rc = opal_pci_eeh_freeze_clear(phb->opal_id,505slave->pe_number,506opt);507if (rc != OPAL_SUCCESS) {508pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",509__func__, rc, opt, phb->hose->global_number,510slave->pe_number);511return -EIO;512}513}514515return 0;516}517518static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)519{520struct pnv_ioda_pe *slave, *pe;521u8 fstate = 0, state;522__be16 pcierr = 0;523s64 rc;524525/* Sanity check on PE number */526if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)527return OPAL_EEH_STOPPED_PERM_UNAVAIL;528529/*530* Fetch the master PE and the PE instance might be531* not initialized yet.532*/533pe = &phb->ioda.pe_array[pe_no];534if (pe->flags & PNV_IODA_PE_SLAVE) {535pe = pe->master;536WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));537pe_no = pe->pe_number;538}539540/* Check the master PE */541rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,542&state, &pcierr, NULL);543if (rc != OPAL_SUCCESS) {544pr_warn("%s: Failure %lld getting "545"PHB#%x-PE#%x state\n",546__func__, rc,547phb->hose->global_number, pe_no);548return OPAL_EEH_STOPPED_TEMP_UNAVAIL;549}550551/* Check the slave PE */552if (!(pe->flags & PNV_IODA_PE_MASTER))553return state;554555list_for_each_entry(slave, &pe->slaves, list) {556rc = opal_pci_eeh_freeze_status(phb->opal_id,557slave->pe_number,558&fstate,559&pcierr,560NULL);561if (rc != OPAL_SUCCESS) {562pr_warn("%s: Failure %lld getting "563"PHB#%x-PE#%x state\n",564__func__, rc,565phb->hose->global_number, slave->pe_number);566return OPAL_EEH_STOPPED_TEMP_UNAVAIL;567}568569/*570* Override the result based on the ascending571* priority.572*/573if (fstate > state)574state = fstate;575}576577return state;578}579580struct pnv_ioda_pe *pnv_pci_bdfn_to_pe(struct pnv_phb *phb, u16 bdfn)581{582int pe_number = phb->ioda.pe_rmap[bdfn];583584if (pe_number == IODA_INVALID_PE)585return NULL;586587return &phb->ioda.pe_array[pe_number];588}589590struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)591{592struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);593struct pci_dn *pdn = pci_get_pdn(dev);594595if (!pdn)596return NULL;597if (pdn->pe_number == IODA_INVALID_PE)598return NULL;599return &phb->ioda.pe_array[pdn->pe_number];600}601602static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,603struct pnv_ioda_pe *parent,604struct pnv_ioda_pe *child,605bool is_add)606{607const char *desc = is_add ? "adding" : "removing";608uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :609OPAL_REMOVE_PE_FROM_DOMAIN;610struct pnv_ioda_pe *slave;611long rc;612613/* Parent PE affects child PE */614rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,615child->pe_number, op);616if (rc != OPAL_SUCCESS) {617pe_warn(child, "OPAL error %ld %s to parent PELTV\n",618rc, desc);619return -ENXIO;620}621622if (!(child->flags & PNV_IODA_PE_MASTER))623return 0;624625/* Compound case: parent PE affects slave PEs */626list_for_each_entry(slave, &child->slaves, list) {627rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,628slave->pe_number, op);629if (rc != OPAL_SUCCESS) {630pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",631rc, desc);632return -ENXIO;633}634}635636return 0;637}638639static int pnv_ioda_set_peltv(struct pnv_phb *phb,640struct pnv_ioda_pe *pe,641bool is_add)642{643struct pnv_ioda_pe *slave;644struct pci_dev *pdev = NULL;645int ret;646647/*648* Clear PE frozen state. If it's master PE, we need649* clear slave PE frozen state as well.650*/651if (is_add) {652opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,653OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);654if (pe->flags & PNV_IODA_PE_MASTER) {655list_for_each_entry(slave, &pe->slaves, list)656opal_pci_eeh_freeze_clear(phb->opal_id,657slave->pe_number,658OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);659}660}661662/*663* Associate PE in PELT. We need add the PE into the664* corresponding PELT-V as well. Otherwise, the error665* originated from the PE might contribute to other666* PEs.667*/668ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);669if (ret)670return ret;671672/* For compound PEs, any one affects all of them */673if (pe->flags & PNV_IODA_PE_MASTER) {674list_for_each_entry(slave, &pe->slaves, list) {675ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);676if (ret)677return ret;678}679}680681if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))682pdev = pe->pbus->self;683else if (pe->flags & PNV_IODA_PE_DEV)684pdev = pe->pdev->bus->self;685#ifdef CONFIG_PCI_IOV686else if (pe->flags & PNV_IODA_PE_VF)687pdev = pe->parent_dev;688#endif /* CONFIG_PCI_IOV */689while (pdev) {690struct pci_dn *pdn = pci_get_pdn(pdev);691struct pnv_ioda_pe *parent;692693if (pdn && pdn->pe_number != IODA_INVALID_PE) {694parent = &phb->ioda.pe_array[pdn->pe_number];695ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);696if (ret)697return ret;698}699700pdev = pdev->bus->self;701}702703return 0;704}705706static void pnv_ioda_unset_peltv(struct pnv_phb *phb,707struct pnv_ioda_pe *pe,708struct pci_dev *parent)709{710int64_t rc;711712while (parent) {713struct pci_dn *pdn = pci_get_pdn(parent);714715if (pdn && pdn->pe_number != IODA_INVALID_PE) {716rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,717pe->pe_number,718OPAL_REMOVE_PE_FROM_DOMAIN);719/* XXX What to do in case of error ? */720}721parent = parent->bus->self;722}723724opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,725OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);726727/* Disassociate PE in PELT */728rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,729pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);730if (rc)731pe_warn(pe, "OPAL error %lld remove self from PELTV\n", rc);732}733734int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)735{736struct pci_dev *parent;737uint8_t bcomp, dcomp, fcomp;738int64_t rc;739long rid_end, rid;740741/* Currently, we just deconfigure VF PE. Bus PE will always there.*/742if (pe->pbus) {743int count;744745dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;746fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;747parent = pe->pbus->self;748if (pe->flags & PNV_IODA_PE_BUS_ALL)749count = resource_size(&pe->pbus->busn_res);750else751count = 1;752753switch(count) {754case 1: bcomp = OpalPciBusAll; break;755case 2: bcomp = OpalPciBus7Bits; break;756case 4: bcomp = OpalPciBus6Bits; break;757case 8: bcomp = OpalPciBus5Bits; break;758case 16: bcomp = OpalPciBus4Bits; break;759case 32: bcomp = OpalPciBus3Bits; break;760default:761dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",762count);763/* Do an exact match only */764bcomp = OpalPciBusAll;765}766rid_end = pe->rid + (count << 8);767} else {768#ifdef CONFIG_PCI_IOV769if (pe->flags & PNV_IODA_PE_VF)770parent = pe->parent_dev;771else772#endif773parent = pe->pdev->bus->self;774bcomp = OpalPciBusAll;775dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;776fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;777rid_end = pe->rid + 1;778}779780/* Clear the reverse map */781for (rid = pe->rid; rid < rid_end; rid++)782phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;783784/*785* Release from all parents PELT-V. NPUs don't have a PELTV786* table787*/788if (phb->type != PNV_PHB_NPU_OCAPI)789pnv_ioda_unset_peltv(phb, pe, parent);790791rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,792bcomp, dcomp, fcomp, OPAL_UNMAP_PE);793if (rc)794pe_err(pe, "OPAL error %lld trying to setup PELT table\n", rc);795796pe->pbus = NULL;797pe->pdev = NULL;798#ifdef CONFIG_PCI_IOV799pe->parent_dev = NULL;800#endif801802return 0;803}804805int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)806{807uint8_t bcomp, dcomp, fcomp;808long rc, rid_end, rid;809810/* Bus validation ? */811if (pe->pbus) {812int count;813814dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;815fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;816if (pe->flags & PNV_IODA_PE_BUS_ALL)817count = resource_size(&pe->pbus->busn_res);818else819count = 1;820821switch(count) {822case 1: bcomp = OpalPciBusAll; break;823case 2: bcomp = OpalPciBus7Bits; break;824case 4: bcomp = OpalPciBus6Bits; break;825case 8: bcomp = OpalPciBus5Bits; break;826case 16: bcomp = OpalPciBus4Bits; break;827case 32: bcomp = OpalPciBus3Bits; break;828default:829dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",830count);831/* Do an exact match only */832bcomp = OpalPciBusAll;833}834rid_end = pe->rid + (count << 8);835} else {836bcomp = OpalPciBusAll;837dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;838fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;839rid_end = pe->rid + 1;840}841842/*843* Associate PE in PELT. We need add the PE into the844* corresponding PELT-V as well. Otherwise, the error845* originated from the PE might contribute to other846* PEs.847*/848rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,849bcomp, dcomp, fcomp, OPAL_MAP_PE);850if (rc) {851pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);852return -ENXIO;853}854855/*856* Configure PELTV. NPUs don't have a PELTV table so skip857* configuration on them.858*/859if (phb->type != PNV_PHB_NPU_OCAPI)860pnv_ioda_set_peltv(phb, pe, true);861862/* Setup reverse map */863for (rid = pe->rid; rid < rid_end; rid++)864phb->ioda.pe_rmap[rid] = pe->pe_number;865866pe->mve_number = 0;867868return 0;869}870871static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)872{873struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);874struct pci_dn *pdn = pci_get_pdn(dev);875struct pnv_ioda_pe *pe;876877if (!pdn) {878pr_err("%s: Device tree node not associated properly\n",879pci_name(dev));880return NULL;881}882if (pdn->pe_number != IODA_INVALID_PE)883return NULL;884885pe = pnv_ioda_alloc_pe(phb, 1);886if (!pe) {887pr_warn("%s: Not enough PE# available, disabling device\n",888pci_name(dev));889return NULL;890}891892/* NOTE: We don't get a reference for the pointer in the PE893* data structure, both the device and PE structures should be894* destroyed at the same time.895*896* At some point we want to remove the PDN completely anyways897*/898pdn->pe_number = pe->pe_number;899pe->flags = PNV_IODA_PE_DEV;900pe->pdev = dev;901pe->pbus = NULL;902pe->mve_number = -1;903pe->rid = dev->bus->number << 8 | pdn->devfn;904pe->device_count++;905906pe_info(pe, "Associated device to PE\n");907908if (pnv_ioda_configure_pe(phb, pe)) {909/* XXX What do we do here ? */910pnv_ioda_free_pe(pe);911pdn->pe_number = IODA_INVALID_PE;912pe->pdev = NULL;913return NULL;914}915916/* Put PE to the list */917mutex_lock(&phb->ioda.pe_list_mutex);918list_add_tail(&pe->list, &phb->ioda.pe_list);919mutex_unlock(&phb->ioda.pe_list_mutex);920return pe;921}922923/*924* There're 2 types of PCI bus sensitive PEs: One that is compromised of925* single PCI bus. Another one that contains the primary PCI bus and its926* subordinate PCI devices and buses. The second type of PE is normally927* orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.928*/929static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)930{931struct pnv_phb *phb = pci_bus_to_pnvhb(bus);932struct pnv_ioda_pe *pe = NULL;933unsigned int pe_num;934935/*936* In partial hotplug case, the PE instance might be still alive.937* We should reuse it instead of allocating a new one.938*/939pe_num = phb->ioda.pe_rmap[bus->number << 8];940if (WARN_ON(pe_num != IODA_INVALID_PE)) {941pe = &phb->ioda.pe_array[pe_num];942return NULL;943}944945/* PE number for root bus should have been reserved */946if (pci_is_root_bus(bus))947pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];948949/* Check if PE is determined by M64 */950if (!pe)951pe = pnv_ioda_pick_m64_pe(bus, all);952953/* The PE number isn't pinned by M64 */954if (!pe)955pe = pnv_ioda_alloc_pe(phb, 1);956957if (!pe) {958pr_warn("%s: Not enough PE# available for PCI bus %04x:%02x\n",959__func__, pci_domain_nr(bus), bus->number);960return NULL;961}962963pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);964pe->pbus = bus;965pe->pdev = NULL;966pe->mve_number = -1;967pe->rid = bus->busn_res.start << 8;968969if (all)970pe_info(pe, "Secondary bus %pad..%pad associated with PE#%x\n",971&bus->busn_res.start, &bus->busn_res.end,972pe->pe_number);973else974pe_info(pe, "Secondary bus %pad associated with PE#%x\n",975&bus->busn_res.start, pe->pe_number);976977if (pnv_ioda_configure_pe(phb, pe)) {978/* XXX What do we do here ? */979pnv_ioda_free_pe(pe);980pe->pbus = NULL;981return NULL;982}983984/* Put PE to the list */985list_add_tail(&pe->list, &phb->ioda.pe_list);986987return pe;988}989990static void pnv_pci_ioda_dma_dev_setup(struct pci_dev *pdev)991{992struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);993struct pci_dn *pdn = pci_get_pdn(pdev);994struct pnv_ioda_pe *pe;995996/* Check if the BDFN for this device is associated with a PE yet */997pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));998if (!pe) {999/* VF PEs should be pre-configured in pnv_pci_sriov_enable() */1000if (WARN_ON(pdev->is_virtfn))1001return;10021003pnv_pci_configure_bus(pdev->bus);1004pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));1005pci_info(pdev, "Configured PE#%x\n", pe ? pe->pe_number : 0xfffff);100610071008/*1009* If we can't setup the IODA PE something has gone horribly1010* wrong and we can't enable DMA for the device.1011*/1012if (WARN_ON(!pe))1013return;1014} else {1015pci_info(pdev, "Added to existing PE#%x\n", pe->pe_number);1016}10171018/*1019* We assume that bridges *probably* don't need to do any DMA so we can1020* skip allocating a TCE table, etc unless we get a non-bridge device.1021*/1022if (!pe->dma_setup_done && !pci_is_bridge(pdev)) {1023switch (phb->type) {1024case PNV_PHB_IODA2:1025pnv_pci_ioda2_setup_dma_pe(phb, pe);1026break;1027default:1028pr_warn("%s: No DMA for PHB#%x (type %d)\n",1029__func__, phb->hose->global_number, phb->type);1030}1031}10321033if (pdn)1034pdn->pe_number = pe->pe_number;1035pe->device_count++;10361037WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);1038pdev->dev.archdata.dma_offset = pe->tce_bypass_base;1039set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);10401041/* PEs with a DMA weight of zero won't have a group */1042if (pe->table_group.group)1043iommu_add_device(&pe->table_group, &pdev->dev);1044}10451046/*1047* Reconfigure TVE#0 to be usable as 64-bit DMA space.1048*1049* The first 4GB of virtual memory for a PE is reserved for 32-bit accesses.1050* Devices can only access more than that if bit 59 of the PCI address is set1051* by hardware, which indicates TVE#1 should be used instead of TVE#0.1052* Many PCI devices are not capable of addressing that many bits, and as a1053* result are limited to the 4GB of virtual memory made available to 32-bit1054* devices in TVE#0.1055*1056* In order to work around this, reconfigure TVE#0 to be suitable for 64-bit1057* devices by configuring the virtual memory past the first 4GB inaccessible1058* by 64-bit DMAs. This should only be used by devices that want more than1059* 4GB, and only on PEs that have no 32-bit devices.1060*1061* Currently this will only work on PHB3 (POWER8).1062*/1063static int pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe *pe)1064{1065u64 window_size, table_size, tce_count, addr;1066struct page *table_pages;1067u64 tce_order = 28; /* 256MB TCEs */1068__be64 *tces;1069s64 rc;10701071/*1072* Window size needs to be a power of two, but needs to account for1073* shifting memory by the 4GB offset required to skip 32bit space.1074*/1075window_size = roundup_pow_of_two(memory_hotplug_max() + (1ULL << 32));1076tce_count = window_size >> tce_order;1077table_size = tce_count << 3;10781079if (table_size < PAGE_SIZE)1080table_size = PAGE_SIZE;10811082table_pages = alloc_pages_node(pe->phb->hose->node, GFP_KERNEL,1083get_order(table_size));1084if (!table_pages)1085goto err;10861087tces = page_address(table_pages);1088if (!tces)1089goto err;10901091memset(tces, 0, table_size);10921093for (addr = 0; addr < memory_hotplug_max(); addr += (1 << tce_order)) {1094tces[(addr + (1ULL << 32)) >> tce_order] =1095cpu_to_be64(addr | TCE_PCI_READ | TCE_PCI_WRITE);1096}10971098rc = opal_pci_map_pe_dma_window(pe->phb->opal_id,1099pe->pe_number,1100/* reconfigure window 0 */1101(pe->pe_number << 1) + 0,11021,1103__pa(tces),1104table_size,11051 << tce_order);1106if (rc == OPAL_SUCCESS) {1107pe_info(pe, "Using 64-bit DMA iommu bypass (through TVE#0)\n");1108return 0;1109}1110err:1111pe_err(pe, "Error configuring 64-bit DMA bypass\n");1112return -EIO;1113}11141115static bool pnv_pci_ioda_iommu_bypass_supported(struct pci_dev *pdev,1116u64 dma_mask)1117{1118struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);1119struct pci_dn *pdn = pci_get_pdn(pdev);1120struct pnv_ioda_pe *pe;11211122if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))1123return false;11241125pe = &phb->ioda.pe_array[pdn->pe_number];1126if (pe->tce_bypass_enabled) {1127u64 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;1128if (dma_mask >= top)1129return true;1130}11311132/*1133* If the device can't set the TCE bypass bit but still wants1134* to access 4GB or more, on PHB3 we can reconfigure TVE#0 to1135* bypass the 32-bit region and be usable for 64-bit DMAs.1136* The device needs to be able to address all of this space.1137*/1138if (dma_mask >> 32 &&1139dma_mask > (memory_hotplug_max() + (1ULL << 32)) &&1140/* pe->pdev should be set if it's a single device, pe->pbus if not */1141(pe->device_count == 1 || !pe->pbus) &&1142phb->model == PNV_PHB_MODEL_PHB3) {1143/* Configure the bypass mode */1144s64 rc = pnv_pci_ioda_dma_64bit_bypass(pe);1145if (rc)1146return false;1147/* 4GB offset bypasses 32-bit space */1148pdev->dev.archdata.dma_offset = (1ULL << 32);1149return true;1150}11511152return false;1153}11541155static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb)1156{1157return phb->regs + 0x210;1158}11591160#ifdef CONFIG_IOMMU_API1161/* Common for IODA1 and IODA2 */1162static int pnv_ioda_tce_xchg_no_kill(struct iommu_table *tbl, long index,1163unsigned long *hpa, enum dma_data_direction *direction)1164{1165return pnv_tce_xchg(tbl, index, hpa, direction);1166}1167#endif11681169#define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)1170#define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)1171#define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)11721173static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)1174{1175/* 01xb - invalidate TCEs that match the specified PE# */1176__be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb);1177unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);11781179mb(); /* Ensure above stores are visible */1180__raw_writeq_be(val, invalidate);1181}11821183static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe,1184unsigned shift, unsigned long index,1185unsigned long npages)1186{1187__be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb);1188unsigned long start, end, inc;11891190/* We'll invalidate DMA address in PE scope */1191start = PHB3_TCE_KILL_INVAL_ONE;1192start |= (pe->pe_number & 0xFF);1193end = start;11941195/* Figure out the start, end and step */1196start |= (index << shift);1197end |= ((index + npages - 1) << shift);1198inc = (0x1ull << shift);1199mb();12001201while (start <= end) {1202__raw_writeq_be(start, invalidate);1203start += inc;1204}1205}12061207static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)1208{1209struct pnv_phb *phb = pe->phb;12101211if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)1212pnv_pci_phb3_tce_invalidate_pe(pe);1213else1214opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE,1215pe->pe_number, 0, 0, 0);1216}12171218static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,1219unsigned long index, unsigned long npages)1220{1221struct iommu_table_group_link *tgl;12221223list_for_each_entry_lockless(tgl, &tbl->it_group_list, next) {1224struct pnv_ioda_pe *pe = container_of(tgl->table_group,1225struct pnv_ioda_pe, table_group);1226struct pnv_phb *phb = pe->phb;1227unsigned int shift = tbl->it_page_shift;12281229if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)1230pnv_pci_phb3_tce_invalidate(pe, shift,1231index, npages);1232else1233opal_pci_tce_kill(phb->opal_id,1234OPAL_PCI_TCE_KILL_PAGES,1235pe->pe_number, 1u << shift,1236index << shift, npages);1237}1238}12391240static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,1241long npages, unsigned long uaddr,1242enum dma_data_direction direction,1243unsigned long attrs)1244{1245int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,1246attrs);12471248if (!ret)1249pnv_pci_ioda2_tce_invalidate(tbl, index, npages);12501251return ret;1252}12531254static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,1255long npages)1256{1257pnv_tce_free(tbl, index, npages);12581259pnv_pci_ioda2_tce_invalidate(tbl, index, npages);1260}12611262static struct iommu_table_ops pnv_ioda2_iommu_ops = {1263.set = pnv_ioda2_tce_build,1264#ifdef CONFIG_IOMMU_API1265.xchg_no_kill = pnv_ioda_tce_xchg_no_kill,1266.tce_kill = pnv_pci_ioda2_tce_invalidate,1267.useraddrptr = pnv_tce_useraddrptr,1268#endif1269.clear = pnv_ioda2_tce_free,1270.get = pnv_tce_get,1271.free = pnv_pci_ioda2_table_free_pages,1272};12731274static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,1275int num, struct iommu_table *tbl)1276{1277struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,1278table_group);1279struct pnv_phb *phb = pe->phb;1280int64_t rc;1281const unsigned long size = tbl->it_indirect_levels ?1282tbl->it_level_size : tbl->it_size;1283const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;1284const __u64 win_size = tbl->it_size << tbl->it_page_shift;12851286pe_info(pe, "Setting up window#%d %llx..%llx pg=%lx\n",1287num, start_addr, start_addr + win_size - 1,1288IOMMU_PAGE_SIZE(tbl));12891290/*1291* Map TCE table through TVT. The TVE index is the PE number1292* shifted by 1 bit for 32-bits DMA space.1293*/1294rc = opal_pci_map_pe_dma_window(phb->opal_id,1295pe->pe_number,1296(pe->pe_number << 1) + num,1297tbl->it_indirect_levels + 1,1298__pa(tbl->it_base),1299size << 3,1300IOMMU_PAGE_SIZE(tbl));1301if (rc) {1302pe_err(pe, "Failed to configure TCE table, err %lld\n", rc);1303return rc;1304}13051306pnv_pci_link_table_and_group(phb->hose->node, num,1307tbl, &pe->table_group);1308pnv_pci_ioda2_tce_invalidate_pe(pe);13091310return 0;1311}13121313static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)1314{1315uint16_t window_id = (pe->pe_number << 1 ) + 1;1316int64_t rc;13171318pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");1319if (enable) {1320phys_addr_t top = memblock_end_of_DRAM();13211322top = roundup_pow_of_two(top);1323rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,1324pe->pe_number,1325window_id,1326pe->tce_bypass_base,1327top);1328} else {1329rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,1330pe->pe_number,1331window_id,1332pe->tce_bypass_base,13330);1334}1335if (rc)1336pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);1337else1338pe->tce_bypass_enabled = enable;1339}13401341static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,1342int num, __u32 page_shift, __u64 window_size, __u32 levels,1343bool alloc_userspace_copy, struct iommu_table **ptbl)1344{1345struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,1346table_group);1347int nid = pe->phb->hose->node;1348__u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;1349long ret;1350struct iommu_table *tbl;13511352tbl = pnv_pci_table_alloc(nid);1353if (!tbl)1354return -ENOMEM;13551356tbl->it_ops = &pnv_ioda2_iommu_ops;13571358ret = pnv_pci_ioda2_table_alloc_pages(nid,1359bus_offset, page_shift, window_size,1360levels, alloc_userspace_copy, tbl);1361if (ret) {1362iommu_tce_table_put(tbl);1363return ret;1364}13651366*ptbl = tbl;13671368return 0;1369}13701371static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)1372{1373struct iommu_table *tbl = NULL;1374long rc;1375unsigned long res_start, res_end;13761377/*1378* crashkernel= specifies the kdump kernel's maximum memory at1379* some offset and there is no guaranteed the result is a power1380* of 2, which will cause errors later.1381*/1382const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());13831384/*1385* In memory constrained environments, e.g. kdump kernel, the1386* DMA window can be larger than available memory, which will1387* cause errors later.1388*/1389const u64 maxblock = 1UL << (PAGE_SHIFT + MAX_PAGE_ORDER);13901391/*1392* We create the default window as big as we can. The constraint is1393* the max order of allocation possible. The TCE table is likely to1394* end up being multilevel and with on-demand allocation in place,1395* the initial use is not going to be huge as the default window aims1396* to support crippled devices (i.e. not fully 64bit DMAble) only.1397*/1398/* iommu_table::it_map uses 1 bit per IOMMU page, hence 8 */1399const u64 window_size = min((maxblock * 8) << PAGE_SHIFT, max_memory);1400/* Each TCE level cannot exceed maxblock so go multilevel if needed */1401unsigned long tces_order = ilog2(window_size >> PAGE_SHIFT);1402unsigned long tcelevel_order = ilog2(maxblock >> 3);1403unsigned int levels = tces_order / tcelevel_order;14041405if (tces_order % tcelevel_order)1406levels += 1;1407/*1408* We try to stick to default levels (which is >1 at the moment) in1409* order to save memory by relying on on-demain TCE level allocation.1410*/1411levels = max_t(unsigned int, levels, POWERNV_IOMMU_DEFAULT_LEVELS);14121413rc = pnv_pci_ioda2_create_table(&pe->table_group, 0, PAGE_SHIFT,1414window_size, levels, false, &tbl);1415if (rc) {1416pe_err(pe, "Failed to create 32-bit TCE table, err %ld",1417rc);1418return rc;1419}14201421/* We use top part of 32bit space for MMIO so exclude it from DMA */1422res_start = 0;1423res_end = 0;1424if (window_size > pe->phb->ioda.m32_pci_base) {1425res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift;1426res_end = min(window_size, SZ_4G) >> tbl->it_page_shift;1427}14281429tbl->it_index = (pe->phb->hose->global_number << 16) | pe->pe_number;1430if (iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end))1431rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);1432else1433rc = -ENOMEM;1434if (rc) {1435pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", rc);1436iommu_tce_table_put(tbl);1437tbl = NULL; /* This clears iommu_table_base below */1438}1439if (!pnv_iommu_bypass_disabled)1440pnv_pci_ioda2_set_bypass(pe, true);14411442/*1443* Set table base for the case of IOMMU DMA use. Usually this is done1444* from dma_dev_setup() which is not called when a device is returned1445* from VFIO so do it here.1446*/1447if (pe->pdev)1448set_iommu_table_base(&pe->pdev->dev, tbl);14491450return 0;1451}14521453static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,1454int num)1455{1456struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,1457table_group);1458struct pnv_phb *phb = pe->phb;1459long ret;14601461pe_info(pe, "Removing DMA window #%d\n", num);14621463ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,1464(pe->pe_number << 1) + num,14650/* levels */, 0/* table address */,14660/* table size */, 0/* page size */);1467if (ret)1468pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);1469else1470pnv_pci_ioda2_tce_invalidate_pe(pe);14711472pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);14731474return ret;1475}14761477#ifdef CONFIG_IOMMU_API1478unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,1479__u64 window_size, __u32 levels)1480{1481unsigned long bytes = 0;1482const unsigned window_shift = ilog2(window_size);1483unsigned entries_shift = window_shift - page_shift;1484unsigned table_shift = entries_shift + 3;1485unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);1486unsigned long direct_table_size;14871488if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||1489!is_power_of_2(window_size))1490return 0;14911492/* Calculate a direct table size from window_size and levels */1493entries_shift = (entries_shift + levels - 1) / levels;1494table_shift = entries_shift + 3;1495table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);1496direct_table_size = 1UL << table_shift;14971498for ( ; levels; --levels) {1499bytes += ALIGN(tce_table_size, direct_table_size);15001501tce_table_size /= direct_table_size;1502tce_table_size <<= 3;1503tce_table_size = max_t(unsigned long,1504tce_table_size, direct_table_size);1505}15061507return bytes + bytes; /* one for HW table, one for userspace copy */1508}15091510static long pnv_pci_ioda2_create_table_userspace(1511struct iommu_table_group *table_group,1512int num, __u32 page_shift, __u64 window_size, __u32 levels,1513struct iommu_table **ptbl)1514{1515long ret = pnv_pci_ioda2_create_table(table_group,1516num, page_shift, window_size, levels, true, ptbl);15171518if (!ret)1519(*ptbl)->it_allocated_size = pnv_pci_ioda2_get_table_size(1520page_shift, window_size, levels);1521return ret;1522}15231524static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus)1525{1526struct pci_dev *dev;15271528list_for_each_entry(dev, &bus->devices, bus_list) {1529set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);1530dev->dev.archdata.dma_offset = pe->tce_bypass_base;15311532if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)1533pnv_ioda_setup_bus_dma(pe, dev->subordinate);1534}1535}15361537static long pnv_ioda2_take_ownership(struct iommu_table_group *table_group,1538struct device *dev __maybe_unused)1539{1540struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,1541table_group);1542/* Store @tbl as pnv_pci_ioda2_unset_window() resets it */1543struct iommu_table *tbl = pe->table_group.tables[0];15441545/*1546* iommu_ops transfers the ownership per a device and we mode1547* the group ownership with the first device in the group.1548*/1549if (!tbl)1550return 0;15511552pnv_pci_ioda2_set_bypass(pe, false);1553pnv_pci_ioda2_unset_window(&pe->table_group, 0);1554if (pe->pbus)1555pnv_ioda_setup_bus_dma(pe, pe->pbus);1556else if (pe->pdev)1557set_iommu_table_base(&pe->pdev->dev, NULL);1558iommu_tce_table_put(tbl);15591560return 0;1561}15621563static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group,1564struct device *dev __maybe_unused)1565{1566struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,1567table_group);15681569/* See the comment about iommu_ops above */1570if (pe->table_group.tables[0])1571return;1572pnv_pci_ioda2_setup_default_config(pe);1573if (pe->pbus)1574pnv_ioda_setup_bus_dma(pe, pe->pbus);1575}15761577static struct iommu_table_group_ops pnv_pci_ioda2_ops = {1578.get_table_size = pnv_pci_ioda2_get_table_size,1579.create_table = pnv_pci_ioda2_create_table_userspace,1580.set_window = pnv_pci_ioda2_set_window,1581.unset_window = pnv_pci_ioda2_unset_window,1582.take_ownership = pnv_ioda2_take_ownership,1583.release_ownership = pnv_ioda2_release_ownership,1584};1585#endif15861587void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,1588struct pnv_ioda_pe *pe)1589{1590int64_t rc;15911592/* TVE #1 is selected by PCI address bit 59 */1593pe->tce_bypass_base = 1ull << 59;15941595/* The PE will reserve all possible 32-bits space */1596pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",1597phb->ioda.m32_pci_base);15981599/* Setup linux iommu table */1600pe->table_group.tce32_start = 0;1601pe->table_group.tce32_size = phb->ioda.m32_pci_base;1602pe->table_group.max_dynamic_windows_supported =1603IOMMU_TABLE_GROUP_MAX_TABLES;1604pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;1605pe->table_group.pgsizes = pnv_ioda_parse_tce_sizes(phb);16061607rc = pnv_pci_ioda2_setup_default_config(pe);1608if (rc)1609return;16101611#ifdef CONFIG_IOMMU_API1612pe->table_group.ops = &pnv_pci_ioda2_ops;1613iommu_register_group(&pe->table_group, phb->hose->global_number,1614pe->pe_number);1615#endif1616pe->dma_setup_done = true;1617}16181619/*1620* Called from KVM in real mode to EOI passthru interrupts. The ICP1621* EOI is handled directly in KVM in kvmppc_deliver_irq_passthru().1622*1623* The IRQ data is mapped in the PCI-MSI domain and the EOI OPAL call1624* needs an HW IRQ number mapped in the XICS IRQ domain. The HW IRQ1625* numbers of the in-the-middle MSI domain are vector numbers and it's1626* good enough for OPAL. Use that.1627*/1628int64_t pnv_opal_pci_msi_eoi(struct irq_data *d)1629{1630struct pci_controller *hose = irq_data_get_irq_chip_data(d->parent_data);1631struct pnv_phb *phb = hose->private_data;16321633return opal_pci_msi_eoi(phb->opal_id, d->parent_data->hwirq);1634}16351636static struct irq_chip pnv_pci_msi_irq_chip;16371638/*1639* Returns true iff chip is something that we could call1640* pnv_opal_pci_msi_eoi for.1641*/1642bool is_pnv_opal_msi(struct irq_chip *chip)1643{1644return chip == &pnv_pci_msi_irq_chip;1645}1646EXPORT_SYMBOL_GPL(is_pnv_opal_msi);16471648static int __pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,1649unsigned int xive_num,1650unsigned int is_64, struct msi_msg *msg)1651{1652struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);1653__be32 data;1654int rc;16551656dev_dbg(&dev->dev, "%s: setup %s-bit MSI for vector #%d\n", __func__,1657is_64 ? "64" : "32", xive_num);16581659/* No PE assigned ? bail out ... no MSI for you ! */1660if (pe == NULL)1661return -ENXIO;16621663/* Check if we have an MVE */1664if (pe->mve_number < 0)1665return -ENXIO;16661667/* Force 32-bit MSI on some broken devices */1668if (dev->no_64bit_msi)1669is_64 = 0;16701671/* Assign XIVE to PE */1672rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);1673if (rc) {1674pr_warn("%s: OPAL error %d setting XIVE %d PE\n",1675pci_name(dev), rc, xive_num);1676return -EIO;1677}16781679if (is_64) {1680__be64 addr64;16811682rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,1683&addr64, &data);1684if (rc) {1685pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",1686pci_name(dev), rc);1687return -EIO;1688}1689msg->address_hi = be64_to_cpu(addr64) >> 32;1690msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;1691} else {1692__be32 addr32;16931694rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,1695&addr32, &data);1696if (rc) {1697pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",1698pci_name(dev), rc);1699return -EIO;1700}1701msg->address_hi = 0;1702msg->address_lo = be32_to_cpu(addr32);1703}1704msg->data = be32_to_cpu(data);17051706return 0;1707}17081709/*1710* The msi_free() op is called before irq_domain_free_irqs_top() when1711* the handler data is still available. Use that to clear the XIVE1712* controller.1713*/1714static void pnv_msi_ops_msi_free(struct irq_domain *domain,1715struct msi_domain_info *info,1716unsigned int irq)1717{1718if (xive_enabled())1719xive_irq_free_data(irq);1720}17211722static struct msi_domain_ops pnv_pci_msi_domain_ops = {1723.msi_free = pnv_msi_ops_msi_free,1724};17251726static void pnv_msi_shutdown(struct irq_data *d)1727{1728d = d->parent_data;1729if (d->chip->irq_shutdown)1730d->chip->irq_shutdown(d);1731}17321733static void pnv_msi_mask(struct irq_data *d)1734{1735pci_msi_mask_irq(d);1736irq_chip_mask_parent(d);1737}17381739static void pnv_msi_unmask(struct irq_data *d)1740{1741pci_msi_unmask_irq(d);1742irq_chip_unmask_parent(d);1743}17441745static struct irq_chip pnv_pci_msi_irq_chip = {1746.name = "PNV-PCI-MSI",1747.irq_shutdown = pnv_msi_shutdown,1748.irq_mask = pnv_msi_mask,1749.irq_unmask = pnv_msi_unmask,1750.irq_eoi = irq_chip_eoi_parent,1751};17521753static struct msi_domain_info pnv_msi_domain_info = {1754.flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |1755MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),1756.ops = &pnv_pci_msi_domain_ops,1757.chip = &pnv_pci_msi_irq_chip,1758};17591760static void pnv_msi_compose_msg(struct irq_data *d, struct msi_msg *msg)1761{1762struct msi_desc *entry = irq_data_get_msi_desc(d);1763struct pci_dev *pdev = msi_desc_to_pci_dev(entry);1764struct pci_controller *hose = irq_data_get_irq_chip_data(d);1765struct pnv_phb *phb = hose->private_data;1766int rc;17671768rc = __pnv_pci_ioda_msi_setup(phb, pdev, d->hwirq,1769entry->pci.msi_attrib.is_64, msg);1770if (rc)1771dev_err(&pdev->dev, "Failed to setup %s-bit MSI #%ld : %d\n",1772entry->pci.msi_attrib.is_64 ? "64" : "32", d->hwirq, rc);1773}17741775/*1776* The IRQ data is mapped in the MSI domain in which HW IRQ numbers1777* correspond to vector numbers.1778*/1779static void pnv_msi_eoi(struct irq_data *d)1780{1781struct pci_controller *hose = irq_data_get_irq_chip_data(d);1782struct pnv_phb *phb = hose->private_data;17831784if (phb->model == PNV_PHB_MODEL_PHB3) {1785/*1786* The EOI OPAL call takes an OPAL HW IRQ number but1787* since it is translated into a vector number in1788* OPAL, use that directly.1789*/1790WARN_ON_ONCE(opal_pci_msi_eoi(phb->opal_id, d->hwirq));1791}17921793irq_chip_eoi_parent(d);1794}17951796static struct irq_chip pnv_msi_irq_chip = {1797.name = "PNV-MSI",1798.irq_shutdown = pnv_msi_shutdown,1799.irq_mask = irq_chip_mask_parent,1800.irq_unmask = irq_chip_unmask_parent,1801.irq_eoi = pnv_msi_eoi,1802.irq_set_affinity = irq_chip_set_affinity_parent,1803.irq_compose_msi_msg = pnv_msi_compose_msg,1804};18051806static int pnv_irq_parent_domain_alloc(struct irq_domain *domain,1807unsigned int virq, int hwirq)1808{1809struct irq_fwspec parent_fwspec;1810int ret;18111812parent_fwspec.fwnode = domain->parent->fwnode;1813parent_fwspec.param_count = 2;1814parent_fwspec.param[0] = hwirq;1815parent_fwspec.param[1] = IRQ_TYPE_EDGE_RISING;18161817ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);1818if (ret)1819return ret;18201821return 0;1822}18231824static int pnv_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,1825unsigned int nr_irqs, void *arg)1826{1827struct pci_controller *hose = domain->host_data;1828struct pnv_phb *phb = hose->private_data;1829msi_alloc_info_t *info = arg;1830struct pci_dev *pdev = msi_desc_to_pci_dev(info->desc);1831int hwirq;1832int i, ret;18331834hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, nr_irqs);1835if (hwirq < 0) {1836dev_warn(&pdev->dev, "failed to find a free MSI\n");1837return -ENOSPC;1838}18391840dev_dbg(&pdev->dev, "%s bridge %pOF %d/%x #%d\n", __func__,1841hose->dn, virq, hwirq, nr_irqs);18421843for (i = 0; i < nr_irqs; i++) {1844ret = pnv_irq_parent_domain_alloc(domain, virq + i,1845phb->msi_base + hwirq + i);1846if (ret)1847goto out;18481849irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,1850&pnv_msi_irq_chip, hose);1851}18521853return 0;18541855out:1856irq_domain_free_irqs_parent(domain, virq, i - 1);1857msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, nr_irqs);1858return ret;1859}18601861static void pnv_irq_domain_free(struct irq_domain *domain, unsigned int virq,1862unsigned int nr_irqs)1863{1864struct irq_data *d = irq_domain_get_irq_data(domain, virq);1865struct pci_controller *hose = irq_data_get_irq_chip_data(d);1866struct pnv_phb *phb = hose->private_data;18671868pr_debug("%s bridge %pOF %d/%lx #%d\n", __func__, hose->dn,1869virq, d->hwirq, nr_irqs);18701871msi_bitmap_free_hwirqs(&phb->msi_bmp, d->hwirq, nr_irqs);1872/* XIVE domain is cleared through ->msi_free() */1873}18741875static const struct irq_domain_ops pnv_irq_domain_ops = {1876.alloc = pnv_irq_domain_alloc,1877.free = pnv_irq_domain_free,1878};18791880static int __init pnv_msi_allocate_domains(struct pci_controller *hose, unsigned int count)1881{1882struct pnv_phb *phb = hose->private_data;1883struct irq_domain *parent = irq_get_default_domain();18841885hose->fwnode = irq_domain_alloc_named_id_fwnode("PNV-MSI", phb->opal_id);1886if (!hose->fwnode)1887return -ENOMEM;18881889hose->dev_domain = irq_domain_create_hierarchy(parent, 0, count,1890hose->fwnode,1891&pnv_irq_domain_ops, hose);1892if (!hose->dev_domain) {1893pr_err("PCI: failed to create IRQ domain bridge %pOF (domain %d)\n",1894hose->dn, hose->global_number);1895irq_domain_free_fwnode(hose->fwnode);1896return -ENOMEM;1897}18981899hose->msi_domain = pci_msi_create_irq_domain(of_fwnode_handle(hose->dn),1900&pnv_msi_domain_info,1901hose->dev_domain);1902if (!hose->msi_domain) {1903pr_err("PCI: failed to create MSI IRQ domain bridge %pOF (domain %d)\n",1904hose->dn, hose->global_number);1905irq_domain_free_fwnode(hose->fwnode);1906irq_domain_remove(hose->dev_domain);1907return -ENOMEM;1908}19091910return 0;1911}19121913static void __init pnv_pci_init_ioda_msis(struct pnv_phb *phb)1914{1915unsigned int count;1916const __be32 *prop = of_get_property(phb->hose->dn,1917"ibm,opal-msi-ranges", NULL);1918if (!prop) {1919/* BML Fallback */1920prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);1921}1922if (!prop)1923return;19241925phb->msi_base = be32_to_cpup(prop);1926count = be32_to_cpup(prop + 1);1927if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {1928pr_err("PCI %d: Failed to allocate MSI bitmap !\n",1929phb->hose->global_number);1930return;1931}19321933pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",1934count, phb->msi_base);19351936pnv_msi_allocate_domains(phb->hose, count);1937}19381939static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,1940struct resource *res)1941{1942struct pnv_phb *phb = pe->phb;1943struct pci_bus_region region;1944int index;1945int64_t rc;19461947if (!res || !res->flags || res->start > res->end ||1948res->flags & IORESOURCE_UNSET)1949return;19501951if (res->flags & IORESOURCE_IO) {1952region.start = res->start - phb->ioda.io_pci_base;1953region.end = res->end - phb->ioda.io_pci_base;1954index = region.start / phb->ioda.io_segsize;19551956while (index < phb->ioda.total_pe_num &&1957region.start <= region.end) {1958phb->ioda.io_segmap[index] = pe->pe_number;1959rc = opal_pci_map_pe_mmio_window(phb->opal_id,1960pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);1961if (rc != OPAL_SUCCESS) {1962pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",1963__func__, rc, index, pe->pe_number);1964break;1965}19661967region.start += phb->ioda.io_segsize;1968index++;1969}1970} else if ((res->flags & IORESOURCE_MEM) &&1971!pnv_pci_is_m64(phb, res)) {1972region.start = res->start -1973phb->hose->mem_offset[0] -1974phb->ioda.m32_pci_base;1975region.end = res->end -1976phb->hose->mem_offset[0] -1977phb->ioda.m32_pci_base;1978index = region.start / phb->ioda.m32_segsize;19791980while (index < phb->ioda.total_pe_num &&1981region.start <= region.end) {1982phb->ioda.m32_segmap[index] = pe->pe_number;1983rc = opal_pci_map_pe_mmio_window(phb->opal_id,1984pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);1985if (rc != OPAL_SUCCESS) {1986pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",1987__func__, rc, index, pe->pe_number);1988break;1989}19901991region.start += phb->ioda.m32_segsize;1992index++;1993}1994}1995}19961997/*1998* This function is supposed to be called on basis of PE from top1999* to bottom style. So the I/O or MMIO segment assigned to2000* parent PE could be overridden by its child PEs if necessary.2001*/2002static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)2003{2004struct pci_dev *pdev;2005int i;20062007/*2008* NOTE: We only care PCI bus based PE for now. For PCI2009* device based PE, for example SRIOV sensitive VF should2010* be figured out later.2011*/2012BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));20132014list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {2015for (i = 0; i <= PCI_ROM_RESOURCE; i++)2016pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);20172018/*2019* If the PE contains all subordinate PCI buses, the2020* windows of the child bridges should be mapped to2021* the PE as well.2022*/2023if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))2024continue;2025for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)2026pnv_ioda_setup_pe_res(pe,2027&pdev->resource[PCI_BRIDGE_RESOURCES + i]);2028}2029}20302031#ifdef CONFIG_DEBUG_FS2032static int pnv_pci_diag_data_set(void *data, u64 val)2033{2034struct pnv_phb *phb = data;2035s64 ret;20362037/* Retrieve the diag data from firmware */2038ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,2039phb->diag_data_size);2040if (ret != OPAL_SUCCESS)2041return -EIO;20422043/* Print the diag data to the kernel log */2044pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);2045return 0;2046}20472048DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_diag_data_fops, NULL, pnv_pci_diag_data_set,2049"%llu\n");20502051static int pnv_pci_ioda_pe_dump(void *data, u64 val)2052{2053struct pnv_phb *phb = data;2054int pe_num;20552056for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {2057struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_num];20582059if (!test_bit(pe_num, phb->ioda.pe_alloc))2060continue;20612062pe_warn(pe, "rid: %04x dev count: %2d flags: %s%s%s%s%s%s\n",2063pe->rid, pe->device_count,2064(pe->flags & PNV_IODA_PE_DEV) ? "dev " : "",2065(pe->flags & PNV_IODA_PE_BUS) ? "bus " : "",2066(pe->flags & PNV_IODA_PE_BUS_ALL) ? "all " : "",2067(pe->flags & PNV_IODA_PE_MASTER) ? "master " : "",2068(pe->flags & PNV_IODA_PE_SLAVE) ? "slave " : "",2069(pe->flags & PNV_IODA_PE_VF) ? "vf " : "");2070}20712072return 0;2073}20742075DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_ioda_pe_dump_fops, NULL,2076pnv_pci_ioda_pe_dump, "%llu\n");20772078#endif /* CONFIG_DEBUG_FS */20792080static void pnv_pci_ioda_create_dbgfs(void)2081{2082#ifdef CONFIG_DEBUG_FS2083struct pci_controller *hose, *tmp;2084struct pnv_phb *phb;2085char name[16];20862087list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {2088phb = hose->private_data;20892090sprintf(name, "PCI%04x", hose->global_number);2091phb->dbgfs = debugfs_create_dir(name, arch_debugfs_dir);20922093debugfs_create_file_unsafe("dump_diag_regs", 0200, phb->dbgfs,2094phb, &pnv_pci_diag_data_fops);2095debugfs_create_file_unsafe("dump_ioda_pe_state", 0200, phb->dbgfs,2096phb, &pnv_pci_ioda_pe_dump_fops);2097}2098#endif /* CONFIG_DEBUG_FS */2099}21002101static void pnv_pci_enable_bridge(struct pci_bus *bus)2102{2103struct pci_dev *dev = bus->self;2104struct pci_bus *child;21052106/* Empty bus ? bail */2107if (list_empty(&bus->devices))2108return;21092110/*2111* If there's a bridge associated with that bus enable it. This works2112* around races in the generic code if the enabling is done during2113* parallel probing. This can be removed once those races have been2114* fixed.2115*/2116if (dev) {2117int rc = pci_enable_device(dev);2118if (rc)2119pci_err(dev, "Error enabling bridge (%d)\n", rc);2120pci_set_master(dev);2121}21222123/* Perform the same to child busses */2124list_for_each_entry(child, &bus->children, node)2125pnv_pci_enable_bridge(child);2126}21272128static void pnv_pci_enable_bridges(void)2129{2130struct pci_controller *hose;21312132list_for_each_entry(hose, &hose_list, list_node)2133pnv_pci_enable_bridge(hose->bus);2134}21352136static void pnv_pci_ioda_fixup(void)2137{2138pnv_pci_ioda_create_dbgfs();21392140pnv_pci_enable_bridges();21412142#ifdef CONFIG_EEH2143pnv_eeh_post_init();2144#endif2145}21462147/*2148* Returns the alignment for I/O or memory windows for P2P2149* bridges. That actually depends on how PEs are segmented.2150* For now, we return I/O or M32 segment size for PE sensitive2151* P2P bridges. Otherwise, the default values (4KiB for I/O,2152* 1MiB for memory) will be returned.2153*2154* The current PCI bus might be put into one PE, which was2155* create against the parent PCI bridge. For that case, we2156* needn't enlarge the alignment so that we can save some2157* resources.2158*/2159static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,2160unsigned long type)2161{2162struct pnv_phb *phb = pci_bus_to_pnvhb(bus);2163int num_pci_bridges = 0;2164struct pci_dev *bridge;21652166bridge = bus->self;2167while (bridge) {2168if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {2169num_pci_bridges++;2170if (num_pci_bridges >= 2)2171return 1;2172}21732174bridge = bridge->bus->self;2175}21762177/*2178* We fall back to M32 if M64 isn't supported. We enforce the M642179* alignment for any 64-bit resource, PCIe doesn't care and2180* bridges only do 64-bit prefetchable anyway.2181*/2182if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type))2183return phb->ioda.m64_segsize;2184if (type & IORESOURCE_MEM)2185return phb->ioda.m32_segsize;21862187return phb->ioda.io_segsize;2188}21892190/*2191* We are updating root port or the upstream port of the2192* bridge behind the root port with PHB's windows in order2193* to accommodate the changes on required resources during2194* PCI (slot) hotplug, which is connected to either root2195* port or the downstream ports of PCIe switch behind the2196* root port.2197*/2198static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,2199unsigned long type)2200{2201struct pci_controller *hose = pci_bus_to_host(bus);2202struct pnv_phb *phb = hose->private_data;2203struct pci_dev *bridge = bus->self;2204struct resource *r, *w;2205bool msi_region = false;2206int i;22072208/* Check if we need apply fixup to the bridge's windows */2209if (!pci_is_root_bus(bridge->bus) &&2210!pci_is_root_bus(bridge->bus->self->bus))2211return;22122213/* Fixup the resources */2214for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {2215r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];2216if (!r->flags || !r->parent)2217continue;22182219w = NULL;2220if (r->flags & type & IORESOURCE_IO)2221w = &hose->io_resource;2222else if (pnv_pci_is_m64(phb, r) &&2223(type & IORESOURCE_PREFETCH) &&2224phb->ioda.m64_segsize)2225w = &hose->mem_resources[1];2226else if (r->flags & type & IORESOURCE_MEM) {2227w = &hose->mem_resources[0];2228msi_region = true;2229}22302231r->start = w->start;2232r->end = w->end;22332234/* The 64KB 32-bits MSI region shouldn't be included in2235* the 32-bits bridge window. Otherwise, we can see strange2236* issues. One of them is EEH error observed on Garrison.2237*2238* Exclude top 1MB region which is the minimal alignment of2239* 32-bits bridge window.2240*/2241if (msi_region) {2242r->end += 0x10000;2243r->end -= 0x100000;2244}2245}2246}22472248static void pnv_pci_configure_bus(struct pci_bus *bus)2249{2250struct pci_dev *bridge = bus->self;2251struct pnv_ioda_pe *pe;2252bool all = (bridge && pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);22532254dev_info(&bus->dev, "Configuring PE for bus\n");22552256/* Don't assign PE to PCI bus, which doesn't have subordinate devices */2257if (WARN_ON(list_empty(&bus->devices)))2258return;22592260/* Reserve PEs according to used M64 resources */2261pnv_ioda_reserve_m64_pe(bus, NULL, all);22622263/*2264* Assign PE. We might run here because of partial hotplug.2265* For the case, we just pick up the existing PE and should2266* not allocate resources again.2267*/2268pe = pnv_ioda_setup_bus_PE(bus, all);2269if (!pe)2270return;22712272pnv_ioda_setup_pe_seg(pe);2273}22742275static resource_size_t pnv_pci_default_alignment(void)2276{2277return PAGE_SIZE;2278}22792280/* Prevent enabling devices for which we couldn't properly2281* assign a PE2282*/2283static bool pnv_pci_enable_device_hook(struct pci_dev *dev)2284{2285struct pci_dn *pdn;22862287pdn = pci_get_pdn(dev);2288if (!pdn || pdn->pe_number == IODA_INVALID_PE) {2289pci_err(dev, "pci_enable_device() blocked, no PE assigned.\n");2290return false;2291}22922293return true;2294}22952296static bool pnv_ocapi_enable_device_hook(struct pci_dev *dev)2297{2298struct pci_dn *pdn;2299struct pnv_ioda_pe *pe;23002301pdn = pci_get_pdn(dev);2302if (!pdn)2303return false;23042305if (pdn->pe_number == IODA_INVALID_PE) {2306pe = pnv_ioda_setup_dev_PE(dev);2307if (!pe)2308return false;2309}2310return true;2311}23122313void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)2314{2315struct iommu_table *tbl = pe->table_group.tables[0];2316int64_t rc;23172318if (!pe->dma_setup_done)2319return;23202321rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);2322if (rc)2323pe_warn(pe, "OPAL error %lld release DMA window\n", rc);23242325pnv_pci_ioda2_set_bypass(pe, false);2326if (pe->table_group.group) {2327iommu_group_put(pe->table_group.group);2328WARN_ON(pe->table_group.group);2329}23302331iommu_tce_table_put(tbl);2332}23332334static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,2335unsigned short win,2336unsigned int *map)2337{2338struct pnv_phb *phb = pe->phb;2339int idx;2340int64_t rc;23412342for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {2343if (map[idx] != pe->pe_number)2344continue;23452346rc = opal_pci_map_pe_mmio_window(phb->opal_id,2347phb->ioda.reserved_pe_idx, win, 0, idx);23482349if (rc != OPAL_SUCCESS)2350pe_warn(pe, "Error %lld unmapping (%d) segment#%d\n",2351rc, win, idx);23522353map[idx] = IODA_INVALID_PE;2354}2355}23562357static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)2358{2359struct pnv_phb *phb = pe->phb;23602361if (phb->type == PNV_PHB_IODA2) {2362pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,2363phb->ioda.m32_segmap);2364}2365}23662367static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)2368{2369struct pnv_phb *phb = pe->phb;2370struct pnv_ioda_pe *slave, *tmp;23712372pe_info(pe, "Releasing PE\n");23732374mutex_lock(&phb->ioda.pe_list_mutex);2375list_del(&pe->list);2376mutex_unlock(&phb->ioda.pe_list_mutex);23772378switch (phb->type) {2379case PNV_PHB_IODA2:2380pnv_pci_ioda2_release_pe_dma(pe);2381break;2382case PNV_PHB_NPU_OCAPI:2383break;2384default:2385WARN_ON(1);2386}23872388pnv_ioda_release_pe_seg(pe);2389pnv_ioda_deconfigure_pe(pe->phb, pe);23902391/* Release slave PEs in the compound PE */2392if (pe->flags & PNV_IODA_PE_MASTER) {2393list_for_each_entry_safe(slave, tmp, &pe->slaves, list) {2394list_del(&slave->list);2395pnv_ioda_free_pe(slave);2396}2397}23982399/*2400* The PE for root bus can be removed because of hotplug in EEH2401* recovery for fenced PHB error. We need to mark the PE dead so2402* that it can be populated again in PCI hot add path. The PE2403* shouldn't be destroyed as it's the global reserved resource.2404*/2405if (phb->ioda.root_pe_idx == pe->pe_number)2406return;24072408pnv_ioda_free_pe(pe);2409}24102411static void pnv_pci_release_device(struct pci_dev *pdev)2412{2413struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);2414struct pci_dn *pdn = pci_get_pdn(pdev);2415struct pnv_ioda_pe *pe;24162417/* The VF PE state is torn down when sriov_disable() is called */2418if (pdev->is_virtfn)2419return;24202421if (!pdn || pdn->pe_number == IODA_INVALID_PE)2422return;24232424#ifdef CONFIG_PCI_IOV2425/*2426* FIXME: Try move this to sriov_disable(). It's here since we allocate2427* the iov state at probe time since we need to fiddle with the IOV2428* resources.2429*/2430if (pdev->is_physfn)2431kfree(pdev->dev.archdata.iov_data);2432#endif24332434/*2435* PCI hotplug can happen as part of EEH error recovery. The @pdn2436* isn't removed and added afterwards in this scenario. We should2437* set the PE number in @pdn to an invalid one. Otherwise, the PE's2438* device count is decreased on removing devices while failing to2439* be increased on adding devices. It leads to unbalanced PE's device2440* count and eventually make normal PCI hotplug path broken.2441*/2442pe = &phb->ioda.pe_array[pdn->pe_number];2443pdn->pe_number = IODA_INVALID_PE;24442445WARN_ON(--pe->device_count < 0);2446if (pe->device_count == 0)2447pnv_ioda_release_pe(pe);2448}24492450static void pnv_pci_ioda_shutdown(struct pci_controller *hose)2451{2452struct pnv_phb *phb = hose->private_data;24532454opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,2455OPAL_ASSERT_RESET);2456}24572458static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus)2459{2460struct pnv_phb *phb = pci_bus_to_pnvhb(bus);2461struct pnv_ioda_pe *pe;24622463list_for_each_entry(pe, &phb->ioda.pe_list, list) {2464if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))2465continue;24662467if (!pe->pbus)2468continue;24692470if (bus->number == ((pe->rid >> 8) & 0xFF)) {2471pe->pbus = bus;2472break;2473}2474}2475}24762477#ifdef CONFIG_IOMMU_API2478static struct iommu_group *pnv_pci_device_group(struct pci_controller *hose,2479struct pci_dev *pdev)2480{2481struct pnv_phb *phb = hose->private_data;2482struct pnv_ioda_pe *pe;24832484if (WARN_ON(!phb))2485return ERR_PTR(-ENODEV);24862487pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));2488if (!pe)2489return ERR_PTR(-ENODEV);24902491if (!pe->table_group.group)2492return ERR_PTR(-ENODEV);24932494return iommu_group_ref_get(pe->table_group.group);2495}2496#endif24972498static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {2499.dma_dev_setup = pnv_pci_ioda_dma_dev_setup,2500.dma_bus_setup = pnv_pci_ioda_dma_bus_setup,2501.iommu_bypass_supported = pnv_pci_ioda_iommu_bypass_supported,2502.enable_device_hook = pnv_pci_enable_device_hook,2503.release_device = pnv_pci_release_device,2504.window_alignment = pnv_pci_window_alignment,2505.setup_bridge = pnv_pci_fixup_bridge_resources,2506.reset_secondary_bus = pnv_pci_reset_secondary_bus,2507.shutdown = pnv_pci_ioda_shutdown,2508#ifdef CONFIG_IOMMU_API2509.device_group = pnv_pci_device_group,2510#endif2511};25122513static const struct pci_controller_ops pnv_npu_ocapi_ioda_controller_ops = {2514.enable_device_hook = pnv_ocapi_enable_device_hook,2515.release_device = pnv_pci_release_device,2516.window_alignment = pnv_pci_window_alignment,2517.reset_secondary_bus = pnv_pci_reset_secondary_bus,2518.shutdown = pnv_pci_ioda_shutdown,2519};25202521static void __init pnv_pci_init_ioda_phb(struct device_node *np,2522u64 hub_id, int ioda_type)2523{2524struct pci_controller *hose;2525struct pnv_phb *phb;2526unsigned long size, m64map_off, m32map_off, pemap_off;2527struct pnv_ioda_pe *root_pe;2528struct resource r;2529const __be64 *prop64;2530const __be32 *prop32;2531int len;2532unsigned int segno;2533u64 phb_id;2534void *aux;2535long rc;25362537if (!of_device_is_available(np))2538return;25392540pr_info("Initializing %s PHB (%pOF)\n", pnv_phb_names[ioda_type], np);25412542prop64 = of_get_property(np, "ibm,opal-phbid", NULL);2543if (!prop64) {2544pr_err(" Missing \"ibm,opal-phbid\" property !\n");2545return;2546}2547phb_id = be64_to_cpup(prop64);2548pr_debug(" PHB-ID : 0x%016llx\n", phb_id);25492550phb = kzalloc(sizeof(*phb), GFP_KERNEL);2551if (!phb)2552panic("%s: Failed to allocate %zu bytes\n", __func__,2553sizeof(*phb));25542555/* Allocate PCI controller */2556phb->hose = hose = pcibios_alloc_controller(np);2557if (!phb->hose) {2558pr_err(" Can't allocate PCI controller for %pOF\n",2559np);2560memblock_free(phb, sizeof(struct pnv_phb));2561return;2562}25632564spin_lock_init(&phb->lock);2565prop32 = of_get_property(np, "bus-range", &len);2566if (prop32 && len == 8) {2567hose->first_busno = be32_to_cpu(prop32[0]);2568hose->last_busno = be32_to_cpu(prop32[1]);2569} else {2570pr_warn(" Broken <bus-range> on %pOF\n", np);2571hose->first_busno = 0;2572hose->last_busno = 0xff;2573}2574hose->private_data = phb;2575phb->hub_id = hub_id;2576phb->opal_id = phb_id;2577phb->type = ioda_type;2578mutex_init(&phb->ioda.pe_alloc_mutex);25792580/* Detect specific models for error handling */2581if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))2582phb->model = PNV_PHB_MODEL_P7IOC;2583else if (of_device_is_compatible(np, "ibm,power8-pciex"))2584phb->model = PNV_PHB_MODEL_PHB3;2585else2586phb->model = PNV_PHB_MODEL_UNKNOWN;25872588/* Initialize diagnostic data buffer */2589prop32 = of_get_property(np, "ibm,phb-diag-data-size", NULL);2590if (prop32)2591phb->diag_data_size = be32_to_cpup(prop32);2592else2593phb->diag_data_size = PNV_PCI_DIAG_BUF_SIZE;25942595phb->diag_data = kzalloc(phb->diag_data_size, GFP_KERNEL);2596if (!phb->diag_data)2597panic("%s: Failed to allocate %u bytes\n", __func__,2598phb->diag_data_size);25992600/* Parse 32-bit and IO ranges (if any) */2601pci_process_bridge_OF_ranges(hose, np, !hose->global_number);26022603/* Get registers */2604if (!of_address_to_resource(np, 0, &r)) {2605phb->regs_phys = r.start;2606phb->regs = ioremap(r.start, resource_size(&r));2607if (phb->regs == NULL)2608pr_err(" Failed to map registers !\n");2609}26102611/* Initialize more IODA stuff */2612phb->ioda.total_pe_num = 1;2613prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);2614if (prop32)2615phb->ioda.total_pe_num = be32_to_cpup(prop32);2616prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);2617if (prop32)2618phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);26192620/* Invalidate RID to PE# mapping */2621for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)2622phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;26232624/* Parse 64-bit MMIO range */2625pnv_ioda_parse_m64_window(phb);26262627phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);2628/* FW Has already off top 64k of M32 space (MSI space) */2629phb->ioda.m32_size += 0x10000;26302631phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;2632phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];2633phb->ioda.io_size = hose->pci_io_size;2634phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;2635phb->ioda.io_pci_base = 0; /* XXX calculate this ? */26362637/* Allocate aux data & arrays. We don't have IO ports on PHB3 */2638size = ALIGN(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,2639sizeof(unsigned long));2640m64map_off = size;2641size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);2642m32map_off = size;2643size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);2644pemap_off = size;2645size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);2646aux = kzalloc(size, GFP_KERNEL);2647if (!aux)2648panic("%s: Failed to allocate %lu bytes\n", __func__, size);26492650phb->ioda.pe_alloc = aux;2651phb->ioda.m64_segmap = aux + m64map_off;2652phb->ioda.m32_segmap = aux + m32map_off;2653for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {2654phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;2655phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;2656}2657phb->ioda.pe_array = aux + pemap_off;26582659/*2660* Choose PE number for root bus, which shouldn't have2661* M64 resources consumed by its child devices. To pick2662* the PE number adjacent to the reserved one if possible.2663*/2664pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);2665if (phb->ioda.reserved_pe_idx == 0) {2666phb->ioda.root_pe_idx = 1;2667pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);2668} else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {2669phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;2670pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);2671} else {2672/* otherwise just allocate one */2673root_pe = pnv_ioda_alloc_pe(phb, 1);2674phb->ioda.root_pe_idx = root_pe->pe_number;2675}26762677INIT_LIST_HEAD(&phb->ioda.pe_list);2678mutex_init(&phb->ioda.pe_list_mutex);26792680#if 0 /* We should really do that ... */2681rc = opal_pci_set_phb_mem_window(opal->phb_id,2682window_type,2683window_num,2684starting_real_address,2685starting_pci_address,2686segment_size);2687#endif26882689pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",2690phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,2691phb->ioda.m32_size, phb->ioda.m32_segsize);2692if (phb->ioda.m64_size)2693pr_info(" M64: 0x%lx [segment=0x%lx]\n",2694phb->ioda.m64_size, phb->ioda.m64_segsize);2695if (phb->ioda.io_size)2696pr_info(" IO: 0x%x [segment=0x%x]\n",2697phb->ioda.io_size, phb->ioda.io_segsize);269826992700phb->hose->ops = &pnv_pci_ops;2701phb->get_pe_state = pnv_ioda_get_pe_state;2702phb->freeze_pe = pnv_ioda_freeze_pe;2703phb->unfreeze_pe = pnv_ioda_unfreeze_pe;27042705/* Setup MSI support */2706pnv_pci_init_ioda_msis(phb);27072708/*2709* We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here2710* to let the PCI core do resource assignment. It's supposed2711* that the PCI core will do correct I/O and MMIO alignment2712* for the P2P bridge bars so that each PCI bus (excluding2713* the child P2P bridges) can form individual PE.2714*/2715ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;27162717switch (phb->type) {2718case PNV_PHB_NPU_OCAPI:2719hose->controller_ops = pnv_npu_ocapi_ioda_controller_ops;2720break;2721default:2722hose->controller_ops = pnv_pci_ioda_controller_ops;2723}27242725ppc_md.pcibios_default_alignment = pnv_pci_default_alignment;27262727#ifdef CONFIG_PCI_IOV2728ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov;2729ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;2730ppc_md.pcibios_sriov_enable = pnv_pcibios_sriov_enable;2731ppc_md.pcibios_sriov_disable = pnv_pcibios_sriov_disable;2732#endif27332734pci_add_flags(PCI_REASSIGN_ALL_RSRC);27352736/* Reset IODA tables to a clean state */2737rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);2738if (rc)2739pr_warn(" OPAL Error %ld performing IODA table reset !\n", rc);27402741/*2742* If we're running in kdump kernel, the previous kernel never2743* shutdown PCI devices correctly. We already got IODA table2744* cleaned out. So we have to issue PHB reset to stop all PCI2745* transactions from previous kernel. The ppc_pci_reset_phbs2746* kernel parameter will force this reset too. Additionally,2747* if the IODA reset above failed then use a bigger hammer.2748* This can happen if we get a PHB fatal error in very early2749* boot.2750*/2751if (is_kdump_kernel() || pci_reset_phbs || rc) {2752pr_info(" Issue PHB reset ...\n");2753pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);2754pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);2755}27562757/* Remove M64 resource if we can't configure it successfully */2758if (!phb->init_m64 || phb->init_m64(phb))2759hose->mem_resources[1].flags = 0;27602761/* create pci_dn's for DT nodes under this PHB */2762pci_devs_phb_init_dynamic(hose);2763}27642765void __init pnv_pci_init_ioda2_phb(struct device_node *np)2766{2767pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);2768}27692770void __init pnv_pci_init_npu2_opencapi_phb(struct device_node *np)2771{2772pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_OCAPI);2773}27742775static void pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev *dev)2776{2777struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);27782779if (!machine_is(powernv))2780return;27812782if (phb->type == PNV_PHB_NPU_OCAPI)2783dev->cfg_size = PCI_CFG_SPACE_EXP_SIZE;2784}2785DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pnv_npu2_opencapi_cfg_size_fixup);278627872788