blob: 866692b851f29557fc94da64a7f4921fc83f00e0 [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 Shan361f2a22014-04-24 18:00:25 +100016#include <linux/crash_dump.h>
Gavin Shan37c367f2013-06-20 18:13:25 +080017#include <linux/debugfs.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000018#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>
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +110025#include <linux/memblock.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000026
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>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000032#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000033#include <asm/ppc-pci.h>
34#include <asm/opal.h>
35#include <asm/iommu.h>
36#include <asm/tce.h>
Gavin Shan137436c2013-04-25 19:20:59 +000037#include <asm/xics.h>
Gavin Shan37c367f2013-06-20 18:13:25 +080038#include <asm/debug.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000039
40#include "powernv.h"
41#include "pci.h"
42
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000043#define define_pe_printk_level(func, kern_level) \
44static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
45{ \
46 struct va_format vaf; \
47 va_list args; \
Gavin Shan490e0782012-10-17 19:53:30 +000048 char pfix[32]; \
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000049 int r; \
50 \
51 va_start(args, fmt); \
52 \
53 vaf.fmt = fmt; \
54 vaf.va = &args; \
55 \
Gavin Shan490e0782012-10-17 19:53:30 +000056 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); \
63 r = printk(kern_level "pci %s: [PE# %.3d] %pV", \
64 pfix, pe->pe_number, &vaf); \
65 \
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000066 va_end(args); \
67 \
68 return r; \
69} \
70
71define_pe_printk_level(pe_err, KERN_ERR);
72define_pe_printk_level(pe_warn, KERN_WARNING);
73define_pe_printk_level(pe_info, KERN_INFO);
74
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +100075/*
76 * stdcix is only supposed to be used in hypervisor real mode as per
77 * the architecture spec
78 */
79static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
80{
81 __asm__ __volatile__("stdcix %0,0,%1"
82 : : "r" (val), "r" (paddr) : "memory");
83}
84
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -080085static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000086{
87 unsigned long pe;
88
89 do {
90 pe = find_next_zero_bit(phb->ioda.pe_alloc,
91 phb->ioda.total_pe, 0);
92 if (pe >= phb->ioda.total_pe)
93 return IODA_INVALID_PE;
94 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
95
Gavin Shan4cce9552013-04-25 19:21:00 +000096 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000097 phb->ioda.pe_array[pe].pe_number = pe;
98 return pe;
99}
100
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800101static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000102{
103 WARN_ON(phb->ioda.pe_array[pe].pdev);
104
105 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
106 clear_bit(pe, phb->ioda.pe_alloc);
107}
108
109/* Currently those 2 are only used when MSIs are enabled, this will change
110 * but in the meantime, we need to protect them to avoid warnings
111 */
112#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800113static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000114{
115 struct pci_controller *hose = pci_bus_to_host(dev->bus);
116 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000117 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000118
119 if (!pdn)
120 return NULL;
121 if (pdn->pe_number == IODA_INVALID_PE)
122 return NULL;
123 return &phb->ioda.pe_array[pdn->pe_number];
124}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000125#endif /* CONFIG_PCI_MSI */
126
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800127static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000128{
129 struct pci_dev *parent;
130 uint8_t bcomp, dcomp, fcomp;
131 long rc, rid_end, rid;
132
133 /* Bus validation ? */
134 if (pe->pbus) {
135 int count;
136
137 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
138 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
139 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000140 if (pe->flags & PNV_IODA_PE_BUS_ALL)
141 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
142 else
143 count = 1;
144
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000145 switch(count) {
146 case 1: bcomp = OpalPciBusAll; break;
147 case 2: bcomp = OpalPciBus7Bits; break;
148 case 4: bcomp = OpalPciBus6Bits; break;
149 case 8: bcomp = OpalPciBus5Bits; break;
150 case 16: bcomp = OpalPciBus4Bits; break;
151 case 32: bcomp = OpalPciBus3Bits; break;
152 default:
153 pr_err("%s: Number of subordinate busses %d"
154 " unsupported\n",
155 pci_name(pe->pbus->self), count);
156 /* Do an exact match only */
157 bcomp = OpalPciBusAll;
158 }
159 rid_end = pe->rid + (count << 8);
160 } else {
161 parent = pe->pdev->bus->self;
162 bcomp = OpalPciBusAll;
163 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
164 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
165 rid_end = pe->rid + 1;
166 }
167
Gavin Shan631ad692013-11-04 16:32:46 +0800168 /*
169 * Associate PE in PELT. We need add the PE into the
170 * corresponding PELT-V as well. Otherwise, the error
171 * originated from the PE might contribute to other
172 * PEs.
173 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000174 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
175 bcomp, dcomp, fcomp, OPAL_MAP_PE);
176 if (rc) {
177 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
178 return -ENXIO;
179 }
Gavin Shan631ad692013-11-04 16:32:46 +0800180
181 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
182 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
183 if (rc)
184 pe_warn(pe, "OPAL error %d adding self to PELTV\n", rc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000185 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
186 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
187
188 /* Add to all parents PELT-V */
189 while (parent) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000190 struct pci_dn *pdn = pci_get_pdn(parent);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000191 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
192 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000193 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000194 /* XXX What to do in case of error ? */
195 }
196 parent = parent->bus->self;
197 }
198 /* Setup reverse map */
199 for (rid = pe->rid; rid < rid_end; rid++)
200 phb->ioda.pe_rmap[rid] = pe->pe_number;
201
202 /* Setup one MVTs on IODA1 */
203 if (phb->type == PNV_PHB_IODA1) {
204 pe->mve_number = pe->pe_number;
205 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
206 pe->pe_number);
207 if (rc) {
208 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
209 rc, pe->mve_number);
210 pe->mve_number = -1;
211 } else {
212 rc = opal_pci_set_mve_enable(phb->opal_id,
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000213 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000214 if (rc) {
215 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
216 rc, pe->mve_number);
217 pe->mve_number = -1;
218 }
219 }
220 } else if (phb->type == PNV_PHB_IODA2)
221 pe->mve_number = 0;
222
223 return 0;
224}
225
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800226static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
227 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000228{
229 struct pnv_ioda_pe *lpe;
230
Gavin Shan7ebdf952012-08-20 03:49:15 +0000231 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000232 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000233 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000234 return;
235 }
236 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000237 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000238}
239
240static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
241{
242 /* This is quite simplistic. The "base" weight of a device
243 * is 10. 0 means no DMA is to be accounted for it.
244 */
245
246 /* If it's a bridge, no DMA */
247 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
248 return 0;
249
250 /* Reduce the weight of slow USB controllers */
251 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
252 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
253 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
254 return 3;
255
256 /* Increase the weight of RAID (includes Obsidian) */
257 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
258 return 15;
259
260 /* Default */
261 return 10;
262}
263
Gavin Shanfb446ad2012-08-20 03:49:14 +0000264#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800265static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000266{
267 struct pci_controller *hose = pci_bus_to_host(dev->bus);
268 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000269 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000270 struct pnv_ioda_pe *pe;
271 int pe_num;
272
273 if (!pdn) {
274 pr_err("%s: Device tree node not associated properly\n",
275 pci_name(dev));
276 return NULL;
277 }
278 if (pdn->pe_number != IODA_INVALID_PE)
279 return NULL;
280
281 /* PE#0 has been pre-set */
282 if (dev->bus->number == 0)
283 pe_num = 0;
284 else
285 pe_num = pnv_ioda_alloc_pe(phb);
286 if (pe_num == IODA_INVALID_PE) {
287 pr_warning("%s: Not enough PE# available, disabling device\n",
288 pci_name(dev));
289 return NULL;
290 }
291
292 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
293 * pointer in the PE data structure, both should be destroyed at the
294 * same time. However, this needs to be looked at more closely again
295 * once we actually start removing things (Hotplug, SR-IOV, ...)
296 *
297 * At some point we want to remove the PDN completely anyways
298 */
299 pe = &phb->ioda.pe_array[pe_num];
300 pci_dev_get(dev);
301 pdn->pcidev = dev;
302 pdn->pe_number = pe_num;
303 pe->pdev = dev;
304 pe->pbus = NULL;
305 pe->tce32_seg = -1;
306 pe->mve_number = -1;
307 pe->rid = dev->bus->number << 8 | pdn->devfn;
308
309 pe_info(pe, "Associated device to PE\n");
310
311 if (pnv_ioda_configure_pe(phb, pe)) {
312 /* XXX What do we do here ? */
313 if (pe_num)
314 pnv_ioda_free_pe(phb, pe_num);
315 pdn->pe_number = IODA_INVALID_PE;
316 pe->pdev = NULL;
317 pci_dev_put(dev);
318 return NULL;
319 }
320
321 /* Assign a DMA weight to the device */
322 pe->dma_weight = pnv_ioda_dma_weight(dev);
323 if (pe->dma_weight != 0) {
324 phb->ioda.dma_weight += pe->dma_weight;
325 phb->ioda.dma_pe_count++;
326 }
327
328 /* Link the PE */
329 pnv_ioda_link_pe_by_weight(phb, pe);
330
331 return pe;
332}
Gavin Shanfb446ad2012-08-20 03:49:14 +0000333#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000334
335static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
336{
337 struct pci_dev *dev;
338
339 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000340 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000341
342 if (pdn == NULL) {
343 pr_warn("%s: No device node associated with device !\n",
344 pci_name(dev));
345 continue;
346 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000347 pdn->pcidev = dev;
348 pdn->pe_number = pe->pe_number;
349 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +0000350 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000351 pnv_ioda_setup_same_PE(dev->subordinate, pe);
352 }
353}
354
Gavin Shanfb446ad2012-08-20 03:49:14 +0000355/*
356 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
357 * single PCI bus. Another one that contains the primary PCI bus and its
358 * subordinate PCI devices and buses. The second type of PE is normally
359 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
360 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800361static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000362{
Gavin Shanfb446ad2012-08-20 03:49:14 +0000363 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000364 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000365 struct pnv_ioda_pe *pe;
366 int pe_num;
367
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000368 pe_num = pnv_ioda_alloc_pe(phb);
369 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000370 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
371 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000372 return;
373 }
374
375 pe = &phb->ioda.pe_array[pe_num];
Gavin Shanfb446ad2012-08-20 03:49:14 +0000376 pe->flags = (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000377 pe->pbus = bus;
378 pe->pdev = NULL;
379 pe->tce32_seg = -1;
380 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -0700381 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000382 pe->dma_weight = 0;
383
Gavin Shanfb446ad2012-08-20 03:49:14 +0000384 if (all)
385 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
386 bus->busn_res.start, bus->busn_res.end, pe_num);
387 else
388 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
389 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000390
391 if (pnv_ioda_configure_pe(phb, pe)) {
392 /* XXX What do we do here ? */
393 if (pe_num)
394 pnv_ioda_free_pe(phb, pe_num);
395 pe->pbus = NULL;
396 return;
397 }
398
399 /* Associate it with all child devices */
400 pnv_ioda_setup_same_PE(bus, pe);
401
Gavin Shan7ebdf952012-08-20 03:49:15 +0000402 /* Put PE to the list */
403 list_add_tail(&pe->list, &phb->ioda.pe_list);
404
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000405 /* Account for one DMA PE if at least one DMA capable device exist
406 * below the bridge
407 */
408 if (pe->dma_weight != 0) {
409 phb->ioda.dma_weight += pe->dma_weight;
410 phb->ioda.dma_pe_count++;
411 }
412
413 /* Link the PE */
414 pnv_ioda_link_pe_by_weight(phb, pe);
415}
416
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800417static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000418{
419 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000420
421 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000422
423 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000424 if (dev->subordinate) {
425 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
426 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
427 else
428 pnv_ioda_setup_PEs(dev->subordinate);
429 }
430 }
431}
432
433/*
434 * Configure PEs so that the downstream PCI buses and devices
435 * could have their associated PE#. Unfortunately, we didn't
436 * figure out the way to identify the PLX bridge yet. So we
437 * simply put the PCI bus and the subordinate behind the root
438 * port to PE# here. The game rule here is expected to be changed
439 * as soon as we can detected PLX bridge correctly.
440 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800441static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +0000442{
443 struct pci_controller *hose, *tmp;
444
445 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
446 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000447 }
448}
449
Gavin Shan959c9bd2013-04-25 19:21:02 +0000450static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000451{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000452 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +0000453 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000454
Gavin Shan959c9bd2013-04-25 19:21:02 +0000455 /*
456 * The function can be called while the PE#
457 * hasn't been assigned. Do nothing for the
458 * case.
459 */
460 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
461 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000462
Gavin Shan959c9bd2013-04-25 19:21:02 +0000463 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100464 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Wei Yang3f28c5a2014-04-23 10:26:32 +0800465 set_iommu_table_base(&pdev->dev, &pe->tce32_table);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000466}
467
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100468static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
469 struct pci_dev *pdev, u64 dma_mask)
470{
471 struct pci_dn *pdn = pci_get_pdn(pdev);
472 struct pnv_ioda_pe *pe;
473 uint64_t top;
474 bool bypass = false;
475
476 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
477 return -ENODEV;;
478
479 pe = &phb->ioda.pe_array[pdn->pe_number];
480 if (pe->tce_bypass_enabled) {
481 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
482 bypass = (dma_mask >= top);
483 }
484
485 if (bypass) {
486 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
487 set_dma_ops(&pdev->dev, &dma_direct_ops);
488 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
489 } else {
490 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
491 set_dma_ops(&pdev->dev, &dma_iommu_ops);
492 set_iommu_table_base(&pdev->dev, &pe->tce32_table);
493 }
Brian W Harta32305b2014-07-31 14:24:37 -0500494 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100495 return 0;
496}
497
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000498static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus)
499{
500 struct pci_dev *dev;
501
502 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +1100503 set_iommu_table_base_and_group(&dev->dev, &pe->tce32_table);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000504 if (dev->subordinate)
505 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
506 }
507}
508
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000509static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
510 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100511 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +0000512{
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100513 __be64 __iomem *invalidate = rm ?
514 (__be64 __iomem *)pe->tce_inval_reg_phys :
515 (__be64 __iomem *)tbl->it_index;
Gavin Shan4cce9552013-04-25 19:21:00 +0000516 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000517 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +0000518
519 start = __pa(startp);
520 end = __pa(endp);
521
522 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
523 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000524 start <<= shift;
525 end <<= shift;
526 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +0000527 start |= tbl->it_busno;
528 end |= tbl->it_busno;
529 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
530 /* p7ioc-style invalidation, 2 TCEs per write */
531 start |= (1ull << 63);
532 end |= (1ull << 63);
533 inc = 16;
534 } else {
535 /* Default (older HW) */
536 inc = 128;
537 }
538
539 end |= inc - 1; /* round up end to be different than start */
540
541 mb(); /* Ensure above stores are visible */
542 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000543 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100544 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000545 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100546 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +0000547 start += inc;
548 }
549
550 /*
551 * The iommu layer will do another mb() for us on build()
552 * and we don't care on free()
553 */
554}
555
556static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
557 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100558 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +0000559{
560 unsigned long start, end, inc;
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100561 __be64 __iomem *invalidate = rm ?
562 (__be64 __iomem *)pe->tce_inval_reg_phys :
563 (__be64 __iomem *)tbl->it_index;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000564 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +0000565
566 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000567 start = 0x2ull << 60;
Gavin Shan4cce9552013-04-25 19:21:00 +0000568 start |= (pe->pe_number & 0xFF);
569 end = start;
570
571 /* Figure out the start, end and step */
572 inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000573 start |= (inc << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +0000574 inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000575 end |= (inc << shift);
576 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +0000577 mb();
578
579 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000580 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100581 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000582 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100583 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +0000584 start += inc;
585 }
586}
587
588void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100589 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +0000590{
591 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
592 tce32_table);
593 struct pnv_phb *phb = pe->phb;
594
595 if (phb->type == PNV_PHB_IODA1)
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000596 pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +0000597 else
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000598 pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +0000599}
600
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800601static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
602 struct pnv_ioda_pe *pe, unsigned int base,
603 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000604{
605
606 struct page *tce_mem = NULL;
607 const __be64 *swinvp;
608 struct iommu_table *tbl;
609 unsigned int i;
610 int64_t rc;
611 void *addr;
612
613 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
614#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
615
616 /* XXX FIXME: Handle 64-bit only DMA devices */
617 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
618 /* XXX FIXME: Allocate multi-level tables on PHB3 */
619
620 /* We shouldn't already have a 32-bit DMA associated */
621 if (WARN_ON(pe->tce32_seg >= 0))
622 return;
623
624 /* Grab a 32-bit TCE table */
625 pe->tce32_seg = base;
626 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
627 (base << 28), ((base + segs) << 28) - 1);
628
629 /* XXX Currently, we allocate one big contiguous table for the
630 * TCEs. We only really need one chunk per 256M of TCE space
631 * (ie per segment) but that's an optimization for later, it
632 * requires some added smarts with our get/put_tce implementation
633 */
634 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
635 get_order(TCE32_TABLE_SIZE * segs));
636 if (!tce_mem) {
637 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
638 goto fail;
639 }
640 addr = page_address(tce_mem);
641 memset(addr, 0, TCE32_TABLE_SIZE * segs);
642
643 /* Configure HW */
644 for (i = 0; i < segs; i++) {
645 rc = opal_pci_map_pe_dma_window(phb->opal_id,
646 pe->pe_number,
647 base + i, 1,
648 __pa(addr) + TCE32_TABLE_SIZE * i,
649 TCE32_TABLE_SIZE, 0x1000);
650 if (rc) {
651 pe_err(pe, " Failed to configure 32-bit TCE table,"
652 " err %ld\n", rc);
653 goto fail;
654 }
655 }
656
657 /* Setup linux iommu table */
658 tbl = &pe->tce32_table;
659 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +1000660 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000661
662 /* OPAL variant of P7IOC SW invalidated TCEs */
663 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
664 if (swinvp) {
665 /* We need a couple more fields -- an address and a data
666 * to or. Since the bus is only printed out on table free
667 * errors, and on the first pass the data will be a relative
668 * bus number, print that out instead.
669 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000670 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
671 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
672 8);
Gavin Shan65fd7662014-04-24 18:00:28 +1000673 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
674 TCE_PCI_SWINV_FREE |
675 TCE_PCI_SWINV_PAIR);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000676 }
677 iommu_init_table(tbl, phb->hose->node);
Gavin Shane9bc03f2014-04-24 18:00:29 +1000678 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000679
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000680 if (pe->pdev)
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +1100681 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000682 else
683 pnv_ioda_setup_bus_dma(pe, pe->pbus);
684
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000685 return;
686 fail:
687 /* XXX Failure: Try to fallback to 64-bit only ? */
688 if (pe->tce32_seg >= 0)
689 pe->tce32_seg = -1;
690 if (tce_mem)
691 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
692}
693
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100694static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
695{
696 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
697 tce32_table);
698 uint16_t window_id = (pe->pe_number << 1 ) + 1;
699 int64_t rc;
700
701 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
702 if (enable) {
703 phys_addr_t top = memblock_end_of_DRAM();
704
705 top = roundup_pow_of_two(top);
706 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
707 pe->pe_number,
708 window_id,
709 pe->tce_bypass_base,
710 top);
711 } else {
712 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
713 pe->pe_number,
714 window_id,
715 pe->tce_bypass_base,
716 0);
717
718 /*
719 * We might want to reset the DMA ops of all devices on
720 * this PE. However in theory, that shouldn't be necessary
721 * as this is used for VFIO/KVM pass-through and the device
722 * hasn't yet been returned to its kernel driver
723 */
724 }
725 if (rc)
726 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
727 else
728 pe->tce_bypass_enabled = enable;
729}
730
731static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
732 struct pnv_ioda_pe *pe)
733{
734 /* TVE #1 is selected by PCI address bit 59 */
735 pe->tce_bypass_base = 1ull << 59;
736
737 /* Install set_bypass callback for VFIO */
738 pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass;
739
740 /* Enable bypass by default */
741 pnv_pci_ioda2_set_bypass(&pe->tce32_table, true);
742}
743
Gavin Shan373f5652013-04-25 19:21:01 +0000744static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
745 struct pnv_ioda_pe *pe)
746{
747 struct page *tce_mem = NULL;
748 void *addr;
749 const __be64 *swinvp;
750 struct iommu_table *tbl;
751 unsigned int tce_table_size, end;
752 int64_t rc;
753
754 /* We shouldn't already have a 32-bit DMA associated */
755 if (WARN_ON(pe->tce32_seg >= 0))
756 return;
757
758 /* The PE will reserve all possible 32-bits space */
759 pe->tce32_seg = 0;
760 end = (1 << ilog2(phb->ioda.m32_pci_base));
761 tce_table_size = (end / 0x1000) * 8;
762 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
763 end);
764
765 /* Allocate TCE table */
766 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
767 get_order(tce_table_size));
768 if (!tce_mem) {
769 pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
770 goto fail;
771 }
772 addr = page_address(tce_mem);
773 memset(addr, 0, tce_table_size);
774
775 /*
776 * Map TCE table through TVT. The TVE index is the PE number
777 * shifted by 1 bit for 32-bits DMA space.
778 */
779 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
780 pe->pe_number << 1, 1, __pa(addr),
781 tce_table_size, 0x1000);
782 if (rc) {
783 pe_err(pe, "Failed to configure 32-bit TCE table,"
784 " err %ld\n", rc);
785 goto fail;
786 }
787
788 /* Setup linux iommu table */
789 tbl = &pe->tce32_table;
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +1000790 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
791 IOMMU_PAGE_SHIFT_4K);
Gavin Shan373f5652013-04-25 19:21:01 +0000792
793 /* OPAL variant of PHB3 invalidated TCEs */
794 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
795 if (swinvp) {
796 /* We need a couple more fields -- an address and a data
797 * to or. Since the bus is only printed out on table free
798 * errors, and on the first pass the data will be a relative
799 * bus number, print that out instead.
800 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000801 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
802 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
803 8);
Gavin Shan65fd7662014-04-24 18:00:28 +1000804 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
Gavin Shan373f5652013-04-25 19:21:01 +0000805 }
806 iommu_init_table(tbl, phb->hose->node);
Gavin Shane9bc03f2014-04-24 18:00:29 +1000807 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
Gavin Shan373f5652013-04-25 19:21:01 +0000808
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000809 if (pe->pdev)
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +1100810 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000811 else
812 pnv_ioda_setup_bus_dma(pe, pe->pbus);
813
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100814 /* Also create a bypass window */
815 pnv_pci_ioda2_setup_bypass_pe(phb, pe);
Gavin Shan373f5652013-04-25 19:21:01 +0000816 return;
817fail:
818 if (pe->tce32_seg >= 0)
819 pe->tce32_seg = -1;
820 if (tce_mem)
821 __free_pages(tce_mem, get_order(tce_table_size));
822}
823
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800824static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000825{
826 struct pci_controller *hose = phb->hose;
827 unsigned int residual, remaining, segs, tw, base;
828 struct pnv_ioda_pe *pe;
829
830 /* If we have more PE# than segments available, hand out one
831 * per PE until we run out and let the rest fail. If not,
832 * then we assign at least one segment per PE, plus more based
833 * on the amount of devices under that PE
834 */
835 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
836 residual = 0;
837 else
838 residual = phb->ioda.tce32_count -
839 phb->ioda.dma_pe_count;
840
841 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
842 hose->global_number, phb->ioda.tce32_count);
843 pr_info("PCI: %d PE# for a total weight of %d\n",
844 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
845
846 /* Walk our PE list and configure their DMA segments, hand them
847 * out one base segment plus any residual segments based on
848 * weight
849 */
850 remaining = phb->ioda.tce32_count;
851 tw = phb->ioda.dma_weight;
852 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +0000853 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000854 if (!pe->dma_weight)
855 continue;
856 if (!remaining) {
857 pe_warn(pe, "No DMA32 resources available\n");
858 continue;
859 }
860 segs = 1;
861 if (residual) {
862 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
863 if (segs > remaining)
864 segs = remaining;
865 }
Gavin Shan373f5652013-04-25 19:21:01 +0000866
867 /*
868 * For IODA2 compliant PHB3, we needn't care about the weight.
869 * The all available 32-bits DMA space will be assigned to
870 * the specific PE.
871 */
872 if (phb->type == PNV_PHB_IODA1) {
873 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
874 pe->dma_weight, segs);
875 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
876 } else {
877 pe_info(pe, "Assign DMA32 space\n");
878 segs = 0;
879 pnv_pci_ioda2_setup_dma_pe(phb, pe);
880 }
881
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000882 remaining -= segs;
883 base += segs;
884 }
885}
886
887#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +0000888static void pnv_ioda2_msi_eoi(struct irq_data *d)
889{
890 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
891 struct irq_chip *chip = irq_data_get_irq_chip(d);
892 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
893 ioda.irq_chip);
894 int64_t rc;
895
896 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
897 WARN_ON_ONCE(rc);
898
899 icp_native_eoi(d);
900}
901
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000902static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +0000903 unsigned int hwirq, unsigned int virq,
904 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000905{
906 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000907 struct pci_dn *pdn = pci_get_pdn(dev);
Gavin Shan137436c2013-04-25 19:20:59 +0000908 struct irq_data *idata;
909 struct irq_chip *ichip;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000910 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000911 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000912 int rc;
913
914 /* No PE assigned ? bail out ... no MSI for you ! */
915 if (pe == NULL)
916 return -ENXIO;
917
918 /* Check if we have an MVE */
919 if (pe->mve_number < 0)
920 return -ENXIO;
921
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000922 /* Force 32-bit MSI on some broken devices */
923 if (pdn && pdn->force_32bit_msi)
924 is_64 = 0;
925
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000926 /* Assign XIVE to PE */
927 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
928 if (rc) {
929 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
930 pci_name(dev), rc, xive_num);
931 return -EIO;
932 }
933
934 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000935 __be64 addr64;
936
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000937 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
938 &addr64, &data);
939 if (rc) {
940 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
941 pci_name(dev), rc);
942 return -EIO;
943 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000944 msg->address_hi = be64_to_cpu(addr64) >> 32;
945 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000946 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000947 __be32 addr32;
948
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000949 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
950 &addr32, &data);
951 if (rc) {
952 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
953 pci_name(dev), rc);
954 return -EIO;
955 }
956 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000957 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000958 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +1000959 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000960
Gavin Shan137436c2013-04-25 19:20:59 +0000961 /*
962 * Change the IRQ chip for the MSI interrupts on PHB3.
963 * The corresponding IRQ chip should be populated for
964 * the first time.
965 */
966 if (phb->type == PNV_PHB_IODA2) {
967 if (!phb->ioda.irq_chip_init) {
968 idata = irq_get_irq_data(virq);
969 ichip = irq_data_get_irq_chip(idata);
970 phb->ioda.irq_chip_init = 1;
971 phb->ioda.irq_chip = *ichip;
972 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
973 }
974
975 irq_set_chip(virq, &phb->ioda.irq_chip);
976 }
977
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000978 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
979 " address=%x_%08x data=%x PE# %d\n",
980 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
981 msg->address_hi, msg->address_lo, data, pe->pe_number);
982
983 return 0;
984}
985
986static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
987{
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000988 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000989 const __be32 *prop = of_get_property(phb->hose->dn,
990 "ibm,opal-msi-ranges", NULL);
991 if (!prop) {
992 /* BML Fallback */
993 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
994 }
995 if (!prop)
996 return;
997
998 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000999 count = be32_to_cpup(prop + 1);
1000 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001001 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1002 phb->hose->global_number);
1003 return;
1004 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001005
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001006 phb->msi_setup = pnv_pci_ioda_msi_setup;
1007 phb->msi32_support = 1;
1008 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001009 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001010}
1011#else
1012static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1013#endif /* CONFIG_PCI_MSI */
1014
Gavin Shan11685be2012-08-20 03:49:16 +00001015/*
1016 * This function is supposed to be called on basis of PE from top
1017 * to bottom style. So the the I/O or MMIO segment assigned to
1018 * parent PE could be overrided by its child PEs if necessary.
1019 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001020static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
1021 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00001022{
1023 struct pnv_phb *phb = hose->private_data;
1024 struct pci_bus_region region;
1025 struct resource *res;
1026 int i, index;
1027 int rc;
1028
1029 /*
1030 * NOTE: We only care PCI bus based PE for now. For PCI
1031 * device based PE, for example SRIOV sensitive VF should
1032 * be figured out later.
1033 */
1034 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
1035
1036 pci_bus_for_each_resource(pe->pbus, res, i) {
1037 if (!res || !res->flags ||
1038 res->start > res->end)
1039 continue;
1040
1041 if (res->flags & IORESOURCE_IO) {
1042 region.start = res->start - phb->ioda.io_pci_base;
1043 region.end = res->end - phb->ioda.io_pci_base;
1044 index = region.start / phb->ioda.io_segsize;
1045
1046 while (index < phb->ioda.total_pe &&
1047 region.start <= region.end) {
1048 phb->ioda.io_segmap[index] = pe->pe_number;
1049 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1050 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
1051 if (rc != OPAL_SUCCESS) {
1052 pr_err("%s: OPAL error %d when mapping IO "
1053 "segment #%d to PE#%d\n",
1054 __func__, rc, index, pe->pe_number);
1055 break;
1056 }
1057
1058 region.start += phb->ioda.io_segsize;
1059 index++;
1060 }
1061 } else if (res->flags & IORESOURCE_MEM) {
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001062 /* WARNING: Assumes M32 is mem region 0 in PHB. We need to
1063 * harden that algorithm when we start supporting M64
1064 */
Gavin Shan11685be2012-08-20 03:49:16 +00001065 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001066 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00001067 phb->ioda.m32_pci_base;
1068 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001069 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00001070 phb->ioda.m32_pci_base;
1071 index = region.start / phb->ioda.m32_segsize;
1072
1073 while (index < phb->ioda.total_pe &&
1074 region.start <= region.end) {
1075 phb->ioda.m32_segmap[index] = pe->pe_number;
1076 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1077 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
1078 if (rc != OPAL_SUCCESS) {
1079 pr_err("%s: OPAL error %d when mapping M32 "
1080 "segment#%d to PE#%d",
1081 __func__, rc, index, pe->pe_number);
1082 break;
1083 }
1084
1085 region.start += phb->ioda.m32_segsize;
1086 index++;
1087 }
1088 }
1089 }
1090}
1091
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001092static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00001093{
1094 struct pci_controller *tmp, *hose;
1095 struct pnv_phb *phb;
1096 struct pnv_ioda_pe *pe;
1097
1098 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1099 phb = hose->private_data;
1100 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
1101 pnv_ioda_setup_pe_seg(hose, pe);
1102 }
1103 }
1104}
1105
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001106static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00001107{
1108 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00001109 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00001110
1111 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1112 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00001113
1114 /* Mark the PHB initialization done */
1115 phb = hose->private_data;
1116 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00001117 }
1118}
1119
Gavin Shan37c367f2013-06-20 18:13:25 +08001120static void pnv_pci_ioda_create_dbgfs(void)
1121{
1122#ifdef CONFIG_DEBUG_FS
1123 struct pci_controller *hose, *tmp;
1124 struct pnv_phb *phb;
1125 char name[16];
1126
1127 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1128 phb = hose->private_data;
1129
1130 sprintf(name, "PCI%04x", hose->global_number);
1131 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
1132 if (!phb->dbgfs)
1133 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
1134 __func__, hose->global_number);
1135 }
1136#endif /* CONFIG_DEBUG_FS */
1137}
1138
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001139static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001140{
1141 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00001142 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00001143 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08001144
Gavin Shan37c367f2013-06-20 18:13:25 +08001145 pnv_pci_ioda_create_dbgfs();
1146
Gavin Shane9cc17d2013-06-20 13:21:14 +08001147#ifdef CONFIG_EEH
Gavin Shan88b6d142013-06-27 13:46:45 +08001148 eeh_probe_mode_set(EEH_PROBE_MODE_DEV);
Gavin Shane9cc17d2013-06-20 13:21:14 +08001149 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04001150 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08001151#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00001152}
1153
Gavin Shan271fd032012-09-11 16:59:47 -06001154/*
1155 * Returns the alignment for I/O or memory windows for P2P
1156 * bridges. That actually depends on how PEs are segmented.
1157 * For now, we return I/O or M32 segment size for PE sensitive
1158 * P2P bridges. Otherwise, the default values (4KiB for I/O,
1159 * 1MiB for memory) will be returned.
1160 *
1161 * The current PCI bus might be put into one PE, which was
1162 * create against the parent PCI bridge. For that case, we
1163 * needn't enlarge the alignment so that we can save some
1164 * resources.
1165 */
1166static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
1167 unsigned long type)
1168{
1169 struct pci_dev *bridge;
1170 struct pci_controller *hose = pci_bus_to_host(bus);
1171 struct pnv_phb *phb = hose->private_data;
1172 int num_pci_bridges = 0;
1173
1174 bridge = bus->self;
1175 while (bridge) {
1176 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
1177 num_pci_bridges++;
1178 if (num_pci_bridges >= 2)
1179 return 1;
1180 }
1181
1182 bridge = bridge->bus->self;
1183 }
1184
1185 /* We need support prefetchable memory window later */
1186 if (type & IORESOURCE_MEM)
1187 return phb->ioda.m32_segsize;
1188
1189 return phb->ioda.io_segsize;
1190}
1191
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001192/* Prevent enabling devices for which we couldn't properly
1193 * assign a PE
1194 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001195static int pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001196{
Gavin Shandb1266c2012-08-20 03:49:18 +00001197 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1198 struct pnv_phb *phb = hose->private_data;
1199 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001200
Gavin Shandb1266c2012-08-20 03:49:18 +00001201 /* The function is probably called while the PEs have
1202 * not be created yet. For example, resource reassignment
1203 * during PCI probe period. We just skip the check if
1204 * PEs isn't ready.
1205 */
1206 if (!phb->initialized)
1207 return 0;
1208
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001209 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001210 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1211 return -EINVAL;
Gavin Shandb1266c2012-08-20 03:49:18 +00001212
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001213 return 0;
1214}
1215
1216static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1217 u32 devfn)
1218{
1219 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1220}
1221
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001222static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
1223{
1224 opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET,
1225 OPAL_ASSERT_RESET);
1226}
1227
Gavin Shane9cc17d2013-06-20 13:21:14 +08001228void __init pnv_pci_init_ioda_phb(struct device_node *np,
1229 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001230{
1231 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001232 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08001233 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10001234 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001235 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001236 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001237 u64 phb_id;
1238 void *aux;
1239 long rc;
1240
Gavin Shan58d714e2013-07-31 16:47:00 +08001241 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001242
1243 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1244 if (!prop64) {
1245 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
1246 return;
1247 }
1248 phb_id = be64_to_cpup(prop64);
1249 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
1250
1251 phb = alloc_bootmem(sizeof(struct pnv_phb));
Gavin Shan58d714e2013-07-31 16:47:00 +08001252 if (!phb) {
1253 pr_err(" Out of memory !\n");
1254 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001255 }
Gavin Shan58d714e2013-07-31 16:47:00 +08001256
1257 /* Allocate PCI controller */
1258 memset(phb, 0, sizeof(struct pnv_phb));
1259 phb->hose = hose = pcibios_alloc_controller(np);
1260 if (!phb->hose) {
1261 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001262 np->full_name);
Gavin Shan58d714e2013-07-31 16:47:00 +08001263 free_bootmem((unsigned long)phb, sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001264 return;
1265 }
1266
1267 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001268 prop32 = of_get_property(np, "bus-range", &len);
1269 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001270 hose->first_busno = be32_to_cpu(prop32[0]);
1271 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001272 } else {
1273 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
1274 hose->first_busno = 0;
1275 hose->last_busno = 0xff;
1276 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001277 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08001278 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001279 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00001280 phb->type = ioda_type;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001281
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00001282 /* Detect specific models for error handling */
1283 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
1284 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00001285 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00001286 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00001287 else
1288 phb->model = PNV_PHB_MODEL_UNKNOWN;
1289
Gavin Shanaa0c0332013-04-25 19:20:57 +00001290 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08001291 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001292
Gavin Shanaa0c0332013-04-25 19:20:57 +00001293 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001294 phb->regs = of_iomap(np, 0);
1295 if (phb->regs == NULL)
1296 pr_err(" Failed to map registers !\n");
1297
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001298 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08001299 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00001300 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08001301 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001302 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08001303 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
1304 if (prop32)
1305 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001306 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00001307 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001308 phb->ioda.m32_size += 0x10000;
1309
1310 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001311 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001312 phb->ioda.io_size = hose->pci_io_size;
1313 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
1314 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
1315
Gavin Shanc35d2a82013-07-31 16:47:04 +08001316 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001317 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
1318 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00001319 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08001320 if (phb->type == PNV_PHB_IODA1) {
1321 iomap_off = size;
1322 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
1323 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001324 pemap_off = size;
1325 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
1326 aux = alloc_bootmem(size);
1327 memset(aux, 0, size);
1328 phb->ioda.pe_alloc = aux;
1329 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08001330 if (phb->type == PNV_PHB_IODA1)
1331 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001332 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08001333 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001334
Gavin Shan7ebdf952012-08-20 03:49:15 +00001335 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001336 INIT_LIST_HEAD(&phb->ioda.pe_list);
1337
1338 /* Calculate how many 32-bit TCE segments we have */
1339 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
1340
1341 /* Clear unusable m64 */
1342 hose->mem_resources[1].flags = 0;
1343 hose->mem_resources[1].start = 0;
1344 hose->mem_resources[1].end = 0;
1345 hose->mem_resources[2].flags = 0;
1346 hose->mem_resources[2].start = 0;
1347 hose->mem_resources[2].end = 0;
1348
Gavin Shanaa0c0332013-04-25 19:20:57 +00001349#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001350 rc = opal_pci_set_phb_mem_window(opal->phb_id,
1351 window_type,
1352 window_num,
1353 starting_real_address,
1354 starting_pci_address,
1355 segment_size);
1356#endif
1357
Gavin Shan36954dc2013-11-04 16:32:47 +08001358 pr_info(" %d (%d) PE's M32: 0x%x [segment=0x%x]"
1359 " IO: 0x%x [segment=0x%x]\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001360 phb->ioda.total_pe,
Gavin Shan36954dc2013-11-04 16:32:47 +08001361 phb->ioda.reserved_pe,
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001362 phb->ioda.m32_size, phb->ioda.m32_segsize,
1363 phb->ioda.io_size, phb->ioda.io_segsize);
1364
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001365 phb->hose->ops = &pnv_pci_ops;
Gavin Shane9cc17d2013-06-20 13:21:14 +08001366#ifdef CONFIG_EEH
1367 phb->eeh_ops = &ioda_eeh_ops;
1368#endif
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001369
1370 /* Setup RID -> PE mapping function */
1371 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
1372
1373 /* Setup TCEs */
1374 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001375 phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001376
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001377 /* Setup shutdown function for kexec */
1378 phb->shutdown = pnv_pci_ioda_shutdown;
1379
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001380 /* Setup MSI support */
1381 pnv_pci_init_ioda_msis(phb);
1382
Gavin Shanc40a4212012-08-20 03:49:20 +00001383 /*
1384 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
1385 * to let the PCI core do resource assignment. It's supposed
1386 * that the PCI core will do correct I/O and MMIO alignment
1387 * for the P2P bridge bars so that each PCI bus (excluding
1388 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001389 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00001390 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001391 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
Gavin Shan271fd032012-09-11 16:59:47 -06001392 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
Gavin Shand92a2082014-04-24 18:00:24 +10001393 ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
Gavin Shanc40a4212012-08-20 03:49:20 +00001394 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001395
1396 /* Reset IODA tables to a clean state */
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00001397 rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001398 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00001399 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10001400
1401 /* If we're running in kdump kerenl, the previous kerenl never
1402 * shutdown PCI devices correctly. We already got IODA table
1403 * cleaned out. So we have to issue PHB reset to stop all PCI
1404 * transactions from previous kerenl.
1405 */
1406 if (is_kdump_kernel()) {
1407 pr_info(" Issue PHB reset ...\n");
1408 ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
1409 ioda_eeh_phb_reset(hose, OPAL_DEASSERT_RESET);
1410 }
Gavin Shanaa0c0332013-04-25 19:20:57 +00001411}
1412
Bjorn Helgaas67975002013-07-02 12:20:03 -06001413void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00001414{
Gavin Shane9cc17d2013-06-20 13:21:14 +08001415 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001416}
1417
1418void __init pnv_pci_init_ioda_hub(struct device_node *np)
1419{
1420 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10001421 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001422 u64 hub_id;
1423
1424 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
1425
1426 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
1427 if (!prop64) {
1428 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
1429 return;
1430 }
1431 hub_id = be64_to_cpup(prop64);
1432 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
1433
1434 /* Count child PHBs */
1435 for_each_child_of_node(np, phbn) {
1436 /* Look for IODA1 PHBs */
1437 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08001438 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001439 }
1440}