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 
788e0a1611SAlexey Kardashevskiy /*
798e0a1611SAlexey Kardashevskiy  * stdcix is only supposed to be used in hypervisor real mode as per
808e0a1611SAlexey Kardashevskiy  * the architecture spec
818e0a1611SAlexey Kardashevskiy  */
828e0a1611SAlexey Kardashevskiy static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
838e0a1611SAlexey Kardashevskiy {
848e0a1611SAlexey Kardashevskiy 	__asm__ __volatile__("stdcix %0,0,%1"
858e0a1611SAlexey Kardashevskiy 		: : "r" (val), "r" (paddr) : "memory");
868e0a1611SAlexey Kardashevskiy }
878e0a1611SAlexey Kardashevskiy 
88262af557SGuo Chao static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
89262af557SGuo Chao {
90262af557SGuo Chao 	return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
91262af557SGuo Chao 		(IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
92262af557SGuo Chao }
93262af557SGuo Chao 
944b82ab18SGavin Shan static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
954b82ab18SGavin Shan {
964b82ab18SGavin Shan 	if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
974b82ab18SGavin Shan 		pr_warn("%s: Invalid PE %d on PHB#%x\n",
984b82ab18SGavin Shan 			__func__, pe_no, phb->hose->global_number);
994b82ab18SGavin Shan 		return;
1004b82ab18SGavin Shan 	}
1014b82ab18SGavin Shan 
1024b82ab18SGavin Shan 	if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) {
1034b82ab18SGavin Shan 		pr_warn("%s: PE %d was assigned on PHB#%x\n",
1044b82ab18SGavin Shan 			__func__, pe_no, phb->hose->global_number);
1054b82ab18SGavin Shan 		return;
1064b82ab18SGavin Shan 	}
1074b82ab18SGavin Shan 
1084b82ab18SGavin Shan 	phb->ioda.pe_array[pe_no].phb = phb;
1094b82ab18SGavin Shan 	phb->ioda.pe_array[pe_no].pe_number = pe_no;
1104b82ab18SGavin Shan }
1114b82ab18SGavin Shan 
112cad5cef6SGreg Kroah-Hartman static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
113184cd4a3SBenjamin Herrenschmidt {
114184cd4a3SBenjamin Herrenschmidt 	unsigned long pe;
115184cd4a3SBenjamin Herrenschmidt 
116184cd4a3SBenjamin Herrenschmidt 	do {
117184cd4a3SBenjamin Herrenschmidt 		pe = find_next_zero_bit(phb->ioda.pe_alloc,
118184cd4a3SBenjamin Herrenschmidt 					phb->ioda.total_pe, 0);
119184cd4a3SBenjamin Herrenschmidt 		if (pe >= phb->ioda.total_pe)
120184cd4a3SBenjamin Herrenschmidt 			return IODA_INVALID_PE;
121184cd4a3SBenjamin Herrenschmidt 	} while(test_and_set_bit(pe, phb->ioda.pe_alloc));
122184cd4a3SBenjamin Herrenschmidt 
1234cce9550SGavin Shan 	phb->ioda.pe_array[pe].phb = phb;
124184cd4a3SBenjamin Herrenschmidt 	phb->ioda.pe_array[pe].pe_number = pe;
125184cd4a3SBenjamin Herrenschmidt 	return pe;
126184cd4a3SBenjamin Herrenschmidt }
127184cd4a3SBenjamin Herrenschmidt 
128cad5cef6SGreg Kroah-Hartman static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
129184cd4a3SBenjamin Herrenschmidt {
130184cd4a3SBenjamin Herrenschmidt 	WARN_ON(phb->ioda.pe_array[pe].pdev);
131184cd4a3SBenjamin Herrenschmidt 
132184cd4a3SBenjamin Herrenschmidt 	memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
133184cd4a3SBenjamin Herrenschmidt 	clear_bit(pe, phb->ioda.pe_alloc);
134184cd4a3SBenjamin Herrenschmidt }
135184cd4a3SBenjamin Herrenschmidt 
136262af557SGuo Chao /* The default M64 BAR is shared by all PEs */
137262af557SGuo Chao static int pnv_ioda2_init_m64(struct pnv_phb *phb)
138262af557SGuo Chao {
139262af557SGuo Chao 	const char *desc;
140262af557SGuo Chao 	struct resource *r;
141262af557SGuo Chao 	s64 rc;
142262af557SGuo Chao 
143262af557SGuo Chao 	/* Configure the default M64 BAR */
144262af557SGuo Chao 	rc = opal_pci_set_phb_mem_window(phb->opal_id,
145262af557SGuo Chao 					 OPAL_M64_WINDOW_TYPE,
146262af557SGuo Chao 					 phb->ioda.m64_bar_idx,
147262af557SGuo Chao 					 phb->ioda.m64_base,
148262af557SGuo Chao 					 0, /* unused */
149262af557SGuo Chao 					 phb->ioda.m64_size);
150262af557SGuo Chao 	if (rc != OPAL_SUCCESS) {
151262af557SGuo Chao 		desc = "configuring";
152262af557SGuo Chao 		goto fail;
153262af557SGuo Chao 	}
154262af557SGuo Chao 
155262af557SGuo Chao 	/* Enable the default M64 BAR */
156262af557SGuo Chao 	rc = opal_pci_phb_mmio_enable(phb->opal_id,
157262af557SGuo Chao 				      OPAL_M64_WINDOW_TYPE,
158262af557SGuo Chao 				      phb->ioda.m64_bar_idx,
159262af557SGuo Chao 				      OPAL_ENABLE_M64_SPLIT);
160262af557SGuo Chao 	if (rc != OPAL_SUCCESS) {
161262af557SGuo Chao 		desc = "enabling";
162262af557SGuo Chao 		goto fail;
163262af557SGuo Chao 	}
164262af557SGuo Chao 
165262af557SGuo Chao 	/* Mark the M64 BAR assigned */
166262af557SGuo Chao 	set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
167262af557SGuo Chao 
168262af557SGuo Chao 	/*
169262af557SGuo Chao 	 * Strip off the segment used by the reserved PE, which is
170262af557SGuo Chao 	 * expected to be 0 or last one of PE capabicity.
171262af557SGuo Chao 	 */
172262af557SGuo Chao 	r = &phb->hose->mem_resources[1];
173262af557SGuo Chao 	if (phb->ioda.reserved_pe == 0)
174262af557SGuo Chao 		r->start += phb->ioda.m64_segsize;
175262af557SGuo Chao 	else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
176262af557SGuo Chao 		r->end -= phb->ioda.m64_segsize;
177262af557SGuo Chao 	else
178262af557SGuo Chao 		pr_warn("  Cannot strip M64 segment for reserved PE#%d\n",
179262af557SGuo Chao 			phb->ioda.reserved_pe);
180262af557SGuo Chao 
181262af557SGuo Chao 	return 0;
182262af557SGuo Chao 
183262af557SGuo Chao fail:
184262af557SGuo Chao 	pr_warn("  Failure %lld %s M64 BAR#%d\n",
185262af557SGuo Chao 		rc, desc, phb->ioda.m64_bar_idx);
186262af557SGuo Chao 	opal_pci_phb_mmio_enable(phb->opal_id,
187262af557SGuo Chao 				 OPAL_M64_WINDOW_TYPE,
188262af557SGuo Chao 				 phb->ioda.m64_bar_idx,
189262af557SGuo Chao 				 OPAL_DISABLE_M64);
190262af557SGuo Chao 	return -EIO;
191262af557SGuo Chao }
192262af557SGuo Chao 
1935ef73567SGavin Shan static void pnv_ioda2_reserve_m64_pe(struct pnv_phb *phb)
194262af557SGuo Chao {
195262af557SGuo Chao 	resource_size_t sgsz = phb->ioda.m64_segsize;
196262af557SGuo Chao 	struct pci_dev *pdev;
197262af557SGuo Chao 	struct resource *r;
198262af557SGuo Chao 	int base, step, i;
199262af557SGuo Chao 
200262af557SGuo Chao 	/*
201262af557SGuo Chao 	 * Root bus always has full M64 range and root port has
202262af557SGuo Chao 	 * M64 range used in reality. So we're checking root port
203262af557SGuo Chao 	 * instead of root bus.
204262af557SGuo Chao 	 */
205262af557SGuo Chao 	list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
2064b82ab18SGavin Shan 		for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
2074b82ab18SGavin Shan 			r = &pdev->resource[PCI_BRIDGE_RESOURCES + i];
208262af557SGuo Chao 			if (!r->parent ||
209262af557SGuo Chao 			    !pnv_pci_is_mem_pref_64(r->flags))
210262af557SGuo Chao 				continue;
211262af557SGuo Chao 
212262af557SGuo Chao 			base = (r->start - phb->ioda.m64_base) / sgsz;
213262af557SGuo Chao 			for (step = 0; step < resource_size(r) / sgsz; step++)
2144b82ab18SGavin Shan 				pnv_ioda_reserve_pe(phb, base + step);
215262af557SGuo Chao 		}
216262af557SGuo Chao 	}
217262af557SGuo Chao }
218262af557SGuo Chao 
219262af557SGuo Chao static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
220262af557SGuo Chao 				 struct pci_bus *bus, int all)
221262af557SGuo Chao {
222262af557SGuo Chao 	resource_size_t segsz = phb->ioda.m64_segsize;
223262af557SGuo Chao 	struct pci_dev *pdev;
224262af557SGuo Chao 	struct resource *r;
225262af557SGuo Chao 	struct pnv_ioda_pe *master_pe, *pe;
226262af557SGuo Chao 	unsigned long size, *pe_alloc;
227262af557SGuo Chao 	bool found;
228262af557SGuo Chao 	int start, i, j;
229262af557SGuo Chao 
230262af557SGuo Chao 	/* Root bus shouldn't use M64 */
231262af557SGuo Chao 	if (pci_is_root_bus(bus))
232262af557SGuo Chao 		return IODA_INVALID_PE;
233262af557SGuo Chao 
234262af557SGuo Chao 	/* We support only one M64 window on each bus */
235262af557SGuo Chao 	found = false;
236262af557SGuo Chao 	pci_bus_for_each_resource(bus, r, i) {
237262af557SGuo Chao 		if (r && r->parent &&
238262af557SGuo Chao 		    pnv_pci_is_mem_pref_64(r->flags)) {
239262af557SGuo Chao 			found = true;
240262af557SGuo Chao 			break;
241262af557SGuo Chao 		}
242262af557SGuo Chao 	}
243262af557SGuo Chao 
244262af557SGuo Chao 	/* No M64 window found ? */
245262af557SGuo Chao 	if (!found)
246262af557SGuo Chao 		return IODA_INVALID_PE;
247262af557SGuo Chao 
248262af557SGuo Chao 	/* Allocate bitmap */
249262af557SGuo Chao 	size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
250262af557SGuo Chao 	pe_alloc = kzalloc(size, GFP_KERNEL);
251262af557SGuo Chao 	if (!pe_alloc) {
252262af557SGuo Chao 		pr_warn("%s: Out of memory !\n",
253262af557SGuo Chao 			__func__);
254262af557SGuo Chao 		return IODA_INVALID_PE;
255262af557SGuo Chao 	}
256262af557SGuo Chao 
257262af557SGuo Chao 	/*
258262af557SGuo Chao 	 * Figure out reserved PE numbers by the PE
259262af557SGuo Chao 	 * the its child PEs.
260262af557SGuo Chao 	 */
261262af557SGuo Chao 	start = (r->start - phb->ioda.m64_base) / segsz;
262262af557SGuo Chao 	for (i = 0; i < resource_size(r) / segsz; i++)
263262af557SGuo Chao 		set_bit(start + i, pe_alloc);
264262af557SGuo Chao 
265262af557SGuo Chao 	if (all)
266262af557SGuo Chao 		goto done;
267262af557SGuo Chao 
268262af557SGuo Chao 	/*
269262af557SGuo Chao 	 * If the PE doesn't cover all subordinate buses,
270262af557SGuo Chao 	 * we need subtract from reserved PEs for children.
271262af557SGuo Chao 	 */
272262af557SGuo Chao 	list_for_each_entry(pdev, &bus->devices, bus_list) {
273262af557SGuo Chao 		if (!pdev->subordinate)
274262af557SGuo Chao 			continue;
275262af557SGuo Chao 
276262af557SGuo Chao 		pci_bus_for_each_resource(pdev->subordinate, r, i) {
277262af557SGuo Chao 			if (!r || !r->parent ||
278262af557SGuo Chao 			    !pnv_pci_is_mem_pref_64(r->flags))
279262af557SGuo Chao 				continue;
280262af557SGuo Chao 
281262af557SGuo Chao 			start = (r->start - phb->ioda.m64_base) / segsz;
282262af557SGuo Chao 			for (j = 0; j < resource_size(r) / segsz ; j++)
283262af557SGuo Chao 				clear_bit(start + j, pe_alloc);
284262af557SGuo Chao                 }
285262af557SGuo Chao         }
286262af557SGuo Chao 
287262af557SGuo Chao 	/*
288262af557SGuo Chao 	 * the current bus might not own M64 window and that's all
289262af557SGuo Chao 	 * contributed by its child buses. For the case, we needn't
290262af557SGuo Chao 	 * pick M64 dependent PE#.
291262af557SGuo Chao 	 */
292262af557SGuo Chao 	if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
293262af557SGuo Chao 		kfree(pe_alloc);
294262af557SGuo Chao 		return IODA_INVALID_PE;
295262af557SGuo Chao 	}
296262af557SGuo Chao 
297262af557SGuo Chao 	/*
298262af557SGuo Chao 	 * Figure out the master PE and put all slave PEs to master
299262af557SGuo Chao 	 * PE's list to form compound PE.
300262af557SGuo Chao 	 */
301262af557SGuo Chao done:
302262af557SGuo Chao 	master_pe = NULL;
303262af557SGuo Chao 	i = -1;
304262af557SGuo Chao 	while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
305262af557SGuo Chao 		phb->ioda.total_pe) {
306262af557SGuo Chao 		pe = &phb->ioda.pe_array[i];
307262af557SGuo Chao 
308262af557SGuo Chao 		if (!master_pe) {
309262af557SGuo Chao 			pe->flags |= PNV_IODA_PE_MASTER;
310262af557SGuo Chao 			INIT_LIST_HEAD(&pe->slaves);
311262af557SGuo Chao 			master_pe = pe;
312262af557SGuo Chao 		} else {
313262af557SGuo Chao 			pe->flags |= PNV_IODA_PE_SLAVE;
314262af557SGuo Chao 			pe->master = master_pe;
315262af557SGuo Chao 			list_add_tail(&pe->list, &master_pe->slaves);
316262af557SGuo Chao 		}
317262af557SGuo Chao 	}
318262af557SGuo Chao 
319262af557SGuo Chao 	kfree(pe_alloc);
320262af557SGuo Chao 	return master_pe->pe_number;
321262af557SGuo Chao }
322262af557SGuo Chao 
323262af557SGuo Chao static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
324262af557SGuo Chao {
325262af557SGuo Chao 	struct pci_controller *hose = phb->hose;
326262af557SGuo Chao 	struct device_node *dn = hose->dn;
327262af557SGuo Chao 	struct resource *res;
328262af557SGuo Chao 	const u32 *r;
329262af557SGuo Chao 	u64 pci_addr;
330262af557SGuo Chao 
3311665c4a8SGavin Shan 	/* FIXME: Support M64 for P7IOC */
3321665c4a8SGavin Shan 	if (phb->type != PNV_PHB_IODA2) {
3331665c4a8SGavin Shan 		pr_info("  Not support M64 window\n");
3341665c4a8SGavin Shan 		return;
3351665c4a8SGavin Shan 	}
3361665c4a8SGavin Shan 
337262af557SGuo Chao 	if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
338262af557SGuo Chao 		pr_info("  Firmware too old to support M64 window\n");
339262af557SGuo Chao 		return;
340262af557SGuo Chao 	}
341262af557SGuo Chao 
342262af557SGuo Chao 	r = of_get_property(dn, "ibm,opal-m64-window", NULL);
343262af557SGuo Chao 	if (!r) {
344262af557SGuo Chao 		pr_info("  No <ibm,opal-m64-window> on %s\n",
345262af557SGuo Chao 			dn->full_name);
346262af557SGuo Chao 		return;
347262af557SGuo Chao 	}
348262af557SGuo Chao 
349262af557SGuo Chao 	res = &hose->mem_resources[1];
350262af557SGuo Chao 	res->start = of_translate_address(dn, r + 2);
351262af557SGuo Chao 	res->end = res->start + of_read_number(r + 4, 2) - 1;
352262af557SGuo Chao 	res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
353262af557SGuo Chao 	pci_addr = of_read_number(r, 2);
354262af557SGuo Chao 	hose->mem_offset[1] = res->start - pci_addr;
355262af557SGuo Chao 
356262af557SGuo Chao 	phb->ioda.m64_size = resource_size(res);
357262af557SGuo Chao 	phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
358262af557SGuo Chao 	phb->ioda.m64_base = pci_addr;
359262af557SGuo Chao 
360262af557SGuo Chao 	/* Use last M64 BAR to cover M64 window */
361262af557SGuo Chao 	phb->ioda.m64_bar_idx = 15;
362262af557SGuo Chao 	phb->init_m64 = pnv_ioda2_init_m64;
3635ef73567SGavin Shan 	phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
364262af557SGuo Chao 	phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
365262af557SGuo Chao }
366262af557SGuo Chao 
36749dec922SGavin Shan static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
36849dec922SGavin Shan {
36949dec922SGavin Shan 	struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
37049dec922SGavin Shan 	struct pnv_ioda_pe *slave;
37149dec922SGavin Shan 	s64 rc;
37249dec922SGavin Shan 
37349dec922SGavin Shan 	/* Fetch master PE */
37449dec922SGavin Shan 	if (pe->flags & PNV_IODA_PE_SLAVE) {
37549dec922SGavin Shan 		pe = pe->master;
37649dec922SGavin Shan 		WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
37749dec922SGavin Shan 		pe_no = pe->pe_number;
37849dec922SGavin Shan 	}
37949dec922SGavin Shan 
38049dec922SGavin Shan 	/* Freeze master PE */
38149dec922SGavin Shan 	rc = opal_pci_eeh_freeze_set(phb->opal_id,
38249dec922SGavin Shan 				     pe_no,
38349dec922SGavin Shan 				     OPAL_EEH_ACTION_SET_FREEZE_ALL);
38449dec922SGavin Shan 	if (rc != OPAL_SUCCESS) {
38549dec922SGavin Shan 		pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
38649dec922SGavin Shan 			__func__, rc, phb->hose->global_number, pe_no);
38749dec922SGavin Shan 		return;
38849dec922SGavin Shan 	}
38949dec922SGavin Shan 
39049dec922SGavin Shan 	/* Freeze slave PEs */
39149dec922SGavin Shan 	if (!(pe->flags & PNV_IODA_PE_MASTER))
39249dec922SGavin Shan 		return;
39349dec922SGavin Shan 
39449dec922SGavin Shan 	list_for_each_entry(slave, &pe->slaves, list) {
39549dec922SGavin Shan 		rc = opal_pci_eeh_freeze_set(phb->opal_id,
39649dec922SGavin Shan 					     slave->pe_number,
39749dec922SGavin Shan 					     OPAL_EEH_ACTION_SET_FREEZE_ALL);
39849dec922SGavin Shan 		if (rc != OPAL_SUCCESS)
39949dec922SGavin Shan 			pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
40049dec922SGavin Shan 				__func__, rc, phb->hose->global_number,
40149dec922SGavin Shan 				slave->pe_number);
40249dec922SGavin Shan 	}
40349dec922SGavin Shan }
40449dec922SGavin Shan 
405e51df2c1SAnton Blanchard static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
40649dec922SGavin Shan {
40749dec922SGavin Shan 	struct pnv_ioda_pe *pe, *slave;
40849dec922SGavin Shan 	s64 rc;
40949dec922SGavin Shan 
41049dec922SGavin Shan 	/* Find master PE */
41149dec922SGavin Shan 	pe = &phb->ioda.pe_array[pe_no];
41249dec922SGavin Shan 	if (pe->flags & PNV_IODA_PE_SLAVE) {
41349dec922SGavin Shan 		pe = pe->master;
41449dec922SGavin Shan 		WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
41549dec922SGavin Shan 		pe_no = pe->pe_number;
41649dec922SGavin Shan 	}
41749dec922SGavin Shan 
41849dec922SGavin Shan 	/* Clear frozen state for master PE */
41949dec922SGavin Shan 	rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
42049dec922SGavin Shan 	if (rc != OPAL_SUCCESS) {
42149dec922SGavin Shan 		pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
42249dec922SGavin Shan 			__func__, rc, opt, phb->hose->global_number, pe_no);
42349dec922SGavin Shan 		return -EIO;
42449dec922SGavin Shan 	}
42549dec922SGavin Shan 
42649dec922SGavin Shan 	if (!(pe->flags & PNV_IODA_PE_MASTER))
42749dec922SGavin Shan 		return 0;
42849dec922SGavin Shan 
42949dec922SGavin Shan 	/* Clear frozen state for slave PEs */
43049dec922SGavin Shan 	list_for_each_entry(slave, &pe->slaves, list) {
43149dec922SGavin Shan 		rc = opal_pci_eeh_freeze_clear(phb->opal_id,
43249dec922SGavin Shan 					     slave->pe_number,
43349dec922SGavin Shan 					     opt);
43449dec922SGavin Shan 		if (rc != OPAL_SUCCESS) {
43549dec922SGavin Shan 			pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
43649dec922SGavin Shan 				__func__, rc, opt, phb->hose->global_number,
43749dec922SGavin Shan 				slave->pe_number);
43849dec922SGavin Shan 			return -EIO;
43949dec922SGavin Shan 		}
44049dec922SGavin Shan 	}
44149dec922SGavin Shan 
44249dec922SGavin Shan 	return 0;
44349dec922SGavin Shan }
44449dec922SGavin Shan 
44549dec922SGavin Shan static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
44649dec922SGavin Shan {
44749dec922SGavin Shan 	struct pnv_ioda_pe *slave, *pe;
44849dec922SGavin Shan 	u8 fstate, state;
44949dec922SGavin Shan 	__be16 pcierr;
45049dec922SGavin Shan 	s64 rc;
45149dec922SGavin Shan 
45249dec922SGavin Shan 	/* Sanity check on PE number */
45349dec922SGavin Shan 	if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
45449dec922SGavin Shan 		return OPAL_EEH_STOPPED_PERM_UNAVAIL;
45549dec922SGavin Shan 
45649dec922SGavin Shan 	/*
45749dec922SGavin Shan 	 * Fetch the master PE and the PE instance might be
45849dec922SGavin Shan 	 * not initialized yet.
45949dec922SGavin Shan 	 */
46049dec922SGavin Shan 	pe = &phb->ioda.pe_array[pe_no];
46149dec922SGavin Shan 	if (pe->flags & PNV_IODA_PE_SLAVE) {
46249dec922SGavin Shan 		pe = pe->master;
46349dec922SGavin Shan 		WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
46449dec922SGavin Shan 		pe_no = pe->pe_number;
46549dec922SGavin Shan 	}
46649dec922SGavin Shan 
46749dec922SGavin Shan 	/* Check the master PE */
46849dec922SGavin Shan 	rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
46949dec922SGavin Shan 					&state, &pcierr, NULL);
47049dec922SGavin Shan 	if (rc != OPAL_SUCCESS) {
47149dec922SGavin Shan 		pr_warn("%s: Failure %lld getting "
47249dec922SGavin Shan 			"PHB#%x-PE#%x state\n",
47349dec922SGavin Shan 			__func__, rc,
47449dec922SGavin Shan 			phb->hose->global_number, pe_no);
47549dec922SGavin Shan 		return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
47649dec922SGavin Shan 	}
47749dec922SGavin Shan 
47849dec922SGavin Shan 	/* Check the slave PE */
47949dec922SGavin Shan 	if (!(pe->flags & PNV_IODA_PE_MASTER))
48049dec922SGavin Shan 		return state;
48149dec922SGavin Shan 
48249dec922SGavin Shan 	list_for_each_entry(slave, &pe->slaves, list) {
48349dec922SGavin Shan 		rc = opal_pci_eeh_freeze_status(phb->opal_id,
48449dec922SGavin Shan 						slave->pe_number,
48549dec922SGavin Shan 						&fstate,
48649dec922SGavin Shan 						&pcierr,
48749dec922SGavin Shan 						NULL);
48849dec922SGavin Shan 		if (rc != OPAL_SUCCESS) {
48949dec922SGavin Shan 			pr_warn("%s: Failure %lld getting "
49049dec922SGavin Shan 				"PHB#%x-PE#%x state\n",
49149dec922SGavin Shan 				__func__, rc,
49249dec922SGavin Shan 				phb->hose->global_number, slave->pe_number);
49349dec922SGavin Shan 			return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
49449dec922SGavin Shan 		}
49549dec922SGavin Shan 
49649dec922SGavin Shan 		/*
49749dec922SGavin Shan 		 * Override the result based on the ascending
49849dec922SGavin Shan 		 * priority.
49949dec922SGavin Shan 		 */
50049dec922SGavin Shan 		if (fstate > state)
50149dec922SGavin Shan 			state = fstate;
50249dec922SGavin Shan 	}
50349dec922SGavin Shan 
50449dec922SGavin Shan 	return state;
50549dec922SGavin Shan }
50649dec922SGavin Shan 
507184cd4a3SBenjamin Herrenschmidt /* Currently those 2 are only used when MSIs are enabled, this will change
508184cd4a3SBenjamin Herrenschmidt  * but in the meantime, we need to protect them to avoid warnings
509184cd4a3SBenjamin Herrenschmidt  */
510184cd4a3SBenjamin Herrenschmidt #ifdef CONFIG_PCI_MSI
511cad5cef6SGreg Kroah-Hartman static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
512184cd4a3SBenjamin Herrenschmidt {
513184cd4a3SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
514184cd4a3SBenjamin Herrenschmidt 	struct pnv_phb *phb = hose->private_data;
515b72c1f65SBenjamin Herrenschmidt 	struct pci_dn *pdn = pci_get_pdn(dev);
516184cd4a3SBenjamin Herrenschmidt 
517184cd4a3SBenjamin Herrenschmidt 	if (!pdn)
518184cd4a3SBenjamin Herrenschmidt 		return NULL;
519184cd4a3SBenjamin Herrenschmidt 	if (pdn->pe_number == IODA_INVALID_PE)
520184cd4a3SBenjamin Herrenschmidt 		return NULL;
521184cd4a3SBenjamin Herrenschmidt 	return &phb->ioda.pe_array[pdn->pe_number];
522184cd4a3SBenjamin Herrenschmidt }
523184cd4a3SBenjamin Herrenschmidt #endif /* CONFIG_PCI_MSI */
524184cd4a3SBenjamin Herrenschmidt 
525b131a842SGavin Shan static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
526b131a842SGavin Shan 				  struct pnv_ioda_pe *parent,
527b131a842SGavin Shan 				  struct pnv_ioda_pe *child,
528b131a842SGavin Shan 				  bool is_add)
529b131a842SGavin Shan {
530b131a842SGavin Shan 	const char *desc = is_add ? "adding" : "removing";
531b131a842SGavin Shan 	uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
532b131a842SGavin Shan 			      OPAL_REMOVE_PE_FROM_DOMAIN;
533b131a842SGavin Shan 	struct pnv_ioda_pe *slave;
534b131a842SGavin Shan 	long rc;
535b131a842SGavin Shan 
536b131a842SGavin Shan 	/* Parent PE affects child PE */
537b131a842SGavin Shan 	rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
538b131a842SGavin Shan 				child->pe_number, op);
539b131a842SGavin Shan 	if (rc != OPAL_SUCCESS) {
540b131a842SGavin Shan 		pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
541b131a842SGavin Shan 			rc, desc);
542b131a842SGavin Shan 		return -ENXIO;
543b131a842SGavin Shan 	}
544b131a842SGavin Shan 
545b131a842SGavin Shan 	if (!(child->flags & PNV_IODA_PE_MASTER))
546b131a842SGavin Shan 		return 0;
547b131a842SGavin Shan 
548b131a842SGavin Shan 	/* Compound case: parent PE affects slave PEs */
549b131a842SGavin Shan 	list_for_each_entry(slave, &child->slaves, list) {
550b131a842SGavin Shan 		rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
551b131a842SGavin Shan 					slave->pe_number, op);
552b131a842SGavin Shan 		if (rc != OPAL_SUCCESS) {
553b131a842SGavin Shan 			pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
554b131a842SGavin Shan 				rc, desc);
555b131a842SGavin Shan 			return -ENXIO;
556b131a842SGavin Shan 		}
557b131a842SGavin Shan 	}
558b131a842SGavin Shan 
559b131a842SGavin Shan 	return 0;
560b131a842SGavin Shan }
561b131a842SGavin Shan 
562b131a842SGavin Shan static int pnv_ioda_set_peltv(struct pnv_phb *phb,
563b131a842SGavin Shan 			      struct pnv_ioda_pe *pe,
564b131a842SGavin Shan 			      bool is_add)
565b131a842SGavin Shan {
566b131a842SGavin Shan 	struct pnv_ioda_pe *slave;
567b131a842SGavin Shan 	struct pci_dev *pdev;
568b131a842SGavin Shan 	int ret;
569b131a842SGavin Shan 
570b131a842SGavin Shan 	/*
571b131a842SGavin Shan 	 * Clear PE frozen state. If it's master PE, we need
572b131a842SGavin Shan 	 * clear slave PE frozen state as well.
573b131a842SGavin Shan 	 */
574b131a842SGavin Shan 	if (is_add) {
575b131a842SGavin Shan 		opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
576b131a842SGavin Shan 					  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
577b131a842SGavin Shan 		if (pe->flags & PNV_IODA_PE_MASTER) {
578b131a842SGavin Shan 			list_for_each_entry(slave, &pe->slaves, list)
579b131a842SGavin Shan 				opal_pci_eeh_freeze_clear(phb->opal_id,
580b131a842SGavin Shan 							  slave->pe_number,
581b131a842SGavin Shan 							  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
582b131a842SGavin Shan 		}
583b131a842SGavin Shan 	}
584b131a842SGavin Shan 
585b131a842SGavin Shan 	/*
586b131a842SGavin Shan 	 * Associate PE in PELT. We need add the PE into the
587b131a842SGavin Shan 	 * corresponding PELT-V as well. Otherwise, the error
588b131a842SGavin Shan 	 * originated from the PE might contribute to other
589b131a842SGavin Shan 	 * PEs.
590b131a842SGavin Shan 	 */
591b131a842SGavin Shan 	ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
592b131a842SGavin Shan 	if (ret)
593b131a842SGavin Shan 		return ret;
594b131a842SGavin Shan 
595b131a842SGavin Shan 	/* For compound PEs, any one affects all of them */
596b131a842SGavin Shan 	if (pe->flags & PNV_IODA_PE_MASTER) {
597b131a842SGavin Shan 		list_for_each_entry(slave, &pe->slaves, list) {
598b131a842SGavin Shan 			ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
599b131a842SGavin Shan 			if (ret)
600b131a842SGavin Shan 				return ret;
601b131a842SGavin Shan 		}
602b131a842SGavin Shan 	}
603b131a842SGavin Shan 
604b131a842SGavin Shan 	if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
605b131a842SGavin Shan 		pdev = pe->pbus->self;
606b131a842SGavin Shan 	else
607b131a842SGavin Shan 		pdev = pe->pdev->bus->self;
608b131a842SGavin Shan 	while (pdev) {
609b131a842SGavin Shan 		struct pci_dn *pdn = pci_get_pdn(pdev);
610b131a842SGavin Shan 		struct pnv_ioda_pe *parent;
611b131a842SGavin Shan 
612b131a842SGavin Shan 		if (pdn && pdn->pe_number != IODA_INVALID_PE) {
613b131a842SGavin Shan 			parent = &phb->ioda.pe_array[pdn->pe_number];
614b131a842SGavin Shan 			ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
615b131a842SGavin Shan 			if (ret)
616b131a842SGavin Shan 				return ret;
617b131a842SGavin Shan 		}
618b131a842SGavin Shan 
619b131a842SGavin Shan 		pdev = pdev->bus->self;
620b131a842SGavin Shan 	}
621b131a842SGavin Shan 
622b131a842SGavin Shan 	return 0;
623b131a842SGavin Shan }
624b131a842SGavin Shan 
625cad5cef6SGreg Kroah-Hartman static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
626184cd4a3SBenjamin Herrenschmidt {
627184cd4a3SBenjamin Herrenschmidt 	struct pci_dev *parent;
628184cd4a3SBenjamin Herrenschmidt 	uint8_t bcomp, dcomp, fcomp;
629184cd4a3SBenjamin Herrenschmidt 	long rc, rid_end, rid;
630184cd4a3SBenjamin Herrenschmidt 
631184cd4a3SBenjamin Herrenschmidt 	/* Bus validation ? */
632184cd4a3SBenjamin Herrenschmidt 	if (pe->pbus) {
633184cd4a3SBenjamin Herrenschmidt 		int count;
634184cd4a3SBenjamin Herrenschmidt 
635184cd4a3SBenjamin Herrenschmidt 		dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
636184cd4a3SBenjamin Herrenschmidt 		fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
637184cd4a3SBenjamin Herrenschmidt 		parent = pe->pbus->self;
638fb446ad0SGavin Shan 		if (pe->flags & PNV_IODA_PE_BUS_ALL)
639b918c62eSYinghai Lu 			count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
640fb446ad0SGavin Shan 		else
641fb446ad0SGavin Shan 			count = 1;
642fb446ad0SGavin Shan 
643184cd4a3SBenjamin Herrenschmidt 		switch(count) {
644184cd4a3SBenjamin Herrenschmidt 		case  1: bcomp = OpalPciBusAll;		break;
645184cd4a3SBenjamin Herrenschmidt 		case  2: bcomp = OpalPciBus7Bits;	break;
646184cd4a3SBenjamin Herrenschmidt 		case  4: bcomp = OpalPciBus6Bits;	break;
647184cd4a3SBenjamin Herrenschmidt 		case  8: bcomp = OpalPciBus5Bits;	break;
648184cd4a3SBenjamin Herrenschmidt 		case 16: bcomp = OpalPciBus4Bits;	break;
649184cd4a3SBenjamin Herrenschmidt 		case 32: bcomp = OpalPciBus3Bits;	break;
650184cd4a3SBenjamin Herrenschmidt 		default:
651184cd4a3SBenjamin Herrenschmidt 			pr_err("%s: Number of subordinate busses %d"
652184cd4a3SBenjamin Herrenschmidt 			       " unsupported\n",
653184cd4a3SBenjamin Herrenschmidt 			       pci_name(pe->pbus->self), count);
654184cd4a3SBenjamin Herrenschmidt 			/* Do an exact match only */
655184cd4a3SBenjamin Herrenschmidt 			bcomp = OpalPciBusAll;
656184cd4a3SBenjamin Herrenschmidt 		}
657184cd4a3SBenjamin Herrenschmidt 		rid_end = pe->rid + (count << 8);
658184cd4a3SBenjamin Herrenschmidt 	} else {
659184cd4a3SBenjamin Herrenschmidt 		parent = pe->pdev->bus->self;
660184cd4a3SBenjamin Herrenschmidt 		bcomp = OpalPciBusAll;
661184cd4a3SBenjamin Herrenschmidt 		dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
662184cd4a3SBenjamin Herrenschmidt 		fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
663184cd4a3SBenjamin Herrenschmidt 		rid_end = pe->rid + 1;
664184cd4a3SBenjamin Herrenschmidt 	}
665184cd4a3SBenjamin Herrenschmidt 
666631ad691SGavin Shan 	/*
667631ad691SGavin Shan 	 * Associate PE in PELT. We need add the PE into the
668631ad691SGavin Shan 	 * corresponding PELT-V as well. Otherwise, the error
669631ad691SGavin Shan 	 * originated from the PE might contribute to other
670631ad691SGavin Shan 	 * PEs.
671631ad691SGavin Shan 	 */
672184cd4a3SBenjamin Herrenschmidt 	rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
673184cd4a3SBenjamin Herrenschmidt 			     bcomp, dcomp, fcomp, OPAL_MAP_PE);
674184cd4a3SBenjamin Herrenschmidt 	if (rc) {
675184cd4a3SBenjamin Herrenschmidt 		pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
676184cd4a3SBenjamin Herrenschmidt 		return -ENXIO;
677184cd4a3SBenjamin Herrenschmidt 	}
678631ad691SGavin Shan 
679b131a842SGavin Shan 	/* Configure PELTV */
680b131a842SGavin Shan 	pnv_ioda_set_peltv(phb, pe, true);
681184cd4a3SBenjamin Herrenschmidt 
682184cd4a3SBenjamin Herrenschmidt 	/* Setup reverse map */
683184cd4a3SBenjamin Herrenschmidt 	for (rid = pe->rid; rid < rid_end; rid++)
684184cd4a3SBenjamin Herrenschmidt 		phb->ioda.pe_rmap[rid] = pe->pe_number;
685184cd4a3SBenjamin Herrenschmidt 
686184cd4a3SBenjamin Herrenschmidt 	/* Setup one MVTs on IODA1 */
6874773f76bSGavin Shan 	if (phb->type != PNV_PHB_IODA1) {
6884773f76bSGavin Shan 		pe->mve_number = 0;
6894773f76bSGavin Shan 		goto out;
6904773f76bSGavin Shan 	}
6914773f76bSGavin Shan 
692184cd4a3SBenjamin Herrenschmidt 	pe->mve_number = pe->pe_number;
6934773f76bSGavin Shan 	rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
6944773f76bSGavin Shan 	if (rc != OPAL_SUCCESS) {
695184cd4a3SBenjamin Herrenschmidt 		pe_err(pe, "OPAL error %ld setting up MVE %d\n",
696184cd4a3SBenjamin Herrenschmidt 		       rc, pe->mve_number);
697184cd4a3SBenjamin Herrenschmidt 		pe->mve_number = -1;
698184cd4a3SBenjamin Herrenschmidt 	} else {
699184cd4a3SBenjamin Herrenschmidt 		rc = opal_pci_set_mve_enable(phb->opal_id,
700cee72d5bSBenjamin Herrenschmidt 					     pe->mve_number, OPAL_ENABLE_MVE);
701184cd4a3SBenjamin Herrenschmidt 		if (rc) {
702184cd4a3SBenjamin Herrenschmidt 			pe_err(pe, "OPAL error %ld enabling MVE %d\n",
703184cd4a3SBenjamin Herrenschmidt 			       rc, pe->mve_number);
704184cd4a3SBenjamin Herrenschmidt 			pe->mve_number = -1;
705184cd4a3SBenjamin Herrenschmidt 		}
706184cd4a3SBenjamin Herrenschmidt 	}
707184cd4a3SBenjamin Herrenschmidt 
7084773f76bSGavin Shan out:
709184cd4a3SBenjamin Herrenschmidt 	return 0;
710184cd4a3SBenjamin Herrenschmidt }
711184cd4a3SBenjamin Herrenschmidt 
712cad5cef6SGreg Kroah-Hartman static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
713184cd4a3SBenjamin Herrenschmidt 				       struct pnv_ioda_pe *pe)
714184cd4a3SBenjamin Herrenschmidt {
715184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *lpe;
716184cd4a3SBenjamin Herrenschmidt 
7177ebdf956SGavin Shan 	list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
718184cd4a3SBenjamin Herrenschmidt 		if (lpe->dma_weight < pe->dma_weight) {
7197ebdf956SGavin Shan 			list_add_tail(&pe->dma_link, &lpe->dma_link);
720184cd4a3SBenjamin Herrenschmidt 			return;
721184cd4a3SBenjamin Herrenschmidt 		}
722184cd4a3SBenjamin Herrenschmidt 	}
7237ebdf956SGavin Shan 	list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
724184cd4a3SBenjamin Herrenschmidt }
725184cd4a3SBenjamin Herrenschmidt 
726184cd4a3SBenjamin Herrenschmidt static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
727184cd4a3SBenjamin Herrenschmidt {
728184cd4a3SBenjamin Herrenschmidt 	/* This is quite simplistic. The "base" weight of a device
729184cd4a3SBenjamin Herrenschmidt 	 * is 10. 0 means no DMA is to be accounted for it.
730184cd4a3SBenjamin Herrenschmidt 	 */
731184cd4a3SBenjamin Herrenschmidt 
732184cd4a3SBenjamin Herrenschmidt 	/* If it's a bridge, no DMA */
733184cd4a3SBenjamin Herrenschmidt 	if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
734184cd4a3SBenjamin Herrenschmidt 		return 0;
735184cd4a3SBenjamin Herrenschmidt 
736184cd4a3SBenjamin Herrenschmidt 	/* Reduce the weight of slow USB controllers */
737184cd4a3SBenjamin Herrenschmidt 	if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
738184cd4a3SBenjamin Herrenschmidt 	    dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
739184cd4a3SBenjamin Herrenschmidt 	    dev->class == PCI_CLASS_SERIAL_USB_EHCI)
740184cd4a3SBenjamin Herrenschmidt 		return 3;
741184cd4a3SBenjamin Herrenschmidt 
742184cd4a3SBenjamin Herrenschmidt 	/* Increase the weight of RAID (includes Obsidian) */
743184cd4a3SBenjamin Herrenschmidt 	if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
744184cd4a3SBenjamin Herrenschmidt 		return 15;
745184cd4a3SBenjamin Herrenschmidt 
746184cd4a3SBenjamin Herrenschmidt 	/* Default */
747184cd4a3SBenjamin Herrenschmidt 	return 10;
748184cd4a3SBenjamin Herrenschmidt }
749184cd4a3SBenjamin Herrenschmidt 
750fb446ad0SGavin Shan #if 0
751cad5cef6SGreg Kroah-Hartman static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
752184cd4a3SBenjamin Herrenschmidt {
753184cd4a3SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
754184cd4a3SBenjamin Herrenschmidt 	struct pnv_phb *phb = hose->private_data;
755b72c1f65SBenjamin Herrenschmidt 	struct pci_dn *pdn = pci_get_pdn(dev);
756184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe;
757184cd4a3SBenjamin Herrenschmidt 	int pe_num;
758184cd4a3SBenjamin Herrenschmidt 
759184cd4a3SBenjamin Herrenschmidt 	if (!pdn) {
760184cd4a3SBenjamin Herrenschmidt 		pr_err("%s: Device tree node not associated properly\n",
761184cd4a3SBenjamin Herrenschmidt 			   pci_name(dev));
762184cd4a3SBenjamin Herrenschmidt 		return NULL;
763184cd4a3SBenjamin Herrenschmidt 	}
764184cd4a3SBenjamin Herrenschmidt 	if (pdn->pe_number != IODA_INVALID_PE)
765184cd4a3SBenjamin Herrenschmidt 		return NULL;
766184cd4a3SBenjamin Herrenschmidt 
767184cd4a3SBenjamin Herrenschmidt 	/* PE#0 has been pre-set */
768184cd4a3SBenjamin Herrenschmidt 	if (dev->bus->number == 0)
769184cd4a3SBenjamin Herrenschmidt 		pe_num = 0;
770184cd4a3SBenjamin Herrenschmidt 	else
771184cd4a3SBenjamin Herrenschmidt 		pe_num = pnv_ioda_alloc_pe(phb);
772184cd4a3SBenjamin Herrenschmidt 	if (pe_num == IODA_INVALID_PE) {
773184cd4a3SBenjamin Herrenschmidt 		pr_warning("%s: Not enough PE# available, disabling device\n",
774184cd4a3SBenjamin Herrenschmidt 			   pci_name(dev));
775184cd4a3SBenjamin Herrenschmidt 		return NULL;
776184cd4a3SBenjamin Herrenschmidt 	}
777184cd4a3SBenjamin Herrenschmidt 
778184cd4a3SBenjamin Herrenschmidt 	/* NOTE: We get only one ref to the pci_dev for the pdn, not for the
779184cd4a3SBenjamin Herrenschmidt 	 * pointer in the PE data structure, both should be destroyed at the
780184cd4a3SBenjamin Herrenschmidt 	 * same time. However, this needs to be looked at more closely again
781184cd4a3SBenjamin Herrenschmidt 	 * once we actually start removing things (Hotplug, SR-IOV, ...)
782184cd4a3SBenjamin Herrenschmidt 	 *
783184cd4a3SBenjamin Herrenschmidt 	 * At some point we want to remove the PDN completely anyways
784184cd4a3SBenjamin Herrenschmidt 	 */
785184cd4a3SBenjamin Herrenschmidt 	pe = &phb->ioda.pe_array[pe_num];
786184cd4a3SBenjamin Herrenschmidt 	pci_dev_get(dev);
787184cd4a3SBenjamin Herrenschmidt 	pdn->pcidev = dev;
788184cd4a3SBenjamin Herrenschmidt 	pdn->pe_number = pe_num;
789184cd4a3SBenjamin Herrenschmidt 	pe->pdev = dev;
790184cd4a3SBenjamin Herrenschmidt 	pe->pbus = NULL;
791184cd4a3SBenjamin Herrenschmidt 	pe->tce32_seg = -1;
792184cd4a3SBenjamin Herrenschmidt 	pe->mve_number = -1;
793184cd4a3SBenjamin Herrenschmidt 	pe->rid = dev->bus->number << 8 | pdn->devfn;
794184cd4a3SBenjamin Herrenschmidt 
795184cd4a3SBenjamin Herrenschmidt 	pe_info(pe, "Associated device to PE\n");
796184cd4a3SBenjamin Herrenschmidt 
797184cd4a3SBenjamin Herrenschmidt 	if (pnv_ioda_configure_pe(phb, pe)) {
798184cd4a3SBenjamin Herrenschmidt 		/* XXX What do we do here ? */
799184cd4a3SBenjamin Herrenschmidt 		if (pe_num)
800184cd4a3SBenjamin Herrenschmidt 			pnv_ioda_free_pe(phb, pe_num);
801184cd4a3SBenjamin Herrenschmidt 		pdn->pe_number = IODA_INVALID_PE;
802184cd4a3SBenjamin Herrenschmidt 		pe->pdev = NULL;
803184cd4a3SBenjamin Herrenschmidt 		pci_dev_put(dev);
804184cd4a3SBenjamin Herrenschmidt 		return NULL;
805184cd4a3SBenjamin Herrenschmidt 	}
806184cd4a3SBenjamin Herrenschmidt 
807184cd4a3SBenjamin Herrenschmidt 	/* Assign a DMA weight to the device */
808184cd4a3SBenjamin Herrenschmidt 	pe->dma_weight = pnv_ioda_dma_weight(dev);
809184cd4a3SBenjamin Herrenschmidt 	if (pe->dma_weight != 0) {
810184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_weight += pe->dma_weight;
811184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_pe_count++;
812184cd4a3SBenjamin Herrenschmidt 	}
813184cd4a3SBenjamin Herrenschmidt 
814184cd4a3SBenjamin Herrenschmidt 	/* Link the PE */
815184cd4a3SBenjamin Herrenschmidt 	pnv_ioda_link_pe_by_weight(phb, pe);
816184cd4a3SBenjamin Herrenschmidt 
817184cd4a3SBenjamin Herrenschmidt 	return pe;
818184cd4a3SBenjamin Herrenschmidt }
819fb446ad0SGavin Shan #endif /* Useful for SRIOV case */
820184cd4a3SBenjamin Herrenschmidt 
821184cd4a3SBenjamin Herrenschmidt static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
822184cd4a3SBenjamin Herrenschmidt {
823184cd4a3SBenjamin Herrenschmidt 	struct pci_dev *dev;
824184cd4a3SBenjamin Herrenschmidt 
825184cd4a3SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
826b72c1f65SBenjamin Herrenschmidt 		struct pci_dn *pdn = pci_get_pdn(dev);
827184cd4a3SBenjamin Herrenschmidt 
828184cd4a3SBenjamin Herrenschmidt 		if (pdn == NULL) {
829184cd4a3SBenjamin Herrenschmidt 			pr_warn("%s: No device node associated with device !\n",
830184cd4a3SBenjamin Herrenschmidt 				pci_name(dev));
831184cd4a3SBenjamin Herrenschmidt 			continue;
832184cd4a3SBenjamin Herrenschmidt 		}
833184cd4a3SBenjamin Herrenschmidt 		pdn->pcidev = dev;
834184cd4a3SBenjamin Herrenschmidt 		pdn->pe_number = pe->pe_number;
835184cd4a3SBenjamin Herrenschmidt 		pe->dma_weight += pnv_ioda_dma_weight(dev);
836fb446ad0SGavin Shan 		if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
837184cd4a3SBenjamin Herrenschmidt 			pnv_ioda_setup_same_PE(dev->subordinate, pe);
838184cd4a3SBenjamin Herrenschmidt 	}
839184cd4a3SBenjamin Herrenschmidt }
840184cd4a3SBenjamin Herrenschmidt 
841fb446ad0SGavin Shan /*
842fb446ad0SGavin Shan  * There're 2 types of PCI bus sensitive PEs: One that is compromised of
843fb446ad0SGavin Shan  * single PCI bus. Another one that contains the primary PCI bus and its
844fb446ad0SGavin Shan  * subordinate PCI devices and buses. The second type of PE is normally
845fb446ad0SGavin Shan  * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
846fb446ad0SGavin Shan  */
847cad5cef6SGreg Kroah-Hartman static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
848184cd4a3SBenjamin Herrenschmidt {
849fb446ad0SGavin Shan 	struct pci_controller *hose = pci_bus_to_host(bus);
850184cd4a3SBenjamin Herrenschmidt 	struct pnv_phb *phb = hose->private_data;
851184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe;
852262af557SGuo Chao 	int pe_num = IODA_INVALID_PE;
853184cd4a3SBenjamin Herrenschmidt 
854262af557SGuo Chao 	/* Check if PE is determined by M64 */
855262af557SGuo Chao 	if (phb->pick_m64_pe)
856262af557SGuo Chao 		pe_num = phb->pick_m64_pe(phb, bus, all);
857262af557SGuo Chao 
858262af557SGuo Chao 	/* The PE number isn't pinned by M64 */
859262af557SGuo Chao 	if (pe_num == IODA_INVALID_PE)
860184cd4a3SBenjamin Herrenschmidt 		pe_num = pnv_ioda_alloc_pe(phb);
861262af557SGuo Chao 
862184cd4a3SBenjamin Herrenschmidt 	if (pe_num == IODA_INVALID_PE) {
863fb446ad0SGavin Shan 		pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
864fb446ad0SGavin Shan 			__func__, pci_domain_nr(bus), bus->number);
865184cd4a3SBenjamin Herrenschmidt 		return;
866184cd4a3SBenjamin Herrenschmidt 	}
867184cd4a3SBenjamin Herrenschmidt 
868184cd4a3SBenjamin Herrenschmidt 	pe = &phb->ioda.pe_array[pe_num];
869262af557SGuo Chao 	pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
870184cd4a3SBenjamin Herrenschmidt 	pe->pbus = bus;
871184cd4a3SBenjamin Herrenschmidt 	pe->pdev = NULL;
872184cd4a3SBenjamin Herrenschmidt 	pe->tce32_seg = -1;
873184cd4a3SBenjamin Herrenschmidt 	pe->mve_number = -1;
874b918c62eSYinghai Lu 	pe->rid = bus->busn_res.start << 8;
875184cd4a3SBenjamin Herrenschmidt 	pe->dma_weight = 0;
876184cd4a3SBenjamin Herrenschmidt 
877fb446ad0SGavin Shan 	if (all)
878fb446ad0SGavin Shan 		pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
879fb446ad0SGavin Shan 			bus->busn_res.start, bus->busn_res.end, pe_num);
880fb446ad0SGavin Shan 	else
881fb446ad0SGavin Shan 		pe_info(pe, "Secondary bus %d associated with PE#%d\n",
882fb446ad0SGavin Shan 			bus->busn_res.start, pe_num);
883184cd4a3SBenjamin Herrenschmidt 
884184cd4a3SBenjamin Herrenschmidt 	if (pnv_ioda_configure_pe(phb, pe)) {
885184cd4a3SBenjamin Herrenschmidt 		/* XXX What do we do here ? */
886184cd4a3SBenjamin Herrenschmidt 		if (pe_num)
887184cd4a3SBenjamin Herrenschmidt 			pnv_ioda_free_pe(phb, pe_num);
888184cd4a3SBenjamin Herrenschmidt 		pe->pbus = NULL;
889184cd4a3SBenjamin Herrenschmidt 		return;
890184cd4a3SBenjamin Herrenschmidt 	}
891184cd4a3SBenjamin Herrenschmidt 
892184cd4a3SBenjamin Herrenschmidt 	/* Associate it with all child devices */
893184cd4a3SBenjamin Herrenschmidt 	pnv_ioda_setup_same_PE(bus, pe);
894184cd4a3SBenjamin Herrenschmidt 
8957ebdf956SGavin Shan 	/* Put PE to the list */
8967ebdf956SGavin Shan 	list_add_tail(&pe->list, &phb->ioda.pe_list);
8977ebdf956SGavin Shan 
898184cd4a3SBenjamin Herrenschmidt 	/* Account for one DMA PE if at least one DMA capable device exist
899184cd4a3SBenjamin Herrenschmidt 	 * below the bridge
900184cd4a3SBenjamin Herrenschmidt 	 */
901184cd4a3SBenjamin Herrenschmidt 	if (pe->dma_weight != 0) {
902184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_weight += pe->dma_weight;
903184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_pe_count++;
904184cd4a3SBenjamin Herrenschmidt 	}
905184cd4a3SBenjamin Herrenschmidt 
906184cd4a3SBenjamin Herrenschmidt 	/* Link the PE */
907184cd4a3SBenjamin Herrenschmidt 	pnv_ioda_link_pe_by_weight(phb, pe);
908184cd4a3SBenjamin Herrenschmidt }
909184cd4a3SBenjamin Herrenschmidt 
910cad5cef6SGreg Kroah-Hartman static void pnv_ioda_setup_PEs(struct pci_bus *bus)
911184cd4a3SBenjamin Herrenschmidt {
912184cd4a3SBenjamin Herrenschmidt 	struct pci_dev *dev;
913fb446ad0SGavin Shan 
914fb446ad0SGavin Shan 	pnv_ioda_setup_bus_PE(bus, 0);
915184cd4a3SBenjamin Herrenschmidt 
916184cd4a3SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
917fb446ad0SGavin Shan 		if (dev->subordinate) {
91862f87c0eSYijing Wang 			if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
919fb446ad0SGavin Shan 				pnv_ioda_setup_bus_PE(dev->subordinate, 1);
920fb446ad0SGavin Shan 			else
921184cd4a3SBenjamin Herrenschmidt 				pnv_ioda_setup_PEs(dev->subordinate);
922184cd4a3SBenjamin Herrenschmidt 		}
923184cd4a3SBenjamin Herrenschmidt 	}
924fb446ad0SGavin Shan }
925fb446ad0SGavin Shan 
926fb446ad0SGavin Shan /*
927fb446ad0SGavin Shan  * Configure PEs so that the downstream PCI buses and devices
928fb446ad0SGavin Shan  * could have their associated PE#. Unfortunately, we didn't
929fb446ad0SGavin Shan  * figure out the way to identify the PLX bridge yet. So we
930fb446ad0SGavin Shan  * simply put the PCI bus and the subordinate behind the root
931fb446ad0SGavin Shan  * port to PE# here. The game rule here is expected to be changed
932fb446ad0SGavin Shan  * as soon as we can detected PLX bridge correctly.
933fb446ad0SGavin Shan  */
934cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_setup_PEs(void)
935fb446ad0SGavin Shan {
936fb446ad0SGavin Shan 	struct pci_controller *hose, *tmp;
937262af557SGuo Chao 	struct pnv_phb *phb;
938fb446ad0SGavin Shan 
939fb446ad0SGavin Shan 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
940262af557SGuo Chao 		phb = hose->private_data;
941262af557SGuo Chao 
942262af557SGuo Chao 		/* M64 layout might affect PE allocation */
9435ef73567SGavin Shan 		if (phb->reserve_m64_pe)
9445ef73567SGavin Shan 			phb->reserve_m64_pe(phb);
945262af557SGuo Chao 
946fb446ad0SGavin Shan 		pnv_ioda_setup_PEs(hose->bus);
947fb446ad0SGavin Shan 	}
948fb446ad0SGavin Shan }
949184cd4a3SBenjamin Herrenschmidt 
950959c9bddSGavin Shan static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
951184cd4a3SBenjamin Herrenschmidt {
952b72c1f65SBenjamin Herrenschmidt 	struct pci_dn *pdn = pci_get_pdn(pdev);
953959c9bddSGavin Shan 	struct pnv_ioda_pe *pe;
954184cd4a3SBenjamin Herrenschmidt 
955959c9bddSGavin Shan 	/*
956959c9bddSGavin Shan 	 * The function can be called while the PE#
957959c9bddSGavin Shan 	 * hasn't been assigned. Do nothing for the
958959c9bddSGavin Shan 	 * case.
959959c9bddSGavin Shan 	 */
960959c9bddSGavin Shan 	if (!pdn || pdn->pe_number == IODA_INVALID_PE)
961959c9bddSGavin Shan 		return;
962184cd4a3SBenjamin Herrenschmidt 
963959c9bddSGavin Shan 	pe = &phb->ioda.pe_array[pdn->pe_number];
964cd15b048SBenjamin Herrenschmidt 	WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
965763fe0adSGavin Shan 	set_iommu_table_base_and_group(&pdev->dev, &pe->tce32_table);
966184cd4a3SBenjamin Herrenschmidt }
967184cd4a3SBenjamin Herrenschmidt 
968cd15b048SBenjamin Herrenschmidt static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
969cd15b048SBenjamin Herrenschmidt 				     struct pci_dev *pdev, u64 dma_mask)
970cd15b048SBenjamin Herrenschmidt {
971cd15b048SBenjamin Herrenschmidt 	struct pci_dn *pdn = pci_get_pdn(pdev);
972cd15b048SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe;
973cd15b048SBenjamin Herrenschmidt 	uint64_t top;
974cd15b048SBenjamin Herrenschmidt 	bool bypass = false;
975cd15b048SBenjamin Herrenschmidt 
976cd15b048SBenjamin Herrenschmidt 	if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
977cd15b048SBenjamin Herrenschmidt 		return -ENODEV;;
978cd15b048SBenjamin Herrenschmidt 
979cd15b048SBenjamin Herrenschmidt 	pe = &phb->ioda.pe_array[pdn->pe_number];
980cd15b048SBenjamin Herrenschmidt 	if (pe->tce_bypass_enabled) {
981cd15b048SBenjamin Herrenschmidt 		top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
982cd15b048SBenjamin Herrenschmidt 		bypass = (dma_mask >= top);
983cd15b048SBenjamin Herrenschmidt 	}
984cd15b048SBenjamin Herrenschmidt 
985cd15b048SBenjamin Herrenschmidt 	if (bypass) {
986cd15b048SBenjamin Herrenschmidt 		dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
987cd15b048SBenjamin Herrenschmidt 		set_dma_ops(&pdev->dev, &dma_direct_ops);
988cd15b048SBenjamin Herrenschmidt 		set_dma_offset(&pdev->dev, pe->tce_bypass_base);
989cd15b048SBenjamin Herrenschmidt 	} else {
990cd15b048SBenjamin Herrenschmidt 		dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
991cd15b048SBenjamin Herrenschmidt 		set_dma_ops(&pdev->dev, &dma_iommu_ops);
992cd15b048SBenjamin Herrenschmidt 		set_iommu_table_base(&pdev->dev, &pe->tce32_table);
993cd15b048SBenjamin Herrenschmidt 	}
994a32305bfSBrian W Hart 	*pdev->dev.dma_mask = dma_mask;
995cd15b048SBenjamin Herrenschmidt 	return 0;
996cd15b048SBenjamin Herrenschmidt }
997cd15b048SBenjamin Herrenschmidt 
998fe7e85c6SGavin Shan static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
999fe7e85c6SGavin Shan 					      struct pci_dev *pdev)
1000fe7e85c6SGavin Shan {
1001fe7e85c6SGavin Shan 	struct pci_dn *pdn = pci_get_pdn(pdev);
1002fe7e85c6SGavin Shan 	struct pnv_ioda_pe *pe;
1003fe7e85c6SGavin Shan 	u64 end, mask;
1004fe7e85c6SGavin Shan 
1005fe7e85c6SGavin Shan 	if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1006fe7e85c6SGavin Shan 		return 0;
1007fe7e85c6SGavin Shan 
1008fe7e85c6SGavin Shan 	pe = &phb->ioda.pe_array[pdn->pe_number];
1009fe7e85c6SGavin Shan 	if (!pe->tce_bypass_enabled)
1010fe7e85c6SGavin Shan 		return __dma_get_required_mask(&pdev->dev);
1011fe7e85c6SGavin Shan 
1012fe7e85c6SGavin Shan 
1013fe7e85c6SGavin Shan 	end = pe->tce_bypass_base + memblock_end_of_DRAM();
1014fe7e85c6SGavin Shan 	mask = 1ULL << (fls64(end) - 1);
1015fe7e85c6SGavin Shan 	mask += mask - 1;
1016fe7e85c6SGavin Shan 
1017fe7e85c6SGavin Shan 	return mask;
1018fe7e85c6SGavin Shan }
1019fe7e85c6SGavin Shan 
1020dff4a39eSGavin Shan static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
1021dff4a39eSGavin Shan 				   struct pci_bus *bus,
1022dff4a39eSGavin Shan 				   bool add_to_iommu_group)
102374251fe2SBenjamin Herrenschmidt {
102474251fe2SBenjamin Herrenschmidt 	struct pci_dev *dev;
102574251fe2SBenjamin Herrenschmidt 
102674251fe2SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
1027dff4a39eSGavin Shan 		if (add_to_iommu_group)
1028dff4a39eSGavin Shan 			set_iommu_table_base_and_group(&dev->dev,
1029dff4a39eSGavin Shan 						       &pe->tce32_table);
1030dff4a39eSGavin Shan 		else
1031dff4a39eSGavin Shan 			set_iommu_table_base(&dev->dev, &pe->tce32_table);
1032dff4a39eSGavin Shan 
103374251fe2SBenjamin Herrenschmidt 		if (dev->subordinate)
1034dff4a39eSGavin Shan 			pnv_ioda_setup_bus_dma(pe, dev->subordinate,
1035dff4a39eSGavin Shan 					       add_to_iommu_group);
103674251fe2SBenjamin Herrenschmidt 	}
103774251fe2SBenjamin Herrenschmidt }
103874251fe2SBenjamin Herrenschmidt 
10398e0a1611SAlexey Kardashevskiy static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
10408e0a1611SAlexey Kardashevskiy 					 struct iommu_table *tbl,
10413ad26e5cSBenjamin Herrenschmidt 					 __be64 *startp, __be64 *endp, bool rm)
10424cce9550SGavin Shan {
10433ad26e5cSBenjamin Herrenschmidt 	__be64 __iomem *invalidate = rm ?
10443ad26e5cSBenjamin Herrenschmidt 		(__be64 __iomem *)pe->tce_inval_reg_phys :
10453ad26e5cSBenjamin Herrenschmidt 		(__be64 __iomem *)tbl->it_index;
10464cce9550SGavin Shan 	unsigned long start, end, inc;
1047b0376c9bSAlexey Kardashevskiy 	const unsigned shift = tbl->it_page_shift;
10484cce9550SGavin Shan 
10494cce9550SGavin Shan 	start = __pa(startp);
10504cce9550SGavin Shan 	end = __pa(endp);
10514cce9550SGavin Shan 
10524cce9550SGavin Shan 	/* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
10534cce9550SGavin Shan 	if (tbl->it_busno) {
1054b0376c9bSAlexey Kardashevskiy 		start <<= shift;
1055b0376c9bSAlexey Kardashevskiy 		end <<= shift;
1056b0376c9bSAlexey Kardashevskiy 		inc = 128ull << shift;
10574cce9550SGavin Shan 		start |= tbl->it_busno;
10584cce9550SGavin Shan 		end |= tbl->it_busno;
10594cce9550SGavin Shan 	} else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
10604cce9550SGavin Shan 		/* p7ioc-style invalidation, 2 TCEs per write */
10614cce9550SGavin Shan 		start |= (1ull << 63);
10624cce9550SGavin Shan 		end |= (1ull << 63);
10634cce9550SGavin Shan 		inc = 16;
10644cce9550SGavin Shan         } else {
10654cce9550SGavin Shan 		/* Default (older HW) */
10664cce9550SGavin Shan                 inc = 128;
10674cce9550SGavin Shan 	}
10684cce9550SGavin Shan 
10694cce9550SGavin Shan         end |= inc - 1;	/* round up end to be different than start */
10704cce9550SGavin Shan 
10714cce9550SGavin Shan         mb(); /* Ensure above stores are visible */
10724cce9550SGavin Shan         while (start <= end) {
10738e0a1611SAlexey Kardashevskiy 		if (rm)
10743ad26e5cSBenjamin Herrenschmidt 			__raw_rm_writeq(cpu_to_be64(start), invalidate);
10758e0a1611SAlexey Kardashevskiy 		else
10763a1a4661SBenjamin Herrenschmidt 			__raw_writeq(cpu_to_be64(start), invalidate);
10774cce9550SGavin Shan                 start += inc;
10784cce9550SGavin Shan         }
10794cce9550SGavin Shan 
10804cce9550SGavin Shan 	/*
10814cce9550SGavin Shan 	 * The iommu layer will do another mb() for us on build()
10824cce9550SGavin Shan 	 * and we don't care on free()
10834cce9550SGavin Shan 	 */
10844cce9550SGavin Shan }
10854cce9550SGavin Shan 
10864cce9550SGavin Shan static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
10874cce9550SGavin Shan 					 struct iommu_table *tbl,
10883ad26e5cSBenjamin Herrenschmidt 					 __be64 *startp, __be64 *endp, bool rm)
10894cce9550SGavin Shan {
10904cce9550SGavin Shan 	unsigned long start, end, inc;
10913ad26e5cSBenjamin Herrenschmidt 	__be64 __iomem *invalidate = rm ?
10923ad26e5cSBenjamin Herrenschmidt 		(__be64 __iomem *)pe->tce_inval_reg_phys :
10933ad26e5cSBenjamin Herrenschmidt 		(__be64 __iomem *)tbl->it_index;
1094b0376c9bSAlexey Kardashevskiy 	const unsigned shift = tbl->it_page_shift;
10954cce9550SGavin Shan 
10964cce9550SGavin Shan 	/* We'll invalidate DMA address in PE scope */
1097b0376c9bSAlexey Kardashevskiy 	start = 0x2ull << 60;
10984cce9550SGavin Shan 	start |= (pe->pe_number & 0xFF);
10994cce9550SGavin Shan 	end = start;
11004cce9550SGavin Shan 
11014cce9550SGavin Shan 	/* Figure out the start, end and step */
11024cce9550SGavin Shan 	inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
1103b0376c9bSAlexey Kardashevskiy 	start |= (inc << shift);
11044cce9550SGavin Shan 	inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
1105b0376c9bSAlexey Kardashevskiy 	end |= (inc << shift);
1106b0376c9bSAlexey Kardashevskiy 	inc = (0x1ull << shift);
11074cce9550SGavin Shan 	mb();
11084cce9550SGavin Shan 
11094cce9550SGavin Shan 	while (start <= end) {
11108e0a1611SAlexey Kardashevskiy 		if (rm)
11113ad26e5cSBenjamin Herrenschmidt 			__raw_rm_writeq(cpu_to_be64(start), invalidate);
11128e0a1611SAlexey Kardashevskiy 		else
11133a1a4661SBenjamin Herrenschmidt 			__raw_writeq(cpu_to_be64(start), invalidate);
11144cce9550SGavin Shan 		start += inc;
11154cce9550SGavin Shan 	}
11164cce9550SGavin Shan }
11174cce9550SGavin Shan 
11184cce9550SGavin Shan void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
11193ad26e5cSBenjamin Herrenschmidt 				 __be64 *startp, __be64 *endp, bool rm)
11204cce9550SGavin Shan {
11214cce9550SGavin Shan 	struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
11224cce9550SGavin Shan 					      tce32_table);
11234cce9550SGavin Shan 	struct pnv_phb *phb = pe->phb;
11244cce9550SGavin Shan 
11254cce9550SGavin Shan 	if (phb->type == PNV_PHB_IODA1)
11268e0a1611SAlexey Kardashevskiy 		pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
11274cce9550SGavin Shan 	else
11288e0a1611SAlexey Kardashevskiy 		pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
11294cce9550SGavin Shan }
11304cce9550SGavin Shan 
1131cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1132cad5cef6SGreg Kroah-Hartman 				      struct pnv_ioda_pe *pe, unsigned int base,
1133184cd4a3SBenjamin Herrenschmidt 				      unsigned int segs)
1134184cd4a3SBenjamin Herrenschmidt {
1135184cd4a3SBenjamin Herrenschmidt 
1136184cd4a3SBenjamin Herrenschmidt 	struct page *tce_mem = NULL;
1137184cd4a3SBenjamin Herrenschmidt 	const __be64 *swinvp;
1138184cd4a3SBenjamin Herrenschmidt 	struct iommu_table *tbl;
1139184cd4a3SBenjamin Herrenschmidt 	unsigned int i;
1140184cd4a3SBenjamin Herrenschmidt 	int64_t rc;
1141184cd4a3SBenjamin Herrenschmidt 	void *addr;
1142184cd4a3SBenjamin Herrenschmidt 
1143184cd4a3SBenjamin Herrenschmidt 	/* 256M DMA window, 4K TCE pages, 8 bytes TCE */
1144184cd4a3SBenjamin Herrenschmidt #define TCE32_TABLE_SIZE	((0x10000000 / 0x1000) * 8)
1145184cd4a3SBenjamin Herrenschmidt 
1146184cd4a3SBenjamin Herrenschmidt 	/* XXX FIXME: Handle 64-bit only DMA devices */
1147184cd4a3SBenjamin Herrenschmidt 	/* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1148184cd4a3SBenjamin Herrenschmidt 	/* XXX FIXME: Allocate multi-level tables on PHB3 */
1149184cd4a3SBenjamin Herrenschmidt 
1150184cd4a3SBenjamin Herrenschmidt 	/* We shouldn't already have a 32-bit DMA associated */
1151184cd4a3SBenjamin Herrenschmidt 	if (WARN_ON(pe->tce32_seg >= 0))
1152184cd4a3SBenjamin Herrenschmidt 		return;
1153184cd4a3SBenjamin Herrenschmidt 
1154184cd4a3SBenjamin Herrenschmidt 	/* Grab a 32-bit TCE table */
1155184cd4a3SBenjamin Herrenschmidt 	pe->tce32_seg = base;
1156184cd4a3SBenjamin Herrenschmidt 	pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1157184cd4a3SBenjamin Herrenschmidt 		(base << 28), ((base + segs) << 28) - 1);
1158184cd4a3SBenjamin Herrenschmidt 
1159184cd4a3SBenjamin Herrenschmidt 	/* XXX Currently, we allocate one big contiguous table for the
1160184cd4a3SBenjamin Herrenschmidt 	 * TCEs. We only really need one chunk per 256M of TCE space
1161184cd4a3SBenjamin Herrenschmidt 	 * (ie per segment) but that's an optimization for later, it
1162184cd4a3SBenjamin Herrenschmidt 	 * requires some added smarts with our get/put_tce implementation
1163184cd4a3SBenjamin Herrenschmidt 	 */
1164184cd4a3SBenjamin Herrenschmidt 	tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1165184cd4a3SBenjamin Herrenschmidt 				   get_order(TCE32_TABLE_SIZE * segs));
1166184cd4a3SBenjamin Herrenschmidt 	if (!tce_mem) {
1167184cd4a3SBenjamin Herrenschmidt 		pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1168184cd4a3SBenjamin Herrenschmidt 		goto fail;
1169184cd4a3SBenjamin Herrenschmidt 	}
1170184cd4a3SBenjamin Herrenschmidt 	addr = page_address(tce_mem);
1171184cd4a3SBenjamin Herrenschmidt 	memset(addr, 0, TCE32_TABLE_SIZE * segs);
1172184cd4a3SBenjamin Herrenschmidt 
1173184cd4a3SBenjamin Herrenschmidt 	/* Configure HW */
1174184cd4a3SBenjamin Herrenschmidt 	for (i = 0; i < segs; i++) {
1175184cd4a3SBenjamin Herrenschmidt 		rc = opal_pci_map_pe_dma_window(phb->opal_id,
1176184cd4a3SBenjamin Herrenschmidt 					      pe->pe_number,
1177184cd4a3SBenjamin Herrenschmidt 					      base + i, 1,
1178184cd4a3SBenjamin Herrenschmidt 					      __pa(addr) + TCE32_TABLE_SIZE * i,
1179184cd4a3SBenjamin Herrenschmidt 					      TCE32_TABLE_SIZE, 0x1000);
1180184cd4a3SBenjamin Herrenschmidt 		if (rc) {
1181184cd4a3SBenjamin Herrenschmidt 			pe_err(pe, " Failed to configure 32-bit TCE table,"
1182184cd4a3SBenjamin Herrenschmidt 			       " err %ld\n", rc);
1183184cd4a3SBenjamin Herrenschmidt 			goto fail;
1184184cd4a3SBenjamin Herrenschmidt 		}
1185184cd4a3SBenjamin Herrenschmidt 	}
1186184cd4a3SBenjamin Herrenschmidt 
1187184cd4a3SBenjamin Herrenschmidt 	/* Setup linux iommu table */
1188184cd4a3SBenjamin Herrenschmidt 	tbl = &pe->tce32_table;
1189184cd4a3SBenjamin Herrenschmidt 	pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
11908fa5d454SAlexey Kardashevskiy 				  base << 28, IOMMU_PAGE_SHIFT_4K);
1191184cd4a3SBenjamin Herrenschmidt 
1192184cd4a3SBenjamin Herrenschmidt 	/* OPAL variant of P7IOC SW invalidated TCEs */
1193184cd4a3SBenjamin Herrenschmidt 	swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1194184cd4a3SBenjamin Herrenschmidt 	if (swinvp) {
1195184cd4a3SBenjamin Herrenschmidt 		/* We need a couple more fields -- an address and a data
1196184cd4a3SBenjamin Herrenschmidt 		 * to or.  Since the bus is only printed out on table free
1197184cd4a3SBenjamin Herrenschmidt 		 * errors, and on the first pass the data will be a relative
1198184cd4a3SBenjamin Herrenschmidt 		 * bus number, print that out instead.
1199184cd4a3SBenjamin Herrenschmidt 		 */
12008e0a1611SAlexey Kardashevskiy 		pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
12018e0a1611SAlexey Kardashevskiy 		tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
12028e0a1611SAlexey Kardashevskiy 				8);
120365fd766bSGavin Shan 		tbl->it_type |= (TCE_PCI_SWINV_CREATE |
120465fd766bSGavin Shan 				 TCE_PCI_SWINV_FREE   |
120565fd766bSGavin Shan 				 TCE_PCI_SWINV_PAIR);
1206184cd4a3SBenjamin Herrenschmidt 	}
1207184cd4a3SBenjamin Herrenschmidt 	iommu_init_table(tbl, phb->hose->node);
1208e9bc03feSGavin Shan 	iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
1209184cd4a3SBenjamin Herrenschmidt 
121074251fe2SBenjamin Herrenschmidt 	if (pe->pdev)
1211d905c5dfSAlexey Kardashevskiy 		set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
121274251fe2SBenjamin Herrenschmidt 	else
1213dff4a39eSGavin Shan 		pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
121474251fe2SBenjamin Herrenschmidt 
1215184cd4a3SBenjamin Herrenschmidt 	return;
1216184cd4a3SBenjamin Herrenschmidt  fail:
1217184cd4a3SBenjamin Herrenschmidt 	/* XXX Failure: Try to fallback to 64-bit only ? */
1218184cd4a3SBenjamin Herrenschmidt 	if (pe->tce32_seg >= 0)
1219184cd4a3SBenjamin Herrenschmidt 		pe->tce32_seg = -1;
1220184cd4a3SBenjamin Herrenschmidt 	if (tce_mem)
1221184cd4a3SBenjamin Herrenschmidt 		__free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
1222184cd4a3SBenjamin Herrenschmidt }
1223184cd4a3SBenjamin Herrenschmidt 
1224cd15b048SBenjamin Herrenschmidt static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
1225cd15b048SBenjamin Herrenschmidt {
1226cd15b048SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
1227cd15b048SBenjamin Herrenschmidt 					      tce32_table);
1228cd15b048SBenjamin Herrenschmidt 	uint16_t window_id = (pe->pe_number << 1 ) + 1;
1229cd15b048SBenjamin Herrenschmidt 	int64_t rc;
1230cd15b048SBenjamin Herrenschmidt 
1231cd15b048SBenjamin Herrenschmidt 	pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1232cd15b048SBenjamin Herrenschmidt 	if (enable) {
1233cd15b048SBenjamin Herrenschmidt 		phys_addr_t top = memblock_end_of_DRAM();
1234cd15b048SBenjamin Herrenschmidt 
1235cd15b048SBenjamin Herrenschmidt 		top = roundup_pow_of_two(top);
1236cd15b048SBenjamin Herrenschmidt 		rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1237cd15b048SBenjamin Herrenschmidt 						     pe->pe_number,
1238cd15b048SBenjamin Herrenschmidt 						     window_id,
1239cd15b048SBenjamin Herrenschmidt 						     pe->tce_bypass_base,
1240cd15b048SBenjamin Herrenschmidt 						     top);
1241cd15b048SBenjamin Herrenschmidt 	} else {
1242cd15b048SBenjamin Herrenschmidt 		rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1243cd15b048SBenjamin Herrenschmidt 						     pe->pe_number,
1244cd15b048SBenjamin Herrenschmidt 						     window_id,
1245cd15b048SBenjamin Herrenschmidt 						     pe->tce_bypass_base,
1246cd15b048SBenjamin Herrenschmidt 						     0);
1247cd15b048SBenjamin Herrenschmidt 
1248cd15b048SBenjamin Herrenschmidt 		/*
1249dff4a39eSGavin Shan 		 * EEH needs the mapping between IOMMU table and group
1250dff4a39eSGavin Shan 		 * of those VFIO/KVM pass-through devices. We can postpone
1251dff4a39eSGavin Shan 		 * resetting DMA ops until the DMA mask is configured in
1252dff4a39eSGavin Shan 		 * host side.
1253cd15b048SBenjamin Herrenschmidt 		 */
1254dff4a39eSGavin Shan 		if (pe->pdev)
1255dff4a39eSGavin Shan 			set_iommu_table_base(&pe->pdev->dev, tbl);
1256dff4a39eSGavin Shan 		else
1257dff4a39eSGavin Shan 			pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
1258cd15b048SBenjamin Herrenschmidt 	}
1259cd15b048SBenjamin Herrenschmidt 	if (rc)
1260cd15b048SBenjamin Herrenschmidt 		pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1261cd15b048SBenjamin Herrenschmidt 	else
1262cd15b048SBenjamin Herrenschmidt 		pe->tce_bypass_enabled = enable;
1263cd15b048SBenjamin Herrenschmidt }
1264cd15b048SBenjamin Herrenschmidt 
1265cd15b048SBenjamin Herrenschmidt static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
1266cd15b048SBenjamin Herrenschmidt 					  struct pnv_ioda_pe *pe)
1267cd15b048SBenjamin Herrenschmidt {
1268cd15b048SBenjamin Herrenschmidt 	/* TVE #1 is selected by PCI address bit 59 */
1269cd15b048SBenjamin Herrenschmidt 	pe->tce_bypass_base = 1ull << 59;
1270cd15b048SBenjamin Herrenschmidt 
1271cd15b048SBenjamin Herrenschmidt 	/* Install set_bypass callback for VFIO */
1272cd15b048SBenjamin Herrenschmidt 	pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass;
1273cd15b048SBenjamin Herrenschmidt 
1274cd15b048SBenjamin Herrenschmidt 	/* Enable bypass by default */
1275cd15b048SBenjamin Herrenschmidt 	pnv_pci_ioda2_set_bypass(&pe->tce32_table, true);
1276cd15b048SBenjamin Herrenschmidt }
1277cd15b048SBenjamin Herrenschmidt 
1278373f5657SGavin Shan static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1279373f5657SGavin Shan 				       struct pnv_ioda_pe *pe)
1280373f5657SGavin Shan {
1281373f5657SGavin Shan 	struct page *tce_mem = NULL;
1282373f5657SGavin Shan 	void *addr;
1283373f5657SGavin Shan 	const __be64 *swinvp;
1284373f5657SGavin Shan 	struct iommu_table *tbl;
1285373f5657SGavin Shan 	unsigned int tce_table_size, end;
1286373f5657SGavin Shan 	int64_t rc;
1287373f5657SGavin Shan 
1288373f5657SGavin Shan 	/* We shouldn't already have a 32-bit DMA associated */
1289373f5657SGavin Shan 	if (WARN_ON(pe->tce32_seg >= 0))
1290373f5657SGavin Shan 		return;
1291373f5657SGavin Shan 
1292373f5657SGavin Shan 	/* The PE will reserve all possible 32-bits space */
1293373f5657SGavin Shan 	pe->tce32_seg = 0;
1294373f5657SGavin Shan 	end = (1 << ilog2(phb->ioda.m32_pci_base));
1295373f5657SGavin Shan 	tce_table_size = (end / 0x1000) * 8;
1296373f5657SGavin Shan 	pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1297373f5657SGavin Shan 		end);
1298373f5657SGavin Shan 
1299373f5657SGavin Shan 	/* Allocate TCE table */
1300373f5657SGavin Shan 	tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1301373f5657SGavin Shan 				   get_order(tce_table_size));
1302373f5657SGavin Shan 	if (!tce_mem) {
1303373f5657SGavin Shan 		pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
1304373f5657SGavin Shan 		goto fail;
1305373f5657SGavin Shan 	}
1306373f5657SGavin Shan 	addr = page_address(tce_mem);
1307373f5657SGavin Shan 	memset(addr, 0, tce_table_size);
1308373f5657SGavin Shan 
1309373f5657SGavin Shan 	/*
1310373f5657SGavin Shan 	 * Map TCE table through TVT. The TVE index is the PE number
1311373f5657SGavin Shan 	 * shifted by 1 bit for 32-bits DMA space.
1312373f5657SGavin Shan 	 */
1313373f5657SGavin Shan 	rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1314373f5657SGavin Shan 					pe->pe_number << 1, 1, __pa(addr),
1315373f5657SGavin Shan 					tce_table_size, 0x1000);
1316373f5657SGavin Shan 	if (rc) {
1317373f5657SGavin Shan 		pe_err(pe, "Failed to configure 32-bit TCE table,"
1318373f5657SGavin Shan 		       " err %ld\n", rc);
1319373f5657SGavin Shan 		goto fail;
1320373f5657SGavin Shan 	}
1321373f5657SGavin Shan 
1322373f5657SGavin Shan 	/* Setup linux iommu table */
1323373f5657SGavin Shan 	tbl = &pe->tce32_table;
13248fa5d454SAlexey Kardashevskiy 	pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
13258fa5d454SAlexey Kardashevskiy 			IOMMU_PAGE_SHIFT_4K);
1326373f5657SGavin Shan 
1327373f5657SGavin Shan 	/* OPAL variant of PHB3 invalidated TCEs */
1328373f5657SGavin Shan 	swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1329373f5657SGavin Shan 	if (swinvp) {
1330373f5657SGavin Shan 		/* We need a couple more fields -- an address and a data
1331373f5657SGavin Shan 		 * to or.  Since the bus is only printed out on table free
1332373f5657SGavin Shan 		 * errors, and on the first pass the data will be a relative
1333373f5657SGavin Shan 		 * bus number, print that out instead.
1334373f5657SGavin Shan 		 */
13358e0a1611SAlexey Kardashevskiy 		pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
13368e0a1611SAlexey Kardashevskiy 		tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
13378e0a1611SAlexey Kardashevskiy 				8);
133865fd766bSGavin Shan 		tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
1339373f5657SGavin Shan 	}
1340373f5657SGavin Shan 	iommu_init_table(tbl, phb->hose->node);
1341e9bc03feSGavin Shan 	iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
1342373f5657SGavin Shan 
134374251fe2SBenjamin Herrenschmidt 	if (pe->pdev)
1344d905c5dfSAlexey Kardashevskiy 		set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
134574251fe2SBenjamin Herrenschmidt 	else
1346dff4a39eSGavin Shan 		pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
134774251fe2SBenjamin Herrenschmidt 
1348cd15b048SBenjamin Herrenschmidt 	/* Also create a bypass window */
1349cd15b048SBenjamin Herrenschmidt 	pnv_pci_ioda2_setup_bypass_pe(phb, pe);
1350373f5657SGavin Shan 	return;
1351373f5657SGavin Shan fail:
1352373f5657SGavin Shan 	if (pe->tce32_seg >= 0)
1353373f5657SGavin Shan 		pe->tce32_seg = -1;
1354373f5657SGavin Shan 	if (tce_mem)
1355373f5657SGavin Shan 		__free_pages(tce_mem, get_order(tce_table_size));
1356373f5657SGavin Shan }
1357373f5657SGavin Shan 
1358cad5cef6SGreg Kroah-Hartman static void pnv_ioda_setup_dma(struct pnv_phb *phb)
1359184cd4a3SBenjamin Herrenschmidt {
1360184cd4a3SBenjamin Herrenschmidt 	struct pci_controller *hose = phb->hose;
1361184cd4a3SBenjamin Herrenschmidt 	unsigned int residual, remaining, segs, tw, base;
1362184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe;
1363184cd4a3SBenjamin Herrenschmidt 
1364184cd4a3SBenjamin Herrenschmidt 	/* If we have more PE# than segments available, hand out one
1365184cd4a3SBenjamin Herrenschmidt 	 * per PE until we run out and let the rest fail. If not,
1366184cd4a3SBenjamin Herrenschmidt 	 * then we assign at least one segment per PE, plus more based
1367184cd4a3SBenjamin Herrenschmidt 	 * on the amount of devices under that PE
1368184cd4a3SBenjamin Herrenschmidt 	 */
1369184cd4a3SBenjamin Herrenschmidt 	if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
1370184cd4a3SBenjamin Herrenschmidt 		residual = 0;
1371184cd4a3SBenjamin Herrenschmidt 	else
1372184cd4a3SBenjamin Herrenschmidt 		residual = phb->ioda.tce32_count -
1373184cd4a3SBenjamin Herrenschmidt 			phb->ioda.dma_pe_count;
1374184cd4a3SBenjamin Herrenschmidt 
1375184cd4a3SBenjamin Herrenschmidt 	pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
1376184cd4a3SBenjamin Herrenschmidt 		hose->global_number, phb->ioda.tce32_count);
1377184cd4a3SBenjamin Herrenschmidt 	pr_info("PCI: %d PE# for a total weight of %d\n",
1378184cd4a3SBenjamin Herrenschmidt 		phb->ioda.dma_pe_count, phb->ioda.dma_weight);
1379184cd4a3SBenjamin Herrenschmidt 
1380184cd4a3SBenjamin Herrenschmidt 	/* Walk our PE list and configure their DMA segments, hand them
1381184cd4a3SBenjamin Herrenschmidt 	 * out one base segment plus any residual segments based on
1382184cd4a3SBenjamin Herrenschmidt 	 * weight
1383184cd4a3SBenjamin Herrenschmidt 	 */
1384184cd4a3SBenjamin Herrenschmidt 	remaining = phb->ioda.tce32_count;
1385184cd4a3SBenjamin Herrenschmidt 	tw = phb->ioda.dma_weight;
1386184cd4a3SBenjamin Herrenschmidt 	base = 0;
13877ebdf956SGavin Shan 	list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
1388184cd4a3SBenjamin Herrenschmidt 		if (!pe->dma_weight)
1389184cd4a3SBenjamin Herrenschmidt 			continue;
1390184cd4a3SBenjamin Herrenschmidt 		if (!remaining) {
1391184cd4a3SBenjamin Herrenschmidt 			pe_warn(pe, "No DMA32 resources available\n");
1392184cd4a3SBenjamin Herrenschmidt 			continue;
1393184cd4a3SBenjamin Herrenschmidt 		}
1394184cd4a3SBenjamin Herrenschmidt 		segs = 1;
1395184cd4a3SBenjamin Herrenschmidt 		if (residual) {
1396184cd4a3SBenjamin Herrenschmidt 			segs += ((pe->dma_weight * residual)  + (tw / 2)) / tw;
1397184cd4a3SBenjamin Herrenschmidt 			if (segs > remaining)
1398184cd4a3SBenjamin Herrenschmidt 				segs = remaining;
1399184cd4a3SBenjamin Herrenschmidt 		}
1400373f5657SGavin Shan 
1401373f5657SGavin Shan 		/*
1402373f5657SGavin Shan 		 * For IODA2 compliant PHB3, we needn't care about the weight.
1403373f5657SGavin Shan 		 * The all available 32-bits DMA space will be assigned to
1404373f5657SGavin Shan 		 * the specific PE.
1405373f5657SGavin Shan 		 */
1406373f5657SGavin Shan 		if (phb->type == PNV_PHB_IODA1) {
1407184cd4a3SBenjamin Herrenschmidt 			pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1408184cd4a3SBenjamin Herrenschmidt 				pe->dma_weight, segs);
1409184cd4a3SBenjamin Herrenschmidt 			pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1410373f5657SGavin Shan 		} else {
1411373f5657SGavin Shan 			pe_info(pe, "Assign DMA32 space\n");
1412373f5657SGavin Shan 			segs = 0;
1413373f5657SGavin Shan 			pnv_pci_ioda2_setup_dma_pe(phb, pe);
1414373f5657SGavin Shan 		}
1415373f5657SGavin Shan 
1416184cd4a3SBenjamin Herrenschmidt 		remaining -= segs;
1417184cd4a3SBenjamin Herrenschmidt 		base += segs;
1418184cd4a3SBenjamin Herrenschmidt 	}
1419184cd4a3SBenjamin Herrenschmidt }
1420184cd4a3SBenjamin Herrenschmidt 
1421184cd4a3SBenjamin Herrenschmidt #ifdef CONFIG_PCI_MSI
1422137436c9SGavin Shan static void pnv_ioda2_msi_eoi(struct irq_data *d)
1423137436c9SGavin Shan {
1424137436c9SGavin Shan 	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1425137436c9SGavin Shan 	struct irq_chip *chip = irq_data_get_irq_chip(d);
1426137436c9SGavin Shan 	struct pnv_phb *phb = container_of(chip, struct pnv_phb,
1427137436c9SGavin Shan 					   ioda.irq_chip);
1428137436c9SGavin Shan 	int64_t rc;
1429137436c9SGavin Shan 
1430137436c9SGavin Shan 	rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
1431137436c9SGavin Shan 	WARN_ON_ONCE(rc);
1432137436c9SGavin Shan 
1433137436c9SGavin Shan 	icp_native_eoi(d);
1434137436c9SGavin Shan }
1435137436c9SGavin Shan 
1436fd9a1c26SIan Munsie 
1437fd9a1c26SIan Munsie static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
1438fd9a1c26SIan Munsie {
1439fd9a1c26SIan Munsie 	struct irq_data *idata;
1440fd9a1c26SIan Munsie 	struct irq_chip *ichip;
1441fd9a1c26SIan Munsie 
1442fd9a1c26SIan Munsie 	if (phb->type != PNV_PHB_IODA2)
1443fd9a1c26SIan Munsie 		return;
1444fd9a1c26SIan Munsie 
1445fd9a1c26SIan Munsie 	if (!phb->ioda.irq_chip_init) {
1446fd9a1c26SIan Munsie 		/*
1447fd9a1c26SIan Munsie 		 * First time we setup an MSI IRQ, we need to setup the
1448fd9a1c26SIan Munsie 		 * corresponding IRQ chip to route correctly.
1449fd9a1c26SIan Munsie 		 */
1450fd9a1c26SIan Munsie 		idata = irq_get_irq_data(virq);
1451fd9a1c26SIan Munsie 		ichip = irq_data_get_irq_chip(idata);
1452fd9a1c26SIan Munsie 		phb->ioda.irq_chip_init = 1;
1453fd9a1c26SIan Munsie 		phb->ioda.irq_chip = *ichip;
1454fd9a1c26SIan Munsie 		phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
1455fd9a1c26SIan Munsie 	}
1456fd9a1c26SIan Munsie 	irq_set_chip(virq, &phb->ioda.irq_chip);
1457fd9a1c26SIan Munsie }
1458fd9a1c26SIan Munsie 
145980c49c7eSIan Munsie #ifdef CONFIG_CXL_BASE
146080c49c7eSIan Munsie 
146180c49c7eSIan Munsie struct device_node *pnv_pci_to_phb_node(struct pci_dev *dev)
146280c49c7eSIan Munsie {
146380c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
146480c49c7eSIan Munsie 
146580c49c7eSIan Munsie 	return hose->dn;
146680c49c7eSIan Munsie }
146780c49c7eSIan Munsie EXPORT_SYMBOL(pnv_pci_to_phb_node);
146880c49c7eSIan Munsie 
146980c49c7eSIan Munsie int pnv_phb_to_cxl(struct pci_dev *dev)
147080c49c7eSIan Munsie {
147180c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
147280c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
147380c49c7eSIan Munsie 	struct pnv_ioda_pe *pe;
147480c49c7eSIan Munsie 	int rc;
147580c49c7eSIan Munsie 
147680c49c7eSIan Munsie 	pe = pnv_ioda_get_pe(dev);
147780c49c7eSIan Munsie 	if (!pe)
147880c49c7eSIan Munsie 		return -ENODEV;
147980c49c7eSIan Munsie 
148080c49c7eSIan Munsie 	pe_info(pe, "Switching PHB to CXL\n");
148180c49c7eSIan Munsie 
148280c49c7eSIan Munsie 	rc = opal_pci_set_phb_cxl_mode(phb->opal_id, 1, pe->pe_number);
148380c49c7eSIan Munsie 	if (rc)
148480c49c7eSIan Munsie 		dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
148580c49c7eSIan Munsie 
148680c49c7eSIan Munsie 	return rc;
148780c49c7eSIan Munsie }
148880c49c7eSIan Munsie EXPORT_SYMBOL(pnv_phb_to_cxl);
148980c49c7eSIan Munsie 
149080c49c7eSIan Munsie /* Find PHB for cxl dev and allocate MSI hwirqs?
149180c49c7eSIan Munsie  * Returns the absolute hardware IRQ number
149280c49c7eSIan Munsie  */
149380c49c7eSIan Munsie int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
149480c49c7eSIan Munsie {
149580c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
149680c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
149780c49c7eSIan Munsie 	int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
149880c49c7eSIan Munsie 
149980c49c7eSIan Munsie 	if (hwirq < 0) {
150080c49c7eSIan Munsie 		dev_warn(&dev->dev, "Failed to find a free MSI\n");
150180c49c7eSIan Munsie 		return -ENOSPC;
150280c49c7eSIan Munsie 	}
150380c49c7eSIan Munsie 
150480c49c7eSIan Munsie 	return phb->msi_base + hwirq;
150580c49c7eSIan Munsie }
150680c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
150780c49c7eSIan Munsie 
150880c49c7eSIan Munsie void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
150980c49c7eSIan Munsie {
151080c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
151180c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
151280c49c7eSIan Munsie 
151380c49c7eSIan Munsie 	msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
151480c49c7eSIan Munsie }
151580c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
151680c49c7eSIan Munsie 
151780c49c7eSIan Munsie void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
151880c49c7eSIan Munsie 				  struct pci_dev *dev)
151980c49c7eSIan Munsie {
152080c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
152180c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
152280c49c7eSIan Munsie 	int i, hwirq;
152380c49c7eSIan Munsie 
152480c49c7eSIan Munsie 	for (i = 1; i < CXL_IRQ_RANGES; i++) {
152580c49c7eSIan Munsie 		if (!irqs->range[i])
152680c49c7eSIan Munsie 			continue;
152780c49c7eSIan Munsie 		pr_devel("cxl release irq range 0x%x: offset: 0x%lx  limit: %ld\n",
152880c49c7eSIan Munsie 			 i, irqs->offset[i],
152980c49c7eSIan Munsie 			 irqs->range[i]);
153080c49c7eSIan Munsie 		hwirq = irqs->offset[i] - phb->msi_base;
153180c49c7eSIan Munsie 		msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
153280c49c7eSIan Munsie 				       irqs->range[i]);
153380c49c7eSIan Munsie 	}
153480c49c7eSIan Munsie }
153580c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
153680c49c7eSIan Munsie 
153780c49c7eSIan Munsie int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
153880c49c7eSIan Munsie 			       struct pci_dev *dev, int num)
153980c49c7eSIan Munsie {
154080c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
154180c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
154280c49c7eSIan Munsie 	int i, hwirq, try;
154380c49c7eSIan Munsie 
154480c49c7eSIan Munsie 	memset(irqs, 0, sizeof(struct cxl_irq_ranges));
154580c49c7eSIan Munsie 
154680c49c7eSIan Munsie 	/* 0 is reserved for the multiplexed PSL DSI interrupt */
154780c49c7eSIan Munsie 	for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
154880c49c7eSIan Munsie 		try = num;
154980c49c7eSIan Munsie 		while (try) {
155080c49c7eSIan Munsie 			hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
155180c49c7eSIan Munsie 			if (hwirq >= 0)
155280c49c7eSIan Munsie 				break;
155380c49c7eSIan Munsie 			try /= 2;
155480c49c7eSIan Munsie 		}
155580c49c7eSIan Munsie 		if (!try)
155680c49c7eSIan Munsie 			goto fail;
155780c49c7eSIan Munsie 
155880c49c7eSIan Munsie 		irqs->offset[i] = phb->msi_base + hwirq;
155980c49c7eSIan Munsie 		irqs->range[i] = try;
156080c49c7eSIan Munsie 		pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx  limit: %li\n",
156180c49c7eSIan Munsie 			 i, irqs->offset[i], irqs->range[i]);
156280c49c7eSIan Munsie 		num -= try;
156380c49c7eSIan Munsie 	}
156480c49c7eSIan Munsie 	if (num)
156580c49c7eSIan Munsie 		goto fail;
156680c49c7eSIan Munsie 
156780c49c7eSIan Munsie 	return 0;
156880c49c7eSIan Munsie fail:
156980c49c7eSIan Munsie 	pnv_cxl_release_hwirq_ranges(irqs, dev);
157080c49c7eSIan Munsie 	return -ENOSPC;
157180c49c7eSIan Munsie }
157280c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
157380c49c7eSIan Munsie 
157480c49c7eSIan Munsie int pnv_cxl_get_irq_count(struct pci_dev *dev)
157580c49c7eSIan Munsie {
157680c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
157780c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
157880c49c7eSIan Munsie 
157980c49c7eSIan Munsie 	return phb->msi_bmp.irq_count;
158080c49c7eSIan Munsie }
158180c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_get_irq_count);
158280c49c7eSIan Munsie 
158380c49c7eSIan Munsie int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
158480c49c7eSIan Munsie 			   unsigned int virq)
158580c49c7eSIan Munsie {
158680c49c7eSIan Munsie 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
158780c49c7eSIan Munsie 	struct pnv_phb *phb = hose->private_data;
158880c49c7eSIan Munsie 	unsigned int xive_num = hwirq - phb->msi_base;
158980c49c7eSIan Munsie 	struct pnv_ioda_pe *pe;
159080c49c7eSIan Munsie 	int rc;
159180c49c7eSIan Munsie 
159280c49c7eSIan Munsie 	if (!(pe = pnv_ioda_get_pe(dev)))
159380c49c7eSIan Munsie 		return -ENODEV;
159480c49c7eSIan Munsie 
159580c49c7eSIan Munsie 	/* Assign XIVE to PE */
159680c49c7eSIan Munsie 	rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
159780c49c7eSIan Munsie 	if (rc) {
159880c49c7eSIan Munsie 		pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
159980c49c7eSIan Munsie 			"hwirq 0x%x XIVE 0x%x PE\n",
160080c49c7eSIan Munsie 			pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
160180c49c7eSIan Munsie 		return -EIO;
160280c49c7eSIan Munsie 	}
160380c49c7eSIan Munsie 	set_msi_irq_chip(phb, virq);
160480c49c7eSIan Munsie 
160580c49c7eSIan Munsie 	return 0;
160680c49c7eSIan Munsie }
160780c49c7eSIan Munsie EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
160880c49c7eSIan Munsie #endif
160980c49c7eSIan Munsie 
1610184cd4a3SBenjamin Herrenschmidt static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
1611137436c9SGavin Shan 				  unsigned int hwirq, unsigned int virq,
1612137436c9SGavin Shan 				  unsigned int is_64, struct msi_msg *msg)
1613184cd4a3SBenjamin Herrenschmidt {
1614184cd4a3SBenjamin Herrenschmidt 	struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1615b72c1f65SBenjamin Herrenschmidt 	struct pci_dn *pdn = pci_get_pdn(dev);
1616184cd4a3SBenjamin Herrenschmidt 	unsigned int xive_num = hwirq - phb->msi_base;
16173a1a4661SBenjamin Herrenschmidt 	__be32 data;
1618184cd4a3SBenjamin Herrenschmidt 	int rc;
1619184cd4a3SBenjamin Herrenschmidt 
1620184cd4a3SBenjamin Herrenschmidt 	/* No PE assigned ? bail out ... no MSI for you ! */
1621184cd4a3SBenjamin Herrenschmidt 	if (pe == NULL)
1622184cd4a3SBenjamin Herrenschmidt 		return -ENXIO;
1623184cd4a3SBenjamin Herrenschmidt 
1624184cd4a3SBenjamin Herrenschmidt 	/* Check if we have an MVE */
1625184cd4a3SBenjamin Herrenschmidt 	if (pe->mve_number < 0)
1626184cd4a3SBenjamin Herrenschmidt 		return -ENXIO;
1627184cd4a3SBenjamin Herrenschmidt 
1628b72c1f65SBenjamin Herrenschmidt 	/* Force 32-bit MSI on some broken devices */
1629b72c1f65SBenjamin Herrenschmidt 	if (pdn && pdn->force_32bit_msi)
1630b72c1f65SBenjamin Herrenschmidt 		is_64 = 0;
1631b72c1f65SBenjamin Herrenschmidt 
1632184cd4a3SBenjamin Herrenschmidt 	/* Assign XIVE to PE */
1633184cd4a3SBenjamin Herrenschmidt 	rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1634184cd4a3SBenjamin Herrenschmidt 	if (rc) {
1635184cd4a3SBenjamin Herrenschmidt 		pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1636184cd4a3SBenjamin Herrenschmidt 			pci_name(dev), rc, xive_num);
1637184cd4a3SBenjamin Herrenschmidt 		return -EIO;
1638184cd4a3SBenjamin Herrenschmidt 	}
1639184cd4a3SBenjamin Herrenschmidt 
1640184cd4a3SBenjamin Herrenschmidt 	if (is_64) {
16413a1a4661SBenjamin Herrenschmidt 		__be64 addr64;
16423a1a4661SBenjamin Herrenschmidt 
1643184cd4a3SBenjamin Herrenschmidt 		rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1644184cd4a3SBenjamin Herrenschmidt 				     &addr64, &data);
1645184cd4a3SBenjamin Herrenschmidt 		if (rc) {
1646184cd4a3SBenjamin Herrenschmidt 			pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1647184cd4a3SBenjamin Herrenschmidt 				pci_name(dev), rc);
1648184cd4a3SBenjamin Herrenschmidt 			return -EIO;
1649184cd4a3SBenjamin Herrenschmidt 		}
16503a1a4661SBenjamin Herrenschmidt 		msg->address_hi = be64_to_cpu(addr64) >> 32;
16513a1a4661SBenjamin Herrenschmidt 		msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
1652184cd4a3SBenjamin Herrenschmidt 	} else {
16533a1a4661SBenjamin Herrenschmidt 		__be32 addr32;
16543a1a4661SBenjamin Herrenschmidt 
1655184cd4a3SBenjamin Herrenschmidt 		rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1656184cd4a3SBenjamin Herrenschmidt 				     &addr32, &data);
1657184cd4a3SBenjamin Herrenschmidt 		if (rc) {
1658184cd4a3SBenjamin Herrenschmidt 			pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1659184cd4a3SBenjamin Herrenschmidt 				pci_name(dev), rc);
1660184cd4a3SBenjamin Herrenschmidt 			return -EIO;
1661184cd4a3SBenjamin Herrenschmidt 		}
1662184cd4a3SBenjamin Herrenschmidt 		msg->address_hi = 0;
16633a1a4661SBenjamin Herrenschmidt 		msg->address_lo = be32_to_cpu(addr32);
1664184cd4a3SBenjamin Herrenschmidt 	}
16653a1a4661SBenjamin Herrenschmidt 	msg->data = be32_to_cpu(data);
1666184cd4a3SBenjamin Herrenschmidt 
1667fd9a1c26SIan Munsie 	set_msi_irq_chip(phb, virq);
1668137436c9SGavin Shan 
1669184cd4a3SBenjamin Herrenschmidt 	pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1670184cd4a3SBenjamin Herrenschmidt 		 " address=%x_%08x data=%x PE# %d\n",
1671184cd4a3SBenjamin Herrenschmidt 		 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
1672184cd4a3SBenjamin Herrenschmidt 		 msg->address_hi, msg->address_lo, data, pe->pe_number);
1673184cd4a3SBenjamin Herrenschmidt 
1674184cd4a3SBenjamin Herrenschmidt 	return 0;
1675184cd4a3SBenjamin Herrenschmidt }
1676184cd4a3SBenjamin Herrenschmidt 
1677184cd4a3SBenjamin Herrenschmidt static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1678184cd4a3SBenjamin Herrenschmidt {
1679fb1b55d6SGavin Shan 	unsigned int count;
1680184cd4a3SBenjamin Herrenschmidt 	const __be32 *prop = of_get_property(phb->hose->dn,
1681184cd4a3SBenjamin Herrenschmidt 					     "ibm,opal-msi-ranges", NULL);
1682184cd4a3SBenjamin Herrenschmidt 	if (!prop) {
1683184cd4a3SBenjamin Herrenschmidt 		/* BML Fallback */
1684184cd4a3SBenjamin Herrenschmidt 		prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1685184cd4a3SBenjamin Herrenschmidt 	}
1686184cd4a3SBenjamin Herrenschmidt 	if (!prop)
1687184cd4a3SBenjamin Herrenschmidt 		return;
1688184cd4a3SBenjamin Herrenschmidt 
1689184cd4a3SBenjamin Herrenschmidt 	phb->msi_base = be32_to_cpup(prop);
1690fb1b55d6SGavin Shan 	count = be32_to_cpup(prop + 1);
1691fb1b55d6SGavin Shan 	if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
1692184cd4a3SBenjamin Herrenschmidt 		pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1693184cd4a3SBenjamin Herrenschmidt 		       phb->hose->global_number);
1694184cd4a3SBenjamin Herrenschmidt 		return;
1695184cd4a3SBenjamin Herrenschmidt 	}
1696fb1b55d6SGavin Shan 
1697184cd4a3SBenjamin Herrenschmidt 	phb->msi_setup = pnv_pci_ioda_msi_setup;
1698184cd4a3SBenjamin Herrenschmidt 	phb->msi32_support = 1;
1699184cd4a3SBenjamin Herrenschmidt 	pr_info("  Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
1700fb1b55d6SGavin Shan 		count, phb->msi_base);
1701184cd4a3SBenjamin Herrenschmidt }
1702184cd4a3SBenjamin Herrenschmidt #else
1703184cd4a3SBenjamin Herrenschmidt static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1704184cd4a3SBenjamin Herrenschmidt #endif /* CONFIG_PCI_MSI */
1705184cd4a3SBenjamin Herrenschmidt 
170611685becSGavin Shan /*
170711685becSGavin Shan  * This function is supposed to be called on basis of PE from top
170811685becSGavin Shan  * to bottom style. So the the I/O or MMIO segment assigned to
170911685becSGavin Shan  * parent PE could be overrided by its child PEs if necessary.
171011685becSGavin Shan  */
1711cad5cef6SGreg Kroah-Hartman static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
171211685becSGavin Shan 				  struct pnv_ioda_pe *pe)
171311685becSGavin Shan {
171411685becSGavin Shan 	struct pnv_phb *phb = hose->private_data;
171511685becSGavin Shan 	struct pci_bus_region region;
171611685becSGavin Shan 	struct resource *res;
171711685becSGavin Shan 	int i, index;
171811685becSGavin Shan 	int rc;
171911685becSGavin Shan 
172011685becSGavin Shan 	/*
172111685becSGavin Shan 	 * NOTE: We only care PCI bus based PE for now. For PCI
172211685becSGavin Shan 	 * device based PE, for example SRIOV sensitive VF should
172311685becSGavin Shan 	 * be figured out later.
172411685becSGavin Shan 	 */
172511685becSGavin Shan 	BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
172611685becSGavin Shan 
172711685becSGavin Shan 	pci_bus_for_each_resource(pe->pbus, res, i) {
172811685becSGavin Shan 		if (!res || !res->flags ||
172911685becSGavin Shan 		    res->start > res->end)
173011685becSGavin Shan 			continue;
173111685becSGavin Shan 
173211685becSGavin Shan 		if (res->flags & IORESOURCE_IO) {
173311685becSGavin Shan 			region.start = res->start - phb->ioda.io_pci_base;
173411685becSGavin Shan 			region.end   = res->end - phb->ioda.io_pci_base;
173511685becSGavin Shan 			index = region.start / phb->ioda.io_segsize;
173611685becSGavin Shan 
173711685becSGavin Shan 			while (index < phb->ioda.total_pe &&
173811685becSGavin Shan 			       region.start <= region.end) {
173911685becSGavin Shan 				phb->ioda.io_segmap[index] = pe->pe_number;
174011685becSGavin Shan 				rc = opal_pci_map_pe_mmio_window(phb->opal_id,
174111685becSGavin Shan 					pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
174211685becSGavin Shan 				if (rc != OPAL_SUCCESS) {
174311685becSGavin Shan 					pr_err("%s: OPAL error %d when mapping IO "
174411685becSGavin Shan 					       "segment #%d to PE#%d\n",
174511685becSGavin Shan 					       __func__, rc, index, pe->pe_number);
174611685becSGavin Shan 					break;
174711685becSGavin Shan 				}
174811685becSGavin Shan 
174911685becSGavin Shan 				region.start += phb->ioda.io_segsize;
175011685becSGavin Shan 				index++;
175111685becSGavin Shan 			}
175211685becSGavin Shan 		} else if (res->flags & IORESOURCE_MEM) {
175311685becSGavin Shan 			region.start = res->start -
17543fd47f06SBenjamin Herrenschmidt 				       hose->mem_offset[0] -
175511685becSGavin Shan 				       phb->ioda.m32_pci_base;
175611685becSGavin Shan 			region.end   = res->end -
17573fd47f06SBenjamin Herrenschmidt 				       hose->mem_offset[0] -
175811685becSGavin Shan 				       phb->ioda.m32_pci_base;
175911685becSGavin Shan 			index = region.start / phb->ioda.m32_segsize;
176011685becSGavin Shan 
176111685becSGavin Shan 			while (index < phb->ioda.total_pe &&
176211685becSGavin Shan 			       region.start <= region.end) {
176311685becSGavin Shan 				phb->ioda.m32_segmap[index] = pe->pe_number;
176411685becSGavin Shan 				rc = opal_pci_map_pe_mmio_window(phb->opal_id,
176511685becSGavin Shan 					pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
176611685becSGavin Shan 				if (rc != OPAL_SUCCESS) {
176711685becSGavin Shan 					pr_err("%s: OPAL error %d when mapping M32 "
176811685becSGavin Shan 					       "segment#%d to PE#%d",
176911685becSGavin Shan 					       __func__, rc, index, pe->pe_number);
177011685becSGavin Shan 					break;
177111685becSGavin Shan 				}
177211685becSGavin Shan 
177311685becSGavin Shan 				region.start += phb->ioda.m32_segsize;
177411685becSGavin Shan 				index++;
177511685becSGavin Shan 			}
177611685becSGavin Shan 		}
177711685becSGavin Shan 	}
177811685becSGavin Shan }
177911685becSGavin Shan 
1780cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_setup_seg(void)
178111685becSGavin Shan {
178211685becSGavin Shan 	struct pci_controller *tmp, *hose;
178311685becSGavin Shan 	struct pnv_phb *phb;
178411685becSGavin Shan 	struct pnv_ioda_pe *pe;
178511685becSGavin Shan 
178611685becSGavin Shan 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
178711685becSGavin Shan 		phb = hose->private_data;
178811685becSGavin Shan 		list_for_each_entry(pe, &phb->ioda.pe_list, list) {
178911685becSGavin Shan 			pnv_ioda_setup_pe_seg(hose, pe);
179011685becSGavin Shan 		}
179111685becSGavin Shan 	}
179211685becSGavin Shan }
179311685becSGavin Shan 
1794cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_setup_DMA(void)
179513395c48SGavin Shan {
179613395c48SGavin Shan 	struct pci_controller *hose, *tmp;
1797db1266c8SGavin Shan 	struct pnv_phb *phb;
179813395c48SGavin Shan 
179913395c48SGavin Shan 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
180013395c48SGavin Shan 		pnv_ioda_setup_dma(hose->private_data);
1801db1266c8SGavin Shan 
1802db1266c8SGavin Shan 		/* Mark the PHB initialization done */
1803db1266c8SGavin Shan 		phb = hose->private_data;
1804db1266c8SGavin Shan 		phb->initialized = 1;
180513395c48SGavin Shan 	}
180613395c48SGavin Shan }
180713395c48SGavin Shan 
180837c367f2SGavin Shan static void pnv_pci_ioda_create_dbgfs(void)
180937c367f2SGavin Shan {
181037c367f2SGavin Shan #ifdef CONFIG_DEBUG_FS
181137c367f2SGavin Shan 	struct pci_controller *hose, *tmp;
181237c367f2SGavin Shan 	struct pnv_phb *phb;
181337c367f2SGavin Shan 	char name[16];
181437c367f2SGavin Shan 
181537c367f2SGavin Shan 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
181637c367f2SGavin Shan 		phb = hose->private_data;
181737c367f2SGavin Shan 
181837c367f2SGavin Shan 		sprintf(name, "PCI%04x", hose->global_number);
181937c367f2SGavin Shan 		phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
182037c367f2SGavin Shan 		if (!phb->dbgfs)
182137c367f2SGavin Shan 			pr_warning("%s: Error on creating debugfs on PHB#%x\n",
182237c367f2SGavin Shan 				__func__, hose->global_number);
182337c367f2SGavin Shan 	}
182437c367f2SGavin Shan #endif /* CONFIG_DEBUG_FS */
182537c367f2SGavin Shan }
182637c367f2SGavin Shan 
1827cad5cef6SGreg Kroah-Hartman static void pnv_pci_ioda_fixup(void)
1828fb446ad0SGavin Shan {
1829fb446ad0SGavin Shan 	pnv_pci_ioda_setup_PEs();
183011685becSGavin Shan 	pnv_pci_ioda_setup_seg();
183113395c48SGavin Shan 	pnv_pci_ioda_setup_DMA();
1832e9cc17d4SGavin Shan 
183337c367f2SGavin Shan 	pnv_pci_ioda_create_dbgfs();
183437c367f2SGavin Shan 
1835e9cc17d4SGavin Shan #ifdef CONFIG_EEH
1836e9cc17d4SGavin Shan 	eeh_init();
1837dadcd6d6SMike Qiu 	eeh_addr_cache_build();
1838e9cc17d4SGavin Shan #endif
1839fb446ad0SGavin Shan }
1840fb446ad0SGavin Shan 
1841271fd03aSGavin Shan /*
1842271fd03aSGavin Shan  * Returns the alignment for I/O or memory windows for P2P
1843271fd03aSGavin Shan  * bridges. That actually depends on how PEs are segmented.
1844271fd03aSGavin Shan  * For now, we return I/O or M32 segment size for PE sensitive
1845271fd03aSGavin Shan  * P2P bridges. Otherwise, the default values (4KiB for I/O,
1846271fd03aSGavin Shan  * 1MiB for memory) will be returned.
1847271fd03aSGavin Shan  *
1848271fd03aSGavin Shan  * The current PCI bus might be put into one PE, which was
1849271fd03aSGavin Shan  * create against the parent PCI bridge. For that case, we
1850271fd03aSGavin Shan  * needn't enlarge the alignment so that we can save some
1851271fd03aSGavin Shan  * resources.
1852271fd03aSGavin Shan  */
1853271fd03aSGavin Shan static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
1854271fd03aSGavin Shan 						unsigned long type)
1855271fd03aSGavin Shan {
1856271fd03aSGavin Shan 	struct pci_dev *bridge;
1857271fd03aSGavin Shan 	struct pci_controller *hose = pci_bus_to_host(bus);
1858271fd03aSGavin Shan 	struct pnv_phb *phb = hose->private_data;
1859271fd03aSGavin Shan 	int num_pci_bridges = 0;
1860271fd03aSGavin Shan 
1861271fd03aSGavin Shan 	bridge = bus->self;
1862271fd03aSGavin Shan 	while (bridge) {
1863271fd03aSGavin Shan 		if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
1864271fd03aSGavin Shan 			num_pci_bridges++;
1865271fd03aSGavin Shan 			if (num_pci_bridges >= 2)
1866271fd03aSGavin Shan 				return 1;
1867271fd03aSGavin Shan 		}
1868271fd03aSGavin Shan 
1869271fd03aSGavin Shan 		bridge = bridge->bus->self;
1870271fd03aSGavin Shan 	}
1871271fd03aSGavin Shan 
1872262af557SGuo Chao 	/* We fail back to M32 if M64 isn't supported */
1873262af557SGuo Chao 	if (phb->ioda.m64_segsize &&
1874262af557SGuo Chao 	    pnv_pci_is_mem_pref_64(type))
1875262af557SGuo Chao 		return phb->ioda.m64_segsize;
1876271fd03aSGavin Shan 	if (type & IORESOURCE_MEM)
1877271fd03aSGavin Shan 		return phb->ioda.m32_segsize;
1878271fd03aSGavin Shan 
1879271fd03aSGavin Shan 	return phb->ioda.io_segsize;
1880271fd03aSGavin Shan }
1881271fd03aSGavin Shan 
1882184cd4a3SBenjamin Herrenschmidt /* Prevent enabling devices for which we couldn't properly
1883184cd4a3SBenjamin Herrenschmidt  * assign a PE
1884184cd4a3SBenjamin Herrenschmidt  */
1885cad5cef6SGreg Kroah-Hartman static int pnv_pci_enable_device_hook(struct pci_dev *dev)
1886184cd4a3SBenjamin Herrenschmidt {
1887db1266c8SGavin Shan 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
1888db1266c8SGavin Shan 	struct pnv_phb *phb = hose->private_data;
1889db1266c8SGavin Shan 	struct pci_dn *pdn;
1890184cd4a3SBenjamin Herrenschmidt 
1891db1266c8SGavin Shan 	/* The function is probably called while the PEs have
1892db1266c8SGavin Shan 	 * not be created yet. For example, resource reassignment
1893db1266c8SGavin Shan 	 * during PCI probe period. We just skip the check if
1894db1266c8SGavin Shan 	 * PEs isn't ready.
1895db1266c8SGavin Shan 	 */
1896db1266c8SGavin Shan 	if (!phb->initialized)
1897db1266c8SGavin Shan 		return 0;
1898db1266c8SGavin Shan 
1899b72c1f65SBenjamin Herrenschmidt 	pdn = pci_get_pdn(dev);
1900184cd4a3SBenjamin Herrenschmidt 	if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1901184cd4a3SBenjamin Herrenschmidt 		return -EINVAL;
1902db1266c8SGavin Shan 
1903184cd4a3SBenjamin Herrenschmidt 	return 0;
1904184cd4a3SBenjamin Herrenschmidt }
1905184cd4a3SBenjamin Herrenschmidt 
1906184cd4a3SBenjamin Herrenschmidt static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1907184cd4a3SBenjamin Herrenschmidt 			       u32 devfn)
1908184cd4a3SBenjamin Herrenschmidt {
1909184cd4a3SBenjamin Herrenschmidt 	return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1910184cd4a3SBenjamin Herrenschmidt }
1911184cd4a3SBenjamin Herrenschmidt 
191273ed148aSBenjamin Herrenschmidt static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
191373ed148aSBenjamin Herrenschmidt {
1914d1a85eeeSGavin Shan 	opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
191573ed148aSBenjamin Herrenschmidt 		       OPAL_ASSERT_RESET);
191673ed148aSBenjamin Herrenschmidt }
191773ed148aSBenjamin Herrenschmidt 
1918e51df2c1SAnton Blanchard static void __init pnv_pci_init_ioda_phb(struct device_node *np,
1919e9cc17d4SGavin Shan 					 u64 hub_id, int ioda_type)
1920184cd4a3SBenjamin Herrenschmidt {
1921184cd4a3SBenjamin Herrenschmidt 	struct pci_controller *hose;
1922184cd4a3SBenjamin Herrenschmidt 	struct pnv_phb *phb;
19238184616fSGavin Shan 	unsigned long size, m32map_off, pemap_off, iomap_off = 0;
1924c681b93cSAlistair Popple 	const __be64 *prop64;
19253a1a4661SBenjamin Herrenschmidt 	const __be32 *prop32;
1926f1b7cc3eSGavin Shan 	int len;
1927184cd4a3SBenjamin Herrenschmidt 	u64 phb_id;
1928184cd4a3SBenjamin Herrenschmidt 	void *aux;
1929184cd4a3SBenjamin Herrenschmidt 	long rc;
1930184cd4a3SBenjamin Herrenschmidt 
1931aa0c033fSGavin Shan 	pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
1932184cd4a3SBenjamin Herrenschmidt 
1933184cd4a3SBenjamin Herrenschmidt 	prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1934184cd4a3SBenjamin Herrenschmidt 	if (!prop64) {
1935184cd4a3SBenjamin Herrenschmidt 		pr_err("  Missing \"ibm,opal-phbid\" property !\n");
1936184cd4a3SBenjamin Herrenschmidt 		return;
1937184cd4a3SBenjamin Herrenschmidt 	}
1938184cd4a3SBenjamin Herrenschmidt 	phb_id = be64_to_cpup(prop64);
1939184cd4a3SBenjamin Herrenschmidt 	pr_debug("  PHB-ID  : 0x%016llx\n", phb_id);
1940184cd4a3SBenjamin Herrenschmidt 
1941184cd4a3SBenjamin Herrenschmidt 	phb = alloc_bootmem(sizeof(struct pnv_phb));
194258d714ecSGavin Shan 	if (!phb) {
194358d714ecSGavin Shan 		pr_err("  Out of memory !\n");
194458d714ecSGavin Shan 		return;
194558d714ecSGavin Shan 	}
194658d714ecSGavin Shan 
194758d714ecSGavin Shan 	/* Allocate PCI controller */
1948184cd4a3SBenjamin Herrenschmidt 	memset(phb, 0, sizeof(struct pnv_phb));
1949184cd4a3SBenjamin Herrenschmidt 	phb->hose = hose = pcibios_alloc_controller(np);
195058d714ecSGavin Shan 	if (!phb->hose) {
195158d714ecSGavin Shan 		pr_err("  Can't allocate PCI controller for %s\n",
1952184cd4a3SBenjamin Herrenschmidt 		       np->full_name);
195358d714ecSGavin Shan 		free_bootmem((unsigned long)phb, sizeof(struct pnv_phb));
1954184cd4a3SBenjamin Herrenschmidt 		return;
1955184cd4a3SBenjamin Herrenschmidt 	}
1956184cd4a3SBenjamin Herrenschmidt 
1957184cd4a3SBenjamin Herrenschmidt 	spin_lock_init(&phb->lock);
1958f1b7cc3eSGavin Shan 	prop32 = of_get_property(np, "bus-range", &len);
1959f1b7cc3eSGavin Shan 	if (prop32 && len == 8) {
19603a1a4661SBenjamin Herrenschmidt 		hose->first_busno = be32_to_cpu(prop32[0]);
19613a1a4661SBenjamin Herrenschmidt 		hose->last_busno = be32_to_cpu(prop32[1]);
1962f1b7cc3eSGavin Shan 	} else {
1963f1b7cc3eSGavin Shan 		pr_warn("  Broken <bus-range> on %s\n", np->full_name);
1964184cd4a3SBenjamin Herrenschmidt 		hose->first_busno = 0;
1965184cd4a3SBenjamin Herrenschmidt 		hose->last_busno = 0xff;
1966f1b7cc3eSGavin Shan 	}
1967184cd4a3SBenjamin Herrenschmidt 	hose->private_data = phb;
1968e9cc17d4SGavin Shan 	phb->hub_id = hub_id;
1969184cd4a3SBenjamin Herrenschmidt 	phb->opal_id = phb_id;
1970aa0c033fSGavin Shan 	phb->type = ioda_type;
1971184cd4a3SBenjamin Herrenschmidt 
1972cee72d5bSBenjamin Herrenschmidt 	/* Detect specific models for error handling */
1973cee72d5bSBenjamin Herrenschmidt 	if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
1974cee72d5bSBenjamin Herrenschmidt 		phb->model = PNV_PHB_MODEL_P7IOC;
1975f3d40c25SBenjamin Herrenschmidt 	else if (of_device_is_compatible(np, "ibm,power8-pciex"))
1976aa0c033fSGavin Shan 		phb->model = PNV_PHB_MODEL_PHB3;
1977cee72d5bSBenjamin Herrenschmidt 	else
1978cee72d5bSBenjamin Herrenschmidt 		phb->model = PNV_PHB_MODEL_UNKNOWN;
1979cee72d5bSBenjamin Herrenschmidt 
1980aa0c033fSGavin Shan 	/* Parse 32-bit and IO ranges (if any) */
19812f1ec02eSGavin Shan 	pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
1982184cd4a3SBenjamin Herrenschmidt 
1983aa0c033fSGavin Shan 	/* Get registers */
1984184cd4a3SBenjamin Herrenschmidt 	phb->regs = of_iomap(np, 0);
1985184cd4a3SBenjamin Herrenschmidt 	if (phb->regs == NULL)
1986184cd4a3SBenjamin Herrenschmidt 		pr_err("  Failed to map registers !\n");
1987184cd4a3SBenjamin Herrenschmidt 
1988184cd4a3SBenjamin Herrenschmidt 	/* Initialize more IODA stuff */
1989aa0c033fSGavin Shan 	phb->ioda.total_pe = 1;
199036954dc7SGavin Shan 	prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
199136954dc7SGavin Shan 	if (prop32)
19923a1a4661SBenjamin Herrenschmidt 		phb->ioda.total_pe = be32_to_cpup(prop32);
199336954dc7SGavin Shan 	prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
199436954dc7SGavin Shan 	if (prop32)
199536954dc7SGavin Shan 		phb->ioda.reserved_pe = be32_to_cpup(prop32);
1996262af557SGuo Chao 
1997262af557SGuo Chao 	/* Parse 64-bit MMIO range */
1998262af557SGuo Chao 	pnv_ioda_parse_m64_window(phb);
1999262af557SGuo Chao 
2000184cd4a3SBenjamin Herrenschmidt 	phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
2001aa0c033fSGavin Shan 	/* FW Has already off top 64k of M32 space (MSI space) */
2002184cd4a3SBenjamin Herrenschmidt 	phb->ioda.m32_size += 0x10000;
2003184cd4a3SBenjamin Herrenschmidt 
2004184cd4a3SBenjamin Herrenschmidt 	phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
20053fd47f06SBenjamin Herrenschmidt 	phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
2006184cd4a3SBenjamin Herrenschmidt 	phb->ioda.io_size = hose->pci_io_size;
2007184cd4a3SBenjamin Herrenschmidt 	phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
2008184cd4a3SBenjamin Herrenschmidt 	phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
2009184cd4a3SBenjamin Herrenschmidt 
2010c35d2a8cSGavin Shan 	/* Allocate aux data & arrays. We don't have IO ports on PHB3 */
2011184cd4a3SBenjamin Herrenschmidt 	size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
2012184cd4a3SBenjamin Herrenschmidt 	m32map_off = size;
2013e47747f4SGavin Shan 	size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
2014c35d2a8cSGavin Shan 	if (phb->type == PNV_PHB_IODA1) {
2015c35d2a8cSGavin Shan 		iomap_off = size;
2016e47747f4SGavin Shan 		size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
2017c35d2a8cSGavin Shan 	}
2018184cd4a3SBenjamin Herrenschmidt 	pemap_off = size;
2019184cd4a3SBenjamin Herrenschmidt 	size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
2020184cd4a3SBenjamin Herrenschmidt 	aux = alloc_bootmem(size);
2021184cd4a3SBenjamin Herrenschmidt 	memset(aux, 0, size);
2022184cd4a3SBenjamin Herrenschmidt 	phb->ioda.pe_alloc = aux;
2023184cd4a3SBenjamin Herrenschmidt 	phb->ioda.m32_segmap = aux + m32map_off;
2024c35d2a8cSGavin Shan 	if (phb->type == PNV_PHB_IODA1)
2025184cd4a3SBenjamin Herrenschmidt 		phb->ioda.io_segmap = aux + iomap_off;
2026184cd4a3SBenjamin Herrenschmidt 	phb->ioda.pe_array = aux + pemap_off;
202736954dc7SGavin Shan 	set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
2028184cd4a3SBenjamin Herrenschmidt 
20297ebdf956SGavin Shan 	INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
2030184cd4a3SBenjamin Herrenschmidt 	INIT_LIST_HEAD(&phb->ioda.pe_list);
2031184cd4a3SBenjamin Herrenschmidt 
2032184cd4a3SBenjamin Herrenschmidt 	/* Calculate how many 32-bit TCE segments we have */
2033184cd4a3SBenjamin Herrenschmidt 	phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
2034184cd4a3SBenjamin Herrenschmidt 
2035aa0c033fSGavin Shan #if 0 /* We should really do that ... */
2036184cd4a3SBenjamin Herrenschmidt 	rc = opal_pci_set_phb_mem_window(opal->phb_id,
2037184cd4a3SBenjamin Herrenschmidt 					 window_type,
2038184cd4a3SBenjamin Herrenschmidt 					 window_num,
2039184cd4a3SBenjamin Herrenschmidt 					 starting_real_address,
2040184cd4a3SBenjamin Herrenschmidt 					 starting_pci_address,
2041184cd4a3SBenjamin Herrenschmidt 					 segment_size);
2042184cd4a3SBenjamin Herrenschmidt #endif
2043184cd4a3SBenjamin Herrenschmidt 
2044262af557SGuo Chao 	pr_info("  %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
2045262af557SGuo Chao 		phb->ioda.total_pe, phb->ioda.reserved_pe,
2046262af557SGuo Chao 		phb->ioda.m32_size, phb->ioda.m32_segsize);
2047262af557SGuo Chao 	if (phb->ioda.m64_size)
2048262af557SGuo Chao 		pr_info("                 M64: 0x%lx [segment=0x%lx]\n",
2049262af557SGuo Chao 			phb->ioda.m64_size, phb->ioda.m64_segsize);
2050262af557SGuo Chao 	if (phb->ioda.io_size)
2051262af557SGuo Chao 		pr_info("                  IO: 0x%x [segment=0x%x]\n",
2052184cd4a3SBenjamin Herrenschmidt 			phb->ioda.io_size, phb->ioda.io_segsize);
2053184cd4a3SBenjamin Herrenschmidt 
2054262af557SGuo Chao 
2055184cd4a3SBenjamin Herrenschmidt 	phb->hose->ops = &pnv_pci_ops;
205649dec922SGavin Shan 	phb->get_pe_state = pnv_ioda_get_pe_state;
205749dec922SGavin Shan 	phb->freeze_pe = pnv_ioda_freeze_pe;
205849dec922SGavin Shan 	phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
2059e9cc17d4SGavin Shan #ifdef CONFIG_EEH
2060e9cc17d4SGavin Shan 	phb->eeh_ops = &ioda_eeh_ops;
2061e9cc17d4SGavin Shan #endif
2062184cd4a3SBenjamin Herrenschmidt 
2063184cd4a3SBenjamin Herrenschmidt 	/* Setup RID -> PE mapping function */
2064184cd4a3SBenjamin Herrenschmidt 	phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
2065184cd4a3SBenjamin Herrenschmidt 
2066184cd4a3SBenjamin Herrenschmidt 	/* Setup TCEs */
2067184cd4a3SBenjamin Herrenschmidt 	phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
2068cd15b048SBenjamin Herrenschmidt 	phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
2069fe7e85c6SGavin Shan 	phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
2070184cd4a3SBenjamin Herrenschmidt 
207173ed148aSBenjamin Herrenschmidt 	/* Setup shutdown function for kexec */
207273ed148aSBenjamin Herrenschmidt 	phb->shutdown = pnv_pci_ioda_shutdown;
207373ed148aSBenjamin Herrenschmidt 
2074184cd4a3SBenjamin Herrenschmidt 	/* Setup MSI support */
2075184cd4a3SBenjamin Herrenschmidt 	pnv_pci_init_ioda_msis(phb);
2076184cd4a3SBenjamin Herrenschmidt 
2077c40a4210SGavin Shan 	/*
2078c40a4210SGavin Shan 	 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
2079c40a4210SGavin Shan 	 * to let the PCI core do resource assignment. It's supposed
2080c40a4210SGavin Shan 	 * that the PCI core will do correct I/O and MMIO alignment
2081c40a4210SGavin Shan 	 * for the P2P bridge bars so that each PCI bus (excluding
2082c40a4210SGavin Shan 	 * the child P2P bridges) can form individual PE.
2083184cd4a3SBenjamin Herrenschmidt 	 */
2084fb446ad0SGavin Shan 	ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
2085184cd4a3SBenjamin Herrenschmidt 	ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
2086271fd03aSGavin Shan 	ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
2087d92a208dSGavin Shan 	ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
2088c40a4210SGavin Shan 	pci_add_flags(PCI_REASSIGN_ALL_RSRC);
2089184cd4a3SBenjamin Herrenschmidt 
2090184cd4a3SBenjamin Herrenschmidt 	/* Reset IODA tables to a clean state */
2091d1a85eeeSGavin Shan 	rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
2092184cd4a3SBenjamin Herrenschmidt 	if (rc)
2093f11fe552SBenjamin Herrenschmidt 		pr_warning("  OPAL Error %ld performing IODA table reset !\n", rc);
2094361f2a2aSGavin Shan 
2095361f2a2aSGavin Shan 	/* If we're running in kdump kerenl, the previous kerenl never
2096361f2a2aSGavin Shan 	 * shutdown PCI devices correctly. We already got IODA table
2097361f2a2aSGavin Shan 	 * cleaned out. So we have to issue PHB reset to stop all PCI
2098361f2a2aSGavin Shan 	 * transactions from previous kerenl.
2099361f2a2aSGavin Shan 	 */
2100361f2a2aSGavin Shan 	if (is_kdump_kernel()) {
2101361f2a2aSGavin Shan 		pr_info("  Issue PHB reset ...\n");
2102361f2a2aSGavin Shan 		ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
2103361f2a2aSGavin Shan 		ioda_eeh_phb_reset(hose, OPAL_DEASSERT_RESET);
2104361f2a2aSGavin Shan 	}
2105262af557SGuo Chao 
21069e9e8935SGavin Shan 	/* Remove M64 resource if we can't configure it successfully */
21079e9e8935SGavin Shan 	if (!phb->init_m64 || phb->init_m64(phb))
2108262af557SGuo Chao 		hose->mem_resources[1].flags = 0;
2109184cd4a3SBenjamin Herrenschmidt }
2110184cd4a3SBenjamin Herrenschmidt 
211167975005SBjorn Helgaas void __init pnv_pci_init_ioda2_phb(struct device_node *np)
2112aa0c033fSGavin Shan {
2113e9cc17d4SGavin Shan 	pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
2114aa0c033fSGavin Shan }
2115aa0c033fSGavin Shan 
2116184cd4a3SBenjamin Herrenschmidt void __init pnv_pci_init_ioda_hub(struct device_node *np)
2117184cd4a3SBenjamin Herrenschmidt {
2118184cd4a3SBenjamin Herrenschmidt 	struct device_node *phbn;
2119c681b93cSAlistair Popple 	const __be64 *prop64;
2120184cd4a3SBenjamin Herrenschmidt 	u64 hub_id;
2121184cd4a3SBenjamin Herrenschmidt 
2122184cd4a3SBenjamin Herrenschmidt 	pr_info("Probing IODA IO-Hub %s\n", np->full_name);
2123184cd4a3SBenjamin Herrenschmidt 
2124184cd4a3SBenjamin Herrenschmidt 	prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
2125184cd4a3SBenjamin Herrenschmidt 	if (!prop64) {
2126184cd4a3SBenjamin Herrenschmidt 		pr_err(" Missing \"ibm,opal-hubid\" property !\n");
2127184cd4a3SBenjamin Herrenschmidt 		return;
2128184cd4a3SBenjamin Herrenschmidt 	}
2129184cd4a3SBenjamin Herrenschmidt 	hub_id = be64_to_cpup(prop64);
2130184cd4a3SBenjamin Herrenschmidt 	pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
2131184cd4a3SBenjamin Herrenschmidt 
2132184cd4a3SBenjamin Herrenschmidt 	/* Count child PHBs */
2133184cd4a3SBenjamin Herrenschmidt 	for_each_child_of_node(np, phbn) {
2134184cd4a3SBenjamin Herrenschmidt 		/* Look for IODA1 PHBs */
2135184cd4a3SBenjamin Herrenschmidt 		if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
2136e9cc17d4SGavin Shan 			pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
2137184cd4a3SBenjamin Herrenschmidt 	}
2138184cd4a3SBenjamin Herrenschmidt }
2139