blob: 4775f9544f5cca5bc27d7dec3de82145275d96f3 [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
Gavin Shane9dc4d72015-06-19 12:26:16 +1000143 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
144 pr_debug("%s: PE %d was reserved on PHB#%x\n",
145 __func__, pe_no, phb->hose->global_number);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100146
147 phb->ioda.pe_array[pe_no].phb = phb;
148 phb->ioda.pe_array[pe_no].pe_number = pe_no;
149}
150
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800151static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000152{
153 unsigned long pe;
154
155 do {
156 pe = find_next_zero_bit(phb->ioda.pe_alloc,
157 phb->ioda.total_pe, 0);
158 if (pe >= phb->ioda.total_pe)
159 return IODA_INVALID_PE;
160 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
161
Gavin Shan4cce9552013-04-25 19:21:00 +0000162 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000163 phb->ioda.pe_array[pe].pe_number = pe;
164 return pe;
165}
166
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800167static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000168{
169 WARN_ON(phb->ioda.pe_array[pe].pdev);
170
171 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
172 clear_bit(pe, phb->ioda.pe_alloc);
173}
174
Guo Chao262af552014-07-21 14:42:30 +1000175/* The default M64 BAR is shared by all PEs */
176static int pnv_ioda2_init_m64(struct pnv_phb *phb)
177{
178 const char *desc;
179 struct resource *r;
180 s64 rc;
181
182 /* Configure the default M64 BAR */
183 rc = opal_pci_set_phb_mem_window(phb->opal_id,
184 OPAL_M64_WINDOW_TYPE,
185 phb->ioda.m64_bar_idx,
186 phb->ioda.m64_base,
187 0, /* unused */
188 phb->ioda.m64_size);
189 if (rc != OPAL_SUCCESS) {
190 desc = "configuring";
191 goto fail;
192 }
193
194 /* Enable the default M64 BAR */
195 rc = opal_pci_phb_mmio_enable(phb->opal_id,
196 OPAL_M64_WINDOW_TYPE,
197 phb->ioda.m64_bar_idx,
198 OPAL_ENABLE_M64_SPLIT);
199 if (rc != OPAL_SUCCESS) {
200 desc = "enabling";
201 goto fail;
202 }
203
204 /* Mark the M64 BAR assigned */
205 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
206
207 /*
208 * Strip off the segment used by the reserved PE, which is
209 * expected to be 0 or last one of PE capabicity.
210 */
211 r = &phb->hose->mem_resources[1];
212 if (phb->ioda.reserved_pe == 0)
213 r->start += phb->ioda.m64_segsize;
214 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
215 r->end -= phb->ioda.m64_segsize;
216 else
217 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
218 phb->ioda.reserved_pe);
219
220 return 0;
221
222fail:
223 pr_warn(" Failure %lld %s M64 BAR#%d\n",
224 rc, desc, phb->ioda.m64_bar_idx);
225 opal_pci_phb_mmio_enable(phb->opal_id,
226 OPAL_M64_WINDOW_TYPE,
227 phb->ioda.m64_bar_idx,
228 OPAL_DISABLE_M64);
229 return -EIO;
230}
231
Gavin Shan5ef73562014-11-12 13:36:06 +1100232static void pnv_ioda2_reserve_m64_pe(struct pnv_phb *phb)
Guo Chao262af552014-07-21 14:42:30 +1000233{
234 resource_size_t sgsz = phb->ioda.m64_segsize;
235 struct pci_dev *pdev;
236 struct resource *r;
237 int base, step, i;
238
239 /*
240 * Root bus always has full M64 range and root port has
241 * M64 range used in reality. So we're checking root port
242 * instead of root bus.
243 */
244 list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100245 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
246 r = &pdev->resource[PCI_BRIDGE_RESOURCES + i];
Guo Chao262af552014-07-21 14:42:30 +1000247 if (!r->parent ||
248 !pnv_pci_is_mem_pref_64(r->flags))
249 continue;
250
251 base = (r->start - phb->ioda.m64_base) / sgsz;
252 for (step = 0; step < resource_size(r) / sgsz; step++)
Gavin Shan4b82ab12014-11-12 13:36:07 +1100253 pnv_ioda_reserve_pe(phb, base + step);
Guo Chao262af552014-07-21 14:42:30 +1000254 }
255 }
256}
257
258static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
259 struct pci_bus *bus, int all)
260{
261 resource_size_t segsz = phb->ioda.m64_segsize;
262 struct pci_dev *pdev;
263 struct resource *r;
264 struct pnv_ioda_pe *master_pe, *pe;
265 unsigned long size, *pe_alloc;
266 bool found;
267 int start, i, j;
268
269 /* Root bus shouldn't use M64 */
270 if (pci_is_root_bus(bus))
271 return IODA_INVALID_PE;
272
273 /* We support only one M64 window on each bus */
274 found = false;
275 pci_bus_for_each_resource(bus, r, i) {
276 if (r && r->parent &&
277 pnv_pci_is_mem_pref_64(r->flags)) {
278 found = true;
279 break;
280 }
281 }
282
283 /* No M64 window found ? */
284 if (!found)
285 return IODA_INVALID_PE;
286
287 /* Allocate bitmap */
288 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
289 pe_alloc = kzalloc(size, GFP_KERNEL);
290 if (!pe_alloc) {
291 pr_warn("%s: Out of memory !\n",
292 __func__);
293 return IODA_INVALID_PE;
294 }
295
296 /*
297 * Figure out reserved PE numbers by the PE
298 * the its child PEs.
299 */
300 start = (r->start - phb->ioda.m64_base) / segsz;
301 for (i = 0; i < resource_size(r) / segsz; i++)
302 set_bit(start + i, pe_alloc);
303
304 if (all)
305 goto done;
306
307 /*
308 * If the PE doesn't cover all subordinate buses,
309 * we need subtract from reserved PEs for children.
310 */
311 list_for_each_entry(pdev, &bus->devices, bus_list) {
312 if (!pdev->subordinate)
313 continue;
314
315 pci_bus_for_each_resource(pdev->subordinate, r, i) {
316 if (!r || !r->parent ||
317 !pnv_pci_is_mem_pref_64(r->flags))
318 continue;
319
320 start = (r->start - phb->ioda.m64_base) / segsz;
321 for (j = 0; j < resource_size(r) / segsz ; j++)
322 clear_bit(start + j, pe_alloc);
323 }
324 }
325
326 /*
327 * the current bus might not own M64 window and that's all
328 * contributed by its child buses. For the case, we needn't
329 * pick M64 dependent PE#.
330 */
331 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
332 kfree(pe_alloc);
333 return IODA_INVALID_PE;
334 }
335
336 /*
337 * Figure out the master PE and put all slave PEs to master
338 * PE's list to form compound PE.
339 */
340done:
341 master_pe = NULL;
342 i = -1;
343 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
344 phb->ioda.total_pe) {
345 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000346
347 if (!master_pe) {
348 pe->flags |= PNV_IODA_PE_MASTER;
349 INIT_LIST_HEAD(&pe->slaves);
350 master_pe = pe;
351 } else {
352 pe->flags |= PNV_IODA_PE_SLAVE;
353 pe->master = master_pe;
354 list_add_tail(&pe->list, &master_pe->slaves);
355 }
356 }
357
358 kfree(pe_alloc);
359 return master_pe->pe_number;
360}
361
362static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
363{
364 struct pci_controller *hose = phb->hose;
365 struct device_node *dn = hose->dn;
366 struct resource *res;
367 const u32 *r;
368 u64 pci_addr;
369
Gavin Shan1665c4a2014-11-12 13:36:04 +1100370 /* FIXME: Support M64 for P7IOC */
371 if (phb->type != PNV_PHB_IODA2) {
372 pr_info(" Not support M64 window\n");
373 return;
374 }
375
Guo Chao262af552014-07-21 14:42:30 +1000376 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
377 pr_info(" Firmware too old to support M64 window\n");
378 return;
379 }
380
381 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
382 if (!r) {
383 pr_info(" No <ibm,opal-m64-window> on %s\n",
384 dn->full_name);
385 return;
386 }
387
Guo Chao262af552014-07-21 14:42:30 +1000388 res = &hose->mem_resources[1];
389 res->start = of_translate_address(dn, r + 2);
390 res->end = res->start + of_read_number(r + 4, 2) - 1;
391 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
392 pci_addr = of_read_number(r, 2);
393 hose->mem_offset[1] = res->start - pci_addr;
394
395 phb->ioda.m64_size = resource_size(res);
396 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
397 phb->ioda.m64_base = pci_addr;
398
Wei Yange9863e62014-12-12 12:39:37 +0800399 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
400 res->start, res->end, pci_addr);
401
Guo Chao262af552014-07-21 14:42:30 +1000402 /* Use last M64 BAR to cover M64 window */
403 phb->ioda.m64_bar_idx = 15;
404 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shan5ef73562014-11-12 13:36:06 +1100405 phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000406 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
407}
408
Gavin Shan49dec922014-07-21 14:42:33 +1000409static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
410{
411 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
412 struct pnv_ioda_pe *slave;
413 s64 rc;
414
415 /* Fetch master PE */
416 if (pe->flags & PNV_IODA_PE_SLAVE) {
417 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100418 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
419 return;
420
Gavin Shan49dec922014-07-21 14:42:33 +1000421 pe_no = pe->pe_number;
422 }
423
424 /* Freeze master PE */
425 rc = opal_pci_eeh_freeze_set(phb->opal_id,
426 pe_no,
427 OPAL_EEH_ACTION_SET_FREEZE_ALL);
428 if (rc != OPAL_SUCCESS) {
429 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
430 __func__, rc, phb->hose->global_number, pe_no);
431 return;
432 }
433
434 /* Freeze slave PEs */
435 if (!(pe->flags & PNV_IODA_PE_MASTER))
436 return;
437
438 list_for_each_entry(slave, &pe->slaves, list) {
439 rc = opal_pci_eeh_freeze_set(phb->opal_id,
440 slave->pe_number,
441 OPAL_EEH_ACTION_SET_FREEZE_ALL);
442 if (rc != OPAL_SUCCESS)
443 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
444 __func__, rc, phb->hose->global_number,
445 slave->pe_number);
446 }
447}
448
Anton Blancharde51df2c2014-08-20 08:55:18 +1000449static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000450{
451 struct pnv_ioda_pe *pe, *slave;
452 s64 rc;
453
454 /* Find master PE */
455 pe = &phb->ioda.pe_array[pe_no];
456 if (pe->flags & PNV_IODA_PE_SLAVE) {
457 pe = pe->master;
458 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
459 pe_no = pe->pe_number;
460 }
461
462 /* Clear frozen state for master PE */
463 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
464 if (rc != OPAL_SUCCESS) {
465 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
466 __func__, rc, opt, phb->hose->global_number, pe_no);
467 return -EIO;
468 }
469
470 if (!(pe->flags & PNV_IODA_PE_MASTER))
471 return 0;
472
473 /* Clear frozen state for slave PEs */
474 list_for_each_entry(slave, &pe->slaves, list) {
475 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
476 slave->pe_number,
477 opt);
478 if (rc != OPAL_SUCCESS) {
479 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
480 __func__, rc, opt, phb->hose->global_number,
481 slave->pe_number);
482 return -EIO;
483 }
484 }
485
486 return 0;
487}
488
489static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
490{
491 struct pnv_ioda_pe *slave, *pe;
492 u8 fstate, state;
493 __be16 pcierr;
494 s64 rc;
495
496 /* Sanity check on PE number */
497 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
498 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
499
500 /*
501 * Fetch the master PE and the PE instance might be
502 * not initialized yet.
503 */
504 pe = &phb->ioda.pe_array[pe_no];
505 if (pe->flags & PNV_IODA_PE_SLAVE) {
506 pe = pe->master;
507 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
508 pe_no = pe->pe_number;
509 }
510
511 /* Check the master PE */
512 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
513 &state, &pcierr, NULL);
514 if (rc != OPAL_SUCCESS) {
515 pr_warn("%s: Failure %lld getting "
516 "PHB#%x-PE#%x state\n",
517 __func__, rc,
518 phb->hose->global_number, pe_no);
519 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
520 }
521
522 /* Check the slave PE */
523 if (!(pe->flags & PNV_IODA_PE_MASTER))
524 return state;
525
526 list_for_each_entry(slave, &pe->slaves, list) {
527 rc = opal_pci_eeh_freeze_status(phb->opal_id,
528 slave->pe_number,
529 &fstate,
530 &pcierr,
531 NULL);
532 if (rc != OPAL_SUCCESS) {
533 pr_warn("%s: Failure %lld getting "
534 "PHB#%x-PE#%x state\n",
535 __func__, rc,
536 phb->hose->global_number, slave->pe_number);
537 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
538 }
539
540 /*
541 * Override the result based on the ascending
542 * priority.
543 */
544 if (fstate > state)
545 state = fstate;
546 }
547
548 return state;
549}
550
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000551/* Currently those 2 are only used when MSIs are enabled, this will change
552 * but in the meantime, we need to protect them to avoid warnings
553 */
554#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800555static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000556{
557 struct pci_controller *hose = pci_bus_to_host(dev->bus);
558 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000559 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000560
561 if (!pdn)
562 return NULL;
563 if (pdn->pe_number == IODA_INVALID_PE)
564 return NULL;
565 return &phb->ioda.pe_array[pdn->pe_number];
566}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000567#endif /* CONFIG_PCI_MSI */
568
Gavin Shanb131a842014-11-12 13:36:08 +1100569static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
570 struct pnv_ioda_pe *parent,
571 struct pnv_ioda_pe *child,
572 bool is_add)
573{
574 const char *desc = is_add ? "adding" : "removing";
575 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
576 OPAL_REMOVE_PE_FROM_DOMAIN;
577 struct pnv_ioda_pe *slave;
578 long rc;
579
580 /* Parent PE affects child PE */
581 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
582 child->pe_number, op);
583 if (rc != OPAL_SUCCESS) {
584 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
585 rc, desc);
586 return -ENXIO;
587 }
588
589 if (!(child->flags & PNV_IODA_PE_MASTER))
590 return 0;
591
592 /* Compound case: parent PE affects slave PEs */
593 list_for_each_entry(slave, &child->slaves, list) {
594 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
595 slave->pe_number, op);
596 if (rc != OPAL_SUCCESS) {
597 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
598 rc, desc);
599 return -ENXIO;
600 }
601 }
602
603 return 0;
604}
605
606static int pnv_ioda_set_peltv(struct pnv_phb *phb,
607 struct pnv_ioda_pe *pe,
608 bool is_add)
609{
610 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800611 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100612 int ret;
613
614 /*
615 * Clear PE frozen state. If it's master PE, we need
616 * clear slave PE frozen state as well.
617 */
618 if (is_add) {
619 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
620 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
621 if (pe->flags & PNV_IODA_PE_MASTER) {
622 list_for_each_entry(slave, &pe->slaves, list)
623 opal_pci_eeh_freeze_clear(phb->opal_id,
624 slave->pe_number,
625 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
626 }
627 }
628
629 /*
630 * Associate PE in PELT. We need add the PE into the
631 * corresponding PELT-V as well. Otherwise, the error
632 * originated from the PE might contribute to other
633 * PEs.
634 */
635 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
636 if (ret)
637 return ret;
638
639 /* For compound PEs, any one affects all of them */
640 if (pe->flags & PNV_IODA_PE_MASTER) {
641 list_for_each_entry(slave, &pe->slaves, list) {
642 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
643 if (ret)
644 return ret;
645 }
646 }
647
648 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
649 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800650 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100651 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800652#ifdef CONFIG_PCI_IOV
653 else if (pe->flags & PNV_IODA_PE_VF)
654 pdev = pe->parent_dev->bus->self;
655#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100656 while (pdev) {
657 struct pci_dn *pdn = pci_get_pdn(pdev);
658 struct pnv_ioda_pe *parent;
659
660 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
661 parent = &phb->ioda.pe_array[pdn->pe_number];
662 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
663 if (ret)
664 return ret;
665 }
666
667 pdev = pdev->bus->self;
668 }
669
670 return 0;
671}
672
Wei Yang781a8682015-03-25 16:23:57 +0800673#ifdef CONFIG_PCI_IOV
674static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
675{
676 struct pci_dev *parent;
677 uint8_t bcomp, dcomp, fcomp;
678 int64_t rc;
679 long rid_end, rid;
680
681 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
682 if (pe->pbus) {
683 int count;
684
685 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
686 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
687 parent = pe->pbus->self;
688 if (pe->flags & PNV_IODA_PE_BUS_ALL)
689 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
690 else
691 count = 1;
692
693 switch(count) {
694 case 1: bcomp = OpalPciBusAll; break;
695 case 2: bcomp = OpalPciBus7Bits; break;
696 case 4: bcomp = OpalPciBus6Bits; break;
697 case 8: bcomp = OpalPciBus5Bits; break;
698 case 16: bcomp = OpalPciBus4Bits; break;
699 case 32: bcomp = OpalPciBus3Bits; break;
700 default:
701 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
702 count);
703 /* Do an exact match only */
704 bcomp = OpalPciBusAll;
705 }
706 rid_end = pe->rid + (count << 8);
707 } else {
708 if (pe->flags & PNV_IODA_PE_VF)
709 parent = pe->parent_dev;
710 else
711 parent = pe->pdev->bus->self;
712 bcomp = OpalPciBusAll;
713 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
714 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
715 rid_end = pe->rid + 1;
716 }
717
718 /* Clear the reverse map */
719 for (rid = pe->rid; rid < rid_end; rid++)
720 phb->ioda.pe_rmap[rid] = 0;
721
722 /* Release from all parents PELT-V */
723 while (parent) {
724 struct pci_dn *pdn = pci_get_pdn(parent);
725 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
726 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
727 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
728 /* XXX What to do in case of error ? */
729 }
730 parent = parent->bus->self;
731 }
732
733 opal_pci_eeh_freeze_set(phb->opal_id, pe->pe_number,
734 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
735
736 /* Disassociate PE in PELT */
737 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
738 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
739 if (rc)
740 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
741 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
742 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
743 if (rc)
744 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
745
746 pe->pbus = NULL;
747 pe->pdev = NULL;
748 pe->parent_dev = NULL;
749
750 return 0;
751}
752#endif /* CONFIG_PCI_IOV */
753
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800754static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000755{
756 struct pci_dev *parent;
757 uint8_t bcomp, dcomp, fcomp;
758 long rc, rid_end, rid;
759
760 /* Bus validation ? */
761 if (pe->pbus) {
762 int count;
763
764 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
765 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
766 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000767 if (pe->flags & PNV_IODA_PE_BUS_ALL)
768 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
769 else
770 count = 1;
771
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000772 switch(count) {
773 case 1: bcomp = OpalPciBusAll; break;
774 case 2: bcomp = OpalPciBus7Bits; break;
775 case 4: bcomp = OpalPciBus6Bits; break;
776 case 8: bcomp = OpalPciBus5Bits; break;
777 case 16: bcomp = OpalPciBus4Bits; break;
778 case 32: bcomp = OpalPciBus3Bits; break;
779 default:
Wei Yang781a8682015-03-25 16:23:57 +0800780 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
781 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000782 /* Do an exact match only */
783 bcomp = OpalPciBusAll;
784 }
785 rid_end = pe->rid + (count << 8);
786 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800787#ifdef CONFIG_PCI_IOV
788 if (pe->flags & PNV_IODA_PE_VF)
789 parent = pe->parent_dev;
790 else
791#endif /* CONFIG_PCI_IOV */
792 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000793 bcomp = OpalPciBusAll;
794 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
795 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
796 rid_end = pe->rid + 1;
797 }
798
Gavin Shan631ad692013-11-04 16:32:46 +0800799 /*
800 * Associate PE in PELT. We need add the PE into the
801 * corresponding PELT-V as well. Otherwise, the error
802 * originated from the PE might contribute to other
803 * PEs.
804 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000805 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
806 bcomp, dcomp, fcomp, OPAL_MAP_PE);
807 if (rc) {
808 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
809 return -ENXIO;
810 }
Gavin Shan631ad692013-11-04 16:32:46 +0800811
Gavin Shanb131a842014-11-12 13:36:08 +1100812 /* Configure PELTV */
813 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000814
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000815 /* Setup reverse map */
816 for (rid = pe->rid; rid < rid_end; rid++)
817 phb->ioda.pe_rmap[rid] = pe->pe_number;
818
819 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100820 if (phb->type != PNV_PHB_IODA1) {
821 pe->mve_number = 0;
822 goto out;
823 }
824
825 pe->mve_number = pe->pe_number;
826 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
827 if (rc != OPAL_SUCCESS) {
828 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
829 rc, pe->mve_number);
830 pe->mve_number = -1;
831 } else {
832 rc = opal_pci_set_mve_enable(phb->opal_id,
833 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000834 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100835 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000836 rc, pe->mve_number);
837 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000838 }
Gavin Shan4773f762014-11-12 13:36:09 +1100839 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000840
Gavin Shan4773f762014-11-12 13:36:09 +1100841out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000842 return 0;
843}
844
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800845static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
846 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000847{
848 struct pnv_ioda_pe *lpe;
849
Gavin Shan7ebdf952012-08-20 03:49:15 +0000850 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000851 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000852 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000853 return;
854 }
855 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000856 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000857}
858
859static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
860{
861 /* This is quite simplistic. The "base" weight of a device
862 * is 10. 0 means no DMA is to be accounted for it.
863 */
864
865 /* If it's a bridge, no DMA */
866 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
867 return 0;
868
869 /* Reduce the weight of slow USB controllers */
870 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
871 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
872 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
873 return 3;
874
875 /* Increase the weight of RAID (includes Obsidian) */
876 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
877 return 15;
878
879 /* Default */
880 return 10;
881}
882
Wei Yang781a8682015-03-25 16:23:57 +0800883#ifdef CONFIG_PCI_IOV
884static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
885{
886 struct pci_dn *pdn = pci_get_pdn(dev);
887 int i;
888 struct resource *res, res2;
889 resource_size_t size;
890 u16 num_vfs;
891
892 if (!dev->is_physfn)
893 return -EINVAL;
894
895 /*
896 * "offset" is in VFs. The M64 windows are sized so that when they
897 * are segmented, each segment is the same size as the IOV BAR.
898 * Each segment is in a separate PE, and the high order bits of the
899 * address are the PE number. Therefore, each VF's BAR is in a
900 * separate PE, and changing the IOV BAR start address changes the
901 * range of PEs the VFs are in.
902 */
903 num_vfs = pdn->num_vfs;
904 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
905 res = &dev->resource[i + PCI_IOV_RESOURCES];
906 if (!res->flags || !res->parent)
907 continue;
908
909 if (!pnv_pci_is_mem_pref_64(res->flags))
910 continue;
911
912 /*
913 * The actual IOV BAR range is determined by the start address
914 * and the actual size for num_vfs VFs BAR. This check is to
915 * make sure that after shifting, the range will not overlap
916 * with another device.
917 */
918 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
919 res2.flags = res->flags;
920 res2.start = res->start + (size * offset);
921 res2.end = res2.start + (size * num_vfs) - 1;
922
923 if (res2.end > res->end) {
924 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
925 i, &res2, res, num_vfs, offset);
926 return -EBUSY;
927 }
928 }
929
930 /*
931 * After doing so, there would be a "hole" in the /proc/iomem when
932 * offset is a positive value. It looks like the device return some
933 * mmio back to the system, which actually no one could use it.
934 */
935 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
936 res = &dev->resource[i + PCI_IOV_RESOURCES];
937 if (!res->flags || !res->parent)
938 continue;
939
940 if (!pnv_pci_is_mem_pref_64(res->flags))
941 continue;
942
943 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
944 res2 = *res;
945 res->start += size * offset;
946
947 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (enabling %d VFs shifted by %d)\n",
948 i, &res2, res, num_vfs, offset);
949 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
950 }
951 return 0;
952}
953#endif /* CONFIG_PCI_IOV */
954
Gavin Shanfb446ad2012-08-20 03:49:14 +0000955#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800956static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000957{
958 struct pci_controller *hose = pci_bus_to_host(dev->bus);
959 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000960 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000961 struct pnv_ioda_pe *pe;
962 int pe_num;
963
964 if (!pdn) {
965 pr_err("%s: Device tree node not associated properly\n",
966 pci_name(dev));
967 return NULL;
968 }
969 if (pdn->pe_number != IODA_INVALID_PE)
970 return NULL;
971
972 /* PE#0 has been pre-set */
973 if (dev->bus->number == 0)
974 pe_num = 0;
975 else
976 pe_num = pnv_ioda_alloc_pe(phb);
977 if (pe_num == IODA_INVALID_PE) {
978 pr_warning("%s: Not enough PE# available, disabling device\n",
979 pci_name(dev));
980 return NULL;
981 }
982
983 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
984 * pointer in the PE data structure, both should be destroyed at the
985 * same time. However, this needs to be looked at more closely again
986 * once we actually start removing things (Hotplug, SR-IOV, ...)
987 *
988 * At some point we want to remove the PDN completely anyways
989 */
990 pe = &phb->ioda.pe_array[pe_num];
991 pci_dev_get(dev);
992 pdn->pcidev = dev;
993 pdn->pe_number = pe_num;
994 pe->pdev = dev;
995 pe->pbus = NULL;
996 pe->tce32_seg = -1;
997 pe->mve_number = -1;
998 pe->rid = dev->bus->number << 8 | pdn->devfn;
999
1000 pe_info(pe, "Associated device to PE\n");
1001
1002 if (pnv_ioda_configure_pe(phb, pe)) {
1003 /* XXX What do we do here ? */
1004 if (pe_num)
1005 pnv_ioda_free_pe(phb, pe_num);
1006 pdn->pe_number = IODA_INVALID_PE;
1007 pe->pdev = NULL;
1008 pci_dev_put(dev);
1009 return NULL;
1010 }
1011
1012 /* Assign a DMA weight to the device */
1013 pe->dma_weight = pnv_ioda_dma_weight(dev);
1014 if (pe->dma_weight != 0) {
1015 phb->ioda.dma_weight += pe->dma_weight;
1016 phb->ioda.dma_pe_count++;
1017 }
1018
1019 /* Link the PE */
1020 pnv_ioda_link_pe_by_weight(phb, pe);
1021
1022 return pe;
1023}
Gavin Shanfb446ad2012-08-20 03:49:14 +00001024#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001025
1026static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1027{
1028 struct pci_dev *dev;
1029
1030 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001031 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001032
1033 if (pdn == NULL) {
1034 pr_warn("%s: No device node associated with device !\n",
1035 pci_name(dev));
1036 continue;
1037 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001038 pdn->pe_number = pe->pe_number;
1039 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001040 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001041 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1042 }
1043}
1044
Gavin Shanfb446ad2012-08-20 03:49:14 +00001045/*
1046 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1047 * single PCI bus. Another one that contains the primary PCI bus and its
1048 * subordinate PCI devices and buses. The second type of PE is normally
1049 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1050 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001051static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001052{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001053 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001054 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001055 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +10001056 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001057
Guo Chao262af552014-07-21 14:42:30 +10001058 /* Check if PE is determined by M64 */
1059 if (phb->pick_m64_pe)
1060 pe_num = phb->pick_m64_pe(phb, bus, all);
1061
1062 /* The PE number isn't pinned by M64 */
1063 if (pe_num == IODA_INVALID_PE)
1064 pe_num = pnv_ioda_alloc_pe(phb);
1065
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001066 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001067 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1068 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001069 return;
1070 }
1071
1072 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +10001073 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001074 pe->pbus = bus;
1075 pe->pdev = NULL;
1076 pe->tce32_seg = -1;
1077 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001078 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001079 pe->dma_weight = 0;
1080
Gavin Shanfb446ad2012-08-20 03:49:14 +00001081 if (all)
1082 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
1083 bus->busn_res.start, bus->busn_res.end, pe_num);
1084 else
1085 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
1086 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001087
1088 if (pnv_ioda_configure_pe(phb, pe)) {
1089 /* XXX What do we do here ? */
1090 if (pe_num)
1091 pnv_ioda_free_pe(phb, pe_num);
1092 pe->pbus = NULL;
1093 return;
1094 }
1095
1096 /* Associate it with all child devices */
1097 pnv_ioda_setup_same_PE(bus, pe);
1098
Gavin Shan7ebdf952012-08-20 03:49:15 +00001099 /* Put PE to the list */
1100 list_add_tail(&pe->list, &phb->ioda.pe_list);
1101
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001102 /* Account for one DMA PE if at least one DMA capable device exist
1103 * below the bridge
1104 */
1105 if (pe->dma_weight != 0) {
1106 phb->ioda.dma_weight += pe->dma_weight;
1107 phb->ioda.dma_pe_count++;
1108 }
1109
1110 /* Link the PE */
1111 pnv_ioda_link_pe_by_weight(phb, pe);
1112}
1113
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001114static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001115{
1116 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001117
1118 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001119
1120 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001121 if (dev->subordinate) {
1122 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
1123 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
1124 else
1125 pnv_ioda_setup_PEs(dev->subordinate);
1126 }
1127 }
1128}
1129
1130/*
1131 * Configure PEs so that the downstream PCI buses and devices
1132 * could have their associated PE#. Unfortunately, we didn't
1133 * figure out the way to identify the PLX bridge yet. So we
1134 * simply put the PCI bus and the subordinate behind the root
1135 * port to PE# here. The game rule here is expected to be changed
1136 * as soon as we can detected PLX bridge correctly.
1137 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001138static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001139{
1140 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001141 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001142
1143 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001144 phb = hose->private_data;
1145
1146 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +11001147 if (phb->reserve_m64_pe)
1148 phb->reserve_m64_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +10001149
Gavin Shanfb446ad2012-08-20 03:49:14 +00001150 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001151 }
1152}
1153
Gavin Shana8b2f822015-03-25 16:23:52 +08001154#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +08001155static int pnv_pci_vf_release_m64(struct pci_dev *pdev)
1156{
1157 struct pci_bus *bus;
1158 struct pci_controller *hose;
1159 struct pnv_phb *phb;
1160 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001161 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001162
1163 bus = pdev->bus;
1164 hose = pci_bus_to_host(bus);
1165 phb = hose->private_data;
1166 pdn = pci_get_pdn(pdev);
1167
Wei Yang02639b02015-03-25 16:23:59 +08001168 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
1169 for (j = 0; j < M64_PER_IOV; j++) {
1170 if (pdn->m64_wins[i][j] == IODA_INVALID_M64)
1171 continue;
1172 opal_pci_phb_mmio_enable(phb->opal_id,
1173 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 0);
1174 clear_bit(pdn->m64_wins[i][j], &phb->ioda.m64_bar_alloc);
1175 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1176 }
Wei Yang781a8682015-03-25 16:23:57 +08001177
1178 return 0;
1179}
1180
Wei Yang02639b02015-03-25 16:23:59 +08001181static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001182{
1183 struct pci_bus *bus;
1184 struct pci_controller *hose;
1185 struct pnv_phb *phb;
1186 struct pci_dn *pdn;
1187 unsigned int win;
1188 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001189 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001190 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001191 int total_vfs;
1192 resource_size_t size, start;
1193 int pe_num;
1194 int vf_groups;
1195 int vf_per_group;
Wei Yang781a8682015-03-25 16:23:57 +08001196
1197 bus = pdev->bus;
1198 hose = pci_bus_to_host(bus);
1199 phb = hose->private_data;
1200 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001201 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001202
1203 /* Initialize the m64_wins to IODA_INVALID_M64 */
1204 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yang02639b02015-03-25 16:23:59 +08001205 for (j = 0; j < M64_PER_IOV; j++)
1206 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1207
1208 if (pdn->m64_per_iov == M64_PER_IOV) {
1209 vf_groups = (num_vfs <= M64_PER_IOV) ? num_vfs: M64_PER_IOV;
1210 vf_per_group = (num_vfs <= M64_PER_IOV)? 1:
1211 roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1212 } else {
1213 vf_groups = 1;
1214 vf_per_group = 1;
1215 }
Wei Yang781a8682015-03-25 16:23:57 +08001216
1217 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1218 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1219 if (!res->flags || !res->parent)
1220 continue;
1221
1222 if (!pnv_pci_is_mem_pref_64(res->flags))
1223 continue;
1224
Wei Yang02639b02015-03-25 16:23:59 +08001225 for (j = 0; j < vf_groups; j++) {
1226 do {
1227 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1228 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001229
Wei Yang02639b02015-03-25 16:23:59 +08001230 if (win >= phb->ioda.m64_bar_idx + 1)
1231 goto m64_failed;
1232 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001233
Wei Yang02639b02015-03-25 16:23:59 +08001234 pdn->m64_wins[i][j] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001235
Wei Yang02639b02015-03-25 16:23:59 +08001236 if (pdn->m64_per_iov == M64_PER_IOV) {
1237 size = pci_iov_resource_size(pdev,
1238 PCI_IOV_RESOURCES + i);
1239 size = size * vf_per_group;
1240 start = res->start + size * j;
1241 } else {
1242 size = resource_size(res);
1243 start = res->start;
1244 }
1245
1246 /* Map the M64 here */
1247 if (pdn->m64_per_iov == M64_PER_IOV) {
1248 pe_num = pdn->offset + j;
1249 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1250 pe_num, OPAL_M64_WINDOW_TYPE,
1251 pdn->m64_wins[i][j], 0);
1252 }
1253
1254 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001255 OPAL_M64_WINDOW_TYPE,
Wei Yang02639b02015-03-25 16:23:59 +08001256 pdn->m64_wins[i][j],
1257 start,
Wei Yang781a8682015-03-25 16:23:57 +08001258 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001259 size);
Wei Yang781a8682015-03-25 16:23:57 +08001260
Wei Yang02639b02015-03-25 16:23:59 +08001261
1262 if (rc != OPAL_SUCCESS) {
1263 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1264 win, rc);
1265 goto m64_failed;
1266 }
1267
1268 if (pdn->m64_per_iov == M64_PER_IOV)
1269 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1270 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 2);
1271 else
1272 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1273 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 1);
1274
1275 if (rc != OPAL_SUCCESS) {
1276 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1277 win, rc);
1278 goto m64_failed;
1279 }
Wei Yang781a8682015-03-25 16:23:57 +08001280 }
1281 }
1282 return 0;
1283
1284m64_failed:
1285 pnv_pci_vf_release_m64(pdev);
1286 return -EBUSY;
1287}
1288
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001289static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1290 int num);
1291static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1292
Wei Yang781a8682015-03-25 16:23:57 +08001293static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1294{
Wei Yang781a8682015-03-25 16:23:57 +08001295 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001296 int64_t rc;
1297
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001298 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001299 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001300 if (rc)
1301 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1302
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001303 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001304 if (pe->table_group.group) {
1305 iommu_group_put(pe->table_group.group);
1306 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001307 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001308 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001309 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001310}
1311
Wei Yang02639b02015-03-25 16:23:59 +08001312static void pnv_ioda_release_vf_PE(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001313{
1314 struct pci_bus *bus;
1315 struct pci_controller *hose;
1316 struct pnv_phb *phb;
1317 struct pnv_ioda_pe *pe, *pe_n;
1318 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001319 u16 vf_index;
1320 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001321
1322 bus = pdev->bus;
1323 hose = pci_bus_to_host(bus);
1324 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001325 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001326
1327 if (!pdev->is_physfn)
1328 return;
1329
Wei Yang02639b02015-03-25 16:23:59 +08001330 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1331 int vf_group;
1332 int vf_per_group;
1333 int vf_index1;
1334
1335 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1336
1337 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++)
1338 for (vf_index = vf_group * vf_per_group;
1339 vf_index < (vf_group + 1) * vf_per_group &&
1340 vf_index < num_vfs;
1341 vf_index++)
1342 for (vf_index1 = vf_group * vf_per_group;
1343 vf_index1 < (vf_group + 1) * vf_per_group &&
1344 vf_index1 < num_vfs;
1345 vf_index1++){
1346
1347 rc = opal_pci_set_peltv(phb->opal_id,
1348 pdn->offset + vf_index,
1349 pdn->offset + vf_index1,
1350 OPAL_REMOVE_PE_FROM_DOMAIN);
1351
1352 if (rc)
1353 dev_warn(&pdev->dev, "%s: Failed to unlink same group PE#%d(%lld)\n",
1354 __func__,
1355 pdn->offset + vf_index1, rc);
1356 }
1357 }
1358
Wei Yang781a8682015-03-25 16:23:57 +08001359 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1360 if (pe->parent_dev != pdev)
1361 continue;
1362
1363 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1364
1365 /* Remove from list */
1366 mutex_lock(&phb->ioda.pe_list_mutex);
1367 list_del(&pe->list);
1368 mutex_unlock(&phb->ioda.pe_list_mutex);
1369
1370 pnv_ioda_deconfigure_pe(phb, pe);
1371
1372 pnv_ioda_free_pe(phb, pe->pe_number);
1373 }
1374}
1375
1376void pnv_pci_sriov_disable(struct pci_dev *pdev)
1377{
1378 struct pci_bus *bus;
1379 struct pci_controller *hose;
1380 struct pnv_phb *phb;
1381 struct pci_dn *pdn;
1382 struct pci_sriov *iov;
1383 u16 num_vfs;
1384
1385 bus = pdev->bus;
1386 hose = pci_bus_to_host(bus);
1387 phb = hose->private_data;
1388 pdn = pci_get_pdn(pdev);
1389 iov = pdev->sriov;
1390 num_vfs = pdn->num_vfs;
1391
1392 /* Release VF PEs */
Wei Yang02639b02015-03-25 16:23:59 +08001393 pnv_ioda_release_vf_PE(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001394
1395 if (phb->type == PNV_PHB_IODA2) {
Wei Yang02639b02015-03-25 16:23:59 +08001396 if (pdn->m64_per_iov == 1)
1397 pnv_pci_vf_resource_shift(pdev, -pdn->offset);
Wei Yang781a8682015-03-25 16:23:57 +08001398
1399 /* Release M64 windows */
1400 pnv_pci_vf_release_m64(pdev);
1401
1402 /* Release PE numbers */
1403 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1404 pdn->offset = 0;
1405 }
1406}
1407
1408static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1409 struct pnv_ioda_pe *pe);
1410static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1411{
1412 struct pci_bus *bus;
1413 struct pci_controller *hose;
1414 struct pnv_phb *phb;
1415 struct pnv_ioda_pe *pe;
1416 int pe_num;
1417 u16 vf_index;
1418 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001419 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001420
1421 bus = pdev->bus;
1422 hose = pci_bus_to_host(bus);
1423 phb = hose->private_data;
1424 pdn = pci_get_pdn(pdev);
1425
1426 if (!pdev->is_physfn)
1427 return;
1428
1429 /* Reserve PE for each VF */
1430 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
1431 pe_num = pdn->offset + vf_index;
1432
1433 pe = &phb->ioda.pe_array[pe_num];
1434 pe->pe_number = pe_num;
1435 pe->phb = phb;
1436 pe->flags = PNV_IODA_PE_VF;
1437 pe->pbus = NULL;
1438 pe->parent_dev = pdev;
1439 pe->tce32_seg = -1;
1440 pe->mve_number = -1;
1441 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1442 pci_iov_virtfn_devfn(pdev, vf_index);
1443
1444 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1445 hose->global_number, pdev->bus->number,
1446 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1447 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1448
1449 if (pnv_ioda_configure_pe(phb, pe)) {
1450 /* XXX What do we do here ? */
1451 if (pe_num)
1452 pnv_ioda_free_pe(phb, pe_num);
1453 pe->pdev = NULL;
1454 continue;
1455 }
1456
Wei Yang781a8682015-03-25 16:23:57 +08001457 /* Put PE to the list */
1458 mutex_lock(&phb->ioda.pe_list_mutex);
1459 list_add_tail(&pe->list, &phb->ioda.pe_list);
1460 mutex_unlock(&phb->ioda.pe_list_mutex);
1461
1462 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1463 }
Wei Yang02639b02015-03-25 16:23:59 +08001464
1465 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1466 int vf_group;
1467 int vf_per_group;
1468 int vf_index1;
1469
1470 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1471
1472 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++) {
1473 for (vf_index = vf_group * vf_per_group;
1474 vf_index < (vf_group + 1) * vf_per_group &&
1475 vf_index < num_vfs;
1476 vf_index++) {
1477 for (vf_index1 = vf_group * vf_per_group;
1478 vf_index1 < (vf_group + 1) * vf_per_group &&
1479 vf_index1 < num_vfs;
1480 vf_index1++) {
1481
1482 rc = opal_pci_set_peltv(phb->opal_id,
1483 pdn->offset + vf_index,
1484 pdn->offset + vf_index1,
1485 OPAL_ADD_PE_TO_DOMAIN);
1486
1487 if (rc)
1488 dev_warn(&pdev->dev, "%s: Failed to link same group PE#%d(%lld)\n",
1489 __func__,
1490 pdn->offset + vf_index1, rc);
1491 }
1492 }
1493 }
1494 }
Wei Yang781a8682015-03-25 16:23:57 +08001495}
1496
1497int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1498{
1499 struct pci_bus *bus;
1500 struct pci_controller *hose;
1501 struct pnv_phb *phb;
1502 struct pci_dn *pdn;
1503 int ret;
1504
1505 bus = pdev->bus;
1506 hose = pci_bus_to_host(bus);
1507 phb = hose->private_data;
1508 pdn = pci_get_pdn(pdev);
1509
1510 if (phb->type == PNV_PHB_IODA2) {
1511 /* Calculate available PE for required VFs */
1512 mutex_lock(&phb->ioda.pe_alloc_mutex);
1513 pdn->offset = bitmap_find_next_zero_area(
1514 phb->ioda.pe_alloc, phb->ioda.total_pe,
1515 0, num_vfs, 0);
1516 if (pdn->offset >= phb->ioda.total_pe) {
1517 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1518 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1519 pdn->offset = 0;
1520 return -EBUSY;
1521 }
1522 bitmap_set(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1523 pdn->num_vfs = num_vfs;
1524 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1525
1526 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001527 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001528 if (ret) {
1529 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1530 goto m64_failed;
1531 }
1532
1533 /*
1534 * When using one M64 BAR to map one IOV BAR, we need to shift
1535 * the IOV BAR according to the PE# allocated to the VFs.
1536 * Otherwise, the PE# for the VF will conflict with others.
1537 */
Wei Yang02639b02015-03-25 16:23:59 +08001538 if (pdn->m64_per_iov == 1) {
1539 ret = pnv_pci_vf_resource_shift(pdev, pdn->offset);
1540 if (ret)
1541 goto m64_failed;
1542 }
Wei Yang781a8682015-03-25 16:23:57 +08001543 }
1544
1545 /* Setup VF PEs */
1546 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1547
1548 return 0;
1549
1550m64_failed:
1551 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1552 pdn->offset = 0;
1553
1554 return ret;
1555}
1556
Gavin Shana8b2f822015-03-25 16:23:52 +08001557int pcibios_sriov_disable(struct pci_dev *pdev)
1558{
Wei Yang781a8682015-03-25 16:23:57 +08001559 pnv_pci_sriov_disable(pdev);
1560
Gavin Shana8b2f822015-03-25 16:23:52 +08001561 /* Release PCI data */
1562 remove_dev_pci_data(pdev);
1563 return 0;
1564}
1565
1566int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1567{
1568 /* Allocate PCI data */
1569 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001570
1571 pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001572 return 0;
1573}
1574#endif /* CONFIG_PCI_IOV */
1575
Gavin Shan959c9bd2013-04-25 19:21:02 +00001576static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001577{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001578 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001579 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001580
Gavin Shan959c9bd2013-04-25 19:21:02 +00001581 /*
1582 * The function can be called while the PE#
1583 * hasn't been assigned. Do nothing for the
1584 * case.
1585 */
1586 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1587 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001588
Gavin Shan959c9bd2013-04-25 19:21:02 +00001589 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001590 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001591 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001592 /*
1593 * Note: iommu_add_device() will fail here as
1594 * for physical PE: the device is already added by now;
1595 * for virtual PE: sysfs entries are not ready yet and
1596 * tce_iommu_bus_notifier will add the device to a group later.
1597 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001598}
1599
Daniel Axtens763d2d82015-04-28 15:12:07 +10001600static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001601{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001602 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1603 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001604 struct pci_dn *pdn = pci_get_pdn(pdev);
1605 struct pnv_ioda_pe *pe;
1606 uint64_t top;
1607 bool bypass = false;
1608
1609 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1610 return -ENODEV;;
1611
1612 pe = &phb->ioda.pe_array[pdn->pe_number];
1613 if (pe->tce_bypass_enabled) {
1614 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1615 bypass = (dma_mask >= top);
1616 }
1617
1618 if (bypass) {
1619 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1620 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001621 } else {
1622 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1623 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001624 }
Brian W Harta32305b2014-07-31 14:24:37 -05001625 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001626 return 0;
1627}
1628
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001629static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
1630 struct pci_dev *pdev)
1631{
1632 struct pci_dn *pdn = pci_get_pdn(pdev);
1633 struct pnv_ioda_pe *pe;
1634 u64 end, mask;
1635
1636 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1637 return 0;
1638
1639 pe = &phb->ioda.pe_array[pdn->pe_number];
1640 if (!pe->tce_bypass_enabled)
1641 return __dma_get_required_mask(&pdev->dev);
1642
1643
1644 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1645 mask = 1ULL << (fls64(end) - 1);
1646 mask += mask - 1;
1647
1648 return mask;
1649}
1650
Gavin Shandff4a392014-07-15 17:00:55 +10001651static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001652 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001653{
1654 struct pci_dev *dev;
1655
1656 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001657 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001658 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001659 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001660
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001661 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001662 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001663 }
1664}
1665
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001666static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
1667 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001668{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001669 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1670 &tbl->it_group_list, struct iommu_table_group_link,
1671 next);
1672 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001673 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001674 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001675 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1676 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001677 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001678 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001679
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001680 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1681 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1682 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001683
1684 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1685 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001686 start <<= shift;
1687 end <<= shift;
1688 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001689 start |= tbl->it_busno;
1690 end |= tbl->it_busno;
1691 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1692 /* p7ioc-style invalidation, 2 TCEs per write */
1693 start |= (1ull << 63);
1694 end |= (1ull << 63);
1695 inc = 16;
1696 } else {
1697 /* Default (older HW) */
1698 inc = 128;
1699 }
1700
1701 end |= inc - 1; /* round up end to be different than start */
1702
1703 mb(); /* Ensure above stores are visible */
1704 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001705 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001706 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001707 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001708 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001709 start += inc;
1710 }
1711
1712 /*
1713 * The iommu layer will do another mb() for us on build()
1714 * and we don't care on free()
1715 */
1716}
1717
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001718static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1719 long npages, unsigned long uaddr,
1720 enum dma_data_direction direction,
1721 struct dma_attrs *attrs)
1722{
1723 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1724 attrs);
1725
1726 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1727 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1728
1729 return ret;
1730}
1731
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001732#ifdef CONFIG_IOMMU_API
1733static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1734 unsigned long *hpa, enum dma_data_direction *direction)
1735{
1736 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1737
1738 if (!ret && (tbl->it_type &
1739 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1740 pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);
1741
1742 return ret;
1743}
1744#endif
1745
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001746static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1747 long npages)
1748{
1749 pnv_tce_free(tbl, index, npages);
1750
1751 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1752 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1753}
1754
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001755static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001756 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001757#ifdef CONFIG_IOMMU_API
1758 .exchange = pnv_ioda1_tce_xchg,
1759#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001760 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001761 .get = pnv_tce_get,
1762};
1763
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001764static inline void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_ioda_pe *pe)
1765{
1766 /* 01xb - invalidate TCEs that match the specified PE# */
1767 unsigned long val = (0x4ull << 60) | (pe->pe_number & 0xFF);
1768 struct pnv_phb *phb = pe->phb;
1769
1770 if (!phb->ioda.tce_inval_reg)
1771 return;
1772
1773 mb(); /* Ensure above stores are visible */
1774 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1775}
1776
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001777static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
1778 __be64 __iomem *invalidate, unsigned shift,
1779 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001780{
1781 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001782
1783 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001784 start = 0x2ull << 60;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001785 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001786 end = start;
1787
1788 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001789 start |= (index << shift);
1790 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001791 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001792 mb();
1793
1794 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001795 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001796 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001797 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001798 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001799 start += inc;
1800 }
1801}
1802
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001803static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1804 unsigned long index, unsigned long npages, bool rm)
1805{
1806 struct iommu_table_group_link *tgl;
1807
1808 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
1809 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1810 struct pnv_ioda_pe, table_group);
1811 __be64 __iomem *invalidate = rm ?
1812 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1813 pe->phb->ioda.tce_inval_reg;
1814
1815 pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
1816 invalidate, tbl->it_page_shift,
1817 index, npages);
1818 }
1819}
1820
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001821static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1822 long npages, unsigned long uaddr,
1823 enum dma_data_direction direction,
1824 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001825{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001826 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1827 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001828
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001829 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1830 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1831
1832 return ret;
1833}
1834
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001835#ifdef CONFIG_IOMMU_API
1836static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1837 unsigned long *hpa, enum dma_data_direction *direction)
1838{
1839 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1840
1841 if (!ret && (tbl->it_type &
1842 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1843 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1844
1845 return ret;
1846}
1847#endif
1848
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001849static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1850 long npages)
1851{
1852 pnv_tce_free(tbl, index, npages);
1853
1854 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1855 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001856}
1857
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001858static void pnv_ioda2_table_free(struct iommu_table *tbl)
1859{
1860 pnv_pci_ioda2_table_free_pages(tbl);
1861 iommu_free_table(tbl, "pnv");
1862}
1863
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001864static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001865 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001866#ifdef CONFIG_IOMMU_API
1867 .exchange = pnv_ioda2_tce_xchg,
1868#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001869 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001870 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001871 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001872};
1873
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001874static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1875 struct pnv_ioda_pe *pe, unsigned int base,
1876 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001877{
1878
1879 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001880 struct iommu_table *tbl;
1881 unsigned int i;
1882 int64_t rc;
1883 void *addr;
1884
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001885 /* XXX FIXME: Handle 64-bit only DMA devices */
1886 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1887 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1888
1889 /* We shouldn't already have a 32-bit DMA associated */
1890 if (WARN_ON(pe->tce32_seg >= 0))
1891 return;
1892
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001893 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001894 iommu_register_group(&pe->table_group, phb->hose->global_number,
1895 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001896 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001897
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001898 /* Grab a 32-bit TCE table */
1899 pe->tce32_seg = base;
1900 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1901 (base << 28), ((base + segs) << 28) - 1);
1902
1903 /* XXX Currently, we allocate one big contiguous table for the
1904 * TCEs. We only really need one chunk per 256M of TCE space
1905 * (ie per segment) but that's an optimization for later, it
1906 * requires some added smarts with our get/put_tce implementation
1907 */
1908 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1909 get_order(TCE32_TABLE_SIZE * segs));
1910 if (!tce_mem) {
1911 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1912 goto fail;
1913 }
1914 addr = page_address(tce_mem);
1915 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1916
1917 /* Configure HW */
1918 for (i = 0; i < segs; i++) {
1919 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1920 pe->pe_number,
1921 base + i, 1,
1922 __pa(addr) + TCE32_TABLE_SIZE * i,
1923 TCE32_TABLE_SIZE, 0x1000);
1924 if (rc) {
1925 pe_err(pe, " Failed to configure 32-bit TCE table,"
1926 " err %ld\n", rc);
1927 goto fail;
1928 }
1929 }
1930
1931 /* Setup linux iommu table */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001932 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001933 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001934
1935 /* OPAL variant of P7IOC SW invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001936 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10001937 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1938 TCE_PCI_SWINV_FREE |
1939 TCE_PCI_SWINV_PAIR);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001940
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001941 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001942 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
1943 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001944 iommu_init_table(tbl, phb->hose->node);
1945
Wei Yang781a8682015-03-25 16:23:57 +08001946 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001947 /*
1948 * Setting table base here only for carrying iommu_group
1949 * further down to let iommu_add_device() do the job.
1950 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
1951 */
1952 set_iommu_table_base(&pe->pdev->dev, tbl);
1953 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001954 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001955 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001956
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001957 return;
1958 fail:
1959 /* XXX Failure: Try to fallback to 64-bit only ? */
1960 if (pe->tce32_seg >= 0)
1961 pe->tce32_seg = -1;
1962 if (tce_mem)
1963 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001964 if (tbl) {
1965 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
1966 iommu_free_table(tbl, "pnv");
1967 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001968}
1969
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001970static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
1971 int num, struct iommu_table *tbl)
1972{
1973 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1974 table_group);
1975 struct pnv_phb *phb = pe->phb;
1976 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001977 const unsigned long size = tbl->it_indirect_levels ?
1978 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001979 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
1980 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
1981
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001982 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001983 start_addr, start_addr + win_size - 1,
1984 IOMMU_PAGE_SIZE(tbl));
1985
1986 /*
1987 * Map TCE table through TVT. The TVE index is the PE number
1988 * shifted by 1 bit for 32-bits DMA space.
1989 */
1990 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1991 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001992 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001993 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001994 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001995 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001996 IOMMU_PAGE_SIZE(tbl));
1997 if (rc) {
1998 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
1999 return rc;
2000 }
2001
2002 pnv_pci_link_table_and_group(phb->hose->node, num,
2003 tbl, &pe->table_group);
2004 pnv_pci_ioda2_tce_invalidate_entire(pe);
2005
2006 return 0;
2007}
2008
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002009static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002010{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002011 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2012 int64_t rc;
2013
2014 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2015 if (enable) {
2016 phys_addr_t top = memblock_end_of_DRAM();
2017
2018 top = roundup_pow_of_two(top);
2019 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2020 pe->pe_number,
2021 window_id,
2022 pe->tce_bypass_base,
2023 top);
2024 } else {
2025 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2026 pe->pe_number,
2027 window_id,
2028 pe->tce_bypass_base,
2029 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002030 }
2031 if (rc)
2032 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2033 else
2034 pe->tce_bypass_enabled = enable;
2035}
2036
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002037static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2038 __u32 page_shift, __u64 window_size, __u32 levels,
2039 struct iommu_table *tbl);
2040
2041static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2042 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2043 struct iommu_table **ptbl)
2044{
2045 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2046 table_group);
2047 int nid = pe->phb->hose->node;
2048 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2049 long ret;
2050 struct iommu_table *tbl;
2051
2052 tbl = pnv_pci_table_alloc(nid);
2053 if (!tbl)
2054 return -ENOMEM;
2055
2056 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2057 bus_offset, page_shift, window_size,
2058 levels, tbl);
2059 if (ret) {
2060 iommu_free_table(tbl, "pnv");
2061 return ret;
2062 }
2063
2064 tbl->it_ops = &pnv_ioda2_iommu_ops;
2065 if (pe->phb->ioda.tce_inval_reg)
2066 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2067
2068 *ptbl = tbl;
2069
2070 return 0;
2071}
2072
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002073static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2074{
2075 struct iommu_table *tbl = NULL;
2076 long rc;
2077
2078 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2079 IOMMU_PAGE_SHIFT_4K,
2080 pe->table_group.tce32_size,
2081 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2082 if (rc) {
2083 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2084 rc);
2085 return rc;
2086 }
2087
2088 iommu_init_table(tbl, pe->phb->hose->node);
2089
2090 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2091 if (rc) {
2092 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2093 rc);
2094 pnv_ioda2_table_free(tbl);
2095 return rc;
2096 }
2097
2098 if (!pnv_iommu_bypass_disabled)
2099 pnv_pci_ioda2_set_bypass(pe, true);
2100
2101 /* OPAL variant of PHB3 invalidated TCEs */
2102 if (pe->phb->ioda.tce_inval_reg)
2103 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2104
2105 /*
2106 * Setting table base here only for carrying iommu_group
2107 * further down to let iommu_add_device() do the job.
2108 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2109 */
2110 if (pe->flags & PNV_IODA_PE_DEV)
2111 set_iommu_table_base(&pe->pdev->dev, tbl);
2112
2113 return 0;
2114}
2115
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002116#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2117static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2118 int num)
2119{
2120 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2121 table_group);
2122 struct pnv_phb *phb = pe->phb;
2123 long ret;
2124
2125 pe_info(pe, "Removing DMA window #%d\n", num);
2126
2127 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2128 (pe->pe_number << 1) + num,
2129 0/* levels */, 0/* table address */,
2130 0/* table size */, 0/* page size */);
2131 if (ret)
2132 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2133 else
2134 pnv_pci_ioda2_tce_invalidate_entire(pe);
2135
2136 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2137
2138 return ret;
2139}
2140#endif
2141
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002142#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002143static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2144 __u64 window_size, __u32 levels)
2145{
2146 unsigned long bytes = 0;
2147 const unsigned window_shift = ilog2(window_size);
2148 unsigned entries_shift = window_shift - page_shift;
2149 unsigned table_shift = entries_shift + 3;
2150 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2151 unsigned long direct_table_size;
2152
2153 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2154 (window_size > memory_hotplug_max()) ||
2155 !is_power_of_2(window_size))
2156 return 0;
2157
2158 /* Calculate a direct table size from window_size and levels */
2159 entries_shift = (entries_shift + levels - 1) / levels;
2160 table_shift = entries_shift + 3;
2161 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2162 direct_table_size = 1UL << table_shift;
2163
2164 for ( ; levels; --levels) {
2165 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2166
2167 tce_table_size /= direct_table_size;
2168 tce_table_size <<= 3;
2169 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2170 }
2171
2172 return bytes;
2173}
2174
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002175static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002176{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002177 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2178 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002179 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2180 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002181
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002182 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002183 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2184 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002185}
2186
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002187static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2188{
2189 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2190 table_group);
2191
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002192 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002193}
2194
2195static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002196 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002197 .create_table = pnv_pci_ioda2_create_table,
2198 .set_window = pnv_pci_ioda2_set_window,
2199 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002200 .take_ownership = pnv_ioda2_take_ownership,
2201 .release_ownership = pnv_ioda2_release_ownership,
2202};
2203#endif
2204
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002205static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2206{
2207 const __be64 *swinvp;
2208
2209 /* OPAL variant of PHB3 invalidated TCEs */
2210 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2211 if (!swinvp)
2212 return;
2213
2214 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2215 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2216}
2217
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002218static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2219 unsigned levels, unsigned long limit,
2220 unsigned long *current_offset)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002221{
2222 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002223 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002224 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002225 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2226 unsigned entries = 1UL << (shift - 3);
2227 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002228
2229 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2230 if (!tce_mem) {
2231 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2232 return NULL;
2233 }
2234 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002235 memset(addr, 0, allocated);
2236
2237 --levels;
2238 if (!levels) {
2239 *current_offset += allocated;
2240 return addr;
2241 }
2242
2243 for (i = 0; i < entries; ++i) {
2244 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
2245 levels, limit, current_offset);
2246 if (!tmp)
2247 break;
2248
2249 addr[i] = cpu_to_be64(__pa(tmp) |
2250 TCE_PCI_READ | TCE_PCI_WRITE);
2251
2252 if (*current_offset >= limit)
2253 break;
2254 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002255
2256 return addr;
2257}
2258
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002259static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2260 unsigned long size, unsigned level);
2261
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002262static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002263 __u32 page_shift, __u64 window_size, __u32 levels,
2264 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002265{
2266 void *addr;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002267 unsigned long offset = 0, level_shift;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002268 const unsigned window_shift = ilog2(window_size);
2269 unsigned entries_shift = window_shift - page_shift;
2270 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2271 const unsigned long tce_table_size = 1UL << table_shift;
2272
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002273 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2274 return -EINVAL;
2275
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002276 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2277 return -EINVAL;
2278
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002279 /* Adjust direct table size from window_size and levels */
2280 entries_shift = (entries_shift + levels - 1) / levels;
2281 level_shift = entries_shift + 3;
2282 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2283
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002284 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002285 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
2286 levels, tce_table_size, &offset);
2287
2288 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002289 if (!addr)
2290 return -ENOMEM;
2291
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002292 /*
2293 * First level was allocated but some lower level failed as
2294 * we did not allocate as much as we wanted,
2295 * release partially allocated table.
2296 */
2297 if (offset < tce_table_size) {
2298 pnv_pci_ioda2_table_do_free_pages(addr,
2299 1ULL << (level_shift - 3), levels - 1);
2300 return -ENOMEM;
2301 }
2302
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002303 /* Setup linux iommu table */
2304 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2305 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002306 tbl->it_level_size = 1ULL << (level_shift - 3);
2307 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002308 tbl->it_allocated_size = offset;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002309
2310 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2311 window_size, tce_table_size, bus_offset);
2312
2313 return 0;
2314}
2315
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002316static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2317 unsigned long size, unsigned level)
2318{
2319 const unsigned long addr_ul = (unsigned long) addr &
2320 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2321
2322 if (level) {
2323 long i;
2324 u64 *tmp = (u64 *) addr_ul;
2325
2326 for (i = 0; i < size; ++i) {
2327 unsigned long hpa = be64_to_cpu(tmp[i]);
2328
2329 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2330 continue;
2331
2332 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2333 level - 1);
2334 }
2335 }
2336
2337 free_pages(addr_ul, get_order(size << 3));
2338}
2339
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002340static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2341{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002342 const unsigned long size = tbl->it_indirect_levels ?
2343 tbl->it_level_size : tbl->it_size;
2344
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002345 if (!tbl->it_size)
2346 return;
2347
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002348 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2349 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002350}
2351
Gavin Shan373f5652013-04-25 19:21:01 +00002352static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2353 struct pnv_ioda_pe *pe)
2354{
Gavin Shan373f5652013-04-25 19:21:01 +00002355 int64_t rc;
2356
2357 /* We shouldn't already have a 32-bit DMA associated */
2358 if (WARN_ON(pe->tce32_seg >= 0))
2359 return;
2360
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002361 /* TVE #1 is selected by PCI address bit 59 */
2362 pe->tce_bypass_base = 1ull << 59;
2363
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002364 iommu_register_group(&pe->table_group, phb->hose->global_number,
2365 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002366
Gavin Shan373f5652013-04-25 19:21:01 +00002367 /* The PE will reserve all possible 32-bits space */
2368 pe->tce32_seg = 0;
Gavin Shan373f5652013-04-25 19:21:01 +00002369 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002370 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002371
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002372 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002373 pe->table_group.tce32_start = 0;
2374 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2375 pe->table_group.max_dynamic_windows_supported =
2376 IOMMU_TABLE_GROUP_MAX_TABLES;
2377 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2378 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002379#ifdef CONFIG_IOMMU_API
2380 pe->table_group.ops = &pnv_pci_ioda2_ops;
2381#endif
2382
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002383 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan373f5652013-04-25 19:21:01 +00002384 if (rc) {
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002385 if (pe->tce32_seg >= 0)
2386 pe->tce32_seg = -1;
2387 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002388 }
2389
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002390 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002391 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002392 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002393 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002394}
2395
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002396static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002397{
2398 struct pci_controller *hose = phb->hose;
2399 unsigned int residual, remaining, segs, tw, base;
2400 struct pnv_ioda_pe *pe;
2401
2402 /* If we have more PE# than segments available, hand out one
2403 * per PE until we run out and let the rest fail. If not,
2404 * then we assign at least one segment per PE, plus more based
2405 * on the amount of devices under that PE
2406 */
2407 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
2408 residual = 0;
2409 else
2410 residual = phb->ioda.tce32_count -
2411 phb->ioda.dma_pe_count;
2412
2413 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
2414 hose->global_number, phb->ioda.tce32_count);
2415 pr_info("PCI: %d PE# for a total weight of %d\n",
2416 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
2417
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002418 pnv_pci_ioda_setup_opal_tce_kill(phb);
2419
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002420 /* Walk our PE list and configure their DMA segments, hand them
2421 * out one base segment plus any residual segments based on
2422 * weight
2423 */
2424 remaining = phb->ioda.tce32_count;
2425 tw = phb->ioda.dma_weight;
2426 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00002427 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002428 if (!pe->dma_weight)
2429 continue;
2430 if (!remaining) {
2431 pe_warn(pe, "No DMA32 resources available\n");
2432 continue;
2433 }
2434 segs = 1;
2435 if (residual) {
2436 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
2437 if (segs > remaining)
2438 segs = remaining;
2439 }
Gavin Shan373f5652013-04-25 19:21:01 +00002440
2441 /*
2442 * For IODA2 compliant PHB3, we needn't care about the weight.
2443 * The all available 32-bits DMA space will be assigned to
2444 * the specific PE.
2445 */
2446 if (phb->type == PNV_PHB_IODA1) {
2447 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
2448 pe->dma_weight, segs);
2449 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
2450 } else {
2451 pe_info(pe, "Assign DMA32 space\n");
2452 segs = 0;
2453 pnv_pci_ioda2_setup_dma_pe(phb, pe);
2454 }
2455
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002456 remaining -= segs;
2457 base += segs;
2458 }
2459}
2460
2461#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002462static void pnv_ioda2_msi_eoi(struct irq_data *d)
2463{
2464 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2465 struct irq_chip *chip = irq_data_get_irq_chip(d);
2466 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2467 ioda.irq_chip);
2468 int64_t rc;
2469
2470 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2471 WARN_ON_ONCE(rc);
2472
2473 icp_native_eoi(d);
2474}
2475
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002476
2477static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2478{
2479 struct irq_data *idata;
2480 struct irq_chip *ichip;
2481
2482 if (phb->type != PNV_PHB_IODA2)
2483 return;
2484
2485 if (!phb->ioda.irq_chip_init) {
2486 /*
2487 * First time we setup an MSI IRQ, we need to setup the
2488 * corresponding IRQ chip to route correctly.
2489 */
2490 idata = irq_get_irq_data(virq);
2491 ichip = irq_data_get_irq_chip(idata);
2492 phb->ioda.irq_chip_init = 1;
2493 phb->ioda.irq_chip = *ichip;
2494 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2495 }
2496 irq_set_chip(virq, &phb->ioda.irq_chip);
2497}
2498
Ian Munsie80c49c72014-10-08 19:54:57 +11002499#ifdef CONFIG_CXL_BASE
2500
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002501struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002502{
2503 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2504
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002505 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002506}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002507EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002508
Ryan Grimm1212aa12015-01-19 11:52:50 -06002509int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002510{
2511 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2512 struct pnv_phb *phb = hose->private_data;
2513 struct pnv_ioda_pe *pe;
2514 int rc;
2515
2516 pe = pnv_ioda_get_pe(dev);
2517 if (!pe)
2518 return -ENODEV;
2519
2520 pe_info(pe, "Switching PHB to CXL\n");
2521
Ryan Grimm1212aa12015-01-19 11:52:50 -06002522 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11002523 if (rc)
2524 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2525
2526 return rc;
2527}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002528EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002529
2530/* Find PHB for cxl dev and allocate MSI hwirqs?
2531 * Returns the absolute hardware IRQ number
2532 */
2533int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2534{
2535 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2536 struct pnv_phb *phb = hose->private_data;
2537 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2538
2539 if (hwirq < 0) {
2540 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2541 return -ENOSPC;
2542 }
2543
2544 return phb->msi_base + hwirq;
2545}
2546EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2547
2548void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2549{
2550 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2551 struct pnv_phb *phb = hose->private_data;
2552
2553 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2554}
2555EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2556
2557void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2558 struct pci_dev *dev)
2559{
2560 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2561 struct pnv_phb *phb = hose->private_data;
2562 int i, hwirq;
2563
2564 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2565 if (!irqs->range[i])
2566 continue;
2567 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2568 i, irqs->offset[i],
2569 irqs->range[i]);
2570 hwirq = irqs->offset[i] - phb->msi_base;
2571 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2572 irqs->range[i]);
2573 }
2574}
2575EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2576
2577int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2578 struct pci_dev *dev, int num)
2579{
2580 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2581 struct pnv_phb *phb = hose->private_data;
2582 int i, hwirq, try;
2583
2584 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2585
2586 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2587 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2588 try = num;
2589 while (try) {
2590 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2591 if (hwirq >= 0)
2592 break;
2593 try /= 2;
2594 }
2595 if (!try)
2596 goto fail;
2597
2598 irqs->offset[i] = phb->msi_base + hwirq;
2599 irqs->range[i] = try;
2600 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2601 i, irqs->offset[i], irqs->range[i]);
2602 num -= try;
2603 }
2604 if (num)
2605 goto fail;
2606
2607 return 0;
2608fail:
2609 pnv_cxl_release_hwirq_ranges(irqs, dev);
2610 return -ENOSPC;
2611}
2612EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2613
2614int pnv_cxl_get_irq_count(struct pci_dev *dev)
2615{
2616 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2617 struct pnv_phb *phb = hose->private_data;
2618
2619 return phb->msi_bmp.irq_count;
2620}
2621EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2622
2623int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2624 unsigned int virq)
2625{
2626 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2627 struct pnv_phb *phb = hose->private_data;
2628 unsigned int xive_num = hwirq - phb->msi_base;
2629 struct pnv_ioda_pe *pe;
2630 int rc;
2631
2632 if (!(pe = pnv_ioda_get_pe(dev)))
2633 return -ENODEV;
2634
2635 /* Assign XIVE to PE */
2636 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2637 if (rc) {
2638 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2639 "hwirq 0x%x XIVE 0x%x PE\n",
2640 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2641 return -EIO;
2642 }
2643 set_msi_irq_chip(phb, virq);
2644
2645 return 0;
2646}
2647EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2648#endif
2649
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002650static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002651 unsigned int hwirq, unsigned int virq,
2652 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002653{
2654 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2655 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002656 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002657 int rc;
2658
2659 /* No PE assigned ? bail out ... no MSI for you ! */
2660 if (pe == NULL)
2661 return -ENXIO;
2662
2663 /* Check if we have an MVE */
2664 if (pe->mve_number < 0)
2665 return -ENXIO;
2666
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002667 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002668 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002669 is_64 = 0;
2670
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002671 /* Assign XIVE to PE */
2672 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2673 if (rc) {
2674 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2675 pci_name(dev), rc, xive_num);
2676 return -EIO;
2677 }
2678
2679 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002680 __be64 addr64;
2681
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002682 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2683 &addr64, &data);
2684 if (rc) {
2685 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2686 pci_name(dev), rc);
2687 return -EIO;
2688 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002689 msg->address_hi = be64_to_cpu(addr64) >> 32;
2690 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002691 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002692 __be32 addr32;
2693
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002694 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2695 &addr32, &data);
2696 if (rc) {
2697 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2698 pci_name(dev), rc);
2699 return -EIO;
2700 }
2701 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002702 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002703 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002704 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002705
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002706 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002707
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002708 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2709 " address=%x_%08x data=%x PE# %d\n",
2710 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2711 msg->address_hi, msg->address_lo, data, pe->pe_number);
2712
2713 return 0;
2714}
2715
2716static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2717{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002718 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002719 const __be32 *prop = of_get_property(phb->hose->dn,
2720 "ibm,opal-msi-ranges", NULL);
2721 if (!prop) {
2722 /* BML Fallback */
2723 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2724 }
2725 if (!prop)
2726 return;
2727
2728 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002729 count = be32_to_cpup(prop + 1);
2730 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002731 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2732 phb->hose->global_number);
2733 return;
2734 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002735
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002736 phb->msi_setup = pnv_pci_ioda_msi_setup;
2737 phb->msi32_support = 1;
2738 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002739 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002740}
2741#else
2742static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2743#endif /* CONFIG_PCI_MSI */
2744
Wei Yang6e628c72015-03-25 16:23:55 +08002745#ifdef CONFIG_PCI_IOV
2746static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2747{
2748 struct pci_controller *hose;
2749 struct pnv_phb *phb;
2750 struct resource *res;
2751 int i;
2752 resource_size_t size;
2753 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002754 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002755
2756 if (!pdev->is_physfn || pdev->is_added)
2757 return;
2758
2759 hose = pci_bus_to_host(pdev->bus);
2760 phb = hose->private_data;
2761
2762 pdn = pci_get_pdn(pdev);
2763 pdn->vfs_expanded = 0;
2764
Wei Yang5b88ec22015-03-25 16:23:58 +08002765 total_vfs = pci_sriov_get_totalvfs(pdev);
2766 pdn->m64_per_iov = 1;
2767 mul = phb->ioda.total_pe;
2768
2769 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2770 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2771 if (!res->flags || res->parent)
2772 continue;
2773 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2774 dev_warn(&pdev->dev, " non M64 VF BAR%d: %pR\n",
2775 i, res);
2776 continue;
2777 }
2778
2779 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
2780
2781 /* bigger than 64M */
2782 if (size > (1 << 26)) {
2783 dev_info(&pdev->dev, "PowerNV: VF BAR%d: %pR IOV size is bigger than 64M, roundup power2\n",
2784 i, res);
2785 pdn->m64_per_iov = M64_PER_IOV;
2786 mul = roundup_pow_of_two(total_vfs);
2787 break;
2788 }
2789 }
2790
Wei Yang6e628c72015-03-25 16:23:55 +08002791 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2792 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2793 if (!res->flags || res->parent)
2794 continue;
2795 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2796 dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
2797 i, res);
2798 continue;
2799 }
2800
2801 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
2802 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002803 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08002804 dev_dbg(&pdev->dev, " %pR\n", res);
2805 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08002806 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08002807 }
Wei Yang5b88ec22015-03-25 16:23:58 +08002808 pdn->vfs_expanded = mul;
Wei Yang6e628c72015-03-25 16:23:55 +08002809}
2810#endif /* CONFIG_PCI_IOV */
2811
Gavin Shan11685be2012-08-20 03:49:16 +00002812/*
2813 * This function is supposed to be called on basis of PE from top
2814 * to bottom style. So the the I/O or MMIO segment assigned to
2815 * parent PE could be overrided by its child PEs if necessary.
2816 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002817static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
2818 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00002819{
2820 struct pnv_phb *phb = hose->private_data;
2821 struct pci_bus_region region;
2822 struct resource *res;
2823 int i, index;
2824 int rc;
2825
2826 /*
2827 * NOTE: We only care PCI bus based PE for now. For PCI
2828 * device based PE, for example SRIOV sensitive VF should
2829 * be figured out later.
2830 */
2831 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2832
2833 pci_bus_for_each_resource(pe->pbus, res, i) {
2834 if (!res || !res->flags ||
2835 res->start > res->end)
2836 continue;
2837
2838 if (res->flags & IORESOURCE_IO) {
2839 region.start = res->start - phb->ioda.io_pci_base;
2840 region.end = res->end - phb->ioda.io_pci_base;
2841 index = region.start / phb->ioda.io_segsize;
2842
2843 while (index < phb->ioda.total_pe &&
2844 region.start <= region.end) {
2845 phb->ioda.io_segmap[index] = pe->pe_number;
2846 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2847 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2848 if (rc != OPAL_SUCCESS) {
2849 pr_err("%s: OPAL error %d when mapping IO "
2850 "segment #%d to PE#%d\n",
2851 __func__, rc, index, pe->pe_number);
2852 break;
2853 }
2854
2855 region.start += phb->ioda.io_segsize;
2856 index++;
2857 }
Gavin Shan027fa022015-03-27 11:29:00 +11002858 } else if ((res->flags & IORESOURCE_MEM) &&
2859 !pnv_pci_is_mem_pref_64(res->flags)) {
Gavin Shan11685be2012-08-20 03:49:16 +00002860 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002861 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002862 phb->ioda.m32_pci_base;
2863 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002864 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002865 phb->ioda.m32_pci_base;
2866 index = region.start / phb->ioda.m32_segsize;
2867
2868 while (index < phb->ioda.total_pe &&
2869 region.start <= region.end) {
2870 phb->ioda.m32_segmap[index] = pe->pe_number;
2871 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2872 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2873 if (rc != OPAL_SUCCESS) {
2874 pr_err("%s: OPAL error %d when mapping M32 "
2875 "segment#%d to PE#%d",
2876 __func__, rc, index, pe->pe_number);
2877 break;
2878 }
2879
2880 region.start += phb->ioda.m32_segsize;
2881 index++;
2882 }
2883 }
2884 }
2885}
2886
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002887static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00002888{
2889 struct pci_controller *tmp, *hose;
2890 struct pnv_phb *phb;
2891 struct pnv_ioda_pe *pe;
2892
2893 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2894 phb = hose->private_data;
2895 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2896 pnv_ioda_setup_pe_seg(hose, pe);
2897 }
2898 }
2899}
2900
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002901static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00002902{
2903 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00002904 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00002905
2906 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2907 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00002908
2909 /* Mark the PHB initialization done */
2910 phb = hose->private_data;
2911 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00002912 }
2913}
2914
Gavin Shan37c367f2013-06-20 18:13:25 +08002915static void pnv_pci_ioda_create_dbgfs(void)
2916{
2917#ifdef CONFIG_DEBUG_FS
2918 struct pci_controller *hose, *tmp;
2919 struct pnv_phb *phb;
2920 char name[16];
2921
2922 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2923 phb = hose->private_data;
2924
2925 sprintf(name, "PCI%04x", hose->global_number);
2926 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
2927 if (!phb->dbgfs)
2928 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
2929 __func__, hose->global_number);
2930 }
2931#endif /* CONFIG_DEBUG_FS */
2932}
2933
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002934static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00002935{
2936 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00002937 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00002938 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002939
Gavin Shan37c367f2013-06-20 18:13:25 +08002940 pnv_pci_ioda_create_dbgfs();
2941
Gavin Shane9cc17d2013-06-20 13:21:14 +08002942#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08002943 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04002944 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002945#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00002946}
2947
Gavin Shan271fd032012-09-11 16:59:47 -06002948/*
2949 * Returns the alignment for I/O or memory windows for P2P
2950 * bridges. That actually depends on how PEs are segmented.
2951 * For now, we return I/O or M32 segment size for PE sensitive
2952 * P2P bridges. Otherwise, the default values (4KiB for I/O,
2953 * 1MiB for memory) will be returned.
2954 *
2955 * The current PCI bus might be put into one PE, which was
2956 * create against the parent PCI bridge. For that case, we
2957 * needn't enlarge the alignment so that we can save some
2958 * resources.
2959 */
2960static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2961 unsigned long type)
2962{
2963 struct pci_dev *bridge;
2964 struct pci_controller *hose = pci_bus_to_host(bus);
2965 struct pnv_phb *phb = hose->private_data;
2966 int num_pci_bridges = 0;
2967
2968 bridge = bus->self;
2969 while (bridge) {
2970 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2971 num_pci_bridges++;
2972 if (num_pci_bridges >= 2)
2973 return 1;
2974 }
2975
2976 bridge = bridge->bus->self;
2977 }
2978
Guo Chao262af552014-07-21 14:42:30 +10002979 /* We fail back to M32 if M64 isn't supported */
2980 if (phb->ioda.m64_segsize &&
2981 pnv_pci_is_mem_pref_64(type))
2982 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06002983 if (type & IORESOURCE_MEM)
2984 return phb->ioda.m32_segsize;
2985
2986 return phb->ioda.io_segsize;
2987}
2988
Wei Yang5350ab32015-03-25 16:23:56 +08002989#ifdef CONFIG_PCI_IOV
2990static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
2991 int resno)
2992{
2993 struct pci_dn *pdn = pci_get_pdn(pdev);
2994 resource_size_t align, iov_align;
2995
2996 iov_align = resource_size(&pdev->resource[resno]);
2997 if (iov_align)
2998 return iov_align;
2999
3000 align = pci_iov_resource_size(pdev, resno);
3001 if (pdn->vfs_expanded)
3002 return pdn->vfs_expanded * align;
3003
3004 return align;
3005}
3006#endif /* CONFIG_PCI_IOV */
3007
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003008/* Prevent enabling devices for which we couldn't properly
3009 * assign a PE
3010 */
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003011static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003012{
Gavin Shandb1266c2012-08-20 03:49:18 +00003013 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3014 struct pnv_phb *phb = hose->private_data;
3015 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003016
Gavin Shandb1266c2012-08-20 03:49:18 +00003017 /* The function is probably called while the PEs have
3018 * not be created yet. For example, resource reassignment
3019 * during PCI probe period. We just skip the check if
3020 * PEs isn't ready.
3021 */
3022 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003023 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003024
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003025 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003026 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003027 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003028
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003029 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003030}
3031
3032static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
3033 u32 devfn)
3034{
3035 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
3036}
3037
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003038static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003039{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003040 struct pnv_phb *phb = hose->private_data;
3041
Gavin Shand1a85ee2014-09-30 12:39:05 +10003042 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003043 OPAL_ASSERT_RESET);
3044}
3045
Daniel Axtens92ae0352015-04-28 15:12:05 +10003046static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
3047 .dma_dev_setup = pnv_pci_dma_dev_setup,
3048#ifdef CONFIG_PCI_MSI
3049 .setup_msi_irqs = pnv_setup_msi_irqs,
3050 .teardown_msi_irqs = pnv_teardown_msi_irqs,
3051#endif
3052 .enable_device_hook = pnv_pci_enable_device_hook,
3053 .window_alignment = pnv_pci_window_alignment,
3054 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
Daniel Axtens763d2d82015-04-28 15:12:07 +10003055 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003056 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003057};
3058
Anton Blancharde51df2c2014-08-20 08:55:18 +10003059static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3060 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003061{
3062 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003063 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08003064 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10003065 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003066 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003067 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003068 u64 phb_id;
3069 void *aux;
3070 long rc;
3071
Gavin Shan58d714e2013-07-31 16:47:00 +08003072 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003073
3074 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3075 if (!prop64) {
3076 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3077 return;
3078 }
3079 phb_id = be64_to_cpup(prop64);
3080 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3081
Michael Ellermane39f223f2014-11-18 16:47:35 +11003082 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003083
3084 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003085 phb->hose = hose = pcibios_alloc_controller(np);
3086 if (!phb->hose) {
3087 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003088 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003089 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003090 return;
3091 }
3092
3093 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003094 prop32 = of_get_property(np, "bus-range", &len);
3095 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003096 hose->first_busno = be32_to_cpu(prop32[0]);
3097 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003098 } else {
3099 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3100 hose->first_busno = 0;
3101 hose->last_busno = 0xff;
3102 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003103 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003104 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003105 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003106 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003107 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003108
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003109 /* Detect specific models for error handling */
3110 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3111 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003112 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003113 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003114 else
3115 phb->model = PNV_PHB_MODEL_UNKNOWN;
3116
Gavin Shanaa0c0332013-04-25 19:20:57 +00003117 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003118 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003119
Gavin Shanaa0c0332013-04-25 19:20:57 +00003120 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003121 phb->regs = of_iomap(np, 0);
3122 if (phb->regs == NULL)
3123 pr_err(" Failed to map registers !\n");
3124
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003125 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08003126 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003127 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003128 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003129 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003130 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3131 if (prop32)
3132 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003133
3134 /* Parse 64-bit MMIO range */
3135 pnv_ioda_parse_m64_window(phb);
3136
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003137 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003138 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003139 phb->ioda.m32_size += 0x10000;
3140
3141 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003142 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003143 phb->ioda.io_size = hose->pci_io_size;
3144 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
3145 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3146
Gavin Shanc35d2a82013-07-31 16:47:04 +08003147 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003148 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
3149 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00003150 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003151 if (phb->type == PNV_PHB_IODA1) {
3152 iomap_off = size;
3153 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
3154 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003155 pemap_off = size;
3156 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003157 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003158 phb->ioda.pe_alloc = aux;
3159 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08003160 if (phb->type == PNV_PHB_IODA1)
3161 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003162 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08003163 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003164
Gavin Shan7ebdf952012-08-20 03:49:15 +00003165 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003166 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003167 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003168
3169 /* Calculate how many 32-bit TCE segments we have */
3170 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
3171
Gavin Shanaa0c0332013-04-25 19:20:57 +00003172#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003173 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3174 window_type,
3175 window_num,
3176 starting_real_address,
3177 starting_pci_address,
3178 segment_size);
3179#endif
3180
Guo Chao262af552014-07-21 14:42:30 +10003181 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
3182 phb->ioda.total_pe, phb->ioda.reserved_pe,
3183 phb->ioda.m32_size, phb->ioda.m32_segsize);
3184 if (phb->ioda.m64_size)
3185 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3186 phb->ioda.m64_size, phb->ioda.m64_segsize);
3187 if (phb->ioda.io_size)
3188 pr_info(" IO: 0x%x [segment=0x%x]\n",
3189 phb->ioda.io_size, phb->ioda.io_segsize);
3190
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003191
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003192 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003193 phb->get_pe_state = pnv_ioda_get_pe_state;
3194 phb->freeze_pe = pnv_ioda_freeze_pe;
3195 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003196
3197 /* Setup RID -> PE mapping function */
3198 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
3199
3200 /* Setup TCEs */
3201 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10003202 phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003203
3204 /* Setup MSI support */
3205 pnv_pci_init_ioda_msis(phb);
3206
Gavin Shanc40a4212012-08-20 03:49:20 +00003207 /*
3208 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3209 * to let the PCI core do resource assignment. It's supposed
3210 * that the PCI core will do correct I/O and MMIO alignment
3211 * for the P2P bridge bars so that each PCI bus (excluding
3212 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003213 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003214 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Daniel Axtens92ae0352015-04-28 15:12:05 +10003215 hose->controller_ops = pnv_pci_ioda_controller_ops;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003216
Wei Yang6e628c72015-03-25 16:23:55 +08003217#ifdef CONFIG_PCI_IOV
3218 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003219 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003220#endif
3221
Gavin Shanc40a4212012-08-20 03:49:20 +00003222 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003223
3224 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003225 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003226 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003227 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003228
3229 /* If we're running in kdump kerenl, the previous kerenl never
3230 * shutdown PCI devices correctly. We already got IODA table
3231 * cleaned out. So we have to issue PHB reset to stop all PCI
3232 * transactions from previous kerenl.
3233 */
3234 if (is_kdump_kernel()) {
3235 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003236 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3237 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003238 }
Guo Chao262af552014-07-21 14:42:30 +10003239
Gavin Shan9e9e8932014-11-12 13:36:05 +11003240 /* Remove M64 resource if we can't configure it successfully */
3241 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003242 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003243}
3244
Bjorn Helgaas67975002013-07-02 12:20:03 -06003245void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003246{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003247 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003248}
3249
3250void __init pnv_pci_init_ioda_hub(struct device_node *np)
3251{
3252 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003253 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003254 u64 hub_id;
3255
3256 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3257
3258 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3259 if (!prop64) {
3260 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3261 return;
3262 }
3263 hub_id = be64_to_cpup(prop64);
3264 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3265
3266 /* Count child PHBs */
3267 for_each_child_of_node(np, phbn) {
3268 /* Look for IODA1 PHBs */
3269 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003270 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003271 }
3272}