blob: dc4ec795696714f07d043c7ed55ca6a4301bf6be [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>
Gavin Shan37c367f2013-06-20 18:13:25 +080016#include <linux/debugfs.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000017#include <linux/delay.h>
18#include <linux/string.h>
19#include <linux/init.h>
20#include <linux/bootmem.h>
21#include <linux/irq.h>
22#include <linux/io.h>
23#include <linux/msi.h>
24
25#include <asm/sections.h>
26#include <asm/io.h>
27#include <asm/prom.h>
28#include <asm/pci-bridge.h>
29#include <asm/machdep.h>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000030#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000031#include <asm/ppc-pci.h>
32#include <asm/opal.h>
33#include <asm/iommu.h>
34#include <asm/tce.h>
Gavin Shan137436c2013-04-25 19:20:59 +000035#include <asm/xics.h>
Gavin Shan37c367f2013-06-20 18:13:25 +080036#include <asm/debug.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000037
38#include "powernv.h"
39#include "pci.h"
40
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000041#define define_pe_printk_level(func, kern_level) \
42static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
43{ \
44 struct va_format vaf; \
45 va_list args; \
Gavin Shan490e0782012-10-17 19:53:30 +000046 char pfix[32]; \
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000047 int r; \
48 \
49 va_start(args, fmt); \
50 \
51 vaf.fmt = fmt; \
52 vaf.va = &args; \
53 \
Gavin Shan490e0782012-10-17 19:53:30 +000054 if (pe->pdev) \
55 strlcpy(pfix, dev_name(&pe->pdev->dev), \
56 sizeof(pfix)); \
57 else \
58 sprintf(pfix, "%04x:%02x ", \
59 pci_domain_nr(pe->pbus), \
60 pe->pbus->number); \
61 r = printk(kern_level "pci %s: [PE# %.3d] %pV", \
62 pfix, pe->pe_number, &vaf); \
63 \
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000064 va_end(args); \
65 \
66 return r; \
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
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -080073static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000074{
75 unsigned long pe;
76
77 do {
78 pe = find_next_zero_bit(phb->ioda.pe_alloc,
79 phb->ioda.total_pe, 0);
80 if (pe >= phb->ioda.total_pe)
81 return IODA_INVALID_PE;
82 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
83
Gavin Shan4cce9552013-04-25 19:21:00 +000084 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000085 phb->ioda.pe_array[pe].pe_number = pe;
86 return pe;
87}
88
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -080089static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000090{
91 WARN_ON(phb->ioda.pe_array[pe].pdev);
92
93 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
94 clear_bit(pe, phb->ioda.pe_alloc);
95}
96
97/* Currently those 2 are only used when MSIs are enabled, this will change
98 * but in the meantime, we need to protect them to avoid warnings
99 */
100#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800101static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000102{
103 struct pci_controller *hose = pci_bus_to_host(dev->bus);
104 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000105 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000106
107 if (!pdn)
108 return NULL;
109 if (pdn->pe_number == IODA_INVALID_PE)
110 return NULL;
111 return &phb->ioda.pe_array[pdn->pe_number];
112}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000113#endif /* CONFIG_PCI_MSI */
114
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800115static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000116{
117 struct pci_dev *parent;
118 uint8_t bcomp, dcomp, fcomp;
119 long rc, rid_end, rid;
120
121 /* Bus validation ? */
122 if (pe->pbus) {
123 int count;
124
125 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
126 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
127 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000128 if (pe->flags & PNV_IODA_PE_BUS_ALL)
129 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
130 else
131 count = 1;
132
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000133 switch(count) {
134 case 1: bcomp = OpalPciBusAll; break;
135 case 2: bcomp = OpalPciBus7Bits; break;
136 case 4: bcomp = OpalPciBus6Bits; break;
137 case 8: bcomp = OpalPciBus5Bits; break;
138 case 16: bcomp = OpalPciBus4Bits; break;
139 case 32: bcomp = OpalPciBus3Bits; break;
140 default:
141 pr_err("%s: Number of subordinate busses %d"
142 " unsupported\n",
143 pci_name(pe->pbus->self), count);
144 /* Do an exact match only */
145 bcomp = OpalPciBusAll;
146 }
147 rid_end = pe->rid + (count << 8);
148 } else {
149 parent = pe->pdev->bus->self;
150 bcomp = OpalPciBusAll;
151 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
152 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
153 rid_end = pe->rid + 1;
154 }
155
156 /* Associate PE in PELT */
157 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
158 bcomp, dcomp, fcomp, OPAL_MAP_PE);
159 if (rc) {
160 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
161 return -ENXIO;
162 }
163 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
164 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
165
166 /* Add to all parents PELT-V */
167 while (parent) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000168 struct pci_dn *pdn = pci_get_pdn(parent);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000169 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
170 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000171 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000172 /* XXX What to do in case of error ? */
173 }
174 parent = parent->bus->self;
175 }
176 /* Setup reverse map */
177 for (rid = pe->rid; rid < rid_end; rid++)
178 phb->ioda.pe_rmap[rid] = pe->pe_number;
179
180 /* Setup one MVTs on IODA1 */
181 if (phb->type == PNV_PHB_IODA1) {
182 pe->mve_number = pe->pe_number;
183 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
184 pe->pe_number);
185 if (rc) {
186 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
187 rc, pe->mve_number);
188 pe->mve_number = -1;
189 } else {
190 rc = opal_pci_set_mve_enable(phb->opal_id,
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000191 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000192 if (rc) {
193 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
194 rc, pe->mve_number);
195 pe->mve_number = -1;
196 }
197 }
198 } else if (phb->type == PNV_PHB_IODA2)
199 pe->mve_number = 0;
200
201 return 0;
202}
203
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800204static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
205 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000206{
207 struct pnv_ioda_pe *lpe;
208
Gavin Shan7ebdf952012-08-20 03:49:15 +0000209 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000210 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000211 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000212 return;
213 }
214 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000215 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000216}
217
218static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
219{
220 /* This is quite simplistic. The "base" weight of a device
221 * is 10. 0 means no DMA is to be accounted for it.
222 */
223
224 /* If it's a bridge, no DMA */
225 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
226 return 0;
227
228 /* Reduce the weight of slow USB controllers */
229 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
230 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
231 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
232 return 3;
233
234 /* Increase the weight of RAID (includes Obsidian) */
235 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
236 return 15;
237
238 /* Default */
239 return 10;
240}
241
Gavin Shanfb446ad2012-08-20 03:49:14 +0000242#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800243static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000244{
245 struct pci_controller *hose = pci_bus_to_host(dev->bus);
246 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000247 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000248 struct pnv_ioda_pe *pe;
249 int pe_num;
250
251 if (!pdn) {
252 pr_err("%s: Device tree node not associated properly\n",
253 pci_name(dev));
254 return NULL;
255 }
256 if (pdn->pe_number != IODA_INVALID_PE)
257 return NULL;
258
259 /* PE#0 has been pre-set */
260 if (dev->bus->number == 0)
261 pe_num = 0;
262 else
263 pe_num = pnv_ioda_alloc_pe(phb);
264 if (pe_num == IODA_INVALID_PE) {
265 pr_warning("%s: Not enough PE# available, disabling device\n",
266 pci_name(dev));
267 return NULL;
268 }
269
270 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
271 * pointer in the PE data structure, both should be destroyed at the
272 * same time. However, this needs to be looked at more closely again
273 * once we actually start removing things (Hotplug, SR-IOV, ...)
274 *
275 * At some point we want to remove the PDN completely anyways
276 */
277 pe = &phb->ioda.pe_array[pe_num];
278 pci_dev_get(dev);
279 pdn->pcidev = dev;
280 pdn->pe_number = pe_num;
281 pe->pdev = dev;
282 pe->pbus = NULL;
283 pe->tce32_seg = -1;
284 pe->mve_number = -1;
285 pe->rid = dev->bus->number << 8 | pdn->devfn;
286
287 pe_info(pe, "Associated device to PE\n");
288
289 if (pnv_ioda_configure_pe(phb, pe)) {
290 /* XXX What do we do here ? */
291 if (pe_num)
292 pnv_ioda_free_pe(phb, pe_num);
293 pdn->pe_number = IODA_INVALID_PE;
294 pe->pdev = NULL;
295 pci_dev_put(dev);
296 return NULL;
297 }
298
299 /* Assign a DMA weight to the device */
300 pe->dma_weight = pnv_ioda_dma_weight(dev);
301 if (pe->dma_weight != 0) {
302 phb->ioda.dma_weight += pe->dma_weight;
303 phb->ioda.dma_pe_count++;
304 }
305
306 /* Link the PE */
307 pnv_ioda_link_pe_by_weight(phb, pe);
308
309 return pe;
310}
Gavin Shanfb446ad2012-08-20 03:49:14 +0000311#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000312
313static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
314{
315 struct pci_dev *dev;
316
317 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000318 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000319
320 if (pdn == NULL) {
321 pr_warn("%s: No device node associated with device !\n",
322 pci_name(dev));
323 continue;
324 }
325 pci_dev_get(dev);
326 pdn->pcidev = dev;
327 pdn->pe_number = pe->pe_number;
328 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +0000329 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000330 pnv_ioda_setup_same_PE(dev->subordinate, pe);
331 }
332}
333
Gavin Shanfb446ad2012-08-20 03:49:14 +0000334/*
335 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
336 * single PCI bus. Another one that contains the primary PCI bus and its
337 * subordinate PCI devices and buses. The second type of PE is normally
338 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
339 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800340static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000341{
Gavin Shanfb446ad2012-08-20 03:49:14 +0000342 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000343 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000344 struct pnv_ioda_pe *pe;
345 int pe_num;
346
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000347 pe_num = pnv_ioda_alloc_pe(phb);
348 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000349 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
350 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000351 return;
352 }
353
354 pe = &phb->ioda.pe_array[pe_num];
Gavin Shanfb446ad2012-08-20 03:49:14 +0000355 pe->flags = (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000356 pe->pbus = bus;
357 pe->pdev = NULL;
358 pe->tce32_seg = -1;
359 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -0700360 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000361 pe->dma_weight = 0;
362
Gavin Shanfb446ad2012-08-20 03:49:14 +0000363 if (all)
364 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
365 bus->busn_res.start, bus->busn_res.end, pe_num);
366 else
367 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
368 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000369
370 if (pnv_ioda_configure_pe(phb, pe)) {
371 /* XXX What do we do here ? */
372 if (pe_num)
373 pnv_ioda_free_pe(phb, pe_num);
374 pe->pbus = NULL;
375 return;
376 }
377
378 /* Associate it with all child devices */
379 pnv_ioda_setup_same_PE(bus, pe);
380
Gavin Shan7ebdf952012-08-20 03:49:15 +0000381 /* Put PE to the list */
382 list_add_tail(&pe->list, &phb->ioda.pe_list);
383
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000384 /* Account for one DMA PE if at least one DMA capable device exist
385 * below the bridge
386 */
387 if (pe->dma_weight != 0) {
388 phb->ioda.dma_weight += pe->dma_weight;
389 phb->ioda.dma_pe_count++;
390 }
391
392 /* Link the PE */
393 pnv_ioda_link_pe_by_weight(phb, pe);
394}
395
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800396static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000397{
398 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000399
400 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000401
402 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000403 if (dev->subordinate) {
404 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
405 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
406 else
407 pnv_ioda_setup_PEs(dev->subordinate);
408 }
409 }
410}
411
412/*
413 * Configure PEs so that the downstream PCI buses and devices
414 * could have their associated PE#. Unfortunately, we didn't
415 * figure out the way to identify the PLX bridge yet. So we
416 * simply put the PCI bus and the subordinate behind the root
417 * port to PE# here. The game rule here is expected to be changed
418 * as soon as we can detected PLX bridge correctly.
419 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800420static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +0000421{
422 struct pci_controller *hose, *tmp;
423
424 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
425 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000426 }
427}
428
Gavin Shan959c9bd2013-04-25 19:21:02 +0000429static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000430{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000431 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +0000432 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000433
Gavin Shan959c9bd2013-04-25 19:21:02 +0000434 /*
435 * The function can be called while the PE#
436 * hasn't been assigned. Do nothing for the
437 * case.
438 */
439 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
440 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000441
Gavin Shan959c9bd2013-04-25 19:21:02 +0000442 pe = &phb->ioda.pe_array[pdn->pe_number];
443 set_iommu_table_base(&pdev->dev, &pe->tce32_table);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000444}
445
Gavin Shan4cce9552013-04-25 19:21:00 +0000446static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
447 u64 *startp, u64 *endp)
448{
449 u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
450 unsigned long start, end, inc;
451
452 start = __pa(startp);
453 end = __pa(endp);
454
455 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
456 if (tbl->it_busno) {
457 start <<= 12;
458 end <<= 12;
459 inc = 128 << 12;
460 start |= tbl->it_busno;
461 end |= tbl->it_busno;
462 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
463 /* p7ioc-style invalidation, 2 TCEs per write */
464 start |= (1ull << 63);
465 end |= (1ull << 63);
466 inc = 16;
467 } else {
468 /* Default (older HW) */
469 inc = 128;
470 }
471
472 end |= inc - 1; /* round up end to be different than start */
473
474 mb(); /* Ensure above stores are visible */
475 while (start <= end) {
476 __raw_writeq(start, invalidate);
477 start += inc;
478 }
479
480 /*
481 * The iommu layer will do another mb() for us on build()
482 * and we don't care on free()
483 */
484}
485
486static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
487 struct iommu_table *tbl,
488 u64 *startp, u64 *endp)
489{
490 unsigned long start, end, inc;
491 u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
492
493 /* We'll invalidate DMA address in PE scope */
494 start = 0x2ul << 60;
495 start |= (pe->pe_number & 0xFF);
496 end = start;
497
498 /* Figure out the start, end and step */
499 inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
500 start |= (inc << 12);
501 inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
502 end |= (inc << 12);
503 inc = (0x1ul << 12);
504 mb();
505
506 while (start <= end) {
507 __raw_writeq(start, invalidate);
508 start += inc;
509 }
510}
511
512void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
513 u64 *startp, u64 *endp)
514{
515 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
516 tce32_table);
517 struct pnv_phb *phb = pe->phb;
518
519 if (phb->type == PNV_PHB_IODA1)
520 pnv_pci_ioda1_tce_invalidate(tbl, startp, endp);
521 else
522 pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp);
523}
524
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800525static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
526 struct pnv_ioda_pe *pe, unsigned int base,
527 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000528{
529
530 struct page *tce_mem = NULL;
531 const __be64 *swinvp;
532 struct iommu_table *tbl;
533 unsigned int i;
534 int64_t rc;
535 void *addr;
536
537 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
538#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
539
540 /* XXX FIXME: Handle 64-bit only DMA devices */
541 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
542 /* XXX FIXME: Allocate multi-level tables on PHB3 */
543
544 /* We shouldn't already have a 32-bit DMA associated */
545 if (WARN_ON(pe->tce32_seg >= 0))
546 return;
547
548 /* Grab a 32-bit TCE table */
549 pe->tce32_seg = base;
550 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
551 (base << 28), ((base + segs) << 28) - 1);
552
553 /* XXX Currently, we allocate one big contiguous table for the
554 * TCEs. We only really need one chunk per 256M of TCE space
555 * (ie per segment) but that's an optimization for later, it
556 * requires some added smarts with our get/put_tce implementation
557 */
558 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
559 get_order(TCE32_TABLE_SIZE * segs));
560 if (!tce_mem) {
561 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
562 goto fail;
563 }
564 addr = page_address(tce_mem);
565 memset(addr, 0, TCE32_TABLE_SIZE * segs);
566
567 /* Configure HW */
568 for (i = 0; i < segs; i++) {
569 rc = opal_pci_map_pe_dma_window(phb->opal_id,
570 pe->pe_number,
571 base + i, 1,
572 __pa(addr) + TCE32_TABLE_SIZE * i,
573 TCE32_TABLE_SIZE, 0x1000);
574 if (rc) {
575 pe_err(pe, " Failed to configure 32-bit TCE table,"
576 " err %ld\n", rc);
577 goto fail;
578 }
579 }
580
581 /* Setup linux iommu table */
582 tbl = &pe->tce32_table;
583 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
584 base << 28);
585
586 /* OPAL variant of P7IOC SW invalidated TCEs */
587 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
588 if (swinvp) {
589 /* We need a couple more fields -- an address and a data
590 * to or. Since the bus is only printed out on table free
591 * errors, and on the first pass the data will be a relative
592 * bus number, print that out instead.
593 */
594 tbl->it_busno = 0;
595 tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
Gavin Shan373f5652013-04-25 19:21:01 +0000596 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE |
597 TCE_PCI_SWINV_PAIR;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000598 }
599 iommu_init_table(tbl, phb->hose->node);
Alexey Kardashevskiy4e13c1a2013-05-21 13:33:09 +1000600 iommu_register_group(tbl, pci_domain_nr(pe->pbus), pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000601
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000602 return;
603 fail:
604 /* XXX Failure: Try to fallback to 64-bit only ? */
605 if (pe->tce32_seg >= 0)
606 pe->tce32_seg = -1;
607 if (tce_mem)
608 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
609}
610
Gavin Shan373f5652013-04-25 19:21:01 +0000611static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
612 struct pnv_ioda_pe *pe)
613{
614 struct page *tce_mem = NULL;
615 void *addr;
616 const __be64 *swinvp;
617 struct iommu_table *tbl;
618 unsigned int tce_table_size, end;
619 int64_t rc;
620
621 /* We shouldn't already have a 32-bit DMA associated */
622 if (WARN_ON(pe->tce32_seg >= 0))
623 return;
624
625 /* The PE will reserve all possible 32-bits space */
626 pe->tce32_seg = 0;
627 end = (1 << ilog2(phb->ioda.m32_pci_base));
628 tce_table_size = (end / 0x1000) * 8;
629 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
630 end);
631
632 /* Allocate TCE table */
633 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
634 get_order(tce_table_size));
635 if (!tce_mem) {
636 pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
637 goto fail;
638 }
639 addr = page_address(tce_mem);
640 memset(addr, 0, tce_table_size);
641
642 /*
643 * Map TCE table through TVT. The TVE index is the PE number
644 * shifted by 1 bit for 32-bits DMA space.
645 */
646 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
647 pe->pe_number << 1, 1, __pa(addr),
648 tce_table_size, 0x1000);
649 if (rc) {
650 pe_err(pe, "Failed to configure 32-bit TCE table,"
651 " err %ld\n", rc);
652 goto fail;
653 }
654
655 /* Setup linux iommu table */
656 tbl = &pe->tce32_table;
657 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0);
658
659 /* OPAL variant of PHB3 invalidated TCEs */
660 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
661 if (swinvp) {
662 /* We need a couple more fields -- an address and a data
663 * to or. Since the bus is only printed out on table free
664 * errors, and on the first pass the data will be a relative
665 * bus number, print that out instead.
666 */
667 tbl->it_busno = 0;
668 tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
669 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
670 }
671 iommu_init_table(tbl, phb->hose->node);
672
Gavin Shan373f5652013-04-25 19:21:01 +0000673 return;
674fail:
675 if (pe->tce32_seg >= 0)
676 pe->tce32_seg = -1;
677 if (tce_mem)
678 __free_pages(tce_mem, get_order(tce_table_size));
679}
680
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800681static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000682{
683 struct pci_controller *hose = phb->hose;
684 unsigned int residual, remaining, segs, tw, base;
685 struct pnv_ioda_pe *pe;
686
687 /* If we have more PE# than segments available, hand out one
688 * per PE until we run out and let the rest fail. If not,
689 * then we assign at least one segment per PE, plus more based
690 * on the amount of devices under that PE
691 */
692 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
693 residual = 0;
694 else
695 residual = phb->ioda.tce32_count -
696 phb->ioda.dma_pe_count;
697
698 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
699 hose->global_number, phb->ioda.tce32_count);
700 pr_info("PCI: %d PE# for a total weight of %d\n",
701 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
702
703 /* Walk our PE list and configure their DMA segments, hand them
704 * out one base segment plus any residual segments based on
705 * weight
706 */
707 remaining = phb->ioda.tce32_count;
708 tw = phb->ioda.dma_weight;
709 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +0000710 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000711 if (!pe->dma_weight)
712 continue;
713 if (!remaining) {
714 pe_warn(pe, "No DMA32 resources available\n");
715 continue;
716 }
717 segs = 1;
718 if (residual) {
719 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
720 if (segs > remaining)
721 segs = remaining;
722 }
Gavin Shan373f5652013-04-25 19:21:01 +0000723
724 /*
725 * For IODA2 compliant PHB3, we needn't care about the weight.
726 * The all available 32-bits DMA space will be assigned to
727 * the specific PE.
728 */
729 if (phb->type == PNV_PHB_IODA1) {
730 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
731 pe->dma_weight, segs);
732 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
733 } else {
734 pe_info(pe, "Assign DMA32 space\n");
735 segs = 0;
736 pnv_pci_ioda2_setup_dma_pe(phb, pe);
737 }
738
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000739 remaining -= segs;
740 base += segs;
741 }
742}
743
744#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +0000745static void pnv_ioda2_msi_eoi(struct irq_data *d)
746{
747 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
748 struct irq_chip *chip = irq_data_get_irq_chip(d);
749 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
750 ioda.irq_chip);
751 int64_t rc;
752
753 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
754 WARN_ON_ONCE(rc);
755
756 icp_native_eoi(d);
757}
758
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000759static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +0000760 unsigned int hwirq, unsigned int virq,
761 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000762{
763 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000764 struct pci_dn *pdn = pci_get_pdn(dev);
Gavin Shan137436c2013-04-25 19:20:59 +0000765 struct irq_data *idata;
766 struct irq_chip *ichip;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000767 unsigned int xive_num = hwirq - phb->msi_base;
768 uint64_t addr64;
769 uint32_t addr32, data;
770 int rc;
771
772 /* No PE assigned ? bail out ... no MSI for you ! */
773 if (pe == NULL)
774 return -ENXIO;
775
776 /* Check if we have an MVE */
777 if (pe->mve_number < 0)
778 return -ENXIO;
779
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000780 /* Force 32-bit MSI on some broken devices */
781 if (pdn && pdn->force_32bit_msi)
782 is_64 = 0;
783
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000784 /* Assign XIVE to PE */
785 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
786 if (rc) {
787 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
788 pci_name(dev), rc, xive_num);
789 return -EIO;
790 }
791
792 if (is_64) {
793 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
794 &addr64, &data);
795 if (rc) {
796 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
797 pci_name(dev), rc);
798 return -EIO;
799 }
800 msg->address_hi = addr64 >> 32;
801 msg->address_lo = addr64 & 0xfffffffful;
802 } else {
803 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
804 &addr32, &data);
805 if (rc) {
806 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
807 pci_name(dev), rc);
808 return -EIO;
809 }
810 msg->address_hi = 0;
811 msg->address_lo = addr32;
812 }
813 msg->data = data;
814
Gavin Shan137436c2013-04-25 19:20:59 +0000815 /*
816 * Change the IRQ chip for the MSI interrupts on PHB3.
817 * The corresponding IRQ chip should be populated for
818 * the first time.
819 */
820 if (phb->type == PNV_PHB_IODA2) {
821 if (!phb->ioda.irq_chip_init) {
822 idata = irq_get_irq_data(virq);
823 ichip = irq_data_get_irq_chip(idata);
824 phb->ioda.irq_chip_init = 1;
825 phb->ioda.irq_chip = *ichip;
826 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
827 }
828
829 irq_set_chip(virq, &phb->ioda.irq_chip);
830 }
831
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000832 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
833 " address=%x_%08x data=%x PE# %d\n",
834 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
835 msg->address_hi, msg->address_lo, data, pe->pe_number);
836
837 return 0;
838}
839
840static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
841{
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000842 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000843 const __be32 *prop = of_get_property(phb->hose->dn,
844 "ibm,opal-msi-ranges", NULL);
845 if (!prop) {
846 /* BML Fallback */
847 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
848 }
849 if (!prop)
850 return;
851
852 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000853 count = be32_to_cpup(prop + 1);
854 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000855 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
856 phb->hose->global_number);
857 return;
858 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000859
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000860 phb->msi_setup = pnv_pci_ioda_msi_setup;
861 phb->msi32_support = 1;
862 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000863 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000864}
865#else
866static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
867#endif /* CONFIG_PCI_MSI */
868
Gavin Shan11685be2012-08-20 03:49:16 +0000869/*
870 * This function is supposed to be called on basis of PE from top
871 * to bottom style. So the the I/O or MMIO segment assigned to
872 * parent PE could be overrided by its child PEs if necessary.
873 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800874static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
875 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +0000876{
877 struct pnv_phb *phb = hose->private_data;
878 struct pci_bus_region region;
879 struct resource *res;
880 int i, index;
881 int rc;
882
883 /*
884 * NOTE: We only care PCI bus based PE for now. For PCI
885 * device based PE, for example SRIOV sensitive VF should
886 * be figured out later.
887 */
888 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
889
890 pci_bus_for_each_resource(pe->pbus, res, i) {
891 if (!res || !res->flags ||
892 res->start > res->end)
893 continue;
894
895 if (res->flags & IORESOURCE_IO) {
896 region.start = res->start - phb->ioda.io_pci_base;
897 region.end = res->end - phb->ioda.io_pci_base;
898 index = region.start / phb->ioda.io_segsize;
899
900 while (index < phb->ioda.total_pe &&
901 region.start <= region.end) {
902 phb->ioda.io_segmap[index] = pe->pe_number;
903 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
904 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
905 if (rc != OPAL_SUCCESS) {
906 pr_err("%s: OPAL error %d when mapping IO "
907 "segment #%d to PE#%d\n",
908 __func__, rc, index, pe->pe_number);
909 break;
910 }
911
912 region.start += phb->ioda.io_segsize;
913 index++;
914 }
915 } else if (res->flags & IORESOURCE_MEM) {
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +1000916 /* WARNING: Assumes M32 is mem region 0 in PHB. We need to
917 * harden that algorithm when we start supporting M64
918 */
Gavin Shan11685be2012-08-20 03:49:16 +0000919 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +1000920 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +0000921 phb->ioda.m32_pci_base;
922 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +1000923 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +0000924 phb->ioda.m32_pci_base;
925 index = region.start / phb->ioda.m32_segsize;
926
927 while (index < phb->ioda.total_pe &&
928 region.start <= region.end) {
929 phb->ioda.m32_segmap[index] = pe->pe_number;
930 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
931 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
932 if (rc != OPAL_SUCCESS) {
933 pr_err("%s: OPAL error %d when mapping M32 "
934 "segment#%d to PE#%d",
935 __func__, rc, index, pe->pe_number);
936 break;
937 }
938
939 region.start += phb->ioda.m32_segsize;
940 index++;
941 }
942 }
943 }
944}
945
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800946static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +0000947{
948 struct pci_controller *tmp, *hose;
949 struct pnv_phb *phb;
950 struct pnv_ioda_pe *pe;
951
952 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
953 phb = hose->private_data;
954 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
955 pnv_ioda_setup_pe_seg(hose, pe);
956 }
957 }
958}
959
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800960static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +0000961{
962 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +0000963 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +0000964
965 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
966 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +0000967
968 /* Mark the PHB initialization done */
969 phb = hose->private_data;
970 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +0000971 }
972}
973
Gavin Shan37c367f2013-06-20 18:13:25 +0800974static void pnv_pci_ioda_create_dbgfs(void)
975{
976#ifdef CONFIG_DEBUG_FS
977 struct pci_controller *hose, *tmp;
978 struct pnv_phb *phb;
979 char name[16];
980
981 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
982 phb = hose->private_data;
983
984 sprintf(name, "PCI%04x", hose->global_number);
985 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
986 if (!phb->dbgfs)
987 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
988 __func__, hose->global_number);
989 }
990#endif /* CONFIG_DEBUG_FS */
991}
992
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800993static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +0000994{
995 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +0000996 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +0000997 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +0800998
Gavin Shan37c367f2013-06-20 18:13:25 +0800999 pnv_pci_ioda_create_dbgfs();
1000
Gavin Shane9cc17d2013-06-20 13:21:14 +08001001#ifdef CONFIG_EEH
1002 eeh_addr_cache_build();
1003 eeh_init();
1004#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00001005}
1006
Gavin Shan271fd032012-09-11 16:59:47 -06001007/*
1008 * Returns the alignment for I/O or memory windows for P2P
1009 * bridges. That actually depends on how PEs are segmented.
1010 * For now, we return I/O or M32 segment size for PE sensitive
1011 * P2P bridges. Otherwise, the default values (4KiB for I/O,
1012 * 1MiB for memory) will be returned.
1013 *
1014 * The current PCI bus might be put into one PE, which was
1015 * create against the parent PCI bridge. For that case, we
1016 * needn't enlarge the alignment so that we can save some
1017 * resources.
1018 */
1019static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
1020 unsigned long type)
1021{
1022 struct pci_dev *bridge;
1023 struct pci_controller *hose = pci_bus_to_host(bus);
1024 struct pnv_phb *phb = hose->private_data;
1025 int num_pci_bridges = 0;
1026
1027 bridge = bus->self;
1028 while (bridge) {
1029 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
1030 num_pci_bridges++;
1031 if (num_pci_bridges >= 2)
1032 return 1;
1033 }
1034
1035 bridge = bridge->bus->self;
1036 }
1037
1038 /* We need support prefetchable memory window later */
1039 if (type & IORESOURCE_MEM)
1040 return phb->ioda.m32_segsize;
1041
1042 return phb->ioda.io_segsize;
1043}
1044
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001045/* Prevent enabling devices for which we couldn't properly
1046 * assign a PE
1047 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001048static int pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001049{
Gavin Shandb1266c2012-08-20 03:49:18 +00001050 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1051 struct pnv_phb *phb = hose->private_data;
1052 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001053
Gavin Shandb1266c2012-08-20 03:49:18 +00001054 /* The function is probably called while the PEs have
1055 * not be created yet. For example, resource reassignment
1056 * during PCI probe period. We just skip the check if
1057 * PEs isn't ready.
1058 */
1059 if (!phb->initialized)
1060 return 0;
1061
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001062 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001063 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1064 return -EINVAL;
Gavin Shandb1266c2012-08-20 03:49:18 +00001065
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001066 return 0;
1067}
1068
1069static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1070 u32 devfn)
1071{
1072 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1073}
1074
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001075static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
1076{
1077 opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET,
1078 OPAL_ASSERT_RESET);
1079}
1080
Gavin Shane9cc17d2013-06-20 13:21:14 +08001081void __init pnv_pci_init_ioda_phb(struct device_node *np,
1082 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001083{
1084 struct pci_controller *hose;
1085 static int primary = 1;
1086 struct pnv_phb *phb;
1087 unsigned long size, m32map_off, iomap_off, pemap_off;
1088 const u64 *prop64;
Gavin Shanaa0c0332013-04-25 19:20:57 +00001089 const u32 *prop32;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001090 u64 phb_id;
1091 void *aux;
1092 long rc;
1093
Gavin Shanaa0c0332013-04-25 19:20:57 +00001094 pr_info(" Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001095
1096 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1097 if (!prop64) {
1098 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
1099 return;
1100 }
1101 phb_id = be64_to_cpup(prop64);
1102 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
1103
1104 phb = alloc_bootmem(sizeof(struct pnv_phb));
1105 if (phb) {
1106 memset(phb, 0, sizeof(struct pnv_phb));
1107 phb->hose = hose = pcibios_alloc_controller(np);
1108 }
1109 if (!phb || !phb->hose) {
1110 pr_err("PCI: Failed to allocate PCI controller for %s\n",
1111 np->full_name);
1112 return;
1113 }
1114
1115 spin_lock_init(&phb->lock);
1116 /* XXX Use device-tree */
1117 hose->first_busno = 0;
1118 hose->last_busno = 0xff;
1119 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08001120 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001121 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00001122 phb->type = ioda_type;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001123
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00001124 /* Detect specific models for error handling */
1125 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
1126 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00001127 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00001128 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00001129 else
1130 phb->model = PNV_PHB_MODEL_UNKNOWN;
1131
Gavin Shanaa0c0332013-04-25 19:20:57 +00001132 /* Parse 32-bit and IO ranges (if any) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001133 pci_process_bridge_OF_ranges(phb->hose, np, primary);
1134 primary = 0;
1135
Gavin Shanaa0c0332013-04-25 19:20:57 +00001136 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001137 phb->regs = of_iomap(np, 0);
1138 if (phb->regs == NULL)
1139 pr_err(" Failed to map registers !\n");
1140
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001141 /* Initialize more IODA stuff */
Gavin Shanaa0c0332013-04-25 19:20:57 +00001142 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
1143 if (!prop32)
1144 phb->ioda.total_pe = 1;
1145 else
1146 phb->ioda.total_pe = *prop32;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001147
1148 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00001149 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001150 phb->ioda.m32_size += 0x10000;
1151
1152 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001153 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001154 phb->ioda.io_size = hose->pci_io_size;
1155 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
1156 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
1157
Gavin Shanaa0c0332013-04-25 19:20:57 +00001158 /* Allocate aux data & arrays
1159 *
1160 * XXX TODO: Don't allocate io segmap on PHB3
1161 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001162 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
1163 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00001164 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001165 iomap_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00001166 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001167 pemap_off = size;
1168 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
1169 aux = alloc_bootmem(size);
1170 memset(aux, 0, size);
1171 phb->ioda.pe_alloc = aux;
1172 phb->ioda.m32_segmap = aux + m32map_off;
1173 phb->ioda.io_segmap = aux + iomap_off;
1174 phb->ioda.pe_array = aux + pemap_off;
1175 set_bit(0, phb->ioda.pe_alloc);
1176
Gavin Shan7ebdf952012-08-20 03:49:15 +00001177 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001178 INIT_LIST_HEAD(&phb->ioda.pe_list);
1179
1180 /* Calculate how many 32-bit TCE segments we have */
1181 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
1182
1183 /* Clear unusable m64 */
1184 hose->mem_resources[1].flags = 0;
1185 hose->mem_resources[1].start = 0;
1186 hose->mem_resources[1].end = 0;
1187 hose->mem_resources[2].flags = 0;
1188 hose->mem_resources[2].start = 0;
1189 hose->mem_resources[2].end = 0;
1190
Gavin Shanaa0c0332013-04-25 19:20:57 +00001191#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001192 rc = opal_pci_set_phb_mem_window(opal->phb_id,
1193 window_type,
1194 window_num,
1195 starting_real_address,
1196 starting_pci_address,
1197 segment_size);
1198#endif
1199
1200 pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
1201 phb->ioda.total_pe,
1202 phb->ioda.m32_size, phb->ioda.m32_segsize,
1203 phb->ioda.io_size, phb->ioda.io_segsize);
1204
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001205 phb->hose->ops = &pnv_pci_ops;
Gavin Shane9cc17d2013-06-20 13:21:14 +08001206#ifdef CONFIG_EEH
1207 phb->eeh_ops = &ioda_eeh_ops;
1208#endif
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001209
1210 /* Setup RID -> PE mapping function */
1211 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
1212
1213 /* Setup TCEs */
1214 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
1215
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001216 /* Setup shutdown function for kexec */
1217 phb->shutdown = pnv_pci_ioda_shutdown;
1218
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001219 /* Setup MSI support */
1220 pnv_pci_init_ioda_msis(phb);
1221
Gavin Shanc40a4212012-08-20 03:49:20 +00001222 /*
1223 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
1224 * to let the PCI core do resource assignment. It's supposed
1225 * that the PCI core will do correct I/O and MMIO alignment
1226 * for the P2P bridge bars so that each PCI bus (excluding
1227 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001228 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00001229 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001230 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
Gavin Shan271fd032012-09-11 16:59:47 -06001231 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
Gavin Shanc40a4212012-08-20 03:49:20 +00001232 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001233
1234 /* Reset IODA tables to a clean state */
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00001235 rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001236 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00001237 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shanaa0c0332013-04-25 19:20:57 +00001238
1239 /*
1240 * On IODA1 map everything to PE#0, on IODA2 we assume the IODA reset
1241 * has cleared the RTT which has the same effect
1242 */
1243 if (ioda_type == PNV_PHB_IODA1)
1244 opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
1245}
1246
1247void pnv_pci_init_ioda2_phb(struct device_node *np)
1248{
Gavin Shane9cc17d2013-06-20 13:21:14 +08001249 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001250}
1251
1252void __init pnv_pci_init_ioda_hub(struct device_node *np)
1253{
1254 struct device_node *phbn;
1255 const u64 *prop64;
1256 u64 hub_id;
1257
1258 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
1259
1260 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
1261 if (!prop64) {
1262 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
1263 return;
1264 }
1265 hub_id = be64_to_cpup(prop64);
1266 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
1267
1268 /* Count child PHBs */
1269 for_each_child_of_node(np, phbn) {
1270 /* Look for IODA1 PHBs */
1271 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08001272 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001273 }
1274}