blob: 7f58f199f2c130fd241f9b6bd03620d8c05b0546 [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
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020078static bool pnv_iommu_bypass_disabled __read_mostly;
79
80static int __init iommu_setup(char *str)
81{
82 if (!str)
83 return -EINVAL;
84
85 while (*str) {
86 if (!strncmp(str, "nobypass", 8)) {
87 pnv_iommu_bypass_disabled = true;
88 pr_info("PowerNV: IOMMU bypass window disabled.\n");
89 break;
90 }
91 str += strcspn(str, ",");
92 if (*str == ',')
93 str++;
94 }
95
96 return 0;
97}
98early_param("iommu", iommu_setup);
99
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000100/*
101 * stdcix is only supposed to be used in hypervisor real mode as per
102 * the architecture spec
103 */
104static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
105{
106 __asm__ __volatile__("stdcix %0,0,%1"
107 : : "r" (val), "r" (paddr) : "memory");
108}
109
Guo Chao262af552014-07-21 14:42:30 +1000110static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
111{
112 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
113 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
114}
115
Gavin Shan4b82ab12014-11-12 13:36:07 +1100116static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
117{
118 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
119 pr_warn("%s: Invalid PE %d on PHB#%x\n",
120 __func__, pe_no, phb->hose->global_number);
121 return;
122 }
123
124 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) {
125 pr_warn("%s: PE %d was assigned on PHB#%x\n",
126 __func__, pe_no, phb->hose->global_number);
127 return;
128 }
129
130 phb->ioda.pe_array[pe_no].phb = phb;
131 phb->ioda.pe_array[pe_no].pe_number = pe_no;
132}
133
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800134static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000135{
136 unsigned long pe;
137
138 do {
139 pe = find_next_zero_bit(phb->ioda.pe_alloc,
140 phb->ioda.total_pe, 0);
141 if (pe >= phb->ioda.total_pe)
142 return IODA_INVALID_PE;
143 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
144
Gavin Shan4cce9552013-04-25 19:21:00 +0000145 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000146 phb->ioda.pe_array[pe].pe_number = pe;
147 return pe;
148}
149
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800150static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000151{
152 WARN_ON(phb->ioda.pe_array[pe].pdev);
153
154 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
155 clear_bit(pe, phb->ioda.pe_alloc);
156}
157
Guo Chao262af552014-07-21 14:42:30 +1000158/* The default M64 BAR is shared by all PEs */
159static int pnv_ioda2_init_m64(struct pnv_phb *phb)
160{
161 const char *desc;
162 struct resource *r;
163 s64 rc;
164
165 /* Configure the default M64 BAR */
166 rc = opal_pci_set_phb_mem_window(phb->opal_id,
167 OPAL_M64_WINDOW_TYPE,
168 phb->ioda.m64_bar_idx,
169 phb->ioda.m64_base,
170 0, /* unused */
171 phb->ioda.m64_size);
172 if (rc != OPAL_SUCCESS) {
173 desc = "configuring";
174 goto fail;
175 }
176
177 /* Enable the default M64 BAR */
178 rc = opal_pci_phb_mmio_enable(phb->opal_id,
179 OPAL_M64_WINDOW_TYPE,
180 phb->ioda.m64_bar_idx,
181 OPAL_ENABLE_M64_SPLIT);
182 if (rc != OPAL_SUCCESS) {
183 desc = "enabling";
184 goto fail;
185 }
186
187 /* Mark the M64 BAR assigned */
188 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
189
190 /*
191 * Strip off the segment used by the reserved PE, which is
192 * expected to be 0 or last one of PE capabicity.
193 */
194 r = &phb->hose->mem_resources[1];
195 if (phb->ioda.reserved_pe == 0)
196 r->start += phb->ioda.m64_segsize;
197 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
198 r->end -= phb->ioda.m64_segsize;
199 else
200 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
201 phb->ioda.reserved_pe);
202
203 return 0;
204
205fail:
206 pr_warn(" Failure %lld %s M64 BAR#%d\n",
207 rc, desc, phb->ioda.m64_bar_idx);
208 opal_pci_phb_mmio_enable(phb->opal_id,
209 OPAL_M64_WINDOW_TYPE,
210 phb->ioda.m64_bar_idx,
211 OPAL_DISABLE_M64);
212 return -EIO;
213}
214
Gavin Shan5ef73562014-11-12 13:36:06 +1100215static void pnv_ioda2_reserve_m64_pe(struct pnv_phb *phb)
Guo Chao262af552014-07-21 14:42:30 +1000216{
217 resource_size_t sgsz = phb->ioda.m64_segsize;
218 struct pci_dev *pdev;
219 struct resource *r;
220 int base, step, i;
221
222 /*
223 * Root bus always has full M64 range and root port has
224 * M64 range used in reality. So we're checking root port
225 * instead of root bus.
226 */
227 list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100228 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
229 r = &pdev->resource[PCI_BRIDGE_RESOURCES + i];
Guo Chao262af552014-07-21 14:42:30 +1000230 if (!r->parent ||
231 !pnv_pci_is_mem_pref_64(r->flags))
232 continue;
233
234 base = (r->start - phb->ioda.m64_base) / sgsz;
235 for (step = 0; step < resource_size(r) / sgsz; step++)
Gavin Shan4b82ab12014-11-12 13:36:07 +1100236 pnv_ioda_reserve_pe(phb, base + step);
Guo Chao262af552014-07-21 14:42:30 +1000237 }
238 }
239}
240
241static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
242 struct pci_bus *bus, int all)
243{
244 resource_size_t segsz = phb->ioda.m64_segsize;
245 struct pci_dev *pdev;
246 struct resource *r;
247 struct pnv_ioda_pe *master_pe, *pe;
248 unsigned long size, *pe_alloc;
249 bool found;
250 int start, i, j;
251
252 /* Root bus shouldn't use M64 */
253 if (pci_is_root_bus(bus))
254 return IODA_INVALID_PE;
255
256 /* We support only one M64 window on each bus */
257 found = false;
258 pci_bus_for_each_resource(bus, r, i) {
259 if (r && r->parent &&
260 pnv_pci_is_mem_pref_64(r->flags)) {
261 found = true;
262 break;
263 }
264 }
265
266 /* No M64 window found ? */
267 if (!found)
268 return IODA_INVALID_PE;
269
270 /* Allocate bitmap */
271 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
272 pe_alloc = kzalloc(size, GFP_KERNEL);
273 if (!pe_alloc) {
274 pr_warn("%s: Out of memory !\n",
275 __func__);
276 return IODA_INVALID_PE;
277 }
278
279 /*
280 * Figure out reserved PE numbers by the PE
281 * the its child PEs.
282 */
283 start = (r->start - phb->ioda.m64_base) / segsz;
284 for (i = 0; i < resource_size(r) / segsz; i++)
285 set_bit(start + i, pe_alloc);
286
287 if (all)
288 goto done;
289
290 /*
291 * If the PE doesn't cover all subordinate buses,
292 * we need subtract from reserved PEs for children.
293 */
294 list_for_each_entry(pdev, &bus->devices, bus_list) {
295 if (!pdev->subordinate)
296 continue;
297
298 pci_bus_for_each_resource(pdev->subordinate, r, i) {
299 if (!r || !r->parent ||
300 !pnv_pci_is_mem_pref_64(r->flags))
301 continue;
302
303 start = (r->start - phb->ioda.m64_base) / segsz;
304 for (j = 0; j < resource_size(r) / segsz ; j++)
305 clear_bit(start + j, pe_alloc);
306 }
307 }
308
309 /*
310 * the current bus might not own M64 window and that's all
311 * contributed by its child buses. For the case, we needn't
312 * pick M64 dependent PE#.
313 */
314 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
315 kfree(pe_alloc);
316 return IODA_INVALID_PE;
317 }
318
319 /*
320 * Figure out the master PE and put all slave PEs to master
321 * PE's list to form compound PE.
322 */
323done:
324 master_pe = NULL;
325 i = -1;
326 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
327 phb->ioda.total_pe) {
328 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000329
330 if (!master_pe) {
331 pe->flags |= PNV_IODA_PE_MASTER;
332 INIT_LIST_HEAD(&pe->slaves);
333 master_pe = pe;
334 } else {
335 pe->flags |= PNV_IODA_PE_SLAVE;
336 pe->master = master_pe;
337 list_add_tail(&pe->list, &master_pe->slaves);
338 }
339 }
340
341 kfree(pe_alloc);
342 return master_pe->pe_number;
343}
344
345static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
346{
347 struct pci_controller *hose = phb->hose;
348 struct device_node *dn = hose->dn;
349 struct resource *res;
350 const u32 *r;
351 u64 pci_addr;
352
Gavin Shan1665c4a2014-11-12 13:36:04 +1100353 /* FIXME: Support M64 for P7IOC */
354 if (phb->type != PNV_PHB_IODA2) {
355 pr_info(" Not support M64 window\n");
356 return;
357 }
358
Guo Chao262af552014-07-21 14:42:30 +1000359 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
360 pr_info(" Firmware too old to support M64 window\n");
361 return;
362 }
363
364 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
365 if (!r) {
366 pr_info(" No <ibm,opal-m64-window> on %s\n",
367 dn->full_name);
368 return;
369 }
370
Guo Chao262af552014-07-21 14:42:30 +1000371 res = &hose->mem_resources[1];
372 res->start = of_translate_address(dn, r + 2);
373 res->end = res->start + of_read_number(r + 4, 2) - 1;
374 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
375 pci_addr = of_read_number(r, 2);
376 hose->mem_offset[1] = res->start - pci_addr;
377
378 phb->ioda.m64_size = resource_size(res);
379 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
380 phb->ioda.m64_base = pci_addr;
381
Wei Yange9863e62014-12-12 12:39:37 +0800382 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
383 res->start, res->end, pci_addr);
384
Guo Chao262af552014-07-21 14:42:30 +1000385 /* Use last M64 BAR to cover M64 window */
386 phb->ioda.m64_bar_idx = 15;
387 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shan5ef73562014-11-12 13:36:06 +1100388 phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000389 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
390}
391
Gavin Shan49dec922014-07-21 14:42:33 +1000392static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
393{
394 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
395 struct pnv_ioda_pe *slave;
396 s64 rc;
397
398 /* Fetch master PE */
399 if (pe->flags & PNV_IODA_PE_SLAVE) {
400 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100401 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
402 return;
403
Gavin Shan49dec922014-07-21 14:42:33 +1000404 pe_no = pe->pe_number;
405 }
406
407 /* Freeze master PE */
408 rc = opal_pci_eeh_freeze_set(phb->opal_id,
409 pe_no,
410 OPAL_EEH_ACTION_SET_FREEZE_ALL);
411 if (rc != OPAL_SUCCESS) {
412 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
413 __func__, rc, phb->hose->global_number, pe_no);
414 return;
415 }
416
417 /* Freeze slave PEs */
418 if (!(pe->flags & PNV_IODA_PE_MASTER))
419 return;
420
421 list_for_each_entry(slave, &pe->slaves, list) {
422 rc = opal_pci_eeh_freeze_set(phb->opal_id,
423 slave->pe_number,
424 OPAL_EEH_ACTION_SET_FREEZE_ALL);
425 if (rc != OPAL_SUCCESS)
426 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
427 __func__, rc, phb->hose->global_number,
428 slave->pe_number);
429 }
430}
431
Anton Blancharde51df2c2014-08-20 08:55:18 +1000432static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000433{
434 struct pnv_ioda_pe *pe, *slave;
435 s64 rc;
436
437 /* Find master PE */
438 pe = &phb->ioda.pe_array[pe_no];
439 if (pe->flags & PNV_IODA_PE_SLAVE) {
440 pe = pe->master;
441 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
442 pe_no = pe->pe_number;
443 }
444
445 /* Clear frozen state for master PE */
446 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
447 if (rc != OPAL_SUCCESS) {
448 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
449 __func__, rc, opt, phb->hose->global_number, pe_no);
450 return -EIO;
451 }
452
453 if (!(pe->flags & PNV_IODA_PE_MASTER))
454 return 0;
455
456 /* Clear frozen state for slave PEs */
457 list_for_each_entry(slave, &pe->slaves, list) {
458 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
459 slave->pe_number,
460 opt);
461 if (rc != OPAL_SUCCESS) {
462 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
463 __func__, rc, opt, phb->hose->global_number,
464 slave->pe_number);
465 return -EIO;
466 }
467 }
468
469 return 0;
470}
471
472static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
473{
474 struct pnv_ioda_pe *slave, *pe;
475 u8 fstate, state;
476 __be16 pcierr;
477 s64 rc;
478
479 /* Sanity check on PE number */
480 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
481 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
482
483 /*
484 * Fetch the master PE and the PE instance might be
485 * not initialized yet.
486 */
487 pe = &phb->ioda.pe_array[pe_no];
488 if (pe->flags & PNV_IODA_PE_SLAVE) {
489 pe = pe->master;
490 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
491 pe_no = pe->pe_number;
492 }
493
494 /* Check the master PE */
495 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
496 &state, &pcierr, NULL);
497 if (rc != OPAL_SUCCESS) {
498 pr_warn("%s: Failure %lld getting "
499 "PHB#%x-PE#%x state\n",
500 __func__, rc,
501 phb->hose->global_number, pe_no);
502 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
503 }
504
505 /* Check the slave PE */
506 if (!(pe->flags & PNV_IODA_PE_MASTER))
507 return state;
508
509 list_for_each_entry(slave, &pe->slaves, list) {
510 rc = opal_pci_eeh_freeze_status(phb->opal_id,
511 slave->pe_number,
512 &fstate,
513 &pcierr,
514 NULL);
515 if (rc != OPAL_SUCCESS) {
516 pr_warn("%s: Failure %lld getting "
517 "PHB#%x-PE#%x state\n",
518 __func__, rc,
519 phb->hose->global_number, slave->pe_number);
520 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
521 }
522
523 /*
524 * Override the result based on the ascending
525 * priority.
526 */
527 if (fstate > state)
528 state = fstate;
529 }
530
531 return state;
532}
533
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000534/* Currently those 2 are only used when MSIs are enabled, this will change
535 * but in the meantime, we need to protect them to avoid warnings
536 */
537#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800538static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000539{
540 struct pci_controller *hose = pci_bus_to_host(dev->bus);
541 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000542 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000543
544 if (!pdn)
545 return NULL;
546 if (pdn->pe_number == IODA_INVALID_PE)
547 return NULL;
548 return &phb->ioda.pe_array[pdn->pe_number];
549}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000550#endif /* CONFIG_PCI_MSI */
551
Gavin Shanb131a842014-11-12 13:36:08 +1100552static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
553 struct pnv_ioda_pe *parent,
554 struct pnv_ioda_pe *child,
555 bool is_add)
556{
557 const char *desc = is_add ? "adding" : "removing";
558 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
559 OPAL_REMOVE_PE_FROM_DOMAIN;
560 struct pnv_ioda_pe *slave;
561 long rc;
562
563 /* Parent PE affects child PE */
564 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
565 child->pe_number, op);
566 if (rc != OPAL_SUCCESS) {
567 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
568 rc, desc);
569 return -ENXIO;
570 }
571
572 if (!(child->flags & PNV_IODA_PE_MASTER))
573 return 0;
574
575 /* Compound case: parent PE affects slave PEs */
576 list_for_each_entry(slave, &child->slaves, list) {
577 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
578 slave->pe_number, op);
579 if (rc != OPAL_SUCCESS) {
580 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
581 rc, desc);
582 return -ENXIO;
583 }
584 }
585
586 return 0;
587}
588
589static int pnv_ioda_set_peltv(struct pnv_phb *phb,
590 struct pnv_ioda_pe *pe,
591 bool is_add)
592{
593 struct pnv_ioda_pe *slave;
594 struct pci_dev *pdev;
595 int ret;
596
597 /*
598 * Clear PE frozen state. If it's master PE, we need
599 * clear slave PE frozen state as well.
600 */
601 if (is_add) {
602 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
603 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
604 if (pe->flags & PNV_IODA_PE_MASTER) {
605 list_for_each_entry(slave, &pe->slaves, list)
606 opal_pci_eeh_freeze_clear(phb->opal_id,
607 slave->pe_number,
608 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
609 }
610 }
611
612 /*
613 * Associate PE in PELT. We need add the PE into the
614 * corresponding PELT-V as well. Otherwise, the error
615 * originated from the PE might contribute to other
616 * PEs.
617 */
618 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
619 if (ret)
620 return ret;
621
622 /* For compound PEs, any one affects all of them */
623 if (pe->flags & PNV_IODA_PE_MASTER) {
624 list_for_each_entry(slave, &pe->slaves, list) {
625 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
626 if (ret)
627 return ret;
628 }
629 }
630
631 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
632 pdev = pe->pbus->self;
633 else
634 pdev = pe->pdev->bus->self;
635 while (pdev) {
636 struct pci_dn *pdn = pci_get_pdn(pdev);
637 struct pnv_ioda_pe *parent;
638
639 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
640 parent = &phb->ioda.pe_array[pdn->pe_number];
641 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
642 if (ret)
643 return ret;
644 }
645
646 pdev = pdev->bus->self;
647 }
648
649 return 0;
650}
651
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800652static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000653{
654 struct pci_dev *parent;
655 uint8_t bcomp, dcomp, fcomp;
656 long rc, rid_end, rid;
657
658 /* Bus validation ? */
659 if (pe->pbus) {
660 int count;
661
662 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
663 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
664 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000665 if (pe->flags & PNV_IODA_PE_BUS_ALL)
666 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
667 else
668 count = 1;
669
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000670 switch(count) {
671 case 1: bcomp = OpalPciBusAll; break;
672 case 2: bcomp = OpalPciBus7Bits; break;
673 case 4: bcomp = OpalPciBus6Bits; break;
674 case 8: bcomp = OpalPciBus5Bits; break;
675 case 16: bcomp = OpalPciBus4Bits; break;
676 case 32: bcomp = OpalPciBus3Bits; break;
677 default:
678 pr_err("%s: Number of subordinate busses %d"
679 " unsupported\n",
680 pci_name(pe->pbus->self), count);
681 /* Do an exact match only */
682 bcomp = OpalPciBusAll;
683 }
684 rid_end = pe->rid + (count << 8);
685 } else {
686 parent = pe->pdev->bus->self;
687 bcomp = OpalPciBusAll;
688 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
689 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
690 rid_end = pe->rid + 1;
691 }
692
Gavin Shan631ad692013-11-04 16:32:46 +0800693 /*
694 * Associate PE in PELT. We need add the PE into the
695 * corresponding PELT-V as well. Otherwise, the error
696 * originated from the PE might contribute to other
697 * PEs.
698 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000699 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
700 bcomp, dcomp, fcomp, OPAL_MAP_PE);
701 if (rc) {
702 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
703 return -ENXIO;
704 }
Gavin Shan631ad692013-11-04 16:32:46 +0800705
Gavin Shanb131a842014-11-12 13:36:08 +1100706 /* Configure PELTV */
707 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000708
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000709 /* Setup reverse map */
710 for (rid = pe->rid; rid < rid_end; rid++)
711 phb->ioda.pe_rmap[rid] = pe->pe_number;
712
713 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100714 if (phb->type != PNV_PHB_IODA1) {
715 pe->mve_number = 0;
716 goto out;
717 }
718
719 pe->mve_number = pe->pe_number;
720 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
721 if (rc != OPAL_SUCCESS) {
722 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
723 rc, pe->mve_number);
724 pe->mve_number = -1;
725 } else {
726 rc = opal_pci_set_mve_enable(phb->opal_id,
727 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000728 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100729 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000730 rc, pe->mve_number);
731 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000732 }
Gavin Shan4773f762014-11-12 13:36:09 +1100733 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000734
Gavin Shan4773f762014-11-12 13:36:09 +1100735out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000736 return 0;
737}
738
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800739static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
740 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000741{
742 struct pnv_ioda_pe *lpe;
743
Gavin Shan7ebdf952012-08-20 03:49:15 +0000744 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000745 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000746 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000747 return;
748 }
749 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000750 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000751}
752
753static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
754{
755 /* This is quite simplistic. The "base" weight of a device
756 * is 10. 0 means no DMA is to be accounted for it.
757 */
758
759 /* If it's a bridge, no DMA */
760 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
761 return 0;
762
763 /* Reduce the weight of slow USB controllers */
764 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
765 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
766 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
767 return 3;
768
769 /* Increase the weight of RAID (includes Obsidian) */
770 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
771 return 15;
772
773 /* Default */
774 return 10;
775}
776
Gavin Shanfb446ad2012-08-20 03:49:14 +0000777#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800778static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000779{
780 struct pci_controller *hose = pci_bus_to_host(dev->bus);
781 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000782 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000783 struct pnv_ioda_pe *pe;
784 int pe_num;
785
786 if (!pdn) {
787 pr_err("%s: Device tree node not associated properly\n",
788 pci_name(dev));
789 return NULL;
790 }
791 if (pdn->pe_number != IODA_INVALID_PE)
792 return NULL;
793
794 /* PE#0 has been pre-set */
795 if (dev->bus->number == 0)
796 pe_num = 0;
797 else
798 pe_num = pnv_ioda_alloc_pe(phb);
799 if (pe_num == IODA_INVALID_PE) {
800 pr_warning("%s: Not enough PE# available, disabling device\n",
801 pci_name(dev));
802 return NULL;
803 }
804
805 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
806 * pointer in the PE data structure, both should be destroyed at the
807 * same time. However, this needs to be looked at more closely again
808 * once we actually start removing things (Hotplug, SR-IOV, ...)
809 *
810 * At some point we want to remove the PDN completely anyways
811 */
812 pe = &phb->ioda.pe_array[pe_num];
813 pci_dev_get(dev);
814 pdn->pcidev = dev;
815 pdn->pe_number = pe_num;
816 pe->pdev = dev;
817 pe->pbus = NULL;
818 pe->tce32_seg = -1;
819 pe->mve_number = -1;
820 pe->rid = dev->bus->number << 8 | pdn->devfn;
821
822 pe_info(pe, "Associated device to PE\n");
823
824 if (pnv_ioda_configure_pe(phb, pe)) {
825 /* XXX What do we do here ? */
826 if (pe_num)
827 pnv_ioda_free_pe(phb, pe_num);
828 pdn->pe_number = IODA_INVALID_PE;
829 pe->pdev = NULL;
830 pci_dev_put(dev);
831 return NULL;
832 }
833
834 /* Assign a DMA weight to the device */
835 pe->dma_weight = pnv_ioda_dma_weight(dev);
836 if (pe->dma_weight != 0) {
837 phb->ioda.dma_weight += pe->dma_weight;
838 phb->ioda.dma_pe_count++;
839 }
840
841 /* Link the PE */
842 pnv_ioda_link_pe_by_weight(phb, pe);
843
844 return pe;
845}
Gavin Shanfb446ad2012-08-20 03:49:14 +0000846#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000847
848static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
849{
850 struct pci_dev *dev;
851
852 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000853 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000854
855 if (pdn == NULL) {
856 pr_warn("%s: No device node associated with device !\n",
857 pci_name(dev));
858 continue;
859 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000860 pdn->pcidev = dev;
861 pdn->pe_number = pe->pe_number;
862 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +0000863 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000864 pnv_ioda_setup_same_PE(dev->subordinate, pe);
865 }
866}
867
Gavin Shanfb446ad2012-08-20 03:49:14 +0000868/*
869 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
870 * single PCI bus. Another one that contains the primary PCI bus and its
871 * subordinate PCI devices and buses. The second type of PE is normally
872 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
873 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800874static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000875{
Gavin Shanfb446ad2012-08-20 03:49:14 +0000876 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000877 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000878 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +1000879 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000880
Guo Chao262af552014-07-21 14:42:30 +1000881 /* Check if PE is determined by M64 */
882 if (phb->pick_m64_pe)
883 pe_num = phb->pick_m64_pe(phb, bus, all);
884
885 /* The PE number isn't pinned by M64 */
886 if (pe_num == IODA_INVALID_PE)
887 pe_num = pnv_ioda_alloc_pe(phb);
888
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000889 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000890 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
891 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000892 return;
893 }
894
895 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +1000896 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000897 pe->pbus = bus;
898 pe->pdev = NULL;
899 pe->tce32_seg = -1;
900 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -0700901 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000902 pe->dma_weight = 0;
903
Gavin Shanfb446ad2012-08-20 03:49:14 +0000904 if (all)
905 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
906 bus->busn_res.start, bus->busn_res.end, pe_num);
907 else
908 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
909 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000910
911 if (pnv_ioda_configure_pe(phb, pe)) {
912 /* XXX What do we do here ? */
913 if (pe_num)
914 pnv_ioda_free_pe(phb, pe_num);
915 pe->pbus = NULL;
916 return;
917 }
918
919 /* Associate it with all child devices */
920 pnv_ioda_setup_same_PE(bus, pe);
921
Gavin Shan7ebdf952012-08-20 03:49:15 +0000922 /* Put PE to the list */
923 list_add_tail(&pe->list, &phb->ioda.pe_list);
924
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000925 /* Account for one DMA PE if at least one DMA capable device exist
926 * below the bridge
927 */
928 if (pe->dma_weight != 0) {
929 phb->ioda.dma_weight += pe->dma_weight;
930 phb->ioda.dma_pe_count++;
931 }
932
933 /* Link the PE */
934 pnv_ioda_link_pe_by_weight(phb, pe);
935}
936
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800937static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000938{
939 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000940
941 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000942
943 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +0000944 if (dev->subordinate) {
945 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
946 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
947 else
948 pnv_ioda_setup_PEs(dev->subordinate);
949 }
950 }
951}
952
953/*
954 * Configure PEs so that the downstream PCI buses and devices
955 * could have their associated PE#. Unfortunately, we didn't
956 * figure out the way to identify the PLX bridge yet. So we
957 * simply put the PCI bus and the subordinate behind the root
958 * port to PE# here. The game rule here is expected to be changed
959 * as soon as we can detected PLX bridge correctly.
960 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800961static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +0000962{
963 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +1000964 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000965
966 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +1000967 phb = hose->private_data;
968
969 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +1100970 if (phb->reserve_m64_pe)
971 phb->reserve_m64_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +1000972
Gavin Shanfb446ad2012-08-20 03:49:14 +0000973 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000974 }
975}
976
Gavin Shana8b2f822015-03-25 16:23:52 +0800977#ifdef CONFIG_PCI_IOV
978int pcibios_sriov_disable(struct pci_dev *pdev)
979{
980 /* Release PCI data */
981 remove_dev_pci_data(pdev);
982 return 0;
983}
984
985int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
986{
987 /* Allocate PCI data */
988 add_dev_pci_data(pdev);
989 return 0;
990}
991#endif /* CONFIG_PCI_IOV */
992
Gavin Shan959c9bd2013-04-25 19:21:02 +0000993static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000994{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000995 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +0000996 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000997
Gavin Shan959c9bd2013-04-25 19:21:02 +0000998 /*
999 * The function can be called while the PE#
1000 * hasn't been assigned. Do nothing for the
1001 * case.
1002 */
1003 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1004 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001005
Gavin Shan959c9bd2013-04-25 19:21:02 +00001006 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001007 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Gavin Shan763fe0a2014-08-06 17:10:16 +10001008 set_iommu_table_base_and_group(&pdev->dev, &pe->tce32_table);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001009}
1010
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001011static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
1012 struct pci_dev *pdev, u64 dma_mask)
1013{
1014 struct pci_dn *pdn = pci_get_pdn(pdev);
1015 struct pnv_ioda_pe *pe;
1016 uint64_t top;
1017 bool bypass = false;
1018
1019 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1020 return -ENODEV;;
1021
1022 pe = &phb->ioda.pe_array[pdn->pe_number];
1023 if (pe->tce_bypass_enabled) {
1024 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1025 bypass = (dma_mask >= top);
1026 }
1027
1028 if (bypass) {
1029 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1030 set_dma_ops(&pdev->dev, &dma_direct_ops);
1031 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
1032 } else {
1033 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1034 set_dma_ops(&pdev->dev, &dma_iommu_ops);
1035 set_iommu_table_base(&pdev->dev, &pe->tce32_table);
1036 }
Brian W Harta32305b2014-07-31 14:24:37 -05001037 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001038 return 0;
1039}
1040
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001041static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
1042 struct pci_dev *pdev)
1043{
1044 struct pci_dn *pdn = pci_get_pdn(pdev);
1045 struct pnv_ioda_pe *pe;
1046 u64 end, mask;
1047
1048 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1049 return 0;
1050
1051 pe = &phb->ioda.pe_array[pdn->pe_number];
1052 if (!pe->tce_bypass_enabled)
1053 return __dma_get_required_mask(&pdev->dev);
1054
1055
1056 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1057 mask = 1ULL << (fls64(end) - 1);
1058 mask += mask - 1;
1059
1060 return mask;
1061}
1062
Gavin Shandff4a392014-07-15 17:00:55 +10001063static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
1064 struct pci_bus *bus,
1065 bool add_to_iommu_group)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001066{
1067 struct pci_dev *dev;
1068
1069 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shandff4a392014-07-15 17:00:55 +10001070 if (add_to_iommu_group)
1071 set_iommu_table_base_and_group(&dev->dev,
1072 &pe->tce32_table);
1073 else
1074 set_iommu_table_base(&dev->dev, &pe->tce32_table);
1075
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001076 if (dev->subordinate)
Gavin Shandff4a392014-07-15 17:00:55 +10001077 pnv_ioda_setup_bus_dma(pe, dev->subordinate,
1078 add_to_iommu_group);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001079 }
1080}
1081
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001082static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
1083 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001084 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001085{
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001086 __be64 __iomem *invalidate = rm ?
1087 (__be64 __iomem *)pe->tce_inval_reg_phys :
1088 (__be64 __iomem *)tbl->it_index;
Gavin Shan4cce9552013-04-25 19:21:00 +00001089 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001090 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001091
1092 start = __pa(startp);
1093 end = __pa(endp);
1094
1095 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1096 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001097 start <<= shift;
1098 end <<= shift;
1099 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001100 start |= tbl->it_busno;
1101 end |= tbl->it_busno;
1102 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1103 /* p7ioc-style invalidation, 2 TCEs per write */
1104 start |= (1ull << 63);
1105 end |= (1ull << 63);
1106 inc = 16;
1107 } else {
1108 /* Default (older HW) */
1109 inc = 128;
1110 }
1111
1112 end |= inc - 1; /* round up end to be different than start */
1113
1114 mb(); /* Ensure above stores are visible */
1115 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001116 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001117 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001118 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001119 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001120 start += inc;
1121 }
1122
1123 /*
1124 * The iommu layer will do another mb() for us on build()
1125 * and we don't care on free()
1126 */
1127}
1128
1129static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
1130 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001131 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001132{
1133 unsigned long start, end, inc;
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001134 __be64 __iomem *invalidate = rm ?
1135 (__be64 __iomem *)pe->tce_inval_reg_phys :
1136 (__be64 __iomem *)tbl->it_index;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001137 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001138
1139 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001140 start = 0x2ull << 60;
Gavin Shan4cce9552013-04-25 19:21:00 +00001141 start |= (pe->pe_number & 0xFF);
1142 end = start;
1143
1144 /* Figure out the start, end and step */
1145 inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001146 start |= (inc << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001147 inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001148 end |= (inc << shift);
1149 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001150 mb();
1151
1152 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001153 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001154 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001155 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001156 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001157 start += inc;
1158 }
1159}
1160
1161void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001162 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001163{
1164 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
1165 tce32_table);
1166 struct pnv_phb *phb = pe->phb;
1167
1168 if (phb->type == PNV_PHB_IODA1)
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001169 pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00001170 else
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001171 pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00001172}
1173
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001174static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1175 struct pnv_ioda_pe *pe, unsigned int base,
1176 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001177{
1178
1179 struct page *tce_mem = NULL;
1180 const __be64 *swinvp;
1181 struct iommu_table *tbl;
1182 unsigned int i;
1183 int64_t rc;
1184 void *addr;
1185
1186 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
1187#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
1188
1189 /* XXX FIXME: Handle 64-bit only DMA devices */
1190 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1191 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1192
1193 /* We shouldn't already have a 32-bit DMA associated */
1194 if (WARN_ON(pe->tce32_seg >= 0))
1195 return;
1196
1197 /* Grab a 32-bit TCE table */
1198 pe->tce32_seg = base;
1199 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1200 (base << 28), ((base + segs) << 28) - 1);
1201
1202 /* XXX Currently, we allocate one big contiguous table for the
1203 * TCEs. We only really need one chunk per 256M of TCE space
1204 * (ie per segment) but that's an optimization for later, it
1205 * requires some added smarts with our get/put_tce implementation
1206 */
1207 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1208 get_order(TCE32_TABLE_SIZE * segs));
1209 if (!tce_mem) {
1210 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1211 goto fail;
1212 }
1213 addr = page_address(tce_mem);
1214 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1215
1216 /* Configure HW */
1217 for (i = 0; i < segs; i++) {
1218 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1219 pe->pe_number,
1220 base + i, 1,
1221 __pa(addr) + TCE32_TABLE_SIZE * i,
1222 TCE32_TABLE_SIZE, 0x1000);
1223 if (rc) {
1224 pe_err(pe, " Failed to configure 32-bit TCE table,"
1225 " err %ld\n", rc);
1226 goto fail;
1227 }
1228 }
1229
1230 /* Setup linux iommu table */
1231 tbl = &pe->tce32_table;
1232 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001233 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001234
1235 /* OPAL variant of P7IOC SW invalidated TCEs */
1236 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1237 if (swinvp) {
1238 /* We need a couple more fields -- an address and a data
1239 * to or. Since the bus is only printed out on table free
1240 * errors, and on the first pass the data will be a relative
1241 * bus number, print that out instead.
1242 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001243 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1244 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1245 8);
Gavin Shan65fd7662014-04-24 18:00:28 +10001246 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1247 TCE_PCI_SWINV_FREE |
1248 TCE_PCI_SWINV_PAIR);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001249 }
1250 iommu_init_table(tbl, phb->hose->node);
Gavin Shane9bc03f2014-04-24 18:00:29 +10001251 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001252
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001253 if (pe->pdev)
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +11001254 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001255 else
Gavin Shandff4a392014-07-15 17:00:55 +10001256 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001257
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001258 return;
1259 fail:
1260 /* XXX Failure: Try to fallback to 64-bit only ? */
1261 if (pe->tce32_seg >= 0)
1262 pe->tce32_seg = -1;
1263 if (tce_mem)
1264 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
1265}
1266
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001267static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
1268{
1269 struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
1270 tce32_table);
1271 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1272 int64_t rc;
1273
1274 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1275 if (enable) {
1276 phys_addr_t top = memblock_end_of_DRAM();
1277
1278 top = roundup_pow_of_two(top);
1279 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1280 pe->pe_number,
1281 window_id,
1282 pe->tce_bypass_base,
1283 top);
1284 } else {
1285 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1286 pe->pe_number,
1287 window_id,
1288 pe->tce_bypass_base,
1289 0);
1290
1291 /*
Gavin Shandff4a392014-07-15 17:00:55 +10001292 * EEH needs the mapping between IOMMU table and group
1293 * of those VFIO/KVM pass-through devices. We can postpone
1294 * resetting DMA ops until the DMA mask is configured in
1295 * host side.
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001296 */
Gavin Shandff4a392014-07-15 17:00:55 +10001297 if (pe->pdev)
1298 set_iommu_table_base(&pe->pdev->dev, tbl);
1299 else
1300 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001301 }
1302 if (rc)
1303 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1304 else
1305 pe->tce_bypass_enabled = enable;
1306}
1307
1308static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
1309 struct pnv_ioda_pe *pe)
1310{
1311 /* TVE #1 is selected by PCI address bit 59 */
1312 pe->tce_bypass_base = 1ull << 59;
1313
1314 /* Install set_bypass callback for VFIO */
1315 pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass;
1316
1317 /* Enable bypass by default */
1318 pnv_pci_ioda2_set_bypass(&pe->tce32_table, true);
1319}
1320
Gavin Shan373f5652013-04-25 19:21:01 +00001321static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1322 struct pnv_ioda_pe *pe)
1323{
1324 struct page *tce_mem = NULL;
1325 void *addr;
1326 const __be64 *swinvp;
1327 struct iommu_table *tbl;
1328 unsigned int tce_table_size, end;
1329 int64_t rc;
1330
1331 /* We shouldn't already have a 32-bit DMA associated */
1332 if (WARN_ON(pe->tce32_seg >= 0))
1333 return;
1334
1335 /* The PE will reserve all possible 32-bits space */
1336 pe->tce32_seg = 0;
1337 end = (1 << ilog2(phb->ioda.m32_pci_base));
1338 tce_table_size = (end / 0x1000) * 8;
1339 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1340 end);
1341
1342 /* Allocate TCE table */
1343 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1344 get_order(tce_table_size));
1345 if (!tce_mem) {
1346 pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
1347 goto fail;
1348 }
1349 addr = page_address(tce_mem);
1350 memset(addr, 0, tce_table_size);
1351
1352 /*
1353 * Map TCE table through TVT. The TVE index is the PE number
1354 * shifted by 1 bit for 32-bits DMA space.
1355 */
1356 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1357 pe->pe_number << 1, 1, __pa(addr),
1358 tce_table_size, 0x1000);
1359 if (rc) {
1360 pe_err(pe, "Failed to configure 32-bit TCE table,"
1361 " err %ld\n", rc);
1362 goto fail;
1363 }
1364
1365 /* Setup linux iommu table */
1366 tbl = &pe->tce32_table;
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001367 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
1368 IOMMU_PAGE_SHIFT_4K);
Gavin Shan373f5652013-04-25 19:21:01 +00001369
1370 /* OPAL variant of PHB3 invalidated TCEs */
1371 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1372 if (swinvp) {
1373 /* We need a couple more fields -- an address and a data
1374 * to or. Since the bus is only printed out on table free
1375 * errors, and on the first pass the data will be a relative
1376 * bus number, print that out instead.
1377 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001378 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1379 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1380 8);
Gavin Shan65fd7662014-04-24 18:00:28 +10001381 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
Gavin Shan373f5652013-04-25 19:21:01 +00001382 }
1383 iommu_init_table(tbl, phb->hose->node);
Gavin Shane9bc03f2014-04-24 18:00:29 +10001384 iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
Gavin Shan373f5652013-04-25 19:21:01 +00001385
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001386 if (pe->pdev)
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +11001387 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001388 else
Gavin Shandff4a392014-07-15 17:00:55 +10001389 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001390
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001391 /* Also create a bypass window */
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -02001392 if (!pnv_iommu_bypass_disabled)
1393 pnv_pci_ioda2_setup_bypass_pe(phb, pe);
1394
Gavin Shan373f5652013-04-25 19:21:01 +00001395 return;
1396fail:
1397 if (pe->tce32_seg >= 0)
1398 pe->tce32_seg = -1;
1399 if (tce_mem)
1400 __free_pages(tce_mem, get_order(tce_table_size));
1401}
1402
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001403static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001404{
1405 struct pci_controller *hose = phb->hose;
1406 unsigned int residual, remaining, segs, tw, base;
1407 struct pnv_ioda_pe *pe;
1408
1409 /* If we have more PE# than segments available, hand out one
1410 * per PE until we run out and let the rest fail. If not,
1411 * then we assign at least one segment per PE, plus more based
1412 * on the amount of devices under that PE
1413 */
1414 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
1415 residual = 0;
1416 else
1417 residual = phb->ioda.tce32_count -
1418 phb->ioda.dma_pe_count;
1419
1420 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
1421 hose->global_number, phb->ioda.tce32_count);
1422 pr_info("PCI: %d PE# for a total weight of %d\n",
1423 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
1424
1425 /* Walk our PE list and configure their DMA segments, hand them
1426 * out one base segment plus any residual segments based on
1427 * weight
1428 */
1429 remaining = phb->ioda.tce32_count;
1430 tw = phb->ioda.dma_weight;
1431 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00001432 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001433 if (!pe->dma_weight)
1434 continue;
1435 if (!remaining) {
1436 pe_warn(pe, "No DMA32 resources available\n");
1437 continue;
1438 }
1439 segs = 1;
1440 if (residual) {
1441 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
1442 if (segs > remaining)
1443 segs = remaining;
1444 }
Gavin Shan373f5652013-04-25 19:21:01 +00001445
1446 /*
1447 * For IODA2 compliant PHB3, we needn't care about the weight.
1448 * The all available 32-bits DMA space will be assigned to
1449 * the specific PE.
1450 */
1451 if (phb->type == PNV_PHB_IODA1) {
1452 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1453 pe->dma_weight, segs);
1454 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1455 } else {
1456 pe_info(pe, "Assign DMA32 space\n");
1457 segs = 0;
1458 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1459 }
1460
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001461 remaining -= segs;
1462 base += segs;
1463 }
1464}
1465
1466#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00001467static void pnv_ioda2_msi_eoi(struct irq_data *d)
1468{
1469 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1470 struct irq_chip *chip = irq_data_get_irq_chip(d);
1471 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
1472 ioda.irq_chip);
1473 int64_t rc;
1474
1475 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
1476 WARN_ON_ONCE(rc);
1477
1478 icp_native_eoi(d);
1479}
1480
Ian Munsiefd9a1c22014-10-08 19:54:55 +11001481
1482static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
1483{
1484 struct irq_data *idata;
1485 struct irq_chip *ichip;
1486
1487 if (phb->type != PNV_PHB_IODA2)
1488 return;
1489
1490 if (!phb->ioda.irq_chip_init) {
1491 /*
1492 * First time we setup an MSI IRQ, we need to setup the
1493 * corresponding IRQ chip to route correctly.
1494 */
1495 idata = irq_get_irq_data(virq);
1496 ichip = irq_data_get_irq_chip(idata);
1497 phb->ioda.irq_chip_init = 1;
1498 phb->ioda.irq_chip = *ichip;
1499 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
1500 }
1501 irq_set_chip(virq, &phb->ioda.irq_chip);
1502}
1503
Ian Munsie80c49c72014-10-08 19:54:57 +11001504#ifdef CONFIG_CXL_BASE
1505
Ryan Grimm6f963ec2015-01-28 20:16:04 -06001506struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11001507{
1508 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1509
Ryan Grimm6f963ec2015-01-28 20:16:04 -06001510 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11001511}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06001512EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11001513
Ryan Grimm1212aa12015-01-19 11:52:50 -06001514int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11001515{
1516 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1517 struct pnv_phb *phb = hose->private_data;
1518 struct pnv_ioda_pe *pe;
1519 int rc;
1520
1521 pe = pnv_ioda_get_pe(dev);
1522 if (!pe)
1523 return -ENODEV;
1524
1525 pe_info(pe, "Switching PHB to CXL\n");
1526
Ryan Grimm1212aa12015-01-19 11:52:50 -06001527 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11001528 if (rc)
1529 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
1530
1531 return rc;
1532}
Ryan Grimm1212aa12015-01-19 11:52:50 -06001533EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11001534
1535/* Find PHB for cxl dev and allocate MSI hwirqs?
1536 * Returns the absolute hardware IRQ number
1537 */
1538int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
1539{
1540 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1541 struct pnv_phb *phb = hose->private_data;
1542 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
1543
1544 if (hwirq < 0) {
1545 dev_warn(&dev->dev, "Failed to find a free MSI\n");
1546 return -ENOSPC;
1547 }
1548
1549 return phb->msi_base + hwirq;
1550}
1551EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
1552
1553void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
1554{
1555 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1556 struct pnv_phb *phb = hose->private_data;
1557
1558 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
1559}
1560EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
1561
1562void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
1563 struct pci_dev *dev)
1564{
1565 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1566 struct pnv_phb *phb = hose->private_data;
1567 int i, hwirq;
1568
1569 for (i = 1; i < CXL_IRQ_RANGES; i++) {
1570 if (!irqs->range[i])
1571 continue;
1572 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
1573 i, irqs->offset[i],
1574 irqs->range[i]);
1575 hwirq = irqs->offset[i] - phb->msi_base;
1576 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
1577 irqs->range[i]);
1578 }
1579}
1580EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
1581
1582int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
1583 struct pci_dev *dev, int num)
1584{
1585 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1586 struct pnv_phb *phb = hose->private_data;
1587 int i, hwirq, try;
1588
1589 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
1590
1591 /* 0 is reserved for the multiplexed PSL DSI interrupt */
1592 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
1593 try = num;
1594 while (try) {
1595 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
1596 if (hwirq >= 0)
1597 break;
1598 try /= 2;
1599 }
1600 if (!try)
1601 goto fail;
1602
1603 irqs->offset[i] = phb->msi_base + hwirq;
1604 irqs->range[i] = try;
1605 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
1606 i, irqs->offset[i], irqs->range[i]);
1607 num -= try;
1608 }
1609 if (num)
1610 goto fail;
1611
1612 return 0;
1613fail:
1614 pnv_cxl_release_hwirq_ranges(irqs, dev);
1615 return -ENOSPC;
1616}
1617EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
1618
1619int pnv_cxl_get_irq_count(struct pci_dev *dev)
1620{
1621 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1622 struct pnv_phb *phb = hose->private_data;
1623
1624 return phb->msi_bmp.irq_count;
1625}
1626EXPORT_SYMBOL(pnv_cxl_get_irq_count);
1627
1628int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
1629 unsigned int virq)
1630{
1631 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1632 struct pnv_phb *phb = hose->private_data;
1633 unsigned int xive_num = hwirq - phb->msi_base;
1634 struct pnv_ioda_pe *pe;
1635 int rc;
1636
1637 if (!(pe = pnv_ioda_get_pe(dev)))
1638 return -ENODEV;
1639
1640 /* Assign XIVE to PE */
1641 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1642 if (rc) {
1643 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
1644 "hwirq 0x%x XIVE 0x%x PE\n",
1645 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
1646 return -EIO;
1647 }
1648 set_msi_irq_chip(phb, virq);
1649
1650 return 0;
1651}
1652EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
1653#endif
1654
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001655static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00001656 unsigned int hwirq, unsigned int virq,
1657 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001658{
1659 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1660 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001661 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001662 int rc;
1663
1664 /* No PE assigned ? bail out ... no MSI for you ! */
1665 if (pe == NULL)
1666 return -ENXIO;
1667
1668 /* Check if we have an MVE */
1669 if (pe->mve_number < 0)
1670 return -ENXIO;
1671
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001672 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11001673 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001674 is_64 = 0;
1675
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001676 /* Assign XIVE to PE */
1677 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1678 if (rc) {
1679 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1680 pci_name(dev), rc, xive_num);
1681 return -EIO;
1682 }
1683
1684 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001685 __be64 addr64;
1686
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001687 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1688 &addr64, &data);
1689 if (rc) {
1690 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1691 pci_name(dev), rc);
1692 return -EIO;
1693 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001694 msg->address_hi = be64_to_cpu(addr64) >> 32;
1695 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001696 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001697 __be32 addr32;
1698
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001699 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1700 &addr32, &data);
1701 if (rc) {
1702 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1703 pci_name(dev), rc);
1704 return -EIO;
1705 }
1706 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001707 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001708 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001709 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001710
Ian Munsiefd9a1c22014-10-08 19:54:55 +11001711 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00001712
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001713 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1714 " address=%x_%08x data=%x PE# %d\n",
1715 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
1716 msg->address_hi, msg->address_lo, data, pe->pe_number);
1717
1718 return 0;
1719}
1720
1721static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1722{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001723 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001724 const __be32 *prop = of_get_property(phb->hose->dn,
1725 "ibm,opal-msi-ranges", NULL);
1726 if (!prop) {
1727 /* BML Fallback */
1728 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1729 }
1730 if (!prop)
1731 return;
1732
1733 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001734 count = be32_to_cpup(prop + 1);
1735 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001736 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1737 phb->hose->global_number);
1738 return;
1739 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001740
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001741 phb->msi_setup = pnv_pci_ioda_msi_setup;
1742 phb->msi32_support = 1;
1743 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00001744 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001745}
1746#else
1747static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1748#endif /* CONFIG_PCI_MSI */
1749
Gavin Shan11685be2012-08-20 03:49:16 +00001750/*
1751 * This function is supposed to be called on basis of PE from top
1752 * to bottom style. So the the I/O or MMIO segment assigned to
1753 * parent PE could be overrided by its child PEs if necessary.
1754 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001755static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
1756 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00001757{
1758 struct pnv_phb *phb = hose->private_data;
1759 struct pci_bus_region region;
1760 struct resource *res;
1761 int i, index;
1762 int rc;
1763
1764 /*
1765 * NOTE: We only care PCI bus based PE for now. For PCI
1766 * device based PE, for example SRIOV sensitive VF should
1767 * be figured out later.
1768 */
1769 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
1770
1771 pci_bus_for_each_resource(pe->pbus, res, i) {
1772 if (!res || !res->flags ||
1773 res->start > res->end)
1774 continue;
1775
1776 if (res->flags & IORESOURCE_IO) {
1777 region.start = res->start - phb->ioda.io_pci_base;
1778 region.end = res->end - phb->ioda.io_pci_base;
1779 index = region.start / phb->ioda.io_segsize;
1780
1781 while (index < phb->ioda.total_pe &&
1782 region.start <= region.end) {
1783 phb->ioda.io_segmap[index] = pe->pe_number;
1784 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1785 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
1786 if (rc != OPAL_SUCCESS) {
1787 pr_err("%s: OPAL error %d when mapping IO "
1788 "segment #%d to PE#%d\n",
1789 __func__, rc, index, pe->pe_number);
1790 break;
1791 }
1792
1793 region.start += phb->ioda.io_segsize;
1794 index++;
1795 }
1796 } else if (res->flags & IORESOURCE_MEM) {
1797 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001798 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00001799 phb->ioda.m32_pci_base;
1800 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10001801 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00001802 phb->ioda.m32_pci_base;
1803 index = region.start / phb->ioda.m32_segsize;
1804
1805 while (index < phb->ioda.total_pe &&
1806 region.start <= region.end) {
1807 phb->ioda.m32_segmap[index] = pe->pe_number;
1808 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1809 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
1810 if (rc != OPAL_SUCCESS) {
1811 pr_err("%s: OPAL error %d when mapping M32 "
1812 "segment#%d to PE#%d",
1813 __func__, rc, index, pe->pe_number);
1814 break;
1815 }
1816
1817 region.start += phb->ioda.m32_segsize;
1818 index++;
1819 }
1820 }
1821 }
1822}
1823
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001824static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00001825{
1826 struct pci_controller *tmp, *hose;
1827 struct pnv_phb *phb;
1828 struct pnv_ioda_pe *pe;
1829
1830 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1831 phb = hose->private_data;
1832 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
1833 pnv_ioda_setup_pe_seg(hose, pe);
1834 }
1835 }
1836}
1837
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001838static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00001839{
1840 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00001841 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00001842
1843 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1844 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00001845
1846 /* Mark the PHB initialization done */
1847 phb = hose->private_data;
1848 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00001849 }
1850}
1851
Gavin Shan37c367f2013-06-20 18:13:25 +08001852static void pnv_pci_ioda_create_dbgfs(void)
1853{
1854#ifdef CONFIG_DEBUG_FS
1855 struct pci_controller *hose, *tmp;
1856 struct pnv_phb *phb;
1857 char name[16];
1858
1859 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1860 phb = hose->private_data;
1861
1862 sprintf(name, "PCI%04x", hose->global_number);
1863 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
1864 if (!phb->dbgfs)
1865 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
1866 __func__, hose->global_number);
1867 }
1868#endif /* CONFIG_DEBUG_FS */
1869}
1870
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001871static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001872{
1873 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00001874 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00001875 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08001876
Gavin Shan37c367f2013-06-20 18:13:25 +08001877 pnv_pci_ioda_create_dbgfs();
1878
Gavin Shane9cc17d2013-06-20 13:21:14 +08001879#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08001880 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04001881 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08001882#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00001883}
1884
Gavin Shan271fd032012-09-11 16:59:47 -06001885/*
1886 * Returns the alignment for I/O or memory windows for P2P
1887 * bridges. That actually depends on how PEs are segmented.
1888 * For now, we return I/O or M32 segment size for PE sensitive
1889 * P2P bridges. Otherwise, the default values (4KiB for I/O,
1890 * 1MiB for memory) will be returned.
1891 *
1892 * The current PCI bus might be put into one PE, which was
1893 * create against the parent PCI bridge. For that case, we
1894 * needn't enlarge the alignment so that we can save some
1895 * resources.
1896 */
1897static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
1898 unsigned long type)
1899{
1900 struct pci_dev *bridge;
1901 struct pci_controller *hose = pci_bus_to_host(bus);
1902 struct pnv_phb *phb = hose->private_data;
1903 int num_pci_bridges = 0;
1904
1905 bridge = bus->self;
1906 while (bridge) {
1907 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
1908 num_pci_bridges++;
1909 if (num_pci_bridges >= 2)
1910 return 1;
1911 }
1912
1913 bridge = bridge->bus->self;
1914 }
1915
Guo Chao262af552014-07-21 14:42:30 +10001916 /* We fail back to M32 if M64 isn't supported */
1917 if (phb->ioda.m64_segsize &&
1918 pnv_pci_is_mem_pref_64(type))
1919 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06001920 if (type & IORESOURCE_MEM)
1921 return phb->ioda.m32_segsize;
1922
1923 return phb->ioda.io_segsize;
1924}
1925
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001926/* Prevent enabling devices for which we couldn't properly
1927 * assign a PE
1928 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001929static int pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001930{
Gavin Shandb1266c2012-08-20 03:49:18 +00001931 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1932 struct pnv_phb *phb = hose->private_data;
1933 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001934
Gavin Shandb1266c2012-08-20 03:49:18 +00001935 /* The function is probably called while the PEs have
1936 * not be created yet. For example, resource reassignment
1937 * during PCI probe period. We just skip the check if
1938 * PEs isn't ready.
1939 */
1940 if (!phb->initialized)
1941 return 0;
1942
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001943 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001944 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1945 return -EINVAL;
Gavin Shandb1266c2012-08-20 03:49:18 +00001946
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001947 return 0;
1948}
1949
1950static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1951 u32 devfn)
1952{
1953 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1954}
1955
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001956static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
1957{
Gavin Shand1a85ee2014-09-30 12:39:05 +10001958 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10001959 OPAL_ASSERT_RESET);
1960}
1961
Anton Blancharde51df2c2014-08-20 08:55:18 +10001962static void __init pnv_pci_init_ioda_phb(struct device_node *np,
1963 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001964{
1965 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001966 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08001967 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10001968 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001969 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001970 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001971 u64 phb_id;
1972 void *aux;
1973 long rc;
1974
Gavin Shan58d714e2013-07-31 16:47:00 +08001975 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001976
1977 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1978 if (!prop64) {
1979 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
1980 return;
1981 }
1982 phb_id = be64_to_cpup(prop64);
1983 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
1984
Michael Ellermane39f223f2014-11-18 16:47:35 +11001985 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08001986
1987 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08001988 phb->hose = hose = pcibios_alloc_controller(np);
1989 if (!phb->hose) {
1990 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001991 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11001992 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001993 return;
1994 }
1995
1996 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08001997 prop32 = of_get_property(np, "bus-range", &len);
1998 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10001999 hose->first_busno = be32_to_cpu(prop32[0]);
2000 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08002001 } else {
2002 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
2003 hose->first_busno = 0;
2004 hose->last_busno = 0xff;
2005 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002006 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08002007 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002008 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00002009 phb->type = ioda_type;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002010
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00002011 /* Detect specific models for error handling */
2012 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
2013 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00002014 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00002015 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00002016 else
2017 phb->model = PNV_PHB_MODEL_UNKNOWN;
2018
Gavin Shanaa0c0332013-04-25 19:20:57 +00002019 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08002020 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002021
Gavin Shanaa0c0332013-04-25 19:20:57 +00002022 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002023 phb->regs = of_iomap(np, 0);
2024 if (phb->regs == NULL)
2025 pr_err(" Failed to map registers !\n");
2026
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002027 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08002028 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00002029 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08002030 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002031 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08002032 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
2033 if (prop32)
2034 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10002035
2036 /* Parse 64-bit MMIO range */
2037 pnv_ioda_parse_m64_window(phb);
2038
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002039 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00002040 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002041 phb->ioda.m32_size += 0x10000;
2042
2043 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002044 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002045 phb->ioda.io_size = hose->pci_io_size;
2046 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
2047 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
2048
Gavin Shanc35d2a82013-07-31 16:47:04 +08002049 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002050 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
2051 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00002052 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08002053 if (phb->type == PNV_PHB_IODA1) {
2054 iomap_off = size;
2055 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
2056 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002057 pemap_off = size;
2058 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11002059 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002060 phb->ioda.pe_alloc = aux;
2061 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08002062 if (phb->type == PNV_PHB_IODA1)
2063 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002064 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08002065 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002066
Gavin Shan7ebdf952012-08-20 03:49:15 +00002067 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002068 INIT_LIST_HEAD(&phb->ioda.pe_list);
2069
2070 /* Calculate how many 32-bit TCE segments we have */
2071 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
2072
Gavin Shanaa0c0332013-04-25 19:20:57 +00002073#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002074 rc = opal_pci_set_phb_mem_window(opal->phb_id,
2075 window_type,
2076 window_num,
2077 starting_real_address,
2078 starting_pci_address,
2079 segment_size);
2080#endif
2081
Guo Chao262af552014-07-21 14:42:30 +10002082 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
2083 phb->ioda.total_pe, phb->ioda.reserved_pe,
2084 phb->ioda.m32_size, phb->ioda.m32_segsize);
2085 if (phb->ioda.m64_size)
2086 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
2087 phb->ioda.m64_size, phb->ioda.m64_segsize);
2088 if (phb->ioda.io_size)
2089 pr_info(" IO: 0x%x [segment=0x%x]\n",
2090 phb->ioda.io_size, phb->ioda.io_segsize);
2091
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002092
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002093 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10002094 phb->get_pe_state = pnv_ioda_get_pe_state;
2095 phb->freeze_pe = pnv_ioda_freeze_pe;
2096 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002097
2098 /* Setup RID -> PE mapping function */
2099 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
2100
2101 /* Setup TCEs */
2102 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002103 phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10002104 phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002105
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10002106 /* Setup shutdown function for kexec */
2107 phb->shutdown = pnv_pci_ioda_shutdown;
2108
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002109 /* Setup MSI support */
2110 pnv_pci_init_ioda_msis(phb);
2111
Gavin Shanc40a4212012-08-20 03:49:20 +00002112 /*
2113 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
2114 * to let the PCI core do resource assignment. It's supposed
2115 * that the PCI core will do correct I/O and MMIO alignment
2116 * for the P2P bridge bars so that each PCI bus (excluding
2117 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002118 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00002119 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002120 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
Gavin Shan271fd032012-09-11 16:59:47 -06002121 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
Gavin Shand92a2082014-04-24 18:00:24 +10002122 ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
Gavin Shanc40a4212012-08-20 03:49:20 +00002123 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002124
2125 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10002126 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002127 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00002128 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10002129
2130 /* If we're running in kdump kerenl, the previous kerenl never
2131 * shutdown PCI devices correctly. We already got IODA table
2132 * cleaned out. So we have to issue PHB reset to stop all PCI
2133 * transactions from previous kerenl.
2134 */
2135 if (is_kdump_kernel()) {
2136 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11002137 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
2138 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10002139 }
Guo Chao262af552014-07-21 14:42:30 +10002140
Gavin Shan9e9e8932014-11-12 13:36:05 +11002141 /* Remove M64 resource if we can't configure it successfully */
2142 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10002143 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00002144}
2145
Bjorn Helgaas67975002013-07-02 12:20:03 -06002146void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00002147{
Gavin Shane9cc17d2013-06-20 13:21:14 +08002148 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002149}
2150
2151void __init pnv_pci_init_ioda_hub(struct device_node *np)
2152{
2153 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10002154 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002155 u64 hub_id;
2156
2157 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
2158
2159 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
2160 if (!prop64) {
2161 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
2162 return;
2163 }
2164 hub_id = be64_to_cpup(prop64);
2165 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
2166
2167 /* Count child PHBs */
2168 for_each_child_of_node(np, phbn) {
2169 /* Look for IODA1 PHBs */
2170 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08002171 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002172 }
2173}