blob: 5187d164cfe1331d46101154bedfce215ebe8a07 [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
Wei Yang781a8682015-03-25 16:23:57 +080047/* 256M DMA window, 4K TCE pages, 8 bytes TCE */
48#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
49
Joe Perches6d31c2f2014-09-21 10:55:06 -070050static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
51 const char *fmt, ...)
52{
53 struct va_format vaf;
54 va_list args;
55 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000056
Joe Perches6d31c2f2014-09-21 10:55:06 -070057 va_start(args, fmt);
58
59 vaf.fmt = fmt;
60 vaf.va = &args;
61
Wei Yang781a8682015-03-25 16:23:57 +080062 if (pe->flags & PNV_IODA_PE_DEV)
Joe Perches6d31c2f2014-09-21 10:55:06 -070063 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
Wei Yang781a8682015-03-25 16:23:57 +080064 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Joe Perches6d31c2f2014-09-21 10:55:06 -070065 sprintf(pfix, "%04x:%02x ",
66 pci_domain_nr(pe->pbus), pe->pbus->number);
Wei Yang781a8682015-03-25 16:23:57 +080067#ifdef CONFIG_PCI_IOV
68 else if (pe->flags & PNV_IODA_PE_VF)
69 sprintf(pfix, "%04x:%02x:%2x.%d",
70 pci_domain_nr(pe->parent_dev->bus),
71 (pe->rid & 0xff00) >> 8,
72 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
73#endif /* CONFIG_PCI_IOV*/
Joe Perches6d31c2f2014-09-21 10:55:06 -070074
75 printk("%spci %s: [PE# %.3d] %pV",
76 level, pfix, pe->pe_number, &vaf);
77
78 va_end(args);
79}
80
81#define pe_err(pe, fmt, ...) \
82 pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
83#define pe_warn(pe, fmt, ...) \
84 pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
85#define pe_info(pe, fmt, ...) \
86 pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000087
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020088static bool pnv_iommu_bypass_disabled __read_mostly;
89
90static int __init iommu_setup(char *str)
91{
92 if (!str)
93 return -EINVAL;
94
95 while (*str) {
96 if (!strncmp(str, "nobypass", 8)) {
97 pnv_iommu_bypass_disabled = true;
98 pr_info("PowerNV: IOMMU bypass window disabled.\n");
99 break;
100 }
101 str += strcspn(str, ",");
102 if (*str == ',')
103 str++;
104 }
105
106 return 0;
107}
108early_param("iommu", iommu_setup);
109
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000110/*
111 * stdcix is only supposed to be used in hypervisor real mode as per
112 * the architecture spec
113 */
114static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
115{
116 __asm__ __volatile__("stdcix %0,0,%1"
117 : : "r" (val), "r" (paddr) : "memory");
118}
119
Guo Chao262af552014-07-21 14:42:30 +1000120static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
121{
122 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
123 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
124}
125
Gavin Shan4b82ab12014-11-12 13:36:07 +1100126static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
127{
128 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
129 pr_warn("%s: Invalid PE %d on PHB#%x\n",
130 __func__, pe_no, phb->hose->global_number);
131 return;
132 }
133
134 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) {
135 pr_warn("%s: PE %d was assigned on PHB#%x\n",
136 __func__, pe_no, phb->hose->global_number);
137 return;
138 }
139
140 phb->ioda.pe_array[pe_no].phb = phb;
141 phb->ioda.pe_array[pe_no].pe_number = pe_no;
142}
143
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800144static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000145{
146 unsigned long pe;
147
148 do {
149 pe = find_next_zero_bit(phb->ioda.pe_alloc,
150 phb->ioda.total_pe, 0);
151 if (pe >= phb->ioda.total_pe)
152 return IODA_INVALID_PE;
153 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
154
Gavin Shan4cce9552013-04-25 19:21:00 +0000155 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000156 phb->ioda.pe_array[pe].pe_number = pe;
157 return pe;
158}
159
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800160static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000161{
162 WARN_ON(phb->ioda.pe_array[pe].pdev);
163
164 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
165 clear_bit(pe, phb->ioda.pe_alloc);
166}
167
Guo Chao262af552014-07-21 14:42:30 +1000168/* The default M64 BAR is shared by all PEs */
169static int pnv_ioda2_init_m64(struct pnv_phb *phb)
170{
171 const char *desc;
172 struct resource *r;
173 s64 rc;
174
175 /* Configure the default M64 BAR */
176 rc = opal_pci_set_phb_mem_window(phb->opal_id,
177 OPAL_M64_WINDOW_TYPE,
178 phb->ioda.m64_bar_idx,
179 phb->ioda.m64_base,
180 0, /* unused */
181 phb->ioda.m64_size);
182 if (rc != OPAL_SUCCESS) {
183 desc = "configuring";
184 goto fail;
185 }
186
187 /* Enable the default M64 BAR */
188 rc = opal_pci_phb_mmio_enable(phb->opal_id,
189 OPAL_M64_WINDOW_TYPE,
190 phb->ioda.m64_bar_idx,
191 OPAL_ENABLE_M64_SPLIT);
192 if (rc != OPAL_SUCCESS) {
193 desc = "enabling";
194 goto fail;
195 }
196
197 /* Mark the M64 BAR assigned */
198 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
199
200 /*
201 * Strip off the segment used by the reserved PE, which is
202 * expected to be 0 or last one of PE capabicity.
203 */
204 r = &phb->hose->mem_resources[1];
205 if (phb->ioda.reserved_pe == 0)
206 r->start += phb->ioda.m64_segsize;
207 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
208 r->end -= phb->ioda.m64_segsize;
209 else
210 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
211 phb->ioda.reserved_pe);
212
213 return 0;
214
215fail:
216 pr_warn(" Failure %lld %s M64 BAR#%d\n",
217 rc, desc, phb->ioda.m64_bar_idx);
218 opal_pci_phb_mmio_enable(phb->opal_id,
219 OPAL_M64_WINDOW_TYPE,
220 phb->ioda.m64_bar_idx,
221 OPAL_DISABLE_M64);
222 return -EIO;
223}
224
Gavin Shan5ef73562014-11-12 13:36:06 +1100225static void pnv_ioda2_reserve_m64_pe(struct pnv_phb *phb)
Guo Chao262af552014-07-21 14:42:30 +1000226{
227 resource_size_t sgsz = phb->ioda.m64_segsize;
228 struct pci_dev *pdev;
229 struct resource *r;
230 int base, step, i;
231
232 /*
233 * Root bus always has full M64 range and root port has
234 * M64 range used in reality. So we're checking root port
235 * instead of root bus.
236 */
237 list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100238 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
239 r = &pdev->resource[PCI_BRIDGE_RESOURCES + i];
Guo Chao262af552014-07-21 14:42:30 +1000240 if (!r->parent ||
241 !pnv_pci_is_mem_pref_64(r->flags))
242 continue;
243
244 base = (r->start - phb->ioda.m64_base) / sgsz;
245 for (step = 0; step < resource_size(r) / sgsz; step++)
Gavin Shan4b82ab12014-11-12 13:36:07 +1100246 pnv_ioda_reserve_pe(phb, base + step);
Guo Chao262af552014-07-21 14:42:30 +1000247 }
248 }
249}
250
251static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
252 struct pci_bus *bus, int all)
253{
254 resource_size_t segsz = phb->ioda.m64_segsize;
255 struct pci_dev *pdev;
256 struct resource *r;
257 struct pnv_ioda_pe *master_pe, *pe;
258 unsigned long size, *pe_alloc;
259 bool found;
260 int start, i, j;
261
262 /* Root bus shouldn't use M64 */
263 if (pci_is_root_bus(bus))
264 return IODA_INVALID_PE;
265
266 /* We support only one M64 window on each bus */
267 found = false;
268 pci_bus_for_each_resource(bus, r, i) {
269 if (r && r->parent &&
270 pnv_pci_is_mem_pref_64(r->flags)) {
271 found = true;
272 break;
273 }
274 }
275
276 /* No M64 window found ? */
277 if (!found)
278 return IODA_INVALID_PE;
279
280 /* Allocate bitmap */
281 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
282 pe_alloc = kzalloc(size, GFP_KERNEL);
283 if (!pe_alloc) {
284 pr_warn("%s: Out of memory !\n",
285 __func__);
286 return IODA_INVALID_PE;
287 }
288
289 /*
290 * Figure out reserved PE numbers by the PE
291 * the its child PEs.
292 */
293 start = (r->start - phb->ioda.m64_base) / segsz;
294 for (i = 0; i < resource_size(r) / segsz; i++)
295 set_bit(start + i, pe_alloc);
296
297 if (all)
298 goto done;
299
300 /*
301 * If the PE doesn't cover all subordinate buses,
302 * we need subtract from reserved PEs for children.
303 */
304 list_for_each_entry(pdev, &bus->devices, bus_list) {
305 if (!pdev->subordinate)
306 continue;
307
308 pci_bus_for_each_resource(pdev->subordinate, r, i) {
309 if (!r || !r->parent ||
310 !pnv_pci_is_mem_pref_64(r->flags))
311 continue;
312
313 start = (r->start - phb->ioda.m64_base) / segsz;
314 for (j = 0; j < resource_size(r) / segsz ; j++)
315 clear_bit(start + j, pe_alloc);
316 }
317 }
318
319 /*
320 * the current bus might not own M64 window and that's all
321 * contributed by its child buses. For the case, we needn't
322 * pick M64 dependent PE#.
323 */
324 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
325 kfree(pe_alloc);
326 return IODA_INVALID_PE;
327 }
328
329 /*
330 * Figure out the master PE and put all slave PEs to master
331 * PE's list to form compound PE.
332 */
333done:
334 master_pe = NULL;
335 i = -1;
336 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
337 phb->ioda.total_pe) {
338 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000339
340 if (!master_pe) {
341 pe->flags |= PNV_IODA_PE_MASTER;
342 INIT_LIST_HEAD(&pe->slaves);
343 master_pe = pe;
344 } else {
345 pe->flags |= PNV_IODA_PE_SLAVE;
346 pe->master = master_pe;
347 list_add_tail(&pe->list, &master_pe->slaves);
348 }
349 }
350
351 kfree(pe_alloc);
352 return master_pe->pe_number;
353}
354
355static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
356{
357 struct pci_controller *hose = phb->hose;
358 struct device_node *dn = hose->dn;
359 struct resource *res;
360 const u32 *r;
361 u64 pci_addr;
362
Gavin Shan1665c4a2014-11-12 13:36:04 +1100363 /* FIXME: Support M64 for P7IOC */
364 if (phb->type != PNV_PHB_IODA2) {
365 pr_info(" Not support M64 window\n");
366 return;
367 }
368
Guo Chao262af552014-07-21 14:42:30 +1000369 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
370 pr_info(" Firmware too old to support M64 window\n");
371 return;
372 }
373
374 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
375 if (!r) {
376 pr_info(" No <ibm,opal-m64-window> on %s\n",
377 dn->full_name);
378 return;
379 }
380
Guo Chao262af552014-07-21 14:42:30 +1000381 res = &hose->mem_resources[1];
382 res->start = of_translate_address(dn, r + 2);
383 res->end = res->start + of_read_number(r + 4, 2) - 1;
384 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
385 pci_addr = of_read_number(r, 2);
386 hose->mem_offset[1] = res->start - pci_addr;
387
388 phb->ioda.m64_size = resource_size(res);
389 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
390 phb->ioda.m64_base = pci_addr;
391
Wei Yange9863e62014-12-12 12:39:37 +0800392 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
393 res->start, res->end, pci_addr);
394
Guo Chao262af552014-07-21 14:42:30 +1000395 /* Use last M64 BAR to cover M64 window */
396 phb->ioda.m64_bar_idx = 15;
397 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shan5ef73562014-11-12 13:36:06 +1100398 phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000399 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
400}
401
Gavin Shan49dec922014-07-21 14:42:33 +1000402static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
403{
404 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
405 struct pnv_ioda_pe *slave;
406 s64 rc;
407
408 /* Fetch master PE */
409 if (pe->flags & PNV_IODA_PE_SLAVE) {
410 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100411 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
412 return;
413
Gavin Shan49dec922014-07-21 14:42:33 +1000414 pe_no = pe->pe_number;
415 }
416
417 /* Freeze master PE */
418 rc = opal_pci_eeh_freeze_set(phb->opal_id,
419 pe_no,
420 OPAL_EEH_ACTION_SET_FREEZE_ALL);
421 if (rc != OPAL_SUCCESS) {
422 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
423 __func__, rc, phb->hose->global_number, pe_no);
424 return;
425 }
426
427 /* Freeze slave PEs */
428 if (!(pe->flags & PNV_IODA_PE_MASTER))
429 return;
430
431 list_for_each_entry(slave, &pe->slaves, list) {
432 rc = opal_pci_eeh_freeze_set(phb->opal_id,
433 slave->pe_number,
434 OPAL_EEH_ACTION_SET_FREEZE_ALL);
435 if (rc != OPAL_SUCCESS)
436 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
437 __func__, rc, phb->hose->global_number,
438 slave->pe_number);
439 }
440}
441
Anton Blancharde51df2c2014-08-20 08:55:18 +1000442static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000443{
444 struct pnv_ioda_pe *pe, *slave;
445 s64 rc;
446
447 /* Find master PE */
448 pe = &phb->ioda.pe_array[pe_no];
449 if (pe->flags & PNV_IODA_PE_SLAVE) {
450 pe = pe->master;
451 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
452 pe_no = pe->pe_number;
453 }
454
455 /* Clear frozen state for master PE */
456 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
457 if (rc != OPAL_SUCCESS) {
458 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
459 __func__, rc, opt, phb->hose->global_number, pe_no);
460 return -EIO;
461 }
462
463 if (!(pe->flags & PNV_IODA_PE_MASTER))
464 return 0;
465
466 /* Clear frozen state for slave PEs */
467 list_for_each_entry(slave, &pe->slaves, list) {
468 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
469 slave->pe_number,
470 opt);
471 if (rc != OPAL_SUCCESS) {
472 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
473 __func__, rc, opt, phb->hose->global_number,
474 slave->pe_number);
475 return -EIO;
476 }
477 }
478
479 return 0;
480}
481
482static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
483{
484 struct pnv_ioda_pe *slave, *pe;
485 u8 fstate, state;
486 __be16 pcierr;
487 s64 rc;
488
489 /* Sanity check on PE number */
490 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
491 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
492
493 /*
494 * Fetch the master PE and the PE instance might be
495 * not initialized yet.
496 */
497 pe = &phb->ioda.pe_array[pe_no];
498 if (pe->flags & PNV_IODA_PE_SLAVE) {
499 pe = pe->master;
500 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
501 pe_no = pe->pe_number;
502 }
503
504 /* Check the master PE */
505 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
506 &state, &pcierr, NULL);
507 if (rc != OPAL_SUCCESS) {
508 pr_warn("%s: Failure %lld getting "
509 "PHB#%x-PE#%x state\n",
510 __func__, rc,
511 phb->hose->global_number, pe_no);
512 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
513 }
514
515 /* Check the slave PE */
516 if (!(pe->flags & PNV_IODA_PE_MASTER))
517 return state;
518
519 list_for_each_entry(slave, &pe->slaves, list) {
520 rc = opal_pci_eeh_freeze_status(phb->opal_id,
521 slave->pe_number,
522 &fstate,
523 &pcierr,
524 NULL);
525 if (rc != OPAL_SUCCESS) {
526 pr_warn("%s: Failure %lld getting "
527 "PHB#%x-PE#%x state\n",
528 __func__, rc,
529 phb->hose->global_number, slave->pe_number);
530 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
531 }
532
533 /*
534 * Override the result based on the ascending
535 * priority.
536 */
537 if (fstate > state)
538 state = fstate;
539 }
540
541 return state;
542}
543
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000544/* Currently those 2 are only used when MSIs are enabled, this will change
545 * but in the meantime, we need to protect them to avoid warnings
546 */
547#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800548static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000549{
550 struct pci_controller *hose = pci_bus_to_host(dev->bus);
551 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000552 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000553
554 if (!pdn)
555 return NULL;
556 if (pdn->pe_number == IODA_INVALID_PE)
557 return NULL;
558 return &phb->ioda.pe_array[pdn->pe_number];
559}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000560#endif /* CONFIG_PCI_MSI */
561
Gavin Shanb131a842014-11-12 13:36:08 +1100562static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
563 struct pnv_ioda_pe *parent,
564 struct pnv_ioda_pe *child,
565 bool is_add)
566{
567 const char *desc = is_add ? "adding" : "removing";
568 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
569 OPAL_REMOVE_PE_FROM_DOMAIN;
570 struct pnv_ioda_pe *slave;
571 long rc;
572
573 /* Parent PE affects child PE */
574 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
575 child->pe_number, op);
576 if (rc != OPAL_SUCCESS) {
577 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
578 rc, desc);
579 return -ENXIO;
580 }
581
582 if (!(child->flags & PNV_IODA_PE_MASTER))
583 return 0;
584
585 /* Compound case: parent PE affects slave PEs */
586 list_for_each_entry(slave, &child->slaves, list) {
587 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
588 slave->pe_number, op);
589 if (rc != OPAL_SUCCESS) {
590 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
591 rc, desc);
592 return -ENXIO;
593 }
594 }
595
596 return 0;
597}
598
599static int pnv_ioda_set_peltv(struct pnv_phb *phb,
600 struct pnv_ioda_pe *pe,
601 bool is_add)
602{
603 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800604 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100605 int ret;
606
607 /*
608 * Clear PE frozen state. If it's master PE, we need
609 * clear slave PE frozen state as well.
610 */
611 if (is_add) {
612 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
613 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
614 if (pe->flags & PNV_IODA_PE_MASTER) {
615 list_for_each_entry(slave, &pe->slaves, list)
616 opal_pci_eeh_freeze_clear(phb->opal_id,
617 slave->pe_number,
618 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
619 }
620 }
621
622 /*
623 * Associate PE in PELT. We need add the PE into the
624 * corresponding PELT-V as well. Otherwise, the error
625 * originated from the PE might contribute to other
626 * PEs.
627 */
628 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
629 if (ret)
630 return ret;
631
632 /* For compound PEs, any one affects all of them */
633 if (pe->flags & PNV_IODA_PE_MASTER) {
634 list_for_each_entry(slave, &pe->slaves, list) {
635 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
636 if (ret)
637 return ret;
638 }
639 }
640
641 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
642 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800643 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100644 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800645#ifdef CONFIG_PCI_IOV
646 else if (pe->flags & PNV_IODA_PE_VF)
647 pdev = pe->parent_dev->bus->self;
648#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100649 while (pdev) {
650 struct pci_dn *pdn = pci_get_pdn(pdev);
651 struct pnv_ioda_pe *parent;
652
653 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
654 parent = &phb->ioda.pe_array[pdn->pe_number];
655 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
656 if (ret)
657 return ret;
658 }
659
660 pdev = pdev->bus->self;
661 }
662
663 return 0;
664}
665
Wei Yang781a8682015-03-25 16:23:57 +0800666#ifdef CONFIG_PCI_IOV
667static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
668{
669 struct pci_dev *parent;
670 uint8_t bcomp, dcomp, fcomp;
671 int64_t rc;
672 long rid_end, rid;
673
674 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
675 if (pe->pbus) {
676 int count;
677
678 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
679 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
680 parent = pe->pbus->self;
681 if (pe->flags & PNV_IODA_PE_BUS_ALL)
682 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
683 else
684 count = 1;
685
686 switch(count) {
687 case 1: bcomp = OpalPciBusAll; break;
688 case 2: bcomp = OpalPciBus7Bits; break;
689 case 4: bcomp = OpalPciBus6Bits; break;
690 case 8: bcomp = OpalPciBus5Bits; break;
691 case 16: bcomp = OpalPciBus4Bits; break;
692 case 32: bcomp = OpalPciBus3Bits; break;
693 default:
694 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
695 count);
696 /* Do an exact match only */
697 bcomp = OpalPciBusAll;
698 }
699 rid_end = pe->rid + (count << 8);
700 } else {
701 if (pe->flags & PNV_IODA_PE_VF)
702 parent = pe->parent_dev;
703 else
704 parent = pe->pdev->bus->self;
705 bcomp = OpalPciBusAll;
706 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
707 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
708 rid_end = pe->rid + 1;
709 }
710
711 /* Clear the reverse map */
712 for (rid = pe->rid; rid < rid_end; rid++)
713 phb->ioda.pe_rmap[rid] = 0;
714
715 /* Release from all parents PELT-V */
716 while (parent) {
717 struct pci_dn *pdn = pci_get_pdn(parent);
718 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
719 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
720 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
721 /* XXX What to do in case of error ? */
722 }
723 parent = parent->bus->self;
724 }
725
726 opal_pci_eeh_freeze_set(phb->opal_id, pe->pe_number,
727 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
728
729 /* Disassociate PE in PELT */
730 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
731 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
732 if (rc)
733 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
734 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
735 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
736 if (rc)
737 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
738
739 pe->pbus = NULL;
740 pe->pdev = NULL;
741 pe->parent_dev = NULL;
742
743 return 0;
744}
745#endif /* CONFIG_PCI_IOV */
746
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800747static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000748{
749 struct pci_dev *parent;
750 uint8_t bcomp, dcomp, fcomp;
751 long rc, rid_end, rid;
752
753 /* Bus validation ? */
754 if (pe->pbus) {
755 int count;
756
757 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
758 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
759 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000760 if (pe->flags & PNV_IODA_PE_BUS_ALL)
761 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
762 else
763 count = 1;
764
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000765 switch(count) {
766 case 1: bcomp = OpalPciBusAll; break;
767 case 2: bcomp = OpalPciBus7Bits; break;
768 case 4: bcomp = OpalPciBus6Bits; break;
769 case 8: bcomp = OpalPciBus5Bits; break;
770 case 16: bcomp = OpalPciBus4Bits; break;
771 case 32: bcomp = OpalPciBus3Bits; break;
772 default:
Wei Yang781a8682015-03-25 16:23:57 +0800773 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
774 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000775 /* Do an exact match only */
776 bcomp = OpalPciBusAll;
777 }
778 rid_end = pe->rid + (count << 8);
779 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800780#ifdef CONFIG_PCI_IOV
781 if (pe->flags & PNV_IODA_PE_VF)
782 parent = pe->parent_dev;
783 else
784#endif /* CONFIG_PCI_IOV */
785 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000786 bcomp = OpalPciBusAll;
787 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
788 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
789 rid_end = pe->rid + 1;
790 }
791
Gavin Shan631ad692013-11-04 16:32:46 +0800792 /*
793 * Associate PE in PELT. We need add the PE into the
794 * corresponding PELT-V as well. Otherwise, the error
795 * originated from the PE might contribute to other
796 * PEs.
797 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000798 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
799 bcomp, dcomp, fcomp, OPAL_MAP_PE);
800 if (rc) {
801 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
802 return -ENXIO;
803 }
Gavin Shan631ad692013-11-04 16:32:46 +0800804
Gavin Shanb131a842014-11-12 13:36:08 +1100805 /* Configure PELTV */
806 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000807
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000808 /* Setup reverse map */
809 for (rid = pe->rid; rid < rid_end; rid++)
810 phb->ioda.pe_rmap[rid] = pe->pe_number;
811
812 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100813 if (phb->type != PNV_PHB_IODA1) {
814 pe->mve_number = 0;
815 goto out;
816 }
817
818 pe->mve_number = pe->pe_number;
819 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
820 if (rc != OPAL_SUCCESS) {
821 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
822 rc, pe->mve_number);
823 pe->mve_number = -1;
824 } else {
825 rc = opal_pci_set_mve_enable(phb->opal_id,
826 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000827 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100828 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000829 rc, pe->mve_number);
830 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000831 }
Gavin Shan4773f762014-11-12 13:36:09 +1100832 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000833
Gavin Shan4773f762014-11-12 13:36:09 +1100834out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000835 return 0;
836}
837
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800838static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
839 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000840{
841 struct pnv_ioda_pe *lpe;
842
Gavin Shan7ebdf952012-08-20 03:49:15 +0000843 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000844 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000845 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000846 return;
847 }
848 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000849 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000850}
851
852static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
853{
854 /* This is quite simplistic. The "base" weight of a device
855 * is 10. 0 means no DMA is to be accounted for it.
856 */
857
858 /* If it's a bridge, no DMA */
859 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
860 return 0;
861
862 /* Reduce the weight of slow USB controllers */
863 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
864 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
865 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
866 return 3;
867
868 /* Increase the weight of RAID (includes Obsidian) */
869 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
870 return 15;
871
872 /* Default */
873 return 10;
874}
875
Wei Yang781a8682015-03-25 16:23:57 +0800876#ifdef CONFIG_PCI_IOV
877static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
878{
879 struct pci_dn *pdn = pci_get_pdn(dev);
880 int i;
881 struct resource *res, res2;
882 resource_size_t size;
883 u16 num_vfs;
884
885 if (!dev->is_physfn)
886 return -EINVAL;
887
888 /*
889 * "offset" is in VFs. The M64 windows are sized so that when they
890 * are segmented, each segment is the same size as the IOV BAR.
891 * Each segment is in a separate PE, and the high order bits of the
892 * address are the PE number. Therefore, each VF's BAR is in a
893 * separate PE, and changing the IOV BAR start address changes the
894 * range of PEs the VFs are in.
895 */
896 num_vfs = pdn->num_vfs;
897 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
898 res = &dev->resource[i + PCI_IOV_RESOURCES];
899 if (!res->flags || !res->parent)
900 continue;
901
902 if (!pnv_pci_is_mem_pref_64(res->flags))
903 continue;
904
905 /*
906 * The actual IOV BAR range is determined by the start address
907 * and the actual size for num_vfs VFs BAR. This check is to
908 * make sure that after shifting, the range will not overlap
909 * with another device.
910 */
911 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
912 res2.flags = res->flags;
913 res2.start = res->start + (size * offset);
914 res2.end = res2.start + (size * num_vfs) - 1;
915
916 if (res2.end > res->end) {
917 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
918 i, &res2, res, num_vfs, offset);
919 return -EBUSY;
920 }
921 }
922
923 /*
924 * After doing so, there would be a "hole" in the /proc/iomem when
925 * offset is a positive value. It looks like the device return some
926 * mmio back to the system, which actually no one could use it.
927 */
928 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
929 res = &dev->resource[i + PCI_IOV_RESOURCES];
930 if (!res->flags || !res->parent)
931 continue;
932
933 if (!pnv_pci_is_mem_pref_64(res->flags))
934 continue;
935
936 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
937 res2 = *res;
938 res->start += size * offset;
939
940 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (enabling %d VFs shifted by %d)\n",
941 i, &res2, res, num_vfs, offset);
942 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
943 }
944 return 0;
945}
946#endif /* CONFIG_PCI_IOV */
947
Gavin Shanfb446ad2012-08-20 03:49:14 +0000948#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800949static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000950{
951 struct pci_controller *hose = pci_bus_to_host(dev->bus);
952 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000953 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000954 struct pnv_ioda_pe *pe;
955 int pe_num;
956
957 if (!pdn) {
958 pr_err("%s: Device tree node not associated properly\n",
959 pci_name(dev));
960 return NULL;
961 }
962 if (pdn->pe_number != IODA_INVALID_PE)
963 return NULL;
964
965 /* PE#0 has been pre-set */
966 if (dev->bus->number == 0)
967 pe_num = 0;
968 else
969 pe_num = pnv_ioda_alloc_pe(phb);
970 if (pe_num == IODA_INVALID_PE) {
971 pr_warning("%s: Not enough PE# available, disabling device\n",
972 pci_name(dev));
973 return NULL;
974 }
975
976 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
977 * pointer in the PE data structure, both should be destroyed at the
978 * same time. However, this needs to be looked at more closely again
979 * once we actually start removing things (Hotplug, SR-IOV, ...)
980 *
981 * At some point we want to remove the PDN completely anyways
982 */
983 pe = &phb->ioda.pe_array[pe_num];
984 pci_dev_get(dev);
985 pdn->pcidev = dev;
986 pdn->pe_number = pe_num;
987 pe->pdev = dev;
988 pe->pbus = NULL;
989 pe->tce32_seg = -1;
990 pe->mve_number = -1;
991 pe->rid = dev->bus->number << 8 | pdn->devfn;
992
993 pe_info(pe, "Associated device to PE\n");
994
995 if (pnv_ioda_configure_pe(phb, pe)) {
996 /* XXX What do we do here ? */
997 if (pe_num)
998 pnv_ioda_free_pe(phb, pe_num);
999 pdn->pe_number = IODA_INVALID_PE;
1000 pe->pdev = NULL;
1001 pci_dev_put(dev);
1002 return NULL;
1003 }
1004
1005 /* Assign a DMA weight to the device */
1006 pe->dma_weight = pnv_ioda_dma_weight(dev);
1007 if (pe->dma_weight != 0) {
1008 phb->ioda.dma_weight += pe->dma_weight;
1009 phb->ioda.dma_pe_count++;
1010 }
1011
1012 /* Link the PE */
1013 pnv_ioda_link_pe_by_weight(phb, pe);
1014
1015 return pe;
1016}
Gavin Shanfb446ad2012-08-20 03:49:14 +00001017#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001018
1019static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1020{
1021 struct pci_dev *dev;
1022
1023 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001024 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001025
1026 if (pdn == NULL) {
1027 pr_warn("%s: No device node associated with device !\n",
1028 pci_name(dev));
1029 continue;
1030 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001031 pdn->pcidev = dev;
1032 pdn->pe_number = pe->pe_number;
1033 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001034 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001035 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1036 }
1037}
1038
Gavin Shanfb446ad2012-08-20 03:49:14 +00001039/*
1040 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1041 * single PCI bus. Another one that contains the primary PCI bus and its
1042 * subordinate PCI devices and buses. The second type of PE is normally
1043 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1044 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001045static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001046{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001047 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001048 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001049 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +10001050 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001051
Guo Chao262af552014-07-21 14:42:30 +10001052 /* Check if PE is determined by M64 */
1053 if (phb->pick_m64_pe)
1054 pe_num = phb->pick_m64_pe(phb, bus, all);
1055
1056 /* The PE number isn't pinned by M64 */
1057 if (pe_num == IODA_INVALID_PE)
1058 pe_num = pnv_ioda_alloc_pe(phb);
1059
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001060 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001061 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1062 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001063 return;
1064 }
1065
1066 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +10001067 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001068 pe->pbus = bus;
1069 pe->pdev = NULL;
1070 pe->tce32_seg = -1;
1071 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001072 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001073 pe->dma_weight = 0;
1074
Gavin Shanfb446ad2012-08-20 03:49:14 +00001075 if (all)
1076 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
1077 bus->busn_res.start, bus->busn_res.end, pe_num);
1078 else
1079 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
1080 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001081
1082 if (pnv_ioda_configure_pe(phb, pe)) {
1083 /* XXX What do we do here ? */
1084 if (pe_num)
1085 pnv_ioda_free_pe(phb, pe_num);
1086 pe->pbus = NULL;
1087 return;
1088 }
1089
Wei Yang9e8d4a12015-03-25 16:23:54 +08001090 pe->tce32_table = kzalloc_node(sizeof(struct iommu_table),
1091 GFP_KERNEL, hose->node);
1092 pe->tce32_table->data = pe;
1093
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001094 /* Associate it with all child devices */
1095 pnv_ioda_setup_same_PE(bus, pe);
1096
Gavin Shan7ebdf952012-08-20 03:49:15 +00001097 /* Put PE to the list */
1098 list_add_tail(&pe->list, &phb->ioda.pe_list);
1099
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001100 /* Account for one DMA PE if at least one DMA capable device exist
1101 * below the bridge
1102 */
1103 if (pe->dma_weight != 0) {
1104 phb->ioda.dma_weight += pe->dma_weight;
1105 phb->ioda.dma_pe_count++;
1106 }
1107
1108 /* Link the PE */
1109 pnv_ioda_link_pe_by_weight(phb, pe);
1110}
1111
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001112static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001113{
1114 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001115
1116 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001117
1118 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001119 if (dev->subordinate) {
1120 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
1121 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
1122 else
1123 pnv_ioda_setup_PEs(dev->subordinate);
1124 }
1125 }
1126}
1127
1128/*
1129 * Configure PEs so that the downstream PCI buses and devices
1130 * could have their associated PE#. Unfortunately, we didn't
1131 * figure out the way to identify the PLX bridge yet. So we
1132 * simply put the PCI bus and the subordinate behind the root
1133 * port to PE# here. The game rule here is expected to be changed
1134 * as soon as we can detected PLX bridge correctly.
1135 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001136static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001137{
1138 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001139 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001140
1141 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001142 phb = hose->private_data;
1143
1144 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +11001145 if (phb->reserve_m64_pe)
1146 phb->reserve_m64_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +10001147
Gavin Shanfb446ad2012-08-20 03:49:14 +00001148 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001149 }
1150}
1151
Gavin Shana8b2f822015-03-25 16:23:52 +08001152#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +08001153static int pnv_pci_vf_release_m64(struct pci_dev *pdev)
1154{
1155 struct pci_bus *bus;
1156 struct pci_controller *hose;
1157 struct pnv_phb *phb;
1158 struct pci_dn *pdn;
1159 int i;
1160
1161 bus = pdev->bus;
1162 hose = pci_bus_to_host(bus);
1163 phb = hose->private_data;
1164 pdn = pci_get_pdn(pdev);
1165
1166 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1167 if (pdn->m64_wins[i] == IODA_INVALID_M64)
1168 continue;
1169 opal_pci_phb_mmio_enable(phb->opal_id,
1170 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i], 0);
1171 clear_bit(pdn->m64_wins[i], &phb->ioda.m64_bar_alloc);
1172 pdn->m64_wins[i] = IODA_INVALID_M64;
1173 }
1174
1175 return 0;
1176}
1177
1178static int pnv_pci_vf_assign_m64(struct pci_dev *pdev)
1179{
1180 struct pci_bus *bus;
1181 struct pci_controller *hose;
1182 struct pnv_phb *phb;
1183 struct pci_dn *pdn;
1184 unsigned int win;
1185 struct resource *res;
1186 int i;
1187 int64_t rc;
1188
1189 bus = pdev->bus;
1190 hose = pci_bus_to_host(bus);
1191 phb = hose->private_data;
1192 pdn = pci_get_pdn(pdev);
1193
1194 /* Initialize the m64_wins to IODA_INVALID_M64 */
1195 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
1196 pdn->m64_wins[i] = IODA_INVALID_M64;
1197
1198 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1199 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1200 if (!res->flags || !res->parent)
1201 continue;
1202
1203 if (!pnv_pci_is_mem_pref_64(res->flags))
1204 continue;
1205
1206 do {
1207 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1208 phb->ioda.m64_bar_idx + 1, 0);
1209
1210 if (win >= phb->ioda.m64_bar_idx + 1)
1211 goto m64_failed;
1212 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
1213
1214 pdn->m64_wins[i] = win;
1215
1216 /* Map the M64 here */
1217 rc = opal_pci_set_phb_mem_window(phb->opal_id,
1218 OPAL_M64_WINDOW_TYPE,
1219 pdn->m64_wins[i],
1220 res->start,
1221 0, /* unused */
1222 resource_size(res));
1223 if (rc != OPAL_SUCCESS) {
1224 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1225 win, rc);
1226 goto m64_failed;
1227 }
1228
1229 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1230 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i], 1);
1231 if (rc != OPAL_SUCCESS) {
1232 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1233 win, rc);
1234 goto m64_failed;
1235 }
1236 }
1237 return 0;
1238
1239m64_failed:
1240 pnv_pci_vf_release_m64(pdev);
1241 return -EBUSY;
1242}
1243
1244static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1245{
1246 struct pci_bus *bus;
1247 struct pci_controller *hose;
1248 struct pnv_phb *phb;
1249 struct iommu_table *tbl;
1250 unsigned long addr;
1251 int64_t rc;
1252
1253 bus = dev->bus;
1254 hose = pci_bus_to_host(bus);
1255 phb = hose->private_data;
1256 tbl = pe->tce32_table;
1257 addr = tbl->it_base;
1258
1259 opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1260 pe->pe_number << 1, 1, __pa(addr),
1261 0, 0x1000);
1262
1263 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1264 pe->pe_number,
1265 (pe->pe_number << 1) + 1,
1266 pe->tce_bypass_base,
1267 0);
1268 if (rc)
1269 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1270
1271 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
1272 free_pages(addr, get_order(TCE32_TABLE_SIZE));
1273 pe->tce32_table = NULL;
1274}
1275
1276static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
1277{
1278 struct pci_bus *bus;
1279 struct pci_controller *hose;
1280 struct pnv_phb *phb;
1281 struct pnv_ioda_pe *pe, *pe_n;
1282 struct pci_dn *pdn;
1283
1284 bus = pdev->bus;
1285 hose = pci_bus_to_host(bus);
1286 phb = hose->private_data;
1287
1288 if (!pdev->is_physfn)
1289 return;
1290
1291 pdn = pci_get_pdn(pdev);
1292 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1293 if (pe->parent_dev != pdev)
1294 continue;
1295
1296 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1297
1298 /* Remove from list */
1299 mutex_lock(&phb->ioda.pe_list_mutex);
1300 list_del(&pe->list);
1301 mutex_unlock(&phb->ioda.pe_list_mutex);
1302
1303 pnv_ioda_deconfigure_pe(phb, pe);
1304
1305 pnv_ioda_free_pe(phb, pe->pe_number);
1306 }
1307}
1308
1309void pnv_pci_sriov_disable(struct pci_dev *pdev)
1310{
1311 struct pci_bus *bus;
1312 struct pci_controller *hose;
1313 struct pnv_phb *phb;
1314 struct pci_dn *pdn;
1315 struct pci_sriov *iov;
1316 u16 num_vfs;
1317
1318 bus = pdev->bus;
1319 hose = pci_bus_to_host(bus);
1320 phb = hose->private_data;
1321 pdn = pci_get_pdn(pdev);
1322 iov = pdev->sriov;
1323 num_vfs = pdn->num_vfs;
1324
1325 /* Release VF PEs */
1326 pnv_ioda_release_vf_PE(pdev);
1327
1328 if (phb->type == PNV_PHB_IODA2) {
1329 pnv_pci_vf_resource_shift(pdev, -pdn->offset);
1330
1331 /* Release M64 windows */
1332 pnv_pci_vf_release_m64(pdev);
1333
1334 /* Release PE numbers */
1335 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1336 pdn->offset = 0;
1337 }
1338}
1339
1340static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1341 struct pnv_ioda_pe *pe);
1342static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1343{
1344 struct pci_bus *bus;
1345 struct pci_controller *hose;
1346 struct pnv_phb *phb;
1347 struct pnv_ioda_pe *pe;
1348 int pe_num;
1349 u16 vf_index;
1350 struct pci_dn *pdn;
1351
1352 bus = pdev->bus;
1353 hose = pci_bus_to_host(bus);
1354 phb = hose->private_data;
1355 pdn = pci_get_pdn(pdev);
1356
1357 if (!pdev->is_physfn)
1358 return;
1359
1360 /* Reserve PE for each VF */
1361 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
1362 pe_num = pdn->offset + vf_index;
1363
1364 pe = &phb->ioda.pe_array[pe_num];
1365 pe->pe_number = pe_num;
1366 pe->phb = phb;
1367 pe->flags = PNV_IODA_PE_VF;
1368 pe->pbus = NULL;
1369 pe->parent_dev = pdev;
1370 pe->tce32_seg = -1;
1371 pe->mve_number = -1;
1372 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1373 pci_iov_virtfn_devfn(pdev, vf_index);
1374
1375 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1376 hose->global_number, pdev->bus->number,
1377 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1378 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1379
1380 if (pnv_ioda_configure_pe(phb, pe)) {
1381 /* XXX What do we do here ? */
1382 if (pe_num)
1383 pnv_ioda_free_pe(phb, pe_num);
1384 pe->pdev = NULL;
1385 continue;
1386 }
1387
1388 pe->tce32_table = kzalloc_node(sizeof(struct iommu_table),
1389 GFP_KERNEL, hose->node);
1390 pe->tce32_table->data = pe;
1391
1392 /* Put PE to the list */
1393 mutex_lock(&phb->ioda.pe_list_mutex);
1394 list_add_tail(&pe->list, &phb->ioda.pe_list);
1395 mutex_unlock(&phb->ioda.pe_list_mutex);
1396
1397 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1398 }
1399}
1400
1401int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1402{
1403 struct pci_bus *bus;
1404 struct pci_controller *hose;
1405 struct pnv_phb *phb;
1406 struct pci_dn *pdn;
1407 int ret;
1408
1409 bus = pdev->bus;
1410 hose = pci_bus_to_host(bus);
1411 phb = hose->private_data;
1412 pdn = pci_get_pdn(pdev);
1413
1414 if (phb->type == PNV_PHB_IODA2) {
1415 /* Calculate available PE for required VFs */
1416 mutex_lock(&phb->ioda.pe_alloc_mutex);
1417 pdn->offset = bitmap_find_next_zero_area(
1418 phb->ioda.pe_alloc, phb->ioda.total_pe,
1419 0, num_vfs, 0);
1420 if (pdn->offset >= phb->ioda.total_pe) {
1421 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1422 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1423 pdn->offset = 0;
1424 return -EBUSY;
1425 }
1426 bitmap_set(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1427 pdn->num_vfs = num_vfs;
1428 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1429
1430 /* Assign M64 window accordingly */
1431 ret = pnv_pci_vf_assign_m64(pdev);
1432 if (ret) {
1433 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1434 goto m64_failed;
1435 }
1436
1437 /*
1438 * When using one M64 BAR to map one IOV BAR, we need to shift
1439 * the IOV BAR according to the PE# allocated to the VFs.
1440 * Otherwise, the PE# for the VF will conflict with others.
1441 */
1442 ret = pnv_pci_vf_resource_shift(pdev, pdn->offset);
1443 if (ret)
1444 goto m64_failed;
1445 }
1446
1447 /* Setup VF PEs */
1448 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1449
1450 return 0;
1451
1452m64_failed:
1453 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1454 pdn->offset = 0;
1455
1456 return ret;
1457}
1458
Gavin Shana8b2f822015-03-25 16:23:52 +08001459int pcibios_sriov_disable(struct pci_dev *pdev)
1460{
Wei Yang781a8682015-03-25 16:23:57 +08001461 pnv_pci_sriov_disable(pdev);
1462
Gavin Shana8b2f822015-03-25 16:23:52 +08001463 /* Release PCI data */
1464 remove_dev_pci_data(pdev);
1465 return 0;
1466}
1467
1468int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1469{
1470 /* Allocate PCI data */
1471 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001472
1473 pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001474 return 0;
1475}
1476#endif /* CONFIG_PCI_IOV */
1477
Gavin Shan959c9bd2013-04-25 19:21:02 +00001478static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001479{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001480 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001481 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001482
Gavin Shan959c9bd2013-04-25 19:21:02 +00001483 /*
1484 * The function can be called while the PE#
1485 * hasn't been assigned. Do nothing for the
1486 * case.
1487 */
1488 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1489 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001490
Gavin Shan959c9bd2013-04-25 19:21:02 +00001491 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001492 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Wei Yang9e8d4a12015-03-25 16:23:54 +08001493 set_iommu_table_base_and_group(&pdev->dev, pe->tce32_table);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001494}
1495
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001496static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
1497 struct pci_dev *pdev, u64 dma_mask)
1498{
1499 struct pci_dn *pdn = pci_get_pdn(pdev);
1500 struct pnv_ioda_pe *pe;
1501 uint64_t top;
1502 bool bypass = false;
1503
1504 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1505 return -ENODEV;;
1506
1507 pe = &phb->ioda.pe_array[pdn->pe_number];
1508 if (pe->tce_bypass_enabled) {
1509 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1510 bypass = (dma_mask >= top);
1511 }
1512
1513 if (bypass) {
1514 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1515 set_dma_ops(&pdev->dev, &dma_direct_ops);
1516 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
1517 } else {
1518 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1519 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Wei Yang9e8d4a12015-03-25 16:23:54 +08001520 set_iommu_table_base(&pdev->dev, pe->tce32_table);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001521 }
Brian W Harta32305b2014-07-31 14:24:37 -05001522 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001523 return 0;
1524}
1525
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001526static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
1527 struct pci_dev *pdev)
1528{
1529 struct pci_dn *pdn = pci_get_pdn(pdev);
1530 struct pnv_ioda_pe *pe;
1531 u64 end, mask;
1532
1533 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1534 return 0;
1535
1536 pe = &phb->ioda.pe_array[pdn->pe_number];
1537 if (!pe->tce_bypass_enabled)
1538 return __dma_get_required_mask(&pdev->dev);
1539
1540
1541 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1542 mask = 1ULL << (fls64(end) - 1);
1543 mask += mask - 1;
1544
1545 return mask;
1546}
1547
Gavin Shandff4a392014-07-15 17:00:55 +10001548static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
1549 struct pci_bus *bus,
1550 bool add_to_iommu_group)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001551{
1552 struct pci_dev *dev;
1553
1554 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shandff4a392014-07-15 17:00:55 +10001555 if (add_to_iommu_group)
1556 set_iommu_table_base_and_group(&dev->dev,
Wei Yang9e8d4a12015-03-25 16:23:54 +08001557 pe->tce32_table);
Gavin Shandff4a392014-07-15 17:00:55 +10001558 else
Wei Yang9e8d4a12015-03-25 16:23:54 +08001559 set_iommu_table_base(&dev->dev, pe->tce32_table);
Gavin Shandff4a392014-07-15 17:00:55 +10001560
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001561 if (dev->subordinate)
Gavin Shandff4a392014-07-15 17:00:55 +10001562 pnv_ioda_setup_bus_dma(pe, dev->subordinate,
1563 add_to_iommu_group);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001564 }
1565}
1566
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001567static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
1568 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001569 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001570{
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001571 __be64 __iomem *invalidate = rm ?
1572 (__be64 __iomem *)pe->tce_inval_reg_phys :
1573 (__be64 __iomem *)tbl->it_index;
Gavin Shan4cce9552013-04-25 19:21:00 +00001574 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001575 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001576
1577 start = __pa(startp);
1578 end = __pa(endp);
1579
1580 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1581 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001582 start <<= shift;
1583 end <<= shift;
1584 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001585 start |= tbl->it_busno;
1586 end |= tbl->it_busno;
1587 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1588 /* p7ioc-style invalidation, 2 TCEs per write */
1589 start |= (1ull << 63);
1590 end |= (1ull << 63);
1591 inc = 16;
1592 } else {
1593 /* Default (older HW) */
1594 inc = 128;
1595 }
1596
1597 end |= inc - 1; /* round up end to be different than start */
1598
1599 mb(); /* Ensure above stores are visible */
1600 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001601 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001602 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001603 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001604 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001605 start += inc;
1606 }
1607
1608 /*
1609 * The iommu layer will do another mb() for us on build()
1610 * and we don't care on free()
1611 */
1612}
1613
1614static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
1615 struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001616 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001617{
1618 unsigned long start, end, inc;
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001619 __be64 __iomem *invalidate = rm ?
1620 (__be64 __iomem *)pe->tce_inval_reg_phys :
1621 (__be64 __iomem *)tbl->it_index;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001622 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001623
1624 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001625 start = 0x2ull << 60;
Gavin Shan4cce9552013-04-25 19:21:00 +00001626 start |= (pe->pe_number & 0xFF);
1627 end = start;
1628
1629 /* Figure out the start, end and step */
1630 inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001631 start |= (inc << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001632 inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001633 end |= (inc << shift);
1634 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001635 mb();
1636
1637 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001638 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001639 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001640 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001641 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001642 start += inc;
1643 }
1644}
1645
1646void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001647 __be64 *startp, __be64 *endp, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001648{
Wei Yang9e8d4a12015-03-25 16:23:54 +08001649 struct pnv_ioda_pe *pe = tbl->data;
Gavin Shan4cce9552013-04-25 19:21:00 +00001650 struct pnv_phb *phb = pe->phb;
1651
1652 if (phb->type == PNV_PHB_IODA1)
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001653 pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00001654 else
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001655 pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00001656}
1657
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001658static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1659 struct pnv_ioda_pe *pe, unsigned int base,
1660 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001661{
1662
1663 struct page *tce_mem = NULL;
1664 const __be64 *swinvp;
1665 struct iommu_table *tbl;
1666 unsigned int i;
1667 int64_t rc;
1668 void *addr;
1669
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001670 /* XXX FIXME: Handle 64-bit only DMA devices */
1671 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1672 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1673
1674 /* We shouldn't already have a 32-bit DMA associated */
1675 if (WARN_ON(pe->tce32_seg >= 0))
1676 return;
1677
1678 /* Grab a 32-bit TCE table */
1679 pe->tce32_seg = base;
1680 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1681 (base << 28), ((base + segs) << 28) - 1);
1682
1683 /* XXX Currently, we allocate one big contiguous table for the
1684 * TCEs. We only really need one chunk per 256M of TCE space
1685 * (ie per segment) but that's an optimization for later, it
1686 * requires some added smarts with our get/put_tce implementation
1687 */
1688 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1689 get_order(TCE32_TABLE_SIZE * segs));
1690 if (!tce_mem) {
1691 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1692 goto fail;
1693 }
1694 addr = page_address(tce_mem);
1695 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1696
1697 /* Configure HW */
1698 for (i = 0; i < segs; i++) {
1699 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1700 pe->pe_number,
1701 base + i, 1,
1702 __pa(addr) + TCE32_TABLE_SIZE * i,
1703 TCE32_TABLE_SIZE, 0x1000);
1704 if (rc) {
1705 pe_err(pe, " Failed to configure 32-bit TCE table,"
1706 " err %ld\n", rc);
1707 goto fail;
1708 }
1709 }
1710
1711 /* Setup linux iommu table */
Wei Yang9e8d4a12015-03-25 16:23:54 +08001712 tbl = pe->tce32_table;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001713 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001714 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001715
1716 /* OPAL variant of P7IOC SW invalidated TCEs */
1717 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1718 if (swinvp) {
1719 /* We need a couple more fields -- an address and a data
1720 * to or. Since the bus is only printed out on table free
1721 * errors, and on the first pass the data will be a relative
1722 * bus number, print that out instead.
1723 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001724 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1725 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1726 8);
Gavin Shan65fd7662014-04-24 18:00:28 +10001727 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1728 TCE_PCI_SWINV_FREE |
1729 TCE_PCI_SWINV_PAIR);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001730 }
1731 iommu_init_table(tbl, phb->hose->node);
1732
Wei Yang781a8682015-03-25 16:23:57 +08001733 if (pe->flags & PNV_IODA_PE_DEV) {
1734 iommu_register_group(tbl, phb->hose->global_number,
1735 pe->pe_number);
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +11001736 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001737 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)) {
1738 iommu_register_group(tbl, phb->hose->global_number,
1739 pe->pe_number);
Gavin Shandff4a392014-07-15 17:00:55 +10001740 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Wei Yang781a8682015-03-25 16:23:57 +08001741 } else if (pe->flags & PNV_IODA_PE_VF) {
1742 iommu_register_group(tbl, phb->hose->global_number,
1743 pe->pe_number);
1744 }
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001745
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001746 return;
1747 fail:
1748 /* XXX Failure: Try to fallback to 64-bit only ? */
1749 if (pe->tce32_seg >= 0)
1750 pe->tce32_seg = -1;
1751 if (tce_mem)
1752 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
1753}
1754
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001755static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
1756{
Wei Yang9e8d4a12015-03-25 16:23:54 +08001757 struct pnv_ioda_pe *pe = tbl->data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001758 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1759 int64_t rc;
1760
1761 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1762 if (enable) {
1763 phys_addr_t top = memblock_end_of_DRAM();
1764
1765 top = roundup_pow_of_two(top);
1766 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1767 pe->pe_number,
1768 window_id,
1769 pe->tce_bypass_base,
1770 top);
1771 } else {
1772 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1773 pe->pe_number,
1774 window_id,
1775 pe->tce_bypass_base,
1776 0);
1777
1778 /*
Gavin Shandff4a392014-07-15 17:00:55 +10001779 * EEH needs the mapping between IOMMU table and group
1780 * of those VFIO/KVM pass-through devices. We can postpone
1781 * resetting DMA ops until the DMA mask is configured in
1782 * host side.
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001783 */
Gavin Shandff4a392014-07-15 17:00:55 +10001784 if (pe->pdev)
1785 set_iommu_table_base(&pe->pdev->dev, tbl);
1786 else
1787 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001788 }
1789 if (rc)
1790 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1791 else
1792 pe->tce_bypass_enabled = enable;
1793}
1794
1795static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
1796 struct pnv_ioda_pe *pe)
1797{
1798 /* TVE #1 is selected by PCI address bit 59 */
1799 pe->tce_bypass_base = 1ull << 59;
1800
1801 /* Install set_bypass callback for VFIO */
Wei Yang9e8d4a12015-03-25 16:23:54 +08001802 pe->tce32_table->set_bypass = pnv_pci_ioda2_set_bypass;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001803
1804 /* Enable bypass by default */
Wei Yang9e8d4a12015-03-25 16:23:54 +08001805 pnv_pci_ioda2_set_bypass(pe->tce32_table, true);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001806}
1807
Gavin Shan373f5652013-04-25 19:21:01 +00001808static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1809 struct pnv_ioda_pe *pe)
1810{
1811 struct page *tce_mem = NULL;
1812 void *addr;
1813 const __be64 *swinvp;
1814 struct iommu_table *tbl;
1815 unsigned int tce_table_size, end;
1816 int64_t rc;
1817
1818 /* We shouldn't already have a 32-bit DMA associated */
1819 if (WARN_ON(pe->tce32_seg >= 0))
1820 return;
1821
1822 /* The PE will reserve all possible 32-bits space */
1823 pe->tce32_seg = 0;
1824 end = (1 << ilog2(phb->ioda.m32_pci_base));
1825 tce_table_size = (end / 0x1000) * 8;
1826 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1827 end);
1828
1829 /* Allocate TCE table */
1830 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1831 get_order(tce_table_size));
1832 if (!tce_mem) {
1833 pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
1834 goto fail;
1835 }
1836 addr = page_address(tce_mem);
1837 memset(addr, 0, tce_table_size);
1838
1839 /*
1840 * Map TCE table through TVT. The TVE index is the PE number
1841 * shifted by 1 bit for 32-bits DMA space.
1842 */
1843 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1844 pe->pe_number << 1, 1, __pa(addr),
1845 tce_table_size, 0x1000);
1846 if (rc) {
1847 pe_err(pe, "Failed to configure 32-bit TCE table,"
1848 " err %ld\n", rc);
1849 goto fail;
1850 }
1851
1852 /* Setup linux iommu table */
Wei Yang9e8d4a12015-03-25 16:23:54 +08001853 tbl = pe->tce32_table;
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001854 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
1855 IOMMU_PAGE_SHIFT_4K);
Gavin Shan373f5652013-04-25 19:21:01 +00001856
1857 /* OPAL variant of PHB3 invalidated TCEs */
1858 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
1859 if (swinvp) {
1860 /* We need a couple more fields -- an address and a data
1861 * to or. Since the bus is only printed out on table free
1862 * errors, and on the first pass the data will be a relative
1863 * bus number, print that out instead.
1864 */
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001865 pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
1866 tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
1867 8);
Gavin Shan65fd7662014-04-24 18:00:28 +10001868 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
Gavin Shan373f5652013-04-25 19:21:01 +00001869 }
1870 iommu_init_table(tbl, phb->hose->node);
1871
Wei Yang781a8682015-03-25 16:23:57 +08001872 if (pe->flags & PNV_IODA_PE_DEV) {
1873 iommu_register_group(tbl, phb->hose->global_number,
1874 pe->pe_number);
Alexey Kardashevskiyd905c5d2013-11-21 17:43:14 +11001875 set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001876 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)) {
1877 iommu_register_group(tbl, phb->hose->global_number,
1878 pe->pe_number);
Gavin Shandff4a392014-07-15 17:00:55 +10001879 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Wei Yang781a8682015-03-25 16:23:57 +08001880 } else if (pe->flags & PNV_IODA_PE_VF) {
1881 iommu_register_group(tbl, phb->hose->global_number,
1882 pe->pe_number);
1883 }
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001884
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001885 /* Also create a bypass window */
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -02001886 if (!pnv_iommu_bypass_disabled)
1887 pnv_pci_ioda2_setup_bypass_pe(phb, pe);
1888
Gavin Shan373f5652013-04-25 19:21:01 +00001889 return;
1890fail:
1891 if (pe->tce32_seg >= 0)
1892 pe->tce32_seg = -1;
1893 if (tce_mem)
1894 __free_pages(tce_mem, get_order(tce_table_size));
1895}
1896
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001897static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001898{
1899 struct pci_controller *hose = phb->hose;
1900 unsigned int residual, remaining, segs, tw, base;
1901 struct pnv_ioda_pe *pe;
1902
1903 /* If we have more PE# than segments available, hand out one
1904 * per PE until we run out and let the rest fail. If not,
1905 * then we assign at least one segment per PE, plus more based
1906 * on the amount of devices under that PE
1907 */
1908 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
1909 residual = 0;
1910 else
1911 residual = phb->ioda.tce32_count -
1912 phb->ioda.dma_pe_count;
1913
1914 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
1915 hose->global_number, phb->ioda.tce32_count);
1916 pr_info("PCI: %d PE# for a total weight of %d\n",
1917 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
1918
1919 /* Walk our PE list and configure their DMA segments, hand them
1920 * out one base segment plus any residual segments based on
1921 * weight
1922 */
1923 remaining = phb->ioda.tce32_count;
1924 tw = phb->ioda.dma_weight;
1925 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00001926 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001927 if (!pe->dma_weight)
1928 continue;
1929 if (!remaining) {
1930 pe_warn(pe, "No DMA32 resources available\n");
1931 continue;
1932 }
1933 segs = 1;
1934 if (residual) {
1935 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
1936 if (segs > remaining)
1937 segs = remaining;
1938 }
Gavin Shan373f5652013-04-25 19:21:01 +00001939
1940 /*
1941 * For IODA2 compliant PHB3, we needn't care about the weight.
1942 * The all available 32-bits DMA space will be assigned to
1943 * the specific PE.
1944 */
1945 if (phb->type == PNV_PHB_IODA1) {
1946 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1947 pe->dma_weight, segs);
1948 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1949 } else {
1950 pe_info(pe, "Assign DMA32 space\n");
1951 segs = 0;
1952 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1953 }
1954
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001955 remaining -= segs;
1956 base += segs;
1957 }
1958}
1959
1960#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00001961static void pnv_ioda2_msi_eoi(struct irq_data *d)
1962{
1963 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
1964 struct irq_chip *chip = irq_data_get_irq_chip(d);
1965 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
1966 ioda.irq_chip);
1967 int64_t rc;
1968
1969 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
1970 WARN_ON_ONCE(rc);
1971
1972 icp_native_eoi(d);
1973}
1974
Ian Munsiefd9a1c22014-10-08 19:54:55 +11001975
1976static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
1977{
1978 struct irq_data *idata;
1979 struct irq_chip *ichip;
1980
1981 if (phb->type != PNV_PHB_IODA2)
1982 return;
1983
1984 if (!phb->ioda.irq_chip_init) {
1985 /*
1986 * First time we setup an MSI IRQ, we need to setup the
1987 * corresponding IRQ chip to route correctly.
1988 */
1989 idata = irq_get_irq_data(virq);
1990 ichip = irq_data_get_irq_chip(idata);
1991 phb->ioda.irq_chip_init = 1;
1992 phb->ioda.irq_chip = *ichip;
1993 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
1994 }
1995 irq_set_chip(virq, &phb->ioda.irq_chip);
1996}
1997
Ian Munsie80c49c72014-10-08 19:54:57 +11001998#ifdef CONFIG_CXL_BASE
1999
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002000struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002001{
2002 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2003
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002004 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002005}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002006EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002007
Ryan Grimm1212aa12015-01-19 11:52:50 -06002008int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002009{
2010 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2011 struct pnv_phb *phb = hose->private_data;
2012 struct pnv_ioda_pe *pe;
2013 int rc;
2014
2015 pe = pnv_ioda_get_pe(dev);
2016 if (!pe)
2017 return -ENODEV;
2018
2019 pe_info(pe, "Switching PHB to CXL\n");
2020
Ryan Grimm1212aa12015-01-19 11:52:50 -06002021 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11002022 if (rc)
2023 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2024
2025 return rc;
2026}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002027EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002028
2029/* Find PHB for cxl dev and allocate MSI hwirqs?
2030 * Returns the absolute hardware IRQ number
2031 */
2032int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2033{
2034 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2035 struct pnv_phb *phb = hose->private_data;
2036 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2037
2038 if (hwirq < 0) {
2039 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2040 return -ENOSPC;
2041 }
2042
2043 return phb->msi_base + hwirq;
2044}
2045EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2046
2047void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2048{
2049 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2050 struct pnv_phb *phb = hose->private_data;
2051
2052 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2053}
2054EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2055
2056void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2057 struct pci_dev *dev)
2058{
2059 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2060 struct pnv_phb *phb = hose->private_data;
2061 int i, hwirq;
2062
2063 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2064 if (!irqs->range[i])
2065 continue;
2066 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2067 i, irqs->offset[i],
2068 irqs->range[i]);
2069 hwirq = irqs->offset[i] - phb->msi_base;
2070 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2071 irqs->range[i]);
2072 }
2073}
2074EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2075
2076int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2077 struct pci_dev *dev, int num)
2078{
2079 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2080 struct pnv_phb *phb = hose->private_data;
2081 int i, hwirq, try;
2082
2083 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2084
2085 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2086 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2087 try = num;
2088 while (try) {
2089 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2090 if (hwirq >= 0)
2091 break;
2092 try /= 2;
2093 }
2094 if (!try)
2095 goto fail;
2096
2097 irqs->offset[i] = phb->msi_base + hwirq;
2098 irqs->range[i] = try;
2099 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2100 i, irqs->offset[i], irqs->range[i]);
2101 num -= try;
2102 }
2103 if (num)
2104 goto fail;
2105
2106 return 0;
2107fail:
2108 pnv_cxl_release_hwirq_ranges(irqs, dev);
2109 return -ENOSPC;
2110}
2111EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2112
2113int pnv_cxl_get_irq_count(struct pci_dev *dev)
2114{
2115 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2116 struct pnv_phb *phb = hose->private_data;
2117
2118 return phb->msi_bmp.irq_count;
2119}
2120EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2121
2122int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2123 unsigned int virq)
2124{
2125 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2126 struct pnv_phb *phb = hose->private_data;
2127 unsigned int xive_num = hwirq - phb->msi_base;
2128 struct pnv_ioda_pe *pe;
2129 int rc;
2130
2131 if (!(pe = pnv_ioda_get_pe(dev)))
2132 return -ENODEV;
2133
2134 /* Assign XIVE to PE */
2135 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2136 if (rc) {
2137 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2138 "hwirq 0x%x XIVE 0x%x PE\n",
2139 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2140 return -EIO;
2141 }
2142 set_msi_irq_chip(phb, virq);
2143
2144 return 0;
2145}
2146EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2147#endif
2148
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002149static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002150 unsigned int hwirq, unsigned int virq,
2151 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002152{
2153 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2154 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002155 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002156 int rc;
2157
2158 /* No PE assigned ? bail out ... no MSI for you ! */
2159 if (pe == NULL)
2160 return -ENXIO;
2161
2162 /* Check if we have an MVE */
2163 if (pe->mve_number < 0)
2164 return -ENXIO;
2165
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002166 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002167 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002168 is_64 = 0;
2169
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002170 /* Assign XIVE to PE */
2171 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2172 if (rc) {
2173 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2174 pci_name(dev), rc, xive_num);
2175 return -EIO;
2176 }
2177
2178 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002179 __be64 addr64;
2180
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002181 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2182 &addr64, &data);
2183 if (rc) {
2184 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2185 pci_name(dev), rc);
2186 return -EIO;
2187 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002188 msg->address_hi = be64_to_cpu(addr64) >> 32;
2189 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002190 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002191 __be32 addr32;
2192
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002193 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2194 &addr32, &data);
2195 if (rc) {
2196 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2197 pci_name(dev), rc);
2198 return -EIO;
2199 }
2200 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002201 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002202 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002203 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002204
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002205 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002206
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002207 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2208 " address=%x_%08x data=%x PE# %d\n",
2209 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2210 msg->address_hi, msg->address_lo, data, pe->pe_number);
2211
2212 return 0;
2213}
2214
2215static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2216{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002217 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002218 const __be32 *prop = of_get_property(phb->hose->dn,
2219 "ibm,opal-msi-ranges", NULL);
2220 if (!prop) {
2221 /* BML Fallback */
2222 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2223 }
2224 if (!prop)
2225 return;
2226
2227 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002228 count = be32_to_cpup(prop + 1);
2229 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002230 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2231 phb->hose->global_number);
2232 return;
2233 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002234
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002235 phb->msi_setup = pnv_pci_ioda_msi_setup;
2236 phb->msi32_support = 1;
2237 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002238 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002239}
2240#else
2241static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2242#endif /* CONFIG_PCI_MSI */
2243
Wei Yang6e628c72015-03-25 16:23:55 +08002244#ifdef CONFIG_PCI_IOV
2245static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2246{
2247 struct pci_controller *hose;
2248 struct pnv_phb *phb;
2249 struct resource *res;
2250 int i;
2251 resource_size_t size;
2252 struct pci_dn *pdn;
2253
2254 if (!pdev->is_physfn || pdev->is_added)
2255 return;
2256
2257 hose = pci_bus_to_host(pdev->bus);
2258 phb = hose->private_data;
2259
2260 pdn = pci_get_pdn(pdev);
2261 pdn->vfs_expanded = 0;
2262
2263 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2264 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2265 if (!res->flags || res->parent)
2266 continue;
2267 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2268 dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
2269 i, res);
2270 continue;
2271 }
2272
2273 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
2274 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
2275 res->end = res->start + size * phb->ioda.total_pe - 1;
2276 dev_dbg(&pdev->dev, " %pR\n", res);
2277 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
2278 i, res, phb->ioda.total_pe);
2279 }
2280 pdn->vfs_expanded = phb->ioda.total_pe;
2281}
2282#endif /* CONFIG_PCI_IOV */
2283
Gavin Shan11685be2012-08-20 03:49:16 +00002284/*
2285 * This function is supposed to be called on basis of PE from top
2286 * to bottom style. So the the I/O or MMIO segment assigned to
2287 * parent PE could be overrided by its child PEs if necessary.
2288 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002289static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
2290 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00002291{
2292 struct pnv_phb *phb = hose->private_data;
2293 struct pci_bus_region region;
2294 struct resource *res;
2295 int i, index;
2296 int rc;
2297
2298 /*
2299 * NOTE: We only care PCI bus based PE for now. For PCI
2300 * device based PE, for example SRIOV sensitive VF should
2301 * be figured out later.
2302 */
2303 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2304
2305 pci_bus_for_each_resource(pe->pbus, res, i) {
2306 if (!res || !res->flags ||
2307 res->start > res->end)
2308 continue;
2309
2310 if (res->flags & IORESOURCE_IO) {
2311 region.start = res->start - phb->ioda.io_pci_base;
2312 region.end = res->end - phb->ioda.io_pci_base;
2313 index = region.start / phb->ioda.io_segsize;
2314
2315 while (index < phb->ioda.total_pe &&
2316 region.start <= region.end) {
2317 phb->ioda.io_segmap[index] = pe->pe_number;
2318 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2319 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2320 if (rc != OPAL_SUCCESS) {
2321 pr_err("%s: OPAL error %d when mapping IO "
2322 "segment #%d to PE#%d\n",
2323 __func__, rc, index, pe->pe_number);
2324 break;
2325 }
2326
2327 region.start += phb->ioda.io_segsize;
2328 index++;
2329 }
2330 } else if (res->flags & IORESOURCE_MEM) {
2331 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002332 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002333 phb->ioda.m32_pci_base;
2334 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002335 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002336 phb->ioda.m32_pci_base;
2337 index = region.start / phb->ioda.m32_segsize;
2338
2339 while (index < phb->ioda.total_pe &&
2340 region.start <= region.end) {
2341 phb->ioda.m32_segmap[index] = pe->pe_number;
2342 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2343 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2344 if (rc != OPAL_SUCCESS) {
2345 pr_err("%s: OPAL error %d when mapping M32 "
2346 "segment#%d to PE#%d",
2347 __func__, rc, index, pe->pe_number);
2348 break;
2349 }
2350
2351 region.start += phb->ioda.m32_segsize;
2352 index++;
2353 }
2354 }
2355 }
2356}
2357
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002358static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00002359{
2360 struct pci_controller *tmp, *hose;
2361 struct pnv_phb *phb;
2362 struct pnv_ioda_pe *pe;
2363
2364 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2365 phb = hose->private_data;
2366 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2367 pnv_ioda_setup_pe_seg(hose, pe);
2368 }
2369 }
2370}
2371
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002372static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00002373{
2374 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00002375 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00002376
2377 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2378 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00002379
2380 /* Mark the PHB initialization done */
2381 phb = hose->private_data;
2382 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00002383 }
2384}
2385
Gavin Shan37c367f2013-06-20 18:13:25 +08002386static void pnv_pci_ioda_create_dbgfs(void)
2387{
2388#ifdef CONFIG_DEBUG_FS
2389 struct pci_controller *hose, *tmp;
2390 struct pnv_phb *phb;
2391 char name[16];
2392
2393 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2394 phb = hose->private_data;
2395
2396 sprintf(name, "PCI%04x", hose->global_number);
2397 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
2398 if (!phb->dbgfs)
2399 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
2400 __func__, hose->global_number);
2401 }
2402#endif /* CONFIG_DEBUG_FS */
2403}
2404
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002405static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00002406{
2407 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00002408 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00002409 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002410
Gavin Shan37c367f2013-06-20 18:13:25 +08002411 pnv_pci_ioda_create_dbgfs();
2412
Gavin Shane9cc17d2013-06-20 13:21:14 +08002413#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08002414 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04002415 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002416#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00002417}
2418
Gavin Shan271fd032012-09-11 16:59:47 -06002419/*
2420 * Returns the alignment for I/O or memory windows for P2P
2421 * bridges. That actually depends on how PEs are segmented.
2422 * For now, we return I/O or M32 segment size for PE sensitive
2423 * P2P bridges. Otherwise, the default values (4KiB for I/O,
2424 * 1MiB for memory) will be returned.
2425 *
2426 * The current PCI bus might be put into one PE, which was
2427 * create against the parent PCI bridge. For that case, we
2428 * needn't enlarge the alignment so that we can save some
2429 * resources.
2430 */
2431static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2432 unsigned long type)
2433{
2434 struct pci_dev *bridge;
2435 struct pci_controller *hose = pci_bus_to_host(bus);
2436 struct pnv_phb *phb = hose->private_data;
2437 int num_pci_bridges = 0;
2438
2439 bridge = bus->self;
2440 while (bridge) {
2441 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2442 num_pci_bridges++;
2443 if (num_pci_bridges >= 2)
2444 return 1;
2445 }
2446
2447 bridge = bridge->bus->self;
2448 }
2449
Guo Chao262af552014-07-21 14:42:30 +10002450 /* We fail back to M32 if M64 isn't supported */
2451 if (phb->ioda.m64_segsize &&
2452 pnv_pci_is_mem_pref_64(type))
2453 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06002454 if (type & IORESOURCE_MEM)
2455 return phb->ioda.m32_segsize;
2456
2457 return phb->ioda.io_segsize;
2458}
2459
Wei Yang5350ab32015-03-25 16:23:56 +08002460#ifdef CONFIG_PCI_IOV
2461static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
2462 int resno)
2463{
2464 struct pci_dn *pdn = pci_get_pdn(pdev);
2465 resource_size_t align, iov_align;
2466
2467 iov_align = resource_size(&pdev->resource[resno]);
2468 if (iov_align)
2469 return iov_align;
2470
2471 align = pci_iov_resource_size(pdev, resno);
2472 if (pdn->vfs_expanded)
2473 return pdn->vfs_expanded * align;
2474
2475 return align;
2476}
2477#endif /* CONFIG_PCI_IOV */
2478
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002479/* Prevent enabling devices for which we couldn't properly
2480 * assign a PE
2481 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002482static int pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002483{
Gavin Shandb1266c2012-08-20 03:49:18 +00002484 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2485 struct pnv_phb *phb = hose->private_data;
2486 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002487
Gavin Shandb1266c2012-08-20 03:49:18 +00002488 /* The function is probably called while the PEs have
2489 * not be created yet. For example, resource reassignment
2490 * during PCI probe period. We just skip the check if
2491 * PEs isn't ready.
2492 */
2493 if (!phb->initialized)
2494 return 0;
2495
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002496 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002497 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2498 return -EINVAL;
Gavin Shandb1266c2012-08-20 03:49:18 +00002499
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002500 return 0;
2501}
2502
2503static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
2504 u32 devfn)
2505{
2506 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
2507}
2508
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10002509static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
2510{
Gavin Shand1a85ee2014-09-30 12:39:05 +10002511 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10002512 OPAL_ASSERT_RESET);
2513}
2514
Anton Blancharde51df2c2014-08-20 08:55:18 +10002515static void __init pnv_pci_init_ioda_phb(struct device_node *np,
2516 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002517{
2518 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002519 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08002520 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10002521 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002522 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08002523 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002524 u64 phb_id;
2525 void *aux;
2526 long rc;
2527
Gavin Shan58d714e2013-07-31 16:47:00 +08002528 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002529
2530 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
2531 if (!prop64) {
2532 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
2533 return;
2534 }
2535 phb_id = be64_to_cpup(prop64);
2536 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
2537
Michael Ellermane39f223f2014-11-18 16:47:35 +11002538 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08002539
2540 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08002541 phb->hose = hose = pcibios_alloc_controller(np);
2542 if (!phb->hose) {
2543 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002544 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11002545 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002546 return;
2547 }
2548
2549 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08002550 prop32 = of_get_property(np, "bus-range", &len);
2551 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002552 hose->first_busno = be32_to_cpu(prop32[0]);
2553 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08002554 } else {
2555 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
2556 hose->first_busno = 0;
2557 hose->last_busno = 0xff;
2558 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002559 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08002560 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002561 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00002562 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08002563 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002564
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00002565 /* Detect specific models for error handling */
2566 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
2567 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00002568 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00002569 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00002570 else
2571 phb->model = PNV_PHB_MODEL_UNKNOWN;
2572
Gavin Shanaa0c0332013-04-25 19:20:57 +00002573 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08002574 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002575
Gavin Shanaa0c0332013-04-25 19:20:57 +00002576 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002577 phb->regs = of_iomap(np, 0);
2578 if (phb->regs == NULL)
2579 pr_err(" Failed to map registers !\n");
2580
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002581 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08002582 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00002583 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08002584 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002585 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08002586 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
2587 if (prop32)
2588 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10002589
2590 /* Parse 64-bit MMIO range */
2591 pnv_ioda_parse_m64_window(phb);
2592
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002593 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00002594 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002595 phb->ioda.m32_size += 0x10000;
2596
2597 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002598 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002599 phb->ioda.io_size = hose->pci_io_size;
2600 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
2601 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
2602
Gavin Shanc35d2a82013-07-31 16:47:04 +08002603 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002604 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
2605 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00002606 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08002607 if (phb->type == PNV_PHB_IODA1) {
2608 iomap_off = size;
2609 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
2610 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002611 pemap_off = size;
2612 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11002613 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002614 phb->ioda.pe_alloc = aux;
2615 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08002616 if (phb->type == PNV_PHB_IODA1)
2617 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002618 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08002619 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002620
Gavin Shan7ebdf952012-08-20 03:49:15 +00002621 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002622 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08002623 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002624
2625 /* Calculate how many 32-bit TCE segments we have */
2626 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
2627
Gavin Shanaa0c0332013-04-25 19:20:57 +00002628#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002629 rc = opal_pci_set_phb_mem_window(opal->phb_id,
2630 window_type,
2631 window_num,
2632 starting_real_address,
2633 starting_pci_address,
2634 segment_size);
2635#endif
2636
Guo Chao262af552014-07-21 14:42:30 +10002637 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
2638 phb->ioda.total_pe, phb->ioda.reserved_pe,
2639 phb->ioda.m32_size, phb->ioda.m32_segsize);
2640 if (phb->ioda.m64_size)
2641 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
2642 phb->ioda.m64_size, phb->ioda.m64_segsize);
2643 if (phb->ioda.io_size)
2644 pr_info(" IO: 0x%x [segment=0x%x]\n",
2645 phb->ioda.io_size, phb->ioda.io_segsize);
2646
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002647
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002648 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10002649 phb->get_pe_state = pnv_ioda_get_pe_state;
2650 phb->freeze_pe = pnv_ioda_freeze_pe;
2651 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002652
2653 /* Setup RID -> PE mapping function */
2654 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
2655
2656 /* Setup TCEs */
2657 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002658 phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10002659 phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002660
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10002661 /* Setup shutdown function for kexec */
2662 phb->shutdown = pnv_pci_ioda_shutdown;
2663
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002664 /* Setup MSI support */
2665 pnv_pci_init_ioda_msis(phb);
2666
Gavin Shanc40a4212012-08-20 03:49:20 +00002667 /*
2668 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
2669 * to let the PCI core do resource assignment. It's supposed
2670 * that the PCI core will do correct I/O and MMIO alignment
2671 * for the P2P bridge bars so that each PCI bus (excluding
2672 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002673 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00002674 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002675 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
Gavin Shan271fd032012-09-11 16:59:47 -06002676 ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
Gavin Shand92a2082014-04-24 18:00:24 +10002677 ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
Wei Yang6e628c72015-03-25 16:23:55 +08002678#ifdef CONFIG_PCI_IOV
2679 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08002680 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Wei Yang6e628c72015-03-25 16:23:55 +08002681#endif /* CONFIG_PCI_IOV */
Gavin Shanc40a4212012-08-20 03:49:20 +00002682 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002683
2684 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10002685 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002686 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00002687 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10002688
2689 /* If we're running in kdump kerenl, the previous kerenl never
2690 * shutdown PCI devices correctly. We already got IODA table
2691 * cleaned out. So we have to issue PHB reset to stop all PCI
2692 * transactions from previous kerenl.
2693 */
2694 if (is_kdump_kernel()) {
2695 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11002696 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
2697 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10002698 }
Guo Chao262af552014-07-21 14:42:30 +10002699
Gavin Shan9e9e8932014-11-12 13:36:05 +11002700 /* Remove M64 resource if we can't configure it successfully */
2701 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10002702 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00002703}
2704
Bjorn Helgaas67975002013-07-02 12:20:03 -06002705void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00002706{
Gavin Shane9cc17d2013-06-20 13:21:14 +08002707 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002708}
2709
2710void __init pnv_pci_init_ioda_hub(struct device_node *np)
2711{
2712 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10002713 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002714 u64 hub_id;
2715
2716 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
2717
2718 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
2719 if (!prop64) {
2720 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
2721 return;
2722 }
2723 hub_id = be64_to_cpup(prop64);
2724 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
2725
2726 /* Count child PHBs */
2727 for_each_child_of_node(np, phbn) {
2728 /* Look for IODA1 PHBs */
2729 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08002730 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002731 }
2732}