xref: /linux/arch/powerpc/platforms/powernv/pci-ioda.c (revision 5acbae5f7eb3d5275120abfe698c394b7325dcec)
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 /*
1627  * Returns true iff chip is something that we could call
1628  * pnv_opal_pci_msi_eoi for.
1629  */
is_pnv_opal_msi(struct irq_chip * chip)1630 bool is_pnv_opal_msi(struct irq_chip *chip)
1631 {
1632 	return chip && chip->name && str_has_prefix(chip->name, "PNV-");
1633 }
1634 EXPORT_SYMBOL_GPL(is_pnv_opal_msi);
1635 
__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)1636 static int __pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
1637 				    unsigned int xive_num,
1638 				    unsigned int is_64, struct msi_msg *msg)
1639 {
1640 	struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1641 	__be32 data;
1642 	int rc;
1643 
1644 	dev_dbg(&dev->dev, "%s: setup %s-bit MSI for vector #%d\n", __func__,
1645 		is_64 ? "64" : "32", xive_num);
1646 
1647 	/* No PE assigned ? bail out ... no MSI for you ! */
1648 	if (pe == NULL)
1649 		return -ENXIO;
1650 
1651 	/* Check if we have an MVE */
1652 	if (pe->mve_number < 0)
1653 		return -ENXIO;
1654 
1655 	/* Force 32-bit MSI on some broken devices */
1656 	if (dev->msi_addr_mask < DMA_BIT_MASK(64))
1657 		is_64 = 0;
1658 
1659 	/* Assign XIVE to PE */
1660 	rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1661 	if (rc) {
1662 		pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1663 			pci_name(dev), rc, xive_num);
1664 		return -EIO;
1665 	}
1666 
1667 	if (is_64) {
1668 		__be64 addr64;
1669 
1670 		rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1671 				     &addr64, &data);
1672 		if (rc) {
1673 			pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1674 				pci_name(dev), rc);
1675 			return -EIO;
1676 		}
1677 		msg->address_hi = be64_to_cpu(addr64) >> 32;
1678 		msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
1679 	} else {
1680 		__be32 addr32;
1681 
1682 		rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1683 				     &addr32, &data);
1684 		if (rc) {
1685 			pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1686 				pci_name(dev), rc);
1687 			return -EIO;
1688 		}
1689 		msg->address_hi = 0;
1690 		msg->address_lo = be32_to_cpu(addr32);
1691 	}
1692 	msg->data = be32_to_cpu(data);
1693 
1694 	return 0;
1695 }
1696 
pnv_msi_shutdown(struct irq_data * d)1697 static void pnv_msi_shutdown(struct irq_data *d)
1698 {
1699 	d = d->parent_data;
1700 	if (d->chip->irq_shutdown)
1701 		d->chip->irq_shutdown(d);
1702 }
1703 
pnv_init_dev_msi_info(struct device * dev,struct irq_domain * domain,struct irq_domain * real_parent,struct msi_domain_info * info)1704 static bool pnv_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
1705 				  struct irq_domain *real_parent, struct msi_domain_info *info)
1706 {
1707 	struct irq_chip *chip = info->chip;
1708 
1709 	if (!msi_lib_init_dev_msi_info(dev, domain, real_parent, info))
1710 		return false;
1711 
1712 	chip->irq_shutdown = pnv_msi_shutdown;
1713 	return true;
1714 }
1715 
1716 #define PNV_PCI_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS		| \
1717 				    MSI_FLAG_USE_DEF_CHIP_OPS		| \
1718 				    MSI_FLAG_PCI_MSI_MASK_PARENT)
1719 #define PNV_PCI_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK		| \
1720 				     MSI_FLAG_PCI_MSIX			| \
1721 				     MSI_FLAG_MULTI_PCI_MSI)
1722 
1723 static const struct msi_parent_ops pnv_msi_parent_ops = {
1724 	.required_flags		= PNV_PCI_MSI_FLAGS_REQUIRED,
1725 	.supported_flags	= PNV_PCI_MSI_FLAGS_SUPPORTED,
1726 	.chip_flags		= MSI_CHIP_FLAG_SET_EOI,
1727 	.bus_select_token	= DOMAIN_BUS_NEXUS,
1728 	.bus_select_mask	= MATCH_PCI_MSI,
1729 	.prefix			= "PNV-", /* Note: is_pnv_opal_msi() uses this */
1730 	.init_dev_msi_info	= pnv_init_dev_msi_info,
1731 };
1732 
pnv_msi_compose_msg(struct irq_data * d,struct msi_msg * msg)1733 static void pnv_msi_compose_msg(struct irq_data *d, struct msi_msg *msg)
1734 {
1735 	struct msi_desc *entry = irq_data_get_msi_desc(d);
1736 	struct pci_dev *pdev = msi_desc_to_pci_dev(entry);
1737 	struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1738 	struct pnv_phb *phb = hose->private_data;
1739 	int rc;
1740 
1741 	rc = __pnv_pci_ioda_msi_setup(phb, pdev, d->hwirq,
1742 				      entry->pci.msi_attrib.is_64, msg);
1743 	if (rc)
1744 		dev_err(&pdev->dev, "Failed to setup %s-bit MSI #%ld : %d\n",
1745 			entry->pci.msi_attrib.is_64 ? "64" : "32", d->hwirq, rc);
1746 }
1747 
1748 /*
1749  * The IRQ data is mapped in the MSI domain in which HW IRQ numbers
1750  * correspond to vector numbers.
1751  */
pnv_msi_eoi(struct irq_data * d)1752 static void pnv_msi_eoi(struct irq_data *d)
1753 {
1754 	struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1755 	struct pnv_phb *phb = hose->private_data;
1756 
1757 	if (phb->model == PNV_PHB_MODEL_PHB3) {
1758 		/*
1759 		 * The EOI OPAL call takes an OPAL HW IRQ number but
1760 		 * since it is translated into a vector number in
1761 		 * OPAL, use that directly.
1762 		 */
1763 		WARN_ON_ONCE(opal_pci_msi_eoi(phb->opal_id, d->hwirq));
1764 	}
1765 
1766 	irq_chip_eoi_parent(d);
1767 }
1768 
1769 static struct irq_chip pnv_msi_irq_chip = {
1770 	.name			= "PNV-MSI",
1771 	.irq_shutdown		= pnv_msi_shutdown,
1772 	.irq_mask		= irq_chip_mask_parent,
1773 	.irq_unmask		= irq_chip_unmask_parent,
1774 	.irq_eoi		= pnv_msi_eoi,
1775 	.irq_set_affinity	= irq_chip_set_affinity_parent,
1776 	.irq_compose_msi_msg	= pnv_msi_compose_msg,
1777 };
1778 
pnv_irq_parent_domain_alloc(struct irq_domain * domain,unsigned int virq,int hwirq)1779 static int pnv_irq_parent_domain_alloc(struct irq_domain *domain,
1780 				       unsigned int virq, int hwirq)
1781 {
1782 	struct irq_fwspec parent_fwspec;
1783 	int ret;
1784 
1785 	parent_fwspec.fwnode = domain->parent->fwnode;
1786 	parent_fwspec.param_count = 2;
1787 	parent_fwspec.param[0] = hwirq;
1788 	parent_fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1789 
1790 	ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
1791 	if (ret)
1792 		return ret;
1793 
1794 	return 0;
1795 }
1796 
pnv_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)1797 static int pnv_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1798 				unsigned int nr_irqs, void *arg)
1799 {
1800 	struct pci_controller *hose = domain->host_data;
1801 	struct pnv_phb *phb = hose->private_data;
1802 	msi_alloc_info_t *info = arg;
1803 	struct pci_dev *pdev = msi_desc_to_pci_dev(info->desc);
1804 	int hwirq;
1805 	int i, ret;
1806 
1807 	hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, nr_irqs);
1808 	if (hwirq < 0) {
1809 		dev_warn(&pdev->dev, "failed to find a free MSI\n");
1810 		return -ENOSPC;
1811 	}
1812 
1813 	dev_dbg(&pdev->dev, "%s bridge %pOF %d/%x #%d\n", __func__,
1814 		hose->dn, virq, hwirq, nr_irqs);
1815 
1816 	for (i = 0; i < nr_irqs; i++) {
1817 		ret = pnv_irq_parent_domain_alloc(domain, virq + i,
1818 						  phb->msi_base + hwirq + i);
1819 		if (ret)
1820 			goto out;
1821 
1822 		irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
1823 					      &pnv_msi_irq_chip, hose);
1824 	}
1825 
1826 	return 0;
1827 
1828 out:
1829 	irq_domain_free_irqs_parent(domain, virq, i);
1830 	msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, nr_irqs);
1831 	return ret;
1832 }
1833 
pnv_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)1834 static void pnv_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1835 				unsigned int nr_irqs)
1836 {
1837 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1838 	struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1839 	struct pnv_phb *phb = hose->private_data;
1840 
1841 	pr_debug("%s bridge %pOF %d/%lx #%d\n", __func__, hose->dn,
1842 		 virq, d->hwirq, nr_irqs);
1843 
1844 	msi_bitmap_free_hwirqs(&phb->msi_bmp, d->hwirq, nr_irqs);
1845 	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1846 }
1847 
1848 static const struct irq_domain_ops pnv_irq_domain_ops = {
1849 	.select	= msi_lib_irq_domain_select,
1850 	.alloc  = pnv_irq_domain_alloc,
1851 	.free   = pnv_irq_domain_free,
1852 };
1853 
pnv_msi_allocate_domains(struct pci_controller * hose,unsigned int count)1854 static int __init pnv_msi_allocate_domains(struct pci_controller *hose, unsigned int count)
1855 {
1856 	struct irq_domain *parent = irq_get_default_domain();
1857 	struct irq_domain_info info = {
1858 		.fwnode		= of_fwnode_handle(hose->dn),
1859 		.ops		= &pnv_irq_domain_ops,
1860 		.host_data	= hose,
1861 		.size		= count,
1862 		.parent		= parent,
1863 	};
1864 
1865 	hose->dev_domain = msi_create_parent_irq_domain(&info, &pnv_msi_parent_ops);
1866 	if (!hose->dev_domain) {
1867 		pr_err("PCI: failed to create MSI IRQ domain bridge %pOF (domain %d)\n",
1868 		       hose->dn, hose->global_number);
1869 		return -ENOMEM;
1870 	}
1871 
1872 	return 0;
1873 }
1874 
pnv_pci_init_ioda_msis(struct pnv_phb * phb)1875 static void __init pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1876 {
1877 	unsigned int count;
1878 	const __be32 *prop = of_get_property(phb->hose->dn,
1879 					     "ibm,opal-msi-ranges", NULL);
1880 	if (!prop) {
1881 		/* BML Fallback */
1882 		prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1883 	}
1884 	if (!prop)
1885 		return;
1886 
1887 	phb->msi_base = be32_to_cpup(prop);
1888 	count = be32_to_cpup(prop + 1);
1889 	if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
1890 		pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1891 		       phb->hose->global_number);
1892 		return;
1893 	}
1894 
1895 	pr_info("  Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
1896 		count, phb->msi_base);
1897 
1898 	pnv_msi_allocate_domains(phb->hose, count);
1899 }
1900 
pnv_ioda_setup_pe_res(struct pnv_ioda_pe * pe,struct resource * res)1901 static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
1902 				  struct resource *res)
1903 {
1904 	struct pnv_phb *phb = pe->phb;
1905 	struct pci_bus_region region;
1906 	int index;
1907 	int64_t rc;
1908 
1909 	if (!res || !res->flags || res->start > res->end ||
1910 	    res->flags & IORESOURCE_UNSET)
1911 		return;
1912 
1913 	if (res->flags & IORESOURCE_IO) {
1914 		region.start = res->start - phb->ioda.io_pci_base;
1915 		region.end   = res->end - phb->ioda.io_pci_base;
1916 		index = region.start / phb->ioda.io_segsize;
1917 
1918 		while (index < phb->ioda.total_pe_num &&
1919 		       region.start <= region.end) {
1920 			phb->ioda.io_segmap[index] = pe->pe_number;
1921 			rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1922 				pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
1923 			if (rc != OPAL_SUCCESS) {
1924 				pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",
1925 				       __func__, rc, index, pe->pe_number);
1926 				break;
1927 			}
1928 
1929 			region.start += phb->ioda.io_segsize;
1930 			index++;
1931 		}
1932 	} else if ((res->flags & IORESOURCE_MEM) &&
1933 		   !pnv_pci_is_m64(phb, res)) {
1934 		region.start = res->start -
1935 			       phb->hose->mem_offset[0] -
1936 			       phb->ioda.m32_pci_base;
1937 		region.end   = res->end -
1938 			       phb->hose->mem_offset[0] -
1939 			       phb->ioda.m32_pci_base;
1940 		index = region.start / phb->ioda.m32_segsize;
1941 
1942 		while (index < phb->ioda.total_pe_num &&
1943 		       region.start <= region.end) {
1944 			phb->ioda.m32_segmap[index] = pe->pe_number;
1945 			rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1946 				pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
1947 			if (rc != OPAL_SUCCESS) {
1948 				pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",
1949 				       __func__, rc, index, pe->pe_number);
1950 				break;
1951 			}
1952 
1953 			region.start += phb->ioda.m32_segsize;
1954 			index++;
1955 		}
1956 	}
1957 }
1958 
1959 /*
1960  * This function is supposed to be called on basis of PE from top
1961  * to bottom style. So the I/O or MMIO segment assigned to
1962  * parent PE could be overridden by its child PEs if necessary.
1963  */
pnv_ioda_setup_pe_seg(struct pnv_ioda_pe * pe)1964 static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
1965 {
1966 	struct pci_dev *pdev;
1967 	int i;
1968 
1969 	/*
1970 	 * NOTE: We only care PCI bus based PE for now. For PCI
1971 	 * device based PE, for example SRIOV sensitive VF should
1972 	 * be figured out later.
1973 	 */
1974 	BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
1975 
1976 	list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
1977 		for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1978 			pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
1979 
1980 		/*
1981 		 * If the PE contains all subordinate PCI buses, the
1982 		 * windows of the child bridges should be mapped to
1983 		 * the PE as well.
1984 		 */
1985 		if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
1986 			continue;
1987 		for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
1988 			pnv_ioda_setup_pe_res(pe,
1989 				&pdev->resource[PCI_BRIDGE_RESOURCES + i]);
1990 	}
1991 }
1992 
1993 #ifdef CONFIG_DEBUG_FS
pnv_pci_diag_data_set(void * data,u64 val)1994 static int pnv_pci_diag_data_set(void *data, u64 val)
1995 {
1996 	struct pnv_phb *phb = data;
1997 	s64 ret;
1998 
1999 	/* Retrieve the diag data from firmware */
2000 	ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
2001 					  phb->diag_data_size);
2002 	if (ret != OPAL_SUCCESS)
2003 		return -EIO;
2004 
2005 	/* Print the diag data to the kernel log */
2006 	pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
2007 	return 0;
2008 }
2009 
2010 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_diag_data_fops, NULL, pnv_pci_diag_data_set,
2011 			 "%llu\n");
2012 
pnv_pci_ioda_pe_dump(void * data,u64 val)2013 static int pnv_pci_ioda_pe_dump(void *data, u64 val)
2014 {
2015 	struct pnv_phb *phb = data;
2016 	int pe_num;
2017 
2018 	for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
2019 		struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_num];
2020 
2021 		if (!test_bit(pe_num, phb->ioda.pe_alloc))
2022 			continue;
2023 
2024 		pe_warn(pe, "rid: %04x dev count: %2d flags: %s%s%s%s%s%s\n",
2025 			pe->rid, pe->device_count,
2026 			(pe->flags & PNV_IODA_PE_DEV) ? "dev " : "",
2027 			(pe->flags & PNV_IODA_PE_BUS) ? "bus " : "",
2028 			(pe->flags & PNV_IODA_PE_BUS_ALL) ? "all " : "",
2029 			(pe->flags & PNV_IODA_PE_MASTER) ? "master " : "",
2030 			(pe->flags & PNV_IODA_PE_SLAVE) ? "slave " : "",
2031 			(pe->flags & PNV_IODA_PE_VF) ? "vf " : "");
2032 	}
2033 
2034 	return 0;
2035 }
2036 
2037 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_ioda_pe_dump_fops, NULL,
2038 			 pnv_pci_ioda_pe_dump, "%llu\n");
2039 
2040 #endif /* CONFIG_DEBUG_FS */
2041 
pnv_pci_ioda_create_dbgfs(void)2042 static void pnv_pci_ioda_create_dbgfs(void)
2043 {
2044 #ifdef CONFIG_DEBUG_FS
2045 	struct pci_controller *hose, *tmp;
2046 	struct pnv_phb *phb;
2047 	char name[16];
2048 
2049 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2050 		phb = hose->private_data;
2051 
2052 		sprintf(name, "PCI%04x", hose->global_number);
2053 		phb->dbgfs = debugfs_create_dir(name, arch_debugfs_dir);
2054 
2055 		debugfs_create_file_unsafe("dump_diag_regs", 0200, phb->dbgfs,
2056 					   phb, &pnv_pci_diag_data_fops);
2057 		debugfs_create_file_unsafe("dump_ioda_pe_state", 0200, phb->dbgfs,
2058 					   phb, &pnv_pci_ioda_pe_dump_fops);
2059 	}
2060 #endif /* CONFIG_DEBUG_FS */
2061 }
2062 
pnv_pci_enable_bridge(struct pci_bus * bus)2063 static void pnv_pci_enable_bridge(struct pci_bus *bus)
2064 {
2065 	struct pci_dev *dev = bus->self;
2066 	struct pci_bus *child;
2067 
2068 	/* Empty bus ? bail */
2069 	if (list_empty(&bus->devices))
2070 		return;
2071 
2072 	/*
2073 	 * If there's a bridge associated with that bus enable it. This works
2074 	 * around races in the generic code if the enabling is done during
2075 	 * parallel probing. This can be removed once those races have been
2076 	 * fixed.
2077 	 */
2078 	if (dev) {
2079 		int rc = pci_enable_device(dev);
2080 		if (rc)
2081 			pci_err(dev, "Error enabling bridge (%d)\n", rc);
2082 		pci_set_master(dev);
2083 	}
2084 
2085 	/* Perform the same to child busses */
2086 	list_for_each_entry(child, &bus->children, node)
2087 		pnv_pci_enable_bridge(child);
2088 }
2089 
pnv_pci_enable_bridges(void)2090 static void pnv_pci_enable_bridges(void)
2091 {
2092 	struct pci_controller *hose;
2093 
2094 	list_for_each_entry(hose, &hose_list, list_node)
2095 		pnv_pci_enable_bridge(hose->bus);
2096 }
2097 
pnv_pci_ioda_fixup(void)2098 static void pnv_pci_ioda_fixup(void)
2099 {
2100 	pnv_pci_ioda_create_dbgfs();
2101 
2102 	pnv_pci_enable_bridges();
2103 
2104 #ifdef CONFIG_EEH
2105 	pnv_eeh_post_init();
2106 #endif
2107 }
2108 
2109 /*
2110  * Returns the alignment for I/O or memory windows for P2P
2111  * bridges. That actually depends on how PEs are segmented.
2112  * For now, we return I/O or M32 segment size for PE sensitive
2113  * P2P bridges. Otherwise, the default values (4KiB for I/O,
2114  * 1MiB for memory) will be returned.
2115  *
2116  * The current PCI bus might be put into one PE, which was
2117  * create against the parent PCI bridge. For that case, we
2118  * needn't enlarge the alignment so that we can save some
2119  * resources.
2120  */
pnv_pci_window_alignment(struct pci_bus * bus,unsigned long type)2121 static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2122 						unsigned long type)
2123 {
2124 	struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
2125 	int num_pci_bridges = 0;
2126 	struct pci_dev *bridge;
2127 
2128 	bridge = bus->self;
2129 	while (bridge) {
2130 		if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2131 			num_pci_bridges++;
2132 			if (num_pci_bridges >= 2)
2133 				return 1;
2134 		}
2135 
2136 		bridge = bridge->bus->self;
2137 	}
2138 
2139 	/*
2140 	 * We fall back to M32 if M64 isn't supported. We enforce the M64
2141 	 * alignment for any 64-bit resource, PCIe doesn't care and
2142 	 * bridges only do 64-bit prefetchable anyway.
2143 	 */
2144 	if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type))
2145 		return phb->ioda.m64_segsize;
2146 	if (type & IORESOURCE_MEM)
2147 		return phb->ioda.m32_segsize;
2148 
2149 	return phb->ioda.io_segsize;
2150 }
2151 
2152 /*
2153  * We are updating root port or the upstream port of the
2154  * bridge behind the root port with PHB's windows in order
2155  * to accommodate the changes on required resources during
2156  * PCI (slot) hotplug, which is connected to either root
2157  * port or the downstream ports of PCIe switch behind the
2158  * root port.
2159  */
pnv_pci_fixup_bridge_resources(struct pci_bus * bus,unsigned long type)2160 static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
2161 					   unsigned long type)
2162 {
2163 	struct pci_controller *hose = pci_bus_to_host(bus);
2164 	struct pnv_phb *phb = hose->private_data;
2165 	struct pci_dev *bridge = bus->self;
2166 	struct resource *r, *w;
2167 	bool msi_region = false;
2168 	int i;
2169 
2170 	/* Check if we need apply fixup to the bridge's windows */
2171 	if (!pci_is_root_bus(bridge->bus) &&
2172 	    !pci_is_root_bus(bridge->bus->self->bus))
2173 		return;
2174 
2175 	/* Fixup the resources */
2176 	for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
2177 		r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
2178 		if (!r->flags || !r->parent)
2179 			continue;
2180 
2181 		w = NULL;
2182 		if (r->flags & type & IORESOURCE_IO)
2183 			w = &hose->io_resource;
2184 		else if (pnv_pci_is_m64(phb, r) &&
2185 			 (type & IORESOURCE_PREFETCH) &&
2186 			 phb->ioda.m64_segsize)
2187 			w = &hose->mem_resources[1];
2188 		else if (r->flags & type & IORESOURCE_MEM) {
2189 			w = &hose->mem_resources[0];
2190 			msi_region = true;
2191 		}
2192 
2193 		r->start = w->start;
2194 		r->end = w->end;
2195 
2196 		/* The 64KB 32-bits MSI region shouldn't be included in
2197 		 * the 32-bits bridge window. Otherwise, we can see strange
2198 		 * issues. One of them is EEH error observed on Garrison.
2199 		 *
2200 		 * Exclude top 1MB region which is the minimal alignment of
2201 		 * 32-bits bridge window.
2202 		 */
2203 		if (msi_region) {
2204 			r->end += 0x10000;
2205 			r->end -= 0x100000;
2206 		}
2207 	}
2208 }
2209 
pnv_pci_configure_bus(struct pci_bus * bus)2210 static void pnv_pci_configure_bus(struct pci_bus *bus)
2211 {
2212 	struct pci_dev *bridge = bus->self;
2213 	struct pnv_ioda_pe *pe;
2214 	bool all = (bridge && pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
2215 
2216 	dev_info(&bus->dev, "Configuring PE for bus\n");
2217 
2218 	/* Don't assign PE to PCI bus, which doesn't have subordinate devices */
2219 	if (WARN_ON(list_empty(&bus->devices)))
2220 		return;
2221 
2222 	/* Reserve PEs according to used M64 resources */
2223 	pnv_ioda_reserve_m64_pe(bus, NULL, all);
2224 
2225 	/*
2226 	 * Assign PE. We might run here because of partial hotplug.
2227 	 * For the case, we just pick up the existing PE and should
2228 	 * not allocate resources again.
2229 	 */
2230 	pe = pnv_ioda_setup_bus_PE(bus, all);
2231 	if (!pe)
2232 		return;
2233 
2234 	pnv_ioda_setup_pe_seg(pe);
2235 }
2236 
pnv_pci_default_alignment(void)2237 static resource_size_t pnv_pci_default_alignment(void)
2238 {
2239 	return PAGE_SIZE;
2240 }
2241 
2242 /* Prevent enabling devices for which we couldn't properly
2243  * assign a PE
2244  */
pnv_pci_enable_device_hook(struct pci_dev * dev)2245 static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
2246 {
2247 	struct pci_dn *pdn;
2248 
2249 	pdn = pci_get_pdn(dev);
2250 	if (!pdn || pdn->pe_number == IODA_INVALID_PE) {
2251 		pci_err(dev, "pci_enable_device() blocked, no PE assigned.\n");
2252 		return false;
2253 	}
2254 
2255 	return true;
2256 }
2257 
pnv_ocapi_enable_device_hook(struct pci_dev * dev)2258 static bool pnv_ocapi_enable_device_hook(struct pci_dev *dev)
2259 {
2260 	struct pci_dn *pdn;
2261 	struct pnv_ioda_pe *pe;
2262 
2263 	pdn = pci_get_pdn(dev);
2264 	if (!pdn)
2265 		return false;
2266 
2267 	if (pdn->pe_number == IODA_INVALID_PE) {
2268 		pe = pnv_ioda_setup_dev_PE(dev);
2269 		if (!pe)
2270 			return false;
2271 	}
2272 	return true;
2273 }
2274 
pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe * pe)2275 void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
2276 {
2277 	struct iommu_table *tbl = pe->table_group.tables[0];
2278 	int64_t rc;
2279 
2280 	if (!pe->dma_setup_done)
2281 		return;
2282 
2283 	rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2284 	if (rc)
2285 		pe_warn(pe, "OPAL error %lld release DMA window\n", rc);
2286 
2287 	pnv_pci_ioda2_set_bypass(pe, false);
2288 	if (pe->table_group.group) {
2289 		iommu_group_put(pe->table_group.group);
2290 		WARN_ON(pe->table_group.group);
2291 	}
2292 
2293 	iommu_tce_table_put(tbl);
2294 }
2295 
pnv_ioda_free_pe_seg(struct pnv_ioda_pe * pe,unsigned short win,unsigned int * map)2296 static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
2297 				 unsigned short win,
2298 				 unsigned int *map)
2299 {
2300 	struct pnv_phb *phb = pe->phb;
2301 	int idx;
2302 	int64_t rc;
2303 
2304 	for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
2305 		if (map[idx] != pe->pe_number)
2306 			continue;
2307 
2308 		rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2309 				phb->ioda.reserved_pe_idx, win, 0, idx);
2310 
2311 		if (rc != OPAL_SUCCESS)
2312 			pe_warn(pe, "Error %lld unmapping (%d) segment#%d\n",
2313 				rc, win, idx);
2314 
2315 		map[idx] = IODA_INVALID_PE;
2316 	}
2317 }
2318 
pnv_ioda_release_pe_seg(struct pnv_ioda_pe * pe)2319 static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
2320 {
2321 	struct pnv_phb *phb = pe->phb;
2322 
2323 	if (phb->type == PNV_PHB_IODA2) {
2324 		pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
2325 				     phb->ioda.m32_segmap);
2326 	}
2327 }
2328 
pnv_ioda_release_pe(struct pnv_ioda_pe * pe)2329 static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
2330 {
2331 	struct pnv_phb *phb = pe->phb;
2332 	struct pnv_ioda_pe *slave, *tmp;
2333 
2334 	pe_info(pe, "Releasing PE\n");
2335 
2336 	mutex_lock(&phb->ioda.pe_list_mutex);
2337 	list_del(&pe->list);
2338 	mutex_unlock(&phb->ioda.pe_list_mutex);
2339 
2340 	switch (phb->type) {
2341 	case PNV_PHB_IODA2:
2342 		pnv_pci_ioda2_release_pe_dma(pe);
2343 		break;
2344 	case PNV_PHB_NPU_OCAPI:
2345 		break;
2346 	default:
2347 		WARN_ON(1);
2348 	}
2349 
2350 	pnv_ioda_release_pe_seg(pe);
2351 	pnv_ioda_deconfigure_pe(pe->phb, pe);
2352 
2353 	/* Release slave PEs in the compound PE */
2354 	if (pe->flags & PNV_IODA_PE_MASTER) {
2355 		list_for_each_entry_safe(slave, tmp, &pe->slaves, list) {
2356 			list_del(&slave->list);
2357 			pnv_ioda_free_pe(slave);
2358 		}
2359 	}
2360 
2361 	/*
2362 	 * The PE for root bus can be removed because of hotplug in EEH
2363 	 * recovery for fenced PHB error. We need to mark the PE dead so
2364 	 * that it can be populated again in PCI hot add path. The PE
2365 	 * shouldn't be destroyed as it's the global reserved resource.
2366 	 */
2367 	if (phb->ioda.root_pe_idx == pe->pe_number)
2368 		return;
2369 
2370 	pnv_ioda_free_pe(pe);
2371 }
2372 
pnv_pci_release_device(struct pci_dev * pdev)2373 static void pnv_pci_release_device(struct pci_dev *pdev)
2374 {
2375 	struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
2376 	struct pci_dn *pdn = pci_get_pdn(pdev);
2377 	struct pnv_ioda_pe *pe;
2378 
2379 	/* The VF PE state is torn down when sriov_disable() is called */
2380 	if (pdev->is_virtfn)
2381 		return;
2382 
2383 	if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2384 		return;
2385 
2386 #ifdef CONFIG_PCI_IOV
2387 	/*
2388 	 * FIXME: Try move this to sriov_disable(). It's here since we allocate
2389 	 * the iov state at probe time since we need to fiddle with the IOV
2390 	 * resources.
2391 	 */
2392 	if (pdev->is_physfn)
2393 		kfree(pdev->dev.archdata.iov_data);
2394 #endif
2395 
2396 	/*
2397 	 * PCI hotplug can happen as part of EEH error recovery. The @pdn
2398 	 * isn't removed and added afterwards in this scenario. We should
2399 	 * set the PE number in @pdn to an invalid one. Otherwise, the PE's
2400 	 * device count is decreased on removing devices while failing to
2401 	 * be increased on adding devices. It leads to unbalanced PE's device
2402 	 * count and eventually make normal PCI hotplug path broken.
2403 	 */
2404 	pe = &phb->ioda.pe_array[pdn->pe_number];
2405 	pdn->pe_number = IODA_INVALID_PE;
2406 
2407 	WARN_ON(--pe->device_count < 0);
2408 	if (pe->device_count == 0)
2409 		pnv_ioda_release_pe(pe);
2410 }
2411 
pnv_pci_ioda_shutdown(struct pci_controller * hose)2412 static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
2413 {
2414 	struct pnv_phb *phb = hose->private_data;
2415 
2416 	opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
2417 		       OPAL_ASSERT_RESET);
2418 }
2419 
pnv_pci_ioda_dma_bus_setup(struct pci_bus * bus)2420 static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus)
2421 {
2422 	struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
2423 	struct pnv_ioda_pe *pe;
2424 
2425 	list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2426 		if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
2427 			continue;
2428 
2429 		if (!pe->pbus)
2430 			continue;
2431 
2432 		if (bus->number == ((pe->rid >> 8) & 0xFF)) {
2433 			pe->pbus = bus;
2434 			break;
2435 		}
2436 	}
2437 }
2438 
2439 #ifdef CONFIG_IOMMU_API
pnv_pci_device_group(struct pci_controller * hose,struct pci_dev * pdev)2440 static struct iommu_group *pnv_pci_device_group(struct pci_controller *hose,
2441 						struct pci_dev *pdev)
2442 {
2443 	struct pnv_phb *phb = hose->private_data;
2444 	struct pnv_ioda_pe *pe;
2445 
2446 	if (WARN_ON(!phb))
2447 		return ERR_PTR(-ENODEV);
2448 
2449 	pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));
2450 	if (!pe)
2451 		return ERR_PTR(-ENODEV);
2452 
2453 	if (!pe->table_group.group)
2454 		return ERR_PTR(-ENODEV);
2455 
2456 	return iommu_group_ref_get(pe->table_group.group);
2457 }
2458 #endif
2459 
2460 static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
2461 	.dma_dev_setup		= pnv_pci_ioda_dma_dev_setup,
2462 	.dma_bus_setup		= pnv_pci_ioda_dma_bus_setup,
2463 	.iommu_bypass_supported	= pnv_pci_ioda_iommu_bypass_supported,
2464 	.enable_device_hook	= pnv_pci_enable_device_hook,
2465 	.release_device		= pnv_pci_release_device,
2466 	.window_alignment	= pnv_pci_window_alignment,
2467 	.setup_bridge		= pnv_pci_fixup_bridge_resources,
2468 	.reset_secondary_bus	= pnv_pci_reset_secondary_bus,
2469 	.shutdown		= pnv_pci_ioda_shutdown,
2470 #ifdef CONFIG_IOMMU_API
2471 	.device_group		= pnv_pci_device_group,
2472 #endif
2473 };
2474 
2475 static const struct pci_controller_ops pnv_npu_ocapi_ioda_controller_ops = {
2476 	.enable_device_hook	= pnv_ocapi_enable_device_hook,
2477 	.release_device		= pnv_pci_release_device,
2478 	.window_alignment	= pnv_pci_window_alignment,
2479 	.reset_secondary_bus	= pnv_pci_reset_secondary_bus,
2480 	.shutdown		= pnv_pci_ioda_shutdown,
2481 };
2482 
pnv_pci_init_ioda_phb(struct device_node * np,u64 hub_id,int ioda_type)2483 static void __init pnv_pci_init_ioda_phb(struct device_node *np,
2484 					 u64 hub_id, int ioda_type)
2485 {
2486 	struct pci_controller *hose;
2487 	struct pnv_phb *phb;
2488 	unsigned long size, m64map_off, m32map_off, pemap_off;
2489 	struct pnv_ioda_pe *root_pe;
2490 	struct resource r;
2491 	const __be64 *prop64;
2492 	const __be32 *prop32;
2493 	int len;
2494 	unsigned int segno;
2495 	u64 phb_id;
2496 	void *aux;
2497 	long rc;
2498 
2499 	if (!of_device_is_available(np))
2500 		return;
2501 
2502 	pr_info("Initializing %s PHB (%pOF)\n",	pnv_phb_names[ioda_type], np);
2503 
2504 	prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
2505 	if (!prop64) {
2506 		pr_err("  Missing \"ibm,opal-phbid\" property !\n");
2507 		return;
2508 	}
2509 	phb_id = be64_to_cpup(prop64);
2510 	pr_debug("  PHB-ID  : 0x%016llx\n", phb_id);
2511 
2512 	phb = kzalloc_obj(*phb);
2513 	if (!phb)
2514 		panic("%s: Failed to allocate %zu bytes\n", __func__,
2515 		      sizeof(*phb));
2516 
2517 	/* Allocate PCI controller */
2518 	phb->hose = hose = pcibios_alloc_controller(np);
2519 	if (!phb->hose) {
2520 		pr_err("  Can't allocate PCI controller for %pOF\n",
2521 		       np);
2522 		memblock_free(phb, sizeof(struct pnv_phb));
2523 		return;
2524 	}
2525 
2526 	spin_lock_init(&phb->lock);
2527 	prop32 = of_get_property(np, "bus-range", &len);
2528 	if (prop32 && len == 8) {
2529 		hose->first_busno = be32_to_cpu(prop32[0]);
2530 		hose->last_busno = be32_to_cpu(prop32[1]);
2531 	} else {
2532 		pr_warn("  Broken <bus-range> on %pOF\n", np);
2533 		hose->first_busno = 0;
2534 		hose->last_busno = 0xff;
2535 	}
2536 	hose->private_data = phb;
2537 	phb->hub_id = hub_id;
2538 	phb->opal_id = phb_id;
2539 	phb->type = ioda_type;
2540 	mutex_init(&phb->ioda.pe_alloc_mutex);
2541 
2542 	/* Detect specific models for error handling */
2543 	if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
2544 		phb->model = PNV_PHB_MODEL_P7IOC;
2545 	else if (of_device_is_compatible(np, "ibm,power8-pciex"))
2546 		phb->model = PNV_PHB_MODEL_PHB3;
2547 	else
2548 		phb->model = PNV_PHB_MODEL_UNKNOWN;
2549 
2550 	/* Initialize diagnostic data buffer */
2551 	prop32 = of_get_property(np, "ibm,phb-diag-data-size", NULL);
2552 	if (prop32)
2553 		phb->diag_data_size = be32_to_cpup(prop32);
2554 	else
2555 		phb->diag_data_size = PNV_PCI_DIAG_BUF_SIZE;
2556 
2557 	phb->diag_data = kzalloc(phb->diag_data_size, GFP_KERNEL);
2558 	if (!phb->diag_data)
2559 		panic("%s: Failed to allocate %u bytes\n", __func__,
2560 		      phb->diag_data_size);
2561 
2562 	/* Parse 32-bit and IO ranges (if any) */
2563 	pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
2564 
2565 	/* Get registers */
2566 	if (!of_address_to_resource(np, 0, &r)) {
2567 		phb->regs_phys = r.start;
2568 		phb->regs = ioremap(r.start, resource_size(&r));
2569 		if (phb->regs == NULL)
2570 			pr_err("  Failed to map registers !\n");
2571 	}
2572 
2573 	/* Initialize more IODA stuff */
2574 	phb->ioda.total_pe_num = 1;
2575 	prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
2576 	if (prop32)
2577 		phb->ioda.total_pe_num = be32_to_cpup(prop32);
2578 	prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
2579 	if (prop32)
2580 		phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
2581 
2582 	/* Invalidate RID to PE# mapping */
2583 	for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
2584 		phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
2585 
2586 	/* Parse 64-bit MMIO range */
2587 	pnv_ioda_parse_m64_window(phb);
2588 
2589 	phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
2590 	/* FW Has already off top 64k of M32 space (MSI space) */
2591 	phb->ioda.m32_size += 0x10000;
2592 
2593 	phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
2594 	phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
2595 	phb->ioda.io_size = hose->pci_io_size;
2596 	phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
2597 	phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
2598 
2599 	/* Allocate aux data & arrays. We don't have IO ports on PHB3 */
2600 	size = ALIGN(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
2601 			sizeof(unsigned long));
2602 	m64map_off = size;
2603 	size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
2604 	m32map_off = size;
2605 	size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
2606 	pemap_off = size;
2607 	size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
2608 	aux = kzalloc(size, GFP_KERNEL);
2609 	if (!aux)
2610 		panic("%s: Failed to allocate %lu bytes\n", __func__, size);
2611 
2612 	phb->ioda.pe_alloc = aux;
2613 	phb->ioda.m64_segmap = aux + m64map_off;
2614 	phb->ioda.m32_segmap = aux + m32map_off;
2615 	for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
2616 		phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
2617 		phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
2618 	}
2619 	phb->ioda.pe_array = aux + pemap_off;
2620 
2621 	/*
2622 	 * Choose PE number for root bus, which shouldn't have
2623 	 * M64 resources consumed by its child devices. To pick
2624 	 * the PE number adjacent to the reserved one if possible.
2625 	 */
2626 	pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
2627 	if (phb->ioda.reserved_pe_idx == 0) {
2628 		phb->ioda.root_pe_idx = 1;
2629 		pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
2630 	} else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
2631 		phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
2632 		pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
2633 	} else {
2634 		/* otherwise just allocate one */
2635 		root_pe = pnv_ioda_alloc_pe(phb, 1);
2636 		phb->ioda.root_pe_idx = root_pe->pe_number;
2637 	}
2638 
2639 	INIT_LIST_HEAD(&phb->ioda.pe_list);
2640 	mutex_init(&phb->ioda.pe_list_mutex);
2641 
2642 #if 0 /* We should really do that ... */
2643 	rc = opal_pci_set_phb_mem_window(opal->phb_id,
2644 					 window_type,
2645 					 window_num,
2646 					 starting_real_address,
2647 					 starting_pci_address,
2648 					 segment_size);
2649 #endif
2650 
2651 	pr_info("  %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
2652 		phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
2653 		phb->ioda.m32_size, phb->ioda.m32_segsize);
2654 	if (phb->ioda.m64_size)
2655 		pr_info("                 M64: 0x%lx [segment=0x%lx]\n",
2656 			phb->ioda.m64_size, phb->ioda.m64_segsize);
2657 	if (phb->ioda.io_size)
2658 		pr_info("                  IO: 0x%x [segment=0x%x]\n",
2659 			phb->ioda.io_size, phb->ioda.io_segsize);
2660 
2661 
2662 	phb->hose->ops = &pnv_pci_ops;
2663 	phb->get_pe_state = pnv_ioda_get_pe_state;
2664 	phb->freeze_pe = pnv_ioda_freeze_pe;
2665 	phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
2666 
2667 	/* Setup MSI support */
2668 	pnv_pci_init_ioda_msis(phb);
2669 
2670 	/*
2671 	 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
2672 	 * to let the PCI core do resource assignment. It's supposed
2673 	 * that the PCI core will do correct I/O and MMIO alignment
2674 	 * for the P2P bridge bars so that each PCI bus (excluding
2675 	 * the child P2P bridges) can form individual PE.
2676 	 */
2677 	ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
2678 
2679 	switch (phb->type) {
2680 	case PNV_PHB_NPU_OCAPI:
2681 		hose->controller_ops = pnv_npu_ocapi_ioda_controller_ops;
2682 		break;
2683 	default:
2684 		hose->controller_ops = pnv_pci_ioda_controller_ops;
2685 	}
2686 
2687 	ppc_md.pcibios_default_alignment = pnv_pci_default_alignment;
2688 
2689 #ifdef CONFIG_PCI_IOV
2690 	ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov;
2691 	ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
2692 	ppc_md.pcibios_sriov_enable = pnv_pcibios_sriov_enable;
2693 	ppc_md.pcibios_sriov_disable = pnv_pcibios_sriov_disable;
2694 #endif
2695 
2696 	pci_add_flags(PCI_REASSIGN_ALL_RSRC);
2697 
2698 	/* Reset IODA tables to a clean state */
2699 	rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
2700 	if (rc)
2701 		pr_warn("  OPAL Error %ld performing IODA table reset !\n", rc);
2702 
2703 	/*
2704 	 * If we're running in kdump kernel, the previous kernel never
2705 	 * shutdown PCI devices correctly. We already got IODA table
2706 	 * cleaned out. So we have to issue PHB reset to stop all PCI
2707 	 * transactions from previous kernel. The ppc_pci_reset_phbs
2708 	 * kernel parameter will force this reset too. Additionally,
2709 	 * if the IODA reset above failed then use a bigger hammer.
2710 	 * This can happen if we get a PHB fatal error in very early
2711 	 * boot.
2712 	 */
2713 	if (is_kdump_kernel() || pci_reset_phbs || rc) {
2714 		pr_info("  Issue PHB reset ...\n");
2715 		pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
2716 		pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
2717 	}
2718 
2719 	/* Remove M64 resource if we can't configure it successfully */
2720 	if (!phb->init_m64 || phb->init_m64(phb))
2721 		hose->mem_resources[1].flags = 0;
2722 
2723 	/* create pci_dn's for DT nodes under this PHB */
2724 	pci_devs_phb_init_dynamic(hose);
2725 }
2726 
pnv_pci_init_ioda2_phb(struct device_node * np)2727 void __init pnv_pci_init_ioda2_phb(struct device_node *np)
2728 {
2729 	pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
2730 }
2731 
pnv_pci_init_npu2_opencapi_phb(struct device_node * np)2732 void __init pnv_pci_init_npu2_opencapi_phb(struct device_node *np)
2733 {
2734 	pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_OCAPI);
2735 }
2736 
pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev * dev)2737 static void pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev *dev)
2738 {
2739 	struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
2740 
2741 	if (!machine_is(powernv))
2742 		return;
2743 
2744 	if (phb->type == PNV_PHB_NPU_OCAPI)
2745 		dev->cfg_size = PCI_CFG_SPACE_EXP_SIZE;
2746 }
2747 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pnv_npu2_opencapi_cfg_size_fixup);
2748