blob: 2c286b57e520ef4aa20de1d15de9e3ccb06c7645 [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>
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +100026#include <linux/iommu.h>
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +100027#include <linux/rculist.h>
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +100028#include <linux/sizes.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000029
30#include <asm/sections.h>
31#include <asm/io.h>
32#include <asm/prom.h>
33#include <asm/pci-bridge.h>
34#include <asm/machdep.h>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000035#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000036#include <asm/ppc-pci.h>
37#include <asm/opal.h>
38#include <asm/iommu.h>
39#include <asm/tce.h>
Gavin Shan137436c2013-04-25 19:20:59 +000040#include <asm/xics.h>
Gavin Shan37c367f2013-06-20 18:13:25 +080041#include <asm/debug.h>
Guo Chao262af552014-07-21 14:42:30 +100042#include <asm/firmware.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110043#include <asm/pnv-pci.h>
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100044#include <asm/mmzone.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110045
Michael Neulingec249dd2015-05-27 16:07:16 +100046#include <misc/cxl-base.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000047
48#include "powernv.h"
49#include "pci.h"
50
Wei Yang781a8682015-03-25 16:23:57 +080051/* 256M DMA window, 4K TCE pages, 8 bytes TCE */
52#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
53
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +100054#define POWERNV_IOMMU_DEFAULT_LEVELS 1
55#define POWERNV_IOMMU_MAX_LEVELS 5
56
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100057static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
58
Joe Perches6d31c2f2014-09-21 10:55:06 -070059static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
60 const char *fmt, ...)
61{
62 struct va_format vaf;
63 va_list args;
64 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000065
Joe Perches6d31c2f2014-09-21 10:55:06 -070066 va_start(args, fmt);
67
68 vaf.fmt = fmt;
69 vaf.va = &args;
70
Wei Yang781a8682015-03-25 16:23:57 +080071 if (pe->flags & PNV_IODA_PE_DEV)
Joe Perches6d31c2f2014-09-21 10:55:06 -070072 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
Wei Yang781a8682015-03-25 16:23:57 +080073 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Joe Perches6d31c2f2014-09-21 10:55:06 -070074 sprintf(pfix, "%04x:%02x ",
75 pci_domain_nr(pe->pbus), pe->pbus->number);
Wei Yang781a8682015-03-25 16:23:57 +080076#ifdef CONFIG_PCI_IOV
77 else if (pe->flags & PNV_IODA_PE_VF)
78 sprintf(pfix, "%04x:%02x:%2x.%d",
79 pci_domain_nr(pe->parent_dev->bus),
80 (pe->rid & 0xff00) >> 8,
81 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
82#endif /* CONFIG_PCI_IOV*/
Joe Perches6d31c2f2014-09-21 10:55:06 -070083
84 printk("%spci %s: [PE# %.3d] %pV",
85 level, pfix, pe->pe_number, &vaf);
86
87 va_end(args);
88}
89
90#define pe_err(pe, fmt, ...) \
91 pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
92#define pe_warn(pe, fmt, ...) \
93 pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
94#define pe_info(pe, fmt, ...) \
95 pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000096
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020097static bool pnv_iommu_bypass_disabled __read_mostly;
98
99static int __init iommu_setup(char *str)
100{
101 if (!str)
102 return -EINVAL;
103
104 while (*str) {
105 if (!strncmp(str, "nobypass", 8)) {
106 pnv_iommu_bypass_disabled = true;
107 pr_info("PowerNV: IOMMU bypass window disabled.\n");
108 break;
109 }
110 str += strcspn(str, ",");
111 if (*str == ',')
112 str++;
113 }
114
115 return 0;
116}
117early_param("iommu", iommu_setup);
118
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000119/*
120 * stdcix is only supposed to be used in hypervisor real mode as per
121 * the architecture spec
122 */
123static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
124{
125 __asm__ __volatile__("stdcix %0,0,%1"
126 : : "r" (val), "r" (paddr) : "memory");
127}
128
Guo Chao262af552014-07-21 14:42:30 +1000129static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
130{
131 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
132 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
133}
134
Gavin Shan4b82ab12014-11-12 13:36:07 +1100135static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
136{
137 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
138 pr_warn("%s: Invalid PE %d on PHB#%x\n",
139 __func__, pe_no, phb->hose->global_number);
140 return;
141 }
142
143 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) {
144 pr_warn("%s: PE %d was assigned on PHB#%x\n",
145 __func__, pe_no, phb->hose->global_number);
146 return;
147 }
148
149 phb->ioda.pe_array[pe_no].phb = phb;
150 phb->ioda.pe_array[pe_no].pe_number = pe_no;
151}
152
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800153static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000154{
155 unsigned long pe;
156
157 do {
158 pe = find_next_zero_bit(phb->ioda.pe_alloc,
159 phb->ioda.total_pe, 0);
160 if (pe >= phb->ioda.total_pe)
161 return IODA_INVALID_PE;
162 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
163
Gavin Shan4cce9552013-04-25 19:21:00 +0000164 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000165 phb->ioda.pe_array[pe].pe_number = pe;
166 return pe;
167}
168
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800169static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000170{
171 WARN_ON(phb->ioda.pe_array[pe].pdev);
172
173 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
174 clear_bit(pe, phb->ioda.pe_alloc);
175}
176
Guo Chao262af552014-07-21 14:42:30 +1000177/* The default M64 BAR is shared by all PEs */
178static int pnv_ioda2_init_m64(struct pnv_phb *phb)
179{
180 const char *desc;
181 struct resource *r;
182 s64 rc;
183
184 /* Configure the default M64 BAR */
185 rc = opal_pci_set_phb_mem_window(phb->opal_id,
186 OPAL_M64_WINDOW_TYPE,
187 phb->ioda.m64_bar_idx,
188 phb->ioda.m64_base,
189 0, /* unused */
190 phb->ioda.m64_size);
191 if (rc != OPAL_SUCCESS) {
192 desc = "configuring";
193 goto fail;
194 }
195
196 /* Enable the default M64 BAR */
197 rc = opal_pci_phb_mmio_enable(phb->opal_id,
198 OPAL_M64_WINDOW_TYPE,
199 phb->ioda.m64_bar_idx,
200 OPAL_ENABLE_M64_SPLIT);
201 if (rc != OPAL_SUCCESS) {
202 desc = "enabling";
203 goto fail;
204 }
205
206 /* Mark the M64 BAR assigned */
207 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
208
209 /*
210 * Strip off the segment used by the reserved PE, which is
211 * expected to be 0 or last one of PE capabicity.
212 */
213 r = &phb->hose->mem_resources[1];
214 if (phb->ioda.reserved_pe == 0)
215 r->start += phb->ioda.m64_segsize;
216 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
217 r->end -= phb->ioda.m64_segsize;
218 else
219 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
220 phb->ioda.reserved_pe);
221
222 return 0;
223
224fail:
225 pr_warn(" Failure %lld %s M64 BAR#%d\n",
226 rc, desc, phb->ioda.m64_bar_idx);
227 opal_pci_phb_mmio_enable(phb->opal_id,
228 OPAL_M64_WINDOW_TYPE,
229 phb->ioda.m64_bar_idx,
230 OPAL_DISABLE_M64);
231 return -EIO;
232}
233
Gavin Shan5ef73562014-11-12 13:36:06 +1100234static void pnv_ioda2_reserve_m64_pe(struct pnv_phb *phb)
Guo Chao262af552014-07-21 14:42:30 +1000235{
236 resource_size_t sgsz = phb->ioda.m64_segsize;
237 struct pci_dev *pdev;
238 struct resource *r;
239 int base, step, i;
240
241 /*
242 * Root bus always has full M64 range and root port has
243 * M64 range used in reality. So we're checking root port
244 * instead of root bus.
245 */
246 list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100247 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
248 r = &pdev->resource[PCI_BRIDGE_RESOURCES + i];
Guo Chao262af552014-07-21 14:42:30 +1000249 if (!r->parent ||
250 !pnv_pci_is_mem_pref_64(r->flags))
251 continue;
252
253 base = (r->start - phb->ioda.m64_base) / sgsz;
254 for (step = 0; step < resource_size(r) / sgsz; step++)
Gavin Shan4b82ab12014-11-12 13:36:07 +1100255 pnv_ioda_reserve_pe(phb, base + step);
Guo Chao262af552014-07-21 14:42:30 +1000256 }
257 }
258}
259
260static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
261 struct pci_bus *bus, int all)
262{
263 resource_size_t segsz = phb->ioda.m64_segsize;
264 struct pci_dev *pdev;
265 struct resource *r;
266 struct pnv_ioda_pe *master_pe, *pe;
267 unsigned long size, *pe_alloc;
268 bool found;
269 int start, i, j;
270
271 /* Root bus shouldn't use M64 */
272 if (pci_is_root_bus(bus))
273 return IODA_INVALID_PE;
274
275 /* We support only one M64 window on each bus */
276 found = false;
277 pci_bus_for_each_resource(bus, r, i) {
278 if (r && r->parent &&
279 pnv_pci_is_mem_pref_64(r->flags)) {
280 found = true;
281 break;
282 }
283 }
284
285 /* No M64 window found ? */
286 if (!found)
287 return IODA_INVALID_PE;
288
289 /* Allocate bitmap */
290 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
291 pe_alloc = kzalloc(size, GFP_KERNEL);
292 if (!pe_alloc) {
293 pr_warn("%s: Out of memory !\n",
294 __func__);
295 return IODA_INVALID_PE;
296 }
297
298 /*
299 * Figure out reserved PE numbers by the PE
300 * the its child PEs.
301 */
302 start = (r->start - phb->ioda.m64_base) / segsz;
303 for (i = 0; i < resource_size(r) / segsz; i++)
304 set_bit(start + i, pe_alloc);
305
306 if (all)
307 goto done;
308
309 /*
310 * If the PE doesn't cover all subordinate buses,
311 * we need subtract from reserved PEs for children.
312 */
313 list_for_each_entry(pdev, &bus->devices, bus_list) {
314 if (!pdev->subordinate)
315 continue;
316
317 pci_bus_for_each_resource(pdev->subordinate, r, i) {
318 if (!r || !r->parent ||
319 !pnv_pci_is_mem_pref_64(r->flags))
320 continue;
321
322 start = (r->start - phb->ioda.m64_base) / segsz;
323 for (j = 0; j < resource_size(r) / segsz ; j++)
324 clear_bit(start + j, pe_alloc);
325 }
326 }
327
328 /*
329 * the current bus might not own M64 window and that's all
330 * contributed by its child buses. For the case, we needn't
331 * pick M64 dependent PE#.
332 */
333 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
334 kfree(pe_alloc);
335 return IODA_INVALID_PE;
336 }
337
338 /*
339 * Figure out the master PE and put all slave PEs to master
340 * PE's list to form compound PE.
341 */
342done:
343 master_pe = NULL;
344 i = -1;
345 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
346 phb->ioda.total_pe) {
347 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000348
349 if (!master_pe) {
350 pe->flags |= PNV_IODA_PE_MASTER;
351 INIT_LIST_HEAD(&pe->slaves);
352 master_pe = pe;
353 } else {
354 pe->flags |= PNV_IODA_PE_SLAVE;
355 pe->master = master_pe;
356 list_add_tail(&pe->list, &master_pe->slaves);
357 }
358 }
359
360 kfree(pe_alloc);
361 return master_pe->pe_number;
362}
363
364static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
365{
366 struct pci_controller *hose = phb->hose;
367 struct device_node *dn = hose->dn;
368 struct resource *res;
369 const u32 *r;
370 u64 pci_addr;
371
Gavin Shan1665c4a2014-11-12 13:36:04 +1100372 /* FIXME: Support M64 for P7IOC */
373 if (phb->type != PNV_PHB_IODA2) {
374 pr_info(" Not support M64 window\n");
375 return;
376 }
377
Guo Chao262af552014-07-21 14:42:30 +1000378 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
379 pr_info(" Firmware too old to support M64 window\n");
380 return;
381 }
382
383 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
384 if (!r) {
385 pr_info(" No <ibm,opal-m64-window> on %s\n",
386 dn->full_name);
387 return;
388 }
389
Guo Chao262af552014-07-21 14:42:30 +1000390 res = &hose->mem_resources[1];
391 res->start = of_translate_address(dn, r + 2);
392 res->end = res->start + of_read_number(r + 4, 2) - 1;
393 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
394 pci_addr = of_read_number(r, 2);
395 hose->mem_offset[1] = res->start - pci_addr;
396
397 phb->ioda.m64_size = resource_size(res);
398 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
399 phb->ioda.m64_base = pci_addr;
400
Wei Yange9863e62014-12-12 12:39:37 +0800401 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
402 res->start, res->end, pci_addr);
403
Guo Chao262af552014-07-21 14:42:30 +1000404 /* Use last M64 BAR to cover M64 window */
405 phb->ioda.m64_bar_idx = 15;
406 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shan5ef73562014-11-12 13:36:06 +1100407 phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000408 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
409}
410
Gavin Shan49dec922014-07-21 14:42:33 +1000411static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
412{
413 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
414 struct pnv_ioda_pe *slave;
415 s64 rc;
416
417 /* Fetch master PE */
418 if (pe->flags & PNV_IODA_PE_SLAVE) {
419 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100420 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
421 return;
422
Gavin Shan49dec922014-07-21 14:42:33 +1000423 pe_no = pe->pe_number;
424 }
425
426 /* Freeze master PE */
427 rc = opal_pci_eeh_freeze_set(phb->opal_id,
428 pe_no,
429 OPAL_EEH_ACTION_SET_FREEZE_ALL);
430 if (rc != OPAL_SUCCESS) {
431 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
432 __func__, rc, phb->hose->global_number, pe_no);
433 return;
434 }
435
436 /* Freeze slave PEs */
437 if (!(pe->flags & PNV_IODA_PE_MASTER))
438 return;
439
440 list_for_each_entry(slave, &pe->slaves, list) {
441 rc = opal_pci_eeh_freeze_set(phb->opal_id,
442 slave->pe_number,
443 OPAL_EEH_ACTION_SET_FREEZE_ALL);
444 if (rc != OPAL_SUCCESS)
445 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
446 __func__, rc, phb->hose->global_number,
447 slave->pe_number);
448 }
449}
450
Anton Blancharde51df2c2014-08-20 08:55:18 +1000451static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000452{
453 struct pnv_ioda_pe *pe, *slave;
454 s64 rc;
455
456 /* Find master PE */
457 pe = &phb->ioda.pe_array[pe_no];
458 if (pe->flags & PNV_IODA_PE_SLAVE) {
459 pe = pe->master;
460 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
461 pe_no = pe->pe_number;
462 }
463
464 /* Clear frozen state for master PE */
465 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
466 if (rc != OPAL_SUCCESS) {
467 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
468 __func__, rc, opt, phb->hose->global_number, pe_no);
469 return -EIO;
470 }
471
472 if (!(pe->flags & PNV_IODA_PE_MASTER))
473 return 0;
474
475 /* Clear frozen state for slave PEs */
476 list_for_each_entry(slave, &pe->slaves, list) {
477 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
478 slave->pe_number,
479 opt);
480 if (rc != OPAL_SUCCESS) {
481 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
482 __func__, rc, opt, phb->hose->global_number,
483 slave->pe_number);
484 return -EIO;
485 }
486 }
487
488 return 0;
489}
490
491static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
492{
493 struct pnv_ioda_pe *slave, *pe;
494 u8 fstate, state;
495 __be16 pcierr;
496 s64 rc;
497
498 /* Sanity check on PE number */
499 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
500 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
501
502 /*
503 * Fetch the master PE and the PE instance might be
504 * not initialized yet.
505 */
506 pe = &phb->ioda.pe_array[pe_no];
507 if (pe->flags & PNV_IODA_PE_SLAVE) {
508 pe = pe->master;
509 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
510 pe_no = pe->pe_number;
511 }
512
513 /* Check the master PE */
514 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
515 &state, &pcierr, NULL);
516 if (rc != OPAL_SUCCESS) {
517 pr_warn("%s: Failure %lld getting "
518 "PHB#%x-PE#%x state\n",
519 __func__, rc,
520 phb->hose->global_number, pe_no);
521 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
522 }
523
524 /* Check the slave PE */
525 if (!(pe->flags & PNV_IODA_PE_MASTER))
526 return state;
527
528 list_for_each_entry(slave, &pe->slaves, list) {
529 rc = opal_pci_eeh_freeze_status(phb->opal_id,
530 slave->pe_number,
531 &fstate,
532 &pcierr,
533 NULL);
534 if (rc != OPAL_SUCCESS) {
535 pr_warn("%s: Failure %lld getting "
536 "PHB#%x-PE#%x state\n",
537 __func__, rc,
538 phb->hose->global_number, slave->pe_number);
539 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
540 }
541
542 /*
543 * Override the result based on the ascending
544 * priority.
545 */
546 if (fstate > state)
547 state = fstate;
548 }
549
550 return state;
551}
552
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000553/* Currently those 2 are only used when MSIs are enabled, this will change
554 * but in the meantime, we need to protect them to avoid warnings
555 */
556#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800557static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000558{
559 struct pci_controller *hose = pci_bus_to_host(dev->bus);
560 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000561 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000562
563 if (!pdn)
564 return NULL;
565 if (pdn->pe_number == IODA_INVALID_PE)
566 return NULL;
567 return &phb->ioda.pe_array[pdn->pe_number];
568}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000569#endif /* CONFIG_PCI_MSI */
570
Gavin Shanb131a842014-11-12 13:36:08 +1100571static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
572 struct pnv_ioda_pe *parent,
573 struct pnv_ioda_pe *child,
574 bool is_add)
575{
576 const char *desc = is_add ? "adding" : "removing";
577 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
578 OPAL_REMOVE_PE_FROM_DOMAIN;
579 struct pnv_ioda_pe *slave;
580 long rc;
581
582 /* Parent PE affects child PE */
583 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
584 child->pe_number, op);
585 if (rc != OPAL_SUCCESS) {
586 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
587 rc, desc);
588 return -ENXIO;
589 }
590
591 if (!(child->flags & PNV_IODA_PE_MASTER))
592 return 0;
593
594 /* Compound case: parent PE affects slave PEs */
595 list_for_each_entry(slave, &child->slaves, list) {
596 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
597 slave->pe_number, op);
598 if (rc != OPAL_SUCCESS) {
599 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
600 rc, desc);
601 return -ENXIO;
602 }
603 }
604
605 return 0;
606}
607
608static int pnv_ioda_set_peltv(struct pnv_phb *phb,
609 struct pnv_ioda_pe *pe,
610 bool is_add)
611{
612 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800613 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100614 int ret;
615
616 /*
617 * Clear PE frozen state. If it's master PE, we need
618 * clear slave PE frozen state as well.
619 */
620 if (is_add) {
621 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
622 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
623 if (pe->flags & PNV_IODA_PE_MASTER) {
624 list_for_each_entry(slave, &pe->slaves, list)
625 opal_pci_eeh_freeze_clear(phb->opal_id,
626 slave->pe_number,
627 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
628 }
629 }
630
631 /*
632 * Associate PE in PELT. We need add the PE into the
633 * corresponding PELT-V as well. Otherwise, the error
634 * originated from the PE might contribute to other
635 * PEs.
636 */
637 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
638 if (ret)
639 return ret;
640
641 /* For compound PEs, any one affects all of them */
642 if (pe->flags & PNV_IODA_PE_MASTER) {
643 list_for_each_entry(slave, &pe->slaves, list) {
644 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
645 if (ret)
646 return ret;
647 }
648 }
649
650 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
651 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800652 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100653 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800654#ifdef CONFIG_PCI_IOV
655 else if (pe->flags & PNV_IODA_PE_VF)
656 pdev = pe->parent_dev->bus->self;
657#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100658 while (pdev) {
659 struct pci_dn *pdn = pci_get_pdn(pdev);
660 struct pnv_ioda_pe *parent;
661
662 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
663 parent = &phb->ioda.pe_array[pdn->pe_number];
664 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
665 if (ret)
666 return ret;
667 }
668
669 pdev = pdev->bus->self;
670 }
671
672 return 0;
673}
674
Wei Yang781a8682015-03-25 16:23:57 +0800675#ifdef CONFIG_PCI_IOV
676static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
677{
678 struct pci_dev *parent;
679 uint8_t bcomp, dcomp, fcomp;
680 int64_t rc;
681 long rid_end, rid;
682
683 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
684 if (pe->pbus) {
685 int count;
686
687 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
688 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
689 parent = pe->pbus->self;
690 if (pe->flags & PNV_IODA_PE_BUS_ALL)
691 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
692 else
693 count = 1;
694
695 switch(count) {
696 case 1: bcomp = OpalPciBusAll; break;
697 case 2: bcomp = OpalPciBus7Bits; break;
698 case 4: bcomp = OpalPciBus6Bits; break;
699 case 8: bcomp = OpalPciBus5Bits; break;
700 case 16: bcomp = OpalPciBus4Bits; break;
701 case 32: bcomp = OpalPciBus3Bits; break;
702 default:
703 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
704 count);
705 /* Do an exact match only */
706 bcomp = OpalPciBusAll;
707 }
708 rid_end = pe->rid + (count << 8);
709 } else {
710 if (pe->flags & PNV_IODA_PE_VF)
711 parent = pe->parent_dev;
712 else
713 parent = pe->pdev->bus->self;
714 bcomp = OpalPciBusAll;
715 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
716 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
717 rid_end = pe->rid + 1;
718 }
719
720 /* Clear the reverse map */
721 for (rid = pe->rid; rid < rid_end; rid++)
722 phb->ioda.pe_rmap[rid] = 0;
723
724 /* Release from all parents PELT-V */
725 while (parent) {
726 struct pci_dn *pdn = pci_get_pdn(parent);
727 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
728 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
729 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
730 /* XXX What to do in case of error ? */
731 }
732 parent = parent->bus->self;
733 }
734
735 opal_pci_eeh_freeze_set(phb->opal_id, pe->pe_number,
736 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
737
738 /* Disassociate PE in PELT */
739 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
740 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
741 if (rc)
742 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
743 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
744 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
745 if (rc)
746 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
747
748 pe->pbus = NULL;
749 pe->pdev = NULL;
750 pe->parent_dev = NULL;
751
752 return 0;
753}
754#endif /* CONFIG_PCI_IOV */
755
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800756static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000757{
758 struct pci_dev *parent;
759 uint8_t bcomp, dcomp, fcomp;
760 long rc, rid_end, rid;
761
762 /* Bus validation ? */
763 if (pe->pbus) {
764 int count;
765
766 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
767 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
768 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000769 if (pe->flags & PNV_IODA_PE_BUS_ALL)
770 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
771 else
772 count = 1;
773
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000774 switch(count) {
775 case 1: bcomp = OpalPciBusAll; break;
776 case 2: bcomp = OpalPciBus7Bits; break;
777 case 4: bcomp = OpalPciBus6Bits; break;
778 case 8: bcomp = OpalPciBus5Bits; break;
779 case 16: bcomp = OpalPciBus4Bits; break;
780 case 32: bcomp = OpalPciBus3Bits; break;
781 default:
Wei Yang781a8682015-03-25 16:23:57 +0800782 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
783 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000784 /* Do an exact match only */
785 bcomp = OpalPciBusAll;
786 }
787 rid_end = pe->rid + (count << 8);
788 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800789#ifdef CONFIG_PCI_IOV
790 if (pe->flags & PNV_IODA_PE_VF)
791 parent = pe->parent_dev;
792 else
793#endif /* CONFIG_PCI_IOV */
794 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000795 bcomp = OpalPciBusAll;
796 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
797 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
798 rid_end = pe->rid + 1;
799 }
800
Gavin Shan631ad692013-11-04 16:32:46 +0800801 /*
802 * Associate PE in PELT. We need add the PE into the
803 * corresponding PELT-V as well. Otherwise, the error
804 * originated from the PE might contribute to other
805 * PEs.
806 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000807 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
808 bcomp, dcomp, fcomp, OPAL_MAP_PE);
809 if (rc) {
810 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
811 return -ENXIO;
812 }
Gavin Shan631ad692013-11-04 16:32:46 +0800813
Gavin Shanb131a842014-11-12 13:36:08 +1100814 /* Configure PELTV */
815 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000816
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000817 /* Setup reverse map */
818 for (rid = pe->rid; rid < rid_end; rid++)
819 phb->ioda.pe_rmap[rid] = pe->pe_number;
820
821 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100822 if (phb->type != PNV_PHB_IODA1) {
823 pe->mve_number = 0;
824 goto out;
825 }
826
827 pe->mve_number = pe->pe_number;
828 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
829 if (rc != OPAL_SUCCESS) {
830 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
831 rc, pe->mve_number);
832 pe->mve_number = -1;
833 } else {
834 rc = opal_pci_set_mve_enable(phb->opal_id,
835 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000836 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100837 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000838 rc, pe->mve_number);
839 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000840 }
Gavin Shan4773f762014-11-12 13:36:09 +1100841 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000842
Gavin Shan4773f762014-11-12 13:36:09 +1100843out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000844 return 0;
845}
846
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800847static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
848 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000849{
850 struct pnv_ioda_pe *lpe;
851
Gavin Shan7ebdf952012-08-20 03:49:15 +0000852 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000853 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000854 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000855 return;
856 }
857 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000858 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000859}
860
861static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
862{
863 /* This is quite simplistic. The "base" weight of a device
864 * is 10. 0 means no DMA is to be accounted for it.
865 */
866
867 /* If it's a bridge, no DMA */
868 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
869 return 0;
870
871 /* Reduce the weight of slow USB controllers */
872 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
873 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
874 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
875 return 3;
876
877 /* Increase the weight of RAID (includes Obsidian) */
878 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
879 return 15;
880
881 /* Default */
882 return 10;
883}
884
Wei Yang781a8682015-03-25 16:23:57 +0800885#ifdef CONFIG_PCI_IOV
886static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
887{
888 struct pci_dn *pdn = pci_get_pdn(dev);
889 int i;
890 struct resource *res, res2;
891 resource_size_t size;
892 u16 num_vfs;
893
894 if (!dev->is_physfn)
895 return -EINVAL;
896
897 /*
898 * "offset" is in VFs. The M64 windows are sized so that when they
899 * are segmented, each segment is the same size as the IOV BAR.
900 * Each segment is in a separate PE, and the high order bits of the
901 * address are the PE number. Therefore, each VF's BAR is in a
902 * separate PE, and changing the IOV BAR start address changes the
903 * range of PEs the VFs are in.
904 */
905 num_vfs = pdn->num_vfs;
906 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
907 res = &dev->resource[i + PCI_IOV_RESOURCES];
908 if (!res->flags || !res->parent)
909 continue;
910
911 if (!pnv_pci_is_mem_pref_64(res->flags))
912 continue;
913
914 /*
915 * The actual IOV BAR range is determined by the start address
916 * and the actual size for num_vfs VFs BAR. This check is to
917 * make sure that after shifting, the range will not overlap
918 * with another device.
919 */
920 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
921 res2.flags = res->flags;
922 res2.start = res->start + (size * offset);
923 res2.end = res2.start + (size * num_vfs) - 1;
924
925 if (res2.end > res->end) {
926 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
927 i, &res2, res, num_vfs, offset);
928 return -EBUSY;
929 }
930 }
931
932 /*
933 * After doing so, there would be a "hole" in the /proc/iomem when
934 * offset is a positive value. It looks like the device return some
935 * mmio back to the system, which actually no one could use it.
936 */
937 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
938 res = &dev->resource[i + PCI_IOV_RESOURCES];
939 if (!res->flags || !res->parent)
940 continue;
941
942 if (!pnv_pci_is_mem_pref_64(res->flags))
943 continue;
944
945 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
946 res2 = *res;
947 res->start += size * offset;
948
949 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (enabling %d VFs shifted by %d)\n",
950 i, &res2, res, num_vfs, offset);
951 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
952 }
953 return 0;
954}
955#endif /* CONFIG_PCI_IOV */
956
Gavin Shanfb446ad2012-08-20 03:49:14 +0000957#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800958static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000959{
960 struct pci_controller *hose = pci_bus_to_host(dev->bus);
961 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000962 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000963 struct pnv_ioda_pe *pe;
964 int pe_num;
965
966 if (!pdn) {
967 pr_err("%s: Device tree node not associated properly\n",
968 pci_name(dev));
969 return NULL;
970 }
971 if (pdn->pe_number != IODA_INVALID_PE)
972 return NULL;
973
974 /* PE#0 has been pre-set */
975 if (dev->bus->number == 0)
976 pe_num = 0;
977 else
978 pe_num = pnv_ioda_alloc_pe(phb);
979 if (pe_num == IODA_INVALID_PE) {
980 pr_warning("%s: Not enough PE# available, disabling device\n",
981 pci_name(dev));
982 return NULL;
983 }
984
985 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
986 * pointer in the PE data structure, both should be destroyed at the
987 * same time. However, this needs to be looked at more closely again
988 * once we actually start removing things (Hotplug, SR-IOV, ...)
989 *
990 * At some point we want to remove the PDN completely anyways
991 */
992 pe = &phb->ioda.pe_array[pe_num];
993 pci_dev_get(dev);
994 pdn->pcidev = dev;
995 pdn->pe_number = pe_num;
996 pe->pdev = dev;
997 pe->pbus = NULL;
998 pe->tce32_seg = -1;
999 pe->mve_number = -1;
1000 pe->rid = dev->bus->number << 8 | pdn->devfn;
1001
1002 pe_info(pe, "Associated device to PE\n");
1003
1004 if (pnv_ioda_configure_pe(phb, pe)) {
1005 /* XXX What do we do here ? */
1006 if (pe_num)
1007 pnv_ioda_free_pe(phb, pe_num);
1008 pdn->pe_number = IODA_INVALID_PE;
1009 pe->pdev = NULL;
1010 pci_dev_put(dev);
1011 return NULL;
1012 }
1013
1014 /* Assign a DMA weight to the device */
1015 pe->dma_weight = pnv_ioda_dma_weight(dev);
1016 if (pe->dma_weight != 0) {
1017 phb->ioda.dma_weight += pe->dma_weight;
1018 phb->ioda.dma_pe_count++;
1019 }
1020
1021 /* Link the PE */
1022 pnv_ioda_link_pe_by_weight(phb, pe);
1023
1024 return pe;
1025}
Gavin Shanfb446ad2012-08-20 03:49:14 +00001026#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001027
1028static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1029{
1030 struct pci_dev *dev;
1031
1032 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001033 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001034
1035 if (pdn == NULL) {
1036 pr_warn("%s: No device node associated with device !\n",
1037 pci_name(dev));
1038 continue;
1039 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001040 pdn->pe_number = pe->pe_number;
1041 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001042 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001043 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1044 }
1045}
1046
Gavin Shanfb446ad2012-08-20 03:49:14 +00001047/*
1048 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1049 * single PCI bus. Another one that contains the primary PCI bus and its
1050 * subordinate PCI devices and buses. The second type of PE is normally
1051 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1052 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001053static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001054{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001055 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001056 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001057 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +10001058 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001059
Guo Chao262af552014-07-21 14:42:30 +10001060 /* Check if PE is determined by M64 */
1061 if (phb->pick_m64_pe)
1062 pe_num = phb->pick_m64_pe(phb, bus, all);
1063
1064 /* The PE number isn't pinned by M64 */
1065 if (pe_num == IODA_INVALID_PE)
1066 pe_num = pnv_ioda_alloc_pe(phb);
1067
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001068 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001069 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1070 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001071 return;
1072 }
1073
1074 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +10001075 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001076 pe->pbus = bus;
1077 pe->pdev = NULL;
1078 pe->tce32_seg = -1;
1079 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001080 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001081 pe->dma_weight = 0;
1082
Gavin Shanfb446ad2012-08-20 03:49:14 +00001083 if (all)
1084 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
1085 bus->busn_res.start, bus->busn_res.end, pe_num);
1086 else
1087 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
1088 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001089
1090 if (pnv_ioda_configure_pe(phb, pe)) {
1091 /* XXX What do we do here ? */
1092 if (pe_num)
1093 pnv_ioda_free_pe(phb, pe_num);
1094 pe->pbus = NULL;
1095 return;
1096 }
1097
1098 /* Associate it with all child devices */
1099 pnv_ioda_setup_same_PE(bus, pe);
1100
Gavin Shan7ebdf952012-08-20 03:49:15 +00001101 /* Put PE to the list */
1102 list_add_tail(&pe->list, &phb->ioda.pe_list);
1103
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001104 /* Account for one DMA PE if at least one DMA capable device exist
1105 * below the bridge
1106 */
1107 if (pe->dma_weight != 0) {
1108 phb->ioda.dma_weight += pe->dma_weight;
1109 phb->ioda.dma_pe_count++;
1110 }
1111
1112 /* Link the PE */
1113 pnv_ioda_link_pe_by_weight(phb, pe);
1114}
1115
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001116static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001117{
1118 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001119
1120 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001121
1122 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001123 if (dev->subordinate) {
1124 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
1125 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
1126 else
1127 pnv_ioda_setup_PEs(dev->subordinate);
1128 }
1129 }
1130}
1131
1132/*
1133 * Configure PEs so that the downstream PCI buses and devices
1134 * could have their associated PE#. Unfortunately, we didn't
1135 * figure out the way to identify the PLX bridge yet. So we
1136 * simply put the PCI bus and the subordinate behind the root
1137 * port to PE# here. The game rule here is expected to be changed
1138 * as soon as we can detected PLX bridge correctly.
1139 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001140static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001141{
1142 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001143 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001144
1145 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001146 phb = hose->private_data;
1147
1148 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +11001149 if (phb->reserve_m64_pe)
1150 phb->reserve_m64_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +10001151
Gavin Shanfb446ad2012-08-20 03:49:14 +00001152 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001153 }
1154}
1155
Gavin Shana8b2f822015-03-25 16:23:52 +08001156#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +08001157static int pnv_pci_vf_release_m64(struct pci_dev *pdev)
1158{
1159 struct pci_bus *bus;
1160 struct pci_controller *hose;
1161 struct pnv_phb *phb;
1162 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001163 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001164
1165 bus = pdev->bus;
1166 hose = pci_bus_to_host(bus);
1167 phb = hose->private_data;
1168 pdn = pci_get_pdn(pdev);
1169
Wei Yang02639b02015-03-25 16:23:59 +08001170 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
1171 for (j = 0; j < M64_PER_IOV; j++) {
1172 if (pdn->m64_wins[i][j] == IODA_INVALID_M64)
1173 continue;
1174 opal_pci_phb_mmio_enable(phb->opal_id,
1175 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 0);
1176 clear_bit(pdn->m64_wins[i][j], &phb->ioda.m64_bar_alloc);
1177 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1178 }
Wei Yang781a8682015-03-25 16:23:57 +08001179
1180 return 0;
1181}
1182
Wei Yang02639b02015-03-25 16:23:59 +08001183static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001184{
1185 struct pci_bus *bus;
1186 struct pci_controller *hose;
1187 struct pnv_phb *phb;
1188 struct pci_dn *pdn;
1189 unsigned int win;
1190 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001191 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001192 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001193 int total_vfs;
1194 resource_size_t size, start;
1195 int pe_num;
1196 int vf_groups;
1197 int vf_per_group;
Wei Yang781a8682015-03-25 16:23:57 +08001198
1199 bus = pdev->bus;
1200 hose = pci_bus_to_host(bus);
1201 phb = hose->private_data;
1202 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001203 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001204
1205 /* Initialize the m64_wins to IODA_INVALID_M64 */
1206 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yang02639b02015-03-25 16:23:59 +08001207 for (j = 0; j < M64_PER_IOV; j++)
1208 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1209
1210 if (pdn->m64_per_iov == M64_PER_IOV) {
1211 vf_groups = (num_vfs <= M64_PER_IOV) ? num_vfs: M64_PER_IOV;
1212 vf_per_group = (num_vfs <= M64_PER_IOV)? 1:
1213 roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1214 } else {
1215 vf_groups = 1;
1216 vf_per_group = 1;
1217 }
Wei Yang781a8682015-03-25 16:23:57 +08001218
1219 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1220 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1221 if (!res->flags || !res->parent)
1222 continue;
1223
1224 if (!pnv_pci_is_mem_pref_64(res->flags))
1225 continue;
1226
Wei Yang02639b02015-03-25 16:23:59 +08001227 for (j = 0; j < vf_groups; j++) {
1228 do {
1229 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1230 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001231
Wei Yang02639b02015-03-25 16:23:59 +08001232 if (win >= phb->ioda.m64_bar_idx + 1)
1233 goto m64_failed;
1234 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001235
Wei Yang02639b02015-03-25 16:23:59 +08001236 pdn->m64_wins[i][j] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001237
Wei Yang02639b02015-03-25 16:23:59 +08001238 if (pdn->m64_per_iov == M64_PER_IOV) {
1239 size = pci_iov_resource_size(pdev,
1240 PCI_IOV_RESOURCES + i);
1241 size = size * vf_per_group;
1242 start = res->start + size * j;
1243 } else {
1244 size = resource_size(res);
1245 start = res->start;
1246 }
1247
1248 /* Map the M64 here */
1249 if (pdn->m64_per_iov == M64_PER_IOV) {
1250 pe_num = pdn->offset + j;
1251 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1252 pe_num, OPAL_M64_WINDOW_TYPE,
1253 pdn->m64_wins[i][j], 0);
1254 }
1255
1256 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001257 OPAL_M64_WINDOW_TYPE,
Wei Yang02639b02015-03-25 16:23:59 +08001258 pdn->m64_wins[i][j],
1259 start,
Wei Yang781a8682015-03-25 16:23:57 +08001260 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001261 size);
Wei Yang781a8682015-03-25 16:23:57 +08001262
Wei Yang02639b02015-03-25 16:23:59 +08001263
1264 if (rc != OPAL_SUCCESS) {
1265 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1266 win, rc);
1267 goto m64_failed;
1268 }
1269
1270 if (pdn->m64_per_iov == M64_PER_IOV)
1271 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1272 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 2);
1273 else
1274 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1275 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 1);
1276
1277 if (rc != OPAL_SUCCESS) {
1278 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1279 win, rc);
1280 goto m64_failed;
1281 }
Wei Yang781a8682015-03-25 16:23:57 +08001282 }
1283 }
1284 return 0;
1285
1286m64_failed:
1287 pnv_pci_vf_release_m64(pdev);
1288 return -EBUSY;
1289}
1290
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001291static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1292 int num);
1293static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1294
Wei Yang781a8682015-03-25 16:23:57 +08001295static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1296{
Wei Yang781a8682015-03-25 16:23:57 +08001297 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001298 int64_t rc;
1299
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001300 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001301 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001302 if (rc)
1303 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1304
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001305 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001306 if (pe->table_group.group) {
1307 iommu_group_put(pe->table_group.group);
1308 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001309 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001310 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001311 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001312}
1313
Wei Yang02639b02015-03-25 16:23:59 +08001314static void pnv_ioda_release_vf_PE(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001315{
1316 struct pci_bus *bus;
1317 struct pci_controller *hose;
1318 struct pnv_phb *phb;
1319 struct pnv_ioda_pe *pe, *pe_n;
1320 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001321 u16 vf_index;
1322 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001323
1324 bus = pdev->bus;
1325 hose = pci_bus_to_host(bus);
1326 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001327 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001328
1329 if (!pdev->is_physfn)
1330 return;
1331
Wei Yang02639b02015-03-25 16:23:59 +08001332 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1333 int vf_group;
1334 int vf_per_group;
1335 int vf_index1;
1336
1337 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1338
1339 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++)
1340 for (vf_index = vf_group * vf_per_group;
1341 vf_index < (vf_group + 1) * vf_per_group &&
1342 vf_index < num_vfs;
1343 vf_index++)
1344 for (vf_index1 = vf_group * vf_per_group;
1345 vf_index1 < (vf_group + 1) * vf_per_group &&
1346 vf_index1 < num_vfs;
1347 vf_index1++){
1348
1349 rc = opal_pci_set_peltv(phb->opal_id,
1350 pdn->offset + vf_index,
1351 pdn->offset + vf_index1,
1352 OPAL_REMOVE_PE_FROM_DOMAIN);
1353
1354 if (rc)
1355 dev_warn(&pdev->dev, "%s: Failed to unlink same group PE#%d(%lld)\n",
1356 __func__,
1357 pdn->offset + vf_index1, rc);
1358 }
1359 }
1360
Wei Yang781a8682015-03-25 16:23:57 +08001361 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1362 if (pe->parent_dev != pdev)
1363 continue;
1364
1365 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1366
1367 /* Remove from list */
1368 mutex_lock(&phb->ioda.pe_list_mutex);
1369 list_del(&pe->list);
1370 mutex_unlock(&phb->ioda.pe_list_mutex);
1371
1372 pnv_ioda_deconfigure_pe(phb, pe);
1373
1374 pnv_ioda_free_pe(phb, pe->pe_number);
1375 }
1376}
1377
1378void pnv_pci_sriov_disable(struct pci_dev *pdev)
1379{
1380 struct pci_bus *bus;
1381 struct pci_controller *hose;
1382 struct pnv_phb *phb;
1383 struct pci_dn *pdn;
1384 struct pci_sriov *iov;
1385 u16 num_vfs;
1386
1387 bus = pdev->bus;
1388 hose = pci_bus_to_host(bus);
1389 phb = hose->private_data;
1390 pdn = pci_get_pdn(pdev);
1391 iov = pdev->sriov;
1392 num_vfs = pdn->num_vfs;
1393
1394 /* Release VF PEs */
Wei Yang02639b02015-03-25 16:23:59 +08001395 pnv_ioda_release_vf_PE(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001396
1397 if (phb->type == PNV_PHB_IODA2) {
Wei Yang02639b02015-03-25 16:23:59 +08001398 if (pdn->m64_per_iov == 1)
1399 pnv_pci_vf_resource_shift(pdev, -pdn->offset);
Wei Yang781a8682015-03-25 16:23:57 +08001400
1401 /* Release M64 windows */
1402 pnv_pci_vf_release_m64(pdev);
1403
1404 /* Release PE numbers */
1405 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1406 pdn->offset = 0;
1407 }
1408}
1409
1410static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1411 struct pnv_ioda_pe *pe);
1412static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1413{
1414 struct pci_bus *bus;
1415 struct pci_controller *hose;
1416 struct pnv_phb *phb;
1417 struct pnv_ioda_pe *pe;
1418 int pe_num;
1419 u16 vf_index;
1420 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001421 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001422
1423 bus = pdev->bus;
1424 hose = pci_bus_to_host(bus);
1425 phb = hose->private_data;
1426 pdn = pci_get_pdn(pdev);
1427
1428 if (!pdev->is_physfn)
1429 return;
1430
1431 /* Reserve PE for each VF */
1432 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
1433 pe_num = pdn->offset + vf_index;
1434
1435 pe = &phb->ioda.pe_array[pe_num];
1436 pe->pe_number = pe_num;
1437 pe->phb = phb;
1438 pe->flags = PNV_IODA_PE_VF;
1439 pe->pbus = NULL;
1440 pe->parent_dev = pdev;
1441 pe->tce32_seg = -1;
1442 pe->mve_number = -1;
1443 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1444 pci_iov_virtfn_devfn(pdev, vf_index);
1445
1446 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1447 hose->global_number, pdev->bus->number,
1448 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1449 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1450
1451 if (pnv_ioda_configure_pe(phb, pe)) {
1452 /* XXX What do we do here ? */
1453 if (pe_num)
1454 pnv_ioda_free_pe(phb, pe_num);
1455 pe->pdev = NULL;
1456 continue;
1457 }
1458
Wei Yang781a8682015-03-25 16:23:57 +08001459 /* Put PE to the list */
1460 mutex_lock(&phb->ioda.pe_list_mutex);
1461 list_add_tail(&pe->list, &phb->ioda.pe_list);
1462 mutex_unlock(&phb->ioda.pe_list_mutex);
1463
1464 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1465 }
Wei Yang02639b02015-03-25 16:23:59 +08001466
1467 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1468 int vf_group;
1469 int vf_per_group;
1470 int vf_index1;
1471
1472 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1473
1474 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++) {
1475 for (vf_index = vf_group * vf_per_group;
1476 vf_index < (vf_group + 1) * vf_per_group &&
1477 vf_index < num_vfs;
1478 vf_index++) {
1479 for (vf_index1 = vf_group * vf_per_group;
1480 vf_index1 < (vf_group + 1) * vf_per_group &&
1481 vf_index1 < num_vfs;
1482 vf_index1++) {
1483
1484 rc = opal_pci_set_peltv(phb->opal_id,
1485 pdn->offset + vf_index,
1486 pdn->offset + vf_index1,
1487 OPAL_ADD_PE_TO_DOMAIN);
1488
1489 if (rc)
1490 dev_warn(&pdev->dev, "%s: Failed to link same group PE#%d(%lld)\n",
1491 __func__,
1492 pdn->offset + vf_index1, rc);
1493 }
1494 }
1495 }
1496 }
Wei Yang781a8682015-03-25 16:23:57 +08001497}
1498
1499int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1500{
1501 struct pci_bus *bus;
1502 struct pci_controller *hose;
1503 struct pnv_phb *phb;
1504 struct pci_dn *pdn;
1505 int ret;
1506
1507 bus = pdev->bus;
1508 hose = pci_bus_to_host(bus);
1509 phb = hose->private_data;
1510 pdn = pci_get_pdn(pdev);
1511
1512 if (phb->type == PNV_PHB_IODA2) {
1513 /* Calculate available PE for required VFs */
1514 mutex_lock(&phb->ioda.pe_alloc_mutex);
1515 pdn->offset = bitmap_find_next_zero_area(
1516 phb->ioda.pe_alloc, phb->ioda.total_pe,
1517 0, num_vfs, 0);
1518 if (pdn->offset >= phb->ioda.total_pe) {
1519 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1520 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1521 pdn->offset = 0;
1522 return -EBUSY;
1523 }
1524 bitmap_set(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1525 pdn->num_vfs = num_vfs;
1526 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1527
1528 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001529 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001530 if (ret) {
1531 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1532 goto m64_failed;
1533 }
1534
1535 /*
1536 * When using one M64 BAR to map one IOV BAR, we need to shift
1537 * the IOV BAR according to the PE# allocated to the VFs.
1538 * Otherwise, the PE# for the VF will conflict with others.
1539 */
Wei Yang02639b02015-03-25 16:23:59 +08001540 if (pdn->m64_per_iov == 1) {
1541 ret = pnv_pci_vf_resource_shift(pdev, pdn->offset);
1542 if (ret)
1543 goto m64_failed;
1544 }
Wei Yang781a8682015-03-25 16:23:57 +08001545 }
1546
1547 /* Setup VF PEs */
1548 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1549
1550 return 0;
1551
1552m64_failed:
1553 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1554 pdn->offset = 0;
1555
1556 return ret;
1557}
1558
Gavin Shana8b2f822015-03-25 16:23:52 +08001559int pcibios_sriov_disable(struct pci_dev *pdev)
1560{
Wei Yang781a8682015-03-25 16:23:57 +08001561 pnv_pci_sriov_disable(pdev);
1562
Gavin Shana8b2f822015-03-25 16:23:52 +08001563 /* Release PCI data */
1564 remove_dev_pci_data(pdev);
1565 return 0;
1566}
1567
1568int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1569{
1570 /* Allocate PCI data */
1571 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001572
1573 pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001574 return 0;
1575}
1576#endif /* CONFIG_PCI_IOV */
1577
Gavin Shan959c9bd2013-04-25 19:21:02 +00001578static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001579{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001580 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001581 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001582
Gavin Shan959c9bd2013-04-25 19:21:02 +00001583 /*
1584 * The function can be called while the PE#
1585 * hasn't been assigned. Do nothing for the
1586 * case.
1587 */
1588 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1589 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001590
Gavin Shan959c9bd2013-04-25 19:21:02 +00001591 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001592 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001593 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001594 /*
1595 * Note: iommu_add_device() will fail here as
1596 * for physical PE: the device is already added by now;
1597 * for virtual PE: sysfs entries are not ready yet and
1598 * tce_iommu_bus_notifier will add the device to a group later.
1599 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001600}
1601
Daniel Axtens763d2d82015-04-28 15:12:07 +10001602static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001603{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001604 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1605 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001606 struct pci_dn *pdn = pci_get_pdn(pdev);
1607 struct pnv_ioda_pe *pe;
1608 uint64_t top;
1609 bool bypass = false;
1610
1611 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1612 return -ENODEV;;
1613
1614 pe = &phb->ioda.pe_array[pdn->pe_number];
1615 if (pe->tce_bypass_enabled) {
1616 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1617 bypass = (dma_mask >= top);
1618 }
1619
1620 if (bypass) {
1621 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1622 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001623 } else {
1624 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1625 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001626 }
Brian W Harta32305b2014-07-31 14:24:37 -05001627 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001628 return 0;
1629}
1630
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001631static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
1632 struct pci_dev *pdev)
1633{
1634 struct pci_dn *pdn = pci_get_pdn(pdev);
1635 struct pnv_ioda_pe *pe;
1636 u64 end, mask;
1637
1638 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1639 return 0;
1640
1641 pe = &phb->ioda.pe_array[pdn->pe_number];
1642 if (!pe->tce_bypass_enabled)
1643 return __dma_get_required_mask(&pdev->dev);
1644
1645
1646 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1647 mask = 1ULL << (fls64(end) - 1);
1648 mask += mask - 1;
1649
1650 return mask;
1651}
1652
Gavin Shandff4a392014-07-15 17:00:55 +10001653static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001654 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001655{
1656 struct pci_dev *dev;
1657
1658 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001659 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001660 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001661 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001662
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001663 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001664 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001665 }
1666}
1667
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001668static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
1669 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001670{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001671 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1672 &tbl->it_group_list, struct iommu_table_group_link,
1673 next);
1674 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001675 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001676 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001677 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1678 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001679 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001680 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001681
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001682 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1683 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1684 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001685
1686 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1687 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001688 start <<= shift;
1689 end <<= shift;
1690 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001691 start |= tbl->it_busno;
1692 end |= tbl->it_busno;
1693 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1694 /* p7ioc-style invalidation, 2 TCEs per write */
1695 start |= (1ull << 63);
1696 end |= (1ull << 63);
1697 inc = 16;
1698 } else {
1699 /* Default (older HW) */
1700 inc = 128;
1701 }
1702
1703 end |= inc - 1; /* round up end to be different than start */
1704
1705 mb(); /* Ensure above stores are visible */
1706 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001707 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001708 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001709 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001710 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001711 start += inc;
1712 }
1713
1714 /*
1715 * The iommu layer will do another mb() for us on build()
1716 * and we don't care on free()
1717 */
1718}
1719
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001720static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1721 long npages, unsigned long uaddr,
1722 enum dma_data_direction direction,
1723 struct dma_attrs *attrs)
1724{
1725 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1726 attrs);
1727
1728 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1729 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1730
1731 return ret;
1732}
1733
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001734#ifdef CONFIG_IOMMU_API
1735static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1736 unsigned long *hpa, enum dma_data_direction *direction)
1737{
1738 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1739
1740 if (!ret && (tbl->it_type &
1741 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1742 pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);
1743
1744 return ret;
1745}
1746#endif
1747
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001748static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1749 long npages)
1750{
1751 pnv_tce_free(tbl, index, npages);
1752
1753 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1754 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1755}
1756
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001757static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001758 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001759#ifdef CONFIG_IOMMU_API
1760 .exchange = pnv_ioda1_tce_xchg,
1761#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001762 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001763 .get = pnv_tce_get,
1764};
1765
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001766static inline void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_ioda_pe *pe)
1767{
1768 /* 01xb - invalidate TCEs that match the specified PE# */
1769 unsigned long val = (0x4ull << 60) | (pe->pe_number & 0xFF);
1770 struct pnv_phb *phb = pe->phb;
1771
1772 if (!phb->ioda.tce_inval_reg)
1773 return;
1774
1775 mb(); /* Ensure above stores are visible */
1776 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1777}
1778
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001779static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
1780 __be64 __iomem *invalidate, unsigned shift,
1781 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001782{
1783 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001784
1785 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001786 start = 0x2ull << 60;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001787 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001788 end = start;
1789
1790 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001791 start |= (index << shift);
1792 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001793 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001794 mb();
1795
1796 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001797 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001798 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001799 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001800 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001801 start += inc;
1802 }
1803}
1804
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001805static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1806 unsigned long index, unsigned long npages, bool rm)
1807{
1808 struct iommu_table_group_link *tgl;
1809
1810 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
1811 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1812 struct pnv_ioda_pe, table_group);
1813 __be64 __iomem *invalidate = rm ?
1814 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1815 pe->phb->ioda.tce_inval_reg;
1816
1817 pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
1818 invalidate, tbl->it_page_shift,
1819 index, npages);
1820 }
1821}
1822
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001823static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1824 long npages, unsigned long uaddr,
1825 enum dma_data_direction direction,
1826 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001827{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001828 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1829 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001830
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001831 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1832 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1833
1834 return ret;
1835}
1836
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001837#ifdef CONFIG_IOMMU_API
1838static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1839 unsigned long *hpa, enum dma_data_direction *direction)
1840{
1841 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1842
1843 if (!ret && (tbl->it_type &
1844 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1845 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1846
1847 return ret;
1848}
1849#endif
1850
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001851static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1852 long npages)
1853{
1854 pnv_tce_free(tbl, index, npages);
1855
1856 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1857 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001858}
1859
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001860static void pnv_ioda2_table_free(struct iommu_table *tbl)
1861{
1862 pnv_pci_ioda2_table_free_pages(tbl);
1863 iommu_free_table(tbl, "pnv");
1864}
1865
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001866static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001867 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001868#ifdef CONFIG_IOMMU_API
1869 .exchange = pnv_ioda2_tce_xchg,
1870#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001871 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001872 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001873 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001874};
1875
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001876static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1877 struct pnv_ioda_pe *pe, unsigned int base,
1878 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001879{
1880
1881 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001882 struct iommu_table *tbl;
1883 unsigned int i;
1884 int64_t rc;
1885 void *addr;
1886
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001887 /* XXX FIXME: Handle 64-bit only DMA devices */
1888 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1889 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1890
1891 /* We shouldn't already have a 32-bit DMA associated */
1892 if (WARN_ON(pe->tce32_seg >= 0))
1893 return;
1894
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001895 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001896 iommu_register_group(&pe->table_group, phb->hose->global_number,
1897 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001898 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001899
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001900 /* Grab a 32-bit TCE table */
1901 pe->tce32_seg = base;
1902 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1903 (base << 28), ((base + segs) << 28) - 1);
1904
1905 /* XXX Currently, we allocate one big contiguous table for the
1906 * TCEs. We only really need one chunk per 256M of TCE space
1907 * (ie per segment) but that's an optimization for later, it
1908 * requires some added smarts with our get/put_tce implementation
1909 */
1910 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1911 get_order(TCE32_TABLE_SIZE * segs));
1912 if (!tce_mem) {
1913 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1914 goto fail;
1915 }
1916 addr = page_address(tce_mem);
1917 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1918
1919 /* Configure HW */
1920 for (i = 0; i < segs; i++) {
1921 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1922 pe->pe_number,
1923 base + i, 1,
1924 __pa(addr) + TCE32_TABLE_SIZE * i,
1925 TCE32_TABLE_SIZE, 0x1000);
1926 if (rc) {
1927 pe_err(pe, " Failed to configure 32-bit TCE table,"
1928 " err %ld\n", rc);
1929 goto fail;
1930 }
1931 }
1932
1933 /* Setup linux iommu table */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001934 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001935 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001936
1937 /* OPAL variant of P7IOC SW invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001938 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10001939 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1940 TCE_PCI_SWINV_FREE |
1941 TCE_PCI_SWINV_PAIR);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001942
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001943 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001944 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
1945 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001946 iommu_init_table(tbl, phb->hose->node);
1947
Wei Yang781a8682015-03-25 16:23:57 +08001948 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001949 /*
1950 * Setting table base here only for carrying iommu_group
1951 * further down to let iommu_add_device() do the job.
1952 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
1953 */
1954 set_iommu_table_base(&pe->pdev->dev, tbl);
1955 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001956 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001957 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001958
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001959 return;
1960 fail:
1961 /* XXX Failure: Try to fallback to 64-bit only ? */
1962 if (pe->tce32_seg >= 0)
1963 pe->tce32_seg = -1;
1964 if (tce_mem)
1965 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001966 if (tbl) {
1967 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
1968 iommu_free_table(tbl, "pnv");
1969 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001970}
1971
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001972static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
1973 int num, struct iommu_table *tbl)
1974{
1975 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1976 table_group);
1977 struct pnv_phb *phb = pe->phb;
1978 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001979 const unsigned long size = tbl->it_indirect_levels ?
1980 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001981 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
1982 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
1983
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001984 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001985 start_addr, start_addr + win_size - 1,
1986 IOMMU_PAGE_SIZE(tbl));
1987
1988 /*
1989 * Map TCE table through TVT. The TVE index is the PE number
1990 * shifted by 1 bit for 32-bits DMA space.
1991 */
1992 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1993 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001994 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001995 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001996 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001997 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001998 IOMMU_PAGE_SIZE(tbl));
1999 if (rc) {
2000 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
2001 return rc;
2002 }
2003
2004 pnv_pci_link_table_and_group(phb->hose->node, num,
2005 tbl, &pe->table_group);
2006 pnv_pci_ioda2_tce_invalidate_entire(pe);
2007
2008 return 0;
2009}
2010
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002011static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002012{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002013 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2014 int64_t rc;
2015
2016 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2017 if (enable) {
2018 phys_addr_t top = memblock_end_of_DRAM();
2019
2020 top = roundup_pow_of_two(top);
2021 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2022 pe->pe_number,
2023 window_id,
2024 pe->tce_bypass_base,
2025 top);
2026 } else {
2027 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2028 pe->pe_number,
2029 window_id,
2030 pe->tce_bypass_base,
2031 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002032 }
2033 if (rc)
2034 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2035 else
2036 pe->tce_bypass_enabled = enable;
2037}
2038
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002039static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2040 __u32 page_shift, __u64 window_size, __u32 levels,
2041 struct iommu_table *tbl);
2042
2043static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2044 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2045 struct iommu_table **ptbl)
2046{
2047 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2048 table_group);
2049 int nid = pe->phb->hose->node;
2050 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2051 long ret;
2052 struct iommu_table *tbl;
2053
2054 tbl = pnv_pci_table_alloc(nid);
2055 if (!tbl)
2056 return -ENOMEM;
2057
2058 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2059 bus_offset, page_shift, window_size,
2060 levels, tbl);
2061 if (ret) {
2062 iommu_free_table(tbl, "pnv");
2063 return ret;
2064 }
2065
2066 tbl->it_ops = &pnv_ioda2_iommu_ops;
2067 if (pe->phb->ioda.tce_inval_reg)
2068 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2069
2070 *ptbl = tbl;
2071
2072 return 0;
2073}
2074
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002075static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2076{
2077 struct iommu_table *tbl = NULL;
2078 long rc;
2079
2080 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2081 IOMMU_PAGE_SHIFT_4K,
2082 pe->table_group.tce32_size,
2083 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2084 if (rc) {
2085 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2086 rc);
2087 return rc;
2088 }
2089
2090 iommu_init_table(tbl, pe->phb->hose->node);
2091
2092 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2093 if (rc) {
2094 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2095 rc);
2096 pnv_ioda2_table_free(tbl);
2097 return rc;
2098 }
2099
2100 if (!pnv_iommu_bypass_disabled)
2101 pnv_pci_ioda2_set_bypass(pe, true);
2102
2103 /* OPAL variant of PHB3 invalidated TCEs */
2104 if (pe->phb->ioda.tce_inval_reg)
2105 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2106
2107 /*
2108 * Setting table base here only for carrying iommu_group
2109 * further down to let iommu_add_device() do the job.
2110 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2111 */
2112 if (pe->flags & PNV_IODA_PE_DEV)
2113 set_iommu_table_base(&pe->pdev->dev, tbl);
2114
2115 return 0;
2116}
2117
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002118#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2119static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2120 int num)
2121{
2122 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2123 table_group);
2124 struct pnv_phb *phb = pe->phb;
2125 long ret;
2126
2127 pe_info(pe, "Removing DMA window #%d\n", num);
2128
2129 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2130 (pe->pe_number << 1) + num,
2131 0/* levels */, 0/* table address */,
2132 0/* table size */, 0/* page size */);
2133 if (ret)
2134 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2135 else
2136 pnv_pci_ioda2_tce_invalidate_entire(pe);
2137
2138 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2139
2140 return ret;
2141}
2142#endif
2143
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002144#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002145static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2146 __u64 window_size, __u32 levels)
2147{
2148 unsigned long bytes = 0;
2149 const unsigned window_shift = ilog2(window_size);
2150 unsigned entries_shift = window_shift - page_shift;
2151 unsigned table_shift = entries_shift + 3;
2152 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2153 unsigned long direct_table_size;
2154
2155 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2156 (window_size > memory_hotplug_max()) ||
2157 !is_power_of_2(window_size))
2158 return 0;
2159
2160 /* Calculate a direct table size from window_size and levels */
2161 entries_shift = (entries_shift + levels - 1) / levels;
2162 table_shift = entries_shift + 3;
2163 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2164 direct_table_size = 1UL << table_shift;
2165
2166 for ( ; levels; --levels) {
2167 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2168
2169 tce_table_size /= direct_table_size;
2170 tce_table_size <<= 3;
2171 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2172 }
2173
2174 return bytes;
2175}
2176
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002177static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002178{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002179 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2180 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002181 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2182 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002183
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002184 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002185 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2186 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002187}
2188
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002189static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2190{
2191 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2192 table_group);
2193
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002194 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002195}
2196
2197static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002198 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002199 .create_table = pnv_pci_ioda2_create_table,
2200 .set_window = pnv_pci_ioda2_set_window,
2201 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002202 .take_ownership = pnv_ioda2_take_ownership,
2203 .release_ownership = pnv_ioda2_release_ownership,
2204};
2205#endif
2206
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002207static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2208{
2209 const __be64 *swinvp;
2210
2211 /* OPAL variant of PHB3 invalidated TCEs */
2212 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2213 if (!swinvp)
2214 return;
2215
2216 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2217 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2218}
2219
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002220static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2221 unsigned levels, unsigned long limit,
2222 unsigned long *current_offset)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002223{
2224 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002225 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002226 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002227 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2228 unsigned entries = 1UL << (shift - 3);
2229 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002230
2231 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2232 if (!tce_mem) {
2233 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2234 return NULL;
2235 }
2236 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002237 memset(addr, 0, allocated);
2238
2239 --levels;
2240 if (!levels) {
2241 *current_offset += allocated;
2242 return addr;
2243 }
2244
2245 for (i = 0; i < entries; ++i) {
2246 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
2247 levels, limit, current_offset);
2248 if (!tmp)
2249 break;
2250
2251 addr[i] = cpu_to_be64(__pa(tmp) |
2252 TCE_PCI_READ | TCE_PCI_WRITE);
2253
2254 if (*current_offset >= limit)
2255 break;
2256 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002257
2258 return addr;
2259}
2260
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002261static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2262 unsigned long size, unsigned level);
2263
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002264static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002265 __u32 page_shift, __u64 window_size, __u32 levels,
2266 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002267{
2268 void *addr;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002269 unsigned long offset = 0, level_shift;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002270 const unsigned window_shift = ilog2(window_size);
2271 unsigned entries_shift = window_shift - page_shift;
2272 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2273 const unsigned long tce_table_size = 1UL << table_shift;
2274
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002275 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2276 return -EINVAL;
2277
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002278 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2279 return -EINVAL;
2280
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002281 /* Adjust direct table size from window_size and levels */
2282 entries_shift = (entries_shift + levels - 1) / levels;
2283 level_shift = entries_shift + 3;
2284 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2285
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002286 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002287 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
2288 levels, tce_table_size, &offset);
2289
2290 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002291 if (!addr)
2292 return -ENOMEM;
2293
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002294 /*
2295 * First level was allocated but some lower level failed as
2296 * we did not allocate as much as we wanted,
2297 * release partially allocated table.
2298 */
2299 if (offset < tce_table_size) {
2300 pnv_pci_ioda2_table_do_free_pages(addr,
2301 1ULL << (level_shift - 3), levels - 1);
2302 return -ENOMEM;
2303 }
2304
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002305 /* Setup linux iommu table */
2306 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2307 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002308 tbl->it_level_size = 1ULL << (level_shift - 3);
2309 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002310 tbl->it_allocated_size = offset;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002311
2312 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2313 window_size, tce_table_size, bus_offset);
2314
2315 return 0;
2316}
2317
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002318static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2319 unsigned long size, unsigned level)
2320{
2321 const unsigned long addr_ul = (unsigned long) addr &
2322 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2323
2324 if (level) {
2325 long i;
2326 u64 *tmp = (u64 *) addr_ul;
2327
2328 for (i = 0; i < size; ++i) {
2329 unsigned long hpa = be64_to_cpu(tmp[i]);
2330
2331 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2332 continue;
2333
2334 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2335 level - 1);
2336 }
2337 }
2338
2339 free_pages(addr_ul, get_order(size << 3));
2340}
2341
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002342static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2343{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002344 const unsigned long size = tbl->it_indirect_levels ?
2345 tbl->it_level_size : tbl->it_size;
2346
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002347 if (!tbl->it_size)
2348 return;
2349
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002350 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2351 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002352}
2353
Gavin Shan373f5652013-04-25 19:21:01 +00002354static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2355 struct pnv_ioda_pe *pe)
2356{
Gavin Shan373f5652013-04-25 19:21:01 +00002357 int64_t rc;
2358
2359 /* We shouldn't already have a 32-bit DMA associated */
2360 if (WARN_ON(pe->tce32_seg >= 0))
2361 return;
2362
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002363 /* TVE #1 is selected by PCI address bit 59 */
2364 pe->tce_bypass_base = 1ull << 59;
2365
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002366 iommu_register_group(&pe->table_group, phb->hose->global_number,
2367 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002368
Gavin Shan373f5652013-04-25 19:21:01 +00002369 /* The PE will reserve all possible 32-bits space */
2370 pe->tce32_seg = 0;
Gavin Shan373f5652013-04-25 19:21:01 +00002371 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002372 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002373
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002374 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002375 pe->table_group.tce32_start = 0;
2376 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2377 pe->table_group.max_dynamic_windows_supported =
2378 IOMMU_TABLE_GROUP_MAX_TABLES;
2379 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2380 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002381#ifdef CONFIG_IOMMU_API
2382 pe->table_group.ops = &pnv_pci_ioda2_ops;
2383#endif
2384
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002385 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan373f5652013-04-25 19:21:01 +00002386 if (rc) {
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002387 if (pe->tce32_seg >= 0)
2388 pe->tce32_seg = -1;
2389 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002390 }
2391
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002392 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002393 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002394 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002395 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002396}
2397
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002398static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002399{
2400 struct pci_controller *hose = phb->hose;
2401 unsigned int residual, remaining, segs, tw, base;
2402 struct pnv_ioda_pe *pe;
2403
2404 /* If we have more PE# than segments available, hand out one
2405 * per PE until we run out and let the rest fail. If not,
2406 * then we assign at least one segment per PE, plus more based
2407 * on the amount of devices under that PE
2408 */
2409 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
2410 residual = 0;
2411 else
2412 residual = phb->ioda.tce32_count -
2413 phb->ioda.dma_pe_count;
2414
2415 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
2416 hose->global_number, phb->ioda.tce32_count);
2417 pr_info("PCI: %d PE# for a total weight of %d\n",
2418 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
2419
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002420 pnv_pci_ioda_setup_opal_tce_kill(phb);
2421
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002422 /* Walk our PE list and configure their DMA segments, hand them
2423 * out one base segment plus any residual segments based on
2424 * weight
2425 */
2426 remaining = phb->ioda.tce32_count;
2427 tw = phb->ioda.dma_weight;
2428 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00002429 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002430 if (!pe->dma_weight)
2431 continue;
2432 if (!remaining) {
2433 pe_warn(pe, "No DMA32 resources available\n");
2434 continue;
2435 }
2436 segs = 1;
2437 if (residual) {
2438 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
2439 if (segs > remaining)
2440 segs = remaining;
2441 }
Gavin Shan373f5652013-04-25 19:21:01 +00002442
2443 /*
2444 * For IODA2 compliant PHB3, we needn't care about the weight.
2445 * The all available 32-bits DMA space will be assigned to
2446 * the specific PE.
2447 */
2448 if (phb->type == PNV_PHB_IODA1) {
2449 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
2450 pe->dma_weight, segs);
2451 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
2452 } else {
2453 pe_info(pe, "Assign DMA32 space\n");
2454 segs = 0;
2455 pnv_pci_ioda2_setup_dma_pe(phb, pe);
2456 }
2457
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002458 remaining -= segs;
2459 base += segs;
2460 }
2461}
2462
2463#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002464static void pnv_ioda2_msi_eoi(struct irq_data *d)
2465{
2466 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2467 struct irq_chip *chip = irq_data_get_irq_chip(d);
2468 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2469 ioda.irq_chip);
2470 int64_t rc;
2471
2472 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2473 WARN_ON_ONCE(rc);
2474
2475 icp_native_eoi(d);
2476}
2477
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002478
2479static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2480{
2481 struct irq_data *idata;
2482 struct irq_chip *ichip;
2483
2484 if (phb->type != PNV_PHB_IODA2)
2485 return;
2486
2487 if (!phb->ioda.irq_chip_init) {
2488 /*
2489 * First time we setup an MSI IRQ, we need to setup the
2490 * corresponding IRQ chip to route correctly.
2491 */
2492 idata = irq_get_irq_data(virq);
2493 ichip = irq_data_get_irq_chip(idata);
2494 phb->ioda.irq_chip_init = 1;
2495 phb->ioda.irq_chip = *ichip;
2496 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2497 }
2498 irq_set_chip(virq, &phb->ioda.irq_chip);
2499}
2500
Ian Munsie80c49c72014-10-08 19:54:57 +11002501#ifdef CONFIG_CXL_BASE
2502
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002503struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002504{
2505 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2506
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002507 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002508}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002509EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002510
Ryan Grimm1212aa12015-01-19 11:52:50 -06002511int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002512{
2513 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2514 struct pnv_phb *phb = hose->private_data;
2515 struct pnv_ioda_pe *pe;
2516 int rc;
2517
2518 pe = pnv_ioda_get_pe(dev);
2519 if (!pe)
2520 return -ENODEV;
2521
2522 pe_info(pe, "Switching PHB to CXL\n");
2523
Ryan Grimm1212aa12015-01-19 11:52:50 -06002524 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11002525 if (rc)
2526 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2527
2528 return rc;
2529}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002530EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002531
2532/* Find PHB for cxl dev and allocate MSI hwirqs?
2533 * Returns the absolute hardware IRQ number
2534 */
2535int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2536{
2537 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2538 struct pnv_phb *phb = hose->private_data;
2539 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2540
2541 if (hwirq < 0) {
2542 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2543 return -ENOSPC;
2544 }
2545
2546 return phb->msi_base + hwirq;
2547}
2548EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2549
2550void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2551{
2552 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2553 struct pnv_phb *phb = hose->private_data;
2554
2555 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2556}
2557EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2558
2559void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2560 struct pci_dev *dev)
2561{
2562 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2563 struct pnv_phb *phb = hose->private_data;
2564 int i, hwirq;
2565
2566 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2567 if (!irqs->range[i])
2568 continue;
2569 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2570 i, irqs->offset[i],
2571 irqs->range[i]);
2572 hwirq = irqs->offset[i] - phb->msi_base;
2573 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2574 irqs->range[i]);
2575 }
2576}
2577EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2578
2579int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2580 struct pci_dev *dev, int num)
2581{
2582 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2583 struct pnv_phb *phb = hose->private_data;
2584 int i, hwirq, try;
2585
2586 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2587
2588 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2589 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2590 try = num;
2591 while (try) {
2592 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2593 if (hwirq >= 0)
2594 break;
2595 try /= 2;
2596 }
2597 if (!try)
2598 goto fail;
2599
2600 irqs->offset[i] = phb->msi_base + hwirq;
2601 irqs->range[i] = try;
2602 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2603 i, irqs->offset[i], irqs->range[i]);
2604 num -= try;
2605 }
2606 if (num)
2607 goto fail;
2608
2609 return 0;
2610fail:
2611 pnv_cxl_release_hwirq_ranges(irqs, dev);
2612 return -ENOSPC;
2613}
2614EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2615
2616int pnv_cxl_get_irq_count(struct pci_dev *dev)
2617{
2618 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2619 struct pnv_phb *phb = hose->private_data;
2620
2621 return phb->msi_bmp.irq_count;
2622}
2623EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2624
2625int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2626 unsigned int virq)
2627{
2628 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2629 struct pnv_phb *phb = hose->private_data;
2630 unsigned int xive_num = hwirq - phb->msi_base;
2631 struct pnv_ioda_pe *pe;
2632 int rc;
2633
2634 if (!(pe = pnv_ioda_get_pe(dev)))
2635 return -ENODEV;
2636
2637 /* Assign XIVE to PE */
2638 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2639 if (rc) {
2640 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2641 "hwirq 0x%x XIVE 0x%x PE\n",
2642 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2643 return -EIO;
2644 }
2645 set_msi_irq_chip(phb, virq);
2646
2647 return 0;
2648}
2649EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2650#endif
2651
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002652static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002653 unsigned int hwirq, unsigned int virq,
2654 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002655{
2656 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2657 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002658 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002659 int rc;
2660
2661 /* No PE assigned ? bail out ... no MSI for you ! */
2662 if (pe == NULL)
2663 return -ENXIO;
2664
2665 /* Check if we have an MVE */
2666 if (pe->mve_number < 0)
2667 return -ENXIO;
2668
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002669 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002670 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002671 is_64 = 0;
2672
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002673 /* Assign XIVE to PE */
2674 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2675 if (rc) {
2676 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2677 pci_name(dev), rc, xive_num);
2678 return -EIO;
2679 }
2680
2681 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002682 __be64 addr64;
2683
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002684 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2685 &addr64, &data);
2686 if (rc) {
2687 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2688 pci_name(dev), rc);
2689 return -EIO;
2690 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002691 msg->address_hi = be64_to_cpu(addr64) >> 32;
2692 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002693 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002694 __be32 addr32;
2695
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002696 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2697 &addr32, &data);
2698 if (rc) {
2699 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2700 pci_name(dev), rc);
2701 return -EIO;
2702 }
2703 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002704 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002705 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002706 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002707
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002708 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002709
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002710 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2711 " address=%x_%08x data=%x PE# %d\n",
2712 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2713 msg->address_hi, msg->address_lo, data, pe->pe_number);
2714
2715 return 0;
2716}
2717
2718static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2719{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002720 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002721 const __be32 *prop = of_get_property(phb->hose->dn,
2722 "ibm,opal-msi-ranges", NULL);
2723 if (!prop) {
2724 /* BML Fallback */
2725 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2726 }
2727 if (!prop)
2728 return;
2729
2730 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002731 count = be32_to_cpup(prop + 1);
2732 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002733 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2734 phb->hose->global_number);
2735 return;
2736 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002737
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002738 phb->msi_setup = pnv_pci_ioda_msi_setup;
2739 phb->msi32_support = 1;
2740 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002741 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002742}
2743#else
2744static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2745#endif /* CONFIG_PCI_MSI */
2746
Wei Yang6e628c72015-03-25 16:23:55 +08002747#ifdef CONFIG_PCI_IOV
2748static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2749{
2750 struct pci_controller *hose;
2751 struct pnv_phb *phb;
2752 struct resource *res;
2753 int i;
2754 resource_size_t size;
2755 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002756 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002757
2758 if (!pdev->is_physfn || pdev->is_added)
2759 return;
2760
2761 hose = pci_bus_to_host(pdev->bus);
2762 phb = hose->private_data;
2763
2764 pdn = pci_get_pdn(pdev);
2765 pdn->vfs_expanded = 0;
2766
Wei Yang5b88ec22015-03-25 16:23:58 +08002767 total_vfs = pci_sriov_get_totalvfs(pdev);
2768 pdn->m64_per_iov = 1;
2769 mul = phb->ioda.total_pe;
2770
2771 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2772 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2773 if (!res->flags || res->parent)
2774 continue;
2775 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2776 dev_warn(&pdev->dev, " non M64 VF BAR%d: %pR\n",
2777 i, res);
2778 continue;
2779 }
2780
2781 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
2782
2783 /* bigger than 64M */
2784 if (size > (1 << 26)) {
2785 dev_info(&pdev->dev, "PowerNV: VF BAR%d: %pR IOV size is bigger than 64M, roundup power2\n",
2786 i, res);
2787 pdn->m64_per_iov = M64_PER_IOV;
2788 mul = roundup_pow_of_two(total_vfs);
2789 break;
2790 }
2791 }
2792
Wei Yang6e628c72015-03-25 16:23:55 +08002793 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2794 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2795 if (!res->flags || res->parent)
2796 continue;
2797 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2798 dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
2799 i, res);
2800 continue;
2801 }
2802
2803 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
2804 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002805 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08002806 dev_dbg(&pdev->dev, " %pR\n", res);
2807 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08002808 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08002809 }
Wei Yang5b88ec22015-03-25 16:23:58 +08002810 pdn->vfs_expanded = mul;
Wei Yang6e628c72015-03-25 16:23:55 +08002811}
2812#endif /* CONFIG_PCI_IOV */
2813
Gavin Shan11685be2012-08-20 03:49:16 +00002814/*
2815 * This function is supposed to be called on basis of PE from top
2816 * to bottom style. So the the I/O or MMIO segment assigned to
2817 * parent PE could be overrided by its child PEs if necessary.
2818 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002819static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
2820 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00002821{
2822 struct pnv_phb *phb = hose->private_data;
2823 struct pci_bus_region region;
2824 struct resource *res;
2825 int i, index;
2826 int rc;
2827
2828 /*
2829 * NOTE: We only care PCI bus based PE for now. For PCI
2830 * device based PE, for example SRIOV sensitive VF should
2831 * be figured out later.
2832 */
2833 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2834
2835 pci_bus_for_each_resource(pe->pbus, res, i) {
2836 if (!res || !res->flags ||
2837 res->start > res->end)
2838 continue;
2839
2840 if (res->flags & IORESOURCE_IO) {
2841 region.start = res->start - phb->ioda.io_pci_base;
2842 region.end = res->end - phb->ioda.io_pci_base;
2843 index = region.start / phb->ioda.io_segsize;
2844
2845 while (index < phb->ioda.total_pe &&
2846 region.start <= region.end) {
2847 phb->ioda.io_segmap[index] = pe->pe_number;
2848 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2849 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2850 if (rc != OPAL_SUCCESS) {
2851 pr_err("%s: OPAL error %d when mapping IO "
2852 "segment #%d to PE#%d\n",
2853 __func__, rc, index, pe->pe_number);
2854 break;
2855 }
2856
2857 region.start += phb->ioda.io_segsize;
2858 index++;
2859 }
Gavin Shan027fa022015-03-27 11:29:00 +11002860 } else if ((res->flags & IORESOURCE_MEM) &&
2861 !pnv_pci_is_mem_pref_64(res->flags)) {
Gavin Shan11685be2012-08-20 03:49:16 +00002862 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002863 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002864 phb->ioda.m32_pci_base;
2865 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002866 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002867 phb->ioda.m32_pci_base;
2868 index = region.start / phb->ioda.m32_segsize;
2869
2870 while (index < phb->ioda.total_pe &&
2871 region.start <= region.end) {
2872 phb->ioda.m32_segmap[index] = pe->pe_number;
2873 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2874 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2875 if (rc != OPAL_SUCCESS) {
2876 pr_err("%s: OPAL error %d when mapping M32 "
2877 "segment#%d to PE#%d",
2878 __func__, rc, index, pe->pe_number);
2879 break;
2880 }
2881
2882 region.start += phb->ioda.m32_segsize;
2883 index++;
2884 }
2885 }
2886 }
2887}
2888
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002889static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00002890{
2891 struct pci_controller *tmp, *hose;
2892 struct pnv_phb *phb;
2893 struct pnv_ioda_pe *pe;
2894
2895 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2896 phb = hose->private_data;
2897 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2898 pnv_ioda_setup_pe_seg(hose, pe);
2899 }
2900 }
2901}
2902
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002903static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00002904{
2905 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00002906 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00002907
2908 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2909 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00002910
2911 /* Mark the PHB initialization done */
2912 phb = hose->private_data;
2913 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00002914 }
2915}
2916
Gavin Shan37c367f2013-06-20 18:13:25 +08002917static void pnv_pci_ioda_create_dbgfs(void)
2918{
2919#ifdef CONFIG_DEBUG_FS
2920 struct pci_controller *hose, *tmp;
2921 struct pnv_phb *phb;
2922 char name[16];
2923
2924 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2925 phb = hose->private_data;
2926
2927 sprintf(name, "PCI%04x", hose->global_number);
2928 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
2929 if (!phb->dbgfs)
2930 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
2931 __func__, hose->global_number);
2932 }
2933#endif /* CONFIG_DEBUG_FS */
2934}
2935
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002936static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00002937{
2938 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00002939 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00002940 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002941
Gavin Shan37c367f2013-06-20 18:13:25 +08002942 pnv_pci_ioda_create_dbgfs();
2943
Gavin Shane9cc17d2013-06-20 13:21:14 +08002944#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08002945 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04002946 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002947#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00002948}
2949
Gavin Shan271fd032012-09-11 16:59:47 -06002950/*
2951 * Returns the alignment for I/O or memory windows for P2P
2952 * bridges. That actually depends on how PEs are segmented.
2953 * For now, we return I/O or M32 segment size for PE sensitive
2954 * P2P bridges. Otherwise, the default values (4KiB for I/O,
2955 * 1MiB for memory) will be returned.
2956 *
2957 * The current PCI bus might be put into one PE, which was
2958 * create against the parent PCI bridge. For that case, we
2959 * needn't enlarge the alignment so that we can save some
2960 * resources.
2961 */
2962static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2963 unsigned long type)
2964{
2965 struct pci_dev *bridge;
2966 struct pci_controller *hose = pci_bus_to_host(bus);
2967 struct pnv_phb *phb = hose->private_data;
2968 int num_pci_bridges = 0;
2969
2970 bridge = bus->self;
2971 while (bridge) {
2972 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2973 num_pci_bridges++;
2974 if (num_pci_bridges >= 2)
2975 return 1;
2976 }
2977
2978 bridge = bridge->bus->self;
2979 }
2980
Guo Chao262af552014-07-21 14:42:30 +10002981 /* We fail back to M32 if M64 isn't supported */
2982 if (phb->ioda.m64_segsize &&
2983 pnv_pci_is_mem_pref_64(type))
2984 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06002985 if (type & IORESOURCE_MEM)
2986 return phb->ioda.m32_segsize;
2987
2988 return phb->ioda.io_segsize;
2989}
2990
Wei Yang5350ab32015-03-25 16:23:56 +08002991#ifdef CONFIG_PCI_IOV
2992static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
2993 int resno)
2994{
2995 struct pci_dn *pdn = pci_get_pdn(pdev);
2996 resource_size_t align, iov_align;
2997
2998 iov_align = resource_size(&pdev->resource[resno]);
2999 if (iov_align)
3000 return iov_align;
3001
3002 align = pci_iov_resource_size(pdev, resno);
3003 if (pdn->vfs_expanded)
3004 return pdn->vfs_expanded * align;
3005
3006 return align;
3007}
3008#endif /* CONFIG_PCI_IOV */
3009
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003010/* Prevent enabling devices for which we couldn't properly
3011 * assign a PE
3012 */
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003013static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003014{
Gavin Shandb1266c2012-08-20 03:49:18 +00003015 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3016 struct pnv_phb *phb = hose->private_data;
3017 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003018
Gavin Shandb1266c2012-08-20 03:49:18 +00003019 /* The function is probably called while the PEs have
3020 * not be created yet. For example, resource reassignment
3021 * during PCI probe period. We just skip the check if
3022 * PEs isn't ready.
3023 */
3024 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003025 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003026
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003027 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003028 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003029 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003030
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003031 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003032}
3033
3034static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
3035 u32 devfn)
3036{
3037 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
3038}
3039
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003040static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003041{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003042 struct pnv_phb *phb = hose->private_data;
3043
Gavin Shand1a85ee2014-09-30 12:39:05 +10003044 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003045 OPAL_ASSERT_RESET);
3046}
3047
Daniel Axtens92ae0352015-04-28 15:12:05 +10003048static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
3049 .dma_dev_setup = pnv_pci_dma_dev_setup,
3050#ifdef CONFIG_PCI_MSI
3051 .setup_msi_irqs = pnv_setup_msi_irqs,
3052 .teardown_msi_irqs = pnv_teardown_msi_irqs,
3053#endif
3054 .enable_device_hook = pnv_pci_enable_device_hook,
3055 .window_alignment = pnv_pci_window_alignment,
3056 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
Daniel Axtens763d2d82015-04-28 15:12:07 +10003057 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003058 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003059};
3060
Anton Blancharde51df2c2014-08-20 08:55:18 +10003061static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3062 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003063{
3064 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003065 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08003066 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10003067 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003068 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003069 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003070 u64 phb_id;
3071 void *aux;
3072 long rc;
3073
Gavin Shan58d714e2013-07-31 16:47:00 +08003074 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003075
3076 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3077 if (!prop64) {
3078 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3079 return;
3080 }
3081 phb_id = be64_to_cpup(prop64);
3082 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3083
Michael Ellermane39f223f2014-11-18 16:47:35 +11003084 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003085
3086 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003087 phb->hose = hose = pcibios_alloc_controller(np);
3088 if (!phb->hose) {
3089 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003090 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003091 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003092 return;
3093 }
3094
3095 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003096 prop32 = of_get_property(np, "bus-range", &len);
3097 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003098 hose->first_busno = be32_to_cpu(prop32[0]);
3099 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003100 } else {
3101 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3102 hose->first_busno = 0;
3103 hose->last_busno = 0xff;
3104 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003105 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003106 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003107 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003108 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003109 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003110
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003111 /* Detect specific models for error handling */
3112 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3113 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003114 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003115 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003116 else
3117 phb->model = PNV_PHB_MODEL_UNKNOWN;
3118
Gavin Shanaa0c0332013-04-25 19:20:57 +00003119 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003120 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003121
Gavin Shanaa0c0332013-04-25 19:20:57 +00003122 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003123 phb->regs = of_iomap(np, 0);
3124 if (phb->regs == NULL)
3125 pr_err(" Failed to map registers !\n");
3126
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003127 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08003128 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003129 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003130 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003131 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003132 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3133 if (prop32)
3134 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003135
3136 /* Parse 64-bit MMIO range */
3137 pnv_ioda_parse_m64_window(phb);
3138
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003139 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003140 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003141 phb->ioda.m32_size += 0x10000;
3142
3143 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003144 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003145 phb->ioda.io_size = hose->pci_io_size;
3146 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
3147 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3148
Gavin Shanc35d2a82013-07-31 16:47:04 +08003149 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003150 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
3151 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00003152 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003153 if (phb->type == PNV_PHB_IODA1) {
3154 iomap_off = size;
3155 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
3156 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003157 pemap_off = size;
3158 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003159 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003160 phb->ioda.pe_alloc = aux;
3161 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08003162 if (phb->type == PNV_PHB_IODA1)
3163 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003164 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08003165 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003166
Gavin Shan7ebdf952012-08-20 03:49:15 +00003167 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003168 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003169 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003170
3171 /* Calculate how many 32-bit TCE segments we have */
3172 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
3173
Gavin Shanaa0c0332013-04-25 19:20:57 +00003174#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003175 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3176 window_type,
3177 window_num,
3178 starting_real_address,
3179 starting_pci_address,
3180 segment_size);
3181#endif
3182
Guo Chao262af552014-07-21 14:42:30 +10003183 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
3184 phb->ioda.total_pe, phb->ioda.reserved_pe,
3185 phb->ioda.m32_size, phb->ioda.m32_segsize);
3186 if (phb->ioda.m64_size)
3187 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3188 phb->ioda.m64_size, phb->ioda.m64_segsize);
3189 if (phb->ioda.io_size)
3190 pr_info(" IO: 0x%x [segment=0x%x]\n",
3191 phb->ioda.io_size, phb->ioda.io_segsize);
3192
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003193
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003194 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003195 phb->get_pe_state = pnv_ioda_get_pe_state;
3196 phb->freeze_pe = pnv_ioda_freeze_pe;
3197 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003198
3199 /* Setup RID -> PE mapping function */
3200 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
3201
3202 /* Setup TCEs */
3203 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10003204 phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003205
3206 /* Setup MSI support */
3207 pnv_pci_init_ioda_msis(phb);
3208
Gavin Shanc40a4212012-08-20 03:49:20 +00003209 /*
3210 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3211 * to let the PCI core do resource assignment. It's supposed
3212 * that the PCI core will do correct I/O and MMIO alignment
3213 * for the P2P bridge bars so that each PCI bus (excluding
3214 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003215 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003216 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Daniel Axtens92ae0352015-04-28 15:12:05 +10003217 hose->controller_ops = pnv_pci_ioda_controller_ops;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003218
Wei Yang6e628c72015-03-25 16:23:55 +08003219#ifdef CONFIG_PCI_IOV
3220 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003221 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003222#endif
3223
Gavin Shanc40a4212012-08-20 03:49:20 +00003224 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003225
3226 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003227 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003228 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003229 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003230
3231 /* If we're running in kdump kerenl, the previous kerenl never
3232 * shutdown PCI devices correctly. We already got IODA table
3233 * cleaned out. So we have to issue PHB reset to stop all PCI
3234 * transactions from previous kerenl.
3235 */
3236 if (is_kdump_kernel()) {
3237 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003238 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3239 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003240 }
Guo Chao262af552014-07-21 14:42:30 +10003241
Gavin Shan9e9e8932014-11-12 13:36:05 +11003242 /* Remove M64 resource if we can't configure it successfully */
3243 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003244 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003245}
3246
Bjorn Helgaas67975002013-07-02 12:20:03 -06003247void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003248{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003249 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003250}
3251
3252void __init pnv_pci_init_ioda_hub(struct device_node *np)
3253{
3254 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003255 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003256 u64 hub_id;
3257
3258 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3259
3260 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3261 if (!prop64) {
3262 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3263 return;
3264 }
3265 hub_id = be64_to_cpup(prop64);
3266 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3267
3268 /* Count child PHBs */
3269 for_each_child_of_node(np, phbn) {
3270 /* Look for IODA1 PHBs */
3271 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003272 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003273 }
3274}