]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/powerpc/platforms/powernv/pci-ioda.c
powerpc: pci-ioda: Remove unnecessary return value from printk
[mirror_ubuntu-focal-kernel.git] / arch / powerpc / platforms / powernv / pci-ioda.c
CommitLineData
184cd4a3
BH
1/*
2 * Support PCI/PCIe on PowerNV platforms
3 *
4 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
cee72d5b 12#undef DEBUG
184cd4a3
BH
13
14#include <linux/kernel.h>
15#include <linux/pci.h>
361f2a2a 16#include <linux/crash_dump.h>
37c367f2 17#include <linux/debugfs.h>
184cd4a3
BH
18#include <linux/delay.h>
19#include <linux/string.h>
20#include <linux/init.h>
21#include <linux/bootmem.h>
22#include <linux/irq.h>
23#include <linux/io.h>
24#include <linux/msi.h>
cd15b048 25#include <linux/memblock.h>
184cd4a3
BH
26
27#include <asm/sections.h>
28#include <asm/io.h>
29#include <asm/prom.h>
30#include <asm/pci-bridge.h>
31#include <asm/machdep.h>
fb1b55d6 32#include <asm/msi_bitmap.h>
184cd4a3
BH
33#include <asm/ppc-pci.h>
34#include <asm/opal.h>
35#include <asm/iommu.h>
36#include <asm/tce.h>
137436c9 37#include <asm/xics.h>
37c367f2 38#include <asm/debug.h>
262af557 39#include <asm/firmware.h>
184cd4a3
BH
40
41#include "powernv.h"
42#include "pci.h"
43
184cd4a3 44#define define_pe_printk_level(func, kern_level) \
45eb4724 45static void func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
184cd4a3
BH
46{ \
47 struct va_format vaf; \
48 va_list args; \
490e078d 49 char pfix[32]; \
184cd4a3
BH
50 \
51 va_start(args, fmt); \
52 \
53 vaf.fmt = fmt; \
54 vaf.va = &args; \
55 \
490e078d
GS
56 if (pe->pdev) \
57 strlcpy(pfix, dev_name(&pe->pdev->dev), \
58 sizeof(pfix)); \
59 else \
60 sprintf(pfix, "%04x:%02x ", \
61 pci_domain_nr(pe->pbus), \
62 pe->pbus->number); \
45eb4724
JP
63 printk(kern_level "pci %s: [PE# %.3d] %pV", \
64 pfix, pe->pe_number, &vaf); \
490e078d 65 \
184cd4a3 66 va_end(args); \
184cd4a3
BH
67} \
68
69define_pe_printk_level(pe_err, KERN_ERR);
70define_pe_printk_level(pe_warn, KERN_WARNING);
71define_pe_printk_level(pe_info, KERN_INFO);
72
8e0a1611
AK
73/*
74 * stdcix is only supposed to be used in hypervisor real mode as per
75 * the architecture spec
76 */
77static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
78{
79 __asm__ __volatile__("stdcix %0,0,%1"
80 : : "r" (val), "r" (paddr) : "memory");
81}
82
262af557
GC
83static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
84{
85 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
86 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
87}
88
cad5cef6 89static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
184cd4a3
BH
90{
91 unsigned long pe;
92
93 do {
94 pe = find_next_zero_bit(phb->ioda.pe_alloc,
95 phb->ioda.total_pe, 0);
96 if (pe >= phb->ioda.total_pe)
97 return IODA_INVALID_PE;
98 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
99
4cce9550 100 phb->ioda.pe_array[pe].phb = phb;
184cd4a3
BH
101 phb->ioda.pe_array[pe].pe_number = pe;
102 return pe;
103}
104
cad5cef6 105static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
184cd4a3
BH
106{
107 WARN_ON(phb->ioda.pe_array[pe].pdev);
108
109 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
110 clear_bit(pe, phb->ioda.pe_alloc);
111}
112
262af557
GC
113/* The default M64 BAR is shared by all PEs */
114static int pnv_ioda2_init_m64(struct pnv_phb *phb)
115{
116 const char *desc;
117 struct resource *r;
118 s64 rc;
119
120 /* Configure the default M64 BAR */
121 rc = opal_pci_set_phb_mem_window(phb->opal_id,
122 OPAL_M64_WINDOW_TYPE,
123 phb->ioda.m64_bar_idx,
124 phb->ioda.m64_base,
125 0, /* unused */
126 phb->ioda.m64_size);
127 if (rc != OPAL_SUCCESS) {
128 desc = "configuring";
129 goto fail;
130 }
131
132 /* Enable the default M64 BAR */
133 rc = opal_pci_phb_mmio_enable(phb->opal_id,
134 OPAL_M64_WINDOW_TYPE,
135 phb->ioda.m64_bar_idx,
136 OPAL_ENABLE_M64_SPLIT);
137 if (rc != OPAL_SUCCESS) {
138 desc = "enabling";
139 goto fail;
140 }
141
142 /* Mark the M64 BAR assigned */
143 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
144
145 /*
146 * Strip off the segment used by the reserved PE, which is
147 * expected to be 0 or last one of PE capabicity.
148 */
149 r = &phb->hose->mem_resources[1];
150 if (phb->ioda.reserved_pe == 0)
151 r->start += phb->ioda.m64_segsize;
152 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
153 r->end -= phb->ioda.m64_segsize;
154 else
155 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
156 phb->ioda.reserved_pe);
157
158 return 0;
159
160fail:
161 pr_warn(" Failure %lld %s M64 BAR#%d\n",
162 rc, desc, phb->ioda.m64_bar_idx);
163 opal_pci_phb_mmio_enable(phb->opal_id,
164 OPAL_M64_WINDOW_TYPE,
165 phb->ioda.m64_bar_idx,
166 OPAL_DISABLE_M64);
167 return -EIO;
168}
169
170static void pnv_ioda2_alloc_m64_pe(struct pnv_phb *phb)
171{
172 resource_size_t sgsz = phb->ioda.m64_segsize;
173 struct pci_dev *pdev;
174 struct resource *r;
175 int base, step, i;
176
177 /*
178 * Root bus always has full M64 range and root port has
179 * M64 range used in reality. So we're checking root port
180 * instead of root bus.
181 */
182 list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
183 for (i = PCI_BRIDGE_RESOURCES;
184 i <= PCI_BRIDGE_RESOURCE_END; i++) {
185 r = &pdev->resource[i];
186 if (!r->parent ||
187 !pnv_pci_is_mem_pref_64(r->flags))
188 continue;
189
190 base = (r->start - phb->ioda.m64_base) / sgsz;
191 for (step = 0; step < resource_size(r) / sgsz; step++)
192 set_bit(base + step, phb->ioda.pe_alloc);
193 }
194 }
195}
196
197static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
198 struct pci_bus *bus, int all)
199{
200 resource_size_t segsz = phb->ioda.m64_segsize;
201 struct pci_dev *pdev;
202 struct resource *r;
203 struct pnv_ioda_pe *master_pe, *pe;
204 unsigned long size, *pe_alloc;
205 bool found;
206 int start, i, j;
207
208 /* Root bus shouldn't use M64 */
209 if (pci_is_root_bus(bus))
210 return IODA_INVALID_PE;
211
212 /* We support only one M64 window on each bus */
213 found = false;
214 pci_bus_for_each_resource(bus, r, i) {
215 if (r && r->parent &&
216 pnv_pci_is_mem_pref_64(r->flags)) {
217 found = true;
218 break;
219 }
220 }
221
222 /* No M64 window found ? */
223 if (!found)
224 return IODA_INVALID_PE;
225
226 /* Allocate bitmap */
227 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
228 pe_alloc = kzalloc(size, GFP_KERNEL);
229 if (!pe_alloc) {
230 pr_warn("%s: Out of memory !\n",
231 __func__);
232 return IODA_INVALID_PE;
233 }
234
235 /*
236 * Figure out reserved PE numbers by the PE
237 * the its child PEs.
238 */
239 start = (r->start - phb->ioda.m64_base) / segsz;
240 for (i = 0; i < resource_size(r) / segsz; i++)
241 set_bit(start + i, pe_alloc);
242
243 if (all)
244 goto done;
245
246 /*
247 * If the PE doesn't cover all subordinate buses,
248 * we need subtract from reserved PEs for children.
249 */
250 list_for_each_entry(pdev, &bus->devices, bus_list) {
251 if (!pdev->subordinate)
252 continue;
253
254 pci_bus_for_each_resource(pdev->subordinate, r, i) {
255 if (!r || !r->parent ||
256 !pnv_pci_is_mem_pref_64(r->flags))
257 continue;
258
259 start = (r->start - phb->ioda.m64_base) / segsz;
260 for (j = 0; j < resource_size(r) / segsz ; j++)
261 clear_bit(start + j, pe_alloc);
262 }
263 }
264
265 /*
266 * the current bus might not own M64 window and that's all
267 * contributed by its child buses. For the case, we needn't
268 * pick M64 dependent PE#.
269 */
270 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
271 kfree(pe_alloc);
272 return IODA_INVALID_PE;
273 }
274
275 /*
276 * Figure out the master PE and put all slave PEs to master
277 * PE's list to form compound PE.
278 */
279done:
280 master_pe = NULL;
281 i = -1;
282 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
283 phb->ioda.total_pe) {
284 pe = &phb->ioda.pe_array[i];
285 pe->phb = phb;
286 pe->pe_number = i;
287
288 if (!master_pe) {
289 pe->flags |= PNV_IODA_PE_MASTER;
290 INIT_LIST_HEAD(&pe->slaves);
291 master_pe = pe;
292 } else {
293 pe->flags |= PNV_IODA_PE_SLAVE;
294 pe->master = master_pe;
295 list_add_tail(&pe->list, &master_pe->slaves);
296 }
297 }
298
299 kfree(pe_alloc);
300 return master_pe->pe_number;
301}
302
303static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
304{
305 struct pci_controller *hose = phb->hose;
306 struct device_node *dn = hose->dn;
307 struct resource *res;
308 const u32 *r;
309 u64 pci_addr;
310
311 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
312 pr_info(" Firmware too old to support M64 window\n");
313 return;
314 }
315
316 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
317 if (!r) {
318 pr_info(" No <ibm,opal-m64-window> on %s\n",
319 dn->full_name);
320 return;
321 }
322
323 /* FIXME: Support M64 for P7IOC */
324 if (phb->type != PNV_PHB_IODA2) {
325 pr_info(" Not support M64 window\n");
326 return;
327 }
328
329 res = &hose->mem_resources[1];
330 res->start = of_translate_address(dn, r + 2);
331 res->end = res->start + of_read_number(r + 4, 2) - 1;
332 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
333 pci_addr = of_read_number(r, 2);
334 hose->mem_offset[1] = res->start - pci_addr;
335
336 phb->ioda.m64_size = resource_size(res);
337 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
338 phb->ioda.m64_base = pci_addr;
339
340 /* Use last M64 BAR to cover M64 window */
341 phb->ioda.m64_bar_idx = 15;
342 phb->init_m64 = pnv_ioda2_init_m64;
343 phb->alloc_m64_pe = pnv_ioda2_alloc_m64_pe;
344 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
345}
346
49dec922
GS
347static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
348{
349 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
350 struct pnv_ioda_pe *slave;
351 s64 rc;
352
353 /* Fetch master PE */
354 if (pe->flags & PNV_IODA_PE_SLAVE) {
355 pe = pe->master;
356 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
357 pe_no = pe->pe_number;
358 }
359
360 /* Freeze master PE */
361 rc = opal_pci_eeh_freeze_set(phb->opal_id,
362 pe_no,
363 OPAL_EEH_ACTION_SET_FREEZE_ALL);
364 if (rc != OPAL_SUCCESS) {
365 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
366 __func__, rc, phb->hose->global_number, pe_no);
367 return;
368 }
369
370 /* Freeze slave PEs */
371 if (!(pe->flags & PNV_IODA_PE_MASTER))
372 return;
373
374 list_for_each_entry(slave, &pe->slaves, list) {
375 rc = opal_pci_eeh_freeze_set(phb->opal_id,
376 slave->pe_number,
377 OPAL_EEH_ACTION_SET_FREEZE_ALL);
378 if (rc != OPAL_SUCCESS)
379 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
380 __func__, rc, phb->hose->global_number,
381 slave->pe_number);
382 }
383}
384
e51df2c1 385static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
49dec922
GS
386{
387 struct pnv_ioda_pe *pe, *slave;
388 s64 rc;
389
390 /* Find master PE */
391 pe = &phb->ioda.pe_array[pe_no];
392 if (pe->flags & PNV_IODA_PE_SLAVE) {
393 pe = pe->master;
394 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
395 pe_no = pe->pe_number;
396 }
397
398 /* Clear frozen state for master PE */
399 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
400 if (rc != OPAL_SUCCESS) {
401 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
402 __func__, rc, opt, phb->hose->global_number, pe_no);
403 return -EIO;
404 }
405
406 if (!(pe->flags & PNV_IODA_PE_MASTER))
407 return 0;
408
409 /* Clear frozen state for slave PEs */
410 list_for_each_entry(slave, &pe->slaves, list) {
411 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
412 slave->pe_number,
413 opt);
414 if (rc != OPAL_SUCCESS) {
415 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
416 __func__, rc, opt, phb->hose->global_number,
417 slave->pe_number);
418 return -EIO;
419 }
420 }
421
422 return 0;
423}
424
425static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
426{
427 struct pnv_ioda_pe *slave, *pe;
428 u8 fstate, state;
429 __be16 pcierr;
430 s64 rc;
431
432 /* Sanity check on PE number */
433 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
434 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
435
436 /*
437 * Fetch the master PE and the PE instance might be
438 * not initialized yet.
439 */
440 pe = &phb->ioda.pe_array[pe_no];
441 if (pe->flags & PNV_IODA_PE_SLAVE) {
442 pe = pe->master;
443 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
444 pe_no = pe->pe_number;
445 }
446
447 /* Check the master PE */
448 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
449 &state, &pcierr, NULL);
450 if (rc != OPAL_SUCCESS) {
451 pr_warn("%s: Failure %lld getting "
452 "PHB#%x-PE#%x state\n",
453 __func__, rc,
454 phb->hose->global_number, pe_no);
455 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
456 }
457
458 /* Check the slave PE */
459 if (!(pe->flags & PNV_IODA_PE_MASTER))
460 return state;
461
462 list_for_each_entry(slave, &pe->slaves, list) {
463 rc = opal_pci_eeh_freeze_status(phb->opal_id,
464 slave->pe_number,
465 &fstate,
466 &pcierr,
467 NULL);
468 if (rc != OPAL_SUCCESS) {
469 pr_warn("%s: Failure %lld getting "
470 "PHB#%x-PE#%x state\n",
471 __func__, rc,
472 phb->hose->global_number, slave->pe_number);
473 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
474 }
475
476 /*
477 * Override the result based on the ascending
478 * priority.
479 */
480 if (fstate > state)
481 state = fstate;
482 }
483
484 return state;
485}
486
184cd4a3
BH
487/* Currently those 2 are only used when MSIs are enabled, this will change
488 * but in the meantime, we need to protect them to avoid warnings
489 */
490#ifdef CONFIG_PCI_MSI
cad5cef6 491static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
184cd4a3
BH
492{
493 struct pci_controller *hose = pci_bus_to_host(dev->bus);
494 struct pnv_phb *phb = hose->private_data;
b72c1f65 495 struct pci_dn *pdn = pci_get_pdn(dev);
184cd4a3
BH
496
497 if (!pdn)
498 return NULL;
499 if (pdn->pe_number == IODA_INVALID_PE)
500 return NULL;
501 return &phb->ioda.pe_array[pdn->pe_number];
502}
184cd4a3
BH
503#endif /* CONFIG_PCI_MSI */
504
cad5cef6 505static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
184cd4a3
BH
506{
507 struct pci_dev *parent;
508 uint8_t bcomp, dcomp, fcomp;
509 long rc, rid_end, rid;
510
511 /* Bus validation ? */
512 if (pe->pbus) {
513 int count;
514
515 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
516 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
517 parent = pe->pbus->self;
fb446ad0
GS
518 if (pe->flags & PNV_IODA_PE_BUS_ALL)
519 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
520 else
521 count = 1;
522
184cd4a3
BH
523 switch(count) {
524 case 1: bcomp = OpalPciBusAll; break;
525 case 2: bcomp = OpalPciBus7Bits; break;
526 case 4: bcomp = OpalPciBus6Bits; break;
527 case 8: bcomp = OpalPciBus5Bits; break;
528 case 16: bcomp = OpalPciBus4Bits; break;
529 case 32: bcomp = OpalPciBus3Bits; break;
530 default:
531 pr_err("%s: Number of subordinate busses %d"
532 " unsupported\n",
533 pci_name(pe->pbus->self), count);
534 /* Do an exact match only */
535 bcomp = OpalPciBusAll;
536 }
537 rid_end = pe->rid + (count << 8);
538 } else {
539 parent = pe->pdev->bus->self;
540 bcomp = OpalPciBusAll;
541 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
542 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
543 rid_end = pe->rid + 1;
544 }
545
631ad691
GS
546 /*
547 * Associate PE in PELT. We need add the PE into the
548 * corresponding PELT-V as well. Otherwise, the error
549 * originated from the PE might contribute to other
550 * PEs.
551 */
184cd4a3
BH
552 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
553 bcomp, dcomp, fcomp, OPAL_MAP_PE);
554 if (rc) {
555 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
556 return -ENXIO;
557 }
631ad691
GS
558
559 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
560 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
561 if (rc)
562 pe_warn(pe, "OPAL error %d adding self to PELTV\n", rc);
184cd4a3
BH
563 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
564 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
565
566 /* Add to all parents PELT-V */
567 while (parent) {
b72c1f65 568 struct pci_dn *pdn = pci_get_pdn(parent);
184cd4a3
BH
569 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
570 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
cee72d5b 571 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
184cd4a3
BH
572 /* XXX What to do in case of error ? */
573 }
574 parent = parent->bus->self;
575 }
576 /* Setup reverse map */
577 for (rid = pe->rid; rid < rid_end; rid++)
578 phb->ioda.pe_rmap[rid] = pe->pe_number;
579
580 /* Setup one MVTs on IODA1 */
581 if (phb->type == PNV_PHB_IODA1) {
582 pe->mve_number = pe->pe_number;
583 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
584 pe->pe_number);
585 if (rc) {
586 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
587 rc, pe->mve_number);
588 pe->mve_number = -1;
589 } else {
590 rc = opal_pci_set_mve_enable(phb->opal_id,
cee72d5b 591 pe->mve_number, OPAL_ENABLE_MVE);
184cd4a3
BH
592 if (rc) {
593 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
594 rc, pe->mve_number);
595 pe->mve_number = -1;
596 }
597 }
598 } else if (phb->type == PNV_PHB_IODA2)
599 pe->mve_number = 0;
600
601 return 0;
602}
603
cad5cef6
GKH
604static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
605 struct pnv_ioda_pe *pe)
184cd4a3
BH
606{
607 struct pnv_ioda_pe *lpe;
608
7ebdf956 609 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
184cd4a3 610 if (lpe->dma_weight < pe->dma_weight) {
7ebdf956 611 list_add_tail(&pe->dma_link, &lpe->dma_link);
184cd4a3
BH
612 return;
613 }
614 }
7ebdf956 615 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
184cd4a3
BH
616}
617
618static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
619{
620 /* This is quite simplistic. The "base" weight of a device
621 * is 10. 0 means no DMA is to be accounted for it.
622 */
623
624 /* If it's a bridge, no DMA */
625 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
626 return 0;
627
628 /* Reduce the weight of slow USB controllers */
629 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
630 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
631 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
632 return 3;
633
634 /* Increase the weight of RAID (includes Obsidian) */
635 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
636 return 15;
637
638 /* Default */
639 return 10;
640}
641
fb446ad0 642#if 0
cad5cef6 643static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
184cd4a3
BH
644{
645 struct pci_controller *hose = pci_bus_to_host(dev->bus);
646 struct pnv_phb *phb = hose->private_data;
b72c1f65 647 struct pci_dn *pdn = pci_get_pdn(dev);
184cd4a3
BH
648 struct pnv_ioda_pe *pe;
649 int pe_num;
650
651 if (!pdn) {
652 pr_err("%s: Device tree node not associated properly\n",
653 pci_name(dev));
654 return NULL;
655 }
656 if (pdn->pe_number != IODA_INVALID_PE)
657 return NULL;
658
659 /* PE#0 has been pre-set */
660 if (dev->bus->number == 0)
661 pe_num = 0;
662 else
663 pe_num = pnv_ioda_alloc_pe(phb);
664 if (pe_num == IODA_INVALID_PE) {
665 pr_warning("%s: Not enough PE# available, disabling device\n",
666 pci_name(dev));
667 return NULL;
668 }
669
670 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
671 * pointer in the PE data structure, both should be destroyed at the
672 * same time. However, this needs to be looked at more closely again
673 * once we actually start removing things (Hotplug, SR-IOV, ...)
674 *
675 * At some point we want to remove the PDN completely anyways
676 */
677 pe = &phb->ioda.pe_array[pe_num];
678 pci_dev_get(dev);
679 pdn->pcidev = dev;
680 pdn->pe_number = pe_num;
681 pe->pdev = dev;
682 pe->pbus = NULL;
683 pe->tce32_seg = -1;
684 pe->mve_number = -1;
685 pe->rid = dev->bus->number << 8 | pdn->devfn;
686
687 pe_info(pe, "Associated device to PE\n");
688
689 if (pnv_ioda_configure_pe(phb, pe)) {
690 /* XXX What do we do here ? */
691 if (pe_num)
692 pnv_ioda_free_pe(phb, pe_num);
693 pdn->pe_number = IODA_INVALID_PE;
694 pe->pdev = NULL;
695 pci_dev_put(dev);
696 return NULL;
697 }
698
699 /* Assign a DMA weight to the device */
700 pe->dma_weight = pnv_ioda_dma_weight(dev);
701 if (pe->dma_weight != 0) {
702 phb->ioda.dma_weight += pe->dma_weight;
703 phb->ioda.dma_pe_count++;
704 }
705
706 /* Link the PE */
707 pnv_ioda_link_pe_by_weight(phb, pe);
708
709 return pe;
710}
fb446ad0 711#endif /* Useful for SRIOV case */
184cd4a3
BH
712
713static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
714{
715 struct pci_dev *dev;
716
717 list_for_each_entry(dev, &bus->devices, bus_list) {
b72c1f65 718 struct pci_dn *pdn = pci_get_pdn(dev);
184cd4a3
BH
719
720 if (pdn == NULL) {
721 pr_warn("%s: No device node associated with device !\n",
722 pci_name(dev));
723 continue;
724 }
184cd4a3
BH
725 pdn->pcidev = dev;
726 pdn->pe_number = pe->pe_number;
727 pe->dma_weight += pnv_ioda_dma_weight(dev);
fb446ad0 728 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
184cd4a3
BH
729 pnv_ioda_setup_same_PE(dev->subordinate, pe);
730 }
731}
732
fb446ad0
GS
733/*
734 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
735 * single PCI bus. Another one that contains the primary PCI bus and its
736 * subordinate PCI devices and buses. The second type of PE is normally
737 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
738 */
cad5cef6 739static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
184cd4a3 740{
fb446ad0 741 struct pci_controller *hose = pci_bus_to_host(bus);
184cd4a3 742 struct pnv_phb *phb = hose->private_data;
184cd4a3 743 struct pnv_ioda_pe *pe;
262af557
GC
744 int pe_num = IODA_INVALID_PE;
745
746 /* Check if PE is determined by M64 */
747 if (phb->pick_m64_pe)
748 pe_num = phb->pick_m64_pe(phb, bus, all);
749
750 /* The PE number isn't pinned by M64 */
751 if (pe_num == IODA_INVALID_PE)
752 pe_num = pnv_ioda_alloc_pe(phb);
184cd4a3 753
184cd4a3 754 if (pe_num == IODA_INVALID_PE) {
fb446ad0
GS
755 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
756 __func__, pci_domain_nr(bus), bus->number);
184cd4a3
BH
757 return;
758 }
759
760 pe = &phb->ioda.pe_array[pe_num];
262af557 761 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
184cd4a3
BH
762 pe->pbus = bus;
763 pe->pdev = NULL;
764 pe->tce32_seg = -1;
765 pe->mve_number = -1;
b918c62e 766 pe->rid = bus->busn_res.start << 8;
184cd4a3
BH
767 pe->dma_weight = 0;
768
fb446ad0
GS
769 if (all)
770 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
771 bus->busn_res.start, bus->busn_res.end, pe_num);
772 else
773 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
774 bus->busn_res.start, pe_num);
184cd4a3
BH
775
776 if (pnv_ioda_configure_pe(phb, pe)) {
777 /* XXX What do we do here ? */
778 if (pe_num)
779 pnv_ioda_free_pe(phb, pe_num);
780 pe->pbus = NULL;
781 return;
782 }
783
784 /* Associate it with all child devices */
785 pnv_ioda_setup_same_PE(bus, pe);
786
7ebdf956
GS
787 /* Put PE to the list */
788 list_add_tail(&pe->list, &phb->ioda.pe_list);
789
184cd4a3
BH
790 /* Account for one DMA PE if at least one DMA capable device exist
791 * below the bridge
792 */
793 if (pe->dma_weight != 0) {
794 phb->ioda.dma_weight += pe->dma_weight;
795 phb->ioda.dma_pe_count++;
796 }
797
798 /* Link the PE */
799 pnv_ioda_link_pe_by_weight(phb, pe);
800}
801
cad5cef6 802static void pnv_ioda_setup_PEs(struct pci_bus *bus)
184cd4a3
BH
803{
804 struct pci_dev *dev;
fb446ad0
GS
805
806 pnv_ioda_setup_bus_PE(bus, 0);
184cd4a3
BH
807
808 list_for_each_entry(dev, &bus->devices, bus_list) {
fb446ad0
GS
809 if (dev->subordinate) {
810 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
811 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
812 else
813 pnv_ioda_setup_PEs(dev->subordinate);
814 }
815 }
816}
817
818/*
819 * Configure PEs so that the downstream PCI buses and devices
820 * could have their associated PE#. Unfortunately, we didn't
821 * figure out the way to identify the PLX bridge yet. So we
822 * simply put the PCI bus and the subordinate behind the root
823 * port to PE# here. The game rule here is expected to be changed
824 * as soon as we can detected PLX bridge correctly.
825 */
cad5cef6 826static void pnv_pci_ioda_setup_PEs(void)
fb446ad0
GS
827{
828 struct pci_controller *hose, *tmp;
262af557 829 struct pnv_phb *phb;
fb446ad0
GS
830
831 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
262af557
GC
832 phb = hose->private_data;
833
834 /* M64 layout might affect PE allocation */
835 if (phb->alloc_m64_pe)
836 phb->alloc_m64_pe(phb);
837
fb446ad0 838 pnv_ioda_setup_PEs(hose->bus);
184cd4a3
BH
839 }
840}
841
959c9bdd 842static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
184cd4a3 843{
b72c1f65 844 struct pci_dn *pdn = pci_get_pdn(pdev);
959c9bdd 845 struct pnv_ioda_pe *pe;
184cd4a3 846
959c9bdd
GS
847 /*
848 * The function can be called while the PE#
849 * hasn't been assigned. Do nothing for the
850 * case.
851 */
852 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
853 return;
184cd4a3 854
959c9bdd 855 pe = &phb->ioda.pe_array[pdn->pe_number];
cd15b048 856 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
763fe0ad 857 set_iommu_table_base_and_group(&pdev->dev, &pe->tce32_table);
184cd4a3
BH
858}
859
cd15b048
BH
860static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
861 struct pci_dev *pdev, u64 dma_mask)
862{
863 struct pci_dn *pdn = pci_get_pdn(pdev);
864 struct pnv_ioda_pe *pe;
865 uint64_t top;
866 bool bypass = false;
867
868 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
869 return -ENODEV;;
870
871 pe = &phb->ioda.pe_array[pdn->pe_number];
872 if (pe->tce_bypass_enabled) {
873 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
874 bypass = (dma_mask >= top);
875 }
876
877 if (bypass) {
878 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
879 set_dma_ops(&pdev->dev, &dma_direct_ops);
880 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
881 } else {
882 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
883 set_dma_ops(&pdev->dev, &dma_iommu_ops);
884 set_iommu_table_base(&pdev->dev, &pe->tce32_table);
885 }
a32305bf 886 *pdev->dev.dma_mask = dma_mask;
cd15b048
BH
887 return 0;
888}
889
dff4a39e
GS
890static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
891 struct pci_bus *bus,
892 bool add_to_iommu_group)
74251fe2
BH
893{
894 struct pci_dev *dev;
895
896 list_for_each_entry(dev, &bus->devices, bus_list) {
dff4a39e
GS
897 if (add_to_iommu_group)
898 set_iommu_table_base_and_group(&dev->dev,
899 &pe->tce32_table);
900 else
901 set_iommu_table_base(&dev->dev, &pe->tce32_table);
902
74251fe2 903 if (dev->subordinate)
dff4a39e
GS
904 pnv_ioda_setup_bus_dma(pe, dev->subordinate,
905 add_to_iommu_group);
74251fe2
BH
906 }
907}
908
8e0a1611
AK
909static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
910 struct iommu_table *tbl,
3ad26e5c 911 __be64 *startp, __be64 *endp, bool rm)
4cce9550 912{
3ad26e5c
BH
913 __be64 __iomem *invalidate = rm ?
914 (__be64 __iomem *)pe->tce_inval_reg_phys :
915 (__be64 __iomem *)tbl->it_index;
4cce9550 916 unsigned long start, end, inc;
b0376c9b 917 const unsigned shift = tbl->it_page_shift;
4cce9550
GS
918
919 start = __pa(startp);
920 end = __pa(endp);
921
922 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
923 if (tbl->it_busno) {
b0376c9b
AK
924 start <<= shift;
925 end <<= shift;
926 inc = 128ull << shift;
4cce9550
GS
927 start |= tbl->it_busno;
928 end |= tbl->it_busno;
929 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
930 /* p7ioc-style invalidation, 2 TCEs per write */
931 start |= (1ull << 63);
932 end |= (1ull << 63);
933 inc = 16;
934 } else {
935 /* Default (older HW) */
936 inc = 128;
937 }
938
939 end |= inc - 1; /* round up end to be different than start */
940
941 mb(); /* Ensure above stores are visible */
942 while (start <= end) {
8e0a1611 943 if (rm)
3ad26e5c 944 __raw_rm_writeq(cpu_to_be64(start), invalidate);
8e0a1611 945 else
3ad26e5c 946 __raw_writeq(cpu_to_be64(start), invalidate);
4cce9550
GS
947 start += inc;
948 }
949
950 /*
951 * The iommu layer will do another mb() for us on build()
952 * and we don't care on free()
953 */
954}
955
956static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
957 struct iommu_table *tbl,
3ad26e5c 958 __be64 *startp, __be64 *endp, bool rm)
4cce9550
GS
959{
960 unsigned long start, end, inc;
3ad26e5c
BH
961 __be64 __iomem *invalidate = rm ?
962 (__be64 __iomem *)pe->tce_inval_reg_phys :
963 (__be64 __iomem *)tbl->it_index;
b0376c9b 964 const unsigned shift = tbl->it_page_shift;
4cce9550
GS
965
966 /* We'll invalidate DMA address in PE scope */
b0376c9b 967 start = 0x2ull << 60;
4cce9550
GS
968 start |= (pe->pe_number & 0xFF);
969 end = start;
970
971 /* Figure out the start, end and step */
972 inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
b0376c9b 973 start |= (inc << shift);
4cce9550 974 inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
b0376c9b
AK
975 end |= (inc << shift);
976 inc = (0x1ull << shift);
4cce9550
GS
977 mb();
978
979 while (start <= end) {
8e0a1611 980 if (rm)
3ad26e5c 981 __raw_rm_writeq(cpu_to_be64(start), invalidate);
8e0a1611 982 else
3ad26e5c 983 __raw_writeq(cpu_to_be64(start), invalidate);
4cce9550
GS
984 start += inc;
985 }
986}
987
988void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
3ad26e5c 989 __be64 *startp, __be64 *endp, bool rm)
4cce9550
GS
990{
991 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
992 tce32_table);
993 struct pnv_phb *phb = pe->phb;
994
995 if (phb->type == PNV_PHB_IODA1)
8e0a1611 996 pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
4cce9550 997 else
8e0a1611 998 pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
4cce9550
GS
999}
1000
cad5cef6
GKH
1001static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1002 struct pnv_ioda_pe *pe, unsigned int base,
1003 unsigned int segs)
184cd4a3
BH
1004{
1005
1006 struct page *tce_mem = NULL;
1007 const __be64 *swinvp;
1008 struct iommu_table *tbl;
1009 unsigned int i;
1010 int64_t rc;
1011 void *addr;
1012
1013 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
1014#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
1015
1016 /* XXX FIXME: Handle 64-bit only DMA devices */
1017 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1018 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1019
1020 /* We shouldn't already have a 32-bit DMA associated */
1021 if (WARN_ON(pe->tce32_seg >= 0))
1022 return;
1023
1024 /* Grab a 32-bit TCE table */
1025 pe->tce32_seg = base;
1026 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1027 (base << 28), ((base + segs) << 28) - 1);
1028
1029 /* XXX Currently, we allocate one big contiguous table for the
1030 * TCEs. We only really need one chunk per 256M of TCE space
1031 * (ie per segment) but that's an optimization for later, it
1032 * requires some added smarts with our get/put_tce implementation
1033 */
1034 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1035 get_order(TCE32_TABLE_SIZE * segs));
1036 if (!tce_mem) {
1037 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1038 goto fail;
1039 }
1040 addr = page_address(tce_mem);
1041 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1042
1043 /* Configure HW */
1044 for (i = 0; i < segs; i++) {
1045 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1046 pe->pe_number,
1047 base + i, 1,
1048 __pa(addr) + TCE32_TABLE_SIZE * i,
1049 TCE32_TABLE_SIZE, 0x1000);
1050 if (rc) {
1051 pe_err(pe, " Failed to configure 32-bit TCE table,"
1052 " err %ld\n", rc);
1053 goto fail;
1054 }
1055 }
1056
1057 /* Setup linux iommu table */
1058 tbl = &pe->tce32_table;
1059 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
8fa5d454 1060 base << 28, IOMMU_PAGE_SHIFT_4K);
184cd4a3
BH
1061
1062 /* OPAL variant of P7IOC SW invalidated TCEs */
1063 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1064 if (swinvp) {
1065 /* We need a couple more fields -- an address and a data
1066 * to or. Since the bus is only printed out on table free
1067 * errors, and on the first pass the data will be a relative
1068 * bus number, print that out instead.
1069 */
8e0a1611
AK
1070 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1071 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1072 8);
65fd766b
GS
1073 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1074 TCE_PCI_SWINV_FREE |
1075 TCE_PCI_SWINV_PAIR);
184cd4a3
BH
1076 }
1077 iommu_init_table(tbl, phb->hose->node);
e9bc03fe 1078 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
184cd4a3 1079
74251fe2 1080 if (pe->pdev)
d905c5df 1081 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
74251fe2 1082 else
dff4a39e 1083 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
74251fe2 1084
184cd4a3
BH
1085 return;
1086 fail:
1087 /* XXX Failure: Try to fallback to 64-bit only ? */
1088 if (pe->tce32_seg >= 0)
1089 pe->tce32_seg = -1;
1090 if (tce_mem)
1091 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
1092}
1093
cd15b048
BH
1094static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
1095{
1096 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
1097 tce32_table);
1098 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1099 int64_t rc;
1100
1101 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1102 if (enable) {
1103 phys_addr_t top = memblock_end_of_DRAM();
1104
1105 top = roundup_pow_of_two(top);
1106 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1107 pe->pe_number,
1108 window_id,
1109 pe->tce_bypass_base,
1110 top);
1111 } else {
1112 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1113 pe->pe_number,
1114 window_id,
1115 pe->tce_bypass_base,
1116 0);
1117
1118 /*
dff4a39e
GS
1119 * EEH needs the mapping between IOMMU table and group
1120 * of those VFIO/KVM pass-through devices. We can postpone
1121 * resetting DMA ops until the DMA mask is configured in
1122 * host side.
cd15b048 1123 */
dff4a39e
GS
1124 if (pe->pdev)
1125 set_iommu_table_base(&pe->pdev->dev, tbl);
1126 else
1127 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
cd15b048
BH
1128 }
1129 if (rc)
1130 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1131 else
1132 pe->tce_bypass_enabled = enable;
1133}
1134
1135static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
1136 struct pnv_ioda_pe *pe)
1137{
1138 /* TVE #1 is selected by PCI address bit 59 */
1139 pe->tce_bypass_base = 1ull << 59;
1140
1141 /* Install set_bypass callback for VFIO */
1142 pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass;
1143
1144 /* Enable bypass by default */
1145 pnv_pci_ioda2_set_bypass(&pe->tce32_table, true);
1146}
1147
373f5657
GS
1148static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1149 struct pnv_ioda_pe *pe)
1150{
1151 struct page *tce_mem = NULL;
1152 void *addr;
1153 const __be64 *swinvp;
1154 struct iommu_table *tbl;
1155 unsigned int tce_table_size, end;
1156 int64_t rc;
1157
1158 /* We shouldn't already have a 32-bit DMA associated */
1159 if (WARN_ON(pe->tce32_seg >= 0))
1160 return;
1161
1162 /* The PE will reserve all possible 32-bits space */
1163 pe->tce32_seg = 0;
1164 end = (1 << ilog2(phb->ioda.m32_pci_base));
1165 tce_table_size = (end / 0x1000) * 8;
1166 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1167 end);
1168
1169 /* Allocate TCE table */
1170 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1171 get_order(tce_table_size));
1172 if (!tce_mem) {
1173 pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
1174 goto fail;
1175 }
1176 addr = page_address(tce_mem);
1177 memset(addr, 0, tce_table_size);
1178
1179 /*
1180 * Map TCE table through TVT. The TVE index is the PE number
1181 * shifted by 1 bit for 32-bits DMA space.
1182 */
1183 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1184 pe->pe_number << 1, 1, __pa(addr),
1185 tce_table_size, 0x1000);
1186 if (rc) {
1187 pe_err(pe, "Failed to configure 32-bit TCE table,"
1188 " err %ld\n", rc);
1189 goto fail;
1190 }
1191
1192 /* Setup linux iommu table */
1193 tbl = &pe->tce32_table;
8fa5d454
AK
1194 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
1195 IOMMU_PAGE_SHIFT_4K);
373f5657
GS
1196
1197 /* OPAL variant of PHB3 invalidated TCEs */
1198 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1199 if (swinvp) {
1200 /* We need a couple more fields -- an address and a data
1201 * to or. Since the bus is only printed out on table free
1202 * errors, and on the first pass the data will be a relative
1203 * bus number, print that out instead.
1204 */
8e0a1611
AK
1205 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1206 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1207 8);
65fd766b 1208 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
373f5657
GS
1209 }
1210 iommu_init_table(tbl, phb->hose->node);
e9bc03fe 1211 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
373f5657 1212
74251fe2 1213 if (pe->pdev)
d905c5df 1214 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
74251fe2 1215 else
dff4a39e 1216 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
74251fe2 1217
cd15b048
BH
1218 /* Also create a bypass window */
1219 pnv_pci_ioda2_setup_bypass_pe(phb, pe);
373f5657
GS
1220 return;
1221fail:
1222 if (pe->tce32_seg >= 0)
1223 pe->tce32_seg = -1;
1224 if (tce_mem)
1225 __free_pages(tce_mem, get_order(tce_table_size));
1226}
1227
cad5cef6 1228static void pnv_ioda_setup_dma(struct pnv_phb *phb)
184cd4a3
BH
1229{
1230 struct pci_controller *hose = phb->hose;
1231 unsigned int residual, remaining, segs, tw, base;
1232 struct pnv_ioda_pe *pe;
1233
1234 /* If we have more PE# than segments available, hand out one
1235 * per PE until we run out and let the rest fail. If not,
1236 * then we assign at least one segment per PE, plus more based
1237 * on the amount of devices under that PE
1238 */
1239 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
1240 residual = 0;
1241 else
1242 residual = phb->ioda.tce32_count -
1243 phb->ioda.dma_pe_count;
1244
1245 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
1246 hose->global_number, phb->ioda.tce32_count);
1247 pr_info("PCI: %d PE# for a total weight of %d\n",
1248 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
1249
1250 /* Walk our PE list and configure their DMA segments, hand them
1251 * out one base segment plus any residual segments based on
1252 * weight
1253 */
1254 remaining = phb->ioda.tce32_count;
1255 tw = phb->ioda.dma_weight;
1256 base = 0;
7ebdf956 1257 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
184cd4a3
BH
1258 if (!pe->dma_weight)
1259 continue;
1260 if (!remaining) {
1261 pe_warn(pe, "No DMA32 resources available\n");
1262 continue;
1263 }
1264 segs = 1;
1265 if (residual) {
1266 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
1267 if (segs > remaining)
1268 segs = remaining;
1269 }
373f5657
GS
1270
1271 /*
1272 * For IODA2 compliant PHB3, we needn't care about the weight.
1273 * The all available 32-bits DMA space will be assigned to
1274 * the specific PE.
1275 */
1276 if (phb->type == PNV_PHB_IODA1) {
1277 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1278 pe->dma_weight, segs);
1279 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1280 } else {
1281 pe_info(pe, "Assign DMA32 space\n");
1282 segs = 0;
1283 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1284 }
1285
184cd4a3
BH
1286 remaining -= segs;
1287 base += segs;
1288 }
1289}
1290
1291#ifdef CONFIG_PCI_MSI
137436c9
GS
1292static void pnv_ioda2_msi_eoi(struct irq_data *d)
1293{
1294 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1295 struct irq_chip *chip = irq_data_get_irq_chip(d);
1296 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
1297 ioda.irq_chip);
1298 int64_t rc;
1299
1300 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
1301 WARN_ON_ONCE(rc);
1302
1303 icp_native_eoi(d);
1304}
1305
184cd4a3 1306static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
137436c9
GS
1307 unsigned int hwirq, unsigned int virq,
1308 unsigned int is_64, struct msi_msg *msg)
184cd4a3
BH
1309{
1310 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
b72c1f65 1311 struct pci_dn *pdn = pci_get_pdn(dev);
137436c9
GS
1312 struct irq_data *idata;
1313 struct irq_chip *ichip;
184cd4a3 1314 unsigned int xive_num = hwirq - phb->msi_base;
3a1a4661 1315 __be32 data;
184cd4a3
BH
1316 int rc;
1317
1318 /* No PE assigned ? bail out ... no MSI for you ! */
1319 if (pe == NULL)
1320 return -ENXIO;
1321
1322 /* Check if we have an MVE */
1323 if (pe->mve_number < 0)
1324 return -ENXIO;
1325
b72c1f65
BH
1326 /* Force 32-bit MSI on some broken devices */
1327 if (pdn && pdn->force_32bit_msi)
1328 is_64 = 0;
1329
184cd4a3
BH
1330 /* Assign XIVE to PE */
1331 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1332 if (rc) {
1333 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1334 pci_name(dev), rc, xive_num);
1335 return -EIO;
1336 }
1337
1338 if (is_64) {
3a1a4661
BH
1339 __be64 addr64;
1340
184cd4a3
BH
1341 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1342 &addr64, &data);
1343 if (rc) {
1344 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1345 pci_name(dev), rc);
1346 return -EIO;
1347 }
3a1a4661
BH
1348 msg->address_hi = be64_to_cpu(addr64) >> 32;
1349 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
184cd4a3 1350 } else {
3a1a4661
BH
1351 __be32 addr32;
1352
184cd4a3
BH
1353 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1354 &addr32, &data);
1355 if (rc) {
1356 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1357 pci_name(dev), rc);
1358 return -EIO;
1359 }
1360 msg->address_hi = 0;
3a1a4661 1361 msg->address_lo = be32_to_cpu(addr32);
184cd4a3 1362 }
3a1a4661 1363 msg->data = be32_to_cpu(data);
184cd4a3 1364
137436c9
GS
1365 /*
1366 * Change the IRQ chip for the MSI interrupts on PHB3.
1367 * The corresponding IRQ chip should be populated for
1368 * the first time.
1369 */
1370 if (phb->type == PNV_PHB_IODA2) {
1371 if (!phb->ioda.irq_chip_init) {
1372 idata = irq_get_irq_data(virq);
1373 ichip = irq_data_get_irq_chip(idata);
1374 phb->ioda.irq_chip_init = 1;
1375 phb->ioda.irq_chip = *ichip;
1376 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
1377 }
1378
1379 irq_set_chip(virq, &phb->ioda.irq_chip);
1380 }
1381
184cd4a3
BH
1382 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1383 " address=%x_%08x data=%x PE# %d\n",
1384 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
1385 msg->address_hi, msg->address_lo, data, pe->pe_number);
1386
1387 return 0;
1388}
1389
1390static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1391{
fb1b55d6 1392 unsigned int count;
184cd4a3
BH
1393 const __be32 *prop = of_get_property(phb->hose->dn,
1394 "ibm,opal-msi-ranges", NULL);
1395 if (!prop) {
1396 /* BML Fallback */
1397 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1398 }
1399 if (!prop)
1400 return;
1401
1402 phb->msi_base = be32_to_cpup(prop);
fb1b55d6
GS
1403 count = be32_to_cpup(prop + 1);
1404 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
184cd4a3
BH
1405 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1406 phb->hose->global_number);
1407 return;
1408 }
fb1b55d6 1409
184cd4a3
BH
1410 phb->msi_setup = pnv_pci_ioda_msi_setup;
1411 phb->msi32_support = 1;
1412 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
fb1b55d6 1413 count, phb->msi_base);
184cd4a3
BH
1414}
1415#else
1416static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1417#endif /* CONFIG_PCI_MSI */
1418
11685bec
GS
1419/*
1420 * This function is supposed to be called on basis of PE from top
1421 * to bottom style. So the the I/O or MMIO segment assigned to
1422 * parent PE could be overrided by its child PEs if necessary.
1423 */
cad5cef6
GKH
1424static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
1425 struct pnv_ioda_pe *pe)
11685bec
GS
1426{
1427 struct pnv_phb *phb = hose->private_data;
1428 struct pci_bus_region region;
1429 struct resource *res;
1430 int i, index;
1431 int rc;
1432
1433 /*
1434 * NOTE: We only care PCI bus based PE for now. For PCI
1435 * device based PE, for example SRIOV sensitive VF should
1436 * be figured out later.
1437 */
1438 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
1439
1440 pci_bus_for_each_resource(pe->pbus, res, i) {
1441 if (!res || !res->flags ||
1442 res->start > res->end)
1443 continue;
1444
1445 if (res->flags & IORESOURCE_IO) {
1446 region.start = res->start - phb->ioda.io_pci_base;
1447 region.end = res->end - phb->ioda.io_pci_base;
1448 index = region.start / phb->ioda.io_segsize;
1449
1450 while (index < phb->ioda.total_pe &&
1451 region.start <= region.end) {
1452 phb->ioda.io_segmap[index] = pe->pe_number;
1453 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1454 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
1455 if (rc != OPAL_SUCCESS) {
1456 pr_err("%s: OPAL error %d when mapping IO "
1457 "segment #%d to PE#%d\n",
1458 __func__, rc, index, pe->pe_number);
1459 break;
1460 }
1461
1462 region.start += phb->ioda.io_segsize;
1463 index++;
1464 }
1465 } else if (res->flags & IORESOURCE_MEM) {
1466 region.start = res->start -
3fd47f06 1467 hose->mem_offset[0] -
11685bec
GS
1468 phb->ioda.m32_pci_base;
1469 region.end = res->end -
3fd47f06 1470 hose->mem_offset[0] -
11685bec
GS
1471 phb->ioda.m32_pci_base;
1472 index = region.start / phb->ioda.m32_segsize;
1473
1474 while (index < phb->ioda.total_pe &&
1475 region.start <= region.end) {
1476 phb->ioda.m32_segmap[index] = pe->pe_number;
1477 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1478 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
1479 if (rc != OPAL_SUCCESS) {
1480 pr_err("%s: OPAL error %d when mapping M32 "
1481 "segment#%d to PE#%d",
1482 __func__, rc, index, pe->pe_number);
1483 break;
1484 }
1485
1486 region.start += phb->ioda.m32_segsize;
1487 index++;
1488 }
1489 }
1490 }
1491}
1492
cad5cef6 1493static void pnv_pci_ioda_setup_seg(void)
11685bec
GS
1494{
1495 struct pci_controller *tmp, *hose;
1496 struct pnv_phb *phb;
1497 struct pnv_ioda_pe *pe;
1498
1499 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1500 phb = hose->private_data;
1501 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
1502 pnv_ioda_setup_pe_seg(hose, pe);
1503 }
1504 }
1505}
1506
cad5cef6 1507static void pnv_pci_ioda_setup_DMA(void)
13395c48
GS
1508{
1509 struct pci_controller *hose, *tmp;
db1266c8 1510 struct pnv_phb *phb;
13395c48
GS
1511
1512 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1513 pnv_ioda_setup_dma(hose->private_data);
db1266c8
GS
1514
1515 /* Mark the PHB initialization done */
1516 phb = hose->private_data;
1517 phb->initialized = 1;
13395c48
GS
1518 }
1519}
1520
37c367f2
GS
1521static void pnv_pci_ioda_create_dbgfs(void)
1522{
1523#ifdef CONFIG_DEBUG_FS
1524 struct pci_controller *hose, *tmp;
1525 struct pnv_phb *phb;
1526 char name[16];
1527
1528 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1529 phb = hose->private_data;
1530
1531 sprintf(name, "PCI%04x", hose->global_number);
1532 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
1533 if (!phb->dbgfs)
1534 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
1535 __func__, hose->global_number);
1536 }
1537#endif /* CONFIG_DEBUG_FS */
1538}
1539
cad5cef6 1540static void pnv_pci_ioda_fixup(void)
fb446ad0
GS
1541{
1542 pnv_pci_ioda_setup_PEs();
11685bec 1543 pnv_pci_ioda_setup_seg();
13395c48 1544 pnv_pci_ioda_setup_DMA();
e9cc17d4 1545
37c367f2
GS
1546 pnv_pci_ioda_create_dbgfs();
1547
e9cc17d4 1548#ifdef CONFIG_EEH
e9cc17d4 1549 eeh_init();
dadcd6d6 1550 eeh_addr_cache_build();
e9cc17d4 1551#endif
fb446ad0
GS
1552}
1553
271fd03a
GS
1554/*
1555 * Returns the alignment for I/O or memory windows for P2P
1556 * bridges. That actually depends on how PEs are segmented.
1557 * For now, we return I/O or M32 segment size for PE sensitive
1558 * P2P bridges. Otherwise, the default values (4KiB for I/O,
1559 * 1MiB for memory) will be returned.
1560 *
1561 * The current PCI bus might be put into one PE, which was
1562 * create against the parent PCI bridge. For that case, we
1563 * needn't enlarge the alignment so that we can save some
1564 * resources.
1565 */
1566static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
1567 unsigned long type)
1568{
1569 struct pci_dev *bridge;
1570 struct pci_controller *hose = pci_bus_to_host(bus);
1571 struct pnv_phb *phb = hose->private_data;
1572 int num_pci_bridges = 0;
1573
1574 bridge = bus->self;
1575 while (bridge) {
1576 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
1577 num_pci_bridges++;
1578 if (num_pci_bridges >= 2)
1579 return 1;
1580 }
1581
1582 bridge = bridge->bus->self;
1583 }
1584
262af557
GC
1585 /* We fail back to M32 if M64 isn't supported */
1586 if (phb->ioda.m64_segsize &&
1587 pnv_pci_is_mem_pref_64(type))
1588 return phb->ioda.m64_segsize;
271fd03a
GS
1589 if (type & IORESOURCE_MEM)
1590 return phb->ioda.m32_segsize;
1591
1592 return phb->ioda.io_segsize;
1593}
1594
184cd4a3
BH
1595/* Prevent enabling devices for which we couldn't properly
1596 * assign a PE
1597 */
cad5cef6 1598static int pnv_pci_enable_device_hook(struct pci_dev *dev)
184cd4a3 1599{
db1266c8
GS
1600 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1601 struct pnv_phb *phb = hose->private_data;
1602 struct pci_dn *pdn;
184cd4a3 1603
db1266c8
GS
1604 /* The function is probably called while the PEs have
1605 * not be created yet. For example, resource reassignment
1606 * during PCI probe period. We just skip the check if
1607 * PEs isn't ready.
1608 */
1609 if (!phb->initialized)
1610 return 0;
1611
b72c1f65 1612 pdn = pci_get_pdn(dev);
184cd4a3
BH
1613 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1614 return -EINVAL;
db1266c8 1615
184cd4a3
BH
1616 return 0;
1617}
1618
1619static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1620 u32 devfn)
1621{
1622 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1623}
1624
73ed148a
BH
1625static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
1626{
1627 opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET,
1628 OPAL_ASSERT_RESET);
1629}
1630
e51df2c1
AB
1631static void __init pnv_pci_init_ioda_phb(struct device_node *np,
1632 u64 hub_id, int ioda_type)
184cd4a3
BH
1633{
1634 struct pci_controller *hose;
184cd4a3 1635 struct pnv_phb *phb;
8184616f 1636 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
c681b93c 1637 const __be64 *prop64;
3a1a4661 1638 const __be32 *prop32;
f1b7cc3e 1639 int len;
184cd4a3
BH
1640 u64 phb_id;
1641 void *aux;
1642 long rc;
1643
58d714ec 1644 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
184cd4a3
BH
1645
1646 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1647 if (!prop64) {
1648 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
1649 return;
1650 }
1651 phb_id = be64_to_cpup(prop64);
1652 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
1653
1654 phb = alloc_bootmem(sizeof(struct pnv_phb));
58d714ec
GS
1655 if (!phb) {
1656 pr_err(" Out of memory !\n");
1657 return;
184cd4a3 1658 }
58d714ec
GS
1659
1660 /* Allocate PCI controller */
1661 memset(phb, 0, sizeof(struct pnv_phb));
1662 phb->hose = hose = pcibios_alloc_controller(np);
1663 if (!phb->hose) {
1664 pr_err(" Can't allocate PCI controller for %s\n",
184cd4a3 1665 np->full_name);
58d714ec 1666 free_bootmem((unsigned long)phb, sizeof(struct pnv_phb));
184cd4a3
BH
1667 return;
1668 }
1669
1670 spin_lock_init(&phb->lock);
f1b7cc3e
GS
1671 prop32 = of_get_property(np, "bus-range", &len);
1672 if (prop32 && len == 8) {
3a1a4661
BH
1673 hose->first_busno = be32_to_cpu(prop32[0]);
1674 hose->last_busno = be32_to_cpu(prop32[1]);
f1b7cc3e
GS
1675 } else {
1676 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
1677 hose->first_busno = 0;
1678 hose->last_busno = 0xff;
1679 }
184cd4a3 1680 hose->private_data = phb;
e9cc17d4 1681 phb->hub_id = hub_id;
184cd4a3 1682 phb->opal_id = phb_id;
aa0c033f 1683 phb->type = ioda_type;
184cd4a3 1684
cee72d5b
BH
1685 /* Detect specific models for error handling */
1686 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
1687 phb->model = PNV_PHB_MODEL_P7IOC;
f3d40c25 1688 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
aa0c033f 1689 phb->model = PNV_PHB_MODEL_PHB3;
cee72d5b
BH
1690 else
1691 phb->model = PNV_PHB_MODEL_UNKNOWN;
1692
aa0c033f 1693 /* Parse 32-bit and IO ranges (if any) */
2f1ec02e 1694 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
184cd4a3 1695
aa0c033f 1696 /* Get registers */
184cd4a3
BH
1697 phb->regs = of_iomap(np, 0);
1698 if (phb->regs == NULL)
1699 pr_err(" Failed to map registers !\n");
1700
184cd4a3 1701 /* Initialize more IODA stuff */
36954dc7 1702 phb->ioda.total_pe = 1;
aa0c033f 1703 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
36954dc7 1704 if (prop32)
3a1a4661 1705 phb->ioda.total_pe = be32_to_cpup(prop32);
36954dc7
GS
1706 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
1707 if (prop32)
1708 phb->ioda.reserved_pe = be32_to_cpup(prop32);
262af557
GC
1709
1710 /* Parse 64-bit MMIO range */
1711 pnv_ioda_parse_m64_window(phb);
1712
184cd4a3 1713 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
aa0c033f 1714 /* FW Has already off top 64k of M32 space (MSI space) */
184cd4a3
BH
1715 phb->ioda.m32_size += 0x10000;
1716
1717 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
3fd47f06 1718 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
184cd4a3
BH
1719 phb->ioda.io_size = hose->pci_io_size;
1720 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
1721 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
1722
c35d2a8c 1723 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
184cd4a3
BH
1724 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
1725 m32map_off = size;
e47747f4 1726 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
c35d2a8c
GS
1727 if (phb->type == PNV_PHB_IODA1) {
1728 iomap_off = size;
1729 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
1730 }
184cd4a3
BH
1731 pemap_off = size;
1732 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
1733 aux = alloc_bootmem(size);
1734 memset(aux, 0, size);
1735 phb->ioda.pe_alloc = aux;
1736 phb->ioda.m32_segmap = aux + m32map_off;
c35d2a8c
GS
1737 if (phb->type == PNV_PHB_IODA1)
1738 phb->ioda.io_segmap = aux + iomap_off;
184cd4a3 1739 phb->ioda.pe_array = aux + pemap_off;
36954dc7 1740 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
184cd4a3 1741
7ebdf956 1742 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
184cd4a3
BH
1743 INIT_LIST_HEAD(&phb->ioda.pe_list);
1744
1745 /* Calculate how many 32-bit TCE segments we have */
1746 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
1747
aa0c033f 1748#if 0 /* We should really do that ... */
184cd4a3
BH
1749 rc = opal_pci_set_phb_mem_window(opal->phb_id,
1750 window_type,
1751 window_num,
1752 starting_real_address,
1753 starting_pci_address,
1754 segment_size);
1755#endif
1756
262af557
GC
1757 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
1758 phb->ioda.total_pe, phb->ioda.reserved_pe,
1759 phb->ioda.m32_size, phb->ioda.m32_segsize);
1760 if (phb->ioda.m64_size)
1761 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
1762 phb->ioda.m64_size, phb->ioda.m64_segsize);
1763 if (phb->ioda.io_size)
1764 pr_info(" IO: 0x%x [segment=0x%x]\n",
1765 phb->ioda.io_size, phb->ioda.io_segsize);
1766
184cd4a3 1767
184cd4a3 1768 phb->hose->ops = &pnv_pci_ops;
49dec922
GS
1769 phb->get_pe_state = pnv_ioda_get_pe_state;
1770 phb->freeze_pe = pnv_ioda_freeze_pe;
1771 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
e9cc17d4
GS
1772#ifdef CONFIG_EEH
1773 phb->eeh_ops = &ioda_eeh_ops;
1774#endif
184cd4a3
BH
1775
1776 /* Setup RID -> PE mapping function */
1777 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
1778
1779 /* Setup TCEs */
1780 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
cd15b048 1781 phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
184cd4a3 1782
73ed148a
BH
1783 /* Setup shutdown function for kexec */
1784 phb->shutdown = pnv_pci_ioda_shutdown;
1785
184cd4a3
BH
1786 /* Setup MSI support */
1787 pnv_pci_init_ioda_msis(phb);
1788
c40a4210
GS
1789 /*
1790 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
1791 * to let the PCI core do resource assignment. It's supposed
1792 * that the PCI core will do correct I/O and MMIO alignment
1793 * for the P2P bridge bars so that each PCI bus (excluding
1794 * the child P2P bridges) can form individual PE.
184cd4a3 1795 */
fb446ad0 1796 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
184cd4a3 1797 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
271fd03a 1798 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
d92a208d 1799 ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
c40a4210 1800 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
184cd4a3
BH
1801
1802 /* Reset IODA tables to a clean state */
f11fe552 1803 rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
184cd4a3 1804 if (rc)
f11fe552 1805 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
361f2a2a
GS
1806
1807 /* If we're running in kdump kerenl, the previous kerenl never
1808 * shutdown PCI devices correctly. We already got IODA table
1809 * cleaned out. So we have to issue PHB reset to stop all PCI
1810 * transactions from previous kerenl.
1811 */
1812 if (is_kdump_kernel()) {
1813 pr_info(" Issue PHB reset ...\n");
1814 ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
1815 ioda_eeh_phb_reset(hose, OPAL_DEASSERT_RESET);
1816 }
262af557
GC
1817
1818 /* Configure M64 window */
1819 if (phb->init_m64 && phb->init_m64(phb))
1820 hose->mem_resources[1].flags = 0;
aa0c033f
GS
1821}
1822
67975005 1823void __init pnv_pci_init_ioda2_phb(struct device_node *np)
aa0c033f 1824{
e9cc17d4 1825 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
184cd4a3
BH
1826}
1827
1828void __init pnv_pci_init_ioda_hub(struct device_node *np)
1829{
1830 struct device_node *phbn;
c681b93c 1831 const __be64 *prop64;
184cd4a3
BH
1832 u64 hub_id;
1833
1834 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
1835
1836 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
1837 if (!prop64) {
1838 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
1839 return;
1840 }
1841 hub_id = be64_to_cpup(prop64);
1842 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
1843
1844 /* Count child PHBs */
1845 for_each_child_of_node(np, phbn) {
1846 /* Look for IODA1 PHBs */
1847 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
e9cc17d4 1848 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
184cd4a3
BH
1849 }
1850}