blob: 272f6566d790cddc175673b0bc97f1913540cc03 [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)
Guo Chao262af552014-07-21 14:42:30 +1000262{
Guo Chao262af552014-07-21 14:42:30 +1000263 struct pci_dev *pdev;
Gavin Shan96a2f922015-06-19 12:26:17 +1000264
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
Stewart Smithe4d54f72015-12-09 17:18:20 +1100347 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
Guo Chao262af552014-07-21 14:42:30 +1000348 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];
Gavin Shane80c4e72015-10-22 12:03:08 +1100360 res->name = dn->full_name;
Guo Chao262af552014-07-21 14:42:30 +1000361 res->start = of_translate_address(dn, r + 2);
362 res->end = res->start + of_read_number(r + 4, 2) - 1;
363 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
364 pci_addr = of_read_number(r, 2);
365 hose->mem_offset[1] = res->start - pci_addr;
366
367 phb->ioda.m64_size = resource_size(res);
368 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
369 phb->ioda.m64_base = pci_addr;
370
Wei Yange9863e62014-12-12 12:39:37 +0800371 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
372 res->start, res->end, pci_addr);
373
Guo Chao262af552014-07-21 14:42:30 +1000374 /* Use last M64 BAR to cover M64 window */
375 phb->ioda.m64_bar_idx = 15;
376 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shan5ef73562014-11-12 13:36:06 +1100377 phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000378 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
379}
380
Gavin Shan49dec922014-07-21 14:42:33 +1000381static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
382{
383 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
384 struct pnv_ioda_pe *slave;
385 s64 rc;
386
387 /* Fetch master PE */
388 if (pe->flags & PNV_IODA_PE_SLAVE) {
389 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100390 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
391 return;
392
Gavin Shan49dec922014-07-21 14:42:33 +1000393 pe_no = pe->pe_number;
394 }
395
396 /* Freeze master PE */
397 rc = opal_pci_eeh_freeze_set(phb->opal_id,
398 pe_no,
399 OPAL_EEH_ACTION_SET_FREEZE_ALL);
400 if (rc != OPAL_SUCCESS) {
401 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
402 __func__, rc, phb->hose->global_number, pe_no);
403 return;
404 }
405
406 /* Freeze slave PEs */
407 if (!(pe->flags & PNV_IODA_PE_MASTER))
408 return;
409
410 list_for_each_entry(slave, &pe->slaves, list) {
411 rc = opal_pci_eeh_freeze_set(phb->opal_id,
412 slave->pe_number,
413 OPAL_EEH_ACTION_SET_FREEZE_ALL);
414 if (rc != OPAL_SUCCESS)
415 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
416 __func__, rc, phb->hose->global_number,
417 slave->pe_number);
418 }
419}
420
Anton Blancharde51df2c2014-08-20 08:55:18 +1000421static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000422{
423 struct pnv_ioda_pe *pe, *slave;
424 s64 rc;
425
426 /* Find master PE */
427 pe = &phb->ioda.pe_array[pe_no];
428 if (pe->flags & PNV_IODA_PE_SLAVE) {
429 pe = pe->master;
430 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
431 pe_no = pe->pe_number;
432 }
433
434 /* Clear frozen state for master PE */
435 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
436 if (rc != OPAL_SUCCESS) {
437 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
438 __func__, rc, opt, phb->hose->global_number, pe_no);
439 return -EIO;
440 }
441
442 if (!(pe->flags & PNV_IODA_PE_MASTER))
443 return 0;
444
445 /* Clear frozen state for slave PEs */
446 list_for_each_entry(slave, &pe->slaves, list) {
447 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
448 slave->pe_number,
449 opt);
450 if (rc != OPAL_SUCCESS) {
451 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
452 __func__, rc, opt, phb->hose->global_number,
453 slave->pe_number);
454 return -EIO;
455 }
456 }
457
458 return 0;
459}
460
461static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
462{
463 struct pnv_ioda_pe *slave, *pe;
464 u8 fstate, state;
465 __be16 pcierr;
466 s64 rc;
467
468 /* Sanity check on PE number */
469 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
470 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
471
472 /*
473 * Fetch the master PE and the PE instance might be
474 * not initialized yet.
475 */
476 pe = &phb->ioda.pe_array[pe_no];
477 if (pe->flags & PNV_IODA_PE_SLAVE) {
478 pe = pe->master;
479 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
480 pe_no = pe->pe_number;
481 }
482
483 /* Check the master PE */
484 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
485 &state, &pcierr, NULL);
486 if (rc != OPAL_SUCCESS) {
487 pr_warn("%s: Failure %lld getting "
488 "PHB#%x-PE#%x state\n",
489 __func__, rc,
490 phb->hose->global_number, pe_no);
491 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
492 }
493
494 /* Check the slave PE */
495 if (!(pe->flags & PNV_IODA_PE_MASTER))
496 return state;
497
498 list_for_each_entry(slave, &pe->slaves, list) {
499 rc = opal_pci_eeh_freeze_status(phb->opal_id,
500 slave->pe_number,
501 &fstate,
502 &pcierr,
503 NULL);
504 if (rc != OPAL_SUCCESS) {
505 pr_warn("%s: Failure %lld getting "
506 "PHB#%x-PE#%x state\n",
507 __func__, rc,
508 phb->hose->global_number, slave->pe_number);
509 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
510 }
511
512 /*
513 * Override the result based on the ascending
514 * priority.
515 */
516 if (fstate > state)
517 state = fstate;
518 }
519
520 return state;
521}
522
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000523/* Currently those 2 are only used when MSIs are enabled, this will change
524 * but in the meantime, we need to protect them to avoid warnings
525 */
526#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800527static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000528{
529 struct pci_controller *hose = pci_bus_to_host(dev->bus);
530 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000531 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000532
533 if (!pdn)
534 return NULL;
535 if (pdn->pe_number == IODA_INVALID_PE)
536 return NULL;
537 return &phb->ioda.pe_array[pdn->pe_number];
538}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000539#endif /* CONFIG_PCI_MSI */
540
Gavin Shanb131a842014-11-12 13:36:08 +1100541static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
542 struct pnv_ioda_pe *parent,
543 struct pnv_ioda_pe *child,
544 bool is_add)
545{
546 const char *desc = is_add ? "adding" : "removing";
547 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
548 OPAL_REMOVE_PE_FROM_DOMAIN;
549 struct pnv_ioda_pe *slave;
550 long rc;
551
552 /* Parent PE affects child PE */
553 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
554 child->pe_number, op);
555 if (rc != OPAL_SUCCESS) {
556 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
557 rc, desc);
558 return -ENXIO;
559 }
560
561 if (!(child->flags & PNV_IODA_PE_MASTER))
562 return 0;
563
564 /* Compound case: parent PE affects slave PEs */
565 list_for_each_entry(slave, &child->slaves, list) {
566 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
567 slave->pe_number, op);
568 if (rc != OPAL_SUCCESS) {
569 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
570 rc, desc);
571 return -ENXIO;
572 }
573 }
574
575 return 0;
576}
577
578static int pnv_ioda_set_peltv(struct pnv_phb *phb,
579 struct pnv_ioda_pe *pe,
580 bool is_add)
581{
582 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800583 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100584 int ret;
585
586 /*
587 * Clear PE frozen state. If it's master PE, we need
588 * clear slave PE frozen state as well.
589 */
590 if (is_add) {
591 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
592 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
593 if (pe->flags & PNV_IODA_PE_MASTER) {
594 list_for_each_entry(slave, &pe->slaves, list)
595 opal_pci_eeh_freeze_clear(phb->opal_id,
596 slave->pe_number,
597 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
598 }
599 }
600
601 /*
602 * Associate PE in PELT. We need add the PE into the
603 * corresponding PELT-V as well. Otherwise, the error
604 * originated from the PE might contribute to other
605 * PEs.
606 */
607 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
608 if (ret)
609 return ret;
610
611 /* For compound PEs, any one affects all of them */
612 if (pe->flags & PNV_IODA_PE_MASTER) {
613 list_for_each_entry(slave, &pe->slaves, list) {
614 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
615 if (ret)
616 return ret;
617 }
618 }
619
620 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
621 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800622 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100623 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800624#ifdef CONFIG_PCI_IOV
625 else if (pe->flags & PNV_IODA_PE_VF)
Gavin Shan283e2d82015-06-22 13:45:47 +1000626 pdev = pe->parent_dev;
Wei Yang781a8682015-03-25 16:23:57 +0800627#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100628 while (pdev) {
629 struct pci_dn *pdn = pci_get_pdn(pdev);
630 struct pnv_ioda_pe *parent;
631
632 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
633 parent = &phb->ioda.pe_array[pdn->pe_number];
634 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
635 if (ret)
636 return ret;
637 }
638
639 pdev = pdev->bus->self;
640 }
641
642 return 0;
643}
644
Wei Yang781a8682015-03-25 16:23:57 +0800645#ifdef CONFIG_PCI_IOV
646static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
647{
648 struct pci_dev *parent;
649 uint8_t bcomp, dcomp, fcomp;
650 int64_t rc;
651 long rid_end, rid;
652
653 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
654 if (pe->pbus) {
655 int count;
656
657 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
658 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
659 parent = pe->pbus->self;
660 if (pe->flags & PNV_IODA_PE_BUS_ALL)
661 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
662 else
663 count = 1;
664
665 switch(count) {
666 case 1: bcomp = OpalPciBusAll; break;
667 case 2: bcomp = OpalPciBus7Bits; break;
668 case 4: bcomp = OpalPciBus6Bits; break;
669 case 8: bcomp = OpalPciBus5Bits; break;
670 case 16: bcomp = OpalPciBus4Bits; break;
671 case 32: bcomp = OpalPciBus3Bits; break;
672 default:
673 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
674 count);
675 /* Do an exact match only */
676 bcomp = OpalPciBusAll;
677 }
678 rid_end = pe->rid + (count << 8);
679 } else {
680 if (pe->flags & PNV_IODA_PE_VF)
681 parent = pe->parent_dev;
682 else
683 parent = pe->pdev->bus->self;
684 bcomp = OpalPciBusAll;
685 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
686 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
687 rid_end = pe->rid + 1;
688 }
689
690 /* Clear the reverse map */
691 for (rid = pe->rid; rid < rid_end; rid++)
692 phb->ioda.pe_rmap[rid] = 0;
693
694 /* Release from all parents PELT-V */
695 while (parent) {
696 struct pci_dn *pdn = pci_get_pdn(parent);
697 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
698 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
699 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
700 /* XXX What to do in case of error ? */
701 }
702 parent = parent->bus->self;
703 }
704
Gavin Shanf951e512015-06-23 17:01:13 +1000705 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
Wei Yang781a8682015-03-25 16:23:57 +0800706 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
707
708 /* Disassociate PE in PELT */
709 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
710 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
711 if (rc)
712 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
713 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
714 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
715 if (rc)
716 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
717
718 pe->pbus = NULL;
719 pe->pdev = NULL;
720 pe->parent_dev = NULL;
721
722 return 0;
723}
724#endif /* CONFIG_PCI_IOV */
725
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800726static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000727{
728 struct pci_dev *parent;
729 uint8_t bcomp, dcomp, fcomp;
730 long rc, rid_end, rid;
731
732 /* Bus validation ? */
733 if (pe->pbus) {
734 int count;
735
736 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
737 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
738 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000739 if (pe->flags & PNV_IODA_PE_BUS_ALL)
740 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
741 else
742 count = 1;
743
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000744 switch(count) {
745 case 1: bcomp = OpalPciBusAll; break;
746 case 2: bcomp = OpalPciBus7Bits; break;
747 case 4: bcomp = OpalPciBus6Bits; break;
748 case 8: bcomp = OpalPciBus5Bits; break;
749 case 16: bcomp = OpalPciBus4Bits; break;
750 case 32: bcomp = OpalPciBus3Bits; break;
751 default:
Wei Yang781a8682015-03-25 16:23:57 +0800752 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
753 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000754 /* Do an exact match only */
755 bcomp = OpalPciBusAll;
756 }
757 rid_end = pe->rid + (count << 8);
758 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800759#ifdef CONFIG_PCI_IOV
760 if (pe->flags & PNV_IODA_PE_VF)
761 parent = pe->parent_dev;
762 else
763#endif /* CONFIG_PCI_IOV */
764 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000765 bcomp = OpalPciBusAll;
766 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
767 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
768 rid_end = pe->rid + 1;
769 }
770
Gavin Shan631ad692013-11-04 16:32:46 +0800771 /*
772 * Associate PE in PELT. We need add the PE into the
773 * corresponding PELT-V as well. Otherwise, the error
774 * originated from the PE might contribute to other
775 * PEs.
776 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000777 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
778 bcomp, dcomp, fcomp, OPAL_MAP_PE);
779 if (rc) {
780 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
781 return -ENXIO;
782 }
Gavin Shan631ad692013-11-04 16:32:46 +0800783
Gavin Shanb131a842014-11-12 13:36:08 +1100784 /* Configure PELTV */
785 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000786
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000787 /* Setup reverse map */
788 for (rid = pe->rid; rid < rid_end; rid++)
789 phb->ioda.pe_rmap[rid] = pe->pe_number;
790
791 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100792 if (phb->type != PNV_PHB_IODA1) {
793 pe->mve_number = 0;
794 goto out;
795 }
796
797 pe->mve_number = pe->pe_number;
798 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
799 if (rc != OPAL_SUCCESS) {
800 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
801 rc, pe->mve_number);
802 pe->mve_number = -1;
803 } else {
804 rc = opal_pci_set_mve_enable(phb->opal_id,
805 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000806 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100807 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000808 rc, pe->mve_number);
809 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000810 }
Gavin Shan4773f762014-11-12 13:36:09 +1100811 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000812
Gavin Shan4773f762014-11-12 13:36:09 +1100813out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000814 return 0;
815}
816
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800817static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
818 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000819{
820 struct pnv_ioda_pe *lpe;
821
Gavin Shan7ebdf952012-08-20 03:49:15 +0000822 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000823 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000824 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000825 return;
826 }
827 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000828 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000829}
830
831static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
832{
833 /* This is quite simplistic. The "base" weight of a device
834 * is 10. 0 means no DMA is to be accounted for it.
835 */
836
837 /* If it's a bridge, no DMA */
838 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
839 return 0;
840
841 /* Reduce the weight of slow USB controllers */
842 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
843 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
844 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
845 return 3;
846
847 /* Increase the weight of RAID (includes Obsidian) */
848 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
849 return 15;
850
851 /* Default */
852 return 10;
853}
854
Wei Yang781a8682015-03-25 16:23:57 +0800855#ifdef CONFIG_PCI_IOV
856static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
857{
858 struct pci_dn *pdn = pci_get_pdn(dev);
859 int i;
860 struct resource *res, res2;
861 resource_size_t size;
862 u16 num_vfs;
863
864 if (!dev->is_physfn)
865 return -EINVAL;
866
867 /*
868 * "offset" is in VFs. The M64 windows are sized so that when they
869 * are segmented, each segment is the same size as the IOV BAR.
870 * Each segment is in a separate PE, and the high order bits of the
871 * address are the PE number. Therefore, each VF's BAR is in a
872 * separate PE, and changing the IOV BAR start address changes the
873 * range of PEs the VFs are in.
874 */
875 num_vfs = pdn->num_vfs;
876 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
877 res = &dev->resource[i + PCI_IOV_RESOURCES];
878 if (!res->flags || !res->parent)
879 continue;
880
881 if (!pnv_pci_is_mem_pref_64(res->flags))
882 continue;
883
884 /*
885 * The actual IOV BAR range is determined by the start address
886 * and the actual size for num_vfs VFs BAR. This check is to
887 * make sure that after shifting, the range will not overlap
888 * with another device.
889 */
890 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
891 res2.flags = res->flags;
892 res2.start = res->start + (size * offset);
893 res2.end = res2.start + (size * num_vfs) - 1;
894
895 if (res2.end > res->end) {
896 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
897 i, &res2, res, num_vfs, offset);
898 return -EBUSY;
899 }
900 }
901
902 /*
903 * After doing so, there would be a "hole" in the /proc/iomem when
904 * offset is a positive value. It looks like the device return some
905 * mmio back to the system, which actually no one could use it.
906 */
907 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
908 res = &dev->resource[i + PCI_IOV_RESOURCES];
909 if (!res->flags || !res->parent)
910 continue;
911
912 if (!pnv_pci_is_mem_pref_64(res->flags))
913 continue;
914
915 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
916 res2 = *res;
917 res->start += size * offset;
918
Wei Yang74703cc2015-07-20 18:14:58 +0800919 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
920 i, &res2, res, (offset > 0) ? "En" : "Dis",
921 num_vfs, offset);
Wei Yang781a8682015-03-25 16:23:57 +0800922 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
923 }
924 return 0;
925}
926#endif /* CONFIG_PCI_IOV */
927
Gavin Shanfb446ad2012-08-20 03:49:14 +0000928#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800929static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000930{
931 struct pci_controller *hose = pci_bus_to_host(dev->bus);
932 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000933 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000934 struct pnv_ioda_pe *pe;
935 int pe_num;
936
937 if (!pdn) {
938 pr_err("%s: Device tree node not associated properly\n",
939 pci_name(dev));
940 return NULL;
941 }
942 if (pdn->pe_number != IODA_INVALID_PE)
943 return NULL;
944
945 /* PE#0 has been pre-set */
946 if (dev->bus->number == 0)
947 pe_num = 0;
948 else
949 pe_num = pnv_ioda_alloc_pe(phb);
950 if (pe_num == IODA_INVALID_PE) {
951 pr_warning("%s: Not enough PE# available, disabling device\n",
952 pci_name(dev));
953 return NULL;
954 }
955
956 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
957 * pointer in the PE data structure, both should be destroyed at the
958 * same time. However, this needs to be looked at more closely again
959 * once we actually start removing things (Hotplug, SR-IOV, ...)
960 *
961 * At some point we want to remove the PDN completely anyways
962 */
963 pe = &phb->ioda.pe_array[pe_num];
964 pci_dev_get(dev);
965 pdn->pcidev = dev;
966 pdn->pe_number = pe_num;
967 pe->pdev = dev;
968 pe->pbus = NULL;
969 pe->tce32_seg = -1;
970 pe->mve_number = -1;
971 pe->rid = dev->bus->number << 8 | pdn->devfn;
972
973 pe_info(pe, "Associated device to PE\n");
974
975 if (pnv_ioda_configure_pe(phb, pe)) {
976 /* XXX What do we do here ? */
977 if (pe_num)
978 pnv_ioda_free_pe(phb, pe_num);
979 pdn->pe_number = IODA_INVALID_PE;
980 pe->pdev = NULL;
981 pci_dev_put(dev);
982 return NULL;
983 }
984
985 /* Assign a DMA weight to the device */
986 pe->dma_weight = pnv_ioda_dma_weight(dev);
987 if (pe->dma_weight != 0) {
988 phb->ioda.dma_weight += pe->dma_weight;
989 phb->ioda.dma_pe_count++;
990 }
991
992 /* Link the PE */
993 pnv_ioda_link_pe_by_weight(phb, pe);
994
995 return pe;
996}
Gavin Shanfb446ad2012-08-20 03:49:14 +0000997#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000998
999static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1000{
1001 struct pci_dev *dev;
1002
1003 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001004 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001005
1006 if (pdn == NULL) {
1007 pr_warn("%s: No device node associated with device !\n",
1008 pci_name(dev));
1009 continue;
1010 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001011 pdn->pe_number = pe->pe_number;
1012 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001013 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001014 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1015 }
1016}
1017
Gavin Shanfb446ad2012-08-20 03:49:14 +00001018/*
1019 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1020 * single PCI bus. Another one that contains the primary PCI bus and its
1021 * subordinate PCI devices and buses. The second type of PE is normally
1022 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1023 */
Gavin Shand1203852015-06-19 12:26:18 +10001024static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001025{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001026 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001027 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001028 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +10001029 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001030
Guo Chao262af552014-07-21 14:42:30 +10001031 /* Check if PE is determined by M64 */
1032 if (phb->pick_m64_pe)
Gavin Shan26ba2482015-06-19 12:26:19 +10001033 pe_num = phb->pick_m64_pe(bus, all);
Guo Chao262af552014-07-21 14:42:30 +10001034
1035 /* The PE number isn't pinned by M64 */
1036 if (pe_num == IODA_INVALID_PE)
1037 pe_num = pnv_ioda_alloc_pe(phb);
1038
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001039 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001040 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1041 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001042 return;
1043 }
1044
1045 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +10001046 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001047 pe->pbus = bus;
1048 pe->pdev = NULL;
1049 pe->tce32_seg = -1;
1050 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001051 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001052 pe->dma_weight = 0;
1053
Gavin Shanfb446ad2012-08-20 03:49:14 +00001054 if (all)
1055 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
1056 bus->busn_res.start, bus->busn_res.end, pe_num);
1057 else
1058 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
1059 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001060
1061 if (pnv_ioda_configure_pe(phb, pe)) {
1062 /* XXX What do we do here ? */
1063 if (pe_num)
1064 pnv_ioda_free_pe(phb, pe_num);
1065 pe->pbus = NULL;
1066 return;
1067 }
1068
1069 /* Associate it with all child devices */
1070 pnv_ioda_setup_same_PE(bus, pe);
1071
Gavin Shan7ebdf952012-08-20 03:49:15 +00001072 /* Put PE to the list */
1073 list_add_tail(&pe->list, &phb->ioda.pe_list);
1074
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001075 /* Account for one DMA PE if at least one DMA capable device exist
1076 * below the bridge
1077 */
1078 if (pe->dma_weight != 0) {
1079 phb->ioda.dma_weight += pe->dma_weight;
1080 phb->ioda.dma_pe_count++;
1081 }
1082
1083 /* Link the PE */
1084 pnv_ioda_link_pe_by_weight(phb, pe);
1085}
1086
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001087static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001088{
1089 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001090
Gavin Shand1203852015-06-19 12:26:18 +10001091 pnv_ioda_setup_bus_PE(bus, false);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001092
1093 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001094 if (dev->subordinate) {
1095 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
Gavin Shand1203852015-06-19 12:26:18 +10001096 pnv_ioda_setup_bus_PE(dev->subordinate, true);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001097 else
1098 pnv_ioda_setup_PEs(dev->subordinate);
1099 }
1100 }
1101}
1102
1103/*
1104 * Configure PEs so that the downstream PCI buses and devices
1105 * could have their associated PE#. Unfortunately, we didn't
1106 * figure out the way to identify the PLX bridge yet. So we
1107 * simply put the PCI bus and the subordinate behind the root
1108 * port to PE# here. The game rule here is expected to be changed
1109 * as soon as we can detected PLX bridge correctly.
1110 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001111static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001112{
1113 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001114 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001115
1116 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001117 phb = hose->private_data;
1118
1119 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +11001120 if (phb->reserve_m64_pe)
Gavin Shan96a2f922015-06-19 12:26:17 +10001121 phb->reserve_m64_pe(hose->bus, NULL, true);
Guo Chao262af552014-07-21 14:42:30 +10001122
Gavin Shanfb446ad2012-08-20 03:49:14 +00001123 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001124 }
1125}
1126
Gavin Shana8b2f822015-03-25 16:23:52 +08001127#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +08001128static int pnv_pci_vf_release_m64(struct pci_dev *pdev)
1129{
1130 struct pci_bus *bus;
1131 struct pci_controller *hose;
1132 struct pnv_phb *phb;
1133 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001134 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001135
1136 bus = pdev->bus;
1137 hose = pci_bus_to_host(bus);
1138 phb = hose->private_data;
1139 pdn = pci_get_pdn(pdev);
1140
Wei Yang02639b02015-03-25 16:23:59 +08001141 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
1142 for (j = 0; j < M64_PER_IOV; j++) {
1143 if (pdn->m64_wins[i][j] == IODA_INVALID_M64)
1144 continue;
1145 opal_pci_phb_mmio_enable(phb->opal_id,
1146 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 0);
1147 clear_bit(pdn->m64_wins[i][j], &phb->ioda.m64_bar_alloc);
1148 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1149 }
Wei Yang781a8682015-03-25 16:23:57 +08001150
1151 return 0;
1152}
1153
Wei Yang02639b02015-03-25 16:23:59 +08001154static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001155{
1156 struct pci_bus *bus;
1157 struct pci_controller *hose;
1158 struct pnv_phb *phb;
1159 struct pci_dn *pdn;
1160 unsigned int win;
1161 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001162 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001163 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001164 int total_vfs;
1165 resource_size_t size, start;
1166 int pe_num;
1167 int vf_groups;
1168 int vf_per_group;
Wei Yang781a8682015-03-25 16:23:57 +08001169
1170 bus = pdev->bus;
1171 hose = pci_bus_to_host(bus);
1172 phb = hose->private_data;
1173 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001174 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001175
1176 /* Initialize the m64_wins to IODA_INVALID_M64 */
1177 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yang02639b02015-03-25 16:23:59 +08001178 for (j = 0; j < M64_PER_IOV; j++)
1179 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1180
1181 if (pdn->m64_per_iov == M64_PER_IOV) {
1182 vf_groups = (num_vfs <= M64_PER_IOV) ? num_vfs: M64_PER_IOV;
1183 vf_per_group = (num_vfs <= M64_PER_IOV)? 1:
1184 roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1185 } else {
1186 vf_groups = 1;
1187 vf_per_group = 1;
1188 }
Wei Yang781a8682015-03-25 16:23:57 +08001189
1190 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1191 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1192 if (!res->flags || !res->parent)
1193 continue;
1194
1195 if (!pnv_pci_is_mem_pref_64(res->flags))
1196 continue;
1197
Wei Yang02639b02015-03-25 16:23:59 +08001198 for (j = 0; j < vf_groups; j++) {
1199 do {
1200 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1201 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001202
Wei Yang02639b02015-03-25 16:23:59 +08001203 if (win >= phb->ioda.m64_bar_idx + 1)
1204 goto m64_failed;
1205 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001206
Wei Yang02639b02015-03-25 16:23:59 +08001207 pdn->m64_wins[i][j] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001208
Wei Yang02639b02015-03-25 16:23:59 +08001209 if (pdn->m64_per_iov == M64_PER_IOV) {
1210 size = pci_iov_resource_size(pdev,
1211 PCI_IOV_RESOURCES + i);
1212 size = size * vf_per_group;
1213 start = res->start + size * j;
1214 } else {
1215 size = resource_size(res);
1216 start = res->start;
1217 }
1218
1219 /* Map the M64 here */
1220 if (pdn->m64_per_iov == M64_PER_IOV) {
1221 pe_num = pdn->offset + j;
1222 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1223 pe_num, OPAL_M64_WINDOW_TYPE,
1224 pdn->m64_wins[i][j], 0);
1225 }
1226
1227 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001228 OPAL_M64_WINDOW_TYPE,
Wei Yang02639b02015-03-25 16:23:59 +08001229 pdn->m64_wins[i][j],
1230 start,
Wei Yang781a8682015-03-25 16:23:57 +08001231 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001232 size);
Wei Yang781a8682015-03-25 16:23:57 +08001233
Wei Yang02639b02015-03-25 16:23:59 +08001234
1235 if (rc != OPAL_SUCCESS) {
1236 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1237 win, rc);
1238 goto m64_failed;
1239 }
1240
1241 if (pdn->m64_per_iov == M64_PER_IOV)
1242 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1243 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 2);
1244 else
1245 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1246 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 1);
1247
1248 if (rc != OPAL_SUCCESS) {
1249 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1250 win, rc);
1251 goto m64_failed;
1252 }
Wei Yang781a8682015-03-25 16:23:57 +08001253 }
1254 }
1255 return 0;
1256
1257m64_failed:
1258 pnv_pci_vf_release_m64(pdev);
1259 return -EBUSY;
1260}
1261
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001262static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1263 int num);
1264static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1265
Wei Yang781a8682015-03-25 16:23:57 +08001266static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1267{
Wei Yang781a8682015-03-25 16:23:57 +08001268 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001269 int64_t rc;
1270
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001271 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001272 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001273 if (rc)
1274 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1275
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001276 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001277 if (pe->table_group.group) {
1278 iommu_group_put(pe->table_group.group);
1279 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001280 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001281 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001282 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001283}
1284
Wei Yang02639b02015-03-25 16:23:59 +08001285static void pnv_ioda_release_vf_PE(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001286{
1287 struct pci_bus *bus;
1288 struct pci_controller *hose;
1289 struct pnv_phb *phb;
1290 struct pnv_ioda_pe *pe, *pe_n;
1291 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001292 u16 vf_index;
1293 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001294
1295 bus = pdev->bus;
1296 hose = pci_bus_to_host(bus);
1297 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001298 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001299
1300 if (!pdev->is_physfn)
1301 return;
1302
Wei Yang02639b02015-03-25 16:23:59 +08001303 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1304 int vf_group;
1305 int vf_per_group;
1306 int vf_index1;
1307
1308 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1309
1310 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++)
1311 for (vf_index = vf_group * vf_per_group;
1312 vf_index < (vf_group + 1) * vf_per_group &&
1313 vf_index < num_vfs;
1314 vf_index++)
1315 for (vf_index1 = vf_group * vf_per_group;
1316 vf_index1 < (vf_group + 1) * vf_per_group &&
1317 vf_index1 < num_vfs;
1318 vf_index1++){
1319
1320 rc = opal_pci_set_peltv(phb->opal_id,
1321 pdn->offset + vf_index,
1322 pdn->offset + vf_index1,
1323 OPAL_REMOVE_PE_FROM_DOMAIN);
1324
1325 if (rc)
1326 dev_warn(&pdev->dev, "%s: Failed to unlink same group PE#%d(%lld)\n",
1327 __func__,
1328 pdn->offset + vf_index1, rc);
1329 }
1330 }
1331
Wei Yang781a8682015-03-25 16:23:57 +08001332 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1333 if (pe->parent_dev != pdev)
1334 continue;
1335
1336 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1337
1338 /* Remove from list */
1339 mutex_lock(&phb->ioda.pe_list_mutex);
1340 list_del(&pe->list);
1341 mutex_unlock(&phb->ioda.pe_list_mutex);
1342
1343 pnv_ioda_deconfigure_pe(phb, pe);
1344
1345 pnv_ioda_free_pe(phb, pe->pe_number);
1346 }
1347}
1348
1349void pnv_pci_sriov_disable(struct pci_dev *pdev)
1350{
1351 struct pci_bus *bus;
1352 struct pci_controller *hose;
1353 struct pnv_phb *phb;
1354 struct pci_dn *pdn;
1355 struct pci_sriov *iov;
1356 u16 num_vfs;
1357
1358 bus = pdev->bus;
1359 hose = pci_bus_to_host(bus);
1360 phb = hose->private_data;
1361 pdn = pci_get_pdn(pdev);
1362 iov = pdev->sriov;
1363 num_vfs = pdn->num_vfs;
1364
1365 /* Release VF PEs */
Wei Yang02639b02015-03-25 16:23:59 +08001366 pnv_ioda_release_vf_PE(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001367
1368 if (phb->type == PNV_PHB_IODA2) {
Wei Yang02639b02015-03-25 16:23:59 +08001369 if (pdn->m64_per_iov == 1)
1370 pnv_pci_vf_resource_shift(pdev, -pdn->offset);
Wei Yang781a8682015-03-25 16:23:57 +08001371
1372 /* Release M64 windows */
1373 pnv_pci_vf_release_m64(pdev);
1374
1375 /* Release PE numbers */
1376 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1377 pdn->offset = 0;
1378 }
1379}
1380
1381static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1382 struct pnv_ioda_pe *pe);
1383static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1384{
1385 struct pci_bus *bus;
1386 struct pci_controller *hose;
1387 struct pnv_phb *phb;
1388 struct pnv_ioda_pe *pe;
1389 int pe_num;
1390 u16 vf_index;
1391 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001392 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001393
1394 bus = pdev->bus;
1395 hose = pci_bus_to_host(bus);
1396 phb = hose->private_data;
1397 pdn = pci_get_pdn(pdev);
1398
1399 if (!pdev->is_physfn)
1400 return;
1401
1402 /* Reserve PE for each VF */
1403 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
1404 pe_num = pdn->offset + vf_index;
1405
1406 pe = &phb->ioda.pe_array[pe_num];
1407 pe->pe_number = pe_num;
1408 pe->phb = phb;
1409 pe->flags = PNV_IODA_PE_VF;
1410 pe->pbus = NULL;
1411 pe->parent_dev = pdev;
1412 pe->tce32_seg = -1;
1413 pe->mve_number = -1;
1414 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1415 pci_iov_virtfn_devfn(pdev, vf_index);
1416
1417 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1418 hose->global_number, pdev->bus->number,
1419 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1420 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1421
1422 if (pnv_ioda_configure_pe(phb, pe)) {
1423 /* XXX What do we do here ? */
1424 if (pe_num)
1425 pnv_ioda_free_pe(phb, pe_num);
1426 pe->pdev = NULL;
1427 continue;
1428 }
1429
Wei Yang781a8682015-03-25 16:23:57 +08001430 /* Put PE to the list */
1431 mutex_lock(&phb->ioda.pe_list_mutex);
1432 list_add_tail(&pe->list, &phb->ioda.pe_list);
1433 mutex_unlock(&phb->ioda.pe_list_mutex);
1434
1435 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1436 }
Wei Yang02639b02015-03-25 16:23:59 +08001437
1438 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1439 int vf_group;
1440 int vf_per_group;
1441 int vf_index1;
1442
1443 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1444
1445 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++) {
1446 for (vf_index = vf_group * vf_per_group;
1447 vf_index < (vf_group + 1) * vf_per_group &&
1448 vf_index < num_vfs;
1449 vf_index++) {
1450 for (vf_index1 = vf_group * vf_per_group;
1451 vf_index1 < (vf_group + 1) * vf_per_group &&
1452 vf_index1 < num_vfs;
1453 vf_index1++) {
1454
1455 rc = opal_pci_set_peltv(phb->opal_id,
1456 pdn->offset + vf_index,
1457 pdn->offset + vf_index1,
1458 OPAL_ADD_PE_TO_DOMAIN);
1459
1460 if (rc)
1461 dev_warn(&pdev->dev, "%s: Failed to link same group PE#%d(%lld)\n",
1462 __func__,
1463 pdn->offset + vf_index1, rc);
1464 }
1465 }
1466 }
1467 }
Wei Yang781a8682015-03-25 16:23:57 +08001468}
1469
1470int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1471{
1472 struct pci_bus *bus;
1473 struct pci_controller *hose;
1474 struct pnv_phb *phb;
1475 struct pci_dn *pdn;
1476 int ret;
1477
1478 bus = pdev->bus;
1479 hose = pci_bus_to_host(bus);
1480 phb = hose->private_data;
1481 pdn = pci_get_pdn(pdev);
1482
1483 if (phb->type == PNV_PHB_IODA2) {
1484 /* Calculate available PE for required VFs */
1485 mutex_lock(&phb->ioda.pe_alloc_mutex);
1486 pdn->offset = bitmap_find_next_zero_area(
1487 phb->ioda.pe_alloc, phb->ioda.total_pe,
1488 0, num_vfs, 0);
1489 if (pdn->offset >= phb->ioda.total_pe) {
1490 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1491 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1492 pdn->offset = 0;
1493 return -EBUSY;
1494 }
1495 bitmap_set(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1496 pdn->num_vfs = num_vfs;
1497 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1498
1499 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001500 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001501 if (ret) {
1502 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1503 goto m64_failed;
1504 }
1505
1506 /*
1507 * When using one M64 BAR to map one IOV BAR, we need to shift
1508 * the IOV BAR according to the PE# allocated to the VFs.
1509 * Otherwise, the PE# for the VF will conflict with others.
1510 */
Wei Yang02639b02015-03-25 16:23:59 +08001511 if (pdn->m64_per_iov == 1) {
1512 ret = pnv_pci_vf_resource_shift(pdev, pdn->offset);
1513 if (ret)
1514 goto m64_failed;
1515 }
Wei Yang781a8682015-03-25 16:23:57 +08001516 }
1517
1518 /* Setup VF PEs */
1519 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1520
1521 return 0;
1522
1523m64_failed:
1524 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1525 pdn->offset = 0;
1526
1527 return ret;
1528}
1529
Gavin Shana8b2f822015-03-25 16:23:52 +08001530int pcibios_sriov_disable(struct pci_dev *pdev)
1531{
Wei Yang781a8682015-03-25 16:23:57 +08001532 pnv_pci_sriov_disable(pdev);
1533
Gavin Shana8b2f822015-03-25 16:23:52 +08001534 /* Release PCI data */
1535 remove_dev_pci_data(pdev);
1536 return 0;
1537}
1538
1539int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1540{
1541 /* Allocate PCI data */
1542 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001543
1544 pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001545 return 0;
1546}
1547#endif /* CONFIG_PCI_IOV */
1548
Gavin Shan959c9bd2013-04-25 19:21:02 +00001549static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001550{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001551 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001552 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001553
Gavin Shan959c9bd2013-04-25 19:21:02 +00001554 /*
1555 * The function can be called while the PE#
1556 * hasn't been assigned. Do nothing for the
1557 * case.
1558 */
1559 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1560 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001561
Gavin Shan959c9bd2013-04-25 19:21:02 +00001562 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001563 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiy0e1ffef2015-08-27 16:01:16 +10001564 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001565 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001566 /*
1567 * Note: iommu_add_device() will fail here as
1568 * for physical PE: the device is already added by now;
1569 * for virtual PE: sysfs entries are not ready yet and
1570 * tce_iommu_bus_notifier will add the device to a group later.
1571 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001572}
1573
Daniel Axtens763d2d82015-04-28 15:12:07 +10001574static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001575{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001576 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1577 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001578 struct pci_dn *pdn = pci_get_pdn(pdev);
1579 struct pnv_ioda_pe *pe;
1580 uint64_t top;
1581 bool bypass = false;
1582
1583 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1584 return -ENODEV;;
1585
1586 pe = &phb->ioda.pe_array[pdn->pe_number];
1587 if (pe->tce_bypass_enabled) {
1588 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1589 bypass = (dma_mask >= top);
1590 }
1591
1592 if (bypass) {
1593 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1594 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001595 } else {
1596 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1597 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001598 }
Brian W Harta32305b2014-07-31 14:24:37 -05001599 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001600 return 0;
1601}
1602
Andrew Donnellan535229822015-08-07 13:45:54 +10001603static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001604{
Andrew Donnellan535229822015-08-07 13:45:54 +10001605 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1606 struct pnv_phb *phb = hose->private_data;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001607 struct pci_dn *pdn = pci_get_pdn(pdev);
1608 struct pnv_ioda_pe *pe;
1609 u64 end, mask;
1610
1611 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1612 return 0;
1613
1614 pe = &phb->ioda.pe_array[pdn->pe_number];
1615 if (!pe->tce_bypass_enabled)
1616 return __dma_get_required_mask(&pdev->dev);
1617
1618
1619 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1620 mask = 1ULL << (fls64(end) - 1);
1621 mask += mask - 1;
1622
1623 return mask;
1624}
1625
Gavin Shandff4a392014-07-15 17:00:55 +10001626static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001627 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001628{
1629 struct pci_dev *dev;
1630
1631 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001632 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001633 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001634 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001635
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001636 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001637 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001638 }
1639}
1640
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001641static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
1642 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001643{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001644 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1645 &tbl->it_group_list, struct iommu_table_group_link,
1646 next);
1647 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001648 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001649 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001650 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1651 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001652 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001653 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001654
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001655 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1656 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1657 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001658
1659 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1660 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001661 start <<= shift;
1662 end <<= shift;
1663 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001664 start |= tbl->it_busno;
1665 end |= tbl->it_busno;
1666 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1667 /* p7ioc-style invalidation, 2 TCEs per write */
1668 start |= (1ull << 63);
1669 end |= (1ull << 63);
1670 inc = 16;
1671 } else {
1672 /* Default (older HW) */
1673 inc = 128;
1674 }
1675
1676 end |= inc - 1; /* round up end to be different than start */
1677
1678 mb(); /* Ensure above stores are visible */
1679 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001680 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001681 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001682 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001683 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001684 start += inc;
1685 }
1686
1687 /*
1688 * The iommu layer will do another mb() for us on build()
1689 * and we don't care on free()
1690 */
1691}
1692
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001693static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1694 long npages, unsigned long uaddr,
1695 enum dma_data_direction direction,
1696 struct dma_attrs *attrs)
1697{
1698 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1699 attrs);
1700
1701 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1702 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1703
1704 return ret;
1705}
1706
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001707#ifdef CONFIG_IOMMU_API
1708static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1709 unsigned long *hpa, enum dma_data_direction *direction)
1710{
1711 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1712
1713 if (!ret && (tbl->it_type &
1714 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1715 pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);
1716
1717 return ret;
1718}
1719#endif
1720
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001721static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1722 long npages)
1723{
1724 pnv_tce_free(tbl, index, npages);
1725
1726 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1727 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1728}
1729
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001730static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001731 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001732#ifdef CONFIG_IOMMU_API
1733 .exchange = pnv_ioda1_tce_xchg,
1734#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001735 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001736 .get = pnv_tce_get,
1737};
1738
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001739static inline void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_ioda_pe *pe)
1740{
1741 /* 01xb - invalidate TCEs that match the specified PE# */
1742 unsigned long val = (0x4ull << 60) | (pe->pe_number & 0xFF);
1743 struct pnv_phb *phb = pe->phb;
1744
1745 if (!phb->ioda.tce_inval_reg)
1746 return;
1747
1748 mb(); /* Ensure above stores are visible */
1749 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1750}
1751
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001752static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
1753 __be64 __iomem *invalidate, unsigned shift,
1754 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001755{
1756 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001757
1758 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001759 start = 0x2ull << 60;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001760 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001761 end = start;
1762
1763 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001764 start |= (index << shift);
1765 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001766 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001767 mb();
1768
1769 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001770 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001771 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001772 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001773 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001774 start += inc;
1775 }
1776}
1777
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001778static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1779 unsigned long index, unsigned long npages, bool rm)
1780{
1781 struct iommu_table_group_link *tgl;
1782
1783 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
1784 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1785 struct pnv_ioda_pe, table_group);
1786 __be64 __iomem *invalidate = rm ?
1787 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1788 pe->phb->ioda.tce_inval_reg;
1789
1790 pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
1791 invalidate, tbl->it_page_shift,
1792 index, npages);
1793 }
1794}
1795
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001796static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1797 long npages, unsigned long uaddr,
1798 enum dma_data_direction direction,
1799 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001800{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001801 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1802 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001803
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001804 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1805 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1806
1807 return ret;
1808}
1809
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001810#ifdef CONFIG_IOMMU_API
1811static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1812 unsigned long *hpa, enum dma_data_direction *direction)
1813{
1814 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1815
1816 if (!ret && (tbl->it_type &
1817 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1818 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1819
1820 return ret;
1821}
1822#endif
1823
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001824static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1825 long npages)
1826{
1827 pnv_tce_free(tbl, index, npages);
1828
1829 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1830 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001831}
1832
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001833static void pnv_ioda2_table_free(struct iommu_table *tbl)
1834{
1835 pnv_pci_ioda2_table_free_pages(tbl);
1836 iommu_free_table(tbl, "pnv");
1837}
1838
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001839static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001840 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001841#ifdef CONFIG_IOMMU_API
1842 .exchange = pnv_ioda2_tce_xchg,
1843#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001844 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001845 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001846 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001847};
1848
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001849static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1850 struct pnv_ioda_pe *pe, unsigned int base,
1851 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001852{
1853
1854 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001855 struct iommu_table *tbl;
1856 unsigned int i;
1857 int64_t rc;
1858 void *addr;
1859
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001860 /* XXX FIXME: Handle 64-bit only DMA devices */
1861 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1862 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1863
1864 /* We shouldn't already have a 32-bit DMA associated */
1865 if (WARN_ON(pe->tce32_seg >= 0))
1866 return;
1867
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001868 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001869 iommu_register_group(&pe->table_group, phb->hose->global_number,
1870 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001871 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001872
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001873 /* Grab a 32-bit TCE table */
1874 pe->tce32_seg = base;
1875 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1876 (base << 28), ((base + segs) << 28) - 1);
1877
1878 /* XXX Currently, we allocate one big contiguous table for the
1879 * TCEs. We only really need one chunk per 256M of TCE space
1880 * (ie per segment) but that's an optimization for later, it
1881 * requires some added smarts with our get/put_tce implementation
1882 */
1883 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1884 get_order(TCE32_TABLE_SIZE * segs));
1885 if (!tce_mem) {
1886 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1887 goto fail;
1888 }
1889 addr = page_address(tce_mem);
1890 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1891
1892 /* Configure HW */
1893 for (i = 0; i < segs; i++) {
1894 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1895 pe->pe_number,
1896 base + i, 1,
1897 __pa(addr) + TCE32_TABLE_SIZE * i,
1898 TCE32_TABLE_SIZE, 0x1000);
1899 if (rc) {
1900 pe_err(pe, " Failed to configure 32-bit TCE table,"
1901 " err %ld\n", rc);
1902 goto fail;
1903 }
1904 }
1905
1906 /* Setup linux iommu table */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001907 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001908 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001909
1910 /* OPAL variant of P7IOC SW invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001911 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10001912 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1913 TCE_PCI_SWINV_FREE |
1914 TCE_PCI_SWINV_PAIR);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001915
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001916 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001917 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
1918 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001919 iommu_init_table(tbl, phb->hose->node);
1920
Wei Yang781a8682015-03-25 16:23:57 +08001921 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001922 /*
1923 * Setting table base here only for carrying iommu_group
1924 * further down to let iommu_add_device() do the job.
1925 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
1926 */
1927 set_iommu_table_base(&pe->pdev->dev, tbl);
1928 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001929 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001930 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001931
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001932 return;
1933 fail:
1934 /* XXX Failure: Try to fallback to 64-bit only ? */
1935 if (pe->tce32_seg >= 0)
1936 pe->tce32_seg = -1;
1937 if (tce_mem)
1938 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001939 if (tbl) {
1940 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
1941 iommu_free_table(tbl, "pnv");
1942 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001943}
1944
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001945static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
1946 int num, struct iommu_table *tbl)
1947{
1948 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1949 table_group);
1950 struct pnv_phb *phb = pe->phb;
1951 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001952 const unsigned long size = tbl->it_indirect_levels ?
1953 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001954 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
1955 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
1956
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001957 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001958 start_addr, start_addr + win_size - 1,
1959 IOMMU_PAGE_SIZE(tbl));
1960
1961 /*
1962 * Map TCE table through TVT. The TVE index is the PE number
1963 * shifted by 1 bit for 32-bits DMA space.
1964 */
1965 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1966 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001967 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001968 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001969 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001970 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001971 IOMMU_PAGE_SIZE(tbl));
1972 if (rc) {
1973 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
1974 return rc;
1975 }
1976
1977 pnv_pci_link_table_and_group(phb->hose->node, num,
1978 tbl, &pe->table_group);
1979 pnv_pci_ioda2_tce_invalidate_entire(pe);
1980
1981 return 0;
1982}
1983
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10001984static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001985{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001986 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1987 int64_t rc;
1988
1989 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1990 if (enable) {
1991 phys_addr_t top = memblock_end_of_DRAM();
1992
1993 top = roundup_pow_of_two(top);
1994 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1995 pe->pe_number,
1996 window_id,
1997 pe->tce_bypass_base,
1998 top);
1999 } else {
2000 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2001 pe->pe_number,
2002 window_id,
2003 pe->tce_bypass_base,
2004 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002005 }
2006 if (rc)
2007 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2008 else
2009 pe->tce_bypass_enabled = enable;
2010}
2011
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002012static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2013 __u32 page_shift, __u64 window_size, __u32 levels,
2014 struct iommu_table *tbl);
2015
2016static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2017 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2018 struct iommu_table **ptbl)
2019{
2020 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2021 table_group);
2022 int nid = pe->phb->hose->node;
2023 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2024 long ret;
2025 struct iommu_table *tbl;
2026
2027 tbl = pnv_pci_table_alloc(nid);
2028 if (!tbl)
2029 return -ENOMEM;
2030
2031 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2032 bus_offset, page_shift, window_size,
2033 levels, tbl);
2034 if (ret) {
2035 iommu_free_table(tbl, "pnv");
2036 return ret;
2037 }
2038
2039 tbl->it_ops = &pnv_ioda2_iommu_ops;
2040 if (pe->phb->ioda.tce_inval_reg)
2041 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2042
2043 *ptbl = tbl;
2044
2045 return 0;
2046}
2047
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002048static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2049{
2050 struct iommu_table *tbl = NULL;
2051 long rc;
2052
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002053 /*
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002054 * crashkernel= specifies the kdump kernel's maximum memory at
2055 * some offset and there is no guaranteed the result is a power
2056 * of 2, which will cause errors later.
2057 */
2058 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
2059
2060 /*
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002061 * In memory constrained environments, e.g. kdump kernel, the
2062 * DMA window can be larger than available memory, which will
2063 * cause errors later.
2064 */
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002065 const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002066
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002067 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2068 IOMMU_PAGE_SHIFT_4K,
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002069 window_size,
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002070 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2071 if (rc) {
2072 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2073 rc);
2074 return rc;
2075 }
2076
2077 iommu_init_table(tbl, pe->phb->hose->node);
2078
2079 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2080 if (rc) {
2081 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2082 rc);
2083 pnv_ioda2_table_free(tbl);
2084 return rc;
2085 }
2086
2087 if (!pnv_iommu_bypass_disabled)
2088 pnv_pci_ioda2_set_bypass(pe, true);
2089
2090 /* OPAL variant of PHB3 invalidated TCEs */
2091 if (pe->phb->ioda.tce_inval_reg)
2092 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2093
2094 /*
2095 * Setting table base here only for carrying iommu_group
2096 * further down to let iommu_add_device() do the job.
2097 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2098 */
2099 if (pe->flags & PNV_IODA_PE_DEV)
2100 set_iommu_table_base(&pe->pdev->dev, tbl);
2101
2102 return 0;
2103}
2104
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002105#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2106static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2107 int num)
2108{
2109 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2110 table_group);
2111 struct pnv_phb *phb = pe->phb;
2112 long ret;
2113
2114 pe_info(pe, "Removing DMA window #%d\n", num);
2115
2116 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2117 (pe->pe_number << 1) + num,
2118 0/* levels */, 0/* table address */,
2119 0/* table size */, 0/* page size */);
2120 if (ret)
2121 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2122 else
2123 pnv_pci_ioda2_tce_invalidate_entire(pe);
2124
2125 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2126
2127 return ret;
2128}
2129#endif
2130
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002131#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002132static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2133 __u64 window_size, __u32 levels)
2134{
2135 unsigned long bytes = 0;
2136 const unsigned window_shift = ilog2(window_size);
2137 unsigned entries_shift = window_shift - page_shift;
2138 unsigned table_shift = entries_shift + 3;
2139 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2140 unsigned long direct_table_size;
2141
2142 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2143 (window_size > memory_hotplug_max()) ||
2144 !is_power_of_2(window_size))
2145 return 0;
2146
2147 /* Calculate a direct table size from window_size and levels */
2148 entries_shift = (entries_shift + levels - 1) / levels;
2149 table_shift = entries_shift + 3;
2150 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2151 direct_table_size = 1UL << table_shift;
2152
2153 for ( ; levels; --levels) {
2154 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2155
2156 tce_table_size /= direct_table_size;
2157 tce_table_size <<= 3;
2158 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2159 }
2160
2161 return bytes;
2162}
2163
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002164static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002165{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002166 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2167 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002168 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2169 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002170
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002171 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002172 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2173 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002174}
2175
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002176static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2177{
2178 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2179 table_group);
2180
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002181 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002182}
2183
2184static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002185 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002186 .create_table = pnv_pci_ioda2_create_table,
2187 .set_window = pnv_pci_ioda2_set_window,
2188 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002189 .take_ownership = pnv_ioda2_take_ownership,
2190 .release_ownership = pnv_ioda2_release_ownership,
2191};
2192#endif
2193
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002194static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2195{
2196 const __be64 *swinvp;
2197
2198 /* OPAL variant of PHB3 invalidated TCEs */
2199 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2200 if (!swinvp)
2201 return;
2202
2203 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2204 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2205}
2206
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002207static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2208 unsigned levels, unsigned long limit,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002209 unsigned long *current_offset, unsigned long *total_allocated)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002210{
2211 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002212 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002213 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002214 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2215 unsigned entries = 1UL << (shift - 3);
2216 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002217
2218 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2219 if (!tce_mem) {
2220 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2221 return NULL;
2222 }
2223 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002224 memset(addr, 0, allocated);
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002225 *total_allocated += allocated;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002226
2227 --levels;
2228 if (!levels) {
2229 *current_offset += allocated;
2230 return addr;
2231 }
2232
2233 for (i = 0; i < entries; ++i) {
2234 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002235 levels, limit, current_offset, total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002236 if (!tmp)
2237 break;
2238
2239 addr[i] = cpu_to_be64(__pa(tmp) |
2240 TCE_PCI_READ | TCE_PCI_WRITE);
2241
2242 if (*current_offset >= limit)
2243 break;
2244 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002245
2246 return addr;
2247}
2248
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002249static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2250 unsigned long size, unsigned level);
2251
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002252static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002253 __u32 page_shift, __u64 window_size, __u32 levels,
2254 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002255{
2256 void *addr;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002257 unsigned long offset = 0, level_shift, total_allocated = 0;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002258 const unsigned window_shift = ilog2(window_size);
2259 unsigned entries_shift = window_shift - page_shift;
2260 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2261 const unsigned long tce_table_size = 1UL << table_shift;
2262
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002263 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2264 return -EINVAL;
2265
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002266 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2267 return -EINVAL;
2268
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002269 /* Adjust direct table size from window_size and levels */
2270 entries_shift = (entries_shift + levels - 1) / levels;
2271 level_shift = entries_shift + 3;
2272 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2273
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002274 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002275 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002276 levels, tce_table_size, &offset, &total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002277
2278 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002279 if (!addr)
2280 return -ENOMEM;
2281
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002282 /*
2283 * First level was allocated but some lower level failed as
2284 * we did not allocate as much as we wanted,
2285 * release partially allocated table.
2286 */
2287 if (offset < tce_table_size) {
2288 pnv_pci_ioda2_table_do_free_pages(addr,
2289 1ULL << (level_shift - 3), levels - 1);
2290 return -ENOMEM;
2291 }
2292
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002293 /* Setup linux iommu table */
2294 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2295 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002296 tbl->it_level_size = 1ULL << (level_shift - 3);
2297 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002298 tbl->it_allocated_size = total_allocated;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002299
2300 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2301 window_size, tce_table_size, bus_offset);
2302
2303 return 0;
2304}
2305
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002306static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2307 unsigned long size, unsigned level)
2308{
2309 const unsigned long addr_ul = (unsigned long) addr &
2310 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2311
2312 if (level) {
2313 long i;
2314 u64 *tmp = (u64 *) addr_ul;
2315
2316 for (i = 0; i < size; ++i) {
2317 unsigned long hpa = be64_to_cpu(tmp[i]);
2318
2319 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2320 continue;
2321
2322 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2323 level - 1);
2324 }
2325 }
2326
2327 free_pages(addr_ul, get_order(size << 3));
2328}
2329
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002330static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2331{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002332 const unsigned long size = tbl->it_indirect_levels ?
2333 tbl->it_level_size : tbl->it_size;
2334
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002335 if (!tbl->it_size)
2336 return;
2337
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002338 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2339 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002340}
2341
Gavin Shan373f5652013-04-25 19:21:01 +00002342static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2343 struct pnv_ioda_pe *pe)
2344{
Gavin Shan373f5652013-04-25 19:21:01 +00002345 int64_t rc;
2346
2347 /* We shouldn't already have a 32-bit DMA associated */
2348 if (WARN_ON(pe->tce32_seg >= 0))
2349 return;
2350
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002351 /* TVE #1 is selected by PCI address bit 59 */
2352 pe->tce_bypass_base = 1ull << 59;
2353
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002354 iommu_register_group(&pe->table_group, phb->hose->global_number,
2355 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002356
Gavin Shan373f5652013-04-25 19:21:01 +00002357 /* The PE will reserve all possible 32-bits space */
2358 pe->tce32_seg = 0;
Gavin Shan373f5652013-04-25 19:21:01 +00002359 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002360 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002361
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002362 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002363 pe->table_group.tce32_start = 0;
2364 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2365 pe->table_group.max_dynamic_windows_supported =
2366 IOMMU_TABLE_GROUP_MAX_TABLES;
2367 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2368 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002369#ifdef CONFIG_IOMMU_API
2370 pe->table_group.ops = &pnv_pci_ioda2_ops;
2371#endif
2372
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002373 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan373f5652013-04-25 19:21:01 +00002374 if (rc) {
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002375 if (pe->tce32_seg >= 0)
2376 pe->tce32_seg = -1;
2377 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002378 }
2379
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002380 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002381 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002382 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002383 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002384}
2385
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002386static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002387{
2388 struct pci_controller *hose = phb->hose;
2389 unsigned int residual, remaining, segs, tw, base;
2390 struct pnv_ioda_pe *pe;
2391
2392 /* If we have more PE# than segments available, hand out one
2393 * per PE until we run out and let the rest fail. If not,
2394 * then we assign at least one segment per PE, plus more based
2395 * on the amount of devices under that PE
2396 */
2397 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
2398 residual = 0;
2399 else
2400 residual = phb->ioda.tce32_count -
2401 phb->ioda.dma_pe_count;
2402
2403 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
2404 hose->global_number, phb->ioda.tce32_count);
2405 pr_info("PCI: %d PE# for a total weight of %d\n",
2406 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
2407
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002408 pnv_pci_ioda_setup_opal_tce_kill(phb);
2409
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002410 /* Walk our PE list and configure their DMA segments, hand them
2411 * out one base segment plus any residual segments based on
2412 * weight
2413 */
2414 remaining = phb->ioda.tce32_count;
2415 tw = phb->ioda.dma_weight;
2416 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00002417 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002418 if (!pe->dma_weight)
2419 continue;
2420 if (!remaining) {
2421 pe_warn(pe, "No DMA32 resources available\n");
2422 continue;
2423 }
2424 segs = 1;
2425 if (residual) {
2426 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
2427 if (segs > remaining)
2428 segs = remaining;
2429 }
Gavin Shan373f5652013-04-25 19:21:01 +00002430
2431 /*
2432 * For IODA2 compliant PHB3, we needn't care about the weight.
2433 * The all available 32-bits DMA space will be assigned to
2434 * the specific PE.
2435 */
2436 if (phb->type == PNV_PHB_IODA1) {
2437 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
2438 pe->dma_weight, segs);
2439 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
2440 } else {
2441 pe_info(pe, "Assign DMA32 space\n");
2442 segs = 0;
2443 pnv_pci_ioda2_setup_dma_pe(phb, pe);
2444 }
2445
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002446 remaining -= segs;
2447 base += segs;
2448 }
2449}
2450
2451#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002452static void pnv_ioda2_msi_eoi(struct irq_data *d)
2453{
2454 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2455 struct irq_chip *chip = irq_data_get_irq_chip(d);
2456 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2457 ioda.irq_chip);
2458 int64_t rc;
2459
2460 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2461 WARN_ON_ONCE(rc);
2462
2463 icp_native_eoi(d);
2464}
2465
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002466
2467static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2468{
2469 struct irq_data *idata;
2470 struct irq_chip *ichip;
2471
2472 if (phb->type != PNV_PHB_IODA2)
2473 return;
2474
2475 if (!phb->ioda.irq_chip_init) {
2476 /*
2477 * First time we setup an MSI IRQ, we need to setup the
2478 * corresponding IRQ chip to route correctly.
2479 */
2480 idata = irq_get_irq_data(virq);
2481 ichip = irq_data_get_irq_chip(idata);
2482 phb->ioda.irq_chip_init = 1;
2483 phb->ioda.irq_chip = *ichip;
2484 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2485 }
2486 irq_set_chip(virq, &phb->ioda.irq_chip);
2487}
2488
Ian Munsie80c49c72014-10-08 19:54:57 +11002489#ifdef CONFIG_CXL_BASE
2490
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002491struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002492{
2493 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2494
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002495 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002496}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002497EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002498
Ryan Grimm1212aa12015-01-19 11:52:50 -06002499int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002500{
2501 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2502 struct pnv_phb *phb = hose->private_data;
2503 struct pnv_ioda_pe *pe;
2504 int rc;
2505
2506 pe = pnv_ioda_get_pe(dev);
2507 if (!pe)
2508 return -ENODEV;
2509
2510 pe_info(pe, "Switching PHB to CXL\n");
2511
Ryan Grimm1212aa12015-01-19 11:52:50 -06002512 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11002513 if (rc)
2514 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2515
2516 return rc;
2517}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002518EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002519
2520/* Find PHB for cxl dev and allocate MSI hwirqs?
2521 * Returns the absolute hardware IRQ number
2522 */
2523int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2524{
2525 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2526 struct pnv_phb *phb = hose->private_data;
2527 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2528
2529 if (hwirq < 0) {
2530 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2531 return -ENOSPC;
2532 }
2533
2534 return phb->msi_base + hwirq;
2535}
2536EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2537
2538void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2539{
2540 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2541 struct pnv_phb *phb = hose->private_data;
2542
2543 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2544}
2545EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2546
2547void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2548 struct pci_dev *dev)
2549{
2550 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2551 struct pnv_phb *phb = hose->private_data;
2552 int i, hwirq;
2553
2554 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2555 if (!irqs->range[i])
2556 continue;
2557 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2558 i, irqs->offset[i],
2559 irqs->range[i]);
2560 hwirq = irqs->offset[i] - phb->msi_base;
2561 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2562 irqs->range[i]);
2563 }
2564}
2565EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2566
2567int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2568 struct pci_dev *dev, int num)
2569{
2570 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2571 struct pnv_phb *phb = hose->private_data;
2572 int i, hwirq, try;
2573
2574 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2575
2576 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2577 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2578 try = num;
2579 while (try) {
2580 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2581 if (hwirq >= 0)
2582 break;
2583 try /= 2;
2584 }
2585 if (!try)
2586 goto fail;
2587
2588 irqs->offset[i] = phb->msi_base + hwirq;
2589 irqs->range[i] = try;
2590 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2591 i, irqs->offset[i], irqs->range[i]);
2592 num -= try;
2593 }
2594 if (num)
2595 goto fail;
2596
2597 return 0;
2598fail:
2599 pnv_cxl_release_hwirq_ranges(irqs, dev);
2600 return -ENOSPC;
2601}
2602EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2603
2604int pnv_cxl_get_irq_count(struct pci_dev *dev)
2605{
2606 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2607 struct pnv_phb *phb = hose->private_data;
2608
2609 return phb->msi_bmp.irq_count;
2610}
2611EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2612
2613int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2614 unsigned int virq)
2615{
2616 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2617 struct pnv_phb *phb = hose->private_data;
2618 unsigned int xive_num = hwirq - phb->msi_base;
2619 struct pnv_ioda_pe *pe;
2620 int rc;
2621
2622 if (!(pe = pnv_ioda_get_pe(dev)))
2623 return -ENODEV;
2624
2625 /* Assign XIVE to PE */
2626 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2627 if (rc) {
2628 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2629 "hwirq 0x%x XIVE 0x%x PE\n",
2630 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2631 return -EIO;
2632 }
2633 set_msi_irq_chip(phb, virq);
2634
2635 return 0;
2636}
2637EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2638#endif
2639
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002640static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002641 unsigned int hwirq, unsigned int virq,
2642 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002643{
2644 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2645 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002646 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002647 int rc;
2648
2649 /* No PE assigned ? bail out ... no MSI for you ! */
2650 if (pe == NULL)
2651 return -ENXIO;
2652
2653 /* Check if we have an MVE */
2654 if (pe->mve_number < 0)
2655 return -ENXIO;
2656
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002657 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002658 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002659 is_64 = 0;
2660
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002661 /* Assign XIVE to PE */
2662 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2663 if (rc) {
2664 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2665 pci_name(dev), rc, xive_num);
2666 return -EIO;
2667 }
2668
2669 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002670 __be64 addr64;
2671
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002672 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2673 &addr64, &data);
2674 if (rc) {
2675 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2676 pci_name(dev), rc);
2677 return -EIO;
2678 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002679 msg->address_hi = be64_to_cpu(addr64) >> 32;
2680 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002681 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002682 __be32 addr32;
2683
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002684 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2685 &addr32, &data);
2686 if (rc) {
2687 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2688 pci_name(dev), rc);
2689 return -EIO;
2690 }
2691 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002692 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002693 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002694 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002695
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002696 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002697
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002698 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2699 " address=%x_%08x data=%x PE# %d\n",
2700 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2701 msg->address_hi, msg->address_lo, data, pe->pe_number);
2702
2703 return 0;
2704}
2705
2706static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2707{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002708 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002709 const __be32 *prop = of_get_property(phb->hose->dn,
2710 "ibm,opal-msi-ranges", NULL);
2711 if (!prop) {
2712 /* BML Fallback */
2713 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2714 }
2715 if (!prop)
2716 return;
2717
2718 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002719 count = be32_to_cpup(prop + 1);
2720 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002721 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2722 phb->hose->global_number);
2723 return;
2724 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002725
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002726 phb->msi_setup = pnv_pci_ioda_msi_setup;
2727 phb->msi32_support = 1;
2728 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002729 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002730}
2731#else
2732static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2733#endif /* CONFIG_PCI_MSI */
2734
Wei Yang6e628c72015-03-25 16:23:55 +08002735#ifdef CONFIG_PCI_IOV
2736static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2737{
2738 struct pci_controller *hose;
2739 struct pnv_phb *phb;
2740 struct resource *res;
2741 int i;
2742 resource_size_t size;
2743 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002744 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002745
2746 if (!pdev->is_physfn || pdev->is_added)
2747 return;
2748
2749 hose = pci_bus_to_host(pdev->bus);
2750 phb = hose->private_data;
2751
2752 pdn = pci_get_pdn(pdev);
2753 pdn->vfs_expanded = 0;
2754
Wei Yang5b88ec22015-03-25 16:23:58 +08002755 total_vfs = pci_sriov_get_totalvfs(pdev);
2756 pdn->m64_per_iov = 1;
2757 mul = phb->ioda.total_pe;
2758
2759 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2760 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2761 if (!res->flags || res->parent)
2762 continue;
2763 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2764 dev_warn(&pdev->dev, " non M64 VF BAR%d: %pR\n",
2765 i, res);
2766 continue;
2767 }
2768
2769 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
2770
2771 /* bigger than 64M */
2772 if (size > (1 << 26)) {
2773 dev_info(&pdev->dev, "PowerNV: VF BAR%d: %pR IOV size is bigger than 64M, roundup power2\n",
2774 i, res);
2775 pdn->m64_per_iov = M64_PER_IOV;
2776 mul = roundup_pow_of_two(total_vfs);
2777 break;
2778 }
2779 }
2780
Wei Yang6e628c72015-03-25 16:23:55 +08002781 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2782 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2783 if (!res->flags || res->parent)
2784 continue;
2785 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2786 dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
2787 i, res);
2788 continue;
2789 }
2790
2791 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
2792 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002793 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08002794 dev_dbg(&pdev->dev, " %pR\n", res);
2795 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08002796 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08002797 }
Wei Yang5b88ec22015-03-25 16:23:58 +08002798 pdn->vfs_expanded = mul;
Wei Yang6e628c72015-03-25 16:23:55 +08002799}
2800#endif /* CONFIG_PCI_IOV */
2801
Gavin Shan11685be2012-08-20 03:49:16 +00002802/*
2803 * This function is supposed to be called on basis of PE from top
2804 * to bottom style. So the the I/O or MMIO segment assigned to
2805 * parent PE could be overrided by its child PEs if necessary.
2806 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002807static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
2808 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00002809{
2810 struct pnv_phb *phb = hose->private_data;
2811 struct pci_bus_region region;
2812 struct resource *res;
2813 int i, index;
2814 int rc;
2815
2816 /*
2817 * NOTE: We only care PCI bus based PE for now. For PCI
2818 * device based PE, for example SRIOV sensitive VF should
2819 * be figured out later.
2820 */
2821 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2822
2823 pci_bus_for_each_resource(pe->pbus, res, i) {
2824 if (!res || !res->flags ||
2825 res->start > res->end)
2826 continue;
2827
2828 if (res->flags & IORESOURCE_IO) {
2829 region.start = res->start - phb->ioda.io_pci_base;
2830 region.end = res->end - phb->ioda.io_pci_base;
2831 index = region.start / phb->ioda.io_segsize;
2832
2833 while (index < phb->ioda.total_pe &&
2834 region.start <= region.end) {
2835 phb->ioda.io_segmap[index] = pe->pe_number;
2836 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2837 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2838 if (rc != OPAL_SUCCESS) {
2839 pr_err("%s: OPAL error %d when mapping IO "
2840 "segment #%d to PE#%d\n",
2841 __func__, rc, index, pe->pe_number);
2842 break;
2843 }
2844
2845 region.start += phb->ioda.io_segsize;
2846 index++;
2847 }
Gavin Shan027fa022015-03-27 11:29:00 +11002848 } else if ((res->flags & IORESOURCE_MEM) &&
2849 !pnv_pci_is_mem_pref_64(res->flags)) {
Gavin Shan11685be2012-08-20 03:49:16 +00002850 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002851 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002852 phb->ioda.m32_pci_base;
2853 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002854 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002855 phb->ioda.m32_pci_base;
2856 index = region.start / phb->ioda.m32_segsize;
2857
2858 while (index < phb->ioda.total_pe &&
2859 region.start <= region.end) {
2860 phb->ioda.m32_segmap[index] = pe->pe_number;
2861 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2862 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2863 if (rc != OPAL_SUCCESS) {
2864 pr_err("%s: OPAL error %d when mapping M32 "
2865 "segment#%d to PE#%d",
2866 __func__, rc, index, pe->pe_number);
2867 break;
2868 }
2869
2870 region.start += phb->ioda.m32_segsize;
2871 index++;
2872 }
2873 }
2874 }
2875}
2876
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002877static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00002878{
2879 struct pci_controller *tmp, *hose;
2880 struct pnv_phb *phb;
2881 struct pnv_ioda_pe *pe;
2882
2883 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2884 phb = hose->private_data;
2885 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2886 pnv_ioda_setup_pe_seg(hose, pe);
2887 }
2888 }
2889}
2890
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002891static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00002892{
2893 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00002894 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00002895
2896 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2897 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00002898
2899 /* Mark the PHB initialization done */
2900 phb = hose->private_data;
2901 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00002902 }
2903}
2904
Gavin Shan37c367f2013-06-20 18:13:25 +08002905static void pnv_pci_ioda_create_dbgfs(void)
2906{
2907#ifdef CONFIG_DEBUG_FS
2908 struct pci_controller *hose, *tmp;
2909 struct pnv_phb *phb;
2910 char name[16];
2911
2912 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2913 phb = hose->private_data;
2914
2915 sprintf(name, "PCI%04x", hose->global_number);
2916 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
2917 if (!phb->dbgfs)
2918 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
2919 __func__, hose->global_number);
2920 }
2921#endif /* CONFIG_DEBUG_FS */
2922}
2923
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002924static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00002925{
2926 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00002927 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00002928 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002929
Gavin Shan37c367f2013-06-20 18:13:25 +08002930 pnv_pci_ioda_create_dbgfs();
2931
Gavin Shane9cc17d2013-06-20 13:21:14 +08002932#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08002933 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04002934 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002935#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00002936}
2937
Gavin Shan271fd032012-09-11 16:59:47 -06002938/*
2939 * Returns the alignment for I/O or memory windows for P2P
2940 * bridges. That actually depends on how PEs are segmented.
2941 * For now, we return I/O or M32 segment size for PE sensitive
2942 * P2P bridges. Otherwise, the default values (4KiB for I/O,
2943 * 1MiB for memory) will be returned.
2944 *
2945 * The current PCI bus might be put into one PE, which was
2946 * create against the parent PCI bridge. For that case, we
2947 * needn't enlarge the alignment so that we can save some
2948 * resources.
2949 */
2950static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2951 unsigned long type)
2952{
2953 struct pci_dev *bridge;
2954 struct pci_controller *hose = pci_bus_to_host(bus);
2955 struct pnv_phb *phb = hose->private_data;
2956 int num_pci_bridges = 0;
2957
2958 bridge = bus->self;
2959 while (bridge) {
2960 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2961 num_pci_bridges++;
2962 if (num_pci_bridges >= 2)
2963 return 1;
2964 }
2965
2966 bridge = bridge->bus->self;
2967 }
2968
Guo Chao262af552014-07-21 14:42:30 +10002969 /* We fail back to M32 if M64 isn't supported */
2970 if (phb->ioda.m64_segsize &&
2971 pnv_pci_is_mem_pref_64(type))
2972 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06002973 if (type & IORESOURCE_MEM)
2974 return phb->ioda.m32_segsize;
2975
2976 return phb->ioda.io_segsize;
2977}
2978
Wei Yang5350ab32015-03-25 16:23:56 +08002979#ifdef CONFIG_PCI_IOV
2980static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
2981 int resno)
2982{
2983 struct pci_dn *pdn = pci_get_pdn(pdev);
2984 resource_size_t align, iov_align;
2985
2986 iov_align = resource_size(&pdev->resource[resno]);
2987 if (iov_align)
2988 return iov_align;
2989
2990 align = pci_iov_resource_size(pdev, resno);
2991 if (pdn->vfs_expanded)
2992 return pdn->vfs_expanded * align;
2993
2994 return align;
2995}
2996#endif /* CONFIG_PCI_IOV */
2997
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002998/* Prevent enabling devices for which we couldn't properly
2999 * assign a PE
3000 */
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003001static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003002{
Gavin Shandb1266c2012-08-20 03:49:18 +00003003 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3004 struct pnv_phb *phb = hose->private_data;
3005 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003006
Gavin Shandb1266c2012-08-20 03:49:18 +00003007 /* The function is probably called while the PEs have
3008 * not be created yet. For example, resource reassignment
3009 * during PCI probe period. We just skip the check if
3010 * PEs isn't ready.
3011 */
3012 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003013 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003014
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003015 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003016 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003017 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003018
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003019 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003020}
3021
3022static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
3023 u32 devfn)
3024{
3025 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
3026}
3027
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003028static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003029{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003030 struct pnv_phb *phb = hose->private_data;
3031
Gavin Shand1a85ee2014-09-30 12:39:05 +10003032 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003033 OPAL_ASSERT_RESET);
3034}
3035
Daniel Axtens92ae0352015-04-28 15:12:05 +10003036static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
3037 .dma_dev_setup = pnv_pci_dma_dev_setup,
3038#ifdef CONFIG_PCI_MSI
3039 .setup_msi_irqs = pnv_setup_msi_irqs,
3040 .teardown_msi_irqs = pnv_teardown_msi_irqs,
3041#endif
3042 .enable_device_hook = pnv_pci_enable_device_hook,
3043 .window_alignment = pnv_pci_window_alignment,
3044 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
Daniel Axtens763d2d82015-04-28 15:12:07 +10003045 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
Andrew Donnellan535229822015-08-07 13:45:54 +10003046 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003047 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003048};
3049
Anton Blancharde51df2c2014-08-20 08:55:18 +10003050static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3051 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003052{
3053 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003054 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08003055 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10003056 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003057 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003058 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003059 u64 phb_id;
3060 void *aux;
3061 long rc;
3062
Gavin Shan58d714e2013-07-31 16:47:00 +08003063 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003064
3065 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3066 if (!prop64) {
3067 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3068 return;
3069 }
3070 phb_id = be64_to_cpup(prop64);
3071 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3072
Michael Ellermane39f223f2014-11-18 16:47:35 +11003073 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003074
3075 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003076 phb->hose = hose = pcibios_alloc_controller(np);
3077 if (!phb->hose) {
3078 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003079 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003080 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003081 return;
3082 }
3083
3084 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003085 prop32 = of_get_property(np, "bus-range", &len);
3086 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003087 hose->first_busno = be32_to_cpu(prop32[0]);
3088 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003089 } else {
3090 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3091 hose->first_busno = 0;
3092 hose->last_busno = 0xff;
3093 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003094 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003095 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003096 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003097 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003098 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003099
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003100 /* Detect specific models for error handling */
3101 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3102 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003103 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003104 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003105 else
3106 phb->model = PNV_PHB_MODEL_UNKNOWN;
3107
Gavin Shanaa0c0332013-04-25 19:20:57 +00003108 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003109 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003110
Gavin Shanaa0c0332013-04-25 19:20:57 +00003111 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003112 phb->regs = of_iomap(np, 0);
3113 if (phb->regs == NULL)
3114 pr_err(" Failed to map registers !\n");
3115
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003116 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08003117 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003118 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003119 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003120 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003121 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3122 if (prop32)
3123 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003124
3125 /* Parse 64-bit MMIO range */
3126 pnv_ioda_parse_m64_window(phb);
3127
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003128 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003129 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003130 phb->ioda.m32_size += 0x10000;
3131
3132 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003133 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003134 phb->ioda.io_size = hose->pci_io_size;
3135 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
3136 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3137
Gavin Shanc35d2a82013-07-31 16:47:04 +08003138 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003139 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
3140 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00003141 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003142 if (phb->type == PNV_PHB_IODA1) {
3143 iomap_off = size;
3144 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
3145 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003146 pemap_off = size;
3147 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003148 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003149 phb->ioda.pe_alloc = aux;
3150 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08003151 if (phb->type == PNV_PHB_IODA1)
3152 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003153 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08003154 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003155
Gavin Shan7ebdf952012-08-20 03:49:15 +00003156 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003157 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003158 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003159
3160 /* Calculate how many 32-bit TCE segments we have */
3161 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
3162
Gavin Shanaa0c0332013-04-25 19:20:57 +00003163#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003164 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3165 window_type,
3166 window_num,
3167 starting_real_address,
3168 starting_pci_address,
3169 segment_size);
3170#endif
3171
Guo Chao262af552014-07-21 14:42:30 +10003172 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
3173 phb->ioda.total_pe, phb->ioda.reserved_pe,
3174 phb->ioda.m32_size, phb->ioda.m32_segsize);
3175 if (phb->ioda.m64_size)
3176 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3177 phb->ioda.m64_size, phb->ioda.m64_segsize);
3178 if (phb->ioda.io_size)
3179 pr_info(" IO: 0x%x [segment=0x%x]\n",
3180 phb->ioda.io_size, phb->ioda.io_segsize);
3181
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003182
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003183 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003184 phb->get_pe_state = pnv_ioda_get_pe_state;
3185 phb->freeze_pe = pnv_ioda_freeze_pe;
3186 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003187
3188 /* Setup RID -> PE mapping function */
3189 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
3190
3191 /* Setup TCEs */
3192 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
3193
3194 /* Setup MSI support */
3195 pnv_pci_init_ioda_msis(phb);
3196
Gavin Shanc40a4212012-08-20 03:49:20 +00003197 /*
3198 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3199 * to let the PCI core do resource assignment. It's supposed
3200 * that the PCI core will do correct I/O and MMIO alignment
3201 * for the P2P bridge bars so that each PCI bus (excluding
3202 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003203 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003204 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Daniel Axtens92ae0352015-04-28 15:12:05 +10003205 hose->controller_ops = pnv_pci_ioda_controller_ops;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003206
Wei Yang6e628c72015-03-25 16:23:55 +08003207#ifdef CONFIG_PCI_IOV
3208 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003209 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003210#endif
3211
Gavin Shanc40a4212012-08-20 03:49:20 +00003212 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003213
3214 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003215 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003216 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003217 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003218
3219 /* If we're running in kdump kerenl, the previous kerenl never
3220 * shutdown PCI devices correctly. We already got IODA table
3221 * cleaned out. So we have to issue PHB reset to stop all PCI
3222 * transactions from previous kerenl.
3223 */
3224 if (is_kdump_kernel()) {
3225 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003226 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3227 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003228 }
Guo Chao262af552014-07-21 14:42:30 +10003229
Gavin Shan9e9e8932014-11-12 13:36:05 +11003230 /* Remove M64 resource if we can't configure it successfully */
3231 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003232 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003233}
3234
Bjorn Helgaas67975002013-07-02 12:20:03 -06003235void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003236{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003237 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003238}
3239
3240void __init pnv_pci_init_ioda_hub(struct device_node *np)
3241{
3242 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003243 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003244 u64 hub_id;
3245
3246 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3247
3248 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3249 if (!prop64) {
3250 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3251 return;
3252 }
3253 hub_id = be64_to_cpup(prop64);
3254 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3255
3256 /* Count child PHBs */
3257 for_each_child_of_node(np, phbn) {
3258 /* Look for IODA1 PHBs */
3259 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003260 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003261 }
3262}