1184cd4a3SBenjamin Herrenschmidt /*
2184cd4a3SBenjamin Herrenschmidt  * Support PCI/PCIe on PowerNV platforms
3184cd4a3SBenjamin Herrenschmidt  *
4184cd4a3SBenjamin Herrenschmidt  * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
5184cd4a3SBenjamin Herrenschmidt  *
6184cd4a3SBenjamin Herrenschmidt  * This program is free software; you can redistribute it and/or
7184cd4a3SBenjamin Herrenschmidt  * modify it under the terms of the GNU General Public License
8184cd4a3SBenjamin Herrenschmidt  * as published by the Free Software Foundation; either version
9184cd4a3SBenjamin Herrenschmidt  * 2 of the License, or (at your option) any later version.
10184cd4a3SBenjamin Herrenschmidt  */
11184cd4a3SBenjamin Herrenschmidt 
12cee72d5bSBenjamin Herrenschmidt #undef DEBUG
13184cd4a3SBenjamin Herrenschmidt 
14184cd4a3SBenjamin Herrenschmidt #include <linux/kernel.h>
15184cd4a3SBenjamin Herrenschmidt #include <linux/pci.h>
16361f2a2aSGavin Shan #include <linux/crash_dump.h>
1737c367f2SGavin Shan #include <linux/debugfs.h>
18184cd4a3SBenjamin Herrenschmidt #include <linux/delay.h>
19184cd4a3SBenjamin Herrenschmidt #include <linux/string.h>
20184cd4a3SBenjamin Herrenschmidt #include <linux/init.h>
21184cd4a3SBenjamin Herrenschmidt #include <linux/bootmem.h>
22184cd4a3SBenjamin Herrenschmidt #include <linux/irq.h>
23184cd4a3SBenjamin Herrenschmidt #include <linux/io.h>
24184cd4a3SBenjamin Herrenschmidt #include <linux/msi.h>
25cd15b048SBenjamin Herrenschmidt #include <linux/memblock.h>
26184cd4a3SBenjamin Herrenschmidt 
27184cd4a3SBenjamin Herrenschmidt #include <asm/sections.h>
28184cd4a3SBenjamin Herrenschmidt #include <asm/io.h>
29184cd4a3SBenjamin Herrenschmidt #include <asm/prom.h>
30184cd4a3SBenjamin Herrenschmidt #include <asm/pci-bridge.h>
31184cd4a3SBenjamin Herrenschmidt #include <asm/machdep.h>
32fb1b55d6SGavin Shan #include <asm/msi_bitmap.h>
33184cd4a3SBenjamin Herrenschmidt #include <asm/ppc-pci.h>
34184cd4a3SBenjamin Herrenschmidt #include <asm/opal.h>
35184cd4a3SBenjamin Herrenschmidt #include <asm/iommu.h>
36184cd4a3SBenjamin Herrenschmidt #include <asm/tce.h>
37137436c9SGavin Shan #include <asm/xics.h>
3837c367f2SGavin Shan #include <asm/debug.h>
39262af557SGuo Chao #include <asm/firmware.h>
4080c49c7eSIan Munsie #include <asm/pnv-pci.h>
4180c49c7eSIan Munsie 
4280c49c7eSIan Munsie #include <misc/cxl.h>
43184cd4a3SBenjamin Herrenschmidt 
44184cd4a3SBenjamin Herrenschmidt #include "powernv.h"
45184cd4a3SBenjamin Herrenschmidt #include "pci.h"
46184cd4a3SBenjamin Herrenschmidt 
476d31c2faSJoe Perches static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
486d31c2faSJoe Perches 			    const char *fmt, ...)
496d31c2faSJoe Perches {
506d31c2faSJoe Perches 	struct va_format vaf;
516d31c2faSJoe Perches 	va_list args;
526d31c2faSJoe Perches 	char pfix[32];
53184cd4a3SBenjamin Herrenschmidt 
546d31c2faSJoe Perches 	va_start(args, fmt);
556d31c2faSJoe Perches 
566d31c2faSJoe Perches 	vaf.fmt = fmt;
576d31c2faSJoe Perches 	vaf.va = &args;
586d31c2faSJoe Perches 
596d31c2faSJoe Perches 	if (pe->pdev)
606d31c2faSJoe Perches 		strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
616d31c2faSJoe Perches 	else
626d31c2faSJoe Perches 		sprintf(pfix, "%04x:%02x     ",
636d31c2faSJoe Perches 			pci_domain_nr(pe->pbus), pe->pbus->number);
646d31c2faSJoe Perches 
656d31c2faSJoe Perches 	printk("%spci %s: [PE# %.3d] %pV",
666d31c2faSJoe Perches 	       level, pfix, pe->pe_number, &vaf);
676d31c2faSJoe Perches 
686d31c2faSJoe Perches 	va_end(args);
696d31c2faSJoe Perches }
706d31c2faSJoe Perches 
716d31c2faSJoe Perches #define pe_err(pe, fmt, ...)					\
726d31c2faSJoe Perches 	pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
736d31c2faSJoe Perches #define pe_warn(pe, fmt, ...)					\
746d31c2faSJoe Perches 	pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
756d31c2faSJoe Perches #define pe_info(pe, fmt, ...)					\
766d31c2faSJoe Perches 	pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
77184cd4a3SBenjamin Herrenschmidt 
784e287840SThadeu Lima de Souza Cascardo static bool pnv_iommu_bypass_disabled __read_mostly;
794e287840SThadeu Lima de Souza Cascardo 
804e287840SThadeu Lima de Souza Cascardo static int __init iommu_setup(char *str)
814e287840SThadeu Lima de Souza Cascardo {
824e287840SThadeu Lima de Souza Cascardo 	if (!str)
834e287840SThadeu Lima de Souza Cascardo 		return -EINVAL;
844e287840SThadeu Lima de Souza Cascardo 
854e287840SThadeu Lima de Souza Cascardo 	while (*str) {
864e287840SThadeu Lima de Souza Cascardo 		if (!strncmp(str, "nobypass", 8)) {
874e287840SThadeu Lima de Souza Cascardo 			pnv_iommu_bypass_disabled = true;
884e287840SThadeu Lima de Souza Cascardo 			pr_info("PowerNV: IOMMU bypass window disabled.\n");
894e287840SThadeu Lima de Souza Cascardo 			break;
904e287840SThadeu Lima de Souza Cascardo 		}
914e287840SThadeu Lima de Souza Cascardo 		str += strcspn(str, ",");
924e287840SThadeu Lima de Souza Cascardo 		if (*str == ',')
934e287840SThadeu Lima de Souza Cascardo 			str++;
944e287840SThadeu Lima de Souza Cascardo 	}
954e287840SThadeu Lima de Souza Cascardo 
964e287840SThadeu Lima de Souza Cascardo 	return 0;
974e287840SThadeu Lima de Souza Cascardo }
984e287840SThadeu Lima de Souza Cascardo early_param("iommu", iommu_setup);
994e287840SThadeu Lima de Souza Cascardo 
1008e0a1611SAlexey Kardashevskiy /*
1018e0a1611SAlexey Kardashevskiy  * stdcix is only supposed to be used in hypervisor real mode as per
1028e0a1611SAlexey Kardashevskiy  * the architecture spec
1038e0a1611SAlexey Kardashevskiy  */
1048e0a1611SAlexey Kardashevskiy static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
1058e0a1611SAlexey Kardashevskiy {
1068e0a1611SAlexey Kardashevskiy 	__asm__ __volatile__("stdcix %0,0,%1"
1078e0a1611SAlexey Kardashevskiy 		: : "r" (val), "r" (paddr) : "memory");
1088e0a1611SAlexey Kardashevskiy }
1098e0a1611SAlexey Kardashevskiy 
110262af557SGuo Chao static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
111262af557SGuo Chao {
112262af557SGuo Chao 	return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
113262af557SGuo Chao 		(IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
114262af557SGuo Chao }
115262af557SGuo Chao 
1164b82ab18SGavin Shan static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
1174b82ab18SGavin Shan {
1184b82ab18SGavin Shan 	if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
1194b82ab18SGavin Shan 		pr_warn("%s: Invalid PE %d on PHB#%x\n",
1204b82ab18SGavin Shan 			__func__, pe_no, phb->hose->global_number);
1214b82ab18SGavin Shan 		return;
1224b82ab18SGavin Shan 	}
1234b82ab18SGavin Shan 
1244b82ab18SGavin Shan 	if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) {
1254b82ab18SGavin Shan 		pr_warn("%s: PE %d was assigned on PHB#%x\n",
1264b82ab18SGavin Shan 			__func__, pe_no, phb->hose->global_number);
1274b82ab18SGavin Shan 		return;
1284b82ab18SGavin Shan 	}
1294b82ab18SGavin Shan 
1304b82ab18SGavin Shan 	phb->ioda.pe_array[pe_no].phb = phb;
1314b82ab18SGavin Shan 	phb->ioda.pe_array[pe_no].pe_number = pe_no;
1324b82ab18SGavin Shan }
1334b82ab18SGavin Shan 
134cad5cef6SGreg Kroah-Hartman static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
135184cd4a3SBenjamin Herrenschmidt {
136184cd4a3SBenjamin Herrenschmidt 	unsigned long pe;
137184cd4a3SBenjamin Herrenschmidt 
138184cd4a3SBenjamin Herrenschmidt 	do {
139184cd4a3SBenjamin Herrenschmidt 		pe = find_next_zero_bit(phb->ioda.pe_alloc,
140184cd4a3SBenjamin Herrenschmidt 					phb->ioda.total_pe, 0);
141184cd4a3SBenjamin Herrenschmidt 		if (pe >= phb->ioda.total_pe)
142184cd4a3SBenjamin Herrenschmidt 			return IODA_INVALID_PE;
143184cd4a3SBenjamin Herrenschmidt 	} while(test_and_set_bit(pe, phb->ioda.pe_alloc));
144184cd4a3SBenjamin Herrenschmidt 
1454cce9550SGavin Shan 	phb->ioda.pe_array[pe].phb = phb;
146184cd4a3SBenjamin Herrenschmidt 	phb->ioda.pe_array[pe].pe_number = pe;
147184cd4a3SBenjamin Herrenschmidt 	return pe;
148184cd4a3SBenjamin Herrenschmidt }
149184cd4a3SBenjamin Herrenschmidt 
150cad5cef6SGreg Kroah-Hartman static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
151184cd4a3SBenjamin Herrenschmidt {
152184cd4a3SBenjamin Herrenschmidt 	WARN_ON(phb->ioda.pe_array[pe].pdev);
153184cd4a3SBenjamin Herrenschmidt 
154184cd4a3SBenjamin Herrenschmidt 	memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
155184cd4a3SBenjamin Herrenschmidt 	clear_bit(pe, phb->ioda.pe_alloc);
156184cd4a3SBenjamin Herrenschmidt }
157184cd4a3SBenjamin Herrenschmidt 
158262af557SGuo Chao /* The default M64 BAR is shared by all PEs */
159262af557SGuo Chao static int pnv_ioda2_init_m64(struct pnv_phb *phb)
160262af557SGuo Chao {
161262af557SGuo Chao 	const char *desc;
162262af557SGuo Chao 	struct resource *r;
163262af557SGuo Chao 	s64 rc;
164262af557SGuo Chao 
165262af557SGuo Chao 	/* Configure the default M64 BAR */
166262af557SGuo Chao 	rc = opal_pci_set_phb_mem_window(phb->opal_id,
167262af557SGuo Chao 					 OPAL_M64_WINDOW_TYPE,
168262af557SGuo Chao 					 phb->ioda.m64_bar_idx,
169262af557SGuo Chao 					 phb->ioda.m64_base,
170262af557SGuo Chao 					 0, /* unused */
171262af557SGuo Chao 					 phb->ioda.m64_size);
172262af557SGuo Chao 	if (rc != OPAL_SUCCESS) {
173262af557SGuo Chao 		desc = "configuring";
174262af557SGuo Chao 		goto fail;
175262af557SGuo Chao 	}
176262af557SGuo Chao 
177262af557SGuo Chao 	/* Enable the default M64 BAR */
178262af557SGuo Chao 	rc = opal_pci_phb_mmio_enable(phb->opal_id,
179262af557SGuo Chao 				      OPAL_M64_WINDOW_TYPE,
180262af557SGuo Chao 				      phb->ioda.m64_bar_idx,
181262af557SGuo Chao 				      OPAL_ENABLE_M64_SPLIT);
182262af557SGuo Chao 	if (rc != OPAL_SUCCESS) {
183262af557SGuo Chao 		desc = "enabling";
184262af557SGuo Chao 		goto fail;
185262af557SGuo Chao 	}
186262af557SGuo Chao 
187262af557SGuo Chao 	/* Mark the M64 BAR assigned */
188262af557SGuo Chao 	set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
189262af557SGuo Chao 
190262af557SGuo Chao 	/*
191262af557SGuo Chao 	 * Strip off the segment used by the reserved PE, which is
192262af557SGuo Chao 	 * expected to be 0 or last one of PE capabicity.
193262af557SGuo Chao 	 */
194262af557SGuo Chao 	r = &phb->hose->mem_resources[1];
195262af557SGuo Chao 	if (phb->ioda.reserved_pe == 0)
196262af557SGuo Chao 		r->start += phb->ioda.m64_segsize;
197262af557SGuo Chao 	else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
198262af557SGuo Chao 		r->end -= phb->ioda.m64_segsize;
199262af557SGuo Chao 	else
200262af557SGuo Chao 		pr_warn("  Cannot strip M64 segment for reserved PE#%d\n",
201262af557SGuo Chao 			phb->ioda.reserved_pe);
202262af557SGuo Chao 
203262af557SGuo Chao 	return 0;
204262af557SGuo Chao 
205262af557SGuo Chao fail:
206262af557SGuo Chao 	pr_warn("  Failure %lld %s M64 BAR#%d\n",
207262af557SGuo Chao 		rc, desc, phb->ioda.m64_bar_idx);
208262af557SGuo Chao 	opal_pci_phb_mmio_enable(phb->opal_id,
209262af557SGuo Chao 				 OPAL_M64_WINDOW_TYPE,
210262af557SGuo Chao 				 phb->ioda.m64_bar_idx,
211262af557SGuo Chao 				 OPAL_DISABLE_M64);
212262af557SGuo Chao 	return -EIO;
213262af557SGuo Chao }
214262af557SGuo Chao 
2155ef73567SGavin Shan static void pnv_ioda2_reserve_m64_pe(struct pnv_phb *phb)
216262af557SGuo Chao {
217262af557SGuo Chao 	resource_size_t sgsz = phb->ioda.m64_segsize;
218262af557SGuo Chao 	struct pci_dev *pdev;
219262af557SGuo Chao 	struct resource *r;
220262af557SGuo Chao 	int base, step, i;
221262af557SGuo Chao 
222262af557SGuo Chao 	/*
223262af557SGuo Chao 	 * Root bus always has full M64 range and root port has
224262af557SGuo Chao 	 * M64 range used in reality. So we're checking root port
225262af557SGuo Chao 	 * instead of root bus.
226262af557SGuo Chao 	 */
227262af557SGuo Chao 	list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
2284b82ab18SGavin Shan 		for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
2294b82ab18SGavin Shan 			r = &pdev->resource[PCI_BRIDGE_RESOURCES + i];
230262af557SGuo Chao 			if (!r->parent ||
231262af557SGuo Chao 			    !pnv_pci_is_mem_pref_64(r->flags))
232262af557SGuo Chao 				continue;
233262af557SGuo Chao 
234262af557SGuo Chao 			base = (r->start - phb->ioda.m64_base) / sgsz;
235262af557SGuo Chao 			for (step = 0; step < resource_size(r) / sgsz; step++)
2364b82ab18SGavin Shan 				pnv_ioda_reserve_pe(phb, base + step);
237262af557SGuo Chao 		}
238262af557SGuo Chao 	}
239262af557SGuo Chao }
240262af557SGuo Chao 
241262af557SGuo Chao static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
242262af557SGuo Chao 				 struct pci_bus *bus, int all)
243262af557SGuo Chao {
244262af557SGuo Chao 	resource_size_t segsz = phb->ioda.m64_segsize;
245262af557SGuo Chao 	struct pci_dev *pdev;
246262af557SGuo Chao 	struct resource *r;
247262af557SGuo Chao 	struct pnv_ioda_pe *master_pe, *pe;
248262af557SGuo Chao 	unsigned long size, *pe_alloc;
249262af557SGuo Chao 	bool found;
250262af557SGuo Chao 	int start, i, j;
251262af557SGuo Chao 
252262af557SGuo Chao 	/* Root bus shouldn't use M64 */
253262af557SGuo Chao 	if (pci_is_root_bus(bus))
254262af557SGuo Chao 		return IODA_INVALID_PE;
255262af557SGuo Chao 
256262af557SGuo Chao 	/* We support only one M64 window on each bus */
257262af557SGuo Chao 	found = false;
258262af557SGuo Chao 	pci_bus_for_each_resource(bus, r, i) {
259262af557SGuo Chao 		if (r && r->parent &&
260262af557SGuo Chao 		    pnv_pci_is_mem_pref_64(r->flags)) {
261262af557SGuo Chao 			found = true;
262262af557SGuo Chao 			break;
263262af557SGuo Chao 		}
264262af557SGuo Chao 	}
265262af557SGuo Chao 
266262af557SGuo Chao 	/* No M64 window found ? */
267262af557SGuo Chao 	if (!found)
268262af557SGuo Chao 		return IODA_INVALID_PE;
269262af557SGuo Chao 
270262af557SGuo Chao 	/* Allocate bitmap */
271262af557SGuo Chao 	size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
272262af557SGuo Chao 	pe_alloc = kzalloc(size, GFP_KERNEL);
273262af557SGuo Chao 	if (!pe_alloc) {
274262af557SGuo Chao 		pr_warn("%s: Out of memory !\n",
275262af557SGuo Chao 			__func__);
276262af557SGuo Chao 		return IODA_INVALID_PE;
277262af557SGuo Chao 	}
278262af557SGuo Chao 
279262af557SGuo Chao 	/*
280262af557SGuo Chao 	 * Figure out reserved PE numbers by the PE
281262af557SGuo Chao 	 * the its child PEs.
282262af557SGuo Chao 	 */
283262af557SGuo Chao 	start = (r->start - phb->ioda.m64_base) / segsz;
284262af557SGuo Chao 	for (i = 0; i < resource_size(r) / segsz; i++)
285262af557SGuo Chao 		set_bit(start + i, pe_alloc);
286262af557SGuo Chao 
287262af557SGuo Chao 	if (all)
288262af557SGuo Chao 		goto done;
289262af557SGuo Chao 
290262af557SGuo Chao 	/*
291262af557SGuo Chao 	 * If the PE doesn't cover all subordinate buses,
292262af557SGuo Chao 	 * we need subtract from reserved PEs for children.
293262af557SGuo Chao 	 */
294262af557SGuo Chao 	list_for_each_entry(pdev, &bus->devices, bus_list) {
295262af557SGuo Chao 		if (!pdev->subordinate)
296262af557SGuo Chao 			continue;
297262af557SGuo Chao 
298262af557SGuo Chao 		pci_bus_for_each_resource(pdev->subordinate, r, i) {
299262af557SGuo Chao 			if (!r || !r->parent ||
300262af557SGuo Chao 			    !pnv_pci_is_mem_pref_64(r->flags))
301262af557SGuo Chao 				continue;
302262af557SGuo Chao 
303262af557SGuo Chao 			start = (r->start - phb->ioda.m64_base) / segsz;
304262af557SGuo Chao 			for (j = 0; j < resource_size(r) / segsz ; j++)
305262af557SGuo Chao 				clear_bit(start + j, pe_alloc);
306262af557SGuo Chao                 }
307262af557SGuo Chao         }
308262af557SGuo Chao 
309262af557SGuo Chao 	/*
310262af557SGuo Chao 	 * the current bus might not own M64 window and that's all
311262af557SGuo Chao 	 * contributed by its child buses. For the case, we needn't
312262af557SGuo Chao 	 * pick M64 dependent PE#.
313262af557SGuo Chao 	 */
314262af557SGuo Chao 	if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
315262af557SGuo Chao 		kfree(pe_alloc);
316262af557SGuo Chao 		return IODA_INVALID_PE;
317262af557SGuo Chao 	}
318262af557SGuo Chao 
319262af557SGuo Chao 	/*
320262af557SGuo Chao 	 * Figure out the master PE and put all slave PEs to master
321262af557SGuo Chao 	 * PE's list to form compound PE.
322262af557SGuo Chao 	 */
323262af557SGuo Chao done:
324262af557SGuo Chao 	master_pe = NULL;
325262af557SGuo Chao 	i = -1;
326262af557SGuo Chao 	while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
327262af557SGuo Chao 		phb->ioda.total_pe) {
328262af557SGuo Chao 		pe = &phb->ioda.pe_array[i];
329262af557SGuo Chao 
330262af557SGuo Chao 		if (!master_pe) {
331262af557SGuo Chao 			pe->flags |= PNV_IODA_PE_MASTER;
332262af557SGuo Chao 			INIT_LIST_HEAD(&pe->slaves);
333262af557SGuo Chao 			master_pe = pe;
334262af557SGuo Chao 		} else {
335262af557SGuo Chao 			pe->flags |= PNV_IODA_PE_SLAVE;
336262af557SGuo Chao 			pe->master = master_pe;
337262af557SGuo Chao 			list_add_tail(&pe->list, &master_pe->slaves);
338262af557SGuo Chao 		}
339262af557SGuo Chao 	}
340262af557SGuo Chao 
341262af557SGuo Chao 	kfree(pe_alloc);
342262af557SGuo Chao 	return master_pe->pe_number;
343262af557SGuo Chao }
344262af557SGuo Chao 
345262af557SGuo Chao static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
346262af557SGuo Chao {
347262af557SGuo Chao 	struct pci_controller *hose = phb->hose;
348262af557SGuo Chao 	struct device_node *dn = hose->dn;
349262af557SGuo Chao 	struct resource *res;
350262af557SGuo Chao 	const u32 *r;
351262af557SGuo Chao 	u64 pci_addr;
352262af557SGuo Chao 
3531665c4a8SGavin Shan 	/* FIXME: Support M64 for P7IOC */
3541665c4a8SGavin Shan 	if (phb->type != PNV_PHB_IODA2) {
3551665c4a8SGavin Shan 		pr_info("  Not support M64 window\n");
3561665c4a8SGavin Shan 		return;
3571665c4a8SGavin Shan 	}
3581665c4a8SGavin Shan 
359262af557SGuo Chao 	if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
360262af557SGuo Chao 		pr_info("  Firmware too old to support M64 window\n");
361262af557SGuo Chao 		return;
362262af557SGuo Chao 	}
363262af557SGuo Chao 
364262af557SGuo Chao 	r = of_get_property(dn, "ibm,opal-m64-window", NULL);
365262af557SGuo Chao 	if (!r) {
366262af557SGuo Chao 		pr_info("  No <ibm,opal-m64-window> on %s\n",
367262af557SGuo Chao 			dn->full_name);
368262af557SGuo Chao 		return;
369262af557SGuo Chao 	}
370262af557SGuo Chao 
371262af557SGuo Chao 	res = &hose->mem_resources[1];
372262af557SGuo Chao 	res->start = of_translate_address(dn, r + 2);
373262af557SGuo Chao 	res->end = res->start + of_read_number(r + 4, 2) - 1;
374262af557SGuo Chao 	res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
375262af557SGuo Chao 	pci_addr = of_read_number(r, 2);
376262af557SGuo Chao 	hose->mem_offset[1] = res->start - pci_addr;
377262af557SGuo Chao 
378262af557SGuo Chao 	phb->ioda.m64_size = resource_size(res);
379262af557SGuo Chao 	phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
380262af557SGuo Chao 	phb->ioda.m64_base = pci_addr;
381262af557SGuo Chao 
382e9863e68SWei Yang 	pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
383e9863e68SWei Yang 			res->start, res->end, pci_addr);
384e9863e68SWei Yang 
385262af557SGuo Chao 	/* Use last M64 BAR to cover M64 window */
386262af557SGuo Chao 	phb->ioda.m64_bar_idx = 15;
387262af557SGuo Chao 	phb->init_m64 = pnv_ioda2_init_m64;
3885ef73567SGavin Shan 	phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
389262af557SGuo Chao 	phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
390262af557SGuo Chao }
391262af557SGuo Chao 
39249dec922SGavin Shan static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
39349dec922SGavin Shan {
39449dec922SGavin Shan 	struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
39549dec922SGavin Shan 	struct pnv_ioda_pe *slave;
39649dec922SGavin Shan 	s64 rc;
39749dec922SGavin Shan 
39849dec922SGavin Shan 	/* Fetch master PE */
39949dec922SGavin Shan 	if (pe->flags & PNV_IODA_PE_SLAVE) {
40049dec922SGavin Shan 		pe = pe->master;
401ec8e4e9dSGavin Shan 		if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
402ec8e4e9dSGavin Shan 			return;
403ec8e4e9dSGavin Shan 
40449dec922SGavin Shan 		pe_no = pe->pe_number;
40549dec922SGavin Shan 	}
40649dec922SGavin Shan 
40749dec922SGavin Shan 	/* Freeze master PE */
40849dec922SGavin Shan 	rc = opal_pci_eeh_freeze_set(phb->opal_id,
40949dec922SGavin Shan 				     pe_no,
41049dec922SGavin Shan 				     OPAL_EEH_ACTION_SET_FREEZE_ALL);
41149dec922SGavin Shan 	if (rc != OPAL_SUCCESS) {
41249dec922SGavin Shan 		pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
41349dec922SGavin Shan 			__func__, rc, phb->hose->global_number, pe_no);
41449dec922SGavin Shan 		return;
41549dec922SGavin Shan 	}
41649dec922SGavin Shan 
41749dec922SGavin Shan 	/* Freeze slave PEs */
41849dec922SGavin Shan 	if (!(pe->flags & PNV_IODA_PE_MASTER))
41949dec922SGavin Shan 		return;
42049dec922SGavin Shan 
42149dec922SGavin Shan 	list_for_each_entry(slave, &pe->slaves, list) {
42249dec922SGavin Shan 		rc = opal_pci_eeh_freeze_set(phb->opal_id,
42349dec922SGavin Shan 					     slave->pe_number,
42449dec922SGavin Shan 					     OPAL_EEH_ACTION_SET_FREEZE_ALL);
42549dec922SGavin Shan 		if (rc != OPAL_SUCCESS)
42649dec922SGavin Shan 			pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
42749dec922SGavin Shan 				__func__, rc, phb->hose->global_number,
42849dec922SGavin Shan 				slave->pe_number);
42949dec922SGavin Shan 	}
43049dec922SGavin Shan }
43149dec922SGavin Shan 
432e51df2c1SAnton Blanchard static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
43349dec922SGavin Shan {
43449dec922SGavin Shan 	struct pnv_ioda_pe *pe, *slave;
43549dec922SGavin Shan 	s64 rc;
43649dec922SGavin Shan 
43749dec922SGavin Shan 	/* Find master PE */
43849dec922SGavin Shan 	pe = &phb->ioda.pe_array[pe_no];
43949dec922SGavin Shan 	if (pe->flags & PNV_IODA_PE_SLAVE) {
44049dec922SGavin Shan 		pe = pe->master;
44149dec922SGavin Shan 		WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
44249dec922SGavin Shan 		pe_no = pe->pe_number;
44349dec922SGavin Shan 	}
44449dec922SGavin Shan 
44549dec922SGavin Shan 	/* Clear frozen state for master PE */
44649dec922SGavin Shan 	rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
44749dec922SGavin Shan 	if (rc != OPAL_SUCCESS) {
44849dec922SGavin Shan 		pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
44949dec922SGavin Shan 			__func__, rc, opt, phb->hose->global_number, pe_no);
45049dec922SGavin Shan 		return -EIO;
45149dec922SGavin Shan 	}
45249dec922SGavin Shan 
45349dec922SGavin Shan 	if (!(pe->flags & PNV_IODA_PE_MASTER))
45449dec922SGavin Shan 		return 0;
45549dec922SGavin Shan 
45649dec922SGavin Shan 	/* Clear frozen state for slave PEs */
45749dec922SGavin Shan 	list_for_each_entry(slave, &pe->slaves, list) {
45849dec922SGavin Shan 		rc = opal_pci_eeh_freeze_clear(phb->opal_id,
45949dec922SGavin Shan 					     slave->pe_number,
46049dec922SGavin Shan 					     opt);
46149dec922SGavin Shan 		if (rc != OPAL_SUCCESS) {
46249dec922SGavin Shan 			pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
46349dec922SGavin Shan 				__func__, rc, opt, phb->hose->global_number,
46449dec922SGavin Shan 				slave->pe_number);
46549dec922SGavin Shan 			return -EIO;
46649dec922SGavin Shan 		}
46749dec922SGavin Shan 	}
46849dec922SGavin Shan 
46949dec922SGavin Shan 	return 0;
47049dec922SGavin Shan }
47149dec922SGavin Shan 
47249dec922SGavin Shan static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
47349dec922SGavin Shan {
47449dec922SGavin Shan 	struct pnv_ioda_pe *slave, *pe;
47549dec922SGavin Shan 	u8 fstate, state;
47649dec922SGavin Shan 	__be16 pcierr;
47749dec922SGavin Shan 	s64 rc;
47849dec922SGavin Shan 
47949dec922SGavin Shan 	/* Sanity check on PE number */
48049dec922SGavin Shan 	if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
48149dec922SGavin Shan 		return OPAL_EEH_STOPPED_PERM_UNAVAIL;
48249dec922SGavin Shan 
48349dec922SGavin Shan 	/*
48449dec922SGavin Shan 	 * Fetch the master PE and the PE instance might be
48549dec922SGavin Shan 	 * not initialized yet.
48649dec922SGavin Shan 	 */
48749dec922SGavin Shan 	pe = &phb->ioda.pe_array[pe_no];
48849dec922SGavin Shan 	if (pe->flags & PNV_IODA_PE_SLAVE) {
48949dec922SGavin Shan 		pe = pe->master;
49049dec922SGavin Shan 		WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
49149dec922SGavin Shan 		pe_no = pe->pe_number;
49249dec922SGavin Shan 	}
49349dec922SGavin Shan 
49449dec922SGavin Shan 	/* Check the master PE */
49549dec922SGavin Shan 	rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
49649dec922SGavin Shan 					&state, &pcierr, NULL);
49749dec922SGavin Shan 	if (rc != OPAL_SUCCESS) {
49849dec922SGavin Shan 		pr_warn("%s: Failure %lld getting "
49949dec922SGavin Shan 			"PHB#%x-PE#%x state\n",
50049dec922SGavin Shan 			__func__, rc,
50149dec922SGavin Shan 			phb->hose->global_number, pe_no);
50249dec922SGavin Shan 		return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
50349dec922SGavin Shan 	}
50449dec922SGavin Shan 
50549dec922SGavin Shan 	/* Check the slave PE */
50649dec922SGavin Shan 	if (!(pe->flags & PNV_IODA_PE_MASTER))
50749dec922SGavin Shan 		return state;
50849dec922SGavin Shan 
50949dec922SGavin Shan 	list_for_each_entry(slave, &pe->slaves, list) {
51049dec922SGavin Shan 		rc = opal_pci_eeh_freeze_status(phb->opal_id,
51149dec922SGavin Shan 						slave->pe_number,
51249dec922SGavin Shan 						&fstate,
51349dec922SGavin Shan 						&pcierr,
51449dec922SGavin Shan 						NULL);
51549dec922SGavin Shan 		if (rc != OPAL_SUCCESS) {
51649dec922SGavin Shan 			pr_warn("%s: Failure %lld getting "
51749dec922SGavin Shan 				"PHB#%x-PE#%x state\n",
51849dec922SGavin Shan 				__func__, rc,
51949dec922SGavin Shan 				phb->hose->global_number, slave->pe_number);
52049dec922SGavin Shan 			return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
52149dec922SGavin Shan 		}
52249dec922SGavin Shan 
52349dec922SGavin Shan 		/*
52449dec922SGavin Shan 		 * Override the result based on the ascending
52549dec922SGavin Shan 		 * priority.
52649dec922SGavin Shan 		 */
52749dec922SGavin Shan 		if (fstate > state)
52849dec922SGavin Shan 			state = fstate;
52949dec922SGavin Shan 	}
53049dec922SGavin Shan 
53149dec922SGavin Shan 	return state;
53249dec922SGavin Shan }
53349dec922SGavin Shan 
534184cd4a3SBenjamin Herrenschmidt /* Currently those 2 are only used when MSIs are enabled, this will change
535184cd4a3SBenjamin Herrenschmidt  * but in the meantime, we need to protect them to avoid warnings
536184cd4a3SBenjamin Herrenschmidt  */
537184cd4a3SBenjamin Herrenschmidt #ifdef CONFIG_PCI_MSI
538cad5cef6SGreg Kroah-Hartman static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
539184cd4a3SBenjamin Herrenschmidt {
540184cd4a3SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
541184cd4a3SBenjamin Herrenschmidt 	struct pnv_phb *phb = hose->private_data;
542b72c1f65SBenjamin Herrenschmidt 	struct pci_dn *pdn = pci_get_pdn(dev);
543184cd4a3SBenjamin Herrenschmidt 
544184cd4a3SBenjamin Herrenschmidt 	if (!pdn)
545184cd4a3SBenjamin Herrenschmidt 		return NULL;
546184cd4a3SBenjamin Herrenschmidt 	if (pdn->pe_number == IODA_INVALID_PE)
547184cd4a3SBenjamin Herrenschmidt 		return NULL;
548184cd4a3SBenjamin Herrenschmidt 	return &phb->ioda.pe_array[pdn->pe_number];
549184cd4a3SBenjamin Herrenschmidt }
550184cd4a3SBenjamin Herrenschmidt #endif /* CONFIG_PCI_MSI */
551184cd4a3SBenjamin Herrenschmidt 
552b131a842SGavin Shan static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
553b131a842SGavin Shan 				  struct pnv_ioda_pe *parent,
554b131a842SGavin Shan 				  struct pnv_ioda_pe *child,
555b131a842SGavin Shan 				  bool is_add)
556b131a842SGavin Shan {
557b131a842SGavin Shan 	const char *desc = is_add ? "adding" : "removing";
558b131a842SGavin Shan 	uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
559b131a842SGavin Shan 			      OPAL_REMOVE_PE_FROM_DOMAIN;
560b131a842SGavin Shan 	struct pnv_ioda_pe *slave;
561b131a842SGavin Shan 	long rc;
562b131a842SGavin Shan 
563b131a842SGavin Shan 	/* Parent PE affects child PE */
564b131a842SGavin Shan 	rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
565b131a842SGavin Shan 				child->pe_number, op);
566b131a842SGavin Shan 	if (rc != OPAL_SUCCESS) {
567b131a842SGavin Shan 		pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
568b131a842SGavin Shan 			rc, desc);
569b131a842SGavin Shan 		return -ENXIO;
570b131a842SGavin Shan 	}
571b131a842SGavin Shan 
572b131a842SGavin Shan 	if (!(child->flags & PNV_IODA_PE_MASTER))
573b131a842SGavin Shan 		return 0;
574b131a842SGavin Shan 
575b131a842SGavin Shan 	/* Compound case: parent PE affects slave PEs */
576b131a842SGavin Shan 	list_for_each_entry(slave, &child->slaves, list) {
577b131a842SGavin Shan 		rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
578b131a842SGavin Shan 					slave->pe_number, op);
579b131a842SGavin Shan 		if (rc != OPAL_SUCCESS) {
580b131a842SGavin Shan 			pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
581b131a842SGavin Shan 				rc, desc);
582b131a842SGavin Shan 			return -ENXIO;
583b131a842SGavin Shan 		}
584b131a842SGavin Shan 	}
585b131a842SGavin Shan 
586b131a842SGavin Shan 	return 0;
587b131a842SGavin Shan }
588b131a842SGavin Shan 
589b131a842SGavin Shan static int pnv_ioda_set_peltv(struct pnv_phb *phb,
590b131a842SGavin Shan 			      struct pnv_ioda_pe *pe,
591b131a842SGavin Shan 			      bool is_add)
592b131a842SGavin Shan {
593b131a842SGavin Shan 	struct pnv_ioda_pe *slave;
594b131a842SGavin Shan 	struct pci_dev *pdev;
595b131a842SGavin Shan 	int ret;
596b131a842SGavin Shan 
597b131a842SGavin Shan 	/*
598b131a842SGavin Shan 	 * Clear PE frozen state. If it's master PE, we need
599b131a842SGavin Shan 	 * clear slave PE frozen state as well.
600b131a842SGavin Shan 	 */
601b131a842SGavin Shan 	if (is_add) {
602b131a842SGavin Shan 		opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
603b131a842SGavin Shan 					  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
604b131a842SGavin Shan 		if (pe->flags & PNV_IODA_PE_MASTER) {
605b131a842SGavin Shan 			list_for_each_entry(slave, &pe->slaves, list)
606b131a842SGavin Shan 				opal_pci_eeh_freeze_clear(phb->opal_id,
607b131a842SGavin Shan 							  slave->pe_number,
608b131a842SGavin Shan 							  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
609b131a842SGavin Shan 		}
610b131a842SGavin Shan 	}
611b131a842SGavin Shan 
612b131a842SGavin Shan 	/*
613b131a842SGavin Shan 	 * Associate PE in PELT. We need add the PE into the
614b131a842SGavin Shan 	 * corresponding PELT-V as well. Otherwise, the error
615b131a842SGavin Shan 	 * originated from the PE might contribute to other
616b131a842SGavin Shan 	 * PEs.
617b131a842SGavin Shan 	 */
618b131a842SGavin Shan 	ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
619b131a842SGavin Shan 	if (ret)
620b131a842SGavin Shan 		return ret;
621b131a842SGavin Shan 
622b131a842SGavin Shan 	/* For compound PEs, any one affects all of them */
623b131a842SGavin Shan 	if (pe->flags & PNV_IODA_PE_MASTER) {
624b131a842SGavin Shan 		list_for_each_entry(slave, &pe->slaves, list) {
625b131a842SGavin Shan 			ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
626b131a842SGavin Shan 			if (ret)
627b131a842SGavin Shan 				return ret;
628b131a842SGavin Shan 		}
629b131a842SGavin Shan 	}
630b131a842SGavin Shan 
631b131a842SGavin Shan 	if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
632b131a842SGavin Shan 		pdev = pe->pbus->self;
633b131a842SGavin Shan 	else
634b131a842SGavin Shan 		pdev = pe->pdev->bus->self;
635b131a842SGavin Shan 	while (pdev) {
636b131a842SGavin Shan 		struct pci_dn *pdn = pci_get_pdn(pdev);
637b131a842SGavin Shan 		struct pnv_ioda_pe *parent;
638b131a842SGavin Shan 
639b131a842SGavin Shan 		if (pdn && pdn->pe_number != IODA_INVALID_PE) {
640b131a842SGavin Shan 			parent = &phb->ioda.pe_array[pdn->pe_number];
641b131a842SGavin Shan 			ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
642b131a842SGavin Shan 			if (ret)
643b131a842SGavin Shan 				return ret;
644b131a842SGavin Shan 		}
645b131a842SGavin Shan 
646b131a842SGavin Shan 		pdev = pdev->bus->self;
647b131a842SGavin Shan 	}
648b131a842SGavin Shan 
649b131a842SGavin Shan 	return 0;
650b131a842SGavin Shan }
651b131a842SGavin Shan 
652cad5cef6SGreg Kroah-Hartman static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
653184cd4a3SBenjamin Herrenschmidt {
654184cd4a3SBenjamin Herrenschmidt 	struct pci_dev *parent;
655184cd4a3SBenjamin Herrenschmidt 	uint8_t bcomp, dcomp, fcomp;
656184cd4a3SBenjamin Herrenschmidt 	long rc, rid_end, rid;
657184cd4a3SBenjamin Herrenschmidt 
658184cd4a3SBenjamin Herrenschmidt 	/* Bus validation ? */
659184cd4a3SBenjamin Herrenschmidt 	if (pe->pbus) {
660184cd4a3SBenjamin Herrenschmidt 		int count;
661184cd4a3SBenjamin Herrenschmidt 
662184cd4a3SBenjamin Herrenschmidt 		dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
663184cd4a3SBenjamin Herrenschmidt 		fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
664184cd4a3SBenjamin Herrenschmidt 		parent = pe->pbus->self;
665fb446ad0SGavin Shan 		if (pe->flags & PNV_IODA_PE_BUS_ALL)
666b918c62eSYinghai Lu 			count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
667fb446ad0SGavin Shan 		else
668fb446ad0SGavin Shan 			count = 1;
669fb446ad0SGavin Shan 
670184cd4a3SBenjamin Herrenschmidt 		switch(count) {
671184cd4a3SBenjamin Herrenschmidt 		case  1: bcomp = OpalPciBusAll;		break;
672184cd4a3SBenjamin Herrenschmidt 		case  2: bcomp = OpalPciBus7Bits;	break;
673184cd4a3SBenjamin Herrenschmidt 		case  4: bcomp = OpalPciBus6Bits;	break;
674184cd4a3SBenjamin Herrenschmidt 		case  8: bcomp = OpalPciBus5Bits;	break;
675184cd4a3SBenjamin Herrenschmidt 		case 16: bcomp = OpalPciBus4Bits;	break;
676184cd4a3SBenjamin Herrenschmidt 		case 32: bcomp = OpalPciBus3Bits;	break;
677184cd4a3SBenjamin Herrenschmidt 		default:
678184cd4a3SBenjamin Herrenschmidt 			pr_err("%s: Number of subordinate busses %d"
679184cd4a3SBenjamin Herrenschmidt 			       " unsupported\n",
680184cd4a3SBenjamin Herrenschmidt 			       pci_name(pe->pbus->self), count);
681184cd4a3SBenjamin Herrenschmidt 			/* Do an exact match only */
682184cd4a3SBenjamin Herrenschmidt 			bcomp = OpalPciBusAll;
683184cd4a3SBenjamin Herrenschmidt 		}
684184cd4a3SBenjamin Herrenschmidt 		rid_end = pe->rid + (count << 8);
685184cd4a3SBenjamin Herrenschmidt 	} else {
686184cd4a3SBenjamin Herrenschmidt 		parent = pe->pdev->bus->self;
687184cd4a3SBenjamin Herrenschmidt 		bcomp = OpalPciBusAll;
688184cd4a3SBenjamin Herrenschmidt 		dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
689184cd4a3SBenjamin Herrenschmidt 		fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
690184cd4a3SBenjamin Herrenschmidt 		rid_end = pe->rid + 1;
691184cd4a3SBenjamin Herrenschmidt 	}
692184cd4a3SBenjamin Herrenschmidt 
693631ad691SGavin Shan 	/*
694631ad691SGavin Shan 	 * Associate PE in PELT. We need add the PE into the
695631ad691SGavin Shan 	 * corresponding PELT-V as well. Otherwise, the error
696631ad691SGavin Shan 	 * originated from the PE might contribute to other
697631ad691SGavin Shan 	 * PEs.
698631ad691SGavin Shan 	 */
699184cd4a3SBenjamin Herrenschmidt 	rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
700184cd4a3SBenjamin Herrenschmidt 			     bcomp, dcomp, fcomp, OPAL_MAP_PE);
701184cd4a3SBenjamin Herrenschmidt 	if (rc) {
702184cd4a3SBenjamin Herrenschmidt 		pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
703184cd4a3SBenjamin Herrenschmidt 		return -ENXIO;
704184cd4a3SBenjamin Herrenschmidt 	}
705631ad691SGavin Shan 
706b131a842SGavin Shan 	/* Configure PELTV */
707b131a842SGavin Shan 	pnv_ioda_set_peltv(phb, pe, true);
708184cd4a3SBenjamin Herrenschmidt 
709184cd4a3SBenjamin Herrenschmidt 	/* Setup reverse map */
710184cd4a3SBenjamin Herrenschmidt 	for (rid = pe->rid; rid < rid_end; rid++)
711184cd4a3SBenjamin Herrenschmidt 		phb->ioda.pe_rmap[rid] = pe->pe_number;
712184cd4a3SBenjamin Herrenschmidt 
713184cd4a3SBenjamin Herrenschmidt 	/* Setup one MVTs on IODA1 */
7144773f76bSGavin Shan 	if (phb->type != PNV_PHB_IODA1) {
7154773f76bSGavin Shan 		pe->mve_number = 0;
7164773f76bSGavin Shan 		goto out;
7174773f76bSGavin Shan 	}
7184773f76bSGavin Shan 
719184cd4a3SBenjamin Herrenschmidt 	pe->mve_number = pe->pe_number;
7204773f76bSGavin Shan 	rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
7214773f76bSGavin Shan 	if (rc != OPAL_SUCCESS) {
722184cd4a3SBenjamin Herrenschmidt 		pe_err(pe, "OPAL error %ld setting up MVE %d\n",
723184cd4a3SBenjamin Herrenschmidt 		       rc, pe->mve_number);
724184cd4a3SBenjamin Herrenschmidt 		pe->mve_number = -1;
725184cd4a3SBenjamin Herrenschmidt 	} else {
726184cd4a3SBenjamin Herrenschmidt 		rc = opal_pci_set_mve_enable(phb->opal_id,
727cee72d5bSBenjamin Herrenschmidt 					     pe->mve_number, OPAL_ENABLE_MVE);
728184cd4a3SBenjamin Herrenschmidt 		if (rc) {
729184cd4a3SBenjamin Herrenschmidt 			pe_err(pe, "OPAL error %ld enabling MVE %d\n",
730184cd4a3SBenjamin Herrenschmidt 			       rc, pe->mve_number);
731184cd4a3SBenjamin Herrenschmidt 			pe->mve_number = -1;
732184cd4a3SBenjamin Herrenschmidt 		}
733184cd4a3SBenjamin Herrenschmidt 	}
734184cd4a3SBenjamin Herrenschmidt 
7354773f76bSGavin Shan out:
736184cd4a3SBenjamin Herrenschmidt 	return 0;
737184cd4a3SBenjamin Herrenschmidt }
738184cd4a3SBenjamin Herrenschmidt 
739cad5cef6SGreg Kroah-Hartman static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
740184cd4a3SBenjamin Herrenschmidt 				       struct pnv_ioda_pe *pe)
741184cd4a3SBenjamin Herrenschmidt {
742184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *lpe;
743184cd4a3SBenjamin Herrenschmidt 
7447ebdf956SGavin Shan 	list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
745184cd4a3SBenjamin Herrenschmidt 		if (lpe->dma_weight < pe->dma_weight) {
7467ebdf956SGavin Shan 			list_add_tail(&pe->dma_link, &lpe->dma_link);
747184cd4a3SBenjamin Herrenschmidt 			return;
748184cd4a3SBenjamin Herrenschmidt 		}
749184cd4a3SBenjamin Herrenschmidt 	}
7507ebdf956SGavin Shan 	list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
751184cd4a3SBenjamin Herrenschmidt }
752184cd4a3SBenjamin Herrenschmidt 
753184cd4a3SBenjamin Herrenschmidt static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
754184cd4a3SBenjamin Herrenschmidt {
755184cd4a3SBenjamin Herrenschmidt 	/* This is quite simplistic. The "base" weight of a device
756184cd4a3SBenjamin Herrenschmidt 	 * is 10. 0 means no DMA is to be accounted for it.
757184cd4a3SBenjamin Herrenschmidt 	 */
758184cd4a3SBenjamin Herrenschmidt 
759184cd4a3SBenjamin Herrenschmidt 	/* If it's a bridge, no DMA */
760184cd4a3SBenjamin Herrenschmidt 	if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
761184cd4a3SBenjamin Herrenschmidt 		return 0;
762184cd4a3SBenjamin Herrenschmidt 
763184cd4a3SBenjamin Herrenschmidt 	/* Reduce the weight of slow USB controllers */
764184cd4a3SBenjamin Herrenschmidt 	if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
765184cd4a3SBenjamin Herrenschmidt 	    dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
766184cd4a3SBenjamin Herrenschmidt 	    dev->class == PCI_CLASS_SERIAL_USB_EHCI)
767184cd4a3SBenjamin Herrenschmidt 		return 3;
768184cd4a3SBenjamin Herrenschmidt 
769184cd4a3SBenjamin Herrenschmidt 	/* Increase the weight of RAID (includes Obsidian) */
770184cd4a3SBenjamin Herrenschmidt 	if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
771184cd4a3SBenjamin Herrenschmidt 		return 15;
772184cd4a3SBenjamin Herrenschmidt 
773184cd4a3SBenjamin Herrenschmidt 	/* Default */
774184cd4a3SBenjamin Herrenschmidt 	return 10;
775184cd4a3SBenjamin Herrenschmidt }
776184cd4a3SBenjamin Herrenschmidt 
777fb446ad0SGavin Shan #if 0
778cad5cef6SGreg Kroah-Hartman static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
779184cd4a3SBenjamin Herrenschmidt {
780184cd4a3SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
781184cd4a3SBenjamin Herrenschmidt 	struct pnv_phb *phb = hose->private_data;
782b72c1f65SBenjamin Herrenschmidt 	struct pci_dn *pdn = pci_get_pdn(dev);
783184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe;
784184cd4a3SBenjamin Herrenschmidt 	int pe_num;
785184cd4a3SBenjamin Herrenschmidt 
786184cd4a3SBenjamin Herrenschmidt 	if (!pdn) {
787184cd4a3SBenjamin Herrenschmidt 		pr_err("%s: Device tree node not associated properly\n",
788184cd4a3SBenjamin Herrenschmidt 			   pci_name(dev));
789184cd4a3SBenjamin Herrenschmidt 		return NULL;
790184cd4a3SBenjamin Herrenschmidt 	}
791184cd4a3SBenjamin Herrenschmidt 	if (pdn->pe_number != IODA_INVALID_PE)
792184cd4a3SBenjamin Herrenschmidt 		return NULL;
793184cd4a3SBenjamin Herrenschmidt 
794184cd4a3SBenjamin Herrenschmidt 	/* PE#0 has been pre-set */
795184cd4a3SBenjamin Herrenschmidt 	if (dev->bus->number == 0)
796184cd4a3SBenjamin Herrenschmidt 		pe_num = 0;
797184cd4a3SBenjamin Herrenschmidt 	else
798184cd4a3SBenjamin Herrenschmidt 		pe_num = pnv_ioda_alloc_pe(phb);
799184cd4a3SBenjamin Herrenschmidt 	if (pe_num == IODA_INVALID_PE) {
800184cd4a3SBenjamin Herrenschmidt 		pr_warning("%s: Not enough PE# available, disabling device\n",
801184cd4a3SBenjamin Herrenschmidt 			   pci_name(dev));
802184cd4a3SBenjamin Herrenschmidt 		return NULL;
803184cd4a3SBenjamin Herrenschmidt 	}
804184cd4a3SBenjamin Herrenschmidt 
805184cd4a3SBenjamin Herrenschmidt 	/* NOTE: We get only one ref to the pci_dev for the pdn, not for the
806184cd4a3SBenjamin Herrenschmidt 	 * pointer in the PE data structure, both should be destroyed at the
807184cd4a3SBenjamin Herrenschmidt 	 * same time. However, this needs to be looked at more closely again
808184cd4a3SBenjamin Herrenschmidt 	 * once we actually start removing things (Hotplug, SR-IOV, ...)
809184cd4a3SBenjamin Herrenschmidt 	 *
810184cd4a3SBenjamin Herrenschmidt 	 * At some point we want to remove the PDN completely anyways
811184cd4a3SBenjamin Herrenschmidt 	 */
812184cd4a3SBenjamin Herrenschmidt 	pe = &phb->ioda.pe_array[pe_num];
813184cd4a3SBenjamin Herrenschmidt 	pci_dev_get(dev);
814184cd4a3SBenjamin Herrenschmidt 	pdn->pcidev = dev;
815184cd4a3SBenjamin Herrenschmidt 	pdn->pe_number = pe_num;
816184cd4a3SBenjamin Herrenschmidt 	pe->pdev = dev;
817184cd4a3SBenjamin Herrenschmidt 	pe->pbus = NULL;
818184cd4a3SBenjamin Herrenschmidt 	pe->tce32_seg = -1;
819184cd4a3SBenjamin Herrenschmidt 	pe->mve_number = -1;
820184cd4a3SBenjamin Herrenschmidt 	pe->rid = dev->bus->number << 8 | pdn->devfn;
821184cd4a3SBenjamin Herrenschmidt 
822184cd4a3SBenjamin Herrenschmidt 	pe_info(pe, "Associated device to PE\n");
823184cd4a3SBenjamin Herrenschmidt 
824184cd4a3SBenjamin Herrenschmidt 	if (pnv_ioda_configure_pe(phb, pe)) {
825184cd4a3SBenjamin Herrenschmidt 		/* XXX What do we do here ? */
826184cd4a3SBenjamin Herrenschmidt 		if (pe_num)
827184cd4a3SBenjamin Herrenschmidt 			pnv_ioda_free_pe(phb, pe_num);
828184cd4a3SBenjamin Herrenschmidt 		pdn->pe_number = IODA_INVALID_PE;
829184cd4a3SBenjamin Herrenschmidt 		pe->pdev = NULL;
830184cd4a3SBenjamin Herrenschmidt 		pci_dev_put(dev);
831184cd4a3SBenjamin Herrenschmidt 		return NULL;
832184cd4a3SBenjamin Herrenschmidt 	}
833184cd4a3SBenjamin Herrenschmidt 
834184cd4a3SBenjamin Herrenschmidt 	/* Assign a DMA weight to the device */
835184cd4a3SBenjamin Herrenschmidt 	pe->dma_weight = pnv_ioda_dma_weight(dev);
836184cd4a3SBenjamin Herrenschmidt 	if (pe->dma_weight != 0) {
837184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_weight += pe->dma_weight;
838184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_pe_count++;
839184cd4a3SBenjamin Herrenschmidt 	}
840184cd4a3SBenjamin Herrenschmidt 
841184cd4a3SBenjamin Herrenschmidt 	/* Link the PE */
842184cd4a3SBenjamin Herrenschmidt 	pnv_ioda_link_pe_by_weight(phb, pe);
843184cd4a3SBenjamin Herrenschmidt 
844184cd4a3SBenjamin Herrenschmidt 	return pe;
845184cd4a3SBenjamin Herrenschmidt }
846fb446ad0SGavin Shan #endif /* Useful for SRIOV case */
847184cd4a3SBenjamin Herrenschmidt 
848184cd4a3SBenjamin Herrenschmidt static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
849184cd4a3SBenjamin Herrenschmidt {
850184cd4a3SBenjamin Herrenschmidt 	struct pci_dev *dev;
851184cd4a3SBenjamin Herrenschmidt 
852184cd4a3SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
853b72c1f65SBenjamin Herrenschmidt 		struct pci_dn *pdn = pci_get_pdn(dev);
854184cd4a3SBenjamin Herrenschmidt 
855184cd4a3SBenjamin Herrenschmidt 		if (pdn == NULL) {
856184cd4a3SBenjamin Herrenschmidt 			pr_warn("%s: No device node associated with device !\n",
857184cd4a3SBenjamin Herrenschmidt 				pci_name(dev));
858184cd4a3SBenjamin Herrenschmidt 			continue;
859184cd4a3SBenjamin Herrenschmidt 		}
860184cd4a3SBenjamin Herrenschmidt 		pdn->pcidev = dev;
861184cd4a3SBenjamin Herrenschmidt 		pdn->pe_number = pe->pe_number;
862184cd4a3SBenjamin Herrenschmidt 		pe->dma_weight += pnv_ioda_dma_weight(dev);
863fb446ad0SGavin Shan 		if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
864184cd4a3SBenjamin Herrenschmidt 			pnv_ioda_setup_same_PE(dev->subordinate, pe);
865184cd4a3SBenjamin Herrenschmidt 	}
866184cd4a3SBenjamin Herrenschmidt }
867184cd4a3SBenjamin Herrenschmidt 
868fb446ad0SGavin Shan /*
869fb446ad0SGavin Shan  * There're 2 types of PCI bus sensitive PEs: One that is compromised of
870fb446ad0SGavin Shan  * single PCI bus. Another one that contains the primary PCI bus and its
871fb446ad0SGavin Shan  * subordinate PCI devices and buses. The second type of PE is normally
872fb446ad0SGavin Shan  * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
873fb446ad0SGavin Shan  */
874cad5cef6SGreg Kroah-Hartman static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
875184cd4a3SBenjamin Herrenschmidt {
876fb446ad0SGavin Shan 	struct pci_controller *hose = pci_bus_to_host(bus);
877184cd4a3SBenjamin Herrenschmidt 	struct pnv_phb *phb = hose->private_data;
878184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe;
879262af557SGuo Chao 	int pe_num = IODA_INVALID_PE;
880184cd4a3SBenjamin Herrenschmidt 
881262af557SGuo Chao 	/* Check if PE is determined by M64 */
882262af557SGuo Chao 	if (phb->pick_m64_pe)
883262af557SGuo Chao 		pe_num = phb->pick_m64_pe(phb, bus, all);
884262af557SGuo Chao 
885262af557SGuo Chao 	/* The PE number isn't pinned by M64 */
886262af557SGuo Chao 	if (pe_num == IODA_INVALID_PE)
887184cd4a3SBenjamin Herrenschmidt 		pe_num = pnv_ioda_alloc_pe(phb);
888262af557SGuo Chao 
889184cd4a3SBenjamin Herrenschmidt 	if (pe_num == IODA_INVALID_PE) {
890fb446ad0SGavin Shan 		pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
891fb446ad0SGavin Shan 			__func__, pci_domain_nr(bus), bus->number);
892184cd4a3SBenjamin Herrenschmidt 		return;
893184cd4a3SBenjamin Herrenschmidt 	}
894184cd4a3SBenjamin Herrenschmidt 
895184cd4a3SBenjamin Herrenschmidt 	pe = &phb->ioda.pe_array[pe_num];
896262af557SGuo Chao 	pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
897184cd4a3SBenjamin Herrenschmidt 	pe->pbus = bus;
898184cd4a3SBenjamin Herrenschmidt 	pe->pdev = NULL;
899184cd4a3SBenjamin Herrenschmidt 	pe->tce32_seg = -1;
900184cd4a3SBenjamin Herrenschmidt 	pe->mve_number = -1;
901b918c62eSYinghai Lu 	pe->rid = bus->busn_res.start << 8;
902184cd4a3SBenjamin Herrenschmidt 	pe->dma_weight = 0;
903184cd4a3SBenjamin Herrenschmidt 
904fb446ad0SGavin Shan 	if (all)
905fb446ad0SGavin Shan 		pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
906fb446ad0SGavin Shan 			bus->busn_res.start, bus->busn_res.end, pe_num);
907fb446ad0SGavin Shan 	else
908fb446ad0SGavin Shan 		pe_info(pe, "Secondary bus %d associated with PE#%d\n",
909fb446ad0SGavin Shan 			bus->busn_res.start, pe_num);
910184cd4a3SBenjamin Herrenschmidt 
911184cd4a3SBenjamin Herrenschmidt 	if (pnv_ioda_configure_pe(phb, pe)) {
912184cd4a3SBenjamin Herrenschmidt 		/* XXX What do we do here ? */
913184cd4a3SBenjamin Herrenschmidt 		if (pe_num)
914184cd4a3SBenjamin Herrenschmidt 			pnv_ioda_free_pe(phb, pe_num);
915184cd4a3SBenjamin Herrenschmidt 		pe->pbus = NULL;
916184cd4a3SBenjamin Herrenschmidt 		return;
917184cd4a3SBenjamin Herrenschmidt 	}
918184cd4a3SBenjamin Herrenschmidt 
9199e8d4a19SWei Yang 	pe->tce32_table = kzalloc_node(sizeof(struct iommu_table),
9209e8d4a19SWei Yang 			GFP_KERNEL, hose->node);
9219e8d4a19SWei Yang 	pe->tce32_table->data = pe;
9229e8d4a19SWei Yang 
923184cd4a3SBenjamin Herrenschmidt 	/* Associate it with all child devices */
924184cd4a3SBenjamin Herrenschmidt 	pnv_ioda_setup_same_PE(bus, pe);
925184cd4a3SBenjamin Herrenschmidt 
9267ebdf956SGavin Shan 	/* Put PE to the list */
9277ebdf956SGavin Shan 	list_add_tail(&pe->list, &phb->ioda.pe_list);
9287ebdf956SGavin Shan 
929184cd4a3SBenjamin Herrenschmidt 	/* Account for one DMA PE if at least one DMA capable device exist
930184cd4a3SBenjamin Herrenschmidt 	 * below the bridge
931184cd4a3SBenjamin Herrenschmidt 	 */
932184cd4a3SBenjamin Herrenschmidt 	if (pe->dma_weight != 0) {
933184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_weight += pe->dma_weight;
934184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_pe_count++;
935184cd4a3SBenjamin Herrenschmidt 	}
936184cd4a3SBenjamin Herrenschmidt 
937184cd4a3SBenjamin Herrenschmidt 	/* Link the PE */
938184cd4a3SBenjamin Herrenschmidt 	pnv_ioda_link_pe_by_weight(phb, pe);
939184cd4a3SBenjamin Herrenschmidt }
940184cd4a3SBenjamin Herrenschmidt 
941cad5cef6SGreg Kroah-Hartman static void pnv_ioda_setup_PEs(struct pci_bus *bus)
942184cd4a3SBenjamin Herrenschmidt {
943184cd4a3SBenjamin Herrenschmidt 	struct pci_dev *dev;
944fb446ad0SGavin Shan 
945fb446ad0SGavin Shan 	pnv_ioda_setup_bus_PE(bus, 0);
946184cd4a3SBenjamin Herrenschmidt 
947184cd4a3SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
948fb446ad0SGavin Shan 		if (dev->subordinate) {
94962f87c0eSYijing Wang 			if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
950fb446ad0SGavin Shan 				pnv_ioda_setup_bus_PE(dev->subordinate, 1);
951fb446ad0SGavin Shan 			else
952184cd4a3SBenjamin Herrenschmidt 				pnv_ioda_setup_PEs(dev->subordinate);
953184cd4a3SBenjamin Herrenschmidt 		}
954184cd4a3SBenjamin Herrenschmidt 	}
955fb446ad0SGavin Shan }
956fb446ad0SGavin Shan 
957fb446ad0SGavin Shan /*
958fb446ad0SGavin Shan  * Configure PEs so that the downstream PCI buses and devices
959fb446ad0SGavin Shan  * could have their associated PE#. Unfortunately, we didn't
960fb446ad0SGavin Shan  * figure out the way to identify the PLX bridge yet. So we
961fb446ad0SGavin Shan  * simply put the PCI bus and the subordinate behind the root
962fb446ad0SGavin Shan  * port to PE# here. The game rule here is expected to be changed
963fb446ad0SGavin Shan  * as soon as we can detected PLX bridge correctly.
964fb446ad0SGavin Shan  */
965cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_setup_PEs(void)
966fb446ad0SGavin Shan {
967fb446ad0SGavin Shan 	struct pci_controller *hose, *tmp;
968262af557SGuo Chao 	struct pnv_phb *phb;
969fb446ad0SGavin Shan 
970fb446ad0SGavin Shan 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
971262af557SGuo Chao 		phb = hose->private_data;
972262af557SGuo Chao 
973262af557SGuo Chao 		/* M64 layout might affect PE allocation */
9745ef73567SGavin Shan 		if (phb->reserve_m64_pe)
9755ef73567SGavin Shan 			phb->reserve_m64_pe(phb);
976262af557SGuo Chao 
977fb446ad0SGavin Shan 		pnv_ioda_setup_PEs(hose->bus);
978fb446ad0SGavin Shan 	}
979fb446ad0SGavin Shan }
980184cd4a3SBenjamin Herrenschmidt 
981a8b2f828SGavin Shan #ifdef CONFIG_PCI_IOV
982a8b2f828SGavin Shan int pcibios_sriov_disable(struct pci_dev *pdev)
983a8b2f828SGavin Shan {
984a8b2f828SGavin Shan 	/* Release PCI data */
985a8b2f828SGavin Shan 	remove_dev_pci_data(pdev);
986a8b2f828SGavin Shan 	return 0;
987a8b2f828SGavin Shan }
988a8b2f828SGavin Shan 
989a8b2f828SGavin Shan int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
990a8b2f828SGavin Shan {
991a8b2f828SGavin Shan 	/* Allocate PCI data */
992a8b2f828SGavin Shan 	add_dev_pci_data(pdev);
993a8b2f828SGavin Shan 	return 0;
994a8b2f828SGavin Shan }
995a8b2f828SGavin Shan #endif /* CONFIG_PCI_IOV */
996a8b2f828SGavin Shan 
997959c9bddSGavin Shan static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
998184cd4a3SBenjamin Herrenschmidt {
999b72c1f65SBenjamin Herrenschmidt 	struct pci_dn *pdn = pci_get_pdn(pdev);
1000959c9bddSGavin Shan 	struct pnv_ioda_pe *pe;
1001184cd4a3SBenjamin Herrenschmidt 
1002959c9bddSGavin Shan 	/*
1003959c9bddSGavin Shan 	 * The function can be called while the PE#
1004959c9bddSGavin Shan 	 * hasn't been assigned. Do nothing for the
1005959c9bddSGavin Shan 	 * case.
1006959c9bddSGavin Shan 	 */
1007959c9bddSGavin Shan 	if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1008959c9bddSGavin Shan 		return;
1009184cd4a3SBenjamin Herrenschmidt 
1010959c9bddSGavin Shan 	pe = &phb->ioda.pe_array[pdn->pe_number];
1011cd15b048SBenjamin Herrenschmidt 	WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
10129e8d4a19SWei Yang 	set_iommu_table_base_and_group(&pdev->dev, pe->tce32_table);
1013184cd4a3SBenjamin Herrenschmidt }
1014184cd4a3SBenjamin Herrenschmidt 
1015cd15b048SBenjamin Herrenschmidt static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
1016cd15b048SBenjamin Herrenschmidt 				     struct pci_dev *pdev, u64 dma_mask)
1017cd15b048SBenjamin Herrenschmidt {
1018cd15b048SBenjamin Herrenschmidt 	struct pci_dn *pdn = pci_get_pdn(pdev);
1019cd15b048SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe;
1020cd15b048SBenjamin Herrenschmidt 	uint64_t top;
1021cd15b048SBenjamin Herrenschmidt 	bool bypass = false;
1022cd15b048SBenjamin Herrenschmidt 
1023cd15b048SBenjamin Herrenschmidt 	if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1024cd15b048SBenjamin Herrenschmidt 		return -ENODEV;;
1025cd15b048SBenjamin Herrenschmidt 
1026cd15b048SBenjamin Herrenschmidt 	pe = &phb->ioda.pe_array[pdn->pe_number];
1027cd15b048SBenjamin Herrenschmidt 	if (pe->tce_bypass_enabled) {
1028cd15b048SBenjamin Herrenschmidt 		top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1029cd15b048SBenjamin Herrenschmidt 		bypass = (dma_mask >= top);
1030cd15b048SBenjamin Herrenschmidt 	}
1031cd15b048SBenjamin Herrenschmidt 
1032cd15b048SBenjamin Herrenschmidt 	if (bypass) {
1033cd15b048SBenjamin Herrenschmidt 		dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1034cd15b048SBenjamin Herrenschmidt 		set_dma_ops(&pdev->dev, &dma_direct_ops);
1035cd15b048SBenjamin Herrenschmidt 		set_dma_offset(&pdev->dev, pe->tce_bypass_base);
1036cd15b048SBenjamin Herrenschmidt 	} else {
1037cd15b048SBenjamin Herrenschmidt 		dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1038cd15b048SBenjamin Herrenschmidt 		set_dma_ops(&pdev->dev, &dma_iommu_ops);
10399e8d4a19SWei Yang 		set_iommu_table_base(&pdev->dev, pe->tce32_table);
1040cd15b048SBenjamin Herrenschmidt 	}
1041a32305bfSBrian W Hart 	*pdev->dev.dma_mask = dma_mask;
1042cd15b048SBenjamin Herrenschmidt 	return 0;
1043cd15b048SBenjamin Herrenschmidt }
1044cd15b048SBenjamin Herrenschmidt 
1045fe7e85c6SGavin Shan static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
1046fe7e85c6SGavin Shan 					      struct pci_dev *pdev)
1047fe7e85c6SGavin Shan {
1048fe7e85c6SGavin Shan 	struct pci_dn *pdn = pci_get_pdn(pdev);
1049fe7e85c6SGavin Shan 	struct pnv_ioda_pe *pe;
1050fe7e85c6SGavin Shan 	u64 end, mask;
1051fe7e85c6SGavin Shan 
1052fe7e85c6SGavin Shan 	if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1053fe7e85c6SGavin Shan 		return 0;
1054fe7e85c6SGavin Shan 
1055fe7e85c6SGavin Shan 	pe = &phb->ioda.pe_array[pdn->pe_number];
1056fe7e85c6SGavin Shan 	if (!pe->tce_bypass_enabled)
1057fe7e85c6SGavin Shan 		return __dma_get_required_mask(&pdev->dev);
1058fe7e85c6SGavin Shan 
1059fe7e85c6SGavin Shan 
1060fe7e85c6SGavin Shan 	end = pe->tce_bypass_base + memblock_end_of_DRAM();
1061fe7e85c6SGavin Shan 	mask = 1ULL << (fls64(end) - 1);
1062fe7e85c6SGavin Shan 	mask += mask - 1;
1063fe7e85c6SGavin Shan 
1064fe7e85c6SGavin Shan 	return mask;
1065fe7e85c6SGavin Shan }
1066fe7e85c6SGavin Shan 
1067dff4a39eSGavin Shan static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
1068dff4a39eSGavin Shan 				   struct pci_bus *bus,
1069dff4a39eSGavin Shan 				   bool add_to_iommu_group)
107074251fe2SBenjamin Herrenschmidt {
107174251fe2SBenjamin Herrenschmidt 	struct pci_dev *dev;
107274251fe2SBenjamin Herrenschmidt 
107374251fe2SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
1074dff4a39eSGavin Shan 		if (add_to_iommu_group)
1075dff4a39eSGavin Shan 			set_iommu_table_base_and_group(&dev->dev,
10769e8d4a19SWei Yang 						       pe->tce32_table);
1077dff4a39eSGavin Shan 		else
10789e8d4a19SWei Yang 			set_iommu_table_base(&dev->dev, pe->tce32_table);
1079dff4a39eSGavin Shan 
108074251fe2SBenjamin Herrenschmidt 		if (dev->subordinate)
1081dff4a39eSGavin Shan 			pnv_ioda_setup_bus_dma(pe, dev->subordinate,
1082dff4a39eSGavin Shan 					       add_to_iommu_group);
108374251fe2SBenjamin Herrenschmidt 	}
108474251fe2SBenjamin Herrenschmidt }
108574251fe2SBenjamin Herrenschmidt 
10868e0a1611SAlexey Kardashevskiy static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
10878e0a1611SAlexey Kardashevskiy 					 struct iommu_table *tbl,
10883ad26e5cSBenjamin Herrenschmidt 					 __be64 *startp, __be64 *endp, bool rm)
10894cce9550SGavin Shan {
10903ad26e5cSBenjamin Herrenschmidt 	__be64 __iomem *invalidate = rm ?
10913ad26e5cSBenjamin Herrenschmidt 		(__be64 __iomem *)pe->tce_inval_reg_phys :
10923ad26e5cSBenjamin Herrenschmidt 		(__be64 __iomem *)tbl->it_index;
10934cce9550SGavin Shan 	unsigned long start, end, inc;
1094b0376c9bSAlexey Kardashevskiy 	const unsigned shift = tbl->it_page_shift;
10954cce9550SGavin Shan 
10964cce9550SGavin Shan 	start = __pa(startp);
10974cce9550SGavin Shan 	end = __pa(endp);
10984cce9550SGavin Shan 
10994cce9550SGavin Shan 	/* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
11004cce9550SGavin Shan 	if (tbl->it_busno) {
1101b0376c9bSAlexey Kardashevskiy 		start <<= shift;
1102b0376c9bSAlexey Kardashevskiy 		end <<= shift;
1103b0376c9bSAlexey Kardashevskiy 		inc = 128ull << shift;
11044cce9550SGavin Shan 		start |= tbl->it_busno;
11054cce9550SGavin Shan 		end |= tbl->it_busno;
11064cce9550SGavin Shan 	} else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
11074cce9550SGavin Shan 		/* p7ioc-style invalidation, 2 TCEs per write */
11084cce9550SGavin Shan 		start |= (1ull << 63);
11094cce9550SGavin Shan 		end |= (1ull << 63);
11104cce9550SGavin Shan 		inc = 16;
11114cce9550SGavin Shan         } else {
11124cce9550SGavin Shan 		/* Default (older HW) */
11134cce9550SGavin Shan                 inc = 128;
11144cce9550SGavin Shan 	}
11154cce9550SGavin Shan 
11164cce9550SGavin Shan         end |= inc - 1;	/* round up end to be different than start */
11174cce9550SGavin Shan 
11184cce9550SGavin Shan         mb(); /* Ensure above stores are visible */
11194cce9550SGavin Shan         while (start <= end) {
11208e0a1611SAlexey Kardashevskiy 		if (rm)
11213ad26e5cSBenjamin Herrenschmidt 			__raw_rm_writeq(cpu_to_be64(start), invalidate);
11228e0a1611SAlexey Kardashevskiy 		else
11233a1a4661SBenjamin Herrenschmidt 			__raw_writeq(cpu_to_be64(start), invalidate);
11244cce9550SGavin Shan                 start += inc;
11254cce9550SGavin Shan         }
11264cce9550SGavin Shan 
11274cce9550SGavin Shan 	/*
11284cce9550SGavin Shan 	 * The iommu layer will do another mb() for us on build()
11294cce9550SGavin Shan 	 * and we don't care on free()
11304cce9550SGavin Shan 	 */
11314cce9550SGavin Shan }
11324cce9550SGavin Shan 
11334cce9550SGavin Shan static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
11344cce9550SGavin Shan 					 struct iommu_table *tbl,
11353ad26e5cSBenjamin Herrenschmidt 					 __be64 *startp, __be64 *endp, bool rm)
11364cce9550SGavin Shan {
11374cce9550SGavin Shan 	unsigned long start, end, inc;
11383ad26e5cSBenjamin Herrenschmidt 	__be64 __iomem *invalidate = rm ?
11393ad26e5cSBenjamin Herrenschmidt 		(__be64 __iomem *)pe->tce_inval_reg_phys :
11403ad26e5cSBenjamin Herrenschmidt 		(__be64 __iomem *)tbl->it_index;
1141b0376c9bSAlexey Kardashevskiy 	const unsigned shift = tbl->it_page_shift;
11424cce9550SGavin Shan 
11434cce9550SGavin Shan 	/* We'll invalidate DMA address in PE scope */
1144b0376c9bSAlexey Kardashevskiy 	start = 0x2ull << 60;
11454cce9550SGavin Shan 	start |= (pe->pe_number & 0xFF);
11464cce9550SGavin Shan 	end = start;
11474cce9550SGavin Shan 
11484cce9550SGavin Shan 	/* Figure out the start, end and step */
11494cce9550SGavin Shan 	inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
1150b0376c9bSAlexey Kardashevskiy 	start |= (inc << shift);
11514cce9550SGavin Shan 	inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
1152b0376c9bSAlexey Kardashevskiy 	end |= (inc << shift);
1153b0376c9bSAlexey Kardashevskiy 	inc = (0x1ull << shift);
11544cce9550SGavin Shan 	mb();
11554cce9550SGavin Shan 
11564cce9550SGavin Shan 	while (start <= end) {
11578e0a1611SAlexey Kardashevskiy 		if (rm)
11583ad26e5cSBenjamin Herrenschmidt 			__raw_rm_writeq(cpu_to_be64(start), invalidate);
11598e0a1611SAlexey Kardashevskiy 		else
11603a1a4661SBenjamin Herrenschmidt 			__raw_writeq(cpu_to_be64(start), invalidate);
11614cce9550SGavin Shan 		start += inc;
11624cce9550SGavin Shan 	}
11634cce9550SGavin Shan }
11644cce9550SGavin Shan 
11654cce9550SGavin Shan void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
11663ad26e5cSBenjamin Herrenschmidt 				 __be64 *startp, __be64 *endp, bool rm)
11674cce9550SGavin Shan {
11689e8d4a19SWei Yang 	struct pnv_ioda_pe *pe = tbl->data;
11694cce9550SGavin Shan 	struct pnv_phb *phb = pe->phb;
11704cce9550SGavin Shan 
11714cce9550SGavin Shan 	if (phb->type == PNV_PHB_IODA1)
11728e0a1611SAlexey Kardashevskiy 		pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
11734cce9550SGavin Shan 	else
11748e0a1611SAlexey Kardashevskiy 		pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
11754cce9550SGavin Shan }
11764cce9550SGavin Shan 
1177cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1178cad5cef6SGreg Kroah-Hartman 				      struct pnv_ioda_pe *pe, unsigned int base,
1179184cd4a3SBenjamin Herrenschmidt 				      unsigned int segs)
1180184cd4a3SBenjamin Herrenschmidt {
1181184cd4a3SBenjamin Herrenschmidt 
1182184cd4a3SBenjamin Herrenschmidt 	struct page *tce_mem = NULL;
1183184cd4a3SBenjamin Herrenschmidt 	const __be64 *swinvp;
1184184cd4a3SBenjamin Herrenschmidt 	struct iommu_table *tbl;
1185184cd4a3SBenjamin Herrenschmidt 	unsigned int i;
1186184cd4a3SBenjamin Herrenschmidt 	int64_t rc;
1187184cd4a3SBenjamin Herrenschmidt 	void *addr;
1188184cd4a3SBenjamin Herrenschmidt 
1189184cd4a3SBenjamin Herrenschmidt 	/* 256M DMA window, 4K TCE pages, 8 bytes TCE */
1190184cd4a3SBenjamin Herrenschmidt #define TCE32_TABLE_SIZE	((0x10000000 / 0x1000) * 8)
1191184cd4a3SBenjamin Herrenschmidt 
1192184cd4a3SBenjamin Herrenschmidt 	/* XXX FIXME: Handle 64-bit only DMA devices */
1193184cd4a3SBenjamin Herrenschmidt 	/* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1194184cd4a3SBenjamin Herrenschmidt 	/* XXX FIXME: Allocate multi-level tables on PHB3 */
1195184cd4a3SBenjamin Herrenschmidt 
1196184cd4a3SBenjamin Herrenschmidt 	/* We shouldn't already have a 32-bit DMA associated */
1197184cd4a3SBenjamin Herrenschmidt 	if (WARN_ON(pe->tce32_seg >= 0))
1198184cd4a3SBenjamin Herrenschmidt 		return;
1199184cd4a3SBenjamin Herrenschmidt 
1200184cd4a3SBenjamin Herrenschmidt 	/* Grab a 32-bit TCE table */
1201184cd4a3SBenjamin Herrenschmidt 	pe->tce32_seg = base;
1202184cd4a3SBenjamin Herrenschmidt 	pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1203184cd4a3SBenjamin Herrenschmidt 		(base << 28), ((base + segs) << 28) - 1);
1204184cd4a3SBenjamin Herrenschmidt 
1205184cd4a3SBenjamin Herrenschmidt 	/* XXX Currently, we allocate one big contiguous table for the
1206184cd4a3SBenjamin Herrenschmidt 	 * TCEs. We only really need one chunk per 256M of TCE space
1207184cd4a3SBenjamin Herrenschmidt 	 * (ie per segment) but that's an optimization for later, it
1208184cd4a3SBenjamin Herrenschmidt 	 * requires some added smarts with our get/put_tce implementation
1209184cd4a3SBenjamin Herrenschmidt 	 */
1210184cd4a3SBenjamin Herrenschmidt 	tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1211184cd4a3SBenjamin Herrenschmidt 				   get_order(TCE32_TABLE_SIZE * segs));
1212184cd4a3SBenjamin Herrenschmidt 	if (!tce_mem) {
1213184cd4a3SBenjamin Herrenschmidt 		pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1214184cd4a3SBenjamin Herrenschmidt 		goto fail;
1215184cd4a3SBenjamin Herrenschmidt 	}
1216184cd4a3SBenjamin Herrenschmidt 	addr = page_address(tce_mem);
1217184cd4a3SBenjamin Herrenschmidt 	memset(addr, 0, TCE32_TABLE_SIZE * segs);
1218184cd4a3SBenjamin Herrenschmidt 
1219184cd4a3SBenjamin Herrenschmidt 	/* Configure HW */
1220184cd4a3SBenjamin Herrenschmidt 	for (i = 0; i < segs; i++) {
1221184cd4a3SBenjamin Herrenschmidt 		rc = opal_pci_map_pe_dma_window(phb->opal_id,
1222184cd4a3SBenjamin Herrenschmidt 					      pe->pe_number,
1223184cd4a3SBenjamin Herrenschmidt 					      base + i, 1,
1224184cd4a3SBenjamin Herrenschmidt 					      __pa(addr) + TCE32_TABLE_SIZE * i,
1225184cd4a3SBenjamin Herrenschmidt 					      TCE32_TABLE_SIZE, 0x1000);
1226184cd4a3SBenjamin Herrenschmidt 		if (rc) {
1227184cd4a3SBenjamin Herrenschmidt 			pe_err(pe, " Failed to configure 32-bit TCE table,"
1228184cd4a3SBenjamin Herrenschmidt 			       " err %ld\n", rc);
1229184cd4a3SBenjamin Herrenschmidt 			goto fail;
1230184cd4a3SBenjamin Herrenschmidt 		}
1231184cd4a3SBenjamin Herrenschmidt 	}
1232184cd4a3SBenjamin Herrenschmidt 
1233184cd4a3SBenjamin Herrenschmidt 	/* Setup linux iommu table */
12349e8d4a19SWei Yang 	tbl = pe->tce32_table;
1235184cd4a3SBenjamin Herrenschmidt 	pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
12368fa5d454SAlexey Kardashevskiy 				  base << 28, IOMMU_PAGE_SHIFT_4K);
1237184cd4a3SBenjamin Herrenschmidt 
1238184cd4a3SBenjamin Herrenschmidt 	/* OPAL variant of P7IOC SW invalidated TCEs */
1239184cd4a3SBenjamin Herrenschmidt 	swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1240184cd4a3SBenjamin Herrenschmidt 	if (swinvp) {
1241184cd4a3SBenjamin Herrenschmidt 		/* We need a couple more fields -- an address and a data
1242184cd4a3SBenjamin Herrenschmidt 		 * to or.  Since the bus is only printed out on table free
1243184cd4a3SBenjamin Herrenschmidt 		 * errors, and on the first pass the data will be a relative
1244184cd4a3SBenjamin Herrenschmidt 		 * bus number, print that out instead.
1245184cd4a3SBenjamin Herrenschmidt 		 */
12468e0a1611SAlexey Kardashevskiy 		pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
12478e0a1611SAlexey Kardashevskiy 		tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
12488e0a1611SAlexey Kardashevskiy 				8);
124965fd766bSGavin Shan 		tbl->it_type |= (TCE_PCI_SWINV_CREATE |
125065fd766bSGavin Shan 				 TCE_PCI_SWINV_FREE   |
125165fd766bSGavin Shan 				 TCE_PCI_SWINV_PAIR);
1252184cd4a3SBenjamin Herrenschmidt 	}
1253184cd4a3SBenjamin Herrenschmidt 	iommu_init_table(tbl, phb->hose->node);
1254e9bc03feSGavin Shan 	iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
1255184cd4a3SBenjamin Herrenschmidt 
125674251fe2SBenjamin Herrenschmidt 	if (pe->pdev)
1257d905c5dfSAlexey Kardashevskiy 		set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
125874251fe2SBenjamin Herrenschmidt 	else
1259dff4a39eSGavin Shan 		pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
126074251fe2SBenjamin Herrenschmidt 
1261184cd4a3SBenjamin Herrenschmidt 	return;
1262184cd4a3SBenjamin Herrenschmidt  fail:
1263184cd4a3SBenjamin Herrenschmidt 	/* XXX Failure: Try to fallback to 64-bit only ? */
1264184cd4a3SBenjamin Herrenschmidt 	if (pe->tce32_seg >= 0)
1265184cd4a3SBenjamin Herrenschmidt 		pe->tce32_seg = -1;
1266184cd4a3SBenjamin Herrenschmidt 	if (tce_mem)
1267184cd4a3SBenjamin Herrenschmidt 		__free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
1268184cd4a3SBenjamin Herrenschmidt }
1269184cd4a3SBenjamin Herrenschmidt 
1270cd15b048SBenjamin Herrenschmidt static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
1271cd15b048SBenjamin Herrenschmidt {
12729e8d4a19SWei Yang 	struct pnv_ioda_pe *pe = tbl->data;
1273cd15b048SBenjamin Herrenschmidt 	uint16_t window_id = (pe->pe_number << 1 ) + 1;
1274cd15b048SBenjamin Herrenschmidt 	int64_t rc;
1275cd15b048SBenjamin Herrenschmidt 
1276cd15b048SBenjamin Herrenschmidt 	pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1277cd15b048SBenjamin Herrenschmidt 	if (enable) {
1278cd15b048SBenjamin Herrenschmidt 		phys_addr_t top = memblock_end_of_DRAM();
1279cd15b048SBenjamin Herrenschmidt 
1280cd15b048SBenjamin Herrenschmidt 		top = roundup_pow_of_two(top);
1281cd15b048SBenjamin Herrenschmidt 		rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1282cd15b048SBenjamin Herrenschmidt 						     pe->pe_number,
1283cd15b048SBenjamin Herrenschmidt 						     window_id,
1284cd15b048SBenjamin Herrenschmidt 						     pe->tce_bypass_base,
1285cd15b048SBenjamin Herrenschmidt 						     top);
1286cd15b048SBenjamin Herrenschmidt 	} else {
1287cd15b048SBenjamin Herrenschmidt 		rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1288cd15b048SBenjamin Herrenschmidt 						     pe->pe_number,
1289cd15b048SBenjamin Herrenschmidt 						     window_id,
1290cd15b048SBenjamin Herrenschmidt 						     pe->tce_bypass_base,
1291cd15b048SBenjamin Herrenschmidt 						     0);
1292cd15b048SBenjamin Herrenschmidt 
1293cd15b048SBenjamin Herrenschmidt 		/*
1294dff4a39eSGavin Shan 		 * EEH needs the mapping between IOMMU table and group
1295dff4a39eSGavin Shan 		 * of those VFIO/KVM pass-through devices. We can postpone
1296dff4a39eSGavin Shan 		 * resetting DMA ops until the DMA mask is configured in
1297dff4a39eSGavin Shan 		 * host side.
1298cd15b048SBenjamin Herrenschmidt 		 */
1299dff4a39eSGavin Shan 		if (pe->pdev)
1300dff4a39eSGavin Shan 			set_iommu_table_base(&pe->pdev->dev, tbl);
1301dff4a39eSGavin Shan 		else
1302dff4a39eSGavin Shan 			pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
1303cd15b048SBenjamin Herrenschmidt 	}
1304cd15b048SBenjamin Herrenschmidt 	if (rc)
1305cd15b048SBenjamin Herrenschmidt 		pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1306cd15b048SBenjamin Herrenschmidt 	else
1307cd15b048SBenjamin Herrenschmidt 		pe->tce_bypass_enabled = enable;
1308cd15b048SBenjamin Herrenschmidt }
1309cd15b048SBenjamin Herrenschmidt 
1310cd15b048SBenjamin Herrenschmidt static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
1311cd15b048SBenjamin Herrenschmidt 					  struct pnv_ioda_pe *pe)
1312cd15b048SBenjamin Herrenschmidt {
1313cd15b048SBenjamin Herrenschmidt 	/* TVE #1 is selected by PCI address bit 59 */
1314cd15b048SBenjamin Herrenschmidt 	pe->tce_bypass_base = 1ull << 59;
1315cd15b048SBenjamin Herrenschmidt 
1316cd15b048SBenjamin Herrenschmidt 	/* Install set_bypass callback for VFIO */
13179e8d4a19SWei Yang 	pe->tce32_table->set_bypass = pnv_pci_ioda2_set_bypass;
1318cd15b048SBenjamin Herrenschmidt 
1319cd15b048SBenjamin Herrenschmidt 	/* Enable bypass by default */
13209e8d4a19SWei Yang 	pnv_pci_ioda2_set_bypass(pe->tce32_table, true);
1321cd15b048SBenjamin Herrenschmidt }
1322cd15b048SBenjamin Herrenschmidt 
1323373f5657SGavin Shan static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1324373f5657SGavin Shan 				       struct pnv_ioda_pe *pe)
1325373f5657SGavin Shan {
1326373f5657SGavin Shan 	struct page *tce_mem = NULL;
1327373f5657SGavin Shan 	void *addr;
1328373f5657SGavin Shan 	const __be64 *swinvp;
1329373f5657SGavin Shan 	struct iommu_table *tbl;
1330373f5657SGavin Shan 	unsigned int tce_table_size, end;
1331373f5657SGavin Shan 	int64_t rc;
1332373f5657SGavin Shan 
1333373f5657SGavin Shan 	/* We shouldn't already have a 32-bit DMA associated */
1334373f5657SGavin Shan 	if (WARN_ON(pe->tce32_seg >= 0))
1335373f5657SGavin Shan 		return;
1336373f5657SGavin Shan 
1337373f5657SGavin Shan 	/* The PE will reserve all possible 32-bits space */
1338373f5657SGavin Shan 	pe->tce32_seg = 0;
1339373f5657SGavin Shan 	end = (1 << ilog2(phb->ioda.m32_pci_base));
1340373f5657SGavin Shan 	tce_table_size = (end / 0x1000) * 8;
1341373f5657SGavin Shan 	pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1342373f5657SGavin Shan 		end);
1343373f5657SGavin Shan 
1344373f5657SGavin Shan 	/* Allocate TCE table */
1345373f5657SGavin Shan 	tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1346373f5657SGavin Shan 				   get_order(tce_table_size));
1347373f5657SGavin Shan 	if (!tce_mem) {
1348373f5657SGavin Shan 		pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
1349373f5657SGavin Shan 		goto fail;
1350373f5657SGavin Shan 	}
1351373f5657SGavin Shan 	addr = page_address(tce_mem);
1352373f5657SGavin Shan 	memset(addr, 0, tce_table_size);
1353373f5657SGavin Shan 
1354373f5657SGavin Shan 	/*
1355373f5657SGavin Shan 	 * Map TCE table through TVT. The TVE index is the PE number
1356373f5657SGavin Shan 	 * shifted by 1 bit for 32-bits DMA space.
1357373f5657SGavin Shan 	 */
1358373f5657SGavin Shan 	rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1359373f5657SGavin Shan 					pe->pe_number << 1, 1, __pa(addr),
1360373f5657SGavin Shan 					tce_table_size, 0x1000);
1361373f5657SGavin Shan 	if (rc) {
1362373f5657SGavin Shan 		pe_err(pe, "Failed to configure 32-bit TCE table,"
1363373f5657SGavin Shan 		       " err %ld\n", rc);
1364373f5657SGavin Shan 		goto fail;
1365373f5657SGavin Shan 	}
1366373f5657SGavin Shan 
1367373f5657SGavin Shan 	/* Setup linux iommu table */
13689e8d4a19SWei Yang 	tbl = pe->tce32_table;
13698fa5d454SAlexey Kardashevskiy 	pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
13708fa5d454SAlexey Kardashevskiy 			IOMMU_PAGE_SHIFT_4K);
1371373f5657SGavin Shan 
1372373f5657SGavin Shan 	/* OPAL variant of PHB3 invalidated TCEs */
1373373f5657SGavin Shan 	swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1374373f5657SGavin Shan 	if (swinvp) {
1375373f5657SGavin Shan 		/* We need a couple more fields -- an address and a data
1376373f5657SGavin Shan 		 * to or.  Since the bus is only printed out on table free
1377373f5657SGavin Shan 		 * errors, and on the first pass the data will be a relative
1378373f5657SGavin Shan 		 * bus number, print that out instead.
1379373f5657SGavin Shan 		 */
13808e0a1611SAlexey Kardashevskiy 		pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
13818e0a1611SAlexey Kardashevskiy 		tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
13828e0a1611SAlexey Kardashevskiy 				8);
138365fd766bSGavin Shan 		tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
1384373f5657SGavin Shan 	}
1385373f5657SGavin Shan 	iommu_init_table(tbl, phb->hose->node);
1386e9bc03feSGavin Shan 	iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
1387373f5657SGavin Shan 
138874251fe2SBenjamin Herrenschmidt 	if (pe->pdev)
1389d905c5dfSAlexey Kardashevskiy 		set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
139074251fe2SBenjamin Herrenschmidt 	else
1391dff4a39eSGavin Shan 		pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
139274251fe2SBenjamin Herrenschmidt 
1393cd15b048SBenjamin Herrenschmidt 	/* Also create a bypass window */
13944e287840SThadeu Lima de Souza Cascardo 	if (!pnv_iommu_bypass_disabled)
1395cd15b048SBenjamin Herrenschmidt 		pnv_pci_ioda2_setup_bypass_pe(phb, pe);
13964e287840SThadeu Lima de Souza Cascardo 
1397373f5657SGavin Shan 	return;
1398373f5657SGavin Shan fail:
1399373f5657SGavin Shan 	if (pe->tce32_seg >= 0)
1400373f5657SGavin Shan 		pe->tce32_seg = -1;
1401373f5657SGavin Shan 	if (tce_mem)
1402373f5657SGavin Shan 		__free_pages(tce_mem, get_order(tce_table_size));
1403373f5657SGavin Shan }
1404373f5657SGavin Shan 
1405cad5cef6SGreg Kroah-Hartman static void pnv_ioda_setup_dma(struct pnv_phb *phb)
1406184cd4a3SBenjamin Herrenschmidt {
1407184cd4a3SBenjamin Herrenschmidt 	struct pci_controller *hose = phb->hose;
1408184cd4a3SBenjamin Herrenschmidt 	unsigned int residual, remaining, segs, tw, base;
1409184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe;
1410184cd4a3SBenjamin Herrenschmidt 
1411184cd4a3SBenjamin Herrenschmidt 	/* If we have more PE# than segments available, hand out one
1412184cd4a3SBenjamin Herrenschmidt 	 * per PE until we run out and let the rest fail. If not,
1413184cd4a3SBenjamin Herrenschmidt 	 * then we assign at least one segment per PE, plus more based
1414184cd4a3SBenjamin Herrenschmidt 	 * on the amount of devices under that PE
1415184cd4a3SBenjamin Herrenschmidt 	 */
1416184cd4a3SBenjamin Herrenschmidt 	if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
1417184cd4a3SBenjamin Herrenschmidt 		residual = 0;
1418184cd4a3SBenjamin Herrenschmidt 	else
1419184cd4a3SBenjamin Herrenschmidt 		residual = phb->ioda.tce32_count -
1420184cd4a3SBenjamin Herrenschmidt 			phb->ioda.dma_pe_count;
1421184cd4a3SBenjamin Herrenschmidt 
1422184cd4a3SBenjamin Herrenschmidt 	pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
1423184cd4a3SBenjamin Herrenschmidt 		hose->global_number, phb->ioda.tce32_count);
1424184cd4a3SBenjamin Herrenschmidt 	pr_info("PCI: %d PE# for a total weight of %d\n",
1425184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_pe_count, phb->ioda.dma_weight);
1426184cd4a3SBenjamin Herrenschmidt 
1427184cd4a3SBenjamin Herrenschmidt 	/* Walk our PE list and configure their DMA segments, hand them
1428184cd4a3SBenjamin Herrenschmidt 	 * out one base segment plus any residual segments based on
1429184cd4a3SBenjamin Herrenschmidt 	 * weight
1430184cd4a3SBenjamin Herrenschmidt 	 */
1431184cd4a3SBenjamin Herrenschmidt 	remaining = phb->ioda.tce32_count;
1432184cd4a3SBenjamin Herrenschmidt 	tw = phb->ioda.dma_weight;
1433184cd4a3SBenjamin Herrenschmidt 	base = 0;
14347ebdf956SGavin Shan 	list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
1435184cd4a3SBenjamin Herrenschmidt 		if (!pe->dma_weight)
1436184cd4a3SBenjamin Herrenschmidt 			continue;
1437184cd4a3SBenjamin Herrenschmidt 		if (!remaining) {
1438184cd4a3SBenjamin Herrenschmidt 			pe_warn(pe, "No DMA32 resources available\n");
1439184cd4a3SBenjamin Herrenschmidt 			continue;
1440184cd4a3SBenjamin Herrenschmidt 		}
1441184cd4a3SBenjamin Herrenschmidt 		segs = 1;
1442184cd4a3SBenjamin Herrenschmidt 		if (residual) {
1443184cd4a3SBenjamin Herrenschmidt 			segs += ((pe->dma_weight * residual)  + (tw / 2)) / tw;
1444184cd4a3SBenjamin Herrenschmidt 			if (segs > remaining)
1445184cd4a3SBenjamin Herrenschmidt 				segs = remaining;
1446184cd4a3SBenjamin Herrenschmidt 		}
1447373f5657SGavin Shan 
1448373f5657SGavin Shan 		/*
1449373f5657SGavin Shan 		 * For IODA2 compliant PHB3, we needn't care about the weight.
1450373f5657SGavin Shan 		 * The all available 32-bits DMA space will be assigned to
1451373f5657SGavin Shan 		 * the specific PE.
1452373f5657SGavin Shan 		 */
1453373f5657SGavin Shan 		if (phb->type == PNV_PHB_IODA1) {
1454184cd4a3SBenjamin Herrenschmidt 			pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1455184cd4a3SBenjamin Herrenschmidt 				pe->dma_weight, segs);
1456184cd4a3SBenjamin Herrenschmidt 			pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1457373f5657SGavin Shan 		} else {
1458373f5657SGavin Shan 			pe_info(pe, "Assign DMA32 space\n");
1459373f5657SGavin Shan 			segs = 0;
1460373f5657SGavin Shan 			pnv_pci_ioda2_setup_dma_pe(phb, pe);
1461373f5657SGavin Shan 		}
1462373f5657SGavin Shan 
1463184cd4a3SBenjamin Herrenschmidt 		remaining -= segs;
1464184cd4a3SBenjamin Herrenschmidt 		base += segs;
1465184cd4a3SBenjamin Herrenschmidt 	}
1466184cd4a3SBenjamin Herrenschmidt }
1467184cd4a3SBenjamin Herrenschmidt 
1468184cd4a3SBenjamin Herrenschmidt #ifdef CONFIG_PCI_MSI
1469137436c9SGavin Shan static void pnv_ioda2_msi_eoi(struct irq_data *d)
1470137436c9SGavin Shan {
1471137436c9SGavin Shan 	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1472137436c9SGavin Shan 	struct irq_chip *chip = irq_data_get_irq_chip(d);
1473137436c9SGavin Shan 	struct pnv_phb *phb = container_of(chip, struct pnv_phb,
1474137436c9SGavin Shan 					   ioda.irq_chip);
1475137436c9SGavin Shan 	int64_t rc;
1476137436c9SGavin Shan 
1477137436c9SGavin Shan 	rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
1478137436c9SGavin Shan 	WARN_ON_ONCE(rc);
1479137436c9SGavin Shan 
1480137436c9SGavin Shan 	icp_native_eoi(d);
1481137436c9SGavin Shan }
1482137436c9SGavin Shan 
1483fd9a1c26SIan Munsie 
1484fd9a1c26SIan Munsie static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
1485fd9a1c26SIan Munsie {
1486fd9a1c26SIan Munsie 	struct irq_data *idata;
1487fd9a1c26SIan Munsie 	struct irq_chip *ichip;
1488fd9a1c26SIan Munsie 
1489fd9a1c26SIan Munsie 	if (phb->type != PNV_PHB_IODA2)
1490fd9a1c26SIan Munsie 		return;
1491fd9a1c26SIan Munsie 
1492fd9a1c26SIan Munsie 	if (!phb->ioda.irq_chip_init) {
1493fd9a1c26SIan Munsie 		/*
1494fd9a1c26SIan Munsie 		 * First time we setup an MSI IRQ, we need to setup the
1495fd9a1c26SIan Munsie 		 * corresponding IRQ chip to route correctly.
1496fd9a1c26SIan Munsie 		 */
1497fd9a1c26SIan Munsie 		idata = irq_get_irq_data(virq);
1498fd9a1c26SIan Munsie 		ichip = irq_data_get_irq_chip(idata);
1499fd9a1c26SIan Munsie 		phb->ioda.irq_chip_init = 1;
1500fd9a1c26SIan Munsie 		phb->ioda.irq_chip = *ichip;
1501fd9a1c26SIan Munsie 		phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
1502fd9a1c26SIan Munsie 	}
1503fd9a1c26SIan Munsie 	irq_set_chip(virq, &phb->ioda.irq_chip);
1504fd9a1c26SIan Munsie }
1505fd9a1c26SIan Munsie 
150680c49c7eSIan Munsie #ifdef CONFIG_CXL_BASE
150780c49c7eSIan Munsie 
15086f963ec2SRyan Grimm struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
150980c49c7eSIan Munsie {
151080c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
151180c49c7eSIan Munsie 
15126f963ec2SRyan Grimm 	return of_node_get(hose->dn);
151380c49c7eSIan Munsie }
15146f963ec2SRyan Grimm EXPORT_SYMBOL(pnv_pci_get_phb_node);
151580c49c7eSIan Munsie 
15161212aa1cSRyan Grimm int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
151780c49c7eSIan Munsie {
151880c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
151980c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
152080c49c7eSIan Munsie 	struct pnv_ioda_pe *pe;
152180c49c7eSIan Munsie 	int rc;
152280c49c7eSIan Munsie 
152380c49c7eSIan Munsie 	pe = pnv_ioda_get_pe(dev);
152480c49c7eSIan Munsie 	if (!pe)
152580c49c7eSIan Munsie 		return -ENODEV;
152680c49c7eSIan Munsie 
152780c49c7eSIan Munsie 	pe_info(pe, "Switching PHB to CXL\n");
152880c49c7eSIan Munsie 
15291212aa1cSRyan Grimm 	rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
153080c49c7eSIan Munsie 	if (rc)
153180c49c7eSIan Munsie 		dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
153280c49c7eSIan Munsie 
153380c49c7eSIan Munsie 	return rc;
153480c49c7eSIan Munsie }
15351212aa1cSRyan Grimm EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
153680c49c7eSIan Munsie 
153780c49c7eSIan Munsie /* Find PHB for cxl dev and allocate MSI hwirqs?
153880c49c7eSIan Munsie  * Returns the absolute hardware IRQ number
153980c49c7eSIan Munsie  */
154080c49c7eSIan Munsie int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
154180c49c7eSIan Munsie {
154280c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
154380c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
154480c49c7eSIan Munsie 	int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
154580c49c7eSIan Munsie 
154680c49c7eSIan Munsie 	if (hwirq < 0) {
154780c49c7eSIan Munsie 		dev_warn(&dev->dev, "Failed to find a free MSI\n");
154880c49c7eSIan Munsie 		return -ENOSPC;
154980c49c7eSIan Munsie 	}
155080c49c7eSIan Munsie 
155180c49c7eSIan Munsie 	return phb->msi_base + hwirq;
155280c49c7eSIan Munsie }
155380c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
155480c49c7eSIan Munsie 
155580c49c7eSIan Munsie void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
155680c49c7eSIan Munsie {
155780c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
155880c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
155980c49c7eSIan Munsie 
156080c49c7eSIan Munsie 	msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
156180c49c7eSIan Munsie }
156280c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
156380c49c7eSIan Munsie 
156480c49c7eSIan Munsie void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
156580c49c7eSIan Munsie 				  struct pci_dev *dev)
156680c49c7eSIan Munsie {
156780c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
156880c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
156980c49c7eSIan Munsie 	int i, hwirq;
157080c49c7eSIan Munsie 
157180c49c7eSIan Munsie 	for (i = 1; i < CXL_IRQ_RANGES; i++) {
157280c49c7eSIan Munsie 		if (!irqs->range[i])
157380c49c7eSIan Munsie 			continue;
157480c49c7eSIan Munsie 		pr_devel("cxl release irq range 0x%x: offset: 0x%lx  limit: %ld\n",
157580c49c7eSIan Munsie 			 i, irqs->offset[i],
157680c49c7eSIan Munsie 			 irqs->range[i]);
157780c49c7eSIan Munsie 		hwirq = irqs->offset[i] - phb->msi_base;
157880c49c7eSIan Munsie 		msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
157980c49c7eSIan Munsie 				       irqs->range[i]);
158080c49c7eSIan Munsie 	}
158180c49c7eSIan Munsie }
158280c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
158380c49c7eSIan Munsie 
158480c49c7eSIan Munsie int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
158580c49c7eSIan Munsie 			       struct pci_dev *dev, int num)
158680c49c7eSIan Munsie {
158780c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
158880c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
158980c49c7eSIan Munsie 	int i, hwirq, try;
159080c49c7eSIan Munsie 
159180c49c7eSIan Munsie 	memset(irqs, 0, sizeof(struct cxl_irq_ranges));
159280c49c7eSIan Munsie 
159380c49c7eSIan Munsie 	/* 0 is reserved for the multiplexed PSL DSI interrupt */
159480c49c7eSIan Munsie 	for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
159580c49c7eSIan Munsie 		try = num;
159680c49c7eSIan Munsie 		while (try) {
159780c49c7eSIan Munsie 			hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
159880c49c7eSIan Munsie 			if (hwirq >= 0)
159980c49c7eSIan Munsie 				break;
160080c49c7eSIan Munsie 			try /= 2;
160180c49c7eSIan Munsie 		}
160280c49c7eSIan Munsie 		if (!try)
160380c49c7eSIan Munsie 			goto fail;
160480c49c7eSIan Munsie 
160580c49c7eSIan Munsie 		irqs->offset[i] = phb->msi_base + hwirq;
160680c49c7eSIan Munsie 		irqs->range[i] = try;
160780c49c7eSIan Munsie 		pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx  limit: %li\n",
160880c49c7eSIan Munsie 			 i, irqs->offset[i], irqs->range[i]);
160980c49c7eSIan Munsie 		num -= try;
161080c49c7eSIan Munsie 	}
161180c49c7eSIan Munsie 	if (num)
161280c49c7eSIan Munsie 		goto fail;
161380c49c7eSIan Munsie 
161480c49c7eSIan Munsie 	return 0;
161580c49c7eSIan Munsie fail:
161680c49c7eSIan Munsie 	pnv_cxl_release_hwirq_ranges(irqs, dev);
161780c49c7eSIan Munsie 	return -ENOSPC;
161880c49c7eSIan Munsie }
161980c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
162080c49c7eSIan Munsie 
162180c49c7eSIan Munsie int pnv_cxl_get_irq_count(struct pci_dev *dev)
162280c49c7eSIan Munsie {
162380c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
162480c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
162580c49c7eSIan Munsie 
162680c49c7eSIan Munsie 	return phb->msi_bmp.irq_count;
162780c49c7eSIan Munsie }
162880c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_get_irq_count);
162980c49c7eSIan Munsie 
163080c49c7eSIan Munsie int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
163180c49c7eSIan Munsie 			   unsigned int virq)
163280c49c7eSIan Munsie {
163380c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
163480c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
163580c49c7eSIan Munsie 	unsigned int xive_num = hwirq - phb->msi_base;
163680c49c7eSIan Munsie 	struct pnv_ioda_pe *pe;
163780c49c7eSIan Munsie 	int rc;
163880c49c7eSIan Munsie 
163980c49c7eSIan Munsie 	if (!(pe = pnv_ioda_get_pe(dev)))
164080c49c7eSIan Munsie 		return -ENODEV;
164180c49c7eSIan Munsie 
164280c49c7eSIan Munsie 	/* Assign XIVE to PE */
164380c49c7eSIan Munsie 	rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
164480c49c7eSIan Munsie 	if (rc) {
164580c49c7eSIan Munsie 		pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
164680c49c7eSIan Munsie 			"hwirq 0x%x XIVE 0x%x PE\n",
164780c49c7eSIan Munsie 			pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
164880c49c7eSIan Munsie 		return -EIO;
164980c49c7eSIan Munsie 	}
165080c49c7eSIan Munsie 	set_msi_irq_chip(phb, virq);
165180c49c7eSIan Munsie 
165280c49c7eSIan Munsie 	return 0;
165380c49c7eSIan Munsie }
165480c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
165580c49c7eSIan Munsie #endif
165680c49c7eSIan Munsie 
1657184cd4a3SBenjamin Herrenschmidt static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
1658137436c9SGavin Shan 				  unsigned int hwirq, unsigned int virq,
1659137436c9SGavin Shan 				  unsigned int is_64, struct msi_msg *msg)
1660184cd4a3SBenjamin Herrenschmidt {
1661184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1662184cd4a3SBenjamin Herrenschmidt 	unsigned int xive_num = hwirq - phb->msi_base;
16633a1a4661SBenjamin Herrenschmidt 	__be32 data;
1664184cd4a3SBenjamin Herrenschmidt 	int rc;
1665184cd4a3SBenjamin Herrenschmidt 
1666184cd4a3SBenjamin Herrenschmidt 	/* No PE assigned ? bail out ... no MSI for you ! */
1667184cd4a3SBenjamin Herrenschmidt 	if (pe == NULL)
1668184cd4a3SBenjamin Herrenschmidt 		return -ENXIO;
1669184cd4a3SBenjamin Herrenschmidt 
1670184cd4a3SBenjamin Herrenschmidt 	/* Check if we have an MVE */
1671184cd4a3SBenjamin Herrenschmidt 	if (pe->mve_number < 0)
1672184cd4a3SBenjamin Herrenschmidt 		return -ENXIO;
1673184cd4a3SBenjamin Herrenschmidt 
1674b72c1f65SBenjamin Herrenschmidt 	/* Force 32-bit MSI on some broken devices */
167536074381SBenjamin Herrenschmidt 	if (dev->no_64bit_msi)
1676b72c1f65SBenjamin Herrenschmidt 		is_64 = 0;
1677b72c1f65SBenjamin Herrenschmidt 
1678184cd4a3SBenjamin Herrenschmidt 	/* Assign XIVE to PE */
1679184cd4a3SBenjamin Herrenschmidt 	rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1680184cd4a3SBenjamin Herrenschmidt 	if (rc) {
1681184cd4a3SBenjamin Herrenschmidt 		pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1682184cd4a3SBenjamin Herrenschmidt 			pci_name(dev), rc, xive_num);
1683184cd4a3SBenjamin Herrenschmidt 		return -EIO;
1684184cd4a3SBenjamin Herrenschmidt 	}
1685184cd4a3SBenjamin Herrenschmidt 
1686184cd4a3SBenjamin Herrenschmidt 	if (is_64) {
16873a1a4661SBenjamin Herrenschmidt 		__be64 addr64;
16883a1a4661SBenjamin Herrenschmidt 
1689184cd4a3SBenjamin Herrenschmidt 		rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1690184cd4a3SBenjamin Herrenschmidt 				     &addr64, &data);
1691184cd4a3SBenjamin Herrenschmidt 		if (rc) {
1692184cd4a3SBenjamin Herrenschmidt 			pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1693184cd4a3SBenjamin Herrenschmidt 				pci_name(dev), rc);
1694184cd4a3SBenjamin Herrenschmidt 			return -EIO;
1695184cd4a3SBenjamin Herrenschmidt 		}
16963a1a4661SBenjamin Herrenschmidt 		msg->address_hi = be64_to_cpu(addr64) >> 32;
16973a1a4661SBenjamin Herrenschmidt 		msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
1698184cd4a3SBenjamin Herrenschmidt 	} else {
16993a1a4661SBenjamin Herrenschmidt 		__be32 addr32;
17003a1a4661SBenjamin Herrenschmidt 
1701184cd4a3SBenjamin Herrenschmidt 		rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1702184cd4a3SBenjamin Herrenschmidt 				     &addr32, &data);
1703184cd4a3SBenjamin Herrenschmidt 		if (rc) {
1704184cd4a3SBenjamin Herrenschmidt 			pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1705184cd4a3SBenjamin Herrenschmidt 				pci_name(dev), rc);
1706184cd4a3SBenjamin Herrenschmidt 			return -EIO;
1707184cd4a3SBenjamin Herrenschmidt 		}
1708184cd4a3SBenjamin Herrenschmidt 		msg->address_hi = 0;
17093a1a4661SBenjamin Herrenschmidt 		msg->address_lo = be32_to_cpu(addr32);
1710184cd4a3SBenjamin Herrenschmidt 	}
17113a1a4661SBenjamin Herrenschmidt 	msg->data = be32_to_cpu(data);
1712184cd4a3SBenjamin Herrenschmidt 
1713fd9a1c26SIan Munsie 	set_msi_irq_chip(phb, virq);
1714137436c9SGavin Shan 
1715184cd4a3SBenjamin Herrenschmidt 	pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1716184cd4a3SBenjamin Herrenschmidt 		 " address=%x_%08x data=%x PE# %d\n",
1717184cd4a3SBenjamin Herrenschmidt 		 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
1718184cd4a3SBenjamin Herrenschmidt 		 msg->address_hi, msg->address_lo, data, pe->pe_number);
1719184cd4a3SBenjamin Herrenschmidt 
1720184cd4a3SBenjamin Herrenschmidt 	return 0;
1721184cd4a3SBenjamin Herrenschmidt }
1722184cd4a3SBenjamin Herrenschmidt 
1723184cd4a3SBenjamin Herrenschmidt static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1724184cd4a3SBenjamin Herrenschmidt {
1725fb1b55d6SGavin Shan 	unsigned int count;
1726184cd4a3SBenjamin Herrenschmidt 	const __be32 *prop = of_get_property(phb->hose->dn,
1727184cd4a3SBenjamin Herrenschmidt 					     "ibm,opal-msi-ranges", NULL);
1728184cd4a3SBenjamin Herrenschmidt 	if (!prop) {
1729184cd4a3SBenjamin Herrenschmidt 		/* BML Fallback */
1730184cd4a3SBenjamin Herrenschmidt 		prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1731184cd4a3SBenjamin Herrenschmidt 	}
1732184cd4a3SBenjamin Herrenschmidt 	if (!prop)
1733184cd4a3SBenjamin Herrenschmidt 		return;
1734184cd4a3SBenjamin Herrenschmidt 
1735184cd4a3SBenjamin Herrenschmidt 	phb->msi_base = be32_to_cpup(prop);
1736fb1b55d6SGavin Shan 	count = be32_to_cpup(prop + 1);
1737fb1b55d6SGavin Shan 	if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
1738184cd4a3SBenjamin Herrenschmidt 		pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1739184cd4a3SBenjamin Herrenschmidt 		       phb->hose->global_number);
1740184cd4a3SBenjamin Herrenschmidt 		return;
1741184cd4a3SBenjamin Herrenschmidt 	}
1742fb1b55d6SGavin Shan 
1743184cd4a3SBenjamin Herrenschmidt 	phb->msi_setup = pnv_pci_ioda_msi_setup;
1744184cd4a3SBenjamin Herrenschmidt 	phb->msi32_support = 1;
1745184cd4a3SBenjamin Herrenschmidt 	pr_info("  Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
1746fb1b55d6SGavin Shan 		count, phb->msi_base);
1747184cd4a3SBenjamin Herrenschmidt }
1748184cd4a3SBenjamin Herrenschmidt #else
1749184cd4a3SBenjamin Herrenschmidt static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1750184cd4a3SBenjamin Herrenschmidt #endif /* CONFIG_PCI_MSI */
1751184cd4a3SBenjamin Herrenschmidt 
17526e628c7dSWei Yang #ifdef CONFIG_PCI_IOV
17536e628c7dSWei Yang static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
17546e628c7dSWei Yang {
17556e628c7dSWei Yang 	struct pci_controller *hose;
17566e628c7dSWei Yang 	struct pnv_phb *phb;
17576e628c7dSWei Yang 	struct resource *res;
17586e628c7dSWei Yang 	int i;
17596e628c7dSWei Yang 	resource_size_t size;
17606e628c7dSWei Yang 	struct pci_dn *pdn;
17616e628c7dSWei Yang 
17626e628c7dSWei Yang 	if (!pdev->is_physfn || pdev->is_added)
17636e628c7dSWei Yang 		return;
17646e628c7dSWei Yang 
17656e628c7dSWei Yang 	hose = pci_bus_to_host(pdev->bus);
17666e628c7dSWei Yang 	phb = hose->private_data;
17676e628c7dSWei Yang 
17686e628c7dSWei Yang 	pdn = pci_get_pdn(pdev);
17696e628c7dSWei Yang 	pdn->vfs_expanded = 0;
17706e628c7dSWei Yang 
17716e628c7dSWei Yang 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
17726e628c7dSWei Yang 		res = &pdev->resource[i + PCI_IOV_RESOURCES];
17736e628c7dSWei Yang 		if (!res->flags || res->parent)
17746e628c7dSWei Yang 			continue;
17756e628c7dSWei Yang 		if (!pnv_pci_is_mem_pref_64(res->flags)) {
17766e628c7dSWei Yang 			dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
17776e628c7dSWei Yang 				 i, res);
17786e628c7dSWei Yang 			continue;
17796e628c7dSWei Yang 		}
17806e628c7dSWei Yang 
17816e628c7dSWei Yang 		dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
17826e628c7dSWei Yang 		size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
17836e628c7dSWei Yang 		res->end = res->start + size * phb->ioda.total_pe - 1;
17846e628c7dSWei Yang 		dev_dbg(&pdev->dev, "                       %pR\n", res);
17856e628c7dSWei Yang 		dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
17866e628c7dSWei Yang 				i, res, phb->ioda.total_pe);
17876e628c7dSWei Yang 	}
17886e628c7dSWei Yang 	pdn->vfs_expanded = phb->ioda.total_pe;
17896e628c7dSWei Yang }
17906e628c7dSWei Yang #endif /* CONFIG_PCI_IOV */
17916e628c7dSWei Yang 
179211685becSGavin Shan /*
179311685becSGavin Shan  * This function is supposed to be called on basis of PE from top
179411685becSGavin Shan  * to bottom style. So the the I/O or MMIO segment assigned to
179511685becSGavin Shan  * parent PE could be overrided by its child PEs if necessary.
179611685becSGavin Shan  */
1797cad5cef6SGreg Kroah-Hartman static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
179811685becSGavin Shan 				  struct pnv_ioda_pe *pe)
179911685becSGavin Shan {
180011685becSGavin Shan 	struct pnv_phb *phb = hose->private_data;
180111685becSGavin Shan 	struct pci_bus_region region;
180211685becSGavin Shan 	struct resource *res;
180311685becSGavin Shan 	int i, index;
180411685becSGavin Shan 	int rc;
180511685becSGavin Shan 
180611685becSGavin Shan 	/*
180711685becSGavin Shan 	 * NOTE: We only care PCI bus based PE for now. For PCI
180811685becSGavin Shan 	 * device based PE, for example SRIOV sensitive VF should
180911685becSGavin Shan 	 * be figured out later.
181011685becSGavin Shan 	 */
181111685becSGavin Shan 	BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
181211685becSGavin Shan 
181311685becSGavin Shan 	pci_bus_for_each_resource(pe->pbus, res, i) {
181411685becSGavin Shan 		if (!res || !res->flags ||
181511685becSGavin Shan 		    res->start > res->end)
181611685becSGavin Shan 			continue;
181711685becSGavin Shan 
181811685becSGavin Shan 		if (res->flags & IORESOURCE_IO) {
181911685becSGavin Shan 			region.start = res->start - phb->ioda.io_pci_base;
182011685becSGavin Shan 			region.end   = res->end - phb->ioda.io_pci_base;
182111685becSGavin Shan 			index = region.start / phb->ioda.io_segsize;
182211685becSGavin Shan 
182311685becSGavin Shan 			while (index < phb->ioda.total_pe &&
182411685becSGavin Shan 			       region.start <= region.end) {
182511685becSGavin Shan 				phb->ioda.io_segmap[index] = pe->pe_number;
182611685becSGavin Shan 				rc = opal_pci_map_pe_mmio_window(phb->opal_id,
182711685becSGavin Shan 					pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
182811685becSGavin Shan 				if (rc != OPAL_SUCCESS) {
182911685becSGavin Shan 					pr_err("%s: OPAL error %d when mapping IO "
183011685becSGavin Shan 					       "segment #%d to PE#%d\n",
183111685becSGavin Shan 					       __func__, rc, index, pe->pe_number);
183211685becSGavin Shan 					break;
183311685becSGavin Shan 				}
183411685becSGavin Shan 
183511685becSGavin Shan 				region.start += phb->ioda.io_segsize;
183611685becSGavin Shan 				index++;
183711685becSGavin Shan 			}
183811685becSGavin Shan 		} else if (res->flags & IORESOURCE_MEM) {
183911685becSGavin Shan 			region.start = res->start -
18403fd47f06SBenjamin Herrenschmidt 				       hose->mem_offset[0] -
184111685becSGavin Shan 				       phb->ioda.m32_pci_base;
184211685becSGavin Shan 			region.end   = res->end -
18433fd47f06SBenjamin Herrenschmidt 				       hose->mem_offset[0] -
184411685becSGavin Shan 				       phb->ioda.m32_pci_base;
184511685becSGavin Shan 			index = region.start / phb->ioda.m32_segsize;
184611685becSGavin Shan 
184711685becSGavin Shan 			while (index < phb->ioda.total_pe &&
184811685becSGavin Shan 			       region.start <= region.end) {
184911685becSGavin Shan 				phb->ioda.m32_segmap[index] = pe->pe_number;
185011685becSGavin Shan 				rc = opal_pci_map_pe_mmio_window(phb->opal_id,
185111685becSGavin Shan 					pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
185211685becSGavin Shan 				if (rc != OPAL_SUCCESS) {
185311685becSGavin Shan 					pr_err("%s: OPAL error %d when mapping M32 "
185411685becSGavin Shan 					       "segment#%d to PE#%d",
185511685becSGavin Shan 					       __func__, rc, index, pe->pe_number);
185611685becSGavin Shan 					break;
185711685becSGavin Shan 				}
185811685becSGavin Shan 
185911685becSGavin Shan 				region.start += phb->ioda.m32_segsize;
186011685becSGavin Shan 				index++;
186111685becSGavin Shan 			}
186211685becSGavin Shan 		}
186311685becSGavin Shan 	}
186411685becSGavin Shan }
186511685becSGavin Shan 
1866cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_setup_seg(void)
186711685becSGavin Shan {
186811685becSGavin Shan 	struct pci_controller *tmp, *hose;
186911685becSGavin Shan 	struct pnv_phb *phb;
187011685becSGavin Shan 	struct pnv_ioda_pe *pe;
187111685becSGavin Shan 
187211685becSGavin Shan 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
187311685becSGavin Shan 		phb = hose->private_data;
187411685becSGavin Shan 		list_for_each_entry(pe, &phb->ioda.pe_list, list) {
187511685becSGavin Shan 			pnv_ioda_setup_pe_seg(hose, pe);
187611685becSGavin Shan 		}
187711685becSGavin Shan 	}
187811685becSGavin Shan }
187911685becSGavin Shan 
1880cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_setup_DMA(void)
188113395c48SGavin Shan {
188213395c48SGavin Shan 	struct pci_controller *hose, *tmp;
1883db1266c8SGavin Shan 	struct pnv_phb *phb;
188413395c48SGavin Shan 
188513395c48SGavin Shan 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
188613395c48SGavin Shan 		pnv_ioda_setup_dma(hose->private_data);
1887db1266c8SGavin Shan 
1888db1266c8SGavin Shan 		/* Mark the PHB initialization done */
1889db1266c8SGavin Shan 		phb = hose->private_data;
1890db1266c8SGavin Shan 		phb->initialized = 1;
189113395c48SGavin Shan 	}
189213395c48SGavin Shan }
189313395c48SGavin Shan 
189437c367f2SGavin Shan static void pnv_pci_ioda_create_dbgfs(void)
189537c367f2SGavin Shan {
189637c367f2SGavin Shan #ifdef CONFIG_DEBUG_FS
189737c367f2SGavin Shan 	struct pci_controller *hose, *tmp;
189837c367f2SGavin Shan 	struct pnv_phb *phb;
189937c367f2SGavin Shan 	char name[16];
190037c367f2SGavin Shan 
190137c367f2SGavin Shan 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
190237c367f2SGavin Shan 		phb = hose->private_data;
190337c367f2SGavin Shan 
190437c367f2SGavin Shan 		sprintf(name, "PCI%04x", hose->global_number);
190537c367f2SGavin Shan 		phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
190637c367f2SGavin Shan 		if (!phb->dbgfs)
190737c367f2SGavin Shan 			pr_warning("%s: Error on creating debugfs on PHB#%x\n",
190837c367f2SGavin Shan 				__func__, hose->global_number);
190937c367f2SGavin Shan 	}
191037c367f2SGavin Shan #endif /* CONFIG_DEBUG_FS */
191137c367f2SGavin Shan }
191237c367f2SGavin Shan 
1913cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_fixup(void)
1914fb446ad0SGavin Shan {
1915fb446ad0SGavin Shan 	pnv_pci_ioda_setup_PEs();
191611685becSGavin Shan 	pnv_pci_ioda_setup_seg();
191713395c48SGavin Shan 	pnv_pci_ioda_setup_DMA();
1918e9cc17d4SGavin Shan 
191937c367f2SGavin Shan 	pnv_pci_ioda_create_dbgfs();
192037c367f2SGavin Shan 
1921e9cc17d4SGavin Shan #ifdef CONFIG_EEH
1922e9cc17d4SGavin Shan 	eeh_init();
1923dadcd6d6SMike Qiu 	eeh_addr_cache_build();
1924e9cc17d4SGavin Shan #endif
1925fb446ad0SGavin Shan }
1926fb446ad0SGavin Shan 
1927271fd03aSGavin Shan /*
1928271fd03aSGavin Shan  * Returns the alignment for I/O or memory windows for P2P
1929271fd03aSGavin Shan  * bridges. That actually depends on how PEs are segmented.
1930271fd03aSGavin Shan  * For now, we return I/O or M32 segment size for PE sensitive
1931271fd03aSGavin Shan  * P2P bridges. Otherwise, the default values (4KiB for I/O,
1932271fd03aSGavin Shan  * 1MiB for memory) will be returned.
1933271fd03aSGavin Shan  *
1934271fd03aSGavin Shan  * The current PCI bus might be put into one PE, which was
1935271fd03aSGavin Shan  * create against the parent PCI bridge. For that case, we
1936271fd03aSGavin Shan  * needn't enlarge the alignment so that we can save some
1937271fd03aSGavin Shan  * resources.
1938271fd03aSGavin Shan  */
1939271fd03aSGavin Shan static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
1940271fd03aSGavin Shan 						unsigned long type)
1941271fd03aSGavin Shan {
1942271fd03aSGavin Shan 	struct pci_dev *bridge;
1943271fd03aSGavin Shan 	struct pci_controller *hose = pci_bus_to_host(bus);
1944271fd03aSGavin Shan 	struct pnv_phb *phb = hose->private_data;
1945271fd03aSGavin Shan 	int num_pci_bridges = 0;
1946271fd03aSGavin Shan 
1947271fd03aSGavin Shan 	bridge = bus->self;
1948271fd03aSGavin Shan 	while (bridge) {
1949271fd03aSGavin Shan 		if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
1950271fd03aSGavin Shan 			num_pci_bridges++;
1951271fd03aSGavin Shan 			if (num_pci_bridges >= 2)
1952271fd03aSGavin Shan 				return 1;
1953271fd03aSGavin Shan 		}
1954271fd03aSGavin Shan 
1955271fd03aSGavin Shan 		bridge = bridge->bus->self;
1956271fd03aSGavin Shan 	}
1957271fd03aSGavin Shan 
1958262af557SGuo Chao 	/* We fail back to M32 if M64 isn't supported */
1959262af557SGuo Chao 	if (phb->ioda.m64_segsize &&
1960262af557SGuo Chao 	    pnv_pci_is_mem_pref_64(type))
1961262af557SGuo Chao 		return phb->ioda.m64_segsize;
1962271fd03aSGavin Shan 	if (type & IORESOURCE_MEM)
1963271fd03aSGavin Shan 		return phb->ioda.m32_segsize;
1964271fd03aSGavin Shan 
1965271fd03aSGavin Shan 	return phb->ioda.io_segsize;
1966271fd03aSGavin Shan }
1967271fd03aSGavin Shan 
19685350ab3fSWei Yang #ifdef CONFIG_PCI_IOV
19695350ab3fSWei Yang static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
19705350ab3fSWei Yang 						      int resno)
19715350ab3fSWei Yang {
19725350ab3fSWei Yang 	struct pci_dn *pdn = pci_get_pdn(pdev);
19735350ab3fSWei Yang 	resource_size_t align, iov_align;
19745350ab3fSWei Yang 
19755350ab3fSWei Yang 	iov_align = resource_size(&pdev->resource[resno]);
19765350ab3fSWei Yang 	if (iov_align)
19775350ab3fSWei Yang 		return iov_align;
19785350ab3fSWei Yang 
19795350ab3fSWei Yang 	align = pci_iov_resource_size(pdev, resno);
19805350ab3fSWei Yang 	if (pdn->vfs_expanded)
19815350ab3fSWei Yang 		return pdn->vfs_expanded * align;
19825350ab3fSWei Yang 
19835350ab3fSWei Yang 	return align;
19845350ab3fSWei Yang }
19855350ab3fSWei Yang #endif /* CONFIG_PCI_IOV */
19865350ab3fSWei Yang 
1987184cd4a3SBenjamin Herrenschmidt /* Prevent enabling devices for which we couldn't properly
1988184cd4a3SBenjamin Herrenschmidt  * assign a PE
1989184cd4a3SBenjamin Herrenschmidt  */
1990cad5cef6SGreg Kroah-Hartman static int pnv_pci_enable_device_hook(struct pci_dev *dev)
1991184cd4a3SBenjamin Herrenschmidt {
1992db1266c8SGavin Shan 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
1993db1266c8SGavin Shan 	struct pnv_phb *phb = hose->private_data;
1994db1266c8SGavin Shan 	struct pci_dn *pdn;
1995184cd4a3SBenjamin Herrenschmidt 
1996db1266c8SGavin Shan 	/* The function is probably called while the PEs have
1997db1266c8SGavin Shan 	 * not be created yet. For example, resource reassignment
1998db1266c8SGavin Shan 	 * during PCI probe period. We just skip the check if
1999db1266c8SGavin Shan 	 * PEs isn't ready.
2000db1266c8SGavin Shan 	 */
2001db1266c8SGavin Shan 	if (!phb->initialized)
2002db1266c8SGavin Shan 		return 0;
2003db1266c8SGavin Shan 
2004b72c1f65SBenjamin Herrenschmidt 	pdn = pci_get_pdn(dev);
2005184cd4a3SBenjamin Herrenschmidt 	if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2006184cd4a3SBenjamin Herrenschmidt 		return -EINVAL;
2007db1266c8SGavin Shan 
2008184cd4a3SBenjamin Herrenschmidt 	return 0;
2009184cd4a3SBenjamin Herrenschmidt }
2010184cd4a3SBenjamin Herrenschmidt 
2011184cd4a3SBenjamin Herrenschmidt static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
2012184cd4a3SBenjamin Herrenschmidt 			       u32 devfn)
2013184cd4a3SBenjamin Herrenschmidt {
2014184cd4a3SBenjamin Herrenschmidt 	return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
2015184cd4a3SBenjamin Herrenschmidt }
2016184cd4a3SBenjamin Herrenschmidt 
201773ed148aSBenjamin Herrenschmidt static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
201873ed148aSBenjamin Herrenschmidt {
2019d1a85eeeSGavin Shan 	opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
202073ed148aSBenjamin Herrenschmidt 		       OPAL_ASSERT_RESET);
202173ed148aSBenjamin Herrenschmidt }
202273ed148aSBenjamin Herrenschmidt 
2023e51df2c1SAnton Blanchard static void __init pnv_pci_init_ioda_phb(struct device_node *np,
2024e9cc17d4SGavin Shan 					 u64 hub_id, int ioda_type)
2025184cd4a3SBenjamin Herrenschmidt {
2026184cd4a3SBenjamin Herrenschmidt 	struct pci_controller *hose;
2027184cd4a3SBenjamin Herrenschmidt 	struct pnv_phb *phb;
20288184616fSGavin Shan 	unsigned long size, m32map_off, pemap_off, iomap_off = 0;
2029c681b93cSAlistair Popple 	const __be64 *prop64;
20303a1a4661SBenjamin Herrenschmidt 	const __be32 *prop32;
2031f1b7cc3eSGavin Shan 	int len;
2032184cd4a3SBenjamin Herrenschmidt 	u64 phb_id;
2033184cd4a3SBenjamin Herrenschmidt 	void *aux;
2034184cd4a3SBenjamin Herrenschmidt 	long rc;
2035184cd4a3SBenjamin Herrenschmidt 
2036aa0c033fSGavin Shan 	pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
2037184cd4a3SBenjamin Herrenschmidt 
2038184cd4a3SBenjamin Herrenschmidt 	prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
2039184cd4a3SBenjamin Herrenschmidt 	if (!prop64) {
2040184cd4a3SBenjamin Herrenschmidt 		pr_err("  Missing \"ibm,opal-phbid\" property !\n");
2041184cd4a3SBenjamin Herrenschmidt 		return;
2042184cd4a3SBenjamin Herrenschmidt 	}
2043184cd4a3SBenjamin Herrenschmidt 	phb_id = be64_to_cpup(prop64);
2044184cd4a3SBenjamin Herrenschmidt 	pr_debug("  PHB-ID  : 0x%016llx\n", phb_id);
2045184cd4a3SBenjamin Herrenschmidt 
2046e39f223fSMichael Ellerman 	phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
204758d714ecSGavin Shan 
204858d714ecSGavin Shan 	/* Allocate PCI controller */
2049184cd4a3SBenjamin Herrenschmidt 	phb->hose = hose = pcibios_alloc_controller(np);
205058d714ecSGavin Shan 	if (!phb->hose) {
205158d714ecSGavin Shan 		pr_err("  Can't allocate PCI controller for %s\n",
2052184cd4a3SBenjamin Herrenschmidt 		       np->full_name);
2053e39f223fSMichael Ellerman 		memblock_free(__pa(phb), sizeof(struct pnv_phb));
2054184cd4a3SBenjamin Herrenschmidt 		return;
2055184cd4a3SBenjamin Herrenschmidt 	}
2056184cd4a3SBenjamin Herrenschmidt 
2057184cd4a3SBenjamin Herrenschmidt 	spin_lock_init(&phb->lock);
2058f1b7cc3eSGavin Shan 	prop32 = of_get_property(np, "bus-range", &len);
2059f1b7cc3eSGavin Shan 	if (prop32 && len == 8) {
20603a1a4661SBenjamin Herrenschmidt 		hose->first_busno = be32_to_cpu(prop32[0]);
20613a1a4661SBenjamin Herrenschmidt 		hose->last_busno = be32_to_cpu(prop32[1]);
2062f1b7cc3eSGavin Shan 	} else {
2063f1b7cc3eSGavin Shan 		pr_warn("  Broken <bus-range> on %s\n", np->full_name);
2064184cd4a3SBenjamin Herrenschmidt 		hose->first_busno = 0;
2065184cd4a3SBenjamin Herrenschmidt 		hose->last_busno = 0xff;
2066f1b7cc3eSGavin Shan 	}
2067184cd4a3SBenjamin Herrenschmidt 	hose->private_data = phb;
2068e9cc17d4SGavin Shan 	phb->hub_id = hub_id;
2069184cd4a3SBenjamin Herrenschmidt 	phb->opal_id = phb_id;
2070aa0c033fSGavin Shan 	phb->type = ioda_type;
2071184cd4a3SBenjamin Herrenschmidt 
2072cee72d5bSBenjamin Herrenschmidt 	/* Detect specific models for error handling */
2073cee72d5bSBenjamin Herrenschmidt 	if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
2074cee72d5bSBenjamin Herrenschmidt 		phb->model = PNV_PHB_MODEL_P7IOC;
2075f3d40c25SBenjamin Herrenschmidt 	else if (of_device_is_compatible(np, "ibm,power8-pciex"))
2076aa0c033fSGavin Shan 		phb->model = PNV_PHB_MODEL_PHB3;
2077cee72d5bSBenjamin Herrenschmidt 	else
2078cee72d5bSBenjamin Herrenschmidt 		phb->model = PNV_PHB_MODEL_UNKNOWN;
2079cee72d5bSBenjamin Herrenschmidt 
2080aa0c033fSGavin Shan 	/* Parse 32-bit and IO ranges (if any) */
20812f1ec02eSGavin Shan 	pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
2082184cd4a3SBenjamin Herrenschmidt 
2083aa0c033fSGavin Shan 	/* Get registers */
2084184cd4a3SBenjamin Herrenschmidt 	phb->regs = of_iomap(np, 0);
2085184cd4a3SBenjamin Herrenschmidt 	if (phb->regs == NULL)
2086184cd4a3SBenjamin Herrenschmidt 		pr_err("  Failed to map registers !\n");
2087184cd4a3SBenjamin Herrenschmidt 
2088184cd4a3SBenjamin Herrenschmidt 	/* Initialize more IODA stuff */
2089aa0c033fSGavin Shan 	phb->ioda.total_pe = 1;
209036954dc7SGavin Shan 	prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
209136954dc7SGavin Shan 	if (prop32)
20923a1a4661SBenjamin Herrenschmidt 		phb->ioda.total_pe = be32_to_cpup(prop32);
209336954dc7SGavin Shan 	prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
209436954dc7SGavin Shan 	if (prop32)
209536954dc7SGavin Shan 		phb->ioda.reserved_pe = be32_to_cpup(prop32);
2096262af557SGuo Chao 
2097262af557SGuo Chao 	/* Parse 64-bit MMIO range */
2098262af557SGuo Chao 	pnv_ioda_parse_m64_window(phb);
2099262af557SGuo Chao 
2100184cd4a3SBenjamin Herrenschmidt 	phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
2101aa0c033fSGavin Shan 	/* FW Has already off top 64k of M32 space (MSI space) */
2102184cd4a3SBenjamin Herrenschmidt 	phb->ioda.m32_size += 0x10000;
2103184cd4a3SBenjamin Herrenschmidt 
2104184cd4a3SBenjamin Herrenschmidt 	phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
21053fd47f06SBenjamin Herrenschmidt 	phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
2106184cd4a3SBenjamin Herrenschmidt 	phb->ioda.io_size = hose->pci_io_size;
2107184cd4a3SBenjamin Herrenschmidt 	phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
2108184cd4a3SBenjamin Herrenschmidt 	phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
2109184cd4a3SBenjamin Herrenschmidt 
2110c35d2a8cSGavin Shan 	/* Allocate aux data & arrays. We don't have IO ports on PHB3 */
2111184cd4a3SBenjamin Herrenschmidt 	size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
2112184cd4a3SBenjamin Herrenschmidt 	m32map_off = size;
2113e47747f4SGavin Shan 	size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
2114c35d2a8cSGavin Shan 	if (phb->type == PNV_PHB_IODA1) {
2115c35d2a8cSGavin Shan 		iomap_off = size;
2116e47747f4SGavin Shan 		size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
2117c35d2a8cSGavin Shan 	}
2118184cd4a3SBenjamin Herrenschmidt 	pemap_off = size;
2119184cd4a3SBenjamin Herrenschmidt 	size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
2120e39f223fSMichael Ellerman 	aux = memblock_virt_alloc(size, 0);
2121184cd4a3SBenjamin Herrenschmidt 	phb->ioda.pe_alloc = aux;
2122184cd4a3SBenjamin Herrenschmidt 	phb->ioda.m32_segmap = aux + m32map_off;
2123c35d2a8cSGavin Shan 	if (phb->type == PNV_PHB_IODA1)
2124184cd4a3SBenjamin Herrenschmidt 		phb->ioda.io_segmap = aux + iomap_off;
2125184cd4a3SBenjamin Herrenschmidt 	phb->ioda.pe_array = aux + pemap_off;
212636954dc7SGavin Shan 	set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
2127184cd4a3SBenjamin Herrenschmidt 
21287ebdf956SGavin Shan 	INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
2129184cd4a3SBenjamin Herrenschmidt 	INIT_LIST_HEAD(&phb->ioda.pe_list);
2130184cd4a3SBenjamin Herrenschmidt 
2131184cd4a3SBenjamin Herrenschmidt 	/* Calculate how many 32-bit TCE segments we have */
2132184cd4a3SBenjamin Herrenschmidt 	phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
2133184cd4a3SBenjamin Herrenschmidt 
2134aa0c033fSGavin Shan #if 0 /* We should really do that ... */
2135184cd4a3SBenjamin Herrenschmidt 	rc = opal_pci_set_phb_mem_window(opal->phb_id,
2136184cd4a3SBenjamin Herrenschmidt 					 window_type,
2137184cd4a3SBenjamin Herrenschmidt 					 window_num,
2138184cd4a3SBenjamin Herrenschmidt 					 starting_real_address,
2139184cd4a3SBenjamin Herrenschmidt 					 starting_pci_address,
2140184cd4a3SBenjamin Herrenschmidt 					 segment_size);
2141184cd4a3SBenjamin Herrenschmidt #endif
2142184cd4a3SBenjamin Herrenschmidt 
2143262af557SGuo Chao 	pr_info("  %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
2144262af557SGuo Chao 		phb->ioda.total_pe, phb->ioda.reserved_pe,
2145262af557SGuo Chao 		phb->ioda.m32_size, phb->ioda.m32_segsize);
2146262af557SGuo Chao 	if (phb->ioda.m64_size)
2147262af557SGuo Chao 		pr_info("                 M64: 0x%lx [segment=0x%lx]\n",
2148262af557SGuo Chao 			phb->ioda.m64_size, phb->ioda.m64_segsize);
2149262af557SGuo Chao 	if (phb->ioda.io_size)
2150262af557SGuo Chao 		pr_info("                  IO: 0x%x [segment=0x%x]\n",
2151184cd4a3SBenjamin Herrenschmidt 			phb->ioda.io_size, phb->ioda.io_segsize);
2152184cd4a3SBenjamin Herrenschmidt 
2153262af557SGuo Chao 
2154184cd4a3SBenjamin Herrenschmidt 	phb->hose->ops = &pnv_pci_ops;
215549dec922SGavin Shan 	phb->get_pe_state = pnv_ioda_get_pe_state;
215649dec922SGavin Shan 	phb->freeze_pe = pnv_ioda_freeze_pe;
215749dec922SGavin Shan 	phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
2158184cd4a3SBenjamin Herrenschmidt 
2159184cd4a3SBenjamin Herrenschmidt 	/* Setup RID -> PE mapping function */
2160184cd4a3SBenjamin Herrenschmidt 	phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
2161184cd4a3SBenjamin Herrenschmidt 
2162184cd4a3SBenjamin Herrenschmidt 	/* Setup TCEs */
2163184cd4a3SBenjamin Herrenschmidt 	phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
2164cd15b048SBenjamin Herrenschmidt 	phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
2165fe7e85c6SGavin Shan 	phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
2166184cd4a3SBenjamin Herrenschmidt 
216773ed148aSBenjamin Herrenschmidt 	/* Setup shutdown function for kexec */
216873ed148aSBenjamin Herrenschmidt 	phb->shutdown = pnv_pci_ioda_shutdown;
216973ed148aSBenjamin Herrenschmidt 
2170184cd4a3SBenjamin Herrenschmidt 	/* Setup MSI support */
2171184cd4a3SBenjamin Herrenschmidt 	pnv_pci_init_ioda_msis(phb);
2172184cd4a3SBenjamin Herrenschmidt 
2173c40a4210SGavin Shan 	/*
2174c40a4210SGavin Shan 	 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
2175c40a4210SGavin Shan 	 * to let the PCI core do resource assignment. It's supposed
2176c40a4210SGavin Shan 	 * that the PCI core will do correct I/O and MMIO alignment
2177c40a4210SGavin Shan 	 * for the P2P bridge bars so that each PCI bus (excluding
2178c40a4210SGavin Shan 	 * the child P2P bridges) can form individual PE.
2179184cd4a3SBenjamin Herrenschmidt 	 */
2180fb446ad0SGavin Shan 	ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
2181184cd4a3SBenjamin Herrenschmidt 	ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
2182271fd03aSGavin Shan 	ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
2183d92a208dSGavin Shan 	ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
21846e628c7dSWei Yang #ifdef CONFIG_PCI_IOV
21856e628c7dSWei Yang 	ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
21865350ab3fSWei Yang 	ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
21876e628c7dSWei Yang #endif /* CONFIG_PCI_IOV */
2188c40a4210SGavin Shan 	pci_add_flags(PCI_REASSIGN_ALL_RSRC);
2189184cd4a3SBenjamin Herrenschmidt 
2190184cd4a3SBenjamin Herrenschmidt 	/* Reset IODA tables to a clean state */
2191d1a85eeeSGavin Shan 	rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
2192184cd4a3SBenjamin Herrenschmidt 	if (rc)
2193f11fe552SBenjamin Herrenschmidt 		pr_warning("  OPAL Error %ld performing IODA table reset !\n", rc);
2194361f2a2aSGavin Shan 
2195361f2a2aSGavin Shan 	/* If we're running in kdump kerenl, the previous kerenl never
2196361f2a2aSGavin Shan 	 * shutdown PCI devices correctly. We already got IODA table
2197361f2a2aSGavin Shan 	 * cleaned out. So we have to issue PHB reset to stop all PCI
2198361f2a2aSGavin Shan 	 * transactions from previous kerenl.
2199361f2a2aSGavin Shan 	 */
2200361f2a2aSGavin Shan 	if (is_kdump_kernel()) {
2201361f2a2aSGavin Shan 		pr_info("  Issue PHB reset ...\n");
2202cadf364dSGavin Shan 		pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
2203cadf364dSGavin Shan 		pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
2204361f2a2aSGavin Shan 	}
2205262af557SGuo Chao 
22069e9e8935SGavin Shan 	/* Remove M64 resource if we can't configure it successfully */
22079e9e8935SGavin Shan 	if (!phb->init_m64 || phb->init_m64(phb))
2208262af557SGuo Chao 		hose->mem_resources[1].flags = 0;
2209184cd4a3SBenjamin Herrenschmidt }
2210184cd4a3SBenjamin Herrenschmidt 
221167975005SBjorn Helgaas void __init pnv_pci_init_ioda2_phb(struct device_node *np)
2212aa0c033fSGavin Shan {
2213e9cc17d4SGavin Shan 	pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
2214aa0c033fSGavin Shan }
2215aa0c033fSGavin Shan 
2216184cd4a3SBenjamin Herrenschmidt void __init pnv_pci_init_ioda_hub(struct device_node *np)
2217184cd4a3SBenjamin Herrenschmidt {
2218184cd4a3SBenjamin Herrenschmidt 	struct device_node *phbn;
2219c681b93cSAlistair Popple 	const __be64 *prop64;
2220184cd4a3SBenjamin Herrenschmidt 	u64 hub_id;
2221184cd4a3SBenjamin Herrenschmidt 
2222184cd4a3SBenjamin Herrenschmidt 	pr_info("Probing IODA IO-Hub %s\n", np->full_name);
2223184cd4a3SBenjamin Herrenschmidt 
2224184cd4a3SBenjamin Herrenschmidt 	prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
2225184cd4a3SBenjamin Herrenschmidt 	if (!prop64) {
2226184cd4a3SBenjamin Herrenschmidt 		pr_err(" Missing \"ibm,opal-hubid\" property !\n");
2227184cd4a3SBenjamin Herrenschmidt 		return;
2228184cd4a3SBenjamin Herrenschmidt 	}
2229184cd4a3SBenjamin Herrenschmidt 	hub_id = be64_to_cpup(prop64);
2230184cd4a3SBenjamin Herrenschmidt 	pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
2231184cd4a3SBenjamin Herrenschmidt 
2232184cd4a3SBenjamin Herrenschmidt 	/* Count child PHBs */
2233184cd4a3SBenjamin Herrenschmidt 	for_each_child_of_node(np, phbn) {
2234184cd4a3SBenjamin Herrenschmidt 		/* Look for IODA1 PHBs */
2235184cd4a3SBenjamin Herrenschmidt 		if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
2236e9cc17d4SGavin Shan 			pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
2237184cd4a3SBenjamin Herrenschmidt 	}
2238184cd4a3SBenjamin Herrenschmidt }
2239