xref: /linux/arch/powerpc/platforms/powernv/pci-ioda.c (revision 3203a08c1266689c204fb8f10d6bb5186921fce2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Support PCI/PCIe on PowerNV platforms
4  *
5  * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
6  */
7 
8 #undef DEBUG
9 
10 #include <linux/kernel.h>
11 #include <linux/pci.h>
12 #include <linux/crash_dump.h>
13 #include <linux/delay.h>
14 #include <linux/string.h>
15 #include <linux/init.h>
16 #include <linux/memblock.h>
17 #include <linux/irq.h>
18 #include <linux/irqchip/irq-msi-lib.h>
19 #include <linux/io.h>
20 #include <linux/msi.h>
21 #include <linux/iommu.h>
22 #include <linux/rculist.h>
23 #include <linux/sizes.h>
24 #include <linux/debugfs.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 
28 #include <asm/sections.h>
29 #include <asm/io.h>
30 #include <asm/pci-bridge.h>
31 #include <asm/machdep.h>
32 #include <asm/msi_bitmap.h>
33 #include <asm/ppc-pci.h>
34 #include <asm/opal.h>
35 #include <asm/iommu.h>
36 #include <asm/tce.h>
37 #include <asm/xics.h>
38 #include <asm/firmware.h>
39 #include <asm/pnv-pci.h>
40 #include <asm/mmzone.h>
41 
42 #include "powernv.h"
43 #include "pci.h"
44 #include "../../../../drivers/pci/pci.h"
45 
46 /* This array is indexed with enum pnv_phb_type */
47 static const char * const pnv_phb_names[] = { "IODA2", "NPU_OCAPI" };
48 
49 static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
50 static void pnv_pci_configure_bus(struct pci_bus *bus);
51 
pe_level_printk(const struct pnv_ioda_pe * pe,const char * level,const char * fmt,...)52 void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
53 			    const char *fmt, ...)
54 {
55 	struct va_format vaf;
56 	va_list args;
57 	char pfix[32];
58 
59 	va_start(args, fmt);
60 
61 	vaf.fmt = fmt;
62 	vaf.va = &args;
63 
64 	if (pe->flags & PNV_IODA_PE_DEV)
65 		strscpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
66 	else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
67 		sprintf(pfix, "%04x:%02x     ",
68 			pci_domain_nr(pe->pbus), pe->pbus->number);
69 #ifdef CONFIG_PCI_IOV
70 	else if (pe->flags & PNV_IODA_PE_VF)
71 		sprintf(pfix, "%04x:%02x:%2x.%d",
72 			pci_domain_nr(pe->parent_dev->bus),
73 			(pe->rid & 0xff00) >> 8,
74 			PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
75 #endif /* CONFIG_PCI_IOV*/
76 
77 	printk("%spci %s: [PE# %.2x] %pV",
78 	       level, pfix, pe->pe_number, &vaf);
79 
80 	va_end(args);
81 }
82 
83 static bool pnv_iommu_bypass_disabled __read_mostly;
84 static bool pci_reset_phbs __read_mostly;
85 
iommu_setup(char * str)86 static int __init iommu_setup(char *str)
87 {
88 	if (!str)
89 		return -EINVAL;
90 
91 	while (*str) {
92 		if (!strncmp(str, "nobypass", 8)) {
93 			pnv_iommu_bypass_disabled = true;
94 			pr_info("PowerNV: IOMMU bypass window disabled.\n");
95 			break;
96 		}
97 		str += strcspn(str, ",");
98 		if (*str == ',')
99 			str++;
100 	}
101 
102 	return 0;
103 }
104 early_param("iommu", iommu_setup);
105 
pci_reset_phbs_setup(char * str)106 static int __init pci_reset_phbs_setup(char *str)
107 {
108 	pci_reset_phbs = true;
109 	return 0;
110 }
111 
112 early_param("ppc_pci_reset_phbs", pci_reset_phbs_setup);
113 
pnv_ioda_init_pe(struct pnv_phb * phb,int pe_no)114 static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
115 {
116 	s64 rc;
117 
118 	phb->ioda.pe_array[pe_no].phb = phb;
119 	phb->ioda.pe_array[pe_no].pe_number = pe_no;
120 	phb->ioda.pe_array[pe_no].dma_setup_done = false;
121 
122 	/*
123 	 * Clear the PE frozen state as it might be put into frozen state
124 	 * in the last PCI remove path. It's not harmful to do so when the
125 	 * PE is already in unfrozen state.
126 	 */
127 	rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
128 				       OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
129 	if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
130 		pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n",
131 			__func__, rc, phb->hose->global_number, pe_no);
132 
133 	return &phb->ioda.pe_array[pe_no];
134 }
135 
pnv_ioda_reserve_pe(struct pnv_phb * phb,int pe_no)136 static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
137 {
138 	if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
139 		pr_warn("%s: Invalid PE %x on PHB#%x\n",
140 			__func__, pe_no, phb->hose->global_number);
141 		return;
142 	}
143 
144 	mutex_lock(&phb->ioda.pe_alloc_mutex);
145 	if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
146 		pr_debug("%s: PE %x was reserved on PHB#%x\n",
147 			 __func__, pe_no, phb->hose->global_number);
148 	mutex_unlock(&phb->ioda.pe_alloc_mutex);
149 
150 	pnv_ioda_init_pe(phb, pe_no);
151 }
152 
pnv_ioda_alloc_pe(struct pnv_phb * phb,int count)153 struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb, int count)
154 {
155 	struct pnv_ioda_pe *ret = NULL;
156 	int run = 0, pe, i;
157 
158 	mutex_lock(&phb->ioda.pe_alloc_mutex);
159 
160 	/* scan backwards for a run of @count cleared bits */
161 	for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
162 		if (test_bit(pe, phb->ioda.pe_alloc)) {
163 			run = 0;
164 			continue;
165 		}
166 
167 		run++;
168 		if (run == count)
169 			break;
170 	}
171 	if (run != count)
172 		goto out;
173 
174 	for (i = pe; i < pe + count; i++) {
175 		set_bit(i, phb->ioda.pe_alloc);
176 		pnv_ioda_init_pe(phb, i);
177 	}
178 	ret = &phb->ioda.pe_array[pe];
179 
180 out:
181 	mutex_unlock(&phb->ioda.pe_alloc_mutex);
182 	return ret;
183 }
184 
pnv_ioda_free_pe(struct pnv_ioda_pe * pe)185 void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
186 {
187 	struct pnv_phb *phb = pe->phb;
188 	unsigned int pe_num = pe->pe_number;
189 
190 	WARN_ON(pe->pdev);
191 	memset(pe, 0, sizeof(struct pnv_ioda_pe));
192 
193 	mutex_lock(&phb->ioda.pe_alloc_mutex);
194 	clear_bit(pe_num, phb->ioda.pe_alloc);
195 	mutex_unlock(&phb->ioda.pe_alloc_mutex);
196 }
197 
198 /* The default M64 BAR is shared by all PEs */
pnv_ioda2_init_m64(struct pnv_phb * phb)199 static int pnv_ioda2_init_m64(struct pnv_phb *phb)
200 {
201 	const char *desc;
202 	struct resource *r;
203 	s64 rc;
204 
205 	/* Configure the default M64 BAR */
206 	rc = opal_pci_set_phb_mem_window(phb->opal_id,
207 					 OPAL_M64_WINDOW_TYPE,
208 					 phb->ioda.m64_bar_idx,
209 					 phb->ioda.m64_base,
210 					 0, /* unused */
211 					 phb->ioda.m64_size);
212 	if (rc != OPAL_SUCCESS) {
213 		desc = "configuring";
214 		goto fail;
215 	}
216 
217 	/* Enable the default M64 BAR */
218 	rc = opal_pci_phb_mmio_enable(phb->opal_id,
219 				      OPAL_M64_WINDOW_TYPE,
220 				      phb->ioda.m64_bar_idx,
221 				      OPAL_ENABLE_M64_SPLIT);
222 	if (rc != OPAL_SUCCESS) {
223 		desc = "enabling";
224 		goto fail;
225 	}
226 
227 	/*
228 	 * Exclude the segments for reserved and root bus PE, which
229 	 * are first or last two PEs.
230 	 */
231 	r = &phb->hose->mem_resources[1];
232 	if (phb->ioda.reserved_pe_idx == 0)
233 		r->start += (2 * phb->ioda.m64_segsize);
234 	else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
235 		r->end -= (2 * phb->ioda.m64_segsize);
236 	else
237 		pr_warn("  Cannot strip M64 segment for reserved PE#%x\n",
238 			phb->ioda.reserved_pe_idx);
239 
240 	return 0;
241 
242 fail:
243 	pr_warn("  Failure %lld %s M64 BAR#%d\n",
244 		rc, desc, phb->ioda.m64_bar_idx);
245 	opal_pci_phb_mmio_enable(phb->opal_id,
246 				 OPAL_M64_WINDOW_TYPE,
247 				 phb->ioda.m64_bar_idx,
248 				 OPAL_DISABLE_M64);
249 	return -EIO;
250 }
251 
pnv_ioda_reserve_dev_m64_pe(struct pci_dev * pdev,unsigned long * pe_bitmap)252 static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
253 					 unsigned long *pe_bitmap)
254 {
255 	struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
256 	struct resource *r;
257 	resource_size_t base, sgsz, start, end;
258 	int segno, i;
259 
260 	base = phb->ioda.m64_base;
261 	sgsz = phb->ioda.m64_segsize;
262 	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
263 		r = &pdev->resource[i];
264 		if (!r->parent || !pnv_pci_is_m64(phb, r))
265 			continue;
266 
267 		start = ALIGN_DOWN(r->start - base, sgsz);
268 		end = ALIGN(r->end - base, sgsz);
269 		for (segno = start / sgsz; segno < end / sgsz; segno++) {
270 			if (pe_bitmap)
271 				set_bit(segno, pe_bitmap);
272 			else
273 				pnv_ioda_reserve_pe(phb, segno);
274 		}
275 	}
276 }
277 
pnv_ioda_reserve_m64_pe(struct pci_bus * bus,unsigned long * pe_bitmap,bool all)278 static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
279 				    unsigned long *pe_bitmap,
280 				    bool all)
281 {
282 	struct pci_dev *pdev;
283 
284 	list_for_each_entry(pdev, &bus->devices, bus_list) {
285 		pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
286 
287 		if (all && pdev->subordinate)
288 			pnv_ioda_reserve_m64_pe(pdev->subordinate,
289 						pe_bitmap, all);
290 	}
291 }
292 
pnv_ioda_pick_m64_pe(struct pci_bus * bus,bool all)293 static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
294 {
295 	unsigned long *pe_alloc __free(bitmap) = NULL;
296 	struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
297 	struct pnv_ioda_pe *master_pe, *pe;
298 	unsigned int i;
299 
300 	/* Root bus shouldn't use M64 */
301 	if (pci_is_root_bus(bus))
302 		return NULL;
303 
304 	pe_alloc = bitmap_zalloc(phb->ioda.total_pe_num, GFP_KERNEL);
305 	if (!pe_alloc) {
306 		pr_warn("%s: Out of memory !\n",
307 			__func__);
308 		return NULL;
309 	}
310 
311 	/* Figure out reserved PE numbers by the PE */
312 	pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
313 
314 	/*
315 	 * Figure out the master PE and put all slave PEs to master
316 	 * PE's list to form compound PE.
317 	 *
318 	 * The current bus might not own M64 window and that's all
319 	 * contributed by its child buses. For the case, we needn't
320 	 * pick M64 dependent PE#.
321 	 */
322 	master_pe = NULL;
323 	for_each_set_bit(i, pe_alloc, phb->ioda.total_pe_num) {
324 		pe = &phb->ioda.pe_array[i];
325 
326 		phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
327 		if (!master_pe) {
328 			pe->flags |= PNV_IODA_PE_MASTER;
329 			INIT_LIST_HEAD(&pe->slaves);
330 			master_pe = pe;
331 		} else {
332 			pe->flags |= PNV_IODA_PE_SLAVE;
333 			pe->master = master_pe;
334 			list_add_tail(&pe->list, &master_pe->slaves);
335 		}
336 	}
337 
338 	return master_pe;
339 }
340 
pnv_ioda_parse_m64_window(struct pnv_phb * phb)341 static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
342 {
343 	struct pci_controller *hose = phb->hose;
344 	struct device_node *dn = hose->dn;
345 	struct resource *res;
346 	u32 m64_range[2], i;
347 	const __be32 *r;
348 	u64 pci_addr;
349 
350 	if (phb->type != PNV_PHB_IODA2) {
351 		pr_info("  Not support M64 window\n");
352 		return;
353 	}
354 
355 	if (!firmware_has_feature(FW_FEATURE_OPAL)) {
356 		pr_info("  Firmware too old to support M64 window\n");
357 		return;
358 	}
359 
360 	r = of_get_property(dn, "ibm,opal-m64-window", NULL);
361 	if (!r) {
362 		pr_info("  No <ibm,opal-m64-window> on %pOF\n",
363 			dn);
364 		return;
365 	}
366 
367 	/*
368 	 * Find the available M64 BAR range and pickup the last one for
369 	 * covering the whole 64-bits space. We support only one range.
370 	 */
371 	if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges",
372 				       m64_range, 2)) {
373 		/* In absence of the property, assume 0..15 */
374 		m64_range[0] = 0;
375 		m64_range[1] = 16;
376 	}
377 	/* We only support 64 bits in our allocator */
378 	if (m64_range[1] > 63) {
379 		pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",
380 			__func__, m64_range[1], phb->hose->global_number);
381 		m64_range[1] = 63;
382 	}
383 	/* Empty range, no m64 */
384 	if (m64_range[1] <= m64_range[0]) {
385 		pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",
386 			__func__, phb->hose->global_number);
387 		return;
388 	}
389 
390 	/* Configure M64 informations */
391 	res = &hose->mem_resources[1];
392 	res->name = dn->full_name;
393 	res->start = of_translate_address(dn, r + 2);
394 	res->end = res->start + of_read_number(r + 4, 2) - 1;
395 	res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
396 	pci_addr = of_read_number(r, 2);
397 	hose->mem_offset[1] = res->start - pci_addr;
398 
399 	phb->ioda.m64_size = resource_size(res);
400 	phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
401 	phb->ioda.m64_base = pci_addr;
402 
403 	/* This lines up nicely with the display from processing OF ranges */
404 	pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",
405 		res->start, res->end, pci_addr, m64_range[0],
406 		m64_range[0] + m64_range[1] - 1);
407 
408 	/* Mark all M64 used up by default */
409 	phb->ioda.m64_bar_alloc = (unsigned long)-1;
410 
411 	/* Use last M64 BAR to cover M64 window */
412 	m64_range[1]--;
413 	phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1];
414 
415 	pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx);
416 
417 	/* Mark remaining ones free */
418 	for (i = m64_range[0]; i < m64_range[1]; i++)
419 		clear_bit(i, &phb->ioda.m64_bar_alloc);
420 
421 	/*
422 	 * Setup init functions for M64 based on IODA version, IODA3 uses
423 	 * the IODA2 code.
424 	 */
425 	phb->init_m64 = pnv_ioda2_init_m64;
426 }
427 
pnv_ioda_freeze_pe(struct pnv_phb * phb,int pe_no)428 static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
429 {
430 	struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
431 	struct pnv_ioda_pe *slave;
432 	s64 rc;
433 
434 	/* Fetch master PE */
435 	if (pe->flags & PNV_IODA_PE_SLAVE) {
436 		pe = pe->master;
437 		if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
438 			return;
439 
440 		pe_no = pe->pe_number;
441 	}
442 
443 	/* Freeze master PE */
444 	rc = opal_pci_eeh_freeze_set(phb->opal_id,
445 				     pe_no,
446 				     OPAL_EEH_ACTION_SET_FREEZE_ALL);
447 	if (rc != OPAL_SUCCESS) {
448 		pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
449 			__func__, rc, phb->hose->global_number, pe_no);
450 		return;
451 	}
452 
453 	/* Freeze slave PEs */
454 	if (!(pe->flags & PNV_IODA_PE_MASTER))
455 		return;
456 
457 	list_for_each_entry(slave, &pe->slaves, list) {
458 		rc = opal_pci_eeh_freeze_set(phb->opal_id,
459 					     slave->pe_number,
460 					     OPAL_EEH_ACTION_SET_FREEZE_ALL);
461 		if (rc != OPAL_SUCCESS)
462 			pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
463 				__func__, rc, phb->hose->global_number,
464 				slave->pe_number);
465 	}
466 }
467 
pnv_ioda_unfreeze_pe(struct pnv_phb * phb,int pe_no,int opt)468 static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
469 {
470 	struct pnv_ioda_pe *pe, *slave;
471 	s64 rc;
472 
473 	/* Find master PE */
474 	pe = &phb->ioda.pe_array[pe_no];
475 	if (pe->flags & PNV_IODA_PE_SLAVE) {
476 		pe = pe->master;
477 		WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
478 		pe_no = pe->pe_number;
479 	}
480 
481 	/* Clear frozen state for master PE */
482 	rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
483 	if (rc != OPAL_SUCCESS) {
484 		pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
485 			__func__, rc, opt, phb->hose->global_number, pe_no);
486 		return -EIO;
487 	}
488 
489 	if (!(pe->flags & PNV_IODA_PE_MASTER))
490 		return 0;
491 
492 	/* Clear frozen state for slave PEs */
493 	list_for_each_entry(slave, &pe->slaves, list) {
494 		rc = opal_pci_eeh_freeze_clear(phb->opal_id,
495 					     slave->pe_number,
496 					     opt);
497 		if (rc != OPAL_SUCCESS) {
498 			pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
499 				__func__, rc, opt, phb->hose->global_number,
500 				slave->pe_number);
501 			return -EIO;
502 		}
503 	}
504 
505 	return 0;
506 }
507 
pnv_ioda_get_pe_state(struct pnv_phb * phb,int pe_no)508 static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
509 {
510 	struct pnv_ioda_pe *slave, *pe;
511 	u8 fstate = 0, state;
512 	__be16 pcierr = 0;
513 	s64 rc;
514 
515 	/* Sanity check on PE number */
516 	if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
517 		return OPAL_EEH_STOPPED_PERM_UNAVAIL;
518 
519 	/*
520 	 * Fetch the master PE and the PE instance might be
521 	 * not initialized yet.
522 	 */
523 	pe = &phb->ioda.pe_array[pe_no];
524 	if (pe->flags & PNV_IODA_PE_SLAVE) {
525 		pe = pe->master;
526 		WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
527 		pe_no = pe->pe_number;
528 	}
529 
530 	/* Check the master PE */
531 	rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
532 					&state, &pcierr, NULL);
533 	if (rc != OPAL_SUCCESS) {
534 		pr_warn("%s: Failure %lld getting "
535 			"PHB#%x-PE#%x state\n",
536 			__func__, rc,
537 			phb->hose->global_number, pe_no);
538 		return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
539 	}
540 
541 	/* Check the slave PE */
542 	if (!(pe->flags & PNV_IODA_PE_MASTER))
543 		return state;
544 
545 	list_for_each_entry(slave, &pe->slaves, list) {
546 		rc = opal_pci_eeh_freeze_status(phb->opal_id,
547 						slave->pe_number,
548 						&fstate,
549 						&pcierr,
550 						NULL);
551 		if (rc != OPAL_SUCCESS) {
552 			pr_warn("%s: Failure %lld getting "
553 				"PHB#%x-PE#%x state\n",
554 				__func__, rc,
555 				phb->hose->global_number, slave->pe_number);
556 			return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
557 		}
558 
559 		/*
560 		 * Override the result based on the ascending
561 		 * priority.
562 		 */
563 		if (fstate > state)
564 			state = fstate;
565 	}
566 
567 	return state;
568 }
569 
pnv_pci_bdfn_to_pe(struct pnv_phb * phb,u16 bdfn)570 struct pnv_ioda_pe *pnv_pci_bdfn_to_pe(struct pnv_phb *phb, u16 bdfn)
571 {
572 	int pe_number = phb->ioda.pe_rmap[bdfn];
573 
574 	if (pe_number == IODA_INVALID_PE)
575 		return NULL;
576 
577 	return &phb->ioda.pe_array[pe_number];
578 }
579 
pnv_ioda_get_pe(struct pci_dev * dev)580 struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
581 {
582 	struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
583 	struct pci_dn *pdn = pci_get_pdn(dev);
584 
585 	if (!pdn)
586 		return NULL;
587 	if (pdn->pe_number == IODA_INVALID_PE)
588 		return NULL;
589 	return &phb->ioda.pe_array[pdn->pe_number];
590 }
591 
pnv_ioda_set_one_peltv(struct pnv_phb * phb,struct pnv_ioda_pe * parent,struct pnv_ioda_pe * child,bool is_add)592 static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
593 				  struct pnv_ioda_pe *parent,
594 				  struct pnv_ioda_pe *child,
595 				  bool is_add)
596 {
597 	const char *desc = is_add ? "adding" : "removing";
598 	uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
599 			      OPAL_REMOVE_PE_FROM_DOMAIN;
600 	struct pnv_ioda_pe *slave;
601 	long rc;
602 
603 	/* Parent PE affects child PE */
604 	rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
605 				child->pe_number, op);
606 	if (rc != OPAL_SUCCESS) {
607 		pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
608 			rc, desc);
609 		return -ENXIO;
610 	}
611 
612 	if (!(child->flags & PNV_IODA_PE_MASTER))
613 		return 0;
614 
615 	/* Compound case: parent PE affects slave PEs */
616 	list_for_each_entry(slave, &child->slaves, list) {
617 		rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
618 					slave->pe_number, op);
619 		if (rc != OPAL_SUCCESS) {
620 			pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
621 				rc, desc);
622 			return -ENXIO;
623 		}
624 	}
625 
626 	return 0;
627 }
628 
pnv_ioda_set_peltv(struct pnv_phb * phb,struct pnv_ioda_pe * pe,bool is_add)629 static int pnv_ioda_set_peltv(struct pnv_phb *phb,
630 			      struct pnv_ioda_pe *pe,
631 			      bool is_add)
632 {
633 	struct pnv_ioda_pe *slave;
634 	struct pci_dev *pdev = NULL;
635 	int ret;
636 
637 	/*
638 	 * Clear PE frozen state. If it's master PE, we need
639 	 * clear slave PE frozen state as well.
640 	 */
641 	if (is_add) {
642 		opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
643 					  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
644 		if (pe->flags & PNV_IODA_PE_MASTER) {
645 			list_for_each_entry(slave, &pe->slaves, list)
646 				opal_pci_eeh_freeze_clear(phb->opal_id,
647 							  slave->pe_number,
648 							  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
649 		}
650 	}
651 
652 	/*
653 	 * Associate PE in PELT. We need add the PE into the
654 	 * corresponding PELT-V as well. Otherwise, the error
655 	 * originated from the PE might contribute to other
656 	 * PEs.
657 	 */
658 	ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
659 	if (ret)
660 		return ret;
661 
662 	/* For compound PEs, any one affects all of them */
663 	if (pe->flags & PNV_IODA_PE_MASTER) {
664 		list_for_each_entry(slave, &pe->slaves, list) {
665 			ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
666 			if (ret)
667 				return ret;
668 		}
669 	}
670 
671 	if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
672 		pdev = pe->pbus->self;
673 	else if (pe->flags & PNV_IODA_PE_DEV)
674 		pdev = pe->pdev->bus->self;
675 #ifdef CONFIG_PCI_IOV
676 	else if (pe->flags & PNV_IODA_PE_VF)
677 		pdev = pe->parent_dev;
678 #endif /* CONFIG_PCI_IOV */
679 	while (pdev) {
680 		struct pci_dn *pdn = pci_get_pdn(pdev);
681 		struct pnv_ioda_pe *parent;
682 
683 		if (pdn && pdn->pe_number != IODA_INVALID_PE) {
684 			parent = &phb->ioda.pe_array[pdn->pe_number];
685 			ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
686 			if (ret)
687 				return ret;
688 		}
689 
690 		pdev = pdev->bus->self;
691 	}
692 
693 	return 0;
694 }
695 
pnv_ioda_unset_peltv(struct pnv_phb * phb,struct pnv_ioda_pe * pe,struct pci_dev * parent)696 static void pnv_ioda_unset_peltv(struct pnv_phb *phb,
697 				 struct pnv_ioda_pe *pe,
698 				 struct pci_dev *parent)
699 {
700 	int64_t rc;
701 
702 	while (parent) {
703 		struct pci_dn *pdn = pci_get_pdn(parent);
704 
705 		if (pdn && pdn->pe_number != IODA_INVALID_PE) {
706 			rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
707 						pe->pe_number,
708 						OPAL_REMOVE_PE_FROM_DOMAIN);
709 			/* XXX What to do in case of error ? */
710 		}
711 		parent = parent->bus->self;
712 	}
713 
714 	opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
715 				  OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
716 
717 	/* Disassociate PE in PELT */
718 	rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
719 				pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
720 	if (rc)
721 		pe_warn(pe, "OPAL error %lld remove self from PELTV\n", rc);
722 }
723 
pnv_ioda_deconfigure_pe(struct pnv_phb * phb,struct pnv_ioda_pe * pe)724 int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
725 {
726 	struct pci_dev *parent;
727 	uint8_t bcomp, dcomp, fcomp;
728 	int64_t rc;
729 	long rid_end, rid;
730 
731 	/* Currently, we just deconfigure VF PE. Bus PE will always there.*/
732 	if (pe->pbus) {
733 		int count;
734 
735 		dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
736 		fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
737 		parent = pe->pbus->self;
738 		if (pe->flags & PNV_IODA_PE_BUS_ALL)
739 			count = resource_size(&pe->pbus->busn_res);
740 		else
741 			count = 1;
742 
743 		switch(count) {
744 		case  1: bcomp = OpalPciBusAll;         break;
745 		case  2: bcomp = OpalPciBus7Bits;       break;
746 		case  4: bcomp = OpalPciBus6Bits;       break;
747 		case  8: bcomp = OpalPciBus5Bits;       break;
748 		case 16: bcomp = OpalPciBus4Bits;       break;
749 		case 32: bcomp = OpalPciBus3Bits;       break;
750 		default:
751 			dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
752 			        count);
753 			/* Do an exact match only */
754 			bcomp = OpalPciBusAll;
755 		}
756 		rid_end = pe->rid + (count << 8);
757 	} else {
758 #ifdef CONFIG_PCI_IOV
759 		if (pe->flags & PNV_IODA_PE_VF)
760 			parent = pe->parent_dev;
761 		else
762 #endif
763 			parent = pe->pdev->bus->self;
764 		bcomp = OpalPciBusAll;
765 		dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
766 		fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
767 		rid_end = pe->rid + 1;
768 	}
769 
770 	/* Clear the reverse map */
771 	for (rid = pe->rid; rid < rid_end; rid++)
772 		phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
773 
774 	/*
775 	 * Release from all parents PELT-V. NPUs don't have a PELTV
776 	 * table
777 	 */
778 	if (phb->type != PNV_PHB_NPU_OCAPI)
779 		pnv_ioda_unset_peltv(phb, pe, parent);
780 
781 	rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
782 			     bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
783 	if (rc)
784 		pe_err(pe, "OPAL error %lld trying to setup PELT table\n", rc);
785 
786 	pe->pbus = NULL;
787 	pe->pdev = NULL;
788 #ifdef CONFIG_PCI_IOV
789 	pe->parent_dev = NULL;
790 #endif
791 
792 	return 0;
793 }
794 
pnv_ioda_configure_pe(struct pnv_phb * phb,struct pnv_ioda_pe * pe)795 int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
796 {
797 	uint8_t bcomp, dcomp, fcomp;
798 	long rc, rid_end, rid;
799 
800 	/* Bus validation ? */
801 	if (pe->pbus) {
802 		int count;
803 
804 		dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
805 		fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
806 		if (pe->flags & PNV_IODA_PE_BUS_ALL)
807 			count = resource_size(&pe->pbus->busn_res);
808 		else
809 			count = 1;
810 
811 		switch(count) {
812 		case  1: bcomp = OpalPciBusAll;		break;
813 		case  2: bcomp = OpalPciBus7Bits;	break;
814 		case  4: bcomp = OpalPciBus6Bits;	break;
815 		case  8: bcomp = OpalPciBus5Bits;	break;
816 		case 16: bcomp = OpalPciBus4Bits;	break;
817 		case 32: bcomp = OpalPciBus3Bits;	break;
818 		default:
819 			dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
820 			        count);
821 			/* Do an exact match only */
822 			bcomp = OpalPciBusAll;
823 		}
824 		rid_end = pe->rid + (count << 8);
825 	} else {
826 		bcomp = OpalPciBusAll;
827 		dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
828 		fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
829 		rid_end = pe->rid + 1;
830 	}
831 
832 	/*
833 	 * Associate PE in PELT. We need add the PE into the
834 	 * corresponding PELT-V as well. Otherwise, the error
835 	 * originated from the PE might contribute to other
836 	 * PEs.
837 	 */
838 	rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
839 			     bcomp, dcomp, fcomp, OPAL_MAP_PE);
840 	if (rc) {
841 		pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
842 		return -ENXIO;
843 	}
844 
845 	/*
846 	 * Configure PELTV. NPUs don't have a PELTV table so skip
847 	 * configuration on them.
848 	 */
849 	if (phb->type != PNV_PHB_NPU_OCAPI)
850 		pnv_ioda_set_peltv(phb, pe, true);
851 
852 	/* Setup reverse map */
853 	for (rid = pe->rid; rid < rid_end; rid++)
854 		phb->ioda.pe_rmap[rid] = pe->pe_number;
855 
856 	pe->mve_number = 0;
857 
858 	return 0;
859 }
860 
pnv_ioda_setup_dev_PE(struct pci_dev * dev)861 static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
862 {
863 	struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
864 	struct pci_dn *pdn = pci_get_pdn(dev);
865 	struct pnv_ioda_pe *pe;
866 
867 	if (!pdn) {
868 		pr_err("%s: Device tree node not associated properly\n",
869 			   pci_name(dev));
870 		return NULL;
871 	}
872 	if (pdn->pe_number != IODA_INVALID_PE)
873 		return NULL;
874 
875 	pe = pnv_ioda_alloc_pe(phb, 1);
876 	if (!pe) {
877 		pr_warn("%s: Not enough PE# available, disabling device\n",
878 			pci_name(dev));
879 		return NULL;
880 	}
881 
882 	/* NOTE: We don't get a reference for the pointer in the PE
883 	 * data structure, both the device and PE structures should be
884 	 * destroyed at the same time.
885 	 *
886 	 * At some point we want to remove the PDN completely anyways
887 	 */
888 	pdn->pe_number = pe->pe_number;
889 	pe->flags = PNV_IODA_PE_DEV;
890 	pe->pdev = dev;
891 	pe->pbus = NULL;
892 	pe->mve_number = -1;
893 	pe->rid = dev->bus->number << 8 | pdn->devfn;
894 	pe->device_count++;
895 
896 	pe_info(pe, "Associated device to PE\n");
897 
898 	if (pnv_ioda_configure_pe(phb, pe)) {
899 		/* XXX What do we do here ? */
900 		pnv_ioda_free_pe(pe);
901 		pdn->pe_number = IODA_INVALID_PE;
902 		pe->pdev = NULL;
903 		return NULL;
904 	}
905 
906 	/* Put PE to the list */
907 	mutex_lock(&phb->ioda.pe_list_mutex);
908 	list_add_tail(&pe->list, &phb->ioda.pe_list);
909 	mutex_unlock(&phb->ioda.pe_list_mutex);
910 	return pe;
911 }
912 
913 /*
914  * There're 2 types of PCI bus sensitive PEs: One that is compromised of
915  * single PCI bus. Another one that contains the primary PCI bus and its
916  * subordinate PCI devices and buses. The second type of PE is normally
917  * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
918  */
pnv_ioda_setup_bus_PE(struct pci_bus * bus,bool all)919 static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
920 {
921 	struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
922 	struct pnv_ioda_pe *pe = NULL;
923 	unsigned int pe_num;
924 
925 	/*
926 	 * In partial hotplug case, the PE instance might be still alive.
927 	 * We should reuse it instead of allocating a new one.
928 	 */
929 	pe_num = phb->ioda.pe_rmap[bus->number << 8];
930 	if (WARN_ON(pe_num != IODA_INVALID_PE)) {
931 		pe = &phb->ioda.pe_array[pe_num];
932 		return NULL;
933 	}
934 
935 	/* PE number for root bus should have been reserved */
936 	if (pci_is_root_bus(bus))
937 		pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
938 
939 	/* Check if PE is determined by M64 */
940 	if (!pe)
941 		pe = pnv_ioda_pick_m64_pe(bus, all);
942 
943 	/* The PE number isn't pinned by M64 */
944 	if (!pe)
945 		pe = pnv_ioda_alloc_pe(phb, 1);
946 
947 	if (!pe) {
948 		pr_warn("%s: Not enough PE# available for PCI bus %04x:%02x\n",
949 			__func__, pci_domain_nr(bus), bus->number);
950 		return NULL;
951 	}
952 
953 	pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
954 	pe->pbus = bus;
955 	pe->pdev = NULL;
956 	pe->mve_number = -1;
957 	pe->rid = bus->busn_res.start << 8;
958 
959 	if (all)
960 		pe_info(pe, "Secondary bus %pad..%pad associated with PE#%x\n",
961 			&bus->busn_res.start, &bus->busn_res.end,
962 			pe->pe_number);
963 	else
964 		pe_info(pe, "Secondary bus %pad associated with PE#%x\n",
965 			&bus->busn_res.start, pe->pe_number);
966 
967 	if (pnv_ioda_configure_pe(phb, pe)) {
968 		/* XXX What do we do here ? */
969 		pnv_ioda_free_pe(pe);
970 		pe->pbus = NULL;
971 		return NULL;
972 	}
973 
974 	/* Put PE to the list */
975 	list_add_tail(&pe->list, &phb->ioda.pe_list);
976 
977 	return pe;
978 }
979 
pnv_pci_ioda_dma_dev_setup(struct pci_dev * pdev)980 static void pnv_pci_ioda_dma_dev_setup(struct pci_dev *pdev)
981 {
982 	struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
983 	struct pci_dn *pdn = pci_get_pdn(pdev);
984 	struct pnv_ioda_pe *pe;
985 
986 	/* Check if the BDFN for this device is associated with a PE yet */
987 	pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));
988 	if (!pe) {
989 		/* VF PEs should be pre-configured in pnv_pci_sriov_enable() */
990 		if (WARN_ON(pdev->is_virtfn))
991 			return;
992 
993 		pnv_pci_configure_bus(pdev->bus);
994 		pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));
995 		pci_info(pdev, "Configured PE#%x\n", pe ? pe->pe_number : 0xfffff);
996 
997 
998 		/*
999 		 * If we can't setup the IODA PE something has gone horribly
1000 		 * wrong and we can't enable DMA for the device.
1001 		 */
1002 		if (WARN_ON(!pe))
1003 			return;
1004 	} else {
1005 		pci_info(pdev, "Added to existing PE#%x\n", pe->pe_number);
1006 	}
1007 
1008 	/*
1009 	 * We assume that bridges *probably* don't need to do any DMA so we can
1010 	 * skip allocating a TCE table, etc unless we get a non-bridge device.
1011 	 */
1012 	if (!pe->dma_setup_done && !pci_is_bridge(pdev)) {
1013 		switch (phb->type) {
1014 		case PNV_PHB_IODA2:
1015 			pnv_pci_ioda2_setup_dma_pe(phb, pe);
1016 			break;
1017 		default:
1018 			pr_warn("%s: No DMA for PHB#%x (type %d)\n",
1019 				__func__, phb->hose->global_number, phb->type);
1020 		}
1021 	}
1022 
1023 	if (pdn)
1024 		pdn->pe_number = pe->pe_number;
1025 	pe->device_count++;
1026 
1027 	WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
1028 	pdev->dev.archdata.dma_offset = pe->tce_bypass_base;
1029 	set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
1030 
1031 	/* PEs with a DMA weight of zero won't have a group */
1032 	if (pe->table_group.group)
1033 		iommu_add_device(&pe->table_group, &pdev->dev);
1034 }
1035 
1036 /*
1037  * Reconfigure TVE#0 to be usable as 64-bit DMA space.
1038  *
1039  * The first 4GB of virtual memory for a PE is reserved for 32-bit accesses.
1040  * Devices can only access more than that if bit 59 of the PCI address is set
1041  * by hardware, which indicates TVE#1 should be used instead of TVE#0.
1042  * Many PCI devices are not capable of addressing that many bits, and as a
1043  * result are limited to the 4GB of virtual memory made available to 32-bit
1044  * devices in TVE#0.
1045  *
1046  * In order to work around this, reconfigure TVE#0 to be suitable for 64-bit
1047  * devices by configuring the virtual memory past the first 4GB inaccessible
1048  * by 64-bit DMAs.  This should only be used by devices that want more than
1049  * 4GB, and only on PEs that have no 32-bit devices.
1050  *
1051  * Currently this will only work on PHB3 (POWER8).
1052  */
pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe * pe)1053 static int pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe *pe)
1054 {
1055 	u64 window_size, table_size, tce_count, addr;
1056 	struct page *table_pages;
1057 	u64 tce_order = 28; /* 256MB TCEs */
1058 	__be64 *tces;
1059 	s64 rc;
1060 
1061 	/*
1062 	 * Window size needs to be a power of two, but needs to account for
1063 	 * shifting memory by the 4GB offset required to skip 32bit space.
1064 	 */
1065 	window_size = roundup_pow_of_two(memory_hotplug_max() + (1ULL << 32));
1066 	tce_count = window_size >> tce_order;
1067 	table_size = tce_count << 3;
1068 
1069 	if (table_size < PAGE_SIZE)
1070 		table_size = PAGE_SIZE;
1071 
1072 	table_pages = alloc_pages_node(pe->phb->hose->node, GFP_KERNEL,
1073 				       get_order(table_size));
1074 	if (!table_pages)
1075 		goto err;
1076 
1077 	tces = page_address(table_pages);
1078 	if (!tces)
1079 		goto err;
1080 
1081 	memset(tces, 0, table_size);
1082 
1083 	for (addr = 0; addr < memory_hotplug_max(); addr += (1 << tce_order)) {
1084 		tces[(addr + (1ULL << 32)) >> tce_order] =
1085 			cpu_to_be64(addr | TCE_PCI_READ | TCE_PCI_WRITE);
1086 	}
1087 
1088 	rc = opal_pci_map_pe_dma_window(pe->phb->opal_id,
1089 					pe->pe_number,
1090 					/* reconfigure window 0 */
1091 					(pe->pe_number << 1) + 0,
1092 					1,
1093 					__pa(tces),
1094 					table_size,
1095 					1 << tce_order);
1096 	if (rc == OPAL_SUCCESS) {
1097 		pe_info(pe, "Using 64-bit DMA iommu bypass (through TVE#0)\n");
1098 		return 0;
1099 	}
1100 err:
1101 	pe_err(pe, "Error configuring 64-bit DMA bypass\n");
1102 	return -EIO;
1103 }
1104 
pnv_pci_ioda_iommu_bypass_supported(struct pci_dev * pdev,u64 dma_mask)1105 static bool pnv_pci_ioda_iommu_bypass_supported(struct pci_dev *pdev,
1106 		u64 dma_mask)
1107 {
1108 	struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
1109 	struct pci_dn *pdn = pci_get_pdn(pdev);
1110 	struct pnv_ioda_pe *pe;
1111 
1112 	if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1113 		return false;
1114 
1115 	pe = &phb->ioda.pe_array[pdn->pe_number];
1116 	if (pe->tce_bypass_enabled) {
1117 		u64 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1118 		if (dma_mask >= top)
1119 			return true;
1120 	}
1121 
1122 	/*
1123 	 * If the device can't set the TCE bypass bit but still wants
1124 	 * to access 4GB or more, on PHB3 we can reconfigure TVE#0 to
1125 	 * bypass the 32-bit region and be usable for 64-bit DMAs.
1126 	 * The device needs to be able to address all of this space.
1127 	 */
1128 	if (dma_mask >> 32 &&
1129 	    dma_mask > (memory_hotplug_max() + (1ULL << 32)) &&
1130 	    /* pe->pdev should be set if it's a single device, pe->pbus if not */
1131 	    (pe->device_count == 1 || !pe->pbus) &&
1132 	    phb->model == PNV_PHB_MODEL_PHB3) {
1133 		/* Configure the bypass mode */
1134 		s64 rc = pnv_pci_ioda_dma_64bit_bypass(pe);
1135 		if (rc)
1136 			return false;
1137 		/* 4GB offset bypasses 32-bit space */
1138 		pdev->dev.archdata.dma_offset = (1ULL << 32);
1139 		return true;
1140 	}
1141 
1142 	return false;
1143 }
1144 
pnv_ioda_get_inval_reg(struct pnv_phb * phb)1145 static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb)
1146 {
1147 	return phb->regs + 0x210;
1148 }
1149 
1150 #ifdef CONFIG_IOMMU_API
1151 /* Common for IODA1 and IODA2 */
pnv_ioda_tce_xchg_no_kill(struct iommu_table * tbl,long index,unsigned long * hpa,enum dma_data_direction * direction)1152 static int pnv_ioda_tce_xchg_no_kill(struct iommu_table *tbl, long index,
1153 		unsigned long *hpa, enum dma_data_direction *direction)
1154 {
1155 	return pnv_tce_xchg(tbl, index, hpa, direction);
1156 }
1157 #endif
1158 
1159 #define PHB3_TCE_KILL_INVAL_ALL		PPC_BIT(0)
1160 #define PHB3_TCE_KILL_INVAL_PE		PPC_BIT(1)
1161 #define PHB3_TCE_KILL_INVAL_ONE		PPC_BIT(2)
1162 
pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe * pe)1163 static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)
1164 {
1165 	/* 01xb - invalidate TCEs that match the specified PE# */
1166 	__be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb);
1167 	unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
1168 
1169 	mb(); /* Ensure above stores are visible */
1170 	__raw_writeq_be(val, invalidate);
1171 }
1172 
pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe * pe,unsigned shift,unsigned long index,unsigned long npages)1173 static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe,
1174 					unsigned shift, unsigned long index,
1175 					unsigned long npages)
1176 {
1177 	__be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb);
1178 	unsigned long start, end, inc;
1179 
1180 	/* We'll invalidate DMA address in PE scope */
1181 	start = PHB3_TCE_KILL_INVAL_ONE;
1182 	start |= (pe->pe_number & 0xFF);
1183 	end = start;
1184 
1185 	/* Figure out the start, end and step */
1186 	start |= (index << shift);
1187 	end |= ((index + npages - 1) << shift);
1188 	inc = (0x1ull << shift);
1189 	mb();
1190 
1191 	while (start <= end) {
1192 		__raw_writeq_be(start, invalidate);
1193 		start += inc;
1194 	}
1195 }
1196 
pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe * pe)1197 static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
1198 {
1199 	struct pnv_phb *phb = pe->phb;
1200 
1201 	if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1202 		pnv_pci_phb3_tce_invalidate_pe(pe);
1203 	else
1204 		opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE,
1205 				  pe->pe_number, 0, 0, 0);
1206 }
1207 
pnv_pci_ioda2_tce_invalidate(struct iommu_table * tbl,unsigned long index,unsigned long npages)1208 static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1209 		unsigned long index, unsigned long npages)
1210 {
1211 	struct iommu_table_group_link *tgl;
1212 
1213 	list_for_each_entry_lockless(tgl, &tbl->it_group_list, next) {
1214 		struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1215 				struct pnv_ioda_pe, table_group);
1216 		struct pnv_phb *phb = pe->phb;
1217 		unsigned int shift = tbl->it_page_shift;
1218 
1219 		if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1220 			pnv_pci_phb3_tce_invalidate(pe, shift,
1221 						    index, npages);
1222 		else
1223 			opal_pci_tce_kill(phb->opal_id,
1224 					  OPAL_PCI_TCE_KILL_PAGES,
1225 					  pe->pe_number, 1u << shift,
1226 					  index << shift, npages);
1227 	}
1228 }
1229 
pnv_ioda2_tce_build(struct iommu_table * tbl,long index,long npages,unsigned long uaddr,enum dma_data_direction direction,unsigned long attrs)1230 static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1231 		long npages, unsigned long uaddr,
1232 		enum dma_data_direction direction,
1233 		unsigned long attrs)
1234 {
1235 	int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1236 			attrs);
1237 
1238 	if (!ret)
1239 		pnv_pci_ioda2_tce_invalidate(tbl, index, npages);
1240 
1241 	return ret;
1242 }
1243 
pnv_ioda2_tce_free(struct iommu_table * tbl,long index,long npages)1244 static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1245 		long npages)
1246 {
1247 	pnv_tce_free(tbl, index, npages);
1248 
1249 	pnv_pci_ioda2_tce_invalidate(tbl, index, npages);
1250 }
1251 
1252 static struct iommu_table_ops pnv_ioda2_iommu_ops = {
1253 	.set = pnv_ioda2_tce_build,
1254 #ifdef CONFIG_IOMMU_API
1255 	.xchg_no_kill = pnv_ioda_tce_xchg_no_kill,
1256 	.tce_kill = pnv_pci_ioda2_tce_invalidate,
1257 	.useraddrptr = pnv_tce_useraddrptr,
1258 #endif
1259 	.clear = pnv_ioda2_tce_free,
1260 	.get = pnv_tce_get,
1261 	.free = pnv_pci_ioda2_table_free_pages,
1262 };
1263 
pnv_pci_ioda2_set_window(struct iommu_table_group * table_group,int num,struct iommu_table * tbl)1264 static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
1265 		int num, struct iommu_table *tbl)
1266 {
1267 	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1268 			table_group);
1269 	struct pnv_phb *phb = pe->phb;
1270 	int64_t rc;
1271 	const unsigned long size = tbl->it_indirect_levels ?
1272 			tbl->it_level_size : tbl->it_size;
1273 	const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
1274 	const __u64 win_size = tbl->it_size << tbl->it_page_shift;
1275 
1276 	pe_info(pe, "Setting up window#%d %llx..%llx pg=%lx\n",
1277 		num, start_addr, start_addr + win_size - 1,
1278 		IOMMU_PAGE_SIZE(tbl));
1279 
1280 	/*
1281 	 * Map TCE table through TVT. The TVE index is the PE number
1282 	 * shifted by 1 bit for 32-bits DMA space.
1283 	 */
1284 	rc = opal_pci_map_pe_dma_window(phb->opal_id,
1285 			pe->pe_number,
1286 			(pe->pe_number << 1) + num,
1287 			tbl->it_indirect_levels + 1,
1288 			__pa(tbl->it_base),
1289 			size << 3,
1290 			IOMMU_PAGE_SIZE(tbl));
1291 	if (rc) {
1292 		pe_err(pe, "Failed to configure TCE table, err %lld\n", rc);
1293 		return rc;
1294 	}
1295 
1296 	pnv_pci_link_table_and_group(phb->hose->node, num,
1297 			tbl, &pe->table_group);
1298 	pnv_pci_ioda2_tce_invalidate_pe(pe);
1299 
1300 	return 0;
1301 }
1302 
pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe * pe,bool enable)1303 static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
1304 {
1305 	uint16_t window_id = (pe->pe_number << 1 ) + 1;
1306 	int64_t rc;
1307 
1308 	pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1309 	if (enable) {
1310 		phys_addr_t top = memblock_end_of_DRAM();
1311 
1312 		top = roundup_pow_of_two(top);
1313 		rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1314 						     pe->pe_number,
1315 						     window_id,
1316 						     pe->tce_bypass_base,
1317 						     top);
1318 	} else {
1319 		rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1320 						     pe->pe_number,
1321 						     window_id,
1322 						     pe->tce_bypass_base,
1323 						     0);
1324 	}
1325 	if (rc)
1326 		pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1327 	else
1328 		pe->tce_bypass_enabled = enable;
1329 }
1330 
pnv_pci_ioda2_create_table(struct iommu_table_group * table_group,int num,__u32 page_shift,__u64 window_size,__u32 levels,bool alloc_userspace_copy,struct iommu_table ** ptbl)1331 static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
1332 		int num, __u32 page_shift, __u64 window_size, __u32 levels,
1333 		bool alloc_userspace_copy, struct iommu_table **ptbl)
1334 {
1335 	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1336 			table_group);
1337 	int nid = pe->phb->hose->node;
1338 	__u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
1339 	long ret;
1340 	struct iommu_table *tbl;
1341 
1342 	tbl = pnv_pci_table_alloc(nid);
1343 	if (!tbl)
1344 		return -ENOMEM;
1345 
1346 	tbl->it_ops = &pnv_ioda2_iommu_ops;
1347 
1348 	ret = pnv_pci_ioda2_table_alloc_pages(nid,
1349 			bus_offset, page_shift, window_size,
1350 			levels, alloc_userspace_copy, tbl);
1351 	if (ret) {
1352 		iommu_tce_table_put(tbl);
1353 		return ret;
1354 	}
1355 
1356 	*ptbl = tbl;
1357 
1358 	return 0;
1359 }
1360 
pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe * pe)1361 static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
1362 {
1363 	struct iommu_table *tbl = NULL;
1364 	long rc;
1365 	unsigned long res_start, res_end;
1366 
1367 	/*
1368 	 * crashkernel= specifies the kdump kernel's maximum memory at
1369 	 * some offset and there is no guaranteed the result is a power
1370 	 * of 2, which will cause errors later.
1371 	 */
1372 	const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
1373 
1374 	/*
1375 	 * In memory constrained environments, e.g. kdump kernel, the
1376 	 * DMA window can be larger than available memory, which will
1377 	 * cause errors later.
1378 	 */
1379 	const u64 maxblock = 1UL << (PAGE_SHIFT + MAX_PAGE_ORDER);
1380 
1381 	/*
1382 	 * We create the default window as big as we can. The constraint is
1383 	 * the max order of allocation possible. The TCE table is likely to
1384 	 * end up being multilevel and with on-demand allocation in place,
1385 	 * the initial use is not going to be huge as the default window aims
1386 	 * to support crippled devices (i.e. not fully 64bit DMAble) only.
1387 	 */
1388 	/* iommu_table::it_map uses 1 bit per IOMMU page, hence 8 */
1389 	const u64 window_size = min((maxblock * 8) << PAGE_SHIFT, max_memory);
1390 	/* Each TCE level cannot exceed maxblock so go multilevel if needed */
1391 	unsigned long tces_order = ilog2(window_size >> PAGE_SHIFT);
1392 	unsigned long tcelevel_order = ilog2(maxblock >> 3);
1393 	unsigned int levels = tces_order / tcelevel_order;
1394 
1395 	if (tces_order % tcelevel_order)
1396 		levels += 1;
1397 	/*
1398 	 * We try to stick to default levels (which is >1 at the moment) in
1399 	 * order to save memory by relying on on-demain TCE level allocation.
1400 	 */
1401 	levels = max_t(unsigned int, levels, POWERNV_IOMMU_DEFAULT_LEVELS);
1402 
1403 	rc = pnv_pci_ioda2_create_table(&pe->table_group, 0, PAGE_SHIFT,
1404 			window_size, levels, false, &tbl);
1405 	if (rc) {
1406 		pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
1407 				rc);
1408 		return rc;
1409 	}
1410 
1411 	/* We use top part of 32bit space for MMIO so exclude it from DMA */
1412 	res_start = 0;
1413 	res_end = 0;
1414 	if (window_size > pe->phb->ioda.m32_pci_base) {
1415 		res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift;
1416 		res_end = min(window_size, SZ_4G) >> tbl->it_page_shift;
1417 	}
1418 
1419 	tbl->it_index = (pe->phb->hose->global_number << 16) | pe->pe_number;
1420 	if (iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end))
1421 		rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
1422 	else
1423 		rc = -ENOMEM;
1424 	if (rc) {
1425 		pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", rc);
1426 		iommu_tce_table_put(tbl);
1427 		tbl = NULL; /* This clears iommu_table_base below */
1428 	}
1429 	if (!pnv_iommu_bypass_disabled)
1430 		pnv_pci_ioda2_set_bypass(pe, true);
1431 
1432 	/*
1433 	 * Set table base for the case of IOMMU DMA use. Usually this is done
1434 	 * from dma_dev_setup() which is not called when a device is returned
1435 	 * from VFIO so do it here.
1436 	 */
1437 	if (pe->pdev)
1438 		set_iommu_table_base(&pe->pdev->dev, tbl);
1439 
1440 	return 0;
1441 }
1442 
pnv_pci_ioda2_unset_window(struct iommu_table_group * table_group,int num)1443 static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1444 		int num)
1445 {
1446 	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1447 			table_group);
1448 	struct pnv_phb *phb = pe->phb;
1449 	long ret;
1450 
1451 	pe_info(pe, "Removing DMA window #%d\n", num);
1452 
1453 	ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1454 			(pe->pe_number << 1) + num,
1455 			0/* levels */, 0/* table address */,
1456 			0/* table size */, 0/* page size */);
1457 	if (ret)
1458 		pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
1459 	else
1460 		pnv_pci_ioda2_tce_invalidate_pe(pe);
1461 
1462 	pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
1463 
1464 	return ret;
1465 }
1466 
1467 #ifdef CONFIG_IOMMU_API
pnv_pci_ioda2_get_table_size(__u32 page_shift,__u64 window_size,__u32 levels)1468 unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
1469 		__u64 window_size, __u32 levels)
1470 {
1471 	unsigned long bytes = 0;
1472 	const unsigned window_shift = ilog2(window_size);
1473 	unsigned entries_shift = window_shift - page_shift;
1474 	unsigned table_shift = entries_shift + 3;
1475 	unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
1476 	unsigned long direct_table_size;
1477 
1478 	if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
1479 			!is_power_of_2(window_size))
1480 		return 0;
1481 
1482 	/* Calculate a direct table size from window_size and levels */
1483 	entries_shift = (entries_shift + levels - 1) / levels;
1484 	table_shift = entries_shift + 3;
1485 	table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
1486 	direct_table_size =  1UL << table_shift;
1487 
1488 	for ( ; levels; --levels) {
1489 		bytes += ALIGN(tce_table_size, direct_table_size);
1490 
1491 		tce_table_size /= direct_table_size;
1492 		tce_table_size <<= 3;
1493 		tce_table_size = max_t(unsigned long,
1494 				tce_table_size, direct_table_size);
1495 	}
1496 
1497 	return bytes + bytes; /* one for HW table, one for userspace copy */
1498 }
1499 
pnv_pci_ioda2_create_table_userspace(struct iommu_table_group * table_group,int num,__u32 page_shift,__u64 window_size,__u32 levels,struct iommu_table ** ptbl)1500 static long pnv_pci_ioda2_create_table_userspace(
1501 		struct iommu_table_group *table_group,
1502 		int num, __u32 page_shift, __u64 window_size, __u32 levels,
1503 		struct iommu_table **ptbl)
1504 {
1505 	long ret = pnv_pci_ioda2_create_table(table_group,
1506 			num, page_shift, window_size, levels, true, ptbl);
1507 
1508 	if (!ret)
1509 		(*ptbl)->it_allocated_size = pnv_pci_ioda2_get_table_size(
1510 				page_shift, window_size, levels);
1511 	return ret;
1512 }
1513 
pnv_ioda_setup_bus_dma(struct pnv_ioda_pe * pe,struct pci_bus * bus)1514 static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus)
1515 {
1516 	struct pci_dev *dev;
1517 
1518 	list_for_each_entry(dev, &bus->devices, bus_list) {
1519 		set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
1520 		dev->dev.archdata.dma_offset = pe->tce_bypass_base;
1521 
1522 		if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
1523 			pnv_ioda_setup_bus_dma(pe, dev->subordinate);
1524 	}
1525 }
1526 
pnv_ioda2_take_ownership(struct iommu_table_group * table_group,struct device * dev __maybe_unused)1527 static long pnv_ioda2_take_ownership(struct iommu_table_group *table_group,
1528 				     struct device *dev __maybe_unused)
1529 {
1530 	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1531 						table_group);
1532 	/* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
1533 	struct iommu_table *tbl = pe->table_group.tables[0];
1534 
1535 	/*
1536 	 * iommu_ops transfers the ownership per a device and we mode
1537 	 * the group ownership with the first device in the group.
1538 	 */
1539 	if (!tbl)
1540 		return 0;
1541 
1542 	pnv_pci_ioda2_set_bypass(pe, false);
1543 	pnv_pci_ioda2_unset_window(&pe->table_group, 0);
1544 	if (pe->pbus)
1545 		pnv_ioda_setup_bus_dma(pe, pe->pbus);
1546 	else if (pe->pdev)
1547 		set_iommu_table_base(&pe->pdev->dev, NULL);
1548 	iommu_tce_table_put(tbl);
1549 
1550 	return 0;
1551 }
1552 
pnv_ioda2_release_ownership(struct iommu_table_group * table_group,struct device * dev __maybe_unused)1553 static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group,
1554 					struct device *dev __maybe_unused)
1555 {
1556 	struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1557 						table_group);
1558 
1559 	/* See the comment about iommu_ops above */
1560 	if (pe->table_group.tables[0])
1561 		return;
1562 	pnv_pci_ioda2_setup_default_config(pe);
1563 	if (pe->pbus)
1564 		pnv_ioda_setup_bus_dma(pe, pe->pbus);
1565 }
1566 
1567 static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
1568 	.get_table_size = pnv_pci_ioda2_get_table_size,
1569 	.create_table = pnv_pci_ioda2_create_table_userspace,
1570 	.set_window = pnv_pci_ioda2_set_window,
1571 	.unset_window = pnv_pci_ioda2_unset_window,
1572 	.take_ownership = pnv_ioda2_take_ownership,
1573 	.release_ownership = pnv_ioda2_release_ownership,
1574 };
1575 #endif
1576 
pnv_pci_ioda2_setup_dma_pe(struct pnv_phb * phb,struct pnv_ioda_pe * pe)1577 void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1578 				struct pnv_ioda_pe *pe)
1579 {
1580 	int64_t rc;
1581 
1582 	/* TVE #1 is selected by PCI address bit 59 */
1583 	pe->tce_bypass_base = 1ull << 59;
1584 
1585 	/* The PE will reserve all possible 32-bits space */
1586 	pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1587 		phb->ioda.m32_pci_base);
1588 
1589 	/* Setup linux iommu table */
1590 	pe->table_group.tce32_start = 0;
1591 	pe->table_group.tce32_size = phb->ioda.m32_pci_base;
1592 	pe->table_group.max_dynamic_windows_supported =
1593 			IOMMU_TABLE_GROUP_MAX_TABLES;
1594 	pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
1595 	pe->table_group.pgsizes = pnv_ioda_parse_tce_sizes(phb);
1596 
1597 	rc = pnv_pci_ioda2_setup_default_config(pe);
1598 	if (rc)
1599 		return;
1600 
1601 #ifdef CONFIG_IOMMU_API
1602 	pe->table_group.ops = &pnv_pci_ioda2_ops;
1603 	iommu_register_group(&pe->table_group, phb->hose->global_number,
1604 			     pe->pe_number);
1605 #endif
1606 	pe->dma_setup_done = true;
1607 }
1608 
1609 /*
1610  * Called from KVM in real mode to EOI passthru interrupts. The ICP
1611  * EOI is handled directly in KVM in kvmppc_deliver_irq_passthru().
1612  *
1613  * The IRQ data is mapped in the PCI-MSI domain and the EOI OPAL call
1614  * needs an HW IRQ number mapped in the XICS IRQ domain. The HW IRQ
1615  * numbers of the in-the-middle MSI domain are vector numbers and it's
1616  * good enough for OPAL. Use that.
1617  */
pnv_opal_pci_msi_eoi(struct irq_data * d)1618 int64_t pnv_opal_pci_msi_eoi(struct irq_data *d)
1619 {
1620 	struct pci_controller *hose = irq_data_get_irq_chip_data(d->parent_data);
1621 	struct pnv_phb *phb = hose->private_data;
1622 
1623 	return opal_pci_msi_eoi(phb->opal_id, d->parent_data->hwirq);
1624 }
1625 
1626 static struct irq_chip pnv_pci_msi_irq_chip;
1627 
1628 /*
1629  * Returns true iff chip is something that we could call
1630  * pnv_opal_pci_msi_eoi for.
1631  */
is_pnv_opal_msi(struct irq_chip * chip)1632 bool is_pnv_opal_msi(struct irq_chip *chip)
1633 {
1634 	return chip == &pnv_pci_msi_irq_chip;
1635 }
1636 EXPORT_SYMBOL_GPL(is_pnv_opal_msi);
1637 
__pnv_pci_ioda_msi_setup(struct pnv_phb * phb,struct pci_dev * dev,unsigned int xive_num,unsigned int is_64,struct msi_msg * msg)1638 static int __pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
1639 				    unsigned int xive_num,
1640 				    unsigned int is_64, struct msi_msg *msg)
1641 {
1642 	struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1643 	__be32 data;
1644 	int rc;
1645 
1646 	dev_dbg(&dev->dev, "%s: setup %s-bit MSI for vector #%d\n", __func__,
1647 		is_64 ? "64" : "32", xive_num);
1648 
1649 	/* No PE assigned ? bail out ... no MSI for you ! */
1650 	if (pe == NULL)
1651 		return -ENXIO;
1652 
1653 	/* Check if we have an MVE */
1654 	if (pe->mve_number < 0)
1655 		return -ENXIO;
1656 
1657 	/* Force 32-bit MSI on some broken devices */
1658 	if (dev->msi_addr_mask < DMA_BIT_MASK(64))
1659 		is_64 = 0;
1660 
1661 	/* Assign XIVE to PE */
1662 	rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1663 	if (rc) {
1664 		pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1665 			pci_name(dev), rc, xive_num);
1666 		return -EIO;
1667 	}
1668 
1669 	if (is_64) {
1670 		__be64 addr64;
1671 
1672 		rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1673 				     &addr64, &data);
1674 		if (rc) {
1675 			pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1676 				pci_name(dev), rc);
1677 			return -EIO;
1678 		}
1679 		msg->address_hi = be64_to_cpu(addr64) >> 32;
1680 		msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
1681 	} else {
1682 		__be32 addr32;
1683 
1684 		rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1685 				     &addr32, &data);
1686 		if (rc) {
1687 			pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1688 				pci_name(dev), rc);
1689 			return -EIO;
1690 		}
1691 		msg->address_hi = 0;
1692 		msg->address_lo = be32_to_cpu(addr32);
1693 	}
1694 	msg->data = be32_to_cpu(data);
1695 
1696 	return 0;
1697 }
1698 
pnv_msi_shutdown(struct irq_data * d)1699 static void pnv_msi_shutdown(struct irq_data *d)
1700 {
1701 	d = d->parent_data;
1702 	if (d->chip->irq_shutdown)
1703 		d->chip->irq_shutdown(d);
1704 }
1705 
pnv_init_dev_msi_info(struct device * dev,struct irq_domain * domain,struct irq_domain * real_parent,struct msi_domain_info * info)1706 static bool pnv_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
1707 				  struct irq_domain *real_parent, struct msi_domain_info *info)
1708 {
1709 	struct irq_chip *chip = info->chip;
1710 
1711 	if (!msi_lib_init_dev_msi_info(dev, domain, real_parent, info))
1712 		return false;
1713 
1714 	chip->irq_shutdown = pnv_msi_shutdown;
1715 	return true;
1716 }
1717 
1718 #define PNV_PCI_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS		| \
1719 				    MSI_FLAG_USE_DEF_CHIP_OPS		| \
1720 				    MSI_FLAG_PCI_MSI_MASK_PARENT)
1721 #define PNV_PCI_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK		| \
1722 				     MSI_FLAG_PCI_MSIX			| \
1723 				     MSI_FLAG_MULTI_PCI_MSI)
1724 
1725 static const struct msi_parent_ops pnv_msi_parent_ops = {
1726 	.required_flags		= PNV_PCI_MSI_FLAGS_REQUIRED,
1727 	.supported_flags	= PNV_PCI_MSI_FLAGS_SUPPORTED,
1728 	.chip_flags		= MSI_CHIP_FLAG_SET_EOI,
1729 	.bus_select_token	= DOMAIN_BUS_NEXUS,
1730 	.bus_select_mask	= MATCH_PCI_MSI,
1731 	.prefix			= "PNV-",
1732 	.init_dev_msi_info	= pnv_init_dev_msi_info,
1733 };
1734 
pnv_msi_compose_msg(struct irq_data * d,struct msi_msg * msg)1735 static void pnv_msi_compose_msg(struct irq_data *d, struct msi_msg *msg)
1736 {
1737 	struct msi_desc *entry = irq_data_get_msi_desc(d);
1738 	struct pci_dev *pdev = msi_desc_to_pci_dev(entry);
1739 	struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1740 	struct pnv_phb *phb = hose->private_data;
1741 	int rc;
1742 
1743 	rc = __pnv_pci_ioda_msi_setup(phb, pdev, d->hwirq,
1744 				      entry->pci.msi_attrib.is_64, msg);
1745 	if (rc)
1746 		dev_err(&pdev->dev, "Failed to setup %s-bit MSI #%ld : %d\n",
1747 			entry->pci.msi_attrib.is_64 ? "64" : "32", d->hwirq, rc);
1748 }
1749 
1750 /*
1751  * The IRQ data is mapped in the MSI domain in which HW IRQ numbers
1752  * correspond to vector numbers.
1753  */
pnv_msi_eoi(struct irq_data * d)1754 static void pnv_msi_eoi(struct irq_data *d)
1755 {
1756 	struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1757 	struct pnv_phb *phb = hose->private_data;
1758 
1759 	if (phb->model == PNV_PHB_MODEL_PHB3) {
1760 		/*
1761 		 * The EOI OPAL call takes an OPAL HW IRQ number but
1762 		 * since it is translated into a vector number in
1763 		 * OPAL, use that directly.
1764 		 */
1765 		WARN_ON_ONCE(opal_pci_msi_eoi(phb->opal_id, d->hwirq));
1766 	}
1767 
1768 	irq_chip_eoi_parent(d);
1769 }
1770 
1771 static struct irq_chip pnv_msi_irq_chip = {
1772 	.name			= "PNV-MSI",
1773 	.irq_shutdown		= pnv_msi_shutdown,
1774 	.irq_mask		= irq_chip_mask_parent,
1775 	.irq_unmask		= irq_chip_unmask_parent,
1776 	.irq_eoi		= pnv_msi_eoi,
1777 	.irq_set_affinity	= irq_chip_set_affinity_parent,
1778 	.irq_compose_msi_msg	= pnv_msi_compose_msg,
1779 };
1780 
pnv_irq_parent_domain_alloc(struct irq_domain * domain,unsigned int virq,int hwirq)1781 static int pnv_irq_parent_domain_alloc(struct irq_domain *domain,
1782 				       unsigned int virq, int hwirq)
1783 {
1784 	struct irq_fwspec parent_fwspec;
1785 	int ret;
1786 
1787 	parent_fwspec.fwnode = domain->parent->fwnode;
1788 	parent_fwspec.param_count = 2;
1789 	parent_fwspec.param[0] = hwirq;
1790 	parent_fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1791 
1792 	ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
1793 	if (ret)
1794 		return ret;
1795 
1796 	return 0;
1797 }
1798 
pnv_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)1799 static int pnv_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1800 				unsigned int nr_irqs, void *arg)
1801 {
1802 	struct pci_controller *hose = domain->host_data;
1803 	struct pnv_phb *phb = hose->private_data;
1804 	msi_alloc_info_t *info = arg;
1805 	struct pci_dev *pdev = msi_desc_to_pci_dev(info->desc);
1806 	int hwirq;
1807 	int i, ret;
1808 
1809 	hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, nr_irqs);
1810 	if (hwirq < 0) {
1811 		dev_warn(&pdev->dev, "failed to find a free MSI\n");
1812 		return -ENOSPC;
1813 	}
1814 
1815 	dev_dbg(&pdev->dev, "%s bridge %pOF %d/%x #%d\n", __func__,
1816 		hose->dn, virq, hwirq, nr_irqs);
1817 
1818 	for (i = 0; i < nr_irqs; i++) {
1819 		ret = pnv_irq_parent_domain_alloc(domain, virq + i,
1820 						  phb->msi_base + hwirq + i);
1821 		if (ret)
1822 			goto out;
1823 
1824 		irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
1825 					      &pnv_msi_irq_chip, hose);
1826 	}
1827 
1828 	return 0;
1829 
1830 out:
1831 	irq_domain_free_irqs_parent(domain, virq, i);
1832 	msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, nr_irqs);
1833 	return ret;
1834 }
1835 
pnv_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)1836 static void pnv_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1837 				unsigned int nr_irqs)
1838 {
1839 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1840 	struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1841 	struct pnv_phb *phb = hose->private_data;
1842 
1843 	pr_debug("%s bridge %pOF %d/%lx #%d\n", __func__, hose->dn,
1844 		 virq, d->hwirq, nr_irqs);
1845 
1846 	msi_bitmap_free_hwirqs(&phb->msi_bmp, d->hwirq, nr_irqs);
1847 	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1848 }
1849 
1850 static const struct irq_domain_ops pnv_irq_domain_ops = {
1851 	.select	= msi_lib_irq_domain_select,
1852 	.alloc  = pnv_irq_domain_alloc,
1853 	.free   = pnv_irq_domain_free,
1854 };
1855 
pnv_msi_allocate_domains(struct pci_controller * hose,unsigned int count)1856 static int __init pnv_msi_allocate_domains(struct pci_controller *hose, unsigned int count)
1857 {
1858 	struct irq_domain *parent = irq_get_default_domain();
1859 	struct irq_domain_info info = {
1860 		.fwnode		= of_fwnode_handle(hose->dn),
1861 		.ops		= &pnv_irq_domain_ops,
1862 		.host_data	= hose,
1863 		.size		= count,
1864 		.parent		= parent,
1865 	};
1866 
1867 	hose->dev_domain = msi_create_parent_irq_domain(&info, &pnv_msi_parent_ops);
1868 	if (!hose->dev_domain) {
1869 		pr_err("PCI: failed to create MSI IRQ domain bridge %pOF (domain %d)\n",
1870 		       hose->dn, hose->global_number);
1871 		return -ENOMEM;
1872 	}
1873 
1874 	return 0;
1875 }
1876 
pnv_pci_init_ioda_msis(struct pnv_phb * phb)1877 static void __init pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1878 {
1879 	unsigned int count;
1880 	const __be32 *prop = of_get_property(phb->hose->dn,
1881 					     "ibm,opal-msi-ranges", NULL);
1882 	if (!prop) {
1883 		/* BML Fallback */
1884 		prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1885 	}
1886 	if (!prop)
1887 		return;
1888 
1889 	phb->msi_base = be32_to_cpup(prop);
1890 	count = be32_to_cpup(prop + 1);
1891 	if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
1892 		pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1893 		       phb->hose->global_number);
1894 		return;
1895 	}
1896 
1897 	pr_info("  Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
1898 		count, phb->msi_base);
1899 
1900 	pnv_msi_allocate_domains(phb->hose, count);
1901 }
1902 
pnv_ioda_setup_pe_res(struct pnv_ioda_pe * pe,struct resource * res)1903 static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
1904 				  struct resource *res)
1905 {
1906 	struct pnv_phb *phb = pe->phb;
1907 	struct pci_bus_region region;
1908 	int index;
1909 	int64_t rc;
1910 
1911 	if (!res || !res->flags || res->start > res->end ||
1912 	    res->flags & IORESOURCE_UNSET)
1913 		return;
1914 
1915 	if (res->flags & IORESOURCE_IO) {
1916 		region.start = res->start - phb->ioda.io_pci_base;
1917 		region.end   = res->end - phb->ioda.io_pci_base;
1918 		index = region.start / phb->ioda.io_segsize;
1919 
1920 		while (index < phb->ioda.total_pe_num &&
1921 		       region.start <= region.end) {
1922 			phb->ioda.io_segmap[index] = pe->pe_number;
1923 			rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1924 				pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
1925 			if (rc != OPAL_SUCCESS) {
1926 				pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",
1927 				       __func__, rc, index, pe->pe_number);
1928 				break;
1929 			}
1930 
1931 			region.start += phb->ioda.io_segsize;
1932 			index++;
1933 		}
1934 	} else if ((res->flags & IORESOURCE_MEM) &&
1935 		   !pnv_pci_is_m64(phb, res)) {
1936 		region.start = res->start -
1937 			       phb->hose->mem_offset[0] -
1938 			       phb->ioda.m32_pci_base;
1939 		region.end   = res->end -
1940 			       phb->hose->mem_offset[0] -
1941 			       phb->ioda.m32_pci_base;
1942 		index = region.start / phb->ioda.m32_segsize;
1943 
1944 		while (index < phb->ioda.total_pe_num &&
1945 		       region.start <= region.end) {
1946 			phb->ioda.m32_segmap[index] = pe->pe_number;
1947 			rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1948 				pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
1949 			if (rc != OPAL_SUCCESS) {
1950 				pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",
1951 				       __func__, rc, index, pe->pe_number);
1952 				break;
1953 			}
1954 
1955 			region.start += phb->ioda.m32_segsize;
1956 			index++;
1957 		}
1958 	}
1959 }
1960 
1961 /*
1962  * This function is supposed to be called on basis of PE from top
1963  * to bottom style. So the I/O or MMIO segment assigned to
1964  * parent PE could be overridden by its child PEs if necessary.
1965  */
pnv_ioda_setup_pe_seg(struct pnv_ioda_pe * pe)1966 static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
1967 {
1968 	struct pci_dev *pdev;
1969 	int i;
1970 
1971 	/*
1972 	 * NOTE: We only care PCI bus based PE for now. For PCI
1973 	 * device based PE, for example SRIOV sensitive VF should
1974 	 * be figured out later.
1975 	 */
1976 	BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
1977 
1978 	list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
1979 		for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1980 			pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
1981 
1982 		/*
1983 		 * If the PE contains all subordinate PCI buses, the
1984 		 * windows of the child bridges should be mapped to
1985 		 * the PE as well.
1986 		 */
1987 		if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
1988 			continue;
1989 		for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
1990 			pnv_ioda_setup_pe_res(pe,
1991 				&pdev->resource[PCI_BRIDGE_RESOURCES + i]);
1992 	}
1993 }
1994 
1995 #ifdef CONFIG_DEBUG_FS
pnv_pci_diag_data_set(void * data,u64 val)1996 static int pnv_pci_diag_data_set(void *data, u64 val)
1997 {
1998 	struct pnv_phb *phb = data;
1999 	s64 ret;
2000 
2001 	/* Retrieve the diag data from firmware */
2002 	ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
2003 					  phb->diag_data_size);
2004 	if (ret != OPAL_SUCCESS)
2005 		return -EIO;
2006 
2007 	/* Print the diag data to the kernel log */
2008 	pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
2009 	return 0;
2010 }
2011 
2012 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_diag_data_fops, NULL, pnv_pci_diag_data_set,
2013 			 "%llu\n");
2014 
pnv_pci_ioda_pe_dump(void * data,u64 val)2015 static int pnv_pci_ioda_pe_dump(void *data, u64 val)
2016 {
2017 	struct pnv_phb *phb = data;
2018 	int pe_num;
2019 
2020 	for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
2021 		struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_num];
2022 
2023 		if (!test_bit(pe_num, phb->ioda.pe_alloc))
2024 			continue;
2025 
2026 		pe_warn(pe, "rid: %04x dev count: %2d flags: %s%s%s%s%s%s\n",
2027 			pe->rid, pe->device_count,
2028 			(pe->flags & PNV_IODA_PE_DEV) ? "dev " : "",
2029 			(pe->flags & PNV_IODA_PE_BUS) ? "bus " : "",
2030 			(pe->flags & PNV_IODA_PE_BUS_ALL) ? "all " : "",
2031 			(pe->flags & PNV_IODA_PE_MASTER) ? "master " : "",
2032 			(pe->flags & PNV_IODA_PE_SLAVE) ? "slave " : "",
2033 			(pe->flags & PNV_IODA_PE_VF) ? "vf " : "");
2034 	}
2035 
2036 	return 0;
2037 }
2038 
2039 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_ioda_pe_dump_fops, NULL,
2040 			 pnv_pci_ioda_pe_dump, "%llu\n");
2041 
2042 #endif /* CONFIG_DEBUG_FS */
2043 
pnv_pci_ioda_create_dbgfs(void)2044 static void pnv_pci_ioda_create_dbgfs(void)
2045 {
2046 #ifdef CONFIG_DEBUG_FS
2047 	struct pci_controller *hose, *tmp;
2048 	struct pnv_phb *phb;
2049 	char name[16];
2050 
2051 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2052 		phb = hose->private_data;
2053 
2054 		sprintf(name, "PCI%04x", hose->global_number);
2055 		phb->dbgfs = debugfs_create_dir(name, arch_debugfs_dir);
2056 
2057 		debugfs_create_file_unsafe("dump_diag_regs", 0200, phb->dbgfs,
2058 					   phb, &pnv_pci_diag_data_fops);
2059 		debugfs_create_file_unsafe("dump_ioda_pe_state", 0200, phb->dbgfs,
2060 					   phb, &pnv_pci_ioda_pe_dump_fops);
2061 	}
2062 #endif /* CONFIG_DEBUG_FS */
2063 }
2064 
pnv_pci_enable_bridge(struct pci_bus * bus)2065 static void pnv_pci_enable_bridge(struct pci_bus *bus)
2066 {
2067 	struct pci_dev *dev = bus->self;
2068 	struct pci_bus *child;
2069 
2070 	/* Empty bus ? bail */
2071 	if (list_empty(&bus->devices))
2072 		return;
2073 
2074 	/*
2075 	 * If there's a bridge associated with that bus enable it. This works
2076 	 * around races in the generic code if the enabling is done during
2077 	 * parallel probing. This can be removed once those races have been
2078 	 * fixed.
2079 	 */
2080 	if (dev) {
2081 		int rc = pci_enable_device(dev);
2082 		if (rc)
2083 			pci_err(dev, "Error enabling bridge (%d)\n", rc);
2084 		pci_set_master(dev);
2085 	}
2086 
2087 	/* Perform the same to child busses */
2088 	list_for_each_entry(child, &bus->children, node)
2089 		pnv_pci_enable_bridge(child);
2090 }
2091 
pnv_pci_enable_bridges(void)2092 static void pnv_pci_enable_bridges(void)
2093 {
2094 	struct pci_controller *hose;
2095 
2096 	list_for_each_entry(hose, &hose_list, list_node)
2097 		pnv_pci_enable_bridge(hose->bus);
2098 }
2099 
pnv_pci_ioda_fixup(void)2100 static void pnv_pci_ioda_fixup(void)
2101 {
2102 	pnv_pci_ioda_create_dbgfs();
2103 
2104 	pnv_pci_enable_bridges();
2105 
2106 #ifdef CONFIG_EEH
2107 	pnv_eeh_post_init();
2108 #endif
2109 }
2110 
2111 /*
2112  * Returns the alignment for I/O or memory windows for P2P
2113  * bridges. That actually depends on how PEs are segmented.
2114  * For now, we return I/O or M32 segment size for PE sensitive
2115  * P2P bridges. Otherwise, the default values (4KiB for I/O,
2116  * 1MiB for memory) will be returned.
2117  *
2118  * The current PCI bus might be put into one PE, which was
2119  * create against the parent PCI bridge. For that case, we
2120  * needn't enlarge the alignment so that we can save some
2121  * resources.
2122  */
pnv_pci_window_alignment(struct pci_bus * bus,unsigned long type)2123 static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2124 						unsigned long type)
2125 {
2126 	struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
2127 	int num_pci_bridges = 0;
2128 	struct pci_dev *bridge;
2129 
2130 	bridge = bus->self;
2131 	while (bridge) {
2132 		if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2133 			num_pci_bridges++;
2134 			if (num_pci_bridges >= 2)
2135 				return 1;
2136 		}
2137 
2138 		bridge = bridge->bus->self;
2139 	}
2140 
2141 	/*
2142 	 * We fall back to M32 if M64 isn't supported. We enforce the M64
2143 	 * alignment for any 64-bit resource, PCIe doesn't care and
2144 	 * bridges only do 64-bit prefetchable anyway.
2145 	 */
2146 	if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type))
2147 		return phb->ioda.m64_segsize;
2148 	if (type & IORESOURCE_MEM)
2149 		return phb->ioda.m32_segsize;
2150 
2151 	return phb->ioda.io_segsize;
2152 }
2153 
2154 /*
2155  * We are updating root port or the upstream port of the
2156  * bridge behind the root port with PHB's windows in order
2157  * to accommodate the changes on required resources during
2158  * PCI (slot) hotplug, which is connected to either root
2159  * port or the downstream ports of PCIe switch behind the
2160  * root port.
2161  */
pnv_pci_fixup_bridge_resources(struct pci_bus * bus,unsigned long type)2162 static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
2163 					   unsigned long type)
2164 {
2165 	struct pci_controller *hose = pci_bus_to_host(bus);
2166 	struct pnv_phb *phb = hose->private_data;
2167 	struct pci_dev *bridge = bus->self;
2168 	struct resource *r, *w;
2169 	bool msi_region = false;
2170 	int i;
2171 
2172 	/* Check if we need apply fixup to the bridge's windows */
2173 	if (!pci_is_root_bus(bridge->bus) &&
2174 	    !pci_is_root_bus(bridge->bus->self->bus))
2175 		return;
2176 
2177 	/* Fixup the resources */
2178 	for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
2179 		r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
2180 		if (!r->flags || !r->parent)
2181 			continue;
2182 
2183 		w = NULL;
2184 		if (r->flags & type & IORESOURCE_IO)
2185 			w = &hose->io_resource;
2186 		else if (pnv_pci_is_m64(phb, r) &&
2187 			 (type & IORESOURCE_PREFETCH) &&
2188 			 phb->ioda.m64_segsize)
2189 			w = &hose->mem_resources[1];
2190 		else if (r->flags & type & IORESOURCE_MEM) {
2191 			w = &hose->mem_resources[0];
2192 			msi_region = true;
2193 		}
2194 
2195 		r->start = w->start;
2196 		r->end = w->end;
2197 
2198 		/* The 64KB 32-bits MSI region shouldn't be included in
2199 		 * the 32-bits bridge window. Otherwise, we can see strange
2200 		 * issues. One of them is EEH error observed on Garrison.
2201 		 *
2202 		 * Exclude top 1MB region which is the minimal alignment of
2203 		 * 32-bits bridge window.
2204 		 */
2205 		if (msi_region) {
2206 			r->end += 0x10000;
2207 			r->end -= 0x100000;
2208 		}
2209 	}
2210 }
2211 
pnv_pci_configure_bus(struct pci_bus * bus)2212 static void pnv_pci_configure_bus(struct pci_bus *bus)
2213 {
2214 	struct pci_dev *bridge = bus->self;
2215 	struct pnv_ioda_pe *pe;
2216 	bool all = (bridge && pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
2217 
2218 	dev_info(&bus->dev, "Configuring PE for bus\n");
2219 
2220 	/* Don't assign PE to PCI bus, which doesn't have subordinate devices */
2221 	if (WARN_ON(list_empty(&bus->devices)))
2222 		return;
2223 
2224 	/* Reserve PEs according to used M64 resources */
2225 	pnv_ioda_reserve_m64_pe(bus, NULL, all);
2226 
2227 	/*
2228 	 * Assign PE. We might run here because of partial hotplug.
2229 	 * For the case, we just pick up the existing PE and should
2230 	 * not allocate resources again.
2231 	 */
2232 	pe = pnv_ioda_setup_bus_PE(bus, all);
2233 	if (!pe)
2234 		return;
2235 
2236 	pnv_ioda_setup_pe_seg(pe);
2237 }
2238 
pnv_pci_default_alignment(void)2239 static resource_size_t pnv_pci_default_alignment(void)
2240 {
2241 	return PAGE_SIZE;
2242 }
2243 
2244 /* Prevent enabling devices for which we couldn't properly
2245  * assign a PE
2246  */
pnv_pci_enable_device_hook(struct pci_dev * dev)2247 static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
2248 {
2249 	struct pci_dn *pdn;
2250 
2251 	pdn = pci_get_pdn(dev);
2252 	if (!pdn || pdn->pe_number == IODA_INVALID_PE) {
2253 		pci_err(dev, "pci_enable_device() blocked, no PE assigned.\n");
2254 		return false;
2255 	}
2256 
2257 	return true;
2258 }
2259 
pnv_ocapi_enable_device_hook(struct pci_dev * dev)2260 static bool pnv_ocapi_enable_device_hook(struct pci_dev *dev)
2261 {
2262 	struct pci_dn *pdn;
2263 	struct pnv_ioda_pe *pe;
2264 
2265 	pdn = pci_get_pdn(dev);
2266 	if (!pdn)
2267 		return false;
2268 
2269 	if (pdn->pe_number == IODA_INVALID_PE) {
2270 		pe = pnv_ioda_setup_dev_PE(dev);
2271 		if (!pe)
2272 			return false;
2273 	}
2274 	return true;
2275 }
2276 
pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe * pe)2277 void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
2278 {
2279 	struct iommu_table *tbl = pe->table_group.tables[0];
2280 	int64_t rc;
2281 
2282 	if (!pe->dma_setup_done)
2283 		return;
2284 
2285 	rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2286 	if (rc)
2287 		pe_warn(pe, "OPAL error %lld release DMA window\n", rc);
2288 
2289 	pnv_pci_ioda2_set_bypass(pe, false);
2290 	if (pe->table_group.group) {
2291 		iommu_group_put(pe->table_group.group);
2292 		WARN_ON(pe->table_group.group);
2293 	}
2294 
2295 	iommu_tce_table_put(tbl);
2296 }
2297 
pnv_ioda_free_pe_seg(struct pnv_ioda_pe * pe,unsigned short win,unsigned int * map)2298 static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
2299 				 unsigned short win,
2300 				 unsigned int *map)
2301 {
2302 	struct pnv_phb *phb = pe->phb;
2303 	int idx;
2304 	int64_t rc;
2305 
2306 	for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
2307 		if (map[idx] != pe->pe_number)
2308 			continue;
2309 
2310 		rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2311 				phb->ioda.reserved_pe_idx, win, 0, idx);
2312 
2313 		if (rc != OPAL_SUCCESS)
2314 			pe_warn(pe, "Error %lld unmapping (%d) segment#%d\n",
2315 				rc, win, idx);
2316 
2317 		map[idx] = IODA_INVALID_PE;
2318 	}
2319 }
2320 
pnv_ioda_release_pe_seg(struct pnv_ioda_pe * pe)2321 static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
2322 {
2323 	struct pnv_phb *phb = pe->phb;
2324 
2325 	if (phb->type == PNV_PHB_IODA2) {
2326 		pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
2327 				     phb->ioda.m32_segmap);
2328 	}
2329 }
2330 
pnv_ioda_release_pe(struct pnv_ioda_pe * pe)2331 static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
2332 {
2333 	struct pnv_phb *phb = pe->phb;
2334 	struct pnv_ioda_pe *slave, *tmp;
2335 
2336 	pe_info(pe, "Releasing PE\n");
2337 
2338 	mutex_lock(&phb->ioda.pe_list_mutex);
2339 	list_del(&pe->list);
2340 	mutex_unlock(&phb->ioda.pe_list_mutex);
2341 
2342 	switch (phb->type) {
2343 	case PNV_PHB_IODA2:
2344 		pnv_pci_ioda2_release_pe_dma(pe);
2345 		break;
2346 	case PNV_PHB_NPU_OCAPI:
2347 		break;
2348 	default:
2349 		WARN_ON(1);
2350 	}
2351 
2352 	pnv_ioda_release_pe_seg(pe);
2353 	pnv_ioda_deconfigure_pe(pe->phb, pe);
2354 
2355 	/* Release slave PEs in the compound PE */
2356 	if (pe->flags & PNV_IODA_PE_MASTER) {
2357 		list_for_each_entry_safe(slave, tmp, &pe->slaves, list) {
2358 			list_del(&slave->list);
2359 			pnv_ioda_free_pe(slave);
2360 		}
2361 	}
2362 
2363 	/*
2364 	 * The PE for root bus can be removed because of hotplug in EEH
2365 	 * recovery for fenced PHB error. We need to mark the PE dead so
2366 	 * that it can be populated again in PCI hot add path. The PE
2367 	 * shouldn't be destroyed as it's the global reserved resource.
2368 	 */
2369 	if (phb->ioda.root_pe_idx == pe->pe_number)
2370 		return;
2371 
2372 	pnv_ioda_free_pe(pe);
2373 }
2374 
pnv_pci_release_device(struct pci_dev * pdev)2375 static void pnv_pci_release_device(struct pci_dev *pdev)
2376 {
2377 	struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
2378 	struct pci_dn *pdn = pci_get_pdn(pdev);
2379 	struct pnv_ioda_pe *pe;
2380 
2381 	/* The VF PE state is torn down when sriov_disable() is called */
2382 	if (pdev->is_virtfn)
2383 		return;
2384 
2385 	if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2386 		return;
2387 
2388 #ifdef CONFIG_PCI_IOV
2389 	/*
2390 	 * FIXME: Try move this to sriov_disable(). It's here since we allocate
2391 	 * the iov state at probe time since we need to fiddle with the IOV
2392 	 * resources.
2393 	 */
2394 	if (pdev->is_physfn)
2395 		kfree(pdev->dev.archdata.iov_data);
2396 #endif
2397 
2398 	/*
2399 	 * PCI hotplug can happen as part of EEH error recovery. The @pdn
2400 	 * isn't removed and added afterwards in this scenario. We should
2401 	 * set the PE number in @pdn to an invalid one. Otherwise, the PE's
2402 	 * device count is decreased on removing devices while failing to
2403 	 * be increased on adding devices. It leads to unbalanced PE's device
2404 	 * count and eventually make normal PCI hotplug path broken.
2405 	 */
2406 	pe = &phb->ioda.pe_array[pdn->pe_number];
2407 	pdn->pe_number = IODA_INVALID_PE;
2408 
2409 	WARN_ON(--pe->device_count < 0);
2410 	if (pe->device_count == 0)
2411 		pnv_ioda_release_pe(pe);
2412 }
2413 
pnv_pci_ioda_shutdown(struct pci_controller * hose)2414 static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
2415 {
2416 	struct pnv_phb *phb = hose->private_data;
2417 
2418 	opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
2419 		       OPAL_ASSERT_RESET);
2420 }
2421 
pnv_pci_ioda_dma_bus_setup(struct pci_bus * bus)2422 static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus)
2423 {
2424 	struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
2425 	struct pnv_ioda_pe *pe;
2426 
2427 	list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2428 		if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
2429 			continue;
2430 
2431 		if (!pe->pbus)
2432 			continue;
2433 
2434 		if (bus->number == ((pe->rid >> 8) & 0xFF)) {
2435 			pe->pbus = bus;
2436 			break;
2437 		}
2438 	}
2439 }
2440 
2441 #ifdef CONFIG_IOMMU_API
pnv_pci_device_group(struct pci_controller * hose,struct pci_dev * pdev)2442 static struct iommu_group *pnv_pci_device_group(struct pci_controller *hose,
2443 						struct pci_dev *pdev)
2444 {
2445 	struct pnv_phb *phb = hose->private_data;
2446 	struct pnv_ioda_pe *pe;
2447 
2448 	if (WARN_ON(!phb))
2449 		return ERR_PTR(-ENODEV);
2450 
2451 	pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));
2452 	if (!pe)
2453 		return ERR_PTR(-ENODEV);
2454 
2455 	if (!pe->table_group.group)
2456 		return ERR_PTR(-ENODEV);
2457 
2458 	return iommu_group_ref_get(pe->table_group.group);
2459 }
2460 #endif
2461 
2462 static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
2463 	.dma_dev_setup		= pnv_pci_ioda_dma_dev_setup,
2464 	.dma_bus_setup		= pnv_pci_ioda_dma_bus_setup,
2465 	.iommu_bypass_supported	= pnv_pci_ioda_iommu_bypass_supported,
2466 	.enable_device_hook	= pnv_pci_enable_device_hook,
2467 	.release_device		= pnv_pci_release_device,
2468 	.window_alignment	= pnv_pci_window_alignment,
2469 	.setup_bridge		= pnv_pci_fixup_bridge_resources,
2470 	.reset_secondary_bus	= pnv_pci_reset_secondary_bus,
2471 	.shutdown		= pnv_pci_ioda_shutdown,
2472 #ifdef CONFIG_IOMMU_API
2473 	.device_group		= pnv_pci_device_group,
2474 #endif
2475 };
2476 
2477 static const struct pci_controller_ops pnv_npu_ocapi_ioda_controller_ops = {
2478 	.enable_device_hook	= pnv_ocapi_enable_device_hook,
2479 	.release_device		= pnv_pci_release_device,
2480 	.window_alignment	= pnv_pci_window_alignment,
2481 	.reset_secondary_bus	= pnv_pci_reset_secondary_bus,
2482 	.shutdown		= pnv_pci_ioda_shutdown,
2483 };
2484 
pnv_pci_init_ioda_phb(struct device_node * np,u64 hub_id,int ioda_type)2485 static void __init pnv_pci_init_ioda_phb(struct device_node *np,
2486 					 u64 hub_id, int ioda_type)
2487 {
2488 	struct pci_controller *hose;
2489 	struct pnv_phb *phb;
2490 	unsigned long size, m64map_off, m32map_off, pemap_off;
2491 	struct pnv_ioda_pe *root_pe;
2492 	struct resource r;
2493 	const __be64 *prop64;
2494 	const __be32 *prop32;
2495 	int len;
2496 	unsigned int segno;
2497 	u64 phb_id;
2498 	void *aux;
2499 	long rc;
2500 
2501 	if (!of_device_is_available(np))
2502 		return;
2503 
2504 	pr_info("Initializing %s PHB (%pOF)\n",	pnv_phb_names[ioda_type], np);
2505 
2506 	prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
2507 	if (!prop64) {
2508 		pr_err("  Missing \"ibm,opal-phbid\" property !\n");
2509 		return;
2510 	}
2511 	phb_id = be64_to_cpup(prop64);
2512 	pr_debug("  PHB-ID  : 0x%016llx\n", phb_id);
2513 
2514 	phb = kzalloc_obj(*phb);
2515 	if (!phb)
2516 		panic("%s: Failed to allocate %zu bytes\n", __func__,
2517 		      sizeof(*phb));
2518 
2519 	/* Allocate PCI controller */
2520 	phb->hose = hose = pcibios_alloc_controller(np);
2521 	if (!phb->hose) {
2522 		pr_err("  Can't allocate PCI controller for %pOF\n",
2523 		       np);
2524 		memblock_free(phb, sizeof(struct pnv_phb));
2525 		return;
2526 	}
2527 
2528 	spin_lock_init(&phb->lock);
2529 	prop32 = of_get_property(np, "bus-range", &len);
2530 	if (prop32 && len == 8) {
2531 		hose->first_busno = be32_to_cpu(prop32[0]);
2532 		hose->last_busno = be32_to_cpu(prop32[1]);
2533 	} else {
2534 		pr_warn("  Broken <bus-range> on %pOF\n", np);
2535 		hose->first_busno = 0;
2536 		hose->last_busno = 0xff;
2537 	}
2538 	hose->private_data = phb;
2539 	phb->hub_id = hub_id;
2540 	phb->opal_id = phb_id;
2541 	phb->type = ioda_type;
2542 	mutex_init(&phb->ioda.pe_alloc_mutex);
2543 
2544 	/* Detect specific models for error handling */
2545 	if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
2546 		phb->model = PNV_PHB_MODEL_P7IOC;
2547 	else if (of_device_is_compatible(np, "ibm,power8-pciex"))
2548 		phb->model = PNV_PHB_MODEL_PHB3;
2549 	else
2550 		phb->model = PNV_PHB_MODEL_UNKNOWN;
2551 
2552 	/* Initialize diagnostic data buffer */
2553 	prop32 = of_get_property(np, "ibm,phb-diag-data-size", NULL);
2554 	if (prop32)
2555 		phb->diag_data_size = be32_to_cpup(prop32);
2556 	else
2557 		phb->diag_data_size = PNV_PCI_DIAG_BUF_SIZE;
2558 
2559 	phb->diag_data = kzalloc(phb->diag_data_size, GFP_KERNEL);
2560 	if (!phb->diag_data)
2561 		panic("%s: Failed to allocate %u bytes\n", __func__,
2562 		      phb->diag_data_size);
2563 
2564 	/* Parse 32-bit and IO ranges (if any) */
2565 	pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
2566 
2567 	/* Get registers */
2568 	if (!of_address_to_resource(np, 0, &r)) {
2569 		phb->regs_phys = r.start;
2570 		phb->regs = ioremap(r.start, resource_size(&r));
2571 		if (phb->regs == NULL)
2572 			pr_err("  Failed to map registers !\n");
2573 	}
2574 
2575 	/* Initialize more IODA stuff */
2576 	phb->ioda.total_pe_num = 1;
2577 	prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
2578 	if (prop32)
2579 		phb->ioda.total_pe_num = be32_to_cpup(prop32);
2580 	prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
2581 	if (prop32)
2582 		phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
2583 
2584 	/* Invalidate RID to PE# mapping */
2585 	for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
2586 		phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
2587 
2588 	/* Parse 64-bit MMIO range */
2589 	pnv_ioda_parse_m64_window(phb);
2590 
2591 	phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
2592 	/* FW Has already off top 64k of M32 space (MSI space) */
2593 	phb->ioda.m32_size += 0x10000;
2594 
2595 	phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
2596 	phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
2597 	phb->ioda.io_size = hose->pci_io_size;
2598 	phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
2599 	phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
2600 
2601 	/* Allocate aux data & arrays. We don't have IO ports on PHB3 */
2602 	size = ALIGN(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
2603 			sizeof(unsigned long));
2604 	m64map_off = size;
2605 	size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
2606 	m32map_off = size;
2607 	size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
2608 	pemap_off = size;
2609 	size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
2610 	aux = kzalloc(size, GFP_KERNEL);
2611 	if (!aux)
2612 		panic("%s: Failed to allocate %lu bytes\n", __func__, size);
2613 
2614 	phb->ioda.pe_alloc = aux;
2615 	phb->ioda.m64_segmap = aux + m64map_off;
2616 	phb->ioda.m32_segmap = aux + m32map_off;
2617 	for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
2618 		phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
2619 		phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
2620 	}
2621 	phb->ioda.pe_array = aux + pemap_off;
2622 
2623 	/*
2624 	 * Choose PE number for root bus, which shouldn't have
2625 	 * M64 resources consumed by its child devices. To pick
2626 	 * the PE number adjacent to the reserved one if possible.
2627 	 */
2628 	pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
2629 	if (phb->ioda.reserved_pe_idx == 0) {
2630 		phb->ioda.root_pe_idx = 1;
2631 		pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
2632 	} else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
2633 		phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
2634 		pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
2635 	} else {
2636 		/* otherwise just allocate one */
2637 		root_pe = pnv_ioda_alloc_pe(phb, 1);
2638 		phb->ioda.root_pe_idx = root_pe->pe_number;
2639 	}
2640 
2641 	INIT_LIST_HEAD(&phb->ioda.pe_list);
2642 	mutex_init(&phb->ioda.pe_list_mutex);
2643 
2644 #if 0 /* We should really do that ... */
2645 	rc = opal_pci_set_phb_mem_window(opal->phb_id,
2646 					 window_type,
2647 					 window_num,
2648 					 starting_real_address,
2649 					 starting_pci_address,
2650 					 segment_size);
2651 #endif
2652 
2653 	pr_info("  %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
2654 		phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
2655 		phb->ioda.m32_size, phb->ioda.m32_segsize);
2656 	if (phb->ioda.m64_size)
2657 		pr_info("                 M64: 0x%lx [segment=0x%lx]\n",
2658 			phb->ioda.m64_size, phb->ioda.m64_segsize);
2659 	if (phb->ioda.io_size)
2660 		pr_info("                  IO: 0x%x [segment=0x%x]\n",
2661 			phb->ioda.io_size, phb->ioda.io_segsize);
2662 
2663 
2664 	phb->hose->ops = &pnv_pci_ops;
2665 	phb->get_pe_state = pnv_ioda_get_pe_state;
2666 	phb->freeze_pe = pnv_ioda_freeze_pe;
2667 	phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
2668 
2669 	/* Setup MSI support */
2670 	pnv_pci_init_ioda_msis(phb);
2671 
2672 	/*
2673 	 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
2674 	 * to let the PCI core do resource assignment. It's supposed
2675 	 * that the PCI core will do correct I/O and MMIO alignment
2676 	 * for the P2P bridge bars so that each PCI bus (excluding
2677 	 * the child P2P bridges) can form individual PE.
2678 	 */
2679 	ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
2680 
2681 	switch (phb->type) {
2682 	case PNV_PHB_NPU_OCAPI:
2683 		hose->controller_ops = pnv_npu_ocapi_ioda_controller_ops;
2684 		break;
2685 	default:
2686 		hose->controller_ops = pnv_pci_ioda_controller_ops;
2687 	}
2688 
2689 	ppc_md.pcibios_default_alignment = pnv_pci_default_alignment;
2690 
2691 #ifdef CONFIG_PCI_IOV
2692 	ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov;
2693 	ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
2694 	ppc_md.pcibios_sriov_enable = pnv_pcibios_sriov_enable;
2695 	ppc_md.pcibios_sriov_disable = pnv_pcibios_sriov_disable;
2696 #endif
2697 
2698 	pci_add_flags(PCI_REASSIGN_ALL_RSRC);
2699 
2700 	/* Reset IODA tables to a clean state */
2701 	rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
2702 	if (rc)
2703 		pr_warn("  OPAL Error %ld performing IODA table reset !\n", rc);
2704 
2705 	/*
2706 	 * If we're running in kdump kernel, the previous kernel never
2707 	 * shutdown PCI devices correctly. We already got IODA table
2708 	 * cleaned out. So we have to issue PHB reset to stop all PCI
2709 	 * transactions from previous kernel. The ppc_pci_reset_phbs
2710 	 * kernel parameter will force this reset too. Additionally,
2711 	 * if the IODA reset above failed then use a bigger hammer.
2712 	 * This can happen if we get a PHB fatal error in very early
2713 	 * boot.
2714 	 */
2715 	if (is_kdump_kernel() || pci_reset_phbs || rc) {
2716 		pr_info("  Issue PHB reset ...\n");
2717 		pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
2718 		pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
2719 	}
2720 
2721 	/* Remove M64 resource if we can't configure it successfully */
2722 	if (!phb->init_m64 || phb->init_m64(phb))
2723 		hose->mem_resources[1].flags = 0;
2724 
2725 	/* create pci_dn's for DT nodes under this PHB */
2726 	pci_devs_phb_init_dynamic(hose);
2727 }
2728 
pnv_pci_init_ioda2_phb(struct device_node * np)2729 void __init pnv_pci_init_ioda2_phb(struct device_node *np)
2730 {
2731 	pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
2732 }
2733 
pnv_pci_init_npu2_opencapi_phb(struct device_node * np)2734 void __init pnv_pci_init_npu2_opencapi_phb(struct device_node *np)
2735 {
2736 	pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_OCAPI);
2737 }
2738 
pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev * dev)2739 static void pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev *dev)
2740 {
2741 	struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
2742 
2743 	if (!machine_is(powernv))
2744 		return;
2745 
2746 	if (phb->type == PNV_PHB_NPU_OCAPI)
2747 		dev->cfg_size = PCI_CFG_SPACE_EXP_SIZE;
2748 }
2749 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pnv_npu2_opencapi_cfg_size_fixup);
2750