blob: 3f88c51cb95b5c518e6b03da66647b03232309ee [file] [log] [blame]
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001/*
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
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +000012#undef DEBUG
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000013
14#include <linux/kernel.h>
15#include <linux/pci.h>
16#include <linux/delay.h>
17#include <linux/string.h>
18#include <linux/init.h>
19#include <linux/bootmem.h>
20#include <linux/irq.h>
21#include <linux/io.h>
22#include <linux/msi.h>
23
24#include <asm/sections.h>
25#include <asm/io.h>
26#include <asm/prom.h>
27#include <asm/pci-bridge.h>
28#include <asm/machdep.h>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000029#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000030#include <asm/ppc-pci.h>
31#include <asm/opal.h>
32#include <asm/iommu.h>
33#include <asm/tce.h>
Gavin Shan137436c2013-04-25 19:20:59 +000034#include <asm/xics.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000035
36#include "powernv.h"
37#include "pci.h"
38
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000039#define define_pe_printk_level(func, kern_level) \
40static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
41{ \
42 struct va_format vaf; \
43 va_list args; \
Gavin Shan490e0782012-10-17 19:53:30 +000044 char pfix[32]; \
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000045 int r; \
46 \
47 va_start(args, fmt); \
48 \
49 vaf.fmt = fmt; \
50 vaf.va = &args; \
51 \
Gavin Shan490e0782012-10-17 19:53:30 +000052 if (pe->pdev) \
53 strlcpy(pfix, dev_name(&pe->pdev->dev), \
54 sizeof(pfix)); \
55 else \
56 sprintf(pfix, "%04x:%02x ", \
57 pci_domain_nr(pe->pbus), \
58 pe->pbus->number); \
59 r = printk(kern_level "pci %s: [PE# %.3d] %pV", \
60 pfix, pe->pe_number, &vaf); \
61 \
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000062 va_end(args); \
63 \
64 return r; \
65} \
66
67define_pe_printk_level(pe_err, KERN_ERR);
68define_pe_printk_level(pe_warn, KERN_WARNING);
69define_pe_printk_level(pe_info, KERN_INFO);
70
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000071static struct pci_dn *pnv_ioda_get_pdn(struct pci_dev *dev)
72{
73 struct device_node *np;
74
75 np = pci_device_to_OF_node(dev);
76 if (!np)
77 return NULL;
78 return PCI_DN(np);
79}
80
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -080081static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000082{
83 unsigned long pe;
84
85 do {
86 pe = find_next_zero_bit(phb->ioda.pe_alloc,
87 phb->ioda.total_pe, 0);
88 if (pe >= phb->ioda.total_pe)
89 return IODA_INVALID_PE;
90 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
91
92 phb->ioda.pe_array[pe].pe_number = pe;
93 return pe;
94}
95
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -080096static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000097{
98 WARN_ON(phb->ioda.pe_array[pe].pdev);
99
100 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
101 clear_bit(pe, phb->ioda.pe_alloc);
102}
103
104/* Currently those 2 are only used when MSIs are enabled, this will change
105 * but in the meantime, we need to protect them to avoid warnings
106 */
107#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800108static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000109{
110 struct pci_controller *hose = pci_bus_to_host(dev->bus);
111 struct pnv_phb *phb = hose->private_data;
112 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
113
114 if (!pdn)
115 return NULL;
116 if (pdn->pe_number == IODA_INVALID_PE)
117 return NULL;
118 return &phb->ioda.pe_array[pdn->pe_number];
119}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000120#endif /* CONFIG_PCI_MSI */
121
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800122static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000123{
124 struct pci_dev *parent;
125 uint8_t bcomp, dcomp, fcomp;
126 long rc, rid_end, rid;
127
128 /* Bus validation ? */
129 if (pe->pbus) {
130 int count;
131
132 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
133 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
134 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000135 if (pe->flags & PNV_IODA_PE_BUS_ALL)
136 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
137 else
138 count = 1;
139
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000140 switch(count) {
141 case 1: bcomp = OpalPciBusAll; break;
142 case 2: bcomp = OpalPciBus7Bits; break;
143 case 4: bcomp = OpalPciBus6Bits; break;
144 case 8: bcomp = OpalPciBus5Bits; break;
145 case 16: bcomp = OpalPciBus4Bits; break;
146 case 32: bcomp = OpalPciBus3Bits; break;
147 default:
148 pr_err("%s: Number of subordinate busses %d"
149 " unsupported\n",
150 pci_name(pe->pbus->self), count);
151 /* Do an exact match only */
152 bcomp = OpalPciBusAll;
153 }
154 rid_end = pe->rid + (count << 8);
155 } else {
156 parent = pe->pdev->bus->self;
157 bcomp = OpalPciBusAll;
158 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
159 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
160 rid_end = pe->rid + 1;
161 }
162
163 /* Associate PE in PELT */
164 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
165 bcomp, dcomp, fcomp, OPAL_MAP_PE);
166 if (rc) {
167 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
168 return -ENXIO;
169 }
170 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
171 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
172
173 /* Add to all parents PELT-V */
174 while (parent) {
175 struct pci_dn *pdn = pnv_ioda_get_pdn(parent);
176 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
177 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000178 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000179 /* XXX What to do in case of error ? */
180 }
181 parent = parent->bus->self;
182 }
183 /* Setup reverse map */
184 for (rid = pe->rid; rid < rid_end; rid++)
185 phb->ioda.pe_rmap[rid] = pe->pe_number;
186
187 /* Setup one MVTs on IODA1 */
188 if (phb->type == PNV_PHB_IODA1) {
189 pe->mve_number = pe->pe_number;
190 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
191 pe->pe_number);
192 if (rc) {
193 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
194 rc, pe->mve_number);
195 pe->mve_number = -1;
196 } else {
197 rc = opal_pci_set_mve_enable(phb->opal_id,
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000198 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000199 if (rc) {
200 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
201 rc, pe->mve_number);
202 pe->mve_number = -1;
203 }
204 }
205 } else if (phb->type == PNV_PHB_IODA2)
206 pe->mve_number = 0;
207
208 return 0;
209}
210
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800211static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
212 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000213{
214 struct pnv_ioda_pe *lpe;
215
Gavin Shan7ebdf952012-08-20 03:49:15 +0000216 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000217 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000218 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000219 return;
220 }
221 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000222 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000223}
224
225static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
226{
227 /* This is quite simplistic. The "base" weight of a device
228 * is 10. 0 means no DMA is to be accounted for it.
229 */
230
231 /* If it's a bridge, no DMA */
232 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
233 return 0;
234
235 /* Reduce the weight of slow USB controllers */
236 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
237 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
238 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
239 return 3;
240
241 /* Increase the weight of RAID (includes Obsidian) */
242 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
243 return 15;
244
245 /* Default */
246 return 10;
247}
248
Gavin Shanfb446ad2012-08-20 03:49:14 +0000249#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800250static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000251{
252 struct pci_controller *hose = pci_bus_to_host(dev->bus);
253 struct pnv_phb *phb = hose->private_data;
254 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
255 struct pnv_ioda_pe *pe;
256 int pe_num;
257
258 if (!pdn) {
259 pr_err("%s: Device tree node not associated properly\n",
260 pci_name(dev));
261 return NULL;
262 }
263 if (pdn->pe_number != IODA_INVALID_PE)
264 return NULL;
265
266 /* PE#0 has been pre-set */
267 if (dev->bus->number == 0)
268 pe_num = 0;
269 else
270 pe_num = pnv_ioda_alloc_pe(phb);
271 if (pe_num == IODA_INVALID_PE) {
272 pr_warning("%s: Not enough PE# available, disabling device\n",
273 pci_name(dev));
274 return NULL;
275 }
276
277 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
278 * pointer in the PE data structure, both should be destroyed at the
279 * same time. However, this needs to be looked at more closely again
280 * once we actually start removing things (Hotplug, SR-IOV, ...)
281 *
282 * At some point we want to remove the PDN completely anyways
283 */
284 pe = &phb->ioda.pe_array[pe_num];
285 pci_dev_get(dev);
286 pdn->pcidev = dev;
287 pdn->pe_number = pe_num;
288 pe->pdev = dev;
289 pe->pbus = NULL;
290 pe->tce32_seg = -1;
291 pe->mve_number = -1;
292 pe->rid = dev->bus->number << 8 | pdn->devfn;
293
294 pe_info(pe, "Associated device to PE\n");
295
296 if (pnv_ioda_configure_pe(phb, pe)) {
297 /* XXX What do we do here ? */
298 if (pe_num)
299 pnv_ioda_free_pe(phb, pe_num);
300 pdn->pe_number = IODA_INVALID_PE;
301 pe->pdev = NULL;
302 pci_dev_put(dev);
303 return NULL;
304 }
305
306 /* Assign a DMA weight to the device */
307 pe->dma_weight = pnv_ioda_dma_weight(dev);
308 if (pe->dma_weight != 0) {
309 phb->ioda.dma_weight += pe->dma_weight;
310 phb->ioda.dma_pe_count++;
311 }
312
313 /* Link the PE */
314 pnv_ioda_link_pe_by_weight(phb, pe);
315
316 return pe;
317}
Gavin Shanfb446ad2012-08-20 03:49:14 +0000318#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000319
320static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
321{
322 struct pci_dev *dev;
323
324 list_for_each_entry(dev, &bus->devices, bus_list) {
325 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
326
327 if (pdn == NULL) {
328 pr_warn("%s: No device node associated with device !\n",
329 pci_name(dev));
330 continue;
331 }
332 pci_dev_get(dev);
333 pdn->pcidev = dev;
334 pdn->pe_number = pe->pe_number;
335 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +0000336 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000337 pnv_ioda_setup_same_PE(dev->subordinate, pe);
338 }
339}
340
Gavin Shanfb446ad2012-08-20 03:49:14 +0000341/*
342 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
343 * single PCI bus. Another one that contains the primary PCI bus and its
344 * subordinate PCI devices and buses. The second type of PE is normally
345 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
346 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800347static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000348{
Gavin Shanfb446ad2012-08-20 03:49:14 +0000349 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000350 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000351 struct pnv_ioda_pe *pe;
352 int pe_num;
353
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000354 pe_num = pnv_ioda_alloc_pe(phb);
355 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000356 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
357 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000358 return;
359 }
360
361 pe = &phb->ioda.pe_array[pe_num];
Gavin Shanfb446ad2012-08-20 03:49:14 +0000362 pe->flags = (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000363 pe->pbus = bus;
364 pe->pdev = NULL;
365 pe->tce32_seg = -1;
366 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -0700367 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000368 pe->dma_weight = 0;
369
Gavin Shanfb446ad2012-08-20 03:49:14 +0000370 if (all)
371 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
372 bus->busn_res.start, bus->busn_res.end, pe_num);
373 else
374 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
375 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000376
377 if (pnv_ioda_configure_pe(phb, pe)) {
378 /* XXX What do we do here ? */
379 if (pe_num)
380 pnv_ioda_free_pe(phb, pe_num);
381 pe->pbus = NULL;
382 return;
383 }
384
385 /* Associate it with all child devices */
386 pnv_ioda_setup_same_PE(bus, pe);
387
Gavin Shan7ebdf952012-08-20 03:49:15 +0000388 /* Put PE to the list */
389 list_add_tail(&pe->list, &phb->ioda.pe_list);
390
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000391 /* Account for one DMA PE if at least one DMA capable device exist
392 * below the bridge
393 */
394 if (pe->dma_weight != 0) {
395 phb->ioda.dma_weight += pe->dma_weight;
396 phb->ioda.dma_pe_count++;
397 }
398
399 /* Link the PE */
400 pnv_ioda_link_pe_by_weight(phb, pe);
401}
402
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800403static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000404{
405 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000406
407 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000408
409 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000410 if (dev->subordinate) {
411 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
412 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
413 else
414 pnv_ioda_setup_PEs(dev->subordinate);
415 }
416 }
417}
418
419/*
420 * Configure PEs so that the downstream PCI buses and devices
421 * could have their associated PE#. Unfortunately, we didn't
422 * figure out the way to identify the PLX bridge yet. So we
423 * simply put the PCI bus and the subordinate behind the root
424 * port to PE# here. The game rule here is expected to be changed
425 * as soon as we can detected PLX bridge correctly.
426 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800427static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +0000428{
429 struct pci_controller *hose, *tmp;
430
431 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
432 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000433 }
434}
435
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800436static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000437{
438 /* We delay DMA setup after we have assigned all PE# */
439}
440
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800441static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000442{
443 struct pci_dev *dev;
444
445 list_for_each_entry(dev, &bus->devices, bus_list) {
446 set_iommu_table_base(&dev->dev, &pe->tce32_table);
447 if (dev->subordinate)
448 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
449 }
450}
451
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800452static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
453 struct pnv_ioda_pe *pe, unsigned int base,
454 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000455{
456
457 struct page *tce_mem = NULL;
458 const __be64 *swinvp;
459 struct iommu_table *tbl;
460 unsigned int i;
461 int64_t rc;
462 void *addr;
463
464 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
465#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
466
467 /* XXX FIXME: Handle 64-bit only DMA devices */
468 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
469 /* XXX FIXME: Allocate multi-level tables on PHB3 */
470
471 /* We shouldn't already have a 32-bit DMA associated */
472 if (WARN_ON(pe->tce32_seg >= 0))
473 return;
474
475 /* Grab a 32-bit TCE table */
476 pe->tce32_seg = base;
477 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
478 (base << 28), ((base + segs) << 28) - 1);
479
480 /* XXX Currently, we allocate one big contiguous table for the
481 * TCEs. We only really need one chunk per 256M of TCE space
482 * (ie per segment) but that's an optimization for later, it
483 * requires some added smarts with our get/put_tce implementation
484 */
485 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
486 get_order(TCE32_TABLE_SIZE * segs));
487 if (!tce_mem) {
488 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
489 goto fail;
490 }
491 addr = page_address(tce_mem);
492 memset(addr, 0, TCE32_TABLE_SIZE * segs);
493
494 /* Configure HW */
495 for (i = 0; i < segs; i++) {
496 rc = opal_pci_map_pe_dma_window(phb->opal_id,
497 pe->pe_number,
498 base + i, 1,
499 __pa(addr) + TCE32_TABLE_SIZE * i,
500 TCE32_TABLE_SIZE, 0x1000);
501 if (rc) {
502 pe_err(pe, " Failed to configure 32-bit TCE table,"
503 " err %ld\n", rc);
504 goto fail;
505 }
506 }
507
508 /* Setup linux iommu table */
509 tbl = &pe->tce32_table;
510 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
511 base << 28);
512
513 /* OPAL variant of P7IOC SW invalidated TCEs */
514 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
515 if (swinvp) {
516 /* We need a couple more fields -- an address and a data
517 * to or. Since the bus is only printed out on table free
518 * errors, and on the first pass the data will be a relative
519 * bus number, print that out instead.
520 */
521 tbl->it_busno = 0;
522 tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
523 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE
524 | TCE_PCI_SWINV_PAIR;
525 }
526 iommu_init_table(tbl, phb->hose->node);
527
528 if (pe->pdev)
529 set_iommu_table_base(&pe->pdev->dev, tbl);
530 else
531 pnv_ioda_setup_bus_dma(pe, pe->pbus);
532
533 return;
534 fail:
535 /* XXX Failure: Try to fallback to 64-bit only ? */
536 if (pe->tce32_seg >= 0)
537 pe->tce32_seg = -1;
538 if (tce_mem)
539 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
540}
541
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800542static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000543{
544 struct pci_controller *hose = phb->hose;
545 unsigned int residual, remaining, segs, tw, base;
546 struct pnv_ioda_pe *pe;
547
548 /* If we have more PE# than segments available, hand out one
549 * per PE until we run out and let the rest fail. If not,
550 * then we assign at least one segment per PE, plus more based
551 * on the amount of devices under that PE
552 */
553 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
554 residual = 0;
555 else
556 residual = phb->ioda.tce32_count -
557 phb->ioda.dma_pe_count;
558
559 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
560 hose->global_number, phb->ioda.tce32_count);
561 pr_info("PCI: %d PE# for a total weight of %d\n",
562 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
563
564 /* Walk our PE list and configure their DMA segments, hand them
565 * out one base segment plus any residual segments based on
566 * weight
567 */
568 remaining = phb->ioda.tce32_count;
569 tw = phb->ioda.dma_weight;
570 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +0000571 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000572 if (!pe->dma_weight)
573 continue;
574 if (!remaining) {
575 pe_warn(pe, "No DMA32 resources available\n");
576 continue;
577 }
578 segs = 1;
579 if (residual) {
580 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
581 if (segs > remaining)
582 segs = remaining;
583 }
584 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
585 pe->dma_weight, segs);
586 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
587 remaining -= segs;
588 base += segs;
589 }
590}
591
592#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +0000593static void pnv_ioda2_msi_eoi(struct irq_data *d)
594{
595 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
596 struct irq_chip *chip = irq_data_get_irq_chip(d);
597 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
598 ioda.irq_chip);
599 int64_t rc;
600
601 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
602 WARN_ON_ONCE(rc);
603
604 icp_native_eoi(d);
605}
606
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000607static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +0000608 unsigned int hwirq, unsigned int virq,
609 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000610{
611 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
Gavin Shan137436c2013-04-25 19:20:59 +0000612 struct irq_data *idata;
613 struct irq_chip *ichip;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000614 unsigned int xive_num = hwirq - phb->msi_base;
615 uint64_t addr64;
616 uint32_t addr32, data;
617 int rc;
618
619 /* No PE assigned ? bail out ... no MSI for you ! */
620 if (pe == NULL)
621 return -ENXIO;
622
623 /* Check if we have an MVE */
624 if (pe->mve_number < 0)
625 return -ENXIO;
626
627 /* Assign XIVE to PE */
628 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
629 if (rc) {
630 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
631 pci_name(dev), rc, xive_num);
632 return -EIO;
633 }
634
635 if (is_64) {
636 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
637 &addr64, &data);
638 if (rc) {
639 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
640 pci_name(dev), rc);
641 return -EIO;
642 }
643 msg->address_hi = addr64 >> 32;
644 msg->address_lo = addr64 & 0xfffffffful;
645 } else {
646 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
647 &addr32, &data);
648 if (rc) {
649 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
650 pci_name(dev), rc);
651 return -EIO;
652 }
653 msg->address_hi = 0;
654 msg->address_lo = addr32;
655 }
656 msg->data = data;
657
Gavin Shan137436c2013-04-25 19:20:59 +0000658 /*
659 * Change the IRQ chip for the MSI interrupts on PHB3.
660 * The corresponding IRQ chip should be populated for
661 * the first time.
662 */
663 if (phb->type == PNV_PHB_IODA2) {
664 if (!phb->ioda.irq_chip_init) {
665 idata = irq_get_irq_data(virq);
666 ichip = irq_data_get_irq_chip(idata);
667 phb->ioda.irq_chip_init = 1;
668 phb->ioda.irq_chip = *ichip;
669 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
670 }
671
672 irq_set_chip(virq, &phb->ioda.irq_chip);
673 }
674
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000675 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
676 " address=%x_%08x data=%x PE# %d\n",
677 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
678 msg->address_hi, msg->address_lo, data, pe->pe_number);
679
680 return 0;
681}
682
683static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
684{
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000685 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000686 const __be32 *prop = of_get_property(phb->hose->dn,
687 "ibm,opal-msi-ranges", NULL);
688 if (!prop) {
689 /* BML Fallback */
690 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
691 }
692 if (!prop)
693 return;
694
695 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000696 count = be32_to_cpup(prop + 1);
697 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000698 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
699 phb->hose->global_number);
700 return;
701 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000702
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000703 phb->msi_setup = pnv_pci_ioda_msi_setup;
704 phb->msi32_support = 1;
705 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000706 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000707}
708#else
709static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
710#endif /* CONFIG_PCI_MSI */
711
Gavin Shan11685be2012-08-20 03:49:16 +0000712/*
713 * This function is supposed to be called on basis of PE from top
714 * to bottom style. So the the I/O or MMIO segment assigned to
715 * parent PE could be overrided by its child PEs if necessary.
716 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800717static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
718 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +0000719{
720 struct pnv_phb *phb = hose->private_data;
721 struct pci_bus_region region;
722 struct resource *res;
723 int i, index;
724 int rc;
725
726 /*
727 * NOTE: We only care PCI bus based PE for now. For PCI
728 * device based PE, for example SRIOV sensitive VF should
729 * be figured out later.
730 */
731 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
732
733 pci_bus_for_each_resource(pe->pbus, res, i) {
734 if (!res || !res->flags ||
735 res->start > res->end)
736 continue;
737
738 if (res->flags & IORESOURCE_IO) {
739 region.start = res->start - phb->ioda.io_pci_base;
740 region.end = res->end - phb->ioda.io_pci_base;
741 index = region.start / phb->ioda.io_segsize;
742
743 while (index < phb->ioda.total_pe &&
744 region.start <= region.end) {
745 phb->ioda.io_segmap[index] = pe->pe_number;
746 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
747 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
748 if (rc != OPAL_SUCCESS) {
749 pr_err("%s: OPAL error %d when mapping IO "
750 "segment #%d to PE#%d\n",
751 __func__, rc, index, pe->pe_number);
752 break;
753 }
754
755 region.start += phb->ioda.io_segsize;
756 index++;
757 }
758 } else if (res->flags & IORESOURCE_MEM) {
759 region.start = res->start -
760 hose->pci_mem_offset -
761 phb->ioda.m32_pci_base;
762 region.end = res->end -
763 hose->pci_mem_offset -
764 phb->ioda.m32_pci_base;
765 index = region.start / phb->ioda.m32_segsize;
766
767 while (index < phb->ioda.total_pe &&
768 region.start <= region.end) {
769 phb->ioda.m32_segmap[index] = pe->pe_number;
770 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
771 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
772 if (rc != OPAL_SUCCESS) {
773 pr_err("%s: OPAL error %d when mapping M32 "
774 "segment#%d to PE#%d",
775 __func__, rc, index, pe->pe_number);
776 break;
777 }
778
779 region.start += phb->ioda.m32_segsize;
780 index++;
781 }
782 }
783 }
784}
785
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800786static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +0000787{
788 struct pci_controller *tmp, *hose;
789 struct pnv_phb *phb;
790 struct pnv_ioda_pe *pe;
791
792 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
793 phb = hose->private_data;
794 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
795 pnv_ioda_setup_pe_seg(hose, pe);
796 }
797 }
798}
799
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800800static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +0000801{
802 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +0000803 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +0000804
805 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
806 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +0000807
808 /* Mark the PHB initialization done */
809 phb = hose->private_data;
810 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +0000811 }
812}
813
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800814static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +0000815{
816 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +0000817 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +0000818 pnv_pci_ioda_setup_DMA();
Gavin Shanfb446ad2012-08-20 03:49:14 +0000819}
820
Gavin Shan271fd032012-09-11 16:59:47 -0600821/*
822 * Returns the alignment for I/O or memory windows for P2P
823 * bridges. That actually depends on how PEs are segmented.
824 * For now, we return I/O or M32 segment size for PE sensitive
825 * P2P bridges. Otherwise, the default values (4KiB for I/O,
826 * 1MiB for memory) will be returned.
827 *
828 * The current PCI bus might be put into one PE, which was
829 * create against the parent PCI bridge. For that case, we
830 * needn't enlarge the alignment so that we can save some
831 * resources.
832 */
833static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
834 unsigned long type)
835{
836 struct pci_dev *bridge;
837 struct pci_controller *hose = pci_bus_to_host(bus);
838 struct pnv_phb *phb = hose->private_data;
839 int num_pci_bridges = 0;
840
841 bridge = bus->self;
842 while (bridge) {
843 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
844 num_pci_bridges++;
845 if (num_pci_bridges >= 2)
846 return 1;
847 }
848
849 bridge = bridge->bus->self;
850 }
851
852 /* We need support prefetchable memory window later */
853 if (type & IORESOURCE_MEM)
854 return phb->ioda.m32_segsize;
855
856 return phb->ioda.io_segsize;
857}
858
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000859/* Prevent enabling devices for which we couldn't properly
860 * assign a PE
861 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800862static int pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000863{
Gavin Shandb1266c2012-08-20 03:49:18 +0000864 struct pci_controller *hose = pci_bus_to_host(dev->bus);
865 struct pnv_phb *phb = hose->private_data;
866 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000867
Gavin Shandb1266c2012-08-20 03:49:18 +0000868 /* The function is probably called while the PEs have
869 * not be created yet. For example, resource reassignment
870 * during PCI probe period. We just skip the check if
871 * PEs isn't ready.
872 */
873 if (!phb->initialized)
874 return 0;
875
876 pdn = pnv_ioda_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000877 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
878 return -EINVAL;
Gavin Shandb1266c2012-08-20 03:49:18 +0000879
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000880 return 0;
881}
882
883static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
884 u32 devfn)
885{
886 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
887}
888
Gavin Shanaa0c0332013-04-25 19:20:57 +0000889void __init pnv_pci_init_ioda_phb(struct device_node *np, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000890{
891 struct pci_controller *hose;
892 static int primary = 1;
893 struct pnv_phb *phb;
894 unsigned long size, m32map_off, iomap_off, pemap_off;
895 const u64 *prop64;
Gavin Shanaa0c0332013-04-25 19:20:57 +0000896 const u32 *prop32;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000897 u64 phb_id;
898 void *aux;
899 long rc;
900
Gavin Shanaa0c0332013-04-25 19:20:57 +0000901 pr_info(" Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000902
903 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
904 if (!prop64) {
905 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
906 return;
907 }
908 phb_id = be64_to_cpup(prop64);
909 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
910
911 phb = alloc_bootmem(sizeof(struct pnv_phb));
912 if (phb) {
913 memset(phb, 0, sizeof(struct pnv_phb));
914 phb->hose = hose = pcibios_alloc_controller(np);
915 }
916 if (!phb || !phb->hose) {
917 pr_err("PCI: Failed to allocate PCI controller for %s\n",
918 np->full_name);
919 return;
920 }
921
922 spin_lock_init(&phb->lock);
923 /* XXX Use device-tree */
924 hose->first_busno = 0;
925 hose->last_busno = 0xff;
926 hose->private_data = phb;
927 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +0000928 phb->type = ioda_type;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000929
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000930 /* Detect specific models for error handling */
931 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
932 phb->model = PNV_PHB_MODEL_P7IOC;
Gavin Shanaa0c0332013-04-25 19:20:57 +0000933 else if (of_device_is_compatible(np, "ibm,p8-pciex"))
934 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000935 else
936 phb->model = PNV_PHB_MODEL_UNKNOWN;
937
Gavin Shanaa0c0332013-04-25 19:20:57 +0000938 /* Parse 32-bit and IO ranges (if any) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000939 pci_process_bridge_OF_ranges(phb->hose, np, primary);
940 primary = 0;
941
Gavin Shanaa0c0332013-04-25 19:20:57 +0000942 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000943 phb->regs = of_iomap(np, 0);
944 if (phb->regs == NULL)
945 pr_err(" Failed to map registers !\n");
946
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000947 /* Initialize more IODA stuff */
Gavin Shanaa0c0332013-04-25 19:20:57 +0000948 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
949 if (!prop32)
950 phb->ioda.total_pe = 1;
951 else
952 phb->ioda.total_pe = *prop32;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000953
954 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +0000955 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000956 phb->ioda.m32_size += 0x10000;
957
958 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
959 phb->ioda.m32_pci_base = hose->mem_resources[0].start -
960 hose->pci_mem_offset;
961 phb->ioda.io_size = hose->pci_io_size;
962 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
963 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
964
Gavin Shanaa0c0332013-04-25 19:20:57 +0000965 /* Allocate aux data & arrays
966 *
967 * XXX TODO: Don't allocate io segmap on PHB3
968 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000969 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
970 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +0000971 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000972 iomap_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +0000973 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000974 pemap_off = size;
975 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
976 aux = alloc_bootmem(size);
977 memset(aux, 0, size);
978 phb->ioda.pe_alloc = aux;
979 phb->ioda.m32_segmap = aux + m32map_off;
980 phb->ioda.io_segmap = aux + iomap_off;
981 phb->ioda.pe_array = aux + pemap_off;
982 set_bit(0, phb->ioda.pe_alloc);
983
Gavin Shan7ebdf952012-08-20 03:49:15 +0000984 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000985 INIT_LIST_HEAD(&phb->ioda.pe_list);
986
987 /* Calculate how many 32-bit TCE segments we have */
988 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
989
990 /* Clear unusable m64 */
991 hose->mem_resources[1].flags = 0;
992 hose->mem_resources[1].start = 0;
993 hose->mem_resources[1].end = 0;
994 hose->mem_resources[2].flags = 0;
995 hose->mem_resources[2].start = 0;
996 hose->mem_resources[2].end = 0;
997
Gavin Shanaa0c0332013-04-25 19:20:57 +0000998#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000999 rc = opal_pci_set_phb_mem_window(opal->phb_id,
1000 window_type,
1001 window_num,
1002 starting_real_address,
1003 starting_pci_address,
1004 segment_size);
1005#endif
1006
1007 pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
1008 phb->ioda.total_pe,
1009 phb->ioda.m32_size, phb->ioda.m32_segsize,
1010 phb->ioda.io_size, phb->ioda.io_segsize);
1011
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001012 phb->hose->ops = &pnv_pci_ops;
1013
1014 /* Setup RID -> PE mapping function */
1015 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
1016
1017 /* Setup TCEs */
1018 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
1019
1020 /* Setup MSI support */
1021 pnv_pci_init_ioda_msis(phb);
1022
Gavin Shanc40a4212012-08-20 03:49:20 +00001023 /*
1024 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
1025 * to let the PCI core do resource assignment. It's supposed
1026 * that the PCI core will do correct I/O and MMIO alignment
1027 * for the P2P bridge bars so that each PCI bus (excluding
1028 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001029 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00001030 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001031 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
Gavin Shan271fd032012-09-11 16:59:47 -06001032 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
Gavin Shanc40a4212012-08-20 03:49:20 +00001033 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001034
1035 /* Reset IODA tables to a clean state */
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00001036 rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001037 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00001038 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shanaa0c0332013-04-25 19:20:57 +00001039
1040 /*
1041 * On IODA1 map everything to PE#0, on IODA2 we assume the IODA reset
1042 * has cleared the RTT which has the same effect
1043 */
1044 if (ioda_type == PNV_PHB_IODA1)
1045 opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
1046}
1047
1048void pnv_pci_init_ioda2_phb(struct device_node *np)
1049{
1050 pnv_pci_init_ioda_phb(np, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001051}
1052
1053void __init pnv_pci_init_ioda_hub(struct device_node *np)
1054{
1055 struct device_node *phbn;
1056 const u64 *prop64;
1057 u64 hub_id;
1058
1059 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
1060
1061 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
1062 if (!prop64) {
1063 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
1064 return;
1065 }
1066 hub_id = be64_to_cpup(prop64);
1067 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
1068
1069 /* Count child PHBs */
1070 for_each_child_of_node(np, phbn) {
1071 /* Look for IODA1 PHBs */
1072 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00001073 pnv_pci_init_ioda_phb(phbn, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001074 }
1075}