blob: 4f1e43c05e84ab4ebf107a6038b32ac3f65bd116 [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>
Ian Munsie80c49c72014-10-08 19:54:57 +110040#include <asm/pnv-pci.h>
41
42#include <misc/cxl.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000043
44#include "powernv.h"
45#include "pci.h"
46
Joe Perches6d31c2f2014-09-21 10:55:06 -070047static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
48 const char *fmt, ...)
49{
50 struct va_format vaf;
51 va_list args;
52 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000053
Joe Perches6d31c2f2014-09-21 10:55:06 -070054 va_start(args, fmt);
55
56 vaf.fmt = fmt;
57 vaf.va = &args;
58
59 if (pe->pdev)
60 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
61 else
62 sprintf(pfix, "%04x:%02x ",
63 pci_domain_nr(pe->pbus), pe->pbus->number);
64
65 printk("%spci %s: [PE# %.3d] %pV",
66 level, pfix, pe->pe_number, &vaf);
67
68 va_end(args);
69}
70
71#define pe_err(pe, fmt, ...) \
72 pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
73#define pe_warn(pe, fmt, ...) \
74 pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
75#define pe_info(pe, fmt, ...) \
76 pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000077
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +100078/*
79 * stdcix is only supposed to be used in hypervisor real mode as per
80 * the architecture spec
81 */
82static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
83{
84 __asm__ __volatile__("stdcix %0,0,%1"
85 : : "r" (val), "r" (paddr) : "memory");
86}
87
Guo Chao262af552014-07-21 14:42:30 +100088static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
89{
90 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
91 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
92}
93
Gavin Shan4b82ab12014-11-12 13:36:07 +110094static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
95{
96 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
97 pr_warn("%s: Invalid PE %d on PHB#%x\n",
98 __func__, pe_no, phb->hose->global_number);
99 return;
100 }
101
102 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) {
103 pr_warn("%s: PE %d was assigned on PHB#%x\n",
104 __func__, pe_no, phb->hose->global_number);
105 return;
106 }
107
108 phb->ioda.pe_array[pe_no].phb = phb;
109 phb->ioda.pe_array[pe_no].pe_number = pe_no;
110}
111
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800112static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000113{
114 unsigned long pe;
115
116 do {
117 pe = find_next_zero_bit(phb->ioda.pe_alloc,
118 phb->ioda.total_pe, 0);
119 if (pe >= phb->ioda.total_pe)
120 return IODA_INVALID_PE;
121 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
122
Gavin Shan4cce9552013-04-25 19:21:00 +0000123 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000124 phb->ioda.pe_array[pe].pe_number = pe;
125 return pe;
126}
127
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800128static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000129{
130 WARN_ON(phb->ioda.pe_array[pe].pdev);
131
132 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
133 clear_bit(pe, phb->ioda.pe_alloc);
134}
135
Guo Chao262af552014-07-21 14:42:30 +1000136/* The default M64 BAR is shared by all PEs */
137static int pnv_ioda2_init_m64(struct pnv_phb *phb)
138{
139 const char *desc;
140 struct resource *r;
141 s64 rc;
142
143 /* Configure the default M64 BAR */
144 rc = opal_pci_set_phb_mem_window(phb->opal_id,
145 OPAL_M64_WINDOW_TYPE,
146 phb->ioda.m64_bar_idx,
147 phb->ioda.m64_base,
148 0, /* unused */
149 phb->ioda.m64_size);
150 if (rc != OPAL_SUCCESS) {
151 desc = "configuring";
152 goto fail;
153 }
154
155 /* Enable the default M64 BAR */
156 rc = opal_pci_phb_mmio_enable(phb->opal_id,
157 OPAL_M64_WINDOW_TYPE,
158 phb->ioda.m64_bar_idx,
159 OPAL_ENABLE_M64_SPLIT);
160 if (rc != OPAL_SUCCESS) {
161 desc = "enabling";
162 goto fail;
163 }
164
165 /* Mark the M64 BAR assigned */
166 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
167
168 /*
169 * Strip off the segment used by the reserved PE, which is
170 * expected to be 0 or last one of PE capabicity.
171 */
172 r = &phb->hose->mem_resources[1];
173 if (phb->ioda.reserved_pe == 0)
174 r->start += phb->ioda.m64_segsize;
175 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
176 r->end -= phb->ioda.m64_segsize;
177 else
178 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
179 phb->ioda.reserved_pe);
180
181 return 0;
182
183fail:
184 pr_warn(" Failure %lld %s M64 BAR#%d\n",
185 rc, desc, phb->ioda.m64_bar_idx);
186 opal_pci_phb_mmio_enable(phb->opal_id,
187 OPAL_M64_WINDOW_TYPE,
188 phb->ioda.m64_bar_idx,
189 OPAL_DISABLE_M64);
190 return -EIO;
191}
192
Gavin Shan5ef73562014-11-12 13:36:06 +1100193static void pnv_ioda2_reserve_m64_pe(struct pnv_phb *phb)
Guo Chao262af552014-07-21 14:42:30 +1000194{
195 resource_size_t sgsz = phb->ioda.m64_segsize;
196 struct pci_dev *pdev;
197 struct resource *r;
198 int base, step, i;
199
200 /*
201 * Root bus always has full M64 range and root port has
202 * M64 range used in reality. So we're checking root port
203 * instead of root bus.
204 */
205 list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100206 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
207 r = &pdev->resource[PCI_BRIDGE_RESOURCES + i];
Guo Chao262af552014-07-21 14:42:30 +1000208 if (!r->parent ||
209 !pnv_pci_is_mem_pref_64(r->flags))
210 continue;
211
212 base = (r->start - phb->ioda.m64_base) / sgsz;
213 for (step = 0; step < resource_size(r) / sgsz; step++)
Gavin Shan4b82ab12014-11-12 13:36:07 +1100214 pnv_ioda_reserve_pe(phb, base + step);
Guo Chao262af552014-07-21 14:42:30 +1000215 }
216 }
217}
218
219static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
220 struct pci_bus *bus, int all)
221{
222 resource_size_t segsz = phb->ioda.m64_segsize;
223 struct pci_dev *pdev;
224 struct resource *r;
225 struct pnv_ioda_pe *master_pe, *pe;
226 unsigned long size, *pe_alloc;
227 bool found;
228 int start, i, j;
229
230 /* Root bus shouldn't use M64 */
231 if (pci_is_root_bus(bus))
232 return IODA_INVALID_PE;
233
234 /* We support only one M64 window on each bus */
235 found = false;
236 pci_bus_for_each_resource(bus, r, i) {
237 if (r && r->parent &&
238 pnv_pci_is_mem_pref_64(r->flags)) {
239 found = true;
240 break;
241 }
242 }
243
244 /* No M64 window found ? */
245 if (!found)
246 return IODA_INVALID_PE;
247
248 /* Allocate bitmap */
249 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
250 pe_alloc = kzalloc(size, GFP_KERNEL);
251 if (!pe_alloc) {
252 pr_warn("%s: Out of memory !\n",
253 __func__);
254 return IODA_INVALID_PE;
255 }
256
257 /*
258 * Figure out reserved PE numbers by the PE
259 * the its child PEs.
260 */
261 start = (r->start - phb->ioda.m64_base) / segsz;
262 for (i = 0; i < resource_size(r) / segsz; i++)
263 set_bit(start + i, pe_alloc);
264
265 if (all)
266 goto done;
267
268 /*
269 * If the PE doesn't cover all subordinate buses,
270 * we need subtract from reserved PEs for children.
271 */
272 list_for_each_entry(pdev, &bus->devices, bus_list) {
273 if (!pdev->subordinate)
274 continue;
275
276 pci_bus_for_each_resource(pdev->subordinate, r, i) {
277 if (!r || !r->parent ||
278 !pnv_pci_is_mem_pref_64(r->flags))
279 continue;
280
281 start = (r->start - phb->ioda.m64_base) / segsz;
282 for (j = 0; j < resource_size(r) / segsz ; j++)
283 clear_bit(start + j, pe_alloc);
284 }
285 }
286
287 /*
288 * the current bus might not own M64 window and that's all
289 * contributed by its child buses. For the case, we needn't
290 * pick M64 dependent PE#.
291 */
292 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
293 kfree(pe_alloc);
294 return IODA_INVALID_PE;
295 }
296
297 /*
298 * Figure out the master PE and put all slave PEs to master
299 * PE's list to form compound PE.
300 */
301done:
302 master_pe = NULL;
303 i = -1;
304 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
305 phb->ioda.total_pe) {
306 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000307
308 if (!master_pe) {
309 pe->flags |= PNV_IODA_PE_MASTER;
310 INIT_LIST_HEAD(&pe->slaves);
311 master_pe = pe;
312 } else {
313 pe->flags |= PNV_IODA_PE_SLAVE;
314 pe->master = master_pe;
315 list_add_tail(&pe->list, &master_pe->slaves);
316 }
317 }
318
319 kfree(pe_alloc);
320 return master_pe->pe_number;
321}
322
323static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
324{
325 struct pci_controller *hose = phb->hose;
326 struct device_node *dn = hose->dn;
327 struct resource *res;
328 const u32 *r;
329 u64 pci_addr;
330
Gavin Shan1665c4a2014-11-12 13:36:04 +1100331 /* FIXME: Support M64 for P7IOC */
332 if (phb->type != PNV_PHB_IODA2) {
333 pr_info(" Not support M64 window\n");
334 return;
335 }
336
Guo Chao262af552014-07-21 14:42:30 +1000337 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
338 pr_info(" Firmware too old to support M64 window\n");
339 return;
340 }
341
342 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
343 if (!r) {
344 pr_info(" No <ibm,opal-m64-window> on %s\n",
345 dn->full_name);
346 return;
347 }
348
Guo Chao262af552014-07-21 14:42:30 +1000349 res = &hose->mem_resources[1];
350 res->start = of_translate_address(dn, r + 2);
351 res->end = res->start + of_read_number(r + 4, 2) - 1;
352 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
353 pci_addr = of_read_number(r, 2);
354 hose->mem_offset[1] = res->start - pci_addr;
355
356 phb->ioda.m64_size = resource_size(res);
357 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
358 phb->ioda.m64_base = pci_addr;
359
Wei Yange9863e62014-12-12 12:39:37 +0800360 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
361 res->start, res->end, pci_addr);
362
Guo Chao262af552014-07-21 14:42:30 +1000363 /* Use last M64 BAR to cover M64 window */
364 phb->ioda.m64_bar_idx = 15;
365 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shan5ef73562014-11-12 13:36:06 +1100366 phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000367 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
368}
369
Gavin Shan49dec922014-07-21 14:42:33 +1000370static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
371{
372 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
373 struct pnv_ioda_pe *slave;
374 s64 rc;
375
376 /* Fetch master PE */
377 if (pe->flags & PNV_IODA_PE_SLAVE) {
378 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100379 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
380 return;
381
Gavin Shan49dec922014-07-21 14:42:33 +1000382 pe_no = pe->pe_number;
383 }
384
385 /* Freeze master PE */
386 rc = opal_pci_eeh_freeze_set(phb->opal_id,
387 pe_no,
388 OPAL_EEH_ACTION_SET_FREEZE_ALL);
389 if (rc != OPAL_SUCCESS) {
390 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
391 __func__, rc, phb->hose->global_number, pe_no);
392 return;
393 }
394
395 /* Freeze slave PEs */
396 if (!(pe->flags & PNV_IODA_PE_MASTER))
397 return;
398
399 list_for_each_entry(slave, &pe->slaves, list) {
400 rc = opal_pci_eeh_freeze_set(phb->opal_id,
401 slave->pe_number,
402 OPAL_EEH_ACTION_SET_FREEZE_ALL);
403 if (rc != OPAL_SUCCESS)
404 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
405 __func__, rc, phb->hose->global_number,
406 slave->pe_number);
407 }
408}
409
Anton Blancharde51df2c2014-08-20 08:55:18 +1000410static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000411{
412 struct pnv_ioda_pe *pe, *slave;
413 s64 rc;
414
415 /* Find master PE */
416 pe = &phb->ioda.pe_array[pe_no];
417 if (pe->flags & PNV_IODA_PE_SLAVE) {
418 pe = pe->master;
419 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
420 pe_no = pe->pe_number;
421 }
422
423 /* Clear frozen state for master PE */
424 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
425 if (rc != OPAL_SUCCESS) {
426 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
427 __func__, rc, opt, phb->hose->global_number, pe_no);
428 return -EIO;
429 }
430
431 if (!(pe->flags & PNV_IODA_PE_MASTER))
432 return 0;
433
434 /* Clear frozen state for slave PEs */
435 list_for_each_entry(slave, &pe->slaves, list) {
436 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
437 slave->pe_number,
438 opt);
439 if (rc != OPAL_SUCCESS) {
440 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
441 __func__, rc, opt, phb->hose->global_number,
442 slave->pe_number);
443 return -EIO;
444 }
445 }
446
447 return 0;
448}
449
450static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
451{
452 struct pnv_ioda_pe *slave, *pe;
453 u8 fstate, state;
454 __be16 pcierr;
455 s64 rc;
456
457 /* Sanity check on PE number */
458 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
459 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
460
461 /*
462 * Fetch the master PE and the PE instance might be
463 * not initialized yet.
464 */
465 pe = &phb->ioda.pe_array[pe_no];
466 if (pe->flags & PNV_IODA_PE_SLAVE) {
467 pe = pe->master;
468 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
469 pe_no = pe->pe_number;
470 }
471
472 /* Check the master PE */
473 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
474 &state, &pcierr, NULL);
475 if (rc != OPAL_SUCCESS) {
476 pr_warn("%s: Failure %lld getting "
477 "PHB#%x-PE#%x state\n",
478 __func__, rc,
479 phb->hose->global_number, pe_no);
480 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
481 }
482
483 /* Check the slave PE */
484 if (!(pe->flags & PNV_IODA_PE_MASTER))
485 return state;
486
487 list_for_each_entry(slave, &pe->slaves, list) {
488 rc = opal_pci_eeh_freeze_status(phb->opal_id,
489 slave->pe_number,
490 &fstate,
491 &pcierr,
492 NULL);
493 if (rc != OPAL_SUCCESS) {
494 pr_warn("%s: Failure %lld getting "
495 "PHB#%x-PE#%x state\n",
496 __func__, rc,
497 phb->hose->global_number, slave->pe_number);
498 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
499 }
500
501 /*
502 * Override the result based on the ascending
503 * priority.
504 */
505 if (fstate > state)
506 state = fstate;
507 }
508
509 return state;
510}
511
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000512/* Currently those 2 are only used when MSIs are enabled, this will change
513 * but in the meantime, we need to protect them to avoid warnings
514 */
515#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800516static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000517{
518 struct pci_controller *hose = pci_bus_to_host(dev->bus);
519 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000520 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000521
522 if (!pdn)
523 return NULL;
524 if (pdn->pe_number == IODA_INVALID_PE)
525 return NULL;
526 return &phb->ioda.pe_array[pdn->pe_number];
527}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000528#endif /* CONFIG_PCI_MSI */
529
Gavin Shanb131a842014-11-12 13:36:08 +1100530static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
531 struct pnv_ioda_pe *parent,
532 struct pnv_ioda_pe *child,
533 bool is_add)
534{
535 const char *desc = is_add ? "adding" : "removing";
536 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
537 OPAL_REMOVE_PE_FROM_DOMAIN;
538 struct pnv_ioda_pe *slave;
539 long rc;
540
541 /* Parent PE affects child PE */
542 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
543 child->pe_number, op);
544 if (rc != OPAL_SUCCESS) {
545 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
546 rc, desc);
547 return -ENXIO;
548 }
549
550 if (!(child->flags & PNV_IODA_PE_MASTER))
551 return 0;
552
553 /* Compound case: parent PE affects slave PEs */
554 list_for_each_entry(slave, &child->slaves, list) {
555 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
556 slave->pe_number, op);
557 if (rc != OPAL_SUCCESS) {
558 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
559 rc, desc);
560 return -ENXIO;
561 }
562 }
563
564 return 0;
565}
566
567static int pnv_ioda_set_peltv(struct pnv_phb *phb,
568 struct pnv_ioda_pe *pe,
569 bool is_add)
570{
571 struct pnv_ioda_pe *slave;
572 struct pci_dev *pdev;
573 int ret;
574
575 /*
576 * Clear PE frozen state. If it's master PE, we need
577 * clear slave PE frozen state as well.
578 */
579 if (is_add) {
580 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
581 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
582 if (pe->flags & PNV_IODA_PE_MASTER) {
583 list_for_each_entry(slave, &pe->slaves, list)
584 opal_pci_eeh_freeze_clear(phb->opal_id,
585 slave->pe_number,
586 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
587 }
588 }
589
590 /*
591 * Associate PE in PELT. We need add the PE into the
592 * corresponding PELT-V as well. Otherwise, the error
593 * originated from the PE might contribute to other
594 * PEs.
595 */
596 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
597 if (ret)
598 return ret;
599
600 /* For compound PEs, any one affects all of them */
601 if (pe->flags & PNV_IODA_PE_MASTER) {
602 list_for_each_entry(slave, &pe->slaves, list) {
603 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
604 if (ret)
605 return ret;
606 }
607 }
608
609 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
610 pdev = pe->pbus->self;
611 else
612 pdev = pe->pdev->bus->self;
613 while (pdev) {
614 struct pci_dn *pdn = pci_get_pdn(pdev);
615 struct pnv_ioda_pe *parent;
616
617 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
618 parent = &phb->ioda.pe_array[pdn->pe_number];
619 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
620 if (ret)
621 return ret;
622 }
623
624 pdev = pdev->bus->self;
625 }
626
627 return 0;
628}
629
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800630static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000631{
632 struct pci_dev *parent;
633 uint8_t bcomp, dcomp, fcomp;
634 long rc, rid_end, rid;
635
636 /* Bus validation ? */
637 if (pe->pbus) {
638 int count;
639
640 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
641 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
642 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000643 if (pe->flags & PNV_IODA_PE_BUS_ALL)
644 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
645 else
646 count = 1;
647
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000648 switch(count) {
649 case 1: bcomp = OpalPciBusAll; break;
650 case 2: bcomp = OpalPciBus7Bits; break;
651 case 4: bcomp = OpalPciBus6Bits; break;
652 case 8: bcomp = OpalPciBus5Bits; break;
653 case 16: bcomp = OpalPciBus4Bits; break;
654 case 32: bcomp = OpalPciBus3Bits; break;
655 default:
656 pr_err("%s: Number of subordinate busses %d"
657 " unsupported\n",
658 pci_name(pe->pbus->self), count);
659 /* Do an exact match only */
660 bcomp = OpalPciBusAll;
661 }
662 rid_end = pe->rid + (count << 8);
663 } else {
664 parent = pe->pdev->bus->self;
665 bcomp = OpalPciBusAll;
666 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
667 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
668 rid_end = pe->rid + 1;
669 }
670
Gavin Shan631ad692013-11-04 16:32:46 +0800671 /*
672 * Associate PE in PELT. We need add the PE into the
673 * corresponding PELT-V as well. Otherwise, the error
674 * originated from the PE might contribute to other
675 * PEs.
676 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000677 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
678 bcomp, dcomp, fcomp, OPAL_MAP_PE);
679 if (rc) {
680 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
681 return -ENXIO;
682 }
Gavin Shan631ad692013-11-04 16:32:46 +0800683
Gavin Shanb131a842014-11-12 13:36:08 +1100684 /* Configure PELTV */
685 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000686
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000687 /* Setup reverse map */
688 for (rid = pe->rid; rid < rid_end; rid++)
689 phb->ioda.pe_rmap[rid] = pe->pe_number;
690
691 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100692 if (phb->type != PNV_PHB_IODA1) {
693 pe->mve_number = 0;
694 goto out;
695 }
696
697 pe->mve_number = pe->pe_number;
698 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
699 if (rc != OPAL_SUCCESS) {
700 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
701 rc, pe->mve_number);
702 pe->mve_number = -1;
703 } else {
704 rc = opal_pci_set_mve_enable(phb->opal_id,
705 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000706 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100707 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000708 rc, pe->mve_number);
709 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000710 }
Gavin Shan4773f762014-11-12 13:36:09 +1100711 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000712
Gavin Shan4773f762014-11-12 13:36:09 +1100713out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000714 return 0;
715}
716
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800717static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
718 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000719{
720 struct pnv_ioda_pe *lpe;
721
Gavin Shan7ebdf952012-08-20 03:49:15 +0000722 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000723 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000724 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000725 return;
726 }
727 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000728 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000729}
730
731static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
732{
733 /* This is quite simplistic. The "base" weight of a device
734 * is 10. 0 means no DMA is to be accounted for it.
735 */
736
737 /* If it's a bridge, no DMA */
738 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
739 return 0;
740
741 /* Reduce the weight of slow USB controllers */
742 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
743 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
744 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
745 return 3;
746
747 /* Increase the weight of RAID (includes Obsidian) */
748 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
749 return 15;
750
751 /* Default */
752 return 10;
753}
754
Gavin Shanfb446ad2012-08-20 03:49:14 +0000755#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800756static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000757{
758 struct pci_controller *hose = pci_bus_to_host(dev->bus);
759 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000760 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000761 struct pnv_ioda_pe *pe;
762 int pe_num;
763
764 if (!pdn) {
765 pr_err("%s: Device tree node not associated properly\n",
766 pci_name(dev));
767 return NULL;
768 }
769 if (pdn->pe_number != IODA_INVALID_PE)
770 return NULL;
771
772 /* PE#0 has been pre-set */
773 if (dev->bus->number == 0)
774 pe_num = 0;
775 else
776 pe_num = pnv_ioda_alloc_pe(phb);
777 if (pe_num == IODA_INVALID_PE) {
778 pr_warning("%s: Not enough PE# available, disabling device\n",
779 pci_name(dev));
780 return NULL;
781 }
782
783 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
784 * pointer in the PE data structure, both should be destroyed at the
785 * same time. However, this needs to be looked at more closely again
786 * once we actually start removing things (Hotplug, SR-IOV, ...)
787 *
788 * At some point we want to remove the PDN completely anyways
789 */
790 pe = &phb->ioda.pe_array[pe_num];
791 pci_dev_get(dev);
792 pdn->pcidev = dev;
793 pdn->pe_number = pe_num;
794 pe->pdev = dev;
795 pe->pbus = NULL;
796 pe->tce32_seg = -1;
797 pe->mve_number = -1;
798 pe->rid = dev->bus->number << 8 | pdn->devfn;
799
800 pe_info(pe, "Associated device to PE\n");
801
802 if (pnv_ioda_configure_pe(phb, pe)) {
803 /* XXX What do we do here ? */
804 if (pe_num)
805 pnv_ioda_free_pe(phb, pe_num);
806 pdn->pe_number = IODA_INVALID_PE;
807 pe->pdev = NULL;
808 pci_dev_put(dev);
809 return NULL;
810 }
811
812 /* Assign a DMA weight to the device */
813 pe->dma_weight = pnv_ioda_dma_weight(dev);
814 if (pe->dma_weight != 0) {
815 phb->ioda.dma_weight += pe->dma_weight;
816 phb->ioda.dma_pe_count++;
817 }
818
819 /* Link the PE */
820 pnv_ioda_link_pe_by_weight(phb, pe);
821
822 return pe;
823}
Gavin Shanfb446ad2012-08-20 03:49:14 +0000824#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000825
826static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
827{
828 struct pci_dev *dev;
829
830 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000831 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000832
833 if (pdn == NULL) {
834 pr_warn("%s: No device node associated with device !\n",
835 pci_name(dev));
836 continue;
837 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000838 pdn->pcidev = dev;
839 pdn->pe_number = pe->pe_number;
840 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +0000841 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000842 pnv_ioda_setup_same_PE(dev->subordinate, pe);
843 }
844}
845
Gavin Shanfb446ad2012-08-20 03:49:14 +0000846/*
847 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
848 * single PCI bus. Another one that contains the primary PCI bus and its
849 * subordinate PCI devices and buses. The second type of PE is normally
850 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
851 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800852static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000853{
Gavin Shanfb446ad2012-08-20 03:49:14 +0000854 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000855 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000856 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +1000857 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000858
Guo Chao262af552014-07-21 14:42:30 +1000859 /* Check if PE is determined by M64 */
860 if (phb->pick_m64_pe)
861 pe_num = phb->pick_m64_pe(phb, bus, all);
862
863 /* The PE number isn't pinned by M64 */
864 if (pe_num == IODA_INVALID_PE)
865 pe_num = pnv_ioda_alloc_pe(phb);
866
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000867 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000868 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
869 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000870 return;
871 }
872
873 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +1000874 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000875 pe->pbus = bus;
876 pe->pdev = NULL;
877 pe->tce32_seg = -1;
878 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -0700879 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000880 pe->dma_weight = 0;
881
Gavin Shanfb446ad2012-08-20 03:49:14 +0000882 if (all)
883 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
884 bus->busn_res.start, bus->busn_res.end, pe_num);
885 else
886 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
887 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000888
889 if (pnv_ioda_configure_pe(phb, pe)) {
890 /* XXX What do we do here ? */
891 if (pe_num)
892 pnv_ioda_free_pe(phb, pe_num);
893 pe->pbus = NULL;
894 return;
895 }
896
897 /* Associate it with all child devices */
898 pnv_ioda_setup_same_PE(bus, pe);
899
Gavin Shan7ebdf952012-08-20 03:49:15 +0000900 /* Put PE to the list */
901 list_add_tail(&pe->list, &phb->ioda.pe_list);
902
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000903 /* Account for one DMA PE if at least one DMA capable device exist
904 * below the bridge
905 */
906 if (pe->dma_weight != 0) {
907 phb->ioda.dma_weight += pe->dma_weight;
908 phb->ioda.dma_pe_count++;
909 }
910
911 /* Link the PE */
912 pnv_ioda_link_pe_by_weight(phb, pe);
913}
914
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800915static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000916{
917 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000918
919 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000920
921 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000922 if (dev->subordinate) {
923 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
924 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
925 else
926 pnv_ioda_setup_PEs(dev->subordinate);
927 }
928 }
929}
930
931/*
932 * Configure PEs so that the downstream PCI buses and devices
933 * could have their associated PE#. Unfortunately, we didn't
934 * figure out the way to identify the PLX bridge yet. So we
935 * simply put the PCI bus and the subordinate behind the root
936 * port to PE# here. The game rule here is expected to be changed
937 * as soon as we can detected PLX bridge correctly.
938 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800939static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +0000940{
941 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +1000942 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000943
944 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +1000945 phb = hose->private_data;
946
947 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +1100948 if (phb->reserve_m64_pe)
949 phb->reserve_m64_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +1000950
Gavin Shanfb446ad2012-08-20 03:49:14 +0000951 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000952 }
953}
954
Gavin Shan959c9bd2013-04-25 19:21:02 +0000955static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000956{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000957 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +0000958 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000959
Gavin Shan959c9bd2013-04-25 19:21:02 +0000960 /*
961 * The function can be called while the PE#
962 * hasn't been assigned. Do nothing for the
963 * case.
964 */
965 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
966 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000967
Gavin Shan959c9bd2013-04-25 19:21:02 +0000968 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100969 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Gavin Shan763fe0a2014-08-06 17:10:16 +1000970 set_iommu_table_base_and_group(&pdev->dev, &pe->tce32_table);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000971}
972
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +1100973static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
974 struct pci_dev *pdev, u64 dma_mask)
975{
976 struct pci_dn *pdn = pci_get_pdn(pdev);
977 struct pnv_ioda_pe *pe;
978 uint64_t top;
979 bool bypass = false;
980
981 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
982 return -ENODEV;;
983
984 pe = &phb->ioda.pe_array[pdn->pe_number];
985 if (pe->tce_bypass_enabled) {
986 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
987 bypass = (dma_mask >= top);
988 }
989
990 if (bypass) {
991 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
992 set_dma_ops(&pdev->dev, &dma_direct_ops);
993 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
994 } else {
995 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
996 set_dma_ops(&pdev->dev, &dma_iommu_ops);
997 set_iommu_table_base(&pdev->dev, &pe->tce32_table);
998 }
Brian W Harta32305b2014-07-31 14:24:37 -0500999 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001000 return 0;
1001}
1002
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001003static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
1004 struct pci_dev *pdev)
1005{
1006 struct pci_dn *pdn = pci_get_pdn(pdev);
1007 struct pnv_ioda_pe *pe;
1008 u64 end, mask;
1009
1010 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1011 return 0;
1012
1013 pe = &phb->ioda.pe_array[pdn->pe_number];
1014 if (!pe->tce_bypass_enabled)
1015 return __dma_get_required_mask(&pdev->dev);
1016
1017
1018 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1019 mask = 1ULL << (fls64(end) - 1);
1020 mask += mask - 1;
1021
1022 return mask;
1023}
1024
Gavin Shandff4a392014-07-15 17:00:55 +10001025static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
1026 struct pci_bus *bus,
1027 bool add_to_iommu_group)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001028{
1029 struct pci_dev *dev;
1030
1031 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shandff4a392014-07-15 17:00:55 +10001032 if (add_to_iommu_group)
1033 set_iommu_table_base_and_group(&dev->dev,
1034 &pe->tce32_table);
1035 else
1036 set_iommu_table_base(&dev->dev, &pe->tce32_table);
1037
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001038 if (dev->subordinate)
Gavin Shandff4a392014-07-15 17:00:55 +10001039 pnv_ioda_setup_bus_dma(pe, dev->subordinate,
1040 add_to_iommu_group);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001041 }
1042}
1043
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001044static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
1045 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001046 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001047{
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001048 __be64 __iomem *invalidate = rm ?
1049 (__be64 __iomem *)pe->tce_inval_reg_phys :
1050 (__be64 __iomem *)tbl->it_index;
Gavin Shan4cce9552013-04-25 19:21:00 +00001051 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001052 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001053
1054 start = __pa(startp);
1055 end = __pa(endp);
1056
1057 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1058 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001059 start <<= shift;
1060 end <<= shift;
1061 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001062 start |= tbl->it_busno;
1063 end |= tbl->it_busno;
1064 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1065 /* p7ioc-style invalidation, 2 TCEs per write */
1066 start |= (1ull << 63);
1067 end |= (1ull << 63);
1068 inc = 16;
1069 } else {
1070 /* Default (older HW) */
1071 inc = 128;
1072 }
1073
1074 end |= inc - 1; /* round up end to be different than start */
1075
1076 mb(); /* Ensure above stores are visible */
1077 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001078 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001079 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001080 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001081 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001082 start += inc;
1083 }
1084
1085 /*
1086 * The iommu layer will do another mb() for us on build()
1087 * and we don't care on free()
1088 */
1089}
1090
1091static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
1092 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001093 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001094{
1095 unsigned long start, end, inc;
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001096 __be64 __iomem *invalidate = rm ?
1097 (__be64 __iomem *)pe->tce_inval_reg_phys :
1098 (__be64 __iomem *)tbl->it_index;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001099 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001100
1101 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001102 start = 0x2ull << 60;
Gavin Shan4cce9552013-04-25 19:21:00 +00001103 start |= (pe->pe_number & 0xFF);
1104 end = start;
1105
1106 /* Figure out the start, end and step */
1107 inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001108 start |= (inc << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001109 inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001110 end |= (inc << shift);
1111 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001112 mb();
1113
1114 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001115 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001116 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001117 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001118 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001119 start += inc;
1120 }
1121}
1122
1123void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001124 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001125{
1126 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
1127 tce32_table);
1128 struct pnv_phb *phb = pe->phb;
1129
1130 if (phb->type == PNV_PHB_IODA1)
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001131 pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00001132 else
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001133 pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00001134}
1135
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001136static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1137 struct pnv_ioda_pe *pe, unsigned int base,
1138 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001139{
1140
1141 struct page *tce_mem = NULL;
1142 const __be64 *swinvp;
1143 struct iommu_table *tbl;
1144 unsigned int i;
1145 int64_t rc;
1146 void *addr;
1147
1148 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
1149#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
1150
1151 /* XXX FIXME: Handle 64-bit only DMA devices */
1152 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1153 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1154
1155 /* We shouldn't already have a 32-bit DMA associated */
1156 if (WARN_ON(pe->tce32_seg >= 0))
1157 return;
1158
1159 /* Grab a 32-bit TCE table */
1160 pe->tce32_seg = base;
1161 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1162 (base << 28), ((base + segs) << 28) - 1);
1163
1164 /* XXX Currently, we allocate one big contiguous table for the
1165 * TCEs. We only really need one chunk per 256M of TCE space
1166 * (ie per segment) but that's an optimization for later, it
1167 * requires some added smarts with our get/put_tce implementation
1168 */
1169 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1170 get_order(TCE32_TABLE_SIZE * segs));
1171 if (!tce_mem) {
1172 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1173 goto fail;
1174 }
1175 addr = page_address(tce_mem);
1176 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1177
1178 /* Configure HW */
1179 for (i = 0; i < segs; i++) {
1180 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1181 pe->pe_number,
1182 base + i, 1,
1183 __pa(addr) + TCE32_TABLE_SIZE * i,
1184 TCE32_TABLE_SIZE, 0x1000);
1185 if (rc) {
1186 pe_err(pe, " Failed to configure 32-bit TCE table,"
1187 " err %ld\n", rc);
1188 goto fail;
1189 }
1190 }
1191
1192 /* Setup linux iommu table */
1193 tbl = &pe->tce32_table;
1194 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001195 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001196
1197 /* OPAL variant of P7IOC SW invalidated TCEs */
1198 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1199 if (swinvp) {
1200 /* We need a couple more fields -- an address and a data
1201 * to or. Since the bus is only printed out on table free
1202 * errors, and on the first pass the data will be a relative
1203 * bus number, print that out instead.
1204 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001205 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1206 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1207 8);
Gavin Shan65fd7662014-04-24 18:00:28 +10001208 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1209 TCE_PCI_SWINV_FREE |
1210 TCE_PCI_SWINV_PAIR);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001211 }
1212 iommu_init_table(tbl, phb->hose->node);
Gavin Shane9bc03f2014-04-24 18:00:29 +10001213 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001214
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001215 if (pe->pdev)
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +11001216 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001217 else
Gavin Shandff4a392014-07-15 17:00:55 +10001218 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001219
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001220 return;
1221 fail:
1222 /* XXX Failure: Try to fallback to 64-bit only ? */
1223 if (pe->tce32_seg >= 0)
1224 pe->tce32_seg = -1;
1225 if (tce_mem)
1226 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
1227}
1228
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001229static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
1230{
1231 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
1232 tce32_table);
1233 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1234 int64_t rc;
1235
1236 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1237 if (enable) {
1238 phys_addr_t top = memblock_end_of_DRAM();
1239
1240 top = roundup_pow_of_two(top);
1241 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1242 pe->pe_number,
1243 window_id,
1244 pe->tce_bypass_base,
1245 top);
1246 } else {
1247 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1248 pe->pe_number,
1249 window_id,
1250 pe->tce_bypass_base,
1251 0);
1252
1253 /*
Gavin Shandff4a392014-07-15 17:00:55 +10001254 * EEH needs the mapping between IOMMU table and group
1255 * of those VFIO/KVM pass-through devices. We can postpone
1256 * resetting DMA ops until the DMA mask is configured in
1257 * host side.
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001258 */
Gavin Shandff4a392014-07-15 17:00:55 +10001259 if (pe->pdev)
1260 set_iommu_table_base(&pe->pdev->dev, tbl);
1261 else
1262 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001263 }
1264 if (rc)
1265 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1266 else
1267 pe->tce_bypass_enabled = enable;
1268}
1269
1270static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
1271 struct pnv_ioda_pe *pe)
1272{
1273 /* TVE #1 is selected by PCI address bit 59 */
1274 pe->tce_bypass_base = 1ull << 59;
1275
1276 /* Install set_bypass callback for VFIO */
1277 pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass;
1278
1279 /* Enable bypass by default */
1280 pnv_pci_ioda2_set_bypass(&pe->tce32_table, true);
1281}
1282
Gavin Shan373f5652013-04-25 19:21:01 +00001283static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1284 struct pnv_ioda_pe *pe)
1285{
1286 struct page *tce_mem = NULL;
1287 void *addr;
1288 const __be64 *swinvp;
1289 struct iommu_table *tbl;
1290 unsigned int tce_table_size, end;
1291 int64_t rc;
1292
1293 /* We shouldn't already have a 32-bit DMA associated */
1294 if (WARN_ON(pe->tce32_seg >= 0))
1295 return;
1296
1297 /* The PE will reserve all possible 32-bits space */
1298 pe->tce32_seg = 0;
1299 end = (1 << ilog2(phb->ioda.m32_pci_base));
1300 tce_table_size = (end / 0x1000) * 8;
1301 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1302 end);
1303
1304 /* Allocate TCE table */
1305 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1306 get_order(tce_table_size));
1307 if (!tce_mem) {
1308 pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
1309 goto fail;
1310 }
1311 addr = page_address(tce_mem);
1312 memset(addr, 0, tce_table_size);
1313
1314 /*
1315 * Map TCE table through TVT. The TVE index is the PE number
1316 * shifted by 1 bit for 32-bits DMA space.
1317 */
1318 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1319 pe->pe_number << 1, 1, __pa(addr),
1320 tce_table_size, 0x1000);
1321 if (rc) {
1322 pe_err(pe, "Failed to configure 32-bit TCE table,"
1323 " err %ld\n", rc);
1324 goto fail;
1325 }
1326
1327 /* Setup linux iommu table */
1328 tbl = &pe->tce32_table;
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001329 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
1330 IOMMU_PAGE_SHIFT_4K);
Gavin Shan373f5652013-04-25 19:21:01 +00001331
1332 /* OPAL variant of PHB3 invalidated TCEs */
1333 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1334 if (swinvp) {
1335 /* We need a couple more fields -- an address and a data
1336 * to or. Since the bus is only printed out on table free
1337 * errors, and on the first pass the data will be a relative
1338 * bus number, print that out instead.
1339 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001340 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1341 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1342 8);
Gavin Shan65fd7662014-04-24 18:00:28 +10001343 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
Gavin Shan373f5652013-04-25 19:21:01 +00001344 }
1345 iommu_init_table(tbl, phb->hose->node);
Gavin Shane9bc03f2014-04-24 18:00:29 +10001346 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
Gavin Shan373f5652013-04-25 19:21:01 +00001347
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001348 if (pe->pdev)
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +11001349 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001350 else
Gavin Shandff4a392014-07-15 17:00:55 +10001351 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001352
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001353 /* Also create a bypass window */
1354 pnv_pci_ioda2_setup_bypass_pe(phb, pe);
Gavin Shan373f5652013-04-25 19:21:01 +00001355 return;
1356fail:
1357 if (pe->tce32_seg >= 0)
1358 pe->tce32_seg = -1;
1359 if (tce_mem)
1360 __free_pages(tce_mem, get_order(tce_table_size));
1361}
1362
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001363static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001364{
1365 struct pci_controller *hose = phb->hose;
1366 unsigned int residual, remaining, segs, tw, base;
1367 struct pnv_ioda_pe *pe;
1368
1369 /* If we have more PE# than segments available, hand out one
1370 * per PE until we run out and let the rest fail. If not,
1371 * then we assign at least one segment per PE, plus more based
1372 * on the amount of devices under that PE
1373 */
1374 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
1375 residual = 0;
1376 else
1377 residual = phb->ioda.tce32_count -
1378 phb->ioda.dma_pe_count;
1379
1380 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
1381 hose->global_number, phb->ioda.tce32_count);
1382 pr_info("PCI: %d PE# for a total weight of %d\n",
1383 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
1384
1385 /* Walk our PE list and configure their DMA segments, hand them
1386 * out one base segment plus any residual segments based on
1387 * weight
1388 */
1389 remaining = phb->ioda.tce32_count;
1390 tw = phb->ioda.dma_weight;
1391 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00001392 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001393 if (!pe->dma_weight)
1394 continue;
1395 if (!remaining) {
1396 pe_warn(pe, "No DMA32 resources available\n");
1397 continue;
1398 }
1399 segs = 1;
1400 if (residual) {
1401 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
1402 if (segs > remaining)
1403 segs = remaining;
1404 }
Gavin Shan373f5652013-04-25 19:21:01 +00001405
1406 /*
1407 * For IODA2 compliant PHB3, we needn't care about the weight.
1408 * The all available 32-bits DMA space will be assigned to
1409 * the specific PE.
1410 */
1411 if (phb->type == PNV_PHB_IODA1) {
1412 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1413 pe->dma_weight, segs);
1414 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1415 } else {
1416 pe_info(pe, "Assign DMA32 space\n");
1417 segs = 0;
1418 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1419 }
1420
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001421 remaining -= segs;
1422 base += segs;
1423 }
1424}
1425
1426#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00001427static void pnv_ioda2_msi_eoi(struct irq_data *d)
1428{
1429 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1430 struct irq_chip *chip = irq_data_get_irq_chip(d);
1431 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
1432 ioda.irq_chip);
1433 int64_t rc;
1434
1435 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
1436 WARN_ON_ONCE(rc);
1437
1438 icp_native_eoi(d);
1439}
1440
Ian Munsiefd9a1c22014-10-08 19:54:55 +11001441
1442static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
1443{
1444 struct irq_data *idata;
1445 struct irq_chip *ichip;
1446
1447 if (phb->type != PNV_PHB_IODA2)
1448 return;
1449
1450 if (!phb->ioda.irq_chip_init) {
1451 /*
1452 * First time we setup an MSI IRQ, we need to setup the
1453 * corresponding IRQ chip to route correctly.
1454 */
1455 idata = irq_get_irq_data(virq);
1456 ichip = irq_data_get_irq_chip(idata);
1457 phb->ioda.irq_chip_init = 1;
1458 phb->ioda.irq_chip = *ichip;
1459 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
1460 }
1461 irq_set_chip(virq, &phb->ioda.irq_chip);
1462}
1463
Ian Munsie80c49c72014-10-08 19:54:57 +11001464#ifdef CONFIG_CXL_BASE
1465
1466struct device_node *pnv_pci_to_phb_node(struct pci_dev *dev)
1467{
1468 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1469
1470 return hose->dn;
1471}
1472EXPORT_SYMBOL(pnv_pci_to_phb_node);
1473
Ryan Grimm1212aa12015-01-19 11:52:50 -06001474int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11001475{
1476 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1477 struct pnv_phb *phb = hose->private_data;
1478 struct pnv_ioda_pe *pe;
1479 int rc;
1480
1481 pe = pnv_ioda_get_pe(dev);
1482 if (!pe)
1483 return -ENODEV;
1484
1485 pe_info(pe, "Switching PHB to CXL\n");
1486
Ryan Grimm1212aa12015-01-19 11:52:50 -06001487 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11001488 if (rc)
1489 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
1490
1491 return rc;
1492}
Ryan Grimm1212aa12015-01-19 11:52:50 -06001493EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11001494
1495/* Find PHB for cxl dev and allocate MSI hwirqs?
1496 * Returns the absolute hardware IRQ number
1497 */
1498int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
1499{
1500 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1501 struct pnv_phb *phb = hose->private_data;
1502 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
1503
1504 if (hwirq < 0) {
1505 dev_warn(&dev->dev, "Failed to find a free MSI\n");
1506 return -ENOSPC;
1507 }
1508
1509 return phb->msi_base + hwirq;
1510}
1511EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
1512
1513void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
1514{
1515 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1516 struct pnv_phb *phb = hose->private_data;
1517
1518 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
1519}
1520EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
1521
1522void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
1523 struct pci_dev *dev)
1524{
1525 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1526 struct pnv_phb *phb = hose->private_data;
1527 int i, hwirq;
1528
1529 for (i = 1; i < CXL_IRQ_RANGES; i++) {
1530 if (!irqs->range[i])
1531 continue;
1532 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
1533 i, irqs->offset[i],
1534 irqs->range[i]);
1535 hwirq = irqs->offset[i] - phb->msi_base;
1536 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
1537 irqs->range[i]);
1538 }
1539}
1540EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
1541
1542int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
1543 struct pci_dev *dev, int num)
1544{
1545 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1546 struct pnv_phb *phb = hose->private_data;
1547 int i, hwirq, try;
1548
1549 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
1550
1551 /* 0 is reserved for the multiplexed PSL DSI interrupt */
1552 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
1553 try = num;
1554 while (try) {
1555 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
1556 if (hwirq >= 0)
1557 break;
1558 try /= 2;
1559 }
1560 if (!try)
1561 goto fail;
1562
1563 irqs->offset[i] = phb->msi_base + hwirq;
1564 irqs->range[i] = try;
1565 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
1566 i, irqs->offset[i], irqs->range[i]);
1567 num -= try;
1568 }
1569 if (num)
1570 goto fail;
1571
1572 return 0;
1573fail:
1574 pnv_cxl_release_hwirq_ranges(irqs, dev);
1575 return -ENOSPC;
1576}
1577EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
1578
1579int pnv_cxl_get_irq_count(struct pci_dev *dev)
1580{
1581 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1582 struct pnv_phb *phb = hose->private_data;
1583
1584 return phb->msi_bmp.irq_count;
1585}
1586EXPORT_SYMBOL(pnv_cxl_get_irq_count);
1587
1588int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
1589 unsigned int virq)
1590{
1591 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1592 struct pnv_phb *phb = hose->private_data;
1593 unsigned int xive_num = hwirq - phb->msi_base;
1594 struct pnv_ioda_pe *pe;
1595 int rc;
1596
1597 if (!(pe = pnv_ioda_get_pe(dev)))
1598 return -ENODEV;
1599
1600 /* Assign XIVE to PE */
1601 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1602 if (rc) {
1603 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
1604 "hwirq 0x%x XIVE 0x%x PE\n",
1605 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
1606 return -EIO;
1607 }
1608 set_msi_irq_chip(phb, virq);
1609
1610 return 0;
1611}
1612EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
1613#endif
1614
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001615static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00001616 unsigned int hwirq, unsigned int virq,
1617 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001618{
1619 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1620 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001621 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001622 int rc;
1623
1624 /* No PE assigned ? bail out ... no MSI for you ! */
1625 if (pe == NULL)
1626 return -ENXIO;
1627
1628 /* Check if we have an MVE */
1629 if (pe->mve_number < 0)
1630 return -ENXIO;
1631
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001632 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11001633 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001634 is_64 = 0;
1635
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001636 /* Assign XIVE to PE */
1637 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1638 if (rc) {
1639 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1640 pci_name(dev), rc, xive_num);
1641 return -EIO;
1642 }
1643
1644 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001645 __be64 addr64;
1646
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001647 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1648 &addr64, &data);
1649 if (rc) {
1650 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1651 pci_name(dev), rc);
1652 return -EIO;
1653 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001654 msg->address_hi = be64_to_cpu(addr64) >> 32;
1655 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001656 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001657 __be32 addr32;
1658
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001659 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1660 &addr32, &data);
1661 if (rc) {
1662 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1663 pci_name(dev), rc);
1664 return -EIO;
1665 }
1666 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001667 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001668 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001669 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001670
Ian Munsiefd9a1c22014-10-08 19:54:55 +11001671 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00001672
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001673 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1674 " address=%x_%08x data=%x PE# %d\n",
1675 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
1676 msg->address_hi, msg->address_lo, data, pe->pe_number);
1677
1678 return 0;
1679}
1680
1681static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1682{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001683 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001684 const __be32 *prop = of_get_property(phb->hose->dn,
1685 "ibm,opal-msi-ranges", NULL);
1686 if (!prop) {
1687 /* BML Fallback */
1688 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1689 }
1690 if (!prop)
1691 return;
1692
1693 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001694 count = be32_to_cpup(prop + 1);
1695 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001696 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1697 phb->hose->global_number);
1698 return;
1699 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001700
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001701 phb->msi_setup = pnv_pci_ioda_msi_setup;
1702 phb->msi32_support = 1;
1703 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001704 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001705}
1706#else
1707static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1708#endif /* CONFIG_PCI_MSI */
1709
Gavin Shan11685be2012-08-20 03:49:16 +00001710/*
1711 * This function is supposed to be called on basis of PE from top
1712 * to bottom style. So the the I/O or MMIO segment assigned to
1713 * parent PE could be overrided by its child PEs if necessary.
1714 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001715static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
1716 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00001717{
1718 struct pnv_phb *phb = hose->private_data;
1719 struct pci_bus_region region;
1720 struct resource *res;
1721 int i, index;
1722 int rc;
1723
1724 /*
1725 * NOTE: We only care PCI bus based PE for now. For PCI
1726 * device based PE, for example SRIOV sensitive VF should
1727 * be figured out later.
1728 */
1729 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
1730
1731 pci_bus_for_each_resource(pe->pbus, res, i) {
1732 if (!res || !res->flags ||
1733 res->start > res->end)
1734 continue;
1735
1736 if (res->flags & IORESOURCE_IO) {
1737 region.start = res->start - phb->ioda.io_pci_base;
1738 region.end = res->end - phb->ioda.io_pci_base;
1739 index = region.start / phb->ioda.io_segsize;
1740
1741 while (index < phb->ioda.total_pe &&
1742 region.start <= region.end) {
1743 phb->ioda.io_segmap[index] = pe->pe_number;
1744 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1745 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
1746 if (rc != OPAL_SUCCESS) {
1747 pr_err("%s: OPAL error %d when mapping IO "
1748 "segment #%d to PE#%d\n",
1749 __func__, rc, index, pe->pe_number);
1750 break;
1751 }
1752
1753 region.start += phb->ioda.io_segsize;
1754 index++;
1755 }
1756 } else if (res->flags & IORESOURCE_MEM) {
1757 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001758 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00001759 phb->ioda.m32_pci_base;
1760 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001761 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00001762 phb->ioda.m32_pci_base;
1763 index = region.start / phb->ioda.m32_segsize;
1764
1765 while (index < phb->ioda.total_pe &&
1766 region.start <= region.end) {
1767 phb->ioda.m32_segmap[index] = pe->pe_number;
1768 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1769 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
1770 if (rc != OPAL_SUCCESS) {
1771 pr_err("%s: OPAL error %d when mapping M32 "
1772 "segment#%d to PE#%d",
1773 __func__, rc, index, pe->pe_number);
1774 break;
1775 }
1776
1777 region.start += phb->ioda.m32_segsize;
1778 index++;
1779 }
1780 }
1781 }
1782}
1783
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001784static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00001785{
1786 struct pci_controller *tmp, *hose;
1787 struct pnv_phb *phb;
1788 struct pnv_ioda_pe *pe;
1789
1790 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1791 phb = hose->private_data;
1792 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
1793 pnv_ioda_setup_pe_seg(hose, pe);
1794 }
1795 }
1796}
1797
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001798static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00001799{
1800 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00001801 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00001802
1803 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1804 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00001805
1806 /* Mark the PHB initialization done */
1807 phb = hose->private_data;
1808 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00001809 }
1810}
1811
Gavin Shan37c367f2013-06-20 18:13:25 +08001812static void pnv_pci_ioda_create_dbgfs(void)
1813{
1814#ifdef CONFIG_DEBUG_FS
1815 struct pci_controller *hose, *tmp;
1816 struct pnv_phb *phb;
1817 char name[16];
1818
1819 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1820 phb = hose->private_data;
1821
1822 sprintf(name, "PCI%04x", hose->global_number);
1823 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
1824 if (!phb->dbgfs)
1825 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
1826 __func__, hose->global_number);
1827 }
1828#endif /* CONFIG_DEBUG_FS */
1829}
1830
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001831static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001832{
1833 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00001834 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00001835 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08001836
Gavin Shan37c367f2013-06-20 18:13:25 +08001837 pnv_pci_ioda_create_dbgfs();
1838
Gavin Shane9cc17d2013-06-20 13:21:14 +08001839#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08001840 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04001841 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08001842#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00001843}
1844
Gavin Shan271fd032012-09-11 16:59:47 -06001845/*
1846 * Returns the alignment for I/O or memory windows for P2P
1847 * bridges. That actually depends on how PEs are segmented.
1848 * For now, we return I/O or M32 segment size for PE sensitive
1849 * P2P bridges. Otherwise, the default values (4KiB for I/O,
1850 * 1MiB for memory) will be returned.
1851 *
1852 * The current PCI bus might be put into one PE, which was
1853 * create against the parent PCI bridge. For that case, we
1854 * needn't enlarge the alignment so that we can save some
1855 * resources.
1856 */
1857static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
1858 unsigned long type)
1859{
1860 struct pci_dev *bridge;
1861 struct pci_controller *hose = pci_bus_to_host(bus);
1862 struct pnv_phb *phb = hose->private_data;
1863 int num_pci_bridges = 0;
1864
1865 bridge = bus->self;
1866 while (bridge) {
1867 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
1868 num_pci_bridges++;
1869 if (num_pci_bridges >= 2)
1870 return 1;
1871 }
1872
1873 bridge = bridge->bus->self;
1874 }
1875
Guo Chao262af552014-07-21 14:42:30 +10001876 /* We fail back to M32 if M64 isn't supported */
1877 if (phb->ioda.m64_segsize &&
1878 pnv_pci_is_mem_pref_64(type))
1879 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06001880 if (type & IORESOURCE_MEM)
1881 return phb->ioda.m32_segsize;
1882
1883 return phb->ioda.io_segsize;
1884}
1885
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001886/* Prevent enabling devices for which we couldn't properly
1887 * assign a PE
1888 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001889static int pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001890{
Gavin Shandb1266c2012-08-20 03:49:18 +00001891 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1892 struct pnv_phb *phb = hose->private_data;
1893 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001894
Gavin Shandb1266c2012-08-20 03:49:18 +00001895 /* The function is probably called while the PEs have
1896 * not be created yet. For example, resource reassignment
1897 * during PCI probe period. We just skip the check if
1898 * PEs isn't ready.
1899 */
1900 if (!phb->initialized)
1901 return 0;
1902
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001903 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001904 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1905 return -EINVAL;
Gavin Shandb1266c2012-08-20 03:49:18 +00001906
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001907 return 0;
1908}
1909
1910static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1911 u32 devfn)
1912{
1913 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1914}
1915
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001916static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
1917{
Gavin Shand1a85ee2014-09-30 12:39:05 +10001918 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001919 OPAL_ASSERT_RESET);
1920}
1921
Anton Blancharde51df2c2014-08-20 08:55:18 +10001922static void __init pnv_pci_init_ioda_phb(struct device_node *np,
1923 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001924{
1925 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001926 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08001927 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10001928 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001929 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001930 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001931 u64 phb_id;
1932 void *aux;
1933 long rc;
1934
Gavin Shan58d714e2013-07-31 16:47:00 +08001935 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001936
1937 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1938 if (!prop64) {
1939 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
1940 return;
1941 }
1942 phb_id = be64_to_cpup(prop64);
1943 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
1944
Michael Ellermane39f223f2014-11-18 16:47:35 +11001945 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08001946
1947 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08001948 phb->hose = hose = pcibios_alloc_controller(np);
1949 if (!phb->hose) {
1950 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001951 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11001952 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001953 return;
1954 }
1955
1956 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001957 prop32 = of_get_property(np, "bus-range", &len);
1958 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001959 hose->first_busno = be32_to_cpu(prop32[0]);
1960 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001961 } else {
1962 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
1963 hose->first_busno = 0;
1964 hose->last_busno = 0xff;
1965 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001966 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08001967 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001968 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00001969 phb->type = ioda_type;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001970
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00001971 /* Detect specific models for error handling */
1972 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
1973 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00001974 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00001975 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00001976 else
1977 phb->model = PNV_PHB_MODEL_UNKNOWN;
1978
Gavin Shanaa0c0332013-04-25 19:20:57 +00001979 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08001980 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001981
Gavin Shanaa0c0332013-04-25 19:20:57 +00001982 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001983 phb->regs = of_iomap(np, 0);
1984 if (phb->regs == NULL)
1985 pr_err(" Failed to map registers !\n");
1986
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001987 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08001988 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00001989 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08001990 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001991 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08001992 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
1993 if (prop32)
1994 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10001995
1996 /* Parse 64-bit MMIO range */
1997 pnv_ioda_parse_m64_window(phb);
1998
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001999 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00002000 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002001 phb->ioda.m32_size += 0x10000;
2002
2003 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002004 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002005 phb->ioda.io_size = hose->pci_io_size;
2006 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
2007 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
2008
Gavin Shanc35d2a82013-07-31 16:47:04 +08002009 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002010 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
2011 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00002012 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08002013 if (phb->type == PNV_PHB_IODA1) {
2014 iomap_off = size;
2015 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
2016 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002017 pemap_off = size;
2018 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11002019 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002020 phb->ioda.pe_alloc = aux;
2021 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08002022 if (phb->type == PNV_PHB_IODA1)
2023 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002024 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08002025 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002026
Gavin Shan7ebdf952012-08-20 03:49:15 +00002027 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002028 INIT_LIST_HEAD(&phb->ioda.pe_list);
2029
2030 /* Calculate how many 32-bit TCE segments we have */
2031 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
2032
Gavin Shanaa0c0332013-04-25 19:20:57 +00002033#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002034 rc = opal_pci_set_phb_mem_window(opal->phb_id,
2035 window_type,
2036 window_num,
2037 starting_real_address,
2038 starting_pci_address,
2039 segment_size);
2040#endif
2041
Guo Chao262af552014-07-21 14:42:30 +10002042 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
2043 phb->ioda.total_pe, phb->ioda.reserved_pe,
2044 phb->ioda.m32_size, phb->ioda.m32_segsize);
2045 if (phb->ioda.m64_size)
2046 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
2047 phb->ioda.m64_size, phb->ioda.m64_segsize);
2048 if (phb->ioda.io_size)
2049 pr_info(" IO: 0x%x [segment=0x%x]\n",
2050 phb->ioda.io_size, phb->ioda.io_segsize);
2051
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002052
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002053 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10002054 phb->get_pe_state = pnv_ioda_get_pe_state;
2055 phb->freeze_pe = pnv_ioda_freeze_pe;
2056 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Gavin Shane9cc17d2013-06-20 13:21:14 +08002057#ifdef CONFIG_EEH
2058 phb->eeh_ops = &ioda_eeh_ops;
2059#endif
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002060
2061 /* Setup RID -> PE mapping function */
2062 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
2063
2064 /* Setup TCEs */
2065 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002066 phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10002067 phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002068
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10002069 /* Setup shutdown function for kexec */
2070 phb->shutdown = pnv_pci_ioda_shutdown;
2071
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002072 /* Setup MSI support */
2073 pnv_pci_init_ioda_msis(phb);
2074
Gavin Shanc40a4212012-08-20 03:49:20 +00002075 /*
2076 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
2077 * to let the PCI core do resource assignment. It's supposed
2078 * that the PCI core will do correct I/O and MMIO alignment
2079 * for the P2P bridge bars so that each PCI bus (excluding
2080 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002081 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00002082 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002083 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
Gavin Shan271fd032012-09-11 16:59:47 -06002084 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
Gavin Shand92a2082014-04-24 18:00:24 +10002085 ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
Gavin Shanc40a4212012-08-20 03:49:20 +00002086 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002087
2088 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10002089 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002090 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00002091 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10002092
2093 /* If we're running in kdump kerenl, the previous kerenl never
2094 * shutdown PCI devices correctly. We already got IODA table
2095 * cleaned out. So we have to issue PHB reset to stop all PCI
2096 * transactions from previous kerenl.
2097 */
2098 if (is_kdump_kernel()) {
2099 pr_info(" Issue PHB reset ...\n");
2100 ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
Gavin Shan360d88a2014-11-25 09:26:59 +11002101 ioda_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10002102 }
Guo Chao262af552014-07-21 14:42:30 +10002103
Gavin Shan9e9e8932014-11-12 13:36:05 +11002104 /* Remove M64 resource if we can't configure it successfully */
2105 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10002106 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00002107}
2108
Bjorn Helgaas67975002013-07-02 12:20:03 -06002109void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00002110{
Gavin Shane9cc17d2013-06-20 13:21:14 +08002111 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002112}
2113
2114void __init pnv_pci_init_ioda_hub(struct device_node *np)
2115{
2116 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10002117 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002118 u64 hub_id;
2119
2120 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
2121
2122 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
2123 if (!prop64) {
2124 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
2125 return;
2126 }
2127 hub_id = be64_to_cpup(prop64);
2128 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
2129
2130 /* Count child PHBs */
2131 for_each_child_of_node(np, phbn) {
2132 /* Look for IODA1 PHBs */
2133 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08002134 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002135 }
2136}