blob: 899fe4049b44c1e4ddbd7027765a94c8f4fc2532 [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>
Guo Chao262af552014-07-21 14:42:30 +100039#include <asm/firmware.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000040
41#include "powernv.h"
42#include "pci.h"
43
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000044#define define_pe_printk_level(func, kern_level) \
45static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
46{ \
47 struct va_format vaf; \
48 va_list args; \
Gavin Shan490e0782012-10-17 19:53:30 +000049 char pfix[32]; \
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000050 int r; \
51 \
52 va_start(args, fmt); \
53 \
54 vaf.fmt = fmt; \
55 vaf.va = &args; \
56 \
Gavin Shan490e0782012-10-17 19:53:30 +000057 if (pe->pdev) \
58 strlcpy(pfix, dev_name(&pe->pdev->dev), \
59 sizeof(pfix)); \
60 else \
61 sprintf(pfix, "%04x:%02x ", \
62 pci_domain_nr(pe->pbus), \
63 pe->pbus->number); \
64 r = printk(kern_level "pci %s: [PE# %.3d] %pV", \
65 pfix, pe->pe_number, &vaf); \
66 \
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000067 va_end(args); \
68 \
69 return r; \
70} \
71
72define_pe_printk_level(pe_err, KERN_ERR);
73define_pe_printk_level(pe_warn, KERN_WARNING);
74define_pe_printk_level(pe_info, KERN_INFO);
75
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +100076/*
77 * stdcix is only supposed to be used in hypervisor real mode as per
78 * the architecture spec
79 */
80static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
81{
82 __asm__ __volatile__("stdcix %0,0,%1"
83 : : "r" (val), "r" (paddr) : "memory");
84}
85
Guo Chao262af552014-07-21 14:42:30 +100086static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
87{
88 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
89 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
90}
91
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -080092static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000093{
94 unsigned long pe;
95
96 do {
97 pe = find_next_zero_bit(phb->ioda.pe_alloc,
98 phb->ioda.total_pe, 0);
99 if (pe >= phb->ioda.total_pe)
100 return IODA_INVALID_PE;
101 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
102
Gavin Shan4cce9552013-04-25 19:21:00 +0000103 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000104 phb->ioda.pe_array[pe].pe_number = pe;
105 return pe;
106}
107
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800108static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000109{
110 WARN_ON(phb->ioda.pe_array[pe].pdev);
111
112 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
113 clear_bit(pe, phb->ioda.pe_alloc);
114}
115
Guo Chao262af552014-07-21 14:42:30 +1000116/* The default M64 BAR is shared by all PEs */
117static int pnv_ioda2_init_m64(struct pnv_phb *phb)
118{
119 const char *desc;
120 struct resource *r;
121 s64 rc;
122
123 /* Configure the default M64 BAR */
124 rc = opal_pci_set_phb_mem_window(phb->opal_id,
125 OPAL_M64_WINDOW_TYPE,
126 phb->ioda.m64_bar_idx,
127 phb->ioda.m64_base,
128 0, /* unused */
129 phb->ioda.m64_size);
130 if (rc != OPAL_SUCCESS) {
131 desc = "configuring";
132 goto fail;
133 }
134
135 /* Enable the default M64 BAR */
136 rc = opal_pci_phb_mmio_enable(phb->opal_id,
137 OPAL_M64_WINDOW_TYPE,
138 phb->ioda.m64_bar_idx,
139 OPAL_ENABLE_M64_SPLIT);
140 if (rc != OPAL_SUCCESS) {
141 desc = "enabling";
142 goto fail;
143 }
144
145 /* Mark the M64 BAR assigned */
146 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
147
148 /*
149 * Strip off the segment used by the reserved PE, which is
150 * expected to be 0 or last one of PE capabicity.
151 */
152 r = &phb->hose->mem_resources[1];
153 if (phb->ioda.reserved_pe == 0)
154 r->start += phb->ioda.m64_segsize;
155 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
156 r->end -= phb->ioda.m64_segsize;
157 else
158 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
159 phb->ioda.reserved_pe);
160
161 return 0;
162
163fail:
164 pr_warn(" Failure %lld %s M64 BAR#%d\n",
165 rc, desc, phb->ioda.m64_bar_idx);
166 opal_pci_phb_mmio_enable(phb->opal_id,
167 OPAL_M64_WINDOW_TYPE,
168 phb->ioda.m64_bar_idx,
169 OPAL_DISABLE_M64);
170 return -EIO;
171}
172
173static void pnv_ioda2_alloc_m64_pe(struct pnv_phb *phb)
174{
175 resource_size_t sgsz = phb->ioda.m64_segsize;
176 struct pci_dev *pdev;
177 struct resource *r;
178 int base, step, i;
179
180 /*
181 * Root bus always has full M64 range and root port has
182 * M64 range used in reality. So we're checking root port
183 * instead of root bus.
184 */
185 list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
186 for (i = PCI_BRIDGE_RESOURCES;
187 i <= PCI_BRIDGE_RESOURCE_END; i++) {
188 r = &pdev->resource[i];
189 if (!r->parent ||
190 !pnv_pci_is_mem_pref_64(r->flags))
191 continue;
192
193 base = (r->start - phb->ioda.m64_base) / sgsz;
194 for (step = 0; step < resource_size(r) / sgsz; step++)
195 set_bit(base + step, phb->ioda.pe_alloc);
196 }
197 }
198}
199
200static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
201 struct pci_bus *bus, int all)
202{
203 resource_size_t segsz = phb->ioda.m64_segsize;
204 struct pci_dev *pdev;
205 struct resource *r;
206 struct pnv_ioda_pe *master_pe, *pe;
207 unsigned long size, *pe_alloc;
208 bool found;
209 int start, i, j;
210
211 /* Root bus shouldn't use M64 */
212 if (pci_is_root_bus(bus))
213 return IODA_INVALID_PE;
214
215 /* We support only one M64 window on each bus */
216 found = false;
217 pci_bus_for_each_resource(bus, r, i) {
218 if (r && r->parent &&
219 pnv_pci_is_mem_pref_64(r->flags)) {
220 found = true;
221 break;
222 }
223 }
224
225 /* No M64 window found ? */
226 if (!found)
227 return IODA_INVALID_PE;
228
229 /* Allocate bitmap */
230 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
231 pe_alloc = kzalloc(size, GFP_KERNEL);
232 if (!pe_alloc) {
233 pr_warn("%s: Out of memory !\n",
234 __func__);
235 return IODA_INVALID_PE;
236 }
237
238 /*
239 * Figure out reserved PE numbers by the PE
240 * the its child PEs.
241 */
242 start = (r->start - phb->ioda.m64_base) / segsz;
243 for (i = 0; i < resource_size(r) / segsz; i++)
244 set_bit(start + i, pe_alloc);
245
246 if (all)
247 goto done;
248
249 /*
250 * If the PE doesn't cover all subordinate buses,
251 * we need subtract from reserved PEs for children.
252 */
253 list_for_each_entry(pdev, &bus->devices, bus_list) {
254 if (!pdev->subordinate)
255 continue;
256
257 pci_bus_for_each_resource(pdev->subordinate, r, i) {
258 if (!r || !r->parent ||
259 !pnv_pci_is_mem_pref_64(r->flags))
260 continue;
261
262 start = (r->start - phb->ioda.m64_base) / segsz;
263 for (j = 0; j < resource_size(r) / segsz ; j++)
264 clear_bit(start + j, pe_alloc);
265 }
266 }
267
268 /*
269 * the current bus might not own M64 window and that's all
270 * contributed by its child buses. For the case, we needn't
271 * pick M64 dependent PE#.
272 */
273 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
274 kfree(pe_alloc);
275 return IODA_INVALID_PE;
276 }
277
278 /*
279 * Figure out the master PE and put all slave PEs to master
280 * PE's list to form compound PE.
281 */
282done:
283 master_pe = NULL;
284 i = -1;
285 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
286 phb->ioda.total_pe) {
287 pe = &phb->ioda.pe_array[i];
288 pe->phb = phb;
289 pe->pe_number = i;
290
291 if (!master_pe) {
292 pe->flags |= PNV_IODA_PE_MASTER;
293 INIT_LIST_HEAD(&pe->slaves);
294 master_pe = pe;
295 } else {
296 pe->flags |= PNV_IODA_PE_SLAVE;
297 pe->master = master_pe;
298 list_add_tail(&pe->list, &master_pe->slaves);
299 }
300 }
301
302 kfree(pe_alloc);
303 return master_pe->pe_number;
304}
305
306static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
307{
308 struct pci_controller *hose = phb->hose;
309 struct device_node *dn = hose->dn;
310 struct resource *res;
311 const u32 *r;
312 u64 pci_addr;
313
314 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
315 pr_info(" Firmware too old to support M64 window\n");
316 return;
317 }
318
319 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
320 if (!r) {
321 pr_info(" No <ibm,opal-m64-window> on %s\n",
322 dn->full_name);
323 return;
324 }
325
326 /* FIXME: Support M64 for P7IOC */
327 if (phb->type != PNV_PHB_IODA2) {
328 pr_info(" Not support M64 window\n");
329 return;
330 }
331
332 res = &hose->mem_resources[1];
333 res->start = of_translate_address(dn, r + 2);
334 res->end = res->start + of_read_number(r + 4, 2) - 1;
335 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
336 pci_addr = of_read_number(r, 2);
337 hose->mem_offset[1] = res->start - pci_addr;
338
339 phb->ioda.m64_size = resource_size(res);
340 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
341 phb->ioda.m64_base = pci_addr;
342
343 /* Use last M64 BAR to cover M64 window */
344 phb->ioda.m64_bar_idx = 15;
345 phb->init_m64 = pnv_ioda2_init_m64;
346 phb->alloc_m64_pe = pnv_ioda2_alloc_m64_pe;
347 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
348}
349
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000350/* Currently those 2 are only used when MSIs are enabled, this will change
351 * but in the meantime, we need to protect them to avoid warnings
352 */
353#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800354static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000355{
356 struct pci_controller *hose = pci_bus_to_host(dev->bus);
357 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000358 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000359
360 if (!pdn)
361 return NULL;
362 if (pdn->pe_number == IODA_INVALID_PE)
363 return NULL;
364 return &phb->ioda.pe_array[pdn->pe_number];
365}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000366#endif /* CONFIG_PCI_MSI */
367
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800368static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000369{
370 struct pci_dev *parent;
371 uint8_t bcomp, dcomp, fcomp;
372 long rc, rid_end, rid;
373
374 /* Bus validation ? */
375 if (pe->pbus) {
376 int count;
377
378 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
379 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
380 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000381 if (pe->flags & PNV_IODA_PE_BUS_ALL)
382 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
383 else
384 count = 1;
385
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000386 switch(count) {
387 case 1: bcomp = OpalPciBusAll; break;
388 case 2: bcomp = OpalPciBus7Bits; break;
389 case 4: bcomp = OpalPciBus6Bits; break;
390 case 8: bcomp = OpalPciBus5Bits; break;
391 case 16: bcomp = OpalPciBus4Bits; break;
392 case 32: bcomp = OpalPciBus3Bits; break;
393 default:
394 pr_err("%s: Number of subordinate busses %d"
395 " unsupported\n",
396 pci_name(pe->pbus->self), count);
397 /* Do an exact match only */
398 bcomp = OpalPciBusAll;
399 }
400 rid_end = pe->rid + (count << 8);
401 } else {
402 parent = pe->pdev->bus->self;
403 bcomp = OpalPciBusAll;
404 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
405 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
406 rid_end = pe->rid + 1;
407 }
408
Gavin Shan631ad692013-11-04 16:32:46 +0800409 /*
410 * Associate PE in PELT. We need add the PE into the
411 * corresponding PELT-V as well. Otherwise, the error
412 * originated from the PE might contribute to other
413 * PEs.
414 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000415 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
416 bcomp, dcomp, fcomp, OPAL_MAP_PE);
417 if (rc) {
418 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
419 return -ENXIO;
420 }
Gavin Shan631ad692013-11-04 16:32:46 +0800421
422 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
423 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
424 if (rc)
425 pe_warn(pe, "OPAL error %d adding self to PELTV\n", rc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000426 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
427 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
428
429 /* Add to all parents PELT-V */
430 while (parent) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000431 struct pci_dn *pdn = pci_get_pdn(parent);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000432 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
433 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000434 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000435 /* XXX What to do in case of error ? */
436 }
437 parent = parent->bus->self;
438 }
439 /* Setup reverse map */
440 for (rid = pe->rid; rid < rid_end; rid++)
441 phb->ioda.pe_rmap[rid] = pe->pe_number;
442
443 /* Setup one MVTs on IODA1 */
444 if (phb->type == PNV_PHB_IODA1) {
445 pe->mve_number = pe->pe_number;
446 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
447 pe->pe_number);
448 if (rc) {
449 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
450 rc, pe->mve_number);
451 pe->mve_number = -1;
452 } else {
453 rc = opal_pci_set_mve_enable(phb->opal_id,
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000454 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000455 if (rc) {
456 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
457 rc, pe->mve_number);
458 pe->mve_number = -1;
459 }
460 }
461 } else if (phb->type == PNV_PHB_IODA2)
462 pe->mve_number = 0;
463
464 return 0;
465}
466
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800467static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
468 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000469{
470 struct pnv_ioda_pe *lpe;
471
Gavin Shan7ebdf952012-08-20 03:49:15 +0000472 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000473 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000474 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000475 return;
476 }
477 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000478 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000479}
480
481static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
482{
483 /* This is quite simplistic. The "base" weight of a device
484 * is 10. 0 means no DMA is to be accounted for it.
485 */
486
487 /* If it's a bridge, no DMA */
488 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
489 return 0;
490
491 /* Reduce the weight of slow USB controllers */
492 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
493 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
494 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
495 return 3;
496
497 /* Increase the weight of RAID (includes Obsidian) */
498 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
499 return 15;
500
501 /* Default */
502 return 10;
503}
504
Gavin Shanfb446ad2012-08-20 03:49:14 +0000505#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800506static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000507{
508 struct pci_controller *hose = pci_bus_to_host(dev->bus);
509 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000510 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000511 struct pnv_ioda_pe *pe;
512 int pe_num;
513
514 if (!pdn) {
515 pr_err("%s: Device tree node not associated properly\n",
516 pci_name(dev));
517 return NULL;
518 }
519 if (pdn->pe_number != IODA_INVALID_PE)
520 return NULL;
521
522 /* PE#0 has been pre-set */
523 if (dev->bus->number == 0)
524 pe_num = 0;
525 else
526 pe_num = pnv_ioda_alloc_pe(phb);
527 if (pe_num == IODA_INVALID_PE) {
528 pr_warning("%s: Not enough PE# available, disabling device\n",
529 pci_name(dev));
530 return NULL;
531 }
532
533 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
534 * pointer in the PE data structure, both should be destroyed at the
535 * same time. However, this needs to be looked at more closely again
536 * once we actually start removing things (Hotplug, SR-IOV, ...)
537 *
538 * At some point we want to remove the PDN completely anyways
539 */
540 pe = &phb->ioda.pe_array[pe_num];
541 pci_dev_get(dev);
542 pdn->pcidev = dev;
543 pdn->pe_number = pe_num;
544 pe->pdev = dev;
545 pe->pbus = NULL;
546 pe->tce32_seg = -1;
547 pe->mve_number = -1;
548 pe->rid = dev->bus->number << 8 | pdn->devfn;
549
550 pe_info(pe, "Associated device to PE\n");
551
552 if (pnv_ioda_configure_pe(phb, pe)) {
553 /* XXX What do we do here ? */
554 if (pe_num)
555 pnv_ioda_free_pe(phb, pe_num);
556 pdn->pe_number = IODA_INVALID_PE;
557 pe->pdev = NULL;
558 pci_dev_put(dev);
559 return NULL;
560 }
561
562 /* Assign a DMA weight to the device */
563 pe->dma_weight = pnv_ioda_dma_weight(dev);
564 if (pe->dma_weight != 0) {
565 phb->ioda.dma_weight += pe->dma_weight;
566 phb->ioda.dma_pe_count++;
567 }
568
569 /* Link the PE */
570 pnv_ioda_link_pe_by_weight(phb, pe);
571
572 return pe;
573}
Gavin Shanfb446ad2012-08-20 03:49:14 +0000574#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000575
576static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
577{
578 struct pci_dev *dev;
579
580 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000581 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000582
583 if (pdn == NULL) {
584 pr_warn("%s: No device node associated with device !\n",
585 pci_name(dev));
586 continue;
587 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000588 pdn->pcidev = dev;
589 pdn->pe_number = pe->pe_number;
590 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +0000591 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000592 pnv_ioda_setup_same_PE(dev->subordinate, pe);
593 }
594}
595
Gavin Shanfb446ad2012-08-20 03:49:14 +0000596/*
597 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
598 * single PCI bus. Another one that contains the primary PCI bus and its
599 * subordinate PCI devices and buses. The second type of PE is normally
600 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
601 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800602static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000603{
Gavin Shanfb446ad2012-08-20 03:49:14 +0000604 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000605 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000606 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +1000607 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000608
Guo Chao262af552014-07-21 14:42:30 +1000609 /* Check if PE is determined by M64 */
610 if (phb->pick_m64_pe)
611 pe_num = phb->pick_m64_pe(phb, bus, all);
612
613 /* The PE number isn't pinned by M64 */
614 if (pe_num == IODA_INVALID_PE)
615 pe_num = pnv_ioda_alloc_pe(phb);
616
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000617 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000618 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
619 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000620 return;
621 }
622
623 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +1000624 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000625 pe->pbus = bus;
626 pe->pdev = NULL;
627 pe->tce32_seg = -1;
628 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -0700629 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000630 pe->dma_weight = 0;
631
Gavin Shanfb446ad2012-08-20 03:49:14 +0000632 if (all)
633 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
634 bus->busn_res.start, bus->busn_res.end, pe_num);
635 else
636 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
637 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000638
639 if (pnv_ioda_configure_pe(phb, pe)) {
640 /* XXX What do we do here ? */
641 if (pe_num)
642 pnv_ioda_free_pe(phb, pe_num);
643 pe->pbus = NULL;
644 return;
645 }
646
647 /* Associate it with all child devices */
648 pnv_ioda_setup_same_PE(bus, pe);
649
Gavin Shan7ebdf952012-08-20 03:49:15 +0000650 /* Put PE to the list */
651 list_add_tail(&pe->list, &phb->ioda.pe_list);
652
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000653 /* Account for one DMA PE if at least one DMA capable device exist
654 * below the bridge
655 */
656 if (pe->dma_weight != 0) {
657 phb->ioda.dma_weight += pe->dma_weight;
658 phb->ioda.dma_pe_count++;
659 }
660
661 /* Link the PE */
662 pnv_ioda_link_pe_by_weight(phb, pe);
663}
664
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800665static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000666{
667 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000668
669 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000670
671 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000672 if (dev->subordinate) {
673 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
674 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
675 else
676 pnv_ioda_setup_PEs(dev->subordinate);
677 }
678 }
679}
680
681/*
682 * Configure PEs so that the downstream PCI buses and devices
683 * could have their associated PE#. Unfortunately, we didn't
684 * figure out the way to identify the PLX bridge yet. So we
685 * simply put the PCI bus and the subordinate behind the root
686 * port to PE# here. The game rule here is expected to be changed
687 * as soon as we can detected PLX bridge correctly.
688 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800689static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +0000690{
691 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +1000692 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000693
694 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +1000695 phb = hose->private_data;
696
697 /* M64 layout might affect PE allocation */
698 if (phb->alloc_m64_pe)
699 phb->alloc_m64_pe(phb);
700
Gavin Shanfb446ad2012-08-20 03:49:14 +0000701 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000702 }
703}
704
Gavin Shan959c9bd2013-04-25 19:21:02 +0000705static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000706{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000707 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +0000708 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000709
Gavin Shan959c9bd2013-04-25 19:21:02 +0000710 /*
711 * The function can be called while the PE#
712 * hasn't been assigned. Do nothing for the
713 * case.
714 */
715 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
716 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000717
Gavin Shan959c9bd2013-04-25 19:21:02 +0000718 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100719 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Wei Yang3f28c5a2014-04-23 10:26:32 +0800720 set_iommu_table_base(&pdev->dev, &pe->tce32_table);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000721}
722
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100723static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
724 struct pci_dev *pdev, u64 dma_mask)
725{
726 struct pci_dn *pdn = pci_get_pdn(pdev);
727 struct pnv_ioda_pe *pe;
728 uint64_t top;
729 bool bypass = false;
730
731 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
732 return -ENODEV;;
733
734 pe = &phb->ioda.pe_array[pdn->pe_number];
735 if (pe->tce_bypass_enabled) {
736 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
737 bypass = (dma_mask >= top);
738 }
739
740 if (bypass) {
741 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
742 set_dma_ops(&pdev->dev, &dma_direct_ops);
743 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
744 } else {
745 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
746 set_dma_ops(&pdev->dev, &dma_iommu_ops);
747 set_iommu_table_base(&pdev->dev, &pe->tce32_table);
748 }
Brian W Harta32305b2014-07-31 14:24:37 -0500749 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100750 return 0;
751}
752
Gavin Shandff4a392014-07-15 17:00:55 +1000753static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
754 struct pci_bus *bus,
755 bool add_to_iommu_group)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000756{
757 struct pci_dev *dev;
758
759 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shandff4a392014-07-15 17:00:55 +1000760 if (add_to_iommu_group)
761 set_iommu_table_base_and_group(&dev->dev,
762 &pe->tce32_table);
763 else
764 set_iommu_table_base(&dev->dev, &pe->tce32_table);
765
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000766 if (dev->subordinate)
Gavin Shandff4a392014-07-15 17:00:55 +1000767 pnv_ioda_setup_bus_dma(pe, dev->subordinate,
768 add_to_iommu_group);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000769 }
770}
771
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000772static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
773 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100774 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +0000775{
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100776 __be64 __iomem *invalidate = rm ?
777 (__be64 __iomem *)pe->tce_inval_reg_phys :
778 (__be64 __iomem *)tbl->it_index;
Gavin Shan4cce9552013-04-25 19:21:00 +0000779 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000780 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +0000781
782 start = __pa(startp);
783 end = __pa(endp);
784
785 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
786 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000787 start <<= shift;
788 end <<= shift;
789 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +0000790 start |= tbl->it_busno;
791 end |= tbl->it_busno;
792 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
793 /* p7ioc-style invalidation, 2 TCEs per write */
794 start |= (1ull << 63);
795 end |= (1ull << 63);
796 inc = 16;
797 } else {
798 /* Default (older HW) */
799 inc = 128;
800 }
801
802 end |= inc - 1; /* round up end to be different than start */
803
804 mb(); /* Ensure above stores are visible */
805 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000806 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100807 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000808 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100809 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +0000810 start += inc;
811 }
812
813 /*
814 * The iommu layer will do another mb() for us on build()
815 * and we don't care on free()
816 */
817}
818
819static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
820 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100821 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +0000822{
823 unsigned long start, end, inc;
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100824 __be64 __iomem *invalidate = rm ?
825 (__be64 __iomem *)pe->tce_inval_reg_phys :
826 (__be64 __iomem *)tbl->it_index;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000827 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +0000828
829 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000830 start = 0x2ull << 60;
Gavin Shan4cce9552013-04-25 19:21:00 +0000831 start |= (pe->pe_number & 0xFF);
832 end = start;
833
834 /* Figure out the start, end and step */
835 inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000836 start |= (inc << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +0000837 inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +1000838 end |= (inc << shift);
839 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +0000840 mb();
841
842 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000843 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100844 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000845 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100846 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +0000847 start += inc;
848 }
849}
850
851void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +1100852 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +0000853{
854 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
855 tce32_table);
856 struct pnv_phb *phb = pe->phb;
857
858 if (phb->type == PNV_PHB_IODA1)
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000859 pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +0000860 else
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000861 pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +0000862}
863
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800864static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
865 struct pnv_ioda_pe *pe, unsigned int base,
866 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000867{
868
869 struct page *tce_mem = NULL;
870 const __be64 *swinvp;
871 struct iommu_table *tbl;
872 unsigned int i;
873 int64_t rc;
874 void *addr;
875
876 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
877#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
878
879 /* XXX FIXME: Handle 64-bit only DMA devices */
880 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
881 /* XXX FIXME: Allocate multi-level tables on PHB3 */
882
883 /* We shouldn't already have a 32-bit DMA associated */
884 if (WARN_ON(pe->tce32_seg >= 0))
885 return;
886
887 /* Grab a 32-bit TCE table */
888 pe->tce32_seg = base;
889 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
890 (base << 28), ((base + segs) << 28) - 1);
891
892 /* XXX Currently, we allocate one big contiguous table for the
893 * TCEs. We only really need one chunk per 256M of TCE space
894 * (ie per segment) but that's an optimization for later, it
895 * requires some added smarts with our get/put_tce implementation
896 */
897 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
898 get_order(TCE32_TABLE_SIZE * segs));
899 if (!tce_mem) {
900 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
901 goto fail;
902 }
903 addr = page_address(tce_mem);
904 memset(addr, 0, TCE32_TABLE_SIZE * segs);
905
906 /* Configure HW */
907 for (i = 0; i < segs; i++) {
908 rc = opal_pci_map_pe_dma_window(phb->opal_id,
909 pe->pe_number,
910 base + i, 1,
911 __pa(addr) + TCE32_TABLE_SIZE * i,
912 TCE32_TABLE_SIZE, 0x1000);
913 if (rc) {
914 pe_err(pe, " Failed to configure 32-bit TCE table,"
915 " err %ld\n", rc);
916 goto fail;
917 }
918 }
919
920 /* Setup linux iommu table */
921 tbl = &pe->tce32_table;
922 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +1000923 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000924
925 /* OPAL variant of P7IOC SW invalidated TCEs */
926 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
927 if (swinvp) {
928 /* We need a couple more fields -- an address and a data
929 * to or. Since the bus is only printed out on table free
930 * errors, and on the first pass the data will be a relative
931 * bus number, print that out instead.
932 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000933 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
934 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
935 8);
Gavin Shan65fd7662014-04-24 18:00:28 +1000936 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
937 TCE_PCI_SWINV_FREE |
938 TCE_PCI_SWINV_PAIR);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000939 }
940 iommu_init_table(tbl, phb->hose->node);
Gavin Shane9bc03f2014-04-24 18:00:29 +1000941 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000942
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000943 if (pe->pdev)
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +1100944 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000945 else
Gavin Shandff4a392014-07-15 17:00:55 +1000946 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +1000947
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000948 return;
949 fail:
950 /* XXX Failure: Try to fallback to 64-bit only ? */
951 if (pe->tce32_seg >= 0)
952 pe->tce32_seg = -1;
953 if (tce_mem)
954 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
955}
956
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100957static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
958{
959 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
960 tce32_table);
961 uint16_t window_id = (pe->pe_number << 1 ) + 1;
962 int64_t rc;
963
964 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
965 if (enable) {
966 phys_addr_t top = memblock_end_of_DRAM();
967
968 top = roundup_pow_of_two(top);
969 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
970 pe->pe_number,
971 window_id,
972 pe->tce_bypass_base,
973 top);
974 } else {
975 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
976 pe->pe_number,
977 window_id,
978 pe->tce_bypass_base,
979 0);
980
981 /*
Gavin Shandff4a392014-07-15 17:00:55 +1000982 * EEH needs the mapping between IOMMU table and group
983 * of those VFIO/KVM pass-through devices. We can postpone
984 * resetting DMA ops until the DMA mask is configured in
985 * host side.
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100986 */
Gavin Shandff4a392014-07-15 17:00:55 +1000987 if (pe->pdev)
988 set_iommu_table_base(&pe->pdev->dev, tbl);
989 else
990 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100991 }
992 if (rc)
993 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
994 else
995 pe->tce_bypass_enabled = enable;
996}
997
998static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
999 struct pnv_ioda_pe *pe)
1000{
1001 /* TVE #1 is selected by PCI address bit 59 */
1002 pe->tce_bypass_base = 1ull << 59;
1003
1004 /* Install set_bypass callback for VFIO */
1005 pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass;
1006
1007 /* Enable bypass by default */
1008 pnv_pci_ioda2_set_bypass(&pe->tce32_table, true);
1009}
1010
Gavin Shan373f5652013-04-25 19:21:01 +00001011static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1012 struct pnv_ioda_pe *pe)
1013{
1014 struct page *tce_mem = NULL;
1015 void *addr;
1016 const __be64 *swinvp;
1017 struct iommu_table *tbl;
1018 unsigned int tce_table_size, end;
1019 int64_t rc;
1020
1021 /* We shouldn't already have a 32-bit DMA associated */
1022 if (WARN_ON(pe->tce32_seg >= 0))
1023 return;
1024
1025 /* The PE will reserve all possible 32-bits space */
1026 pe->tce32_seg = 0;
1027 end = (1 << ilog2(phb->ioda.m32_pci_base));
1028 tce_table_size = (end / 0x1000) * 8;
1029 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1030 end);
1031
1032 /* Allocate TCE table */
1033 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1034 get_order(tce_table_size));
1035 if (!tce_mem) {
1036 pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
1037 goto fail;
1038 }
1039 addr = page_address(tce_mem);
1040 memset(addr, 0, tce_table_size);
1041
1042 /*
1043 * Map TCE table through TVT. The TVE index is the PE number
1044 * shifted by 1 bit for 32-bits DMA space.
1045 */
1046 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1047 pe->pe_number << 1, 1, __pa(addr),
1048 tce_table_size, 0x1000);
1049 if (rc) {
1050 pe_err(pe, "Failed to configure 32-bit TCE table,"
1051 " err %ld\n", rc);
1052 goto fail;
1053 }
1054
1055 /* Setup linux iommu table */
1056 tbl = &pe->tce32_table;
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001057 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
1058 IOMMU_PAGE_SHIFT_4K);
Gavin Shan373f5652013-04-25 19:21:01 +00001059
1060 /* OPAL variant of PHB3 invalidated TCEs */
1061 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1062 if (swinvp) {
1063 /* We need a couple more fields -- an address and a data
1064 * to or. Since the bus is only printed out on table free
1065 * errors, and on the first pass the data will be a relative
1066 * bus number, print that out instead.
1067 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001068 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1069 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1070 8);
Gavin Shan65fd7662014-04-24 18:00:28 +10001071 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
Gavin Shan373f5652013-04-25 19:21:01 +00001072 }
1073 iommu_init_table(tbl, phb->hose->node);
Gavin Shane9bc03f2014-04-24 18:00:29 +10001074 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
Gavin Shan373f5652013-04-25 19:21:01 +00001075
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001076 if (pe->pdev)
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +11001077 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001078 else
Gavin Shandff4a392014-07-15 17:00:55 +10001079 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001080
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001081 /* Also create a bypass window */
1082 pnv_pci_ioda2_setup_bypass_pe(phb, pe);
Gavin Shan373f5652013-04-25 19:21:01 +00001083 return;
1084fail:
1085 if (pe->tce32_seg >= 0)
1086 pe->tce32_seg = -1;
1087 if (tce_mem)
1088 __free_pages(tce_mem, get_order(tce_table_size));
1089}
1090
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001091static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001092{
1093 struct pci_controller *hose = phb->hose;
1094 unsigned int residual, remaining, segs, tw, base;
1095 struct pnv_ioda_pe *pe;
1096
1097 /* If we have more PE# than segments available, hand out one
1098 * per PE until we run out and let the rest fail. If not,
1099 * then we assign at least one segment per PE, plus more based
1100 * on the amount of devices under that PE
1101 */
1102 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
1103 residual = 0;
1104 else
1105 residual = phb->ioda.tce32_count -
1106 phb->ioda.dma_pe_count;
1107
1108 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
1109 hose->global_number, phb->ioda.tce32_count);
1110 pr_info("PCI: %d PE# for a total weight of %d\n",
1111 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
1112
1113 /* Walk our PE list and configure their DMA segments, hand them
1114 * out one base segment plus any residual segments based on
1115 * weight
1116 */
1117 remaining = phb->ioda.tce32_count;
1118 tw = phb->ioda.dma_weight;
1119 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00001120 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001121 if (!pe->dma_weight)
1122 continue;
1123 if (!remaining) {
1124 pe_warn(pe, "No DMA32 resources available\n");
1125 continue;
1126 }
1127 segs = 1;
1128 if (residual) {
1129 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
1130 if (segs > remaining)
1131 segs = remaining;
1132 }
Gavin Shan373f5652013-04-25 19:21:01 +00001133
1134 /*
1135 * For IODA2 compliant PHB3, we needn't care about the weight.
1136 * The all available 32-bits DMA space will be assigned to
1137 * the specific PE.
1138 */
1139 if (phb->type == PNV_PHB_IODA1) {
1140 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1141 pe->dma_weight, segs);
1142 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1143 } else {
1144 pe_info(pe, "Assign DMA32 space\n");
1145 segs = 0;
1146 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1147 }
1148
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001149 remaining -= segs;
1150 base += segs;
1151 }
1152}
1153
1154#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00001155static void pnv_ioda2_msi_eoi(struct irq_data *d)
1156{
1157 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1158 struct irq_chip *chip = irq_data_get_irq_chip(d);
1159 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
1160 ioda.irq_chip);
1161 int64_t rc;
1162
1163 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
1164 WARN_ON_ONCE(rc);
1165
1166 icp_native_eoi(d);
1167}
1168
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001169static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00001170 unsigned int hwirq, unsigned int virq,
1171 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001172{
1173 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001174 struct pci_dn *pdn = pci_get_pdn(dev);
Gavin Shan137436c2013-04-25 19:20:59 +00001175 struct irq_data *idata;
1176 struct irq_chip *ichip;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001177 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001178 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001179 int rc;
1180
1181 /* No PE assigned ? bail out ... no MSI for you ! */
1182 if (pe == NULL)
1183 return -ENXIO;
1184
1185 /* Check if we have an MVE */
1186 if (pe->mve_number < 0)
1187 return -ENXIO;
1188
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001189 /* Force 32-bit MSI on some broken devices */
1190 if (pdn && pdn->force_32bit_msi)
1191 is_64 = 0;
1192
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001193 /* Assign XIVE to PE */
1194 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1195 if (rc) {
1196 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1197 pci_name(dev), rc, xive_num);
1198 return -EIO;
1199 }
1200
1201 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001202 __be64 addr64;
1203
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001204 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1205 &addr64, &data);
1206 if (rc) {
1207 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1208 pci_name(dev), rc);
1209 return -EIO;
1210 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001211 msg->address_hi = be64_to_cpu(addr64) >> 32;
1212 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001213 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001214 __be32 addr32;
1215
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001216 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1217 &addr32, &data);
1218 if (rc) {
1219 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1220 pci_name(dev), rc);
1221 return -EIO;
1222 }
1223 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001224 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001225 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001226 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001227
Gavin Shan137436c2013-04-25 19:20:59 +00001228 /*
1229 * Change the IRQ chip for the MSI interrupts on PHB3.
1230 * The corresponding IRQ chip should be populated for
1231 * the first time.
1232 */
1233 if (phb->type == PNV_PHB_IODA2) {
1234 if (!phb->ioda.irq_chip_init) {
1235 idata = irq_get_irq_data(virq);
1236 ichip = irq_data_get_irq_chip(idata);
1237 phb->ioda.irq_chip_init = 1;
1238 phb->ioda.irq_chip = *ichip;
1239 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
1240 }
1241
1242 irq_set_chip(virq, &phb->ioda.irq_chip);
1243 }
1244
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001245 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1246 " address=%x_%08x data=%x PE# %d\n",
1247 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
1248 msg->address_hi, msg->address_lo, data, pe->pe_number);
1249
1250 return 0;
1251}
1252
1253static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1254{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001255 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001256 const __be32 *prop = of_get_property(phb->hose->dn,
1257 "ibm,opal-msi-ranges", NULL);
1258 if (!prop) {
1259 /* BML Fallback */
1260 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1261 }
1262 if (!prop)
1263 return;
1264
1265 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001266 count = be32_to_cpup(prop + 1);
1267 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001268 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1269 phb->hose->global_number);
1270 return;
1271 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001272
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001273 phb->msi_setup = pnv_pci_ioda_msi_setup;
1274 phb->msi32_support = 1;
1275 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001276 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001277}
1278#else
1279static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1280#endif /* CONFIG_PCI_MSI */
1281
Gavin Shan11685be2012-08-20 03:49:16 +00001282/*
1283 * This function is supposed to be called on basis of PE from top
1284 * to bottom style. So the the I/O or MMIO segment assigned to
1285 * parent PE could be overrided by its child PEs if necessary.
1286 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001287static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
1288 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00001289{
1290 struct pnv_phb *phb = hose->private_data;
1291 struct pci_bus_region region;
1292 struct resource *res;
1293 int i, index;
1294 int rc;
1295
1296 /*
1297 * NOTE: We only care PCI bus based PE for now. For PCI
1298 * device based PE, for example SRIOV sensitive VF should
1299 * be figured out later.
1300 */
1301 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
1302
1303 pci_bus_for_each_resource(pe->pbus, res, i) {
1304 if (!res || !res->flags ||
1305 res->start > res->end)
1306 continue;
1307
1308 if (res->flags & IORESOURCE_IO) {
1309 region.start = res->start - phb->ioda.io_pci_base;
1310 region.end = res->end - phb->ioda.io_pci_base;
1311 index = region.start / phb->ioda.io_segsize;
1312
1313 while (index < phb->ioda.total_pe &&
1314 region.start <= region.end) {
1315 phb->ioda.io_segmap[index] = pe->pe_number;
1316 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1317 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
1318 if (rc != OPAL_SUCCESS) {
1319 pr_err("%s: OPAL error %d when mapping IO "
1320 "segment #%d to PE#%d\n",
1321 __func__, rc, index, pe->pe_number);
1322 break;
1323 }
1324
1325 region.start += phb->ioda.io_segsize;
1326 index++;
1327 }
1328 } else if (res->flags & IORESOURCE_MEM) {
1329 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001330 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00001331 phb->ioda.m32_pci_base;
1332 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001333 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00001334 phb->ioda.m32_pci_base;
1335 index = region.start / phb->ioda.m32_segsize;
1336
1337 while (index < phb->ioda.total_pe &&
1338 region.start <= region.end) {
1339 phb->ioda.m32_segmap[index] = pe->pe_number;
1340 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1341 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
1342 if (rc != OPAL_SUCCESS) {
1343 pr_err("%s: OPAL error %d when mapping M32 "
1344 "segment#%d to PE#%d",
1345 __func__, rc, index, pe->pe_number);
1346 break;
1347 }
1348
1349 region.start += phb->ioda.m32_segsize;
1350 index++;
1351 }
1352 }
1353 }
1354}
1355
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001356static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00001357{
1358 struct pci_controller *tmp, *hose;
1359 struct pnv_phb *phb;
1360 struct pnv_ioda_pe *pe;
1361
1362 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1363 phb = hose->private_data;
1364 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
1365 pnv_ioda_setup_pe_seg(hose, pe);
1366 }
1367 }
1368}
1369
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001370static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00001371{
1372 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00001373 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00001374
1375 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1376 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00001377
1378 /* Mark the PHB initialization done */
1379 phb = hose->private_data;
1380 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00001381 }
1382}
1383
Gavin Shan37c367f2013-06-20 18:13:25 +08001384static void pnv_pci_ioda_create_dbgfs(void)
1385{
1386#ifdef CONFIG_DEBUG_FS
1387 struct pci_controller *hose, *tmp;
1388 struct pnv_phb *phb;
1389 char name[16];
1390
1391 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1392 phb = hose->private_data;
1393
1394 sprintf(name, "PCI%04x", hose->global_number);
1395 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
1396 if (!phb->dbgfs)
1397 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
1398 __func__, hose->global_number);
1399 }
1400#endif /* CONFIG_DEBUG_FS */
1401}
1402
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001403static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001404{
1405 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00001406 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00001407 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08001408
Gavin Shan37c367f2013-06-20 18:13:25 +08001409 pnv_pci_ioda_create_dbgfs();
1410
Gavin Shane9cc17d2013-06-20 13:21:14 +08001411#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08001412 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04001413 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08001414#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00001415}
1416
Gavin Shan271fd032012-09-11 16:59:47 -06001417/*
1418 * Returns the alignment for I/O or memory windows for P2P
1419 * bridges. That actually depends on how PEs are segmented.
1420 * For now, we return I/O or M32 segment size for PE sensitive
1421 * P2P bridges. Otherwise, the default values (4KiB for I/O,
1422 * 1MiB for memory) will be returned.
1423 *
1424 * The current PCI bus might be put into one PE, which was
1425 * create against the parent PCI bridge. For that case, we
1426 * needn't enlarge the alignment so that we can save some
1427 * resources.
1428 */
1429static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
1430 unsigned long type)
1431{
1432 struct pci_dev *bridge;
1433 struct pci_controller *hose = pci_bus_to_host(bus);
1434 struct pnv_phb *phb = hose->private_data;
1435 int num_pci_bridges = 0;
1436
1437 bridge = bus->self;
1438 while (bridge) {
1439 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
1440 num_pci_bridges++;
1441 if (num_pci_bridges >= 2)
1442 return 1;
1443 }
1444
1445 bridge = bridge->bus->self;
1446 }
1447
Guo Chao262af552014-07-21 14:42:30 +10001448 /* We fail back to M32 if M64 isn't supported */
1449 if (phb->ioda.m64_segsize &&
1450 pnv_pci_is_mem_pref_64(type))
1451 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06001452 if (type & IORESOURCE_MEM)
1453 return phb->ioda.m32_segsize;
1454
1455 return phb->ioda.io_segsize;
1456}
1457
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001458/* Prevent enabling devices for which we couldn't properly
1459 * assign a PE
1460 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001461static int pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001462{
Gavin Shandb1266c2012-08-20 03:49:18 +00001463 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1464 struct pnv_phb *phb = hose->private_data;
1465 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001466
Gavin Shandb1266c2012-08-20 03:49:18 +00001467 /* The function is probably called while the PEs have
1468 * not be created yet. For example, resource reassignment
1469 * during PCI probe period. We just skip the check if
1470 * PEs isn't ready.
1471 */
1472 if (!phb->initialized)
1473 return 0;
1474
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001475 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001476 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1477 return -EINVAL;
Gavin Shandb1266c2012-08-20 03:49:18 +00001478
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001479 return 0;
1480}
1481
1482static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1483 u32 devfn)
1484{
1485 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1486}
1487
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001488static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
1489{
1490 opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET,
1491 OPAL_ASSERT_RESET);
1492}
1493
Gavin Shane9cc17d2013-06-20 13:21:14 +08001494void __init pnv_pci_init_ioda_phb(struct device_node *np,
1495 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001496{
1497 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001498 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08001499 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10001500 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001501 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001502 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001503 u64 phb_id;
1504 void *aux;
1505 long rc;
1506
Gavin Shan58d714e2013-07-31 16:47:00 +08001507 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001508
1509 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1510 if (!prop64) {
1511 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
1512 return;
1513 }
1514 phb_id = be64_to_cpup(prop64);
1515 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
1516
1517 phb = alloc_bootmem(sizeof(struct pnv_phb));
Gavin Shan58d714e2013-07-31 16:47:00 +08001518 if (!phb) {
1519 pr_err(" Out of memory !\n");
1520 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001521 }
Gavin Shan58d714e2013-07-31 16:47:00 +08001522
1523 /* Allocate PCI controller */
1524 memset(phb, 0, sizeof(struct pnv_phb));
1525 phb->hose = hose = pcibios_alloc_controller(np);
1526 if (!phb->hose) {
1527 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001528 np->full_name);
Gavin Shan58d714e2013-07-31 16:47:00 +08001529 free_bootmem((unsigned long)phb, sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001530 return;
1531 }
1532
1533 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001534 prop32 = of_get_property(np, "bus-range", &len);
1535 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001536 hose->first_busno = be32_to_cpu(prop32[0]);
1537 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001538 } else {
1539 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
1540 hose->first_busno = 0;
1541 hose->last_busno = 0xff;
1542 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001543 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08001544 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001545 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00001546 phb->type = ioda_type;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001547
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00001548 /* Detect specific models for error handling */
1549 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
1550 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00001551 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00001552 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00001553 else
1554 phb->model = PNV_PHB_MODEL_UNKNOWN;
1555
Gavin Shanaa0c0332013-04-25 19:20:57 +00001556 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08001557 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001558
Gavin Shanaa0c0332013-04-25 19:20:57 +00001559 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001560 phb->regs = of_iomap(np, 0);
1561 if (phb->regs == NULL)
1562 pr_err(" Failed to map registers !\n");
1563
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001564 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08001565 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00001566 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08001567 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001568 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08001569 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
1570 if (prop32)
1571 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10001572
1573 /* Parse 64-bit MMIO range */
1574 pnv_ioda_parse_m64_window(phb);
1575
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001576 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00001577 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001578 phb->ioda.m32_size += 0x10000;
1579
1580 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001581 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001582 phb->ioda.io_size = hose->pci_io_size;
1583 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
1584 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
1585
Gavin Shanc35d2a82013-07-31 16:47:04 +08001586 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001587 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
1588 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00001589 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08001590 if (phb->type == PNV_PHB_IODA1) {
1591 iomap_off = size;
1592 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
1593 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001594 pemap_off = size;
1595 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
1596 aux = alloc_bootmem(size);
1597 memset(aux, 0, size);
1598 phb->ioda.pe_alloc = aux;
1599 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08001600 if (phb->type == PNV_PHB_IODA1)
1601 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001602 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08001603 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001604
Gavin Shan7ebdf952012-08-20 03:49:15 +00001605 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001606 INIT_LIST_HEAD(&phb->ioda.pe_list);
1607
1608 /* Calculate how many 32-bit TCE segments we have */
1609 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
1610
Gavin Shanaa0c0332013-04-25 19:20:57 +00001611#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001612 rc = opal_pci_set_phb_mem_window(opal->phb_id,
1613 window_type,
1614 window_num,
1615 starting_real_address,
1616 starting_pci_address,
1617 segment_size);
1618#endif
1619
Guo Chao262af552014-07-21 14:42:30 +10001620 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
1621 phb->ioda.total_pe, phb->ioda.reserved_pe,
1622 phb->ioda.m32_size, phb->ioda.m32_segsize);
1623 if (phb->ioda.m64_size)
1624 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
1625 phb->ioda.m64_size, phb->ioda.m64_segsize);
1626 if (phb->ioda.io_size)
1627 pr_info(" IO: 0x%x [segment=0x%x]\n",
1628 phb->ioda.io_size, phb->ioda.io_segsize);
1629
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001630
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001631 phb->hose->ops = &pnv_pci_ops;
Gavin Shane9cc17d2013-06-20 13:21:14 +08001632#ifdef CONFIG_EEH
1633 phb->eeh_ops = &ioda_eeh_ops;
1634#endif
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001635
1636 /* Setup RID -> PE mapping function */
1637 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
1638
1639 /* Setup TCEs */
1640 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001641 phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001642
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001643 /* Setup shutdown function for kexec */
1644 phb->shutdown = pnv_pci_ioda_shutdown;
1645
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001646 /* Setup MSI support */
1647 pnv_pci_init_ioda_msis(phb);
1648
Gavin Shanc40a4212012-08-20 03:49:20 +00001649 /*
1650 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
1651 * to let the PCI core do resource assignment. It's supposed
1652 * that the PCI core will do correct I/O and MMIO alignment
1653 * for the P2P bridge bars so that each PCI bus (excluding
1654 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001655 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00001656 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001657 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
Gavin Shan271fd032012-09-11 16:59:47 -06001658 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
Gavin Shand92a2082014-04-24 18:00:24 +10001659 ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
Gavin Shanc40a4212012-08-20 03:49:20 +00001660 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001661
1662 /* Reset IODA tables to a clean state */
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00001663 rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001664 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00001665 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10001666
1667 /* If we're running in kdump kerenl, the previous kerenl never
1668 * shutdown PCI devices correctly. We already got IODA table
1669 * cleaned out. So we have to issue PHB reset to stop all PCI
1670 * transactions from previous kerenl.
1671 */
1672 if (is_kdump_kernel()) {
1673 pr_info(" Issue PHB reset ...\n");
1674 ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
1675 ioda_eeh_phb_reset(hose, OPAL_DEASSERT_RESET);
1676 }
Guo Chao262af552014-07-21 14:42:30 +10001677
1678 /* Configure M64 window */
1679 if (phb->init_m64 && phb->init_m64(phb))
1680 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00001681}
1682
Bjorn Helgaas67975002013-07-02 12:20:03 -06001683void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00001684{
Gavin Shane9cc17d2013-06-20 13:21:14 +08001685 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001686}
1687
1688void __init pnv_pci_init_ioda_hub(struct device_node *np)
1689{
1690 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10001691 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001692 u64 hub_id;
1693
1694 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
1695
1696 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
1697 if (!prop64) {
1698 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
1699 return;
1700 }
1701 hub_id = be64_to_cpup(prop64);
1702 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
1703
1704 /* Count child PHBs */
1705 for_each_child_of_node(np, phbn) {
1706 /* Look for IODA1 PHBs */
1707 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08001708 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001709 }
1710}