blob: 4b8d3bd72a781991241695272dc1ebd6287a741d [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 Shan96a2f922015-06-19 12:26:17 +1000232static void pnv_ioda2_reserve_dev_m64_pe(struct pci_dev *pdev,
233 unsigned long *pe_bitmap)
Guo Chao262af552014-07-21 14:42:30 +1000234{
Gavin Shan96a2f922015-06-19 12:26:17 +1000235 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
236 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000237 struct resource *r;
Gavin Shan96a2f922015-06-19 12:26:17 +1000238 resource_size_t base, sgsz, start, end;
239 int segno, i;
Guo Chao262af552014-07-21 14:42:30 +1000240
Gavin Shan96a2f922015-06-19 12:26:17 +1000241 base = phb->ioda.m64_base;
242 sgsz = phb->ioda.m64_segsize;
243 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
244 r = &pdev->resource[i];
245 if (!r->parent || !pnv_pci_is_mem_pref_64(r->flags))
246 continue;
Guo Chao262af552014-07-21 14:42:30 +1000247
Gavin Shan96a2f922015-06-19 12:26:17 +1000248 start = _ALIGN_DOWN(r->start - base, sgsz);
249 end = _ALIGN_UP(r->end - base, sgsz);
250 for (segno = start / sgsz; segno < end / sgsz; segno++) {
251 if (pe_bitmap)
252 set_bit(segno, pe_bitmap);
253 else
254 pnv_ioda_reserve_pe(phb, segno);
Guo Chao262af552014-07-21 14:42:30 +1000255 }
256 }
257}
258
Gavin Shan96a2f922015-06-19 12:26:17 +1000259static void pnv_ioda2_reserve_m64_pe(struct pci_bus *bus,
260 unsigned long *pe_bitmap,
261 bool all)
262{
263 struct pci_dev *pdev;
264
265 list_for_each_entry(pdev, &bus->devices, bus_list) {
266 pnv_ioda2_reserve_dev_m64_pe(pdev, pe_bitmap);
267
268 if (all && pdev->subordinate)
269 pnv_ioda2_reserve_m64_pe(pdev->subordinate,
270 pe_bitmap, all);
271 }
272}
273
Gavin Shan26ba2482015-06-19 12:26:19 +1000274static int pnv_ioda2_pick_m64_pe(struct pci_bus *bus, bool all)
Guo Chao262af552014-07-21 14:42:30 +1000275{
Gavin Shan26ba2482015-06-19 12:26:19 +1000276 struct pci_controller *hose = pci_bus_to_host(bus);
277 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000278 struct pnv_ioda_pe *master_pe, *pe;
279 unsigned long size, *pe_alloc;
Gavin Shan26ba2482015-06-19 12:26:19 +1000280 int i;
Guo Chao262af552014-07-21 14:42:30 +1000281
282 /* Root bus shouldn't use M64 */
283 if (pci_is_root_bus(bus))
284 return IODA_INVALID_PE;
285
Guo Chao262af552014-07-21 14:42:30 +1000286 /* Allocate bitmap */
287 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
288 pe_alloc = kzalloc(size, GFP_KERNEL);
289 if (!pe_alloc) {
290 pr_warn("%s: Out of memory !\n",
291 __func__);
292 return IODA_INVALID_PE;
293 }
294
Gavin Shan26ba2482015-06-19 12:26:19 +1000295 /* Figure out reserved PE numbers by the PE */
296 pnv_ioda2_reserve_m64_pe(bus, pe_alloc, all);
Guo Chao262af552014-07-21 14:42:30 +1000297
298 /*
299 * the current bus might not own M64 window and that's all
300 * contributed by its child buses. For the case, we needn't
301 * pick M64 dependent PE#.
302 */
303 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
304 kfree(pe_alloc);
305 return IODA_INVALID_PE;
306 }
307
308 /*
309 * Figure out the master PE and put all slave PEs to master
310 * PE's list to form compound PE.
311 */
Guo Chao262af552014-07-21 14:42:30 +1000312 master_pe = NULL;
313 i = -1;
314 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
315 phb->ioda.total_pe) {
316 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000317
318 if (!master_pe) {
319 pe->flags |= PNV_IODA_PE_MASTER;
320 INIT_LIST_HEAD(&pe->slaves);
321 master_pe = pe;
322 } else {
323 pe->flags |= PNV_IODA_PE_SLAVE;
324 pe->master = master_pe;
325 list_add_tail(&pe->list, &master_pe->slaves);
326 }
327 }
328
329 kfree(pe_alloc);
330 return master_pe->pe_number;
331}
332
333static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
334{
335 struct pci_controller *hose = phb->hose;
336 struct device_node *dn = hose->dn;
337 struct resource *res;
338 const u32 *r;
339 u64 pci_addr;
340
Gavin Shan1665c4a2014-11-12 13:36:04 +1100341 /* FIXME: Support M64 for P7IOC */
342 if (phb->type != PNV_PHB_IODA2) {
343 pr_info(" Not support M64 window\n");
344 return;
345 }
346
Guo Chao262af552014-07-21 14:42:30 +1000347 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
348 pr_info(" Firmware too old to support M64 window\n");
349 return;
350 }
351
352 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
353 if (!r) {
354 pr_info(" No <ibm,opal-m64-window> on %s\n",
355 dn->full_name);
356 return;
357 }
358
Guo Chao262af552014-07-21 14:42:30 +1000359 res = &hose->mem_resources[1];
360 res->start = of_translate_address(dn, r + 2);
361 res->end = res->start + of_read_number(r + 4, 2) - 1;
362 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
363 pci_addr = of_read_number(r, 2);
364 hose->mem_offset[1] = res->start - pci_addr;
365
366 phb->ioda.m64_size = resource_size(res);
367 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
368 phb->ioda.m64_base = pci_addr;
369
Wei Yange9863e62014-12-12 12:39:37 +0800370 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
371 res->start, res->end, pci_addr);
372
Guo Chao262af552014-07-21 14:42:30 +1000373 /* Use last M64 BAR to cover M64 window */
374 phb->ioda.m64_bar_idx = 15;
375 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shan5ef73562014-11-12 13:36:06 +1100376 phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000377 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
378}
379
Gavin Shan49dec922014-07-21 14:42:33 +1000380static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
381{
382 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
383 struct pnv_ioda_pe *slave;
384 s64 rc;
385
386 /* Fetch master PE */
387 if (pe->flags & PNV_IODA_PE_SLAVE) {
388 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100389 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
390 return;
391
Gavin Shan49dec922014-07-21 14:42:33 +1000392 pe_no = pe->pe_number;
393 }
394
395 /* Freeze master PE */
396 rc = opal_pci_eeh_freeze_set(phb->opal_id,
397 pe_no,
398 OPAL_EEH_ACTION_SET_FREEZE_ALL);
399 if (rc != OPAL_SUCCESS) {
400 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
401 __func__, rc, phb->hose->global_number, pe_no);
402 return;
403 }
404
405 /* Freeze slave PEs */
406 if (!(pe->flags & PNV_IODA_PE_MASTER))
407 return;
408
409 list_for_each_entry(slave, &pe->slaves, list) {
410 rc = opal_pci_eeh_freeze_set(phb->opal_id,
411 slave->pe_number,
412 OPAL_EEH_ACTION_SET_FREEZE_ALL);
413 if (rc != OPAL_SUCCESS)
414 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
415 __func__, rc, phb->hose->global_number,
416 slave->pe_number);
417 }
418}
419
Anton Blancharde51df2c2014-08-20 08:55:18 +1000420static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000421{
422 struct pnv_ioda_pe *pe, *slave;
423 s64 rc;
424
425 /* Find master PE */
426 pe = &phb->ioda.pe_array[pe_no];
427 if (pe->flags & PNV_IODA_PE_SLAVE) {
428 pe = pe->master;
429 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
430 pe_no = pe->pe_number;
431 }
432
433 /* Clear frozen state for master PE */
434 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
435 if (rc != OPAL_SUCCESS) {
436 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
437 __func__, rc, opt, phb->hose->global_number, pe_no);
438 return -EIO;
439 }
440
441 if (!(pe->flags & PNV_IODA_PE_MASTER))
442 return 0;
443
444 /* Clear frozen state for slave PEs */
445 list_for_each_entry(slave, &pe->slaves, list) {
446 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
447 slave->pe_number,
448 opt);
449 if (rc != OPAL_SUCCESS) {
450 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
451 __func__, rc, opt, phb->hose->global_number,
452 slave->pe_number);
453 return -EIO;
454 }
455 }
456
457 return 0;
458}
459
460static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
461{
462 struct pnv_ioda_pe *slave, *pe;
463 u8 fstate, state;
464 __be16 pcierr;
465 s64 rc;
466
467 /* Sanity check on PE number */
468 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
469 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
470
471 /*
472 * Fetch the master PE and the PE instance might be
473 * not initialized yet.
474 */
475 pe = &phb->ioda.pe_array[pe_no];
476 if (pe->flags & PNV_IODA_PE_SLAVE) {
477 pe = pe->master;
478 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
479 pe_no = pe->pe_number;
480 }
481
482 /* Check the master PE */
483 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
484 &state, &pcierr, NULL);
485 if (rc != OPAL_SUCCESS) {
486 pr_warn("%s: Failure %lld getting "
487 "PHB#%x-PE#%x state\n",
488 __func__, rc,
489 phb->hose->global_number, pe_no);
490 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
491 }
492
493 /* Check the slave PE */
494 if (!(pe->flags & PNV_IODA_PE_MASTER))
495 return state;
496
497 list_for_each_entry(slave, &pe->slaves, list) {
498 rc = opal_pci_eeh_freeze_status(phb->opal_id,
499 slave->pe_number,
500 &fstate,
501 &pcierr,
502 NULL);
503 if (rc != OPAL_SUCCESS) {
504 pr_warn("%s: Failure %lld getting "
505 "PHB#%x-PE#%x state\n",
506 __func__, rc,
507 phb->hose->global_number, slave->pe_number);
508 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
509 }
510
511 /*
512 * Override the result based on the ascending
513 * priority.
514 */
515 if (fstate > state)
516 state = fstate;
517 }
518
519 return state;
520}
521
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000522/* Currently those 2 are only used when MSIs are enabled, this will change
523 * but in the meantime, we need to protect them to avoid warnings
524 */
525#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800526static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000527{
528 struct pci_controller *hose = pci_bus_to_host(dev->bus);
529 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000530 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000531
532 if (!pdn)
533 return NULL;
534 if (pdn->pe_number == IODA_INVALID_PE)
535 return NULL;
536 return &phb->ioda.pe_array[pdn->pe_number];
537}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000538#endif /* CONFIG_PCI_MSI */
539
Gavin Shanb131a842014-11-12 13:36:08 +1100540static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
541 struct pnv_ioda_pe *parent,
542 struct pnv_ioda_pe *child,
543 bool is_add)
544{
545 const char *desc = is_add ? "adding" : "removing";
546 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
547 OPAL_REMOVE_PE_FROM_DOMAIN;
548 struct pnv_ioda_pe *slave;
549 long rc;
550
551 /* Parent PE affects child PE */
552 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
553 child->pe_number, op);
554 if (rc != OPAL_SUCCESS) {
555 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
556 rc, desc);
557 return -ENXIO;
558 }
559
560 if (!(child->flags & PNV_IODA_PE_MASTER))
561 return 0;
562
563 /* Compound case: parent PE affects slave PEs */
564 list_for_each_entry(slave, &child->slaves, list) {
565 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
566 slave->pe_number, op);
567 if (rc != OPAL_SUCCESS) {
568 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
569 rc, desc);
570 return -ENXIO;
571 }
572 }
573
574 return 0;
575}
576
577static int pnv_ioda_set_peltv(struct pnv_phb *phb,
578 struct pnv_ioda_pe *pe,
579 bool is_add)
580{
581 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800582 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100583 int ret;
584
585 /*
586 * Clear PE frozen state. If it's master PE, we need
587 * clear slave PE frozen state as well.
588 */
589 if (is_add) {
590 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
591 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
592 if (pe->flags & PNV_IODA_PE_MASTER) {
593 list_for_each_entry(slave, &pe->slaves, list)
594 opal_pci_eeh_freeze_clear(phb->opal_id,
595 slave->pe_number,
596 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
597 }
598 }
599
600 /*
601 * Associate PE in PELT. We need add the PE into the
602 * corresponding PELT-V as well. Otherwise, the error
603 * originated from the PE might contribute to other
604 * PEs.
605 */
606 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
607 if (ret)
608 return ret;
609
610 /* For compound PEs, any one affects all of them */
611 if (pe->flags & PNV_IODA_PE_MASTER) {
612 list_for_each_entry(slave, &pe->slaves, list) {
613 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
614 if (ret)
615 return ret;
616 }
617 }
618
619 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
620 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800621 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100622 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800623#ifdef CONFIG_PCI_IOV
624 else if (pe->flags & PNV_IODA_PE_VF)
Gavin Shan283e2d82015-06-22 13:45:47 +1000625 pdev = pe->parent_dev;
Wei Yang781a8682015-03-25 16:23:57 +0800626#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100627 while (pdev) {
628 struct pci_dn *pdn = pci_get_pdn(pdev);
629 struct pnv_ioda_pe *parent;
630
631 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
632 parent = &phb->ioda.pe_array[pdn->pe_number];
633 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
634 if (ret)
635 return ret;
636 }
637
638 pdev = pdev->bus->self;
639 }
640
641 return 0;
642}
643
Wei Yang781a8682015-03-25 16:23:57 +0800644#ifdef CONFIG_PCI_IOV
645static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
646{
647 struct pci_dev *parent;
648 uint8_t bcomp, dcomp, fcomp;
649 int64_t rc;
650 long rid_end, rid;
651
652 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
653 if (pe->pbus) {
654 int count;
655
656 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
657 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
658 parent = pe->pbus->self;
659 if (pe->flags & PNV_IODA_PE_BUS_ALL)
660 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
661 else
662 count = 1;
663
664 switch(count) {
665 case 1: bcomp = OpalPciBusAll; break;
666 case 2: bcomp = OpalPciBus7Bits; break;
667 case 4: bcomp = OpalPciBus6Bits; break;
668 case 8: bcomp = OpalPciBus5Bits; break;
669 case 16: bcomp = OpalPciBus4Bits; break;
670 case 32: bcomp = OpalPciBus3Bits; break;
671 default:
672 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
673 count);
674 /* Do an exact match only */
675 bcomp = OpalPciBusAll;
676 }
677 rid_end = pe->rid + (count << 8);
678 } else {
679 if (pe->flags & PNV_IODA_PE_VF)
680 parent = pe->parent_dev;
681 else
682 parent = pe->pdev->bus->self;
683 bcomp = OpalPciBusAll;
684 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
685 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
686 rid_end = pe->rid + 1;
687 }
688
689 /* Clear the reverse map */
690 for (rid = pe->rid; rid < rid_end; rid++)
691 phb->ioda.pe_rmap[rid] = 0;
692
693 /* Release from all parents PELT-V */
694 while (parent) {
695 struct pci_dn *pdn = pci_get_pdn(parent);
696 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
697 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
698 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
699 /* XXX What to do in case of error ? */
700 }
701 parent = parent->bus->self;
702 }
703
Gavin Shanf951e512015-06-23 17:01:13 +1000704 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
Wei Yang781a8682015-03-25 16:23:57 +0800705 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
706
707 /* Disassociate PE in PELT */
708 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
709 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
710 if (rc)
711 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
712 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
713 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
714 if (rc)
715 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
716
717 pe->pbus = NULL;
718 pe->pdev = NULL;
719 pe->parent_dev = NULL;
720
721 return 0;
722}
723#endif /* CONFIG_PCI_IOV */
724
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800725static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000726{
727 struct pci_dev *parent;
728 uint8_t bcomp, dcomp, fcomp;
729 long rc, rid_end, rid;
730
731 /* Bus validation ? */
732 if (pe->pbus) {
733 int count;
734
735 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
736 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
737 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000738 if (pe->flags & PNV_IODA_PE_BUS_ALL)
739 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
740 else
741 count = 1;
742
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000743 switch(count) {
744 case 1: bcomp = OpalPciBusAll; break;
745 case 2: bcomp = OpalPciBus7Bits; break;
746 case 4: bcomp = OpalPciBus6Bits; break;
747 case 8: bcomp = OpalPciBus5Bits; break;
748 case 16: bcomp = OpalPciBus4Bits; break;
749 case 32: bcomp = OpalPciBus3Bits; break;
750 default:
Wei Yang781a8682015-03-25 16:23:57 +0800751 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
752 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000753 /* Do an exact match only */
754 bcomp = OpalPciBusAll;
755 }
756 rid_end = pe->rid + (count << 8);
757 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800758#ifdef CONFIG_PCI_IOV
759 if (pe->flags & PNV_IODA_PE_VF)
760 parent = pe->parent_dev;
761 else
762#endif /* CONFIG_PCI_IOV */
763 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000764 bcomp = OpalPciBusAll;
765 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
766 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
767 rid_end = pe->rid + 1;
768 }
769
Gavin Shan631ad692013-11-04 16:32:46 +0800770 /*
771 * Associate PE in PELT. We need add the PE into the
772 * corresponding PELT-V as well. Otherwise, the error
773 * originated from the PE might contribute to other
774 * PEs.
775 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000776 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
777 bcomp, dcomp, fcomp, OPAL_MAP_PE);
778 if (rc) {
779 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
780 return -ENXIO;
781 }
Gavin Shan631ad692013-11-04 16:32:46 +0800782
Gavin Shanb131a842014-11-12 13:36:08 +1100783 /* Configure PELTV */
784 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000785
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000786 /* Setup reverse map */
787 for (rid = pe->rid; rid < rid_end; rid++)
788 phb->ioda.pe_rmap[rid] = pe->pe_number;
789
790 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100791 if (phb->type != PNV_PHB_IODA1) {
792 pe->mve_number = 0;
793 goto out;
794 }
795
796 pe->mve_number = pe->pe_number;
797 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
798 if (rc != OPAL_SUCCESS) {
799 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
800 rc, pe->mve_number);
801 pe->mve_number = -1;
802 } else {
803 rc = opal_pci_set_mve_enable(phb->opal_id,
804 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000805 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100806 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000807 rc, pe->mve_number);
808 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000809 }
Gavin Shan4773f762014-11-12 13:36:09 +1100810 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000811
Gavin Shan4773f762014-11-12 13:36:09 +1100812out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000813 return 0;
814}
815
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800816static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
817 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000818{
819 struct pnv_ioda_pe *lpe;
820
Gavin Shan7ebdf952012-08-20 03:49:15 +0000821 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000822 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000823 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000824 return;
825 }
826 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000827 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000828}
829
830static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
831{
832 /* This is quite simplistic. The "base" weight of a device
833 * is 10. 0 means no DMA is to be accounted for it.
834 */
835
836 /* If it's a bridge, no DMA */
837 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
838 return 0;
839
840 /* Reduce the weight of slow USB controllers */
841 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
842 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
843 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
844 return 3;
845
846 /* Increase the weight of RAID (includes Obsidian) */
847 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
848 return 15;
849
850 /* Default */
851 return 10;
852}
853
Wei Yang781a8682015-03-25 16:23:57 +0800854#ifdef CONFIG_PCI_IOV
855static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
856{
857 struct pci_dn *pdn = pci_get_pdn(dev);
858 int i;
859 struct resource *res, res2;
860 resource_size_t size;
861 u16 num_vfs;
862
863 if (!dev->is_physfn)
864 return -EINVAL;
865
866 /*
867 * "offset" is in VFs. The M64 windows are sized so that when they
868 * are segmented, each segment is the same size as the IOV BAR.
869 * Each segment is in a separate PE, and the high order bits of the
870 * address are the PE number. Therefore, each VF's BAR is in a
871 * separate PE, and changing the IOV BAR start address changes the
872 * range of PEs the VFs are in.
873 */
874 num_vfs = pdn->num_vfs;
875 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
876 res = &dev->resource[i + PCI_IOV_RESOURCES];
877 if (!res->flags || !res->parent)
878 continue;
879
880 if (!pnv_pci_is_mem_pref_64(res->flags))
881 continue;
882
883 /*
884 * The actual IOV BAR range is determined by the start address
885 * and the actual size for num_vfs VFs BAR. This check is to
886 * make sure that after shifting, the range will not overlap
887 * with another device.
888 */
889 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
890 res2.flags = res->flags;
891 res2.start = res->start + (size * offset);
892 res2.end = res2.start + (size * num_vfs) - 1;
893
894 if (res2.end > res->end) {
895 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
896 i, &res2, res, num_vfs, offset);
897 return -EBUSY;
898 }
899 }
900
901 /*
902 * After doing so, there would be a "hole" in the /proc/iomem when
903 * offset is a positive value. It looks like the device return some
904 * mmio back to the system, which actually no one could use it.
905 */
906 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
907 res = &dev->resource[i + PCI_IOV_RESOURCES];
908 if (!res->flags || !res->parent)
909 continue;
910
911 if (!pnv_pci_is_mem_pref_64(res->flags))
912 continue;
913
914 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
915 res2 = *res;
916 res->start += size * offset;
917
Wei Yang74703cc2015-07-20 18:14:58 +0800918 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
919 i, &res2, res, (offset > 0) ? "En" : "Dis",
920 num_vfs, offset);
Wei Yang781a8682015-03-25 16:23:57 +0800921 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
922 }
923 return 0;
924}
925#endif /* CONFIG_PCI_IOV */
926
Gavin Shanfb446ad2012-08-20 03:49:14 +0000927#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800928static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000929{
930 struct pci_controller *hose = pci_bus_to_host(dev->bus);
931 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000932 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000933 struct pnv_ioda_pe *pe;
934 int pe_num;
935
936 if (!pdn) {
937 pr_err("%s: Device tree node not associated properly\n",
938 pci_name(dev));
939 return NULL;
940 }
941 if (pdn->pe_number != IODA_INVALID_PE)
942 return NULL;
943
944 /* PE#0 has been pre-set */
945 if (dev->bus->number == 0)
946 pe_num = 0;
947 else
948 pe_num = pnv_ioda_alloc_pe(phb);
949 if (pe_num == IODA_INVALID_PE) {
950 pr_warning("%s: Not enough PE# available, disabling device\n",
951 pci_name(dev));
952 return NULL;
953 }
954
955 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
956 * pointer in the PE data structure, both should be destroyed at the
957 * same time. However, this needs to be looked at more closely again
958 * once we actually start removing things (Hotplug, SR-IOV, ...)
959 *
960 * At some point we want to remove the PDN completely anyways
961 */
962 pe = &phb->ioda.pe_array[pe_num];
963 pci_dev_get(dev);
964 pdn->pcidev = dev;
965 pdn->pe_number = pe_num;
966 pe->pdev = dev;
967 pe->pbus = NULL;
968 pe->tce32_seg = -1;
969 pe->mve_number = -1;
970 pe->rid = dev->bus->number << 8 | pdn->devfn;
971
972 pe_info(pe, "Associated device to PE\n");
973
974 if (pnv_ioda_configure_pe(phb, pe)) {
975 /* XXX What do we do here ? */
976 if (pe_num)
977 pnv_ioda_free_pe(phb, pe_num);
978 pdn->pe_number = IODA_INVALID_PE;
979 pe->pdev = NULL;
980 pci_dev_put(dev);
981 return NULL;
982 }
983
984 /* Assign a DMA weight to the device */
985 pe->dma_weight = pnv_ioda_dma_weight(dev);
986 if (pe->dma_weight != 0) {
987 phb->ioda.dma_weight += pe->dma_weight;
988 phb->ioda.dma_pe_count++;
989 }
990
991 /* Link the PE */
992 pnv_ioda_link_pe_by_weight(phb, pe);
993
994 return pe;
995}
Gavin Shanfb446ad2012-08-20 03:49:14 +0000996#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000997
998static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
999{
1000 struct pci_dev *dev;
1001
1002 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001003 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001004
1005 if (pdn == NULL) {
1006 pr_warn("%s: No device node associated with device !\n",
1007 pci_name(dev));
1008 continue;
1009 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001010 pdn->pe_number = pe->pe_number;
1011 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001012 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001013 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1014 }
1015}
1016
Gavin Shanfb446ad2012-08-20 03:49:14 +00001017/*
1018 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1019 * single PCI bus. Another one that contains the primary PCI bus and its
1020 * subordinate PCI devices and buses. The second type of PE is normally
1021 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1022 */
Gavin Shand1203852015-06-19 12:26:18 +10001023static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001024{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001025 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001026 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001027 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +10001028 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001029
Guo Chao262af552014-07-21 14:42:30 +10001030 /* Check if PE is determined by M64 */
1031 if (phb->pick_m64_pe)
Gavin Shan26ba2482015-06-19 12:26:19 +10001032 pe_num = phb->pick_m64_pe(bus, all);
Guo Chao262af552014-07-21 14:42:30 +10001033
1034 /* The PE number isn't pinned by M64 */
1035 if (pe_num == IODA_INVALID_PE)
1036 pe_num = pnv_ioda_alloc_pe(phb);
1037
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001038 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001039 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1040 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001041 return;
1042 }
1043
1044 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +10001045 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001046 pe->pbus = bus;
1047 pe->pdev = NULL;
1048 pe->tce32_seg = -1;
1049 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001050 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001051 pe->dma_weight = 0;
1052
Gavin Shanfb446ad2012-08-20 03:49:14 +00001053 if (all)
1054 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
1055 bus->busn_res.start, bus->busn_res.end, pe_num);
1056 else
1057 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
1058 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001059
1060 if (pnv_ioda_configure_pe(phb, pe)) {
1061 /* XXX What do we do here ? */
1062 if (pe_num)
1063 pnv_ioda_free_pe(phb, pe_num);
1064 pe->pbus = NULL;
1065 return;
1066 }
1067
1068 /* Associate it with all child devices */
1069 pnv_ioda_setup_same_PE(bus, pe);
1070
Gavin Shan7ebdf952012-08-20 03:49:15 +00001071 /* Put PE to the list */
1072 list_add_tail(&pe->list, &phb->ioda.pe_list);
1073
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001074 /* Account for one DMA PE if at least one DMA capable device exist
1075 * below the bridge
1076 */
1077 if (pe->dma_weight != 0) {
1078 phb->ioda.dma_weight += pe->dma_weight;
1079 phb->ioda.dma_pe_count++;
1080 }
1081
1082 /* Link the PE */
1083 pnv_ioda_link_pe_by_weight(phb, pe);
1084}
1085
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001086static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001087{
1088 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001089
Gavin Shand1203852015-06-19 12:26:18 +10001090 pnv_ioda_setup_bus_PE(bus, false);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001091
1092 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001093 if (dev->subordinate) {
1094 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
Gavin Shand1203852015-06-19 12:26:18 +10001095 pnv_ioda_setup_bus_PE(dev->subordinate, true);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001096 else
1097 pnv_ioda_setup_PEs(dev->subordinate);
1098 }
1099 }
1100}
1101
1102/*
1103 * Configure PEs so that the downstream PCI buses and devices
1104 * could have their associated PE#. Unfortunately, we didn't
1105 * figure out the way to identify the PLX bridge yet. So we
1106 * simply put the PCI bus and the subordinate behind the root
1107 * port to PE# here. The game rule here is expected to be changed
1108 * as soon as we can detected PLX bridge correctly.
1109 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001110static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001111{
1112 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001113 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001114
1115 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001116 phb = hose->private_data;
1117
1118 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +11001119 if (phb->reserve_m64_pe)
Gavin Shan96a2f922015-06-19 12:26:17 +10001120 phb->reserve_m64_pe(hose->bus, NULL, true);
Guo Chao262af552014-07-21 14:42:30 +10001121
Gavin Shanfb446ad2012-08-20 03:49:14 +00001122 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001123 }
1124}
1125
Gavin Shana8b2f822015-03-25 16:23:52 +08001126#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +08001127static int pnv_pci_vf_release_m64(struct pci_dev *pdev)
1128{
1129 struct pci_bus *bus;
1130 struct pci_controller *hose;
1131 struct pnv_phb *phb;
1132 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001133 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001134
1135 bus = pdev->bus;
1136 hose = pci_bus_to_host(bus);
1137 phb = hose->private_data;
1138 pdn = pci_get_pdn(pdev);
1139
Wei Yang02639b02015-03-25 16:23:59 +08001140 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
1141 for (j = 0; j < M64_PER_IOV; j++) {
1142 if (pdn->m64_wins[i][j] == IODA_INVALID_M64)
1143 continue;
1144 opal_pci_phb_mmio_enable(phb->opal_id,
1145 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 0);
1146 clear_bit(pdn->m64_wins[i][j], &phb->ioda.m64_bar_alloc);
1147 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1148 }
Wei Yang781a8682015-03-25 16:23:57 +08001149
1150 return 0;
1151}
1152
Wei Yang02639b02015-03-25 16:23:59 +08001153static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001154{
1155 struct pci_bus *bus;
1156 struct pci_controller *hose;
1157 struct pnv_phb *phb;
1158 struct pci_dn *pdn;
1159 unsigned int win;
1160 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001161 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001162 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001163 int total_vfs;
1164 resource_size_t size, start;
1165 int pe_num;
1166 int vf_groups;
1167 int vf_per_group;
Wei Yang781a8682015-03-25 16:23:57 +08001168
1169 bus = pdev->bus;
1170 hose = pci_bus_to_host(bus);
1171 phb = hose->private_data;
1172 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001173 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001174
1175 /* Initialize the m64_wins to IODA_INVALID_M64 */
1176 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yang02639b02015-03-25 16:23:59 +08001177 for (j = 0; j < M64_PER_IOV; j++)
1178 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1179
1180 if (pdn->m64_per_iov == M64_PER_IOV) {
1181 vf_groups = (num_vfs <= M64_PER_IOV) ? num_vfs: M64_PER_IOV;
1182 vf_per_group = (num_vfs <= M64_PER_IOV)? 1:
1183 roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1184 } else {
1185 vf_groups = 1;
1186 vf_per_group = 1;
1187 }
Wei Yang781a8682015-03-25 16:23:57 +08001188
1189 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1190 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1191 if (!res->flags || !res->parent)
1192 continue;
1193
1194 if (!pnv_pci_is_mem_pref_64(res->flags))
1195 continue;
1196
Wei Yang02639b02015-03-25 16:23:59 +08001197 for (j = 0; j < vf_groups; j++) {
1198 do {
1199 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1200 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001201
Wei Yang02639b02015-03-25 16:23:59 +08001202 if (win >= phb->ioda.m64_bar_idx + 1)
1203 goto m64_failed;
1204 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001205
Wei Yang02639b02015-03-25 16:23:59 +08001206 pdn->m64_wins[i][j] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001207
Wei Yang02639b02015-03-25 16:23:59 +08001208 if (pdn->m64_per_iov == M64_PER_IOV) {
1209 size = pci_iov_resource_size(pdev,
1210 PCI_IOV_RESOURCES + i);
1211 size = size * vf_per_group;
1212 start = res->start + size * j;
1213 } else {
1214 size = resource_size(res);
1215 start = res->start;
1216 }
1217
1218 /* Map the M64 here */
1219 if (pdn->m64_per_iov == M64_PER_IOV) {
1220 pe_num = pdn->offset + j;
1221 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1222 pe_num, OPAL_M64_WINDOW_TYPE,
1223 pdn->m64_wins[i][j], 0);
1224 }
1225
1226 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001227 OPAL_M64_WINDOW_TYPE,
Wei Yang02639b02015-03-25 16:23:59 +08001228 pdn->m64_wins[i][j],
1229 start,
Wei Yang781a8682015-03-25 16:23:57 +08001230 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001231 size);
Wei Yang781a8682015-03-25 16:23:57 +08001232
Wei Yang02639b02015-03-25 16:23:59 +08001233
1234 if (rc != OPAL_SUCCESS) {
1235 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1236 win, rc);
1237 goto m64_failed;
1238 }
1239
1240 if (pdn->m64_per_iov == M64_PER_IOV)
1241 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1242 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 2);
1243 else
1244 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1245 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 1);
1246
1247 if (rc != OPAL_SUCCESS) {
1248 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1249 win, rc);
1250 goto m64_failed;
1251 }
Wei Yang781a8682015-03-25 16:23:57 +08001252 }
1253 }
1254 return 0;
1255
1256m64_failed:
1257 pnv_pci_vf_release_m64(pdev);
1258 return -EBUSY;
1259}
1260
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001261static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1262 int num);
1263static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1264
Wei Yang781a8682015-03-25 16:23:57 +08001265static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1266{
Wei Yang781a8682015-03-25 16:23:57 +08001267 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001268 int64_t rc;
1269
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001270 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001271 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001272 if (rc)
1273 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1274
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001275 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001276 if (pe->table_group.group) {
1277 iommu_group_put(pe->table_group.group);
1278 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001279 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001280 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001281 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001282}
1283
Wei Yang02639b02015-03-25 16:23:59 +08001284static void pnv_ioda_release_vf_PE(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001285{
1286 struct pci_bus *bus;
1287 struct pci_controller *hose;
1288 struct pnv_phb *phb;
1289 struct pnv_ioda_pe *pe, *pe_n;
1290 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001291 u16 vf_index;
1292 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001293
1294 bus = pdev->bus;
1295 hose = pci_bus_to_host(bus);
1296 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001297 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001298
1299 if (!pdev->is_physfn)
1300 return;
1301
Wei Yang02639b02015-03-25 16:23:59 +08001302 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1303 int vf_group;
1304 int vf_per_group;
1305 int vf_index1;
1306
1307 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1308
1309 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++)
1310 for (vf_index = vf_group * vf_per_group;
1311 vf_index < (vf_group + 1) * vf_per_group &&
1312 vf_index < num_vfs;
1313 vf_index++)
1314 for (vf_index1 = vf_group * vf_per_group;
1315 vf_index1 < (vf_group + 1) * vf_per_group &&
1316 vf_index1 < num_vfs;
1317 vf_index1++){
1318
1319 rc = opal_pci_set_peltv(phb->opal_id,
1320 pdn->offset + vf_index,
1321 pdn->offset + vf_index1,
1322 OPAL_REMOVE_PE_FROM_DOMAIN);
1323
1324 if (rc)
1325 dev_warn(&pdev->dev, "%s: Failed to unlink same group PE#%d(%lld)\n",
1326 __func__,
1327 pdn->offset + vf_index1, rc);
1328 }
1329 }
1330
Wei Yang781a8682015-03-25 16:23:57 +08001331 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1332 if (pe->parent_dev != pdev)
1333 continue;
1334
1335 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1336
1337 /* Remove from list */
1338 mutex_lock(&phb->ioda.pe_list_mutex);
1339 list_del(&pe->list);
1340 mutex_unlock(&phb->ioda.pe_list_mutex);
1341
1342 pnv_ioda_deconfigure_pe(phb, pe);
1343
1344 pnv_ioda_free_pe(phb, pe->pe_number);
1345 }
1346}
1347
1348void pnv_pci_sriov_disable(struct pci_dev *pdev)
1349{
1350 struct pci_bus *bus;
1351 struct pci_controller *hose;
1352 struct pnv_phb *phb;
1353 struct pci_dn *pdn;
1354 struct pci_sriov *iov;
1355 u16 num_vfs;
1356
1357 bus = pdev->bus;
1358 hose = pci_bus_to_host(bus);
1359 phb = hose->private_data;
1360 pdn = pci_get_pdn(pdev);
1361 iov = pdev->sriov;
1362 num_vfs = pdn->num_vfs;
1363
1364 /* Release VF PEs */
Wei Yang02639b02015-03-25 16:23:59 +08001365 pnv_ioda_release_vf_PE(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001366
1367 if (phb->type == PNV_PHB_IODA2) {
Wei Yang02639b02015-03-25 16:23:59 +08001368 if (pdn->m64_per_iov == 1)
1369 pnv_pci_vf_resource_shift(pdev, -pdn->offset);
Wei Yang781a8682015-03-25 16:23:57 +08001370
1371 /* Release M64 windows */
1372 pnv_pci_vf_release_m64(pdev);
1373
1374 /* Release PE numbers */
1375 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1376 pdn->offset = 0;
1377 }
1378}
1379
1380static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1381 struct pnv_ioda_pe *pe);
1382static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1383{
1384 struct pci_bus *bus;
1385 struct pci_controller *hose;
1386 struct pnv_phb *phb;
1387 struct pnv_ioda_pe *pe;
1388 int pe_num;
1389 u16 vf_index;
1390 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001391 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001392
1393 bus = pdev->bus;
1394 hose = pci_bus_to_host(bus);
1395 phb = hose->private_data;
1396 pdn = pci_get_pdn(pdev);
1397
1398 if (!pdev->is_physfn)
1399 return;
1400
1401 /* Reserve PE for each VF */
1402 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
1403 pe_num = pdn->offset + vf_index;
1404
1405 pe = &phb->ioda.pe_array[pe_num];
1406 pe->pe_number = pe_num;
1407 pe->phb = phb;
1408 pe->flags = PNV_IODA_PE_VF;
1409 pe->pbus = NULL;
1410 pe->parent_dev = pdev;
1411 pe->tce32_seg = -1;
1412 pe->mve_number = -1;
1413 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1414 pci_iov_virtfn_devfn(pdev, vf_index);
1415
1416 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1417 hose->global_number, pdev->bus->number,
1418 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1419 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1420
1421 if (pnv_ioda_configure_pe(phb, pe)) {
1422 /* XXX What do we do here ? */
1423 if (pe_num)
1424 pnv_ioda_free_pe(phb, pe_num);
1425 pe->pdev = NULL;
1426 continue;
1427 }
1428
Wei Yang781a8682015-03-25 16:23:57 +08001429 /* Put PE to the list */
1430 mutex_lock(&phb->ioda.pe_list_mutex);
1431 list_add_tail(&pe->list, &phb->ioda.pe_list);
1432 mutex_unlock(&phb->ioda.pe_list_mutex);
1433
1434 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1435 }
Wei Yang02639b02015-03-25 16:23:59 +08001436
1437 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1438 int vf_group;
1439 int vf_per_group;
1440 int vf_index1;
1441
1442 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1443
1444 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++) {
1445 for (vf_index = vf_group * vf_per_group;
1446 vf_index < (vf_group + 1) * vf_per_group &&
1447 vf_index < num_vfs;
1448 vf_index++) {
1449 for (vf_index1 = vf_group * vf_per_group;
1450 vf_index1 < (vf_group + 1) * vf_per_group &&
1451 vf_index1 < num_vfs;
1452 vf_index1++) {
1453
1454 rc = opal_pci_set_peltv(phb->opal_id,
1455 pdn->offset + vf_index,
1456 pdn->offset + vf_index1,
1457 OPAL_ADD_PE_TO_DOMAIN);
1458
1459 if (rc)
1460 dev_warn(&pdev->dev, "%s: Failed to link same group PE#%d(%lld)\n",
1461 __func__,
1462 pdn->offset + vf_index1, rc);
1463 }
1464 }
1465 }
1466 }
Wei Yang781a8682015-03-25 16:23:57 +08001467}
1468
1469int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1470{
1471 struct pci_bus *bus;
1472 struct pci_controller *hose;
1473 struct pnv_phb *phb;
1474 struct pci_dn *pdn;
1475 int ret;
1476
1477 bus = pdev->bus;
1478 hose = pci_bus_to_host(bus);
1479 phb = hose->private_data;
1480 pdn = pci_get_pdn(pdev);
1481
1482 if (phb->type == PNV_PHB_IODA2) {
1483 /* Calculate available PE for required VFs */
1484 mutex_lock(&phb->ioda.pe_alloc_mutex);
1485 pdn->offset = bitmap_find_next_zero_area(
1486 phb->ioda.pe_alloc, phb->ioda.total_pe,
1487 0, num_vfs, 0);
1488 if (pdn->offset >= phb->ioda.total_pe) {
1489 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1490 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1491 pdn->offset = 0;
1492 return -EBUSY;
1493 }
1494 bitmap_set(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1495 pdn->num_vfs = num_vfs;
1496 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1497
1498 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001499 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001500 if (ret) {
1501 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1502 goto m64_failed;
1503 }
1504
1505 /*
1506 * When using one M64 BAR to map one IOV BAR, we need to shift
1507 * the IOV BAR according to the PE# allocated to the VFs.
1508 * Otherwise, the PE# for the VF will conflict with others.
1509 */
Wei Yang02639b02015-03-25 16:23:59 +08001510 if (pdn->m64_per_iov == 1) {
1511 ret = pnv_pci_vf_resource_shift(pdev, pdn->offset);
1512 if (ret)
1513 goto m64_failed;
1514 }
Wei Yang781a8682015-03-25 16:23:57 +08001515 }
1516
1517 /* Setup VF PEs */
1518 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1519
1520 return 0;
1521
1522m64_failed:
1523 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1524 pdn->offset = 0;
1525
1526 return ret;
1527}
1528
Gavin Shana8b2f822015-03-25 16:23:52 +08001529int pcibios_sriov_disable(struct pci_dev *pdev)
1530{
Wei Yang781a8682015-03-25 16:23:57 +08001531 pnv_pci_sriov_disable(pdev);
1532
Gavin Shana8b2f822015-03-25 16:23:52 +08001533 /* Release PCI data */
1534 remove_dev_pci_data(pdev);
1535 return 0;
1536}
1537
1538int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1539{
1540 /* Allocate PCI data */
1541 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001542
1543 pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001544 return 0;
1545}
1546#endif /* CONFIG_PCI_IOV */
1547
Gavin Shan959c9bd2013-04-25 19:21:02 +00001548static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001549{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001550 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001551 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001552
Gavin Shan959c9bd2013-04-25 19:21:02 +00001553 /*
1554 * The function can be called while the PE#
1555 * hasn't been assigned. Do nothing for the
1556 * case.
1557 */
1558 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1559 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001560
Gavin Shan959c9bd2013-04-25 19:21:02 +00001561 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001562 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiy0e1ffef2015-08-27 16:01:16 +10001563 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001564 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001565 /*
1566 * Note: iommu_add_device() will fail here as
1567 * for physical PE: the device is already added by now;
1568 * for virtual PE: sysfs entries are not ready yet and
1569 * tce_iommu_bus_notifier will add the device to a group later.
1570 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001571}
1572
Daniel Axtens763d2d82015-04-28 15:12:07 +10001573static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001574{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001575 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1576 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001577 struct pci_dn *pdn = pci_get_pdn(pdev);
1578 struct pnv_ioda_pe *pe;
1579 uint64_t top;
1580 bool bypass = false;
1581
1582 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1583 return -ENODEV;;
1584
1585 pe = &phb->ioda.pe_array[pdn->pe_number];
1586 if (pe->tce_bypass_enabled) {
1587 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1588 bypass = (dma_mask >= top);
1589 }
1590
1591 if (bypass) {
1592 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1593 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001594 } else {
1595 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1596 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001597 }
Brian W Harta32305b2014-07-31 14:24:37 -05001598 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001599 return 0;
1600}
1601
Andrew Donnellan535229822015-08-07 13:45:54 +10001602static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001603{
Andrew Donnellan535229822015-08-07 13:45:54 +10001604 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1605 struct pnv_phb *phb = hose->private_data;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001606 struct pci_dn *pdn = pci_get_pdn(pdev);
1607 struct pnv_ioda_pe *pe;
1608 u64 end, mask;
1609
1610 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1611 return 0;
1612
1613 pe = &phb->ioda.pe_array[pdn->pe_number];
1614 if (!pe->tce_bypass_enabled)
1615 return __dma_get_required_mask(&pdev->dev);
1616
1617
1618 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1619 mask = 1ULL << (fls64(end) - 1);
1620 mask += mask - 1;
1621
1622 return mask;
1623}
1624
Gavin Shandff4a392014-07-15 17:00:55 +10001625static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001626 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001627{
1628 struct pci_dev *dev;
1629
1630 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001631 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001632 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001633 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001634
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001635 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001636 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001637 }
1638}
1639
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001640static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
1641 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001642{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001643 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1644 &tbl->it_group_list, struct iommu_table_group_link,
1645 next);
1646 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001647 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001648 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001649 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1650 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001651 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001652 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001653
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001654 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1655 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1656 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001657
1658 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1659 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001660 start <<= shift;
1661 end <<= shift;
1662 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001663 start |= tbl->it_busno;
1664 end |= tbl->it_busno;
1665 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1666 /* p7ioc-style invalidation, 2 TCEs per write */
1667 start |= (1ull << 63);
1668 end |= (1ull << 63);
1669 inc = 16;
1670 } else {
1671 /* Default (older HW) */
1672 inc = 128;
1673 }
1674
1675 end |= inc - 1; /* round up end to be different than start */
1676
1677 mb(); /* Ensure above stores are visible */
1678 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001679 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001680 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001681 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001682 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001683 start += inc;
1684 }
1685
1686 /*
1687 * The iommu layer will do another mb() for us on build()
1688 * and we don't care on free()
1689 */
1690}
1691
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001692static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1693 long npages, unsigned long uaddr,
1694 enum dma_data_direction direction,
1695 struct dma_attrs *attrs)
1696{
1697 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1698 attrs);
1699
1700 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1701 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1702
1703 return ret;
1704}
1705
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001706#ifdef CONFIG_IOMMU_API
1707static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1708 unsigned long *hpa, enum dma_data_direction *direction)
1709{
1710 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1711
1712 if (!ret && (tbl->it_type &
1713 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1714 pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);
1715
1716 return ret;
1717}
1718#endif
1719
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001720static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1721 long npages)
1722{
1723 pnv_tce_free(tbl, index, npages);
1724
1725 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1726 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1727}
1728
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001729static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001730 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001731#ifdef CONFIG_IOMMU_API
1732 .exchange = pnv_ioda1_tce_xchg,
1733#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001734 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001735 .get = pnv_tce_get,
1736};
1737
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001738static inline void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_ioda_pe *pe)
1739{
1740 /* 01xb - invalidate TCEs that match the specified PE# */
1741 unsigned long val = (0x4ull << 60) | (pe->pe_number & 0xFF);
1742 struct pnv_phb *phb = pe->phb;
1743
1744 if (!phb->ioda.tce_inval_reg)
1745 return;
1746
1747 mb(); /* Ensure above stores are visible */
1748 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1749}
1750
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001751static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
1752 __be64 __iomem *invalidate, unsigned shift,
1753 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001754{
1755 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001756
1757 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001758 start = 0x2ull << 60;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001759 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001760 end = start;
1761
1762 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001763 start |= (index << shift);
1764 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001765 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001766 mb();
1767
1768 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001769 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001770 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001771 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001772 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001773 start += inc;
1774 }
1775}
1776
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001777static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1778 unsigned long index, unsigned long npages, bool rm)
1779{
1780 struct iommu_table_group_link *tgl;
1781
1782 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
1783 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1784 struct pnv_ioda_pe, table_group);
1785 __be64 __iomem *invalidate = rm ?
1786 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1787 pe->phb->ioda.tce_inval_reg;
1788
1789 pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
1790 invalidate, tbl->it_page_shift,
1791 index, npages);
1792 }
1793}
1794
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001795static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1796 long npages, unsigned long uaddr,
1797 enum dma_data_direction direction,
1798 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001799{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001800 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1801 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001802
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001803 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1804 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1805
1806 return ret;
1807}
1808
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001809#ifdef CONFIG_IOMMU_API
1810static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1811 unsigned long *hpa, enum dma_data_direction *direction)
1812{
1813 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1814
1815 if (!ret && (tbl->it_type &
1816 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1817 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1818
1819 return ret;
1820}
1821#endif
1822
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001823static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1824 long npages)
1825{
1826 pnv_tce_free(tbl, index, npages);
1827
1828 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1829 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001830}
1831
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001832static void pnv_ioda2_table_free(struct iommu_table *tbl)
1833{
1834 pnv_pci_ioda2_table_free_pages(tbl);
1835 iommu_free_table(tbl, "pnv");
1836}
1837
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001838static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001839 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001840#ifdef CONFIG_IOMMU_API
1841 .exchange = pnv_ioda2_tce_xchg,
1842#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001843 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001844 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001845 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001846};
1847
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001848static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1849 struct pnv_ioda_pe *pe, unsigned int base,
1850 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001851{
1852
1853 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001854 struct iommu_table *tbl;
1855 unsigned int i;
1856 int64_t rc;
1857 void *addr;
1858
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001859 /* XXX FIXME: Handle 64-bit only DMA devices */
1860 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1861 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1862
1863 /* We shouldn't already have a 32-bit DMA associated */
1864 if (WARN_ON(pe->tce32_seg >= 0))
1865 return;
1866
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001867 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001868 iommu_register_group(&pe->table_group, phb->hose->global_number,
1869 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001870 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001871
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001872 /* Grab a 32-bit TCE table */
1873 pe->tce32_seg = base;
1874 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1875 (base << 28), ((base + segs) << 28) - 1);
1876
1877 /* XXX Currently, we allocate one big contiguous table for the
1878 * TCEs. We only really need one chunk per 256M of TCE space
1879 * (ie per segment) but that's an optimization for later, it
1880 * requires some added smarts with our get/put_tce implementation
1881 */
1882 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1883 get_order(TCE32_TABLE_SIZE * segs));
1884 if (!tce_mem) {
1885 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1886 goto fail;
1887 }
1888 addr = page_address(tce_mem);
1889 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1890
1891 /* Configure HW */
1892 for (i = 0; i < segs; i++) {
1893 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1894 pe->pe_number,
1895 base + i, 1,
1896 __pa(addr) + TCE32_TABLE_SIZE * i,
1897 TCE32_TABLE_SIZE, 0x1000);
1898 if (rc) {
1899 pe_err(pe, " Failed to configure 32-bit TCE table,"
1900 " err %ld\n", rc);
1901 goto fail;
1902 }
1903 }
1904
1905 /* Setup linux iommu table */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001906 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001907 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001908
1909 /* OPAL variant of P7IOC SW invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001910 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10001911 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1912 TCE_PCI_SWINV_FREE |
1913 TCE_PCI_SWINV_PAIR);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001914
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001915 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001916 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
1917 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001918 iommu_init_table(tbl, phb->hose->node);
1919
Wei Yang781a8682015-03-25 16:23:57 +08001920 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001921 /*
1922 * Setting table base here only for carrying iommu_group
1923 * further down to let iommu_add_device() do the job.
1924 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
1925 */
1926 set_iommu_table_base(&pe->pdev->dev, tbl);
1927 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001928 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001929 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001930
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001931 return;
1932 fail:
1933 /* XXX Failure: Try to fallback to 64-bit only ? */
1934 if (pe->tce32_seg >= 0)
1935 pe->tce32_seg = -1;
1936 if (tce_mem)
1937 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001938 if (tbl) {
1939 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
1940 iommu_free_table(tbl, "pnv");
1941 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001942}
1943
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001944static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
1945 int num, struct iommu_table *tbl)
1946{
1947 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1948 table_group);
1949 struct pnv_phb *phb = pe->phb;
1950 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001951 const unsigned long size = tbl->it_indirect_levels ?
1952 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001953 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
1954 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
1955
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001956 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001957 start_addr, start_addr + win_size - 1,
1958 IOMMU_PAGE_SIZE(tbl));
1959
1960 /*
1961 * Map TCE table through TVT. The TVE index is the PE number
1962 * shifted by 1 bit for 32-bits DMA space.
1963 */
1964 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1965 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001966 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001967 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001968 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001969 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001970 IOMMU_PAGE_SIZE(tbl));
1971 if (rc) {
1972 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
1973 return rc;
1974 }
1975
1976 pnv_pci_link_table_and_group(phb->hose->node, num,
1977 tbl, &pe->table_group);
1978 pnv_pci_ioda2_tce_invalidate_entire(pe);
1979
1980 return 0;
1981}
1982
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10001983static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001984{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001985 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1986 int64_t rc;
1987
1988 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1989 if (enable) {
1990 phys_addr_t top = memblock_end_of_DRAM();
1991
1992 top = roundup_pow_of_two(top);
1993 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1994 pe->pe_number,
1995 window_id,
1996 pe->tce_bypass_base,
1997 top);
1998 } else {
1999 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2000 pe->pe_number,
2001 window_id,
2002 pe->tce_bypass_base,
2003 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002004 }
2005 if (rc)
2006 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2007 else
2008 pe->tce_bypass_enabled = enable;
2009}
2010
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002011static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2012 __u32 page_shift, __u64 window_size, __u32 levels,
2013 struct iommu_table *tbl);
2014
2015static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2016 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2017 struct iommu_table **ptbl)
2018{
2019 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2020 table_group);
2021 int nid = pe->phb->hose->node;
2022 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2023 long ret;
2024 struct iommu_table *tbl;
2025
2026 tbl = pnv_pci_table_alloc(nid);
2027 if (!tbl)
2028 return -ENOMEM;
2029
2030 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2031 bus_offset, page_shift, window_size,
2032 levels, tbl);
2033 if (ret) {
2034 iommu_free_table(tbl, "pnv");
2035 return ret;
2036 }
2037
2038 tbl->it_ops = &pnv_ioda2_iommu_ops;
2039 if (pe->phb->ioda.tce_inval_reg)
2040 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2041
2042 *ptbl = tbl;
2043
2044 return 0;
2045}
2046
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002047static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2048{
2049 struct iommu_table *tbl = NULL;
2050 long rc;
2051
2052 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2053 IOMMU_PAGE_SHIFT_4K,
2054 pe->table_group.tce32_size,
2055 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2056 if (rc) {
2057 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2058 rc);
2059 return rc;
2060 }
2061
2062 iommu_init_table(tbl, pe->phb->hose->node);
2063
2064 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2065 if (rc) {
2066 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2067 rc);
2068 pnv_ioda2_table_free(tbl);
2069 return rc;
2070 }
2071
2072 if (!pnv_iommu_bypass_disabled)
2073 pnv_pci_ioda2_set_bypass(pe, true);
2074
2075 /* OPAL variant of PHB3 invalidated TCEs */
2076 if (pe->phb->ioda.tce_inval_reg)
2077 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2078
2079 /*
2080 * Setting table base here only for carrying iommu_group
2081 * further down to let iommu_add_device() do the job.
2082 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2083 */
2084 if (pe->flags & PNV_IODA_PE_DEV)
2085 set_iommu_table_base(&pe->pdev->dev, tbl);
2086
2087 return 0;
2088}
2089
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002090#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2091static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2092 int num)
2093{
2094 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2095 table_group);
2096 struct pnv_phb *phb = pe->phb;
2097 long ret;
2098
2099 pe_info(pe, "Removing DMA window #%d\n", num);
2100
2101 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2102 (pe->pe_number << 1) + num,
2103 0/* levels */, 0/* table address */,
2104 0/* table size */, 0/* page size */);
2105 if (ret)
2106 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2107 else
2108 pnv_pci_ioda2_tce_invalidate_entire(pe);
2109
2110 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2111
2112 return ret;
2113}
2114#endif
2115
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002116#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002117static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2118 __u64 window_size, __u32 levels)
2119{
2120 unsigned long bytes = 0;
2121 const unsigned window_shift = ilog2(window_size);
2122 unsigned entries_shift = window_shift - page_shift;
2123 unsigned table_shift = entries_shift + 3;
2124 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2125 unsigned long direct_table_size;
2126
2127 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2128 (window_size > memory_hotplug_max()) ||
2129 !is_power_of_2(window_size))
2130 return 0;
2131
2132 /* Calculate a direct table size from window_size and levels */
2133 entries_shift = (entries_shift + levels - 1) / levels;
2134 table_shift = entries_shift + 3;
2135 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2136 direct_table_size = 1UL << table_shift;
2137
2138 for ( ; levels; --levels) {
2139 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2140
2141 tce_table_size /= direct_table_size;
2142 tce_table_size <<= 3;
2143 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2144 }
2145
2146 return bytes;
2147}
2148
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002149static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002150{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002151 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2152 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002153 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2154 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002155
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002156 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002157 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2158 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002159}
2160
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002161static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2162{
2163 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2164 table_group);
2165
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002166 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002167}
2168
2169static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002170 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002171 .create_table = pnv_pci_ioda2_create_table,
2172 .set_window = pnv_pci_ioda2_set_window,
2173 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002174 .take_ownership = pnv_ioda2_take_ownership,
2175 .release_ownership = pnv_ioda2_release_ownership,
2176};
2177#endif
2178
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002179static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2180{
2181 const __be64 *swinvp;
2182
2183 /* OPAL variant of PHB3 invalidated TCEs */
2184 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2185 if (!swinvp)
2186 return;
2187
2188 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2189 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2190}
2191
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002192static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2193 unsigned levels, unsigned long limit,
2194 unsigned long *current_offset)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002195{
2196 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002197 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002198 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002199 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2200 unsigned entries = 1UL << (shift - 3);
2201 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002202
2203 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2204 if (!tce_mem) {
2205 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2206 return NULL;
2207 }
2208 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002209 memset(addr, 0, allocated);
2210
2211 --levels;
2212 if (!levels) {
2213 *current_offset += allocated;
2214 return addr;
2215 }
2216
2217 for (i = 0; i < entries; ++i) {
2218 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
2219 levels, limit, current_offset);
2220 if (!tmp)
2221 break;
2222
2223 addr[i] = cpu_to_be64(__pa(tmp) |
2224 TCE_PCI_READ | TCE_PCI_WRITE);
2225
2226 if (*current_offset >= limit)
2227 break;
2228 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002229
2230 return addr;
2231}
2232
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002233static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2234 unsigned long size, unsigned level);
2235
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002236static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002237 __u32 page_shift, __u64 window_size, __u32 levels,
2238 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002239{
2240 void *addr;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002241 unsigned long offset = 0, level_shift;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002242 const unsigned window_shift = ilog2(window_size);
2243 unsigned entries_shift = window_shift - page_shift;
2244 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2245 const unsigned long tce_table_size = 1UL << table_shift;
2246
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002247 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2248 return -EINVAL;
2249
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002250 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2251 return -EINVAL;
2252
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002253 /* Adjust direct table size from window_size and levels */
2254 entries_shift = (entries_shift + levels - 1) / levels;
2255 level_shift = entries_shift + 3;
2256 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2257
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002258 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002259 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
2260 levels, tce_table_size, &offset);
2261
2262 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002263 if (!addr)
2264 return -ENOMEM;
2265
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002266 /*
2267 * First level was allocated but some lower level failed as
2268 * we did not allocate as much as we wanted,
2269 * release partially allocated table.
2270 */
2271 if (offset < tce_table_size) {
2272 pnv_pci_ioda2_table_do_free_pages(addr,
2273 1ULL << (level_shift - 3), levels - 1);
2274 return -ENOMEM;
2275 }
2276
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002277 /* Setup linux iommu table */
2278 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2279 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002280 tbl->it_level_size = 1ULL << (level_shift - 3);
2281 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002282 tbl->it_allocated_size = offset;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002283
2284 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2285 window_size, tce_table_size, bus_offset);
2286
2287 return 0;
2288}
2289
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002290static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2291 unsigned long size, unsigned level)
2292{
2293 const unsigned long addr_ul = (unsigned long) addr &
2294 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2295
2296 if (level) {
2297 long i;
2298 u64 *tmp = (u64 *) addr_ul;
2299
2300 for (i = 0; i < size; ++i) {
2301 unsigned long hpa = be64_to_cpu(tmp[i]);
2302
2303 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2304 continue;
2305
2306 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2307 level - 1);
2308 }
2309 }
2310
2311 free_pages(addr_ul, get_order(size << 3));
2312}
2313
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002314static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2315{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002316 const unsigned long size = tbl->it_indirect_levels ?
2317 tbl->it_level_size : tbl->it_size;
2318
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002319 if (!tbl->it_size)
2320 return;
2321
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002322 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2323 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002324}
2325
Gavin Shan373f5652013-04-25 19:21:01 +00002326static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2327 struct pnv_ioda_pe *pe)
2328{
Gavin Shan373f5652013-04-25 19:21:01 +00002329 int64_t rc;
2330
2331 /* We shouldn't already have a 32-bit DMA associated */
2332 if (WARN_ON(pe->tce32_seg >= 0))
2333 return;
2334
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002335 /* TVE #1 is selected by PCI address bit 59 */
2336 pe->tce_bypass_base = 1ull << 59;
2337
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002338 iommu_register_group(&pe->table_group, phb->hose->global_number,
2339 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002340
Gavin Shan373f5652013-04-25 19:21:01 +00002341 /* The PE will reserve all possible 32-bits space */
2342 pe->tce32_seg = 0;
Gavin Shan373f5652013-04-25 19:21:01 +00002343 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002344 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002345
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002346 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002347 pe->table_group.tce32_start = 0;
2348 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2349 pe->table_group.max_dynamic_windows_supported =
2350 IOMMU_TABLE_GROUP_MAX_TABLES;
2351 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2352 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002353#ifdef CONFIG_IOMMU_API
2354 pe->table_group.ops = &pnv_pci_ioda2_ops;
2355#endif
2356
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002357 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan373f5652013-04-25 19:21:01 +00002358 if (rc) {
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002359 if (pe->tce32_seg >= 0)
2360 pe->tce32_seg = -1;
2361 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002362 }
2363
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002364 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002365 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002366 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002367 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002368}
2369
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002370static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002371{
2372 struct pci_controller *hose = phb->hose;
2373 unsigned int residual, remaining, segs, tw, base;
2374 struct pnv_ioda_pe *pe;
2375
2376 /* If we have more PE# than segments available, hand out one
2377 * per PE until we run out and let the rest fail. If not,
2378 * then we assign at least one segment per PE, plus more based
2379 * on the amount of devices under that PE
2380 */
2381 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
2382 residual = 0;
2383 else
2384 residual = phb->ioda.tce32_count -
2385 phb->ioda.dma_pe_count;
2386
2387 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
2388 hose->global_number, phb->ioda.tce32_count);
2389 pr_info("PCI: %d PE# for a total weight of %d\n",
2390 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
2391
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002392 pnv_pci_ioda_setup_opal_tce_kill(phb);
2393
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002394 /* Walk our PE list and configure their DMA segments, hand them
2395 * out one base segment plus any residual segments based on
2396 * weight
2397 */
2398 remaining = phb->ioda.tce32_count;
2399 tw = phb->ioda.dma_weight;
2400 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00002401 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002402 if (!pe->dma_weight)
2403 continue;
2404 if (!remaining) {
2405 pe_warn(pe, "No DMA32 resources available\n");
2406 continue;
2407 }
2408 segs = 1;
2409 if (residual) {
2410 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
2411 if (segs > remaining)
2412 segs = remaining;
2413 }
Gavin Shan373f5652013-04-25 19:21:01 +00002414
2415 /*
2416 * For IODA2 compliant PHB3, we needn't care about the weight.
2417 * The all available 32-bits DMA space will be assigned to
2418 * the specific PE.
2419 */
2420 if (phb->type == PNV_PHB_IODA1) {
2421 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
2422 pe->dma_weight, segs);
2423 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
2424 } else {
2425 pe_info(pe, "Assign DMA32 space\n");
2426 segs = 0;
2427 pnv_pci_ioda2_setup_dma_pe(phb, pe);
2428 }
2429
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002430 remaining -= segs;
2431 base += segs;
2432 }
2433}
2434
2435#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002436static void pnv_ioda2_msi_eoi(struct irq_data *d)
2437{
2438 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2439 struct irq_chip *chip = irq_data_get_irq_chip(d);
2440 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2441 ioda.irq_chip);
2442 int64_t rc;
2443
2444 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2445 WARN_ON_ONCE(rc);
2446
2447 icp_native_eoi(d);
2448}
2449
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002450
2451static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2452{
2453 struct irq_data *idata;
2454 struct irq_chip *ichip;
2455
2456 if (phb->type != PNV_PHB_IODA2)
2457 return;
2458
2459 if (!phb->ioda.irq_chip_init) {
2460 /*
2461 * First time we setup an MSI IRQ, we need to setup the
2462 * corresponding IRQ chip to route correctly.
2463 */
2464 idata = irq_get_irq_data(virq);
2465 ichip = irq_data_get_irq_chip(idata);
2466 phb->ioda.irq_chip_init = 1;
2467 phb->ioda.irq_chip = *ichip;
2468 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2469 }
2470 irq_set_chip(virq, &phb->ioda.irq_chip);
2471}
2472
Ian Munsie80c49c72014-10-08 19:54:57 +11002473#ifdef CONFIG_CXL_BASE
2474
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002475struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002476{
2477 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2478
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002479 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002480}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002481EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002482
Ryan Grimm1212aa12015-01-19 11:52:50 -06002483int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002484{
2485 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2486 struct pnv_phb *phb = hose->private_data;
2487 struct pnv_ioda_pe *pe;
2488 int rc;
2489
2490 pe = pnv_ioda_get_pe(dev);
2491 if (!pe)
2492 return -ENODEV;
2493
2494 pe_info(pe, "Switching PHB to CXL\n");
2495
Ryan Grimm1212aa12015-01-19 11:52:50 -06002496 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11002497 if (rc)
2498 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2499
2500 return rc;
2501}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002502EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002503
2504/* Find PHB for cxl dev and allocate MSI hwirqs?
2505 * Returns the absolute hardware IRQ number
2506 */
2507int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2508{
2509 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2510 struct pnv_phb *phb = hose->private_data;
2511 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2512
2513 if (hwirq < 0) {
2514 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2515 return -ENOSPC;
2516 }
2517
2518 return phb->msi_base + hwirq;
2519}
2520EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2521
2522void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2523{
2524 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2525 struct pnv_phb *phb = hose->private_data;
2526
2527 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2528}
2529EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2530
2531void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2532 struct pci_dev *dev)
2533{
2534 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2535 struct pnv_phb *phb = hose->private_data;
2536 int i, hwirq;
2537
2538 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2539 if (!irqs->range[i])
2540 continue;
2541 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2542 i, irqs->offset[i],
2543 irqs->range[i]);
2544 hwirq = irqs->offset[i] - phb->msi_base;
2545 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2546 irqs->range[i]);
2547 }
2548}
2549EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2550
2551int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2552 struct pci_dev *dev, int num)
2553{
2554 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2555 struct pnv_phb *phb = hose->private_data;
2556 int i, hwirq, try;
2557
2558 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2559
2560 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2561 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2562 try = num;
2563 while (try) {
2564 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2565 if (hwirq >= 0)
2566 break;
2567 try /= 2;
2568 }
2569 if (!try)
2570 goto fail;
2571
2572 irqs->offset[i] = phb->msi_base + hwirq;
2573 irqs->range[i] = try;
2574 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2575 i, irqs->offset[i], irqs->range[i]);
2576 num -= try;
2577 }
2578 if (num)
2579 goto fail;
2580
2581 return 0;
2582fail:
2583 pnv_cxl_release_hwirq_ranges(irqs, dev);
2584 return -ENOSPC;
2585}
2586EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2587
2588int pnv_cxl_get_irq_count(struct pci_dev *dev)
2589{
2590 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2591 struct pnv_phb *phb = hose->private_data;
2592
2593 return phb->msi_bmp.irq_count;
2594}
2595EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2596
2597int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2598 unsigned int virq)
2599{
2600 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2601 struct pnv_phb *phb = hose->private_data;
2602 unsigned int xive_num = hwirq - phb->msi_base;
2603 struct pnv_ioda_pe *pe;
2604 int rc;
2605
2606 if (!(pe = pnv_ioda_get_pe(dev)))
2607 return -ENODEV;
2608
2609 /* Assign XIVE to PE */
2610 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2611 if (rc) {
2612 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2613 "hwirq 0x%x XIVE 0x%x PE\n",
2614 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2615 return -EIO;
2616 }
2617 set_msi_irq_chip(phb, virq);
2618
2619 return 0;
2620}
2621EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2622#endif
2623
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002624static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002625 unsigned int hwirq, unsigned int virq,
2626 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002627{
2628 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2629 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002630 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002631 int rc;
2632
2633 /* No PE assigned ? bail out ... no MSI for you ! */
2634 if (pe == NULL)
2635 return -ENXIO;
2636
2637 /* Check if we have an MVE */
2638 if (pe->mve_number < 0)
2639 return -ENXIO;
2640
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002641 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002642 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002643 is_64 = 0;
2644
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002645 /* Assign XIVE to PE */
2646 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2647 if (rc) {
2648 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2649 pci_name(dev), rc, xive_num);
2650 return -EIO;
2651 }
2652
2653 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002654 __be64 addr64;
2655
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002656 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2657 &addr64, &data);
2658 if (rc) {
2659 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2660 pci_name(dev), rc);
2661 return -EIO;
2662 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002663 msg->address_hi = be64_to_cpu(addr64) >> 32;
2664 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002665 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002666 __be32 addr32;
2667
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002668 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2669 &addr32, &data);
2670 if (rc) {
2671 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2672 pci_name(dev), rc);
2673 return -EIO;
2674 }
2675 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002676 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002677 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002678 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002679
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002680 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002681
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002682 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2683 " address=%x_%08x data=%x PE# %d\n",
2684 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2685 msg->address_hi, msg->address_lo, data, pe->pe_number);
2686
2687 return 0;
2688}
2689
2690static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2691{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002692 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002693 const __be32 *prop = of_get_property(phb->hose->dn,
2694 "ibm,opal-msi-ranges", NULL);
2695 if (!prop) {
2696 /* BML Fallback */
2697 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2698 }
2699 if (!prop)
2700 return;
2701
2702 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002703 count = be32_to_cpup(prop + 1);
2704 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002705 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2706 phb->hose->global_number);
2707 return;
2708 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002709
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002710 phb->msi_setup = pnv_pci_ioda_msi_setup;
2711 phb->msi32_support = 1;
2712 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002713 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002714}
2715#else
2716static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2717#endif /* CONFIG_PCI_MSI */
2718
Wei Yang6e628c72015-03-25 16:23:55 +08002719#ifdef CONFIG_PCI_IOV
2720static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2721{
2722 struct pci_controller *hose;
2723 struct pnv_phb *phb;
2724 struct resource *res;
2725 int i;
2726 resource_size_t size;
2727 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002728 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002729
2730 if (!pdev->is_physfn || pdev->is_added)
2731 return;
2732
2733 hose = pci_bus_to_host(pdev->bus);
2734 phb = hose->private_data;
2735
2736 pdn = pci_get_pdn(pdev);
2737 pdn->vfs_expanded = 0;
2738
Wei Yang5b88ec22015-03-25 16:23:58 +08002739 total_vfs = pci_sriov_get_totalvfs(pdev);
2740 pdn->m64_per_iov = 1;
2741 mul = phb->ioda.total_pe;
2742
2743 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2744 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2745 if (!res->flags || res->parent)
2746 continue;
2747 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2748 dev_warn(&pdev->dev, " non M64 VF BAR%d: %pR\n",
2749 i, res);
2750 continue;
2751 }
2752
2753 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
2754
2755 /* bigger than 64M */
2756 if (size > (1 << 26)) {
2757 dev_info(&pdev->dev, "PowerNV: VF BAR%d: %pR IOV size is bigger than 64M, roundup power2\n",
2758 i, res);
2759 pdn->m64_per_iov = M64_PER_IOV;
2760 mul = roundup_pow_of_two(total_vfs);
2761 break;
2762 }
2763 }
2764
Wei Yang6e628c72015-03-25 16:23:55 +08002765 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2766 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2767 if (!res->flags || res->parent)
2768 continue;
2769 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2770 dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
2771 i, res);
2772 continue;
2773 }
2774
2775 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
2776 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002777 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08002778 dev_dbg(&pdev->dev, " %pR\n", res);
2779 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08002780 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08002781 }
Wei Yang5b88ec22015-03-25 16:23:58 +08002782 pdn->vfs_expanded = mul;
Wei Yang6e628c72015-03-25 16:23:55 +08002783}
2784#endif /* CONFIG_PCI_IOV */
2785
Gavin Shan11685be2012-08-20 03:49:16 +00002786/*
2787 * This function is supposed to be called on basis of PE from top
2788 * to bottom style. So the the I/O or MMIO segment assigned to
2789 * parent PE could be overrided by its child PEs if necessary.
2790 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002791static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
2792 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00002793{
2794 struct pnv_phb *phb = hose->private_data;
2795 struct pci_bus_region region;
2796 struct resource *res;
2797 int i, index;
2798 int rc;
2799
2800 /*
2801 * NOTE: We only care PCI bus based PE for now. For PCI
2802 * device based PE, for example SRIOV sensitive VF should
2803 * be figured out later.
2804 */
2805 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2806
2807 pci_bus_for_each_resource(pe->pbus, res, i) {
2808 if (!res || !res->flags ||
2809 res->start > res->end)
2810 continue;
2811
2812 if (res->flags & IORESOURCE_IO) {
2813 region.start = res->start - phb->ioda.io_pci_base;
2814 region.end = res->end - phb->ioda.io_pci_base;
2815 index = region.start / phb->ioda.io_segsize;
2816
2817 while (index < phb->ioda.total_pe &&
2818 region.start <= region.end) {
2819 phb->ioda.io_segmap[index] = pe->pe_number;
2820 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2821 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2822 if (rc != OPAL_SUCCESS) {
2823 pr_err("%s: OPAL error %d when mapping IO "
2824 "segment #%d to PE#%d\n",
2825 __func__, rc, index, pe->pe_number);
2826 break;
2827 }
2828
2829 region.start += phb->ioda.io_segsize;
2830 index++;
2831 }
Gavin Shan027fa022015-03-27 11:29:00 +11002832 } else if ((res->flags & IORESOURCE_MEM) &&
2833 !pnv_pci_is_mem_pref_64(res->flags)) {
Gavin Shan11685be2012-08-20 03:49:16 +00002834 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002835 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002836 phb->ioda.m32_pci_base;
2837 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002838 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002839 phb->ioda.m32_pci_base;
2840 index = region.start / phb->ioda.m32_segsize;
2841
2842 while (index < phb->ioda.total_pe &&
2843 region.start <= region.end) {
2844 phb->ioda.m32_segmap[index] = pe->pe_number;
2845 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2846 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2847 if (rc != OPAL_SUCCESS) {
2848 pr_err("%s: OPAL error %d when mapping M32 "
2849 "segment#%d to PE#%d",
2850 __func__, rc, index, pe->pe_number);
2851 break;
2852 }
2853
2854 region.start += phb->ioda.m32_segsize;
2855 index++;
2856 }
2857 }
2858 }
2859}
2860
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002861static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00002862{
2863 struct pci_controller *tmp, *hose;
2864 struct pnv_phb *phb;
2865 struct pnv_ioda_pe *pe;
2866
2867 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2868 phb = hose->private_data;
2869 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2870 pnv_ioda_setup_pe_seg(hose, pe);
2871 }
2872 }
2873}
2874
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002875static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00002876{
2877 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00002878 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00002879
2880 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2881 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00002882
2883 /* Mark the PHB initialization done */
2884 phb = hose->private_data;
2885 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00002886 }
2887}
2888
Gavin Shan37c367f2013-06-20 18:13:25 +08002889static void pnv_pci_ioda_create_dbgfs(void)
2890{
2891#ifdef CONFIG_DEBUG_FS
2892 struct pci_controller *hose, *tmp;
2893 struct pnv_phb *phb;
2894 char name[16];
2895
2896 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2897 phb = hose->private_data;
2898
2899 sprintf(name, "PCI%04x", hose->global_number);
2900 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
2901 if (!phb->dbgfs)
2902 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
2903 __func__, hose->global_number);
2904 }
2905#endif /* CONFIG_DEBUG_FS */
2906}
2907
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002908static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00002909{
2910 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00002911 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00002912 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002913
Gavin Shan37c367f2013-06-20 18:13:25 +08002914 pnv_pci_ioda_create_dbgfs();
2915
Gavin Shane9cc17d2013-06-20 13:21:14 +08002916#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08002917 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04002918 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002919#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00002920}
2921
Gavin Shan271fd032012-09-11 16:59:47 -06002922/*
2923 * Returns the alignment for I/O or memory windows for P2P
2924 * bridges. That actually depends on how PEs are segmented.
2925 * For now, we return I/O or M32 segment size for PE sensitive
2926 * P2P bridges. Otherwise, the default values (4KiB for I/O,
2927 * 1MiB for memory) will be returned.
2928 *
2929 * The current PCI bus might be put into one PE, which was
2930 * create against the parent PCI bridge. For that case, we
2931 * needn't enlarge the alignment so that we can save some
2932 * resources.
2933 */
2934static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2935 unsigned long type)
2936{
2937 struct pci_dev *bridge;
2938 struct pci_controller *hose = pci_bus_to_host(bus);
2939 struct pnv_phb *phb = hose->private_data;
2940 int num_pci_bridges = 0;
2941
2942 bridge = bus->self;
2943 while (bridge) {
2944 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2945 num_pci_bridges++;
2946 if (num_pci_bridges >= 2)
2947 return 1;
2948 }
2949
2950 bridge = bridge->bus->self;
2951 }
2952
Guo Chao262af552014-07-21 14:42:30 +10002953 /* We fail back to M32 if M64 isn't supported */
2954 if (phb->ioda.m64_segsize &&
2955 pnv_pci_is_mem_pref_64(type))
2956 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06002957 if (type & IORESOURCE_MEM)
2958 return phb->ioda.m32_segsize;
2959
2960 return phb->ioda.io_segsize;
2961}
2962
Wei Yang5350ab32015-03-25 16:23:56 +08002963#ifdef CONFIG_PCI_IOV
2964static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
2965 int resno)
2966{
2967 struct pci_dn *pdn = pci_get_pdn(pdev);
2968 resource_size_t align, iov_align;
2969
2970 iov_align = resource_size(&pdev->resource[resno]);
2971 if (iov_align)
2972 return iov_align;
2973
2974 align = pci_iov_resource_size(pdev, resno);
2975 if (pdn->vfs_expanded)
2976 return pdn->vfs_expanded * align;
2977
2978 return align;
2979}
2980#endif /* CONFIG_PCI_IOV */
2981
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002982/* Prevent enabling devices for which we couldn't properly
2983 * assign a PE
2984 */
Daniel Axtensc88c2a12015-03-31 16:00:41 +11002985static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002986{
Gavin Shandb1266c2012-08-20 03:49:18 +00002987 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2988 struct pnv_phb *phb = hose->private_data;
2989 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002990
Gavin Shandb1266c2012-08-20 03:49:18 +00002991 /* The function is probably called while the PEs have
2992 * not be created yet. For example, resource reassignment
2993 * during PCI probe period. We just skip the check if
2994 * PEs isn't ready.
2995 */
2996 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11002997 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00002998
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002999 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003000 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003001 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003002
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003003 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003004}
3005
3006static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
3007 u32 devfn)
3008{
3009 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
3010}
3011
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003012static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003013{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003014 struct pnv_phb *phb = hose->private_data;
3015
Gavin Shand1a85ee2014-09-30 12:39:05 +10003016 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003017 OPAL_ASSERT_RESET);
3018}
3019
Daniel Axtens92ae0352015-04-28 15:12:05 +10003020static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
3021 .dma_dev_setup = pnv_pci_dma_dev_setup,
3022#ifdef CONFIG_PCI_MSI
3023 .setup_msi_irqs = pnv_setup_msi_irqs,
3024 .teardown_msi_irqs = pnv_teardown_msi_irqs,
3025#endif
3026 .enable_device_hook = pnv_pci_enable_device_hook,
3027 .window_alignment = pnv_pci_window_alignment,
3028 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
Daniel Axtens763d2d82015-04-28 15:12:07 +10003029 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
Andrew Donnellan535229822015-08-07 13:45:54 +10003030 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003031 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003032};
3033
Anton Blancharde51df2c2014-08-20 08:55:18 +10003034static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3035 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003036{
3037 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003038 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08003039 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10003040 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003041 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003042 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003043 u64 phb_id;
3044 void *aux;
3045 long rc;
3046
Gavin Shan58d714e2013-07-31 16:47:00 +08003047 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003048
3049 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3050 if (!prop64) {
3051 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3052 return;
3053 }
3054 phb_id = be64_to_cpup(prop64);
3055 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3056
Michael Ellermane39f223f2014-11-18 16:47:35 +11003057 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003058
3059 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003060 phb->hose = hose = pcibios_alloc_controller(np);
3061 if (!phb->hose) {
3062 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003063 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003064 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003065 return;
3066 }
3067
3068 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003069 prop32 = of_get_property(np, "bus-range", &len);
3070 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003071 hose->first_busno = be32_to_cpu(prop32[0]);
3072 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003073 } else {
3074 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3075 hose->first_busno = 0;
3076 hose->last_busno = 0xff;
3077 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003078 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003079 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003080 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003081 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003082 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003083
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003084 /* Detect specific models for error handling */
3085 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3086 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003087 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003088 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003089 else
3090 phb->model = PNV_PHB_MODEL_UNKNOWN;
3091
Gavin Shanaa0c0332013-04-25 19:20:57 +00003092 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003093 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003094
Gavin Shanaa0c0332013-04-25 19:20:57 +00003095 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003096 phb->regs = of_iomap(np, 0);
3097 if (phb->regs == NULL)
3098 pr_err(" Failed to map registers !\n");
3099
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003100 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08003101 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003102 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003103 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003104 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003105 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3106 if (prop32)
3107 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003108
3109 /* Parse 64-bit MMIO range */
3110 pnv_ioda_parse_m64_window(phb);
3111
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003112 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003113 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003114 phb->ioda.m32_size += 0x10000;
3115
3116 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003117 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003118 phb->ioda.io_size = hose->pci_io_size;
3119 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
3120 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3121
Gavin Shanc35d2a82013-07-31 16:47:04 +08003122 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003123 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
3124 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00003125 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003126 if (phb->type == PNV_PHB_IODA1) {
3127 iomap_off = size;
3128 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
3129 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003130 pemap_off = size;
3131 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003132 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003133 phb->ioda.pe_alloc = aux;
3134 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08003135 if (phb->type == PNV_PHB_IODA1)
3136 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003137 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08003138 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003139
Gavin Shan7ebdf952012-08-20 03:49:15 +00003140 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003141 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003142 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003143
3144 /* Calculate how many 32-bit TCE segments we have */
3145 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
3146
Gavin Shanaa0c0332013-04-25 19:20:57 +00003147#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003148 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3149 window_type,
3150 window_num,
3151 starting_real_address,
3152 starting_pci_address,
3153 segment_size);
3154#endif
3155
Guo Chao262af552014-07-21 14:42:30 +10003156 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
3157 phb->ioda.total_pe, phb->ioda.reserved_pe,
3158 phb->ioda.m32_size, phb->ioda.m32_segsize);
3159 if (phb->ioda.m64_size)
3160 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3161 phb->ioda.m64_size, phb->ioda.m64_segsize);
3162 if (phb->ioda.io_size)
3163 pr_info(" IO: 0x%x [segment=0x%x]\n",
3164 phb->ioda.io_size, phb->ioda.io_segsize);
3165
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003166
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003167 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003168 phb->get_pe_state = pnv_ioda_get_pe_state;
3169 phb->freeze_pe = pnv_ioda_freeze_pe;
3170 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003171
3172 /* Setup RID -> PE mapping function */
3173 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
3174
3175 /* Setup TCEs */
3176 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
3177
3178 /* Setup MSI support */
3179 pnv_pci_init_ioda_msis(phb);
3180
Gavin Shanc40a4212012-08-20 03:49:20 +00003181 /*
3182 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3183 * to let the PCI core do resource assignment. It's supposed
3184 * that the PCI core will do correct I/O and MMIO alignment
3185 * for the P2P bridge bars so that each PCI bus (excluding
3186 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003187 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003188 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Daniel Axtens92ae0352015-04-28 15:12:05 +10003189 hose->controller_ops = pnv_pci_ioda_controller_ops;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003190
Wei Yang6e628c72015-03-25 16:23:55 +08003191#ifdef CONFIG_PCI_IOV
3192 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003193 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003194#endif
3195
Gavin Shanc40a4212012-08-20 03:49:20 +00003196 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003197
3198 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003199 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003200 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003201 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003202
3203 /* If we're running in kdump kerenl, the previous kerenl never
3204 * shutdown PCI devices correctly. We already got IODA table
3205 * cleaned out. So we have to issue PHB reset to stop all PCI
3206 * transactions from previous kerenl.
3207 */
3208 if (is_kdump_kernel()) {
3209 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003210 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3211 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003212 }
Guo Chao262af552014-07-21 14:42:30 +10003213
Gavin Shan9e9e8932014-11-12 13:36:05 +11003214 /* Remove M64 resource if we can't configure it successfully */
3215 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003216 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003217}
3218
Bjorn Helgaas67975002013-07-02 12:20:03 -06003219void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003220{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003221 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003222}
3223
3224void __init pnv_pci_init_ioda_hub(struct device_node *np)
3225{
3226 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003227 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003228 u64 hub_id;
3229
3230 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3231
3232 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3233 if (!prop64) {
3234 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3235 return;
3236 }
3237 hub_id = be64_to_cpup(prop64);
3238 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3239
3240 /* Count child PHBs */
3241 for_each_child_of_node(np, phbn) {
3242 /* Look for IODA1 PHBs */
3243 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003244 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003245 }
3246}