blob: e61610cccf6e5ef71f4968bfd99eefa370b1a2d3 [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>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000028
29#include <asm/sections.h>
30#include <asm/io.h>
31#include <asm/prom.h>
32#include <asm/pci-bridge.h>
33#include <asm/machdep.h>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000034#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000035#include <asm/ppc-pci.h>
36#include <asm/opal.h>
37#include <asm/iommu.h>
38#include <asm/tce.h>
Gavin Shan137436c2013-04-25 19:20:59 +000039#include <asm/xics.h>
Gavin Shan37c367f2013-06-20 18:13:25 +080040#include <asm/debug.h>
Guo Chao262af552014-07-21 14:42:30 +100041#include <asm/firmware.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110042#include <asm/pnv-pci.h>
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100043#include <asm/mmzone.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110044
Michael Neulingec249dd2015-05-27 16:07:16 +100045#include <misc/cxl-base.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000046
47#include "powernv.h"
48#include "pci.h"
49
Wei Yang781a8682015-03-25 16:23:57 +080050/* 256M DMA window, 4K TCE pages, 8 bytes TCE */
51#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
52
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100053static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
54
Joe Perches6d31c2f2014-09-21 10:55:06 -070055static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
56 const char *fmt, ...)
57{
58 struct va_format vaf;
59 va_list args;
60 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000061
Joe Perches6d31c2f2014-09-21 10:55:06 -070062 va_start(args, fmt);
63
64 vaf.fmt = fmt;
65 vaf.va = &args;
66
Wei Yang781a8682015-03-25 16:23:57 +080067 if (pe->flags & PNV_IODA_PE_DEV)
Joe Perches6d31c2f2014-09-21 10:55:06 -070068 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
Wei Yang781a8682015-03-25 16:23:57 +080069 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Joe Perches6d31c2f2014-09-21 10:55:06 -070070 sprintf(pfix, "%04x:%02x ",
71 pci_domain_nr(pe->pbus), pe->pbus->number);
Wei Yang781a8682015-03-25 16:23:57 +080072#ifdef CONFIG_PCI_IOV
73 else if (pe->flags & PNV_IODA_PE_VF)
74 sprintf(pfix, "%04x:%02x:%2x.%d",
75 pci_domain_nr(pe->parent_dev->bus),
76 (pe->rid & 0xff00) >> 8,
77 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
78#endif /* CONFIG_PCI_IOV*/
Joe Perches6d31c2f2014-09-21 10:55:06 -070079
80 printk("%spci %s: [PE# %.3d] %pV",
81 level, pfix, pe->pe_number, &vaf);
82
83 va_end(args);
84}
85
86#define pe_err(pe, fmt, ...) \
87 pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
88#define pe_warn(pe, fmt, ...) \
89 pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
90#define pe_info(pe, fmt, ...) \
91 pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000092
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020093static bool pnv_iommu_bypass_disabled __read_mostly;
94
95static int __init iommu_setup(char *str)
96{
97 if (!str)
98 return -EINVAL;
99
100 while (*str) {
101 if (!strncmp(str, "nobypass", 8)) {
102 pnv_iommu_bypass_disabled = true;
103 pr_info("PowerNV: IOMMU bypass window disabled.\n");
104 break;
105 }
106 str += strcspn(str, ",");
107 if (*str == ',')
108 str++;
109 }
110
111 return 0;
112}
113early_param("iommu", iommu_setup);
114
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +1000115/*
116 * stdcix is only supposed to be used in hypervisor real mode as per
117 * the architecture spec
118 */
119static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
120{
121 __asm__ __volatile__("stdcix %0,0,%1"
122 : : "r" (val), "r" (paddr) : "memory");
123}
124
Guo Chao262af552014-07-21 14:42:30 +1000125static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
126{
127 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
128 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
129}
130
Gavin Shan4b82ab12014-11-12 13:36:07 +1100131static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
132{
133 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
134 pr_warn("%s: Invalid PE %d on PHB#%x\n",
135 __func__, pe_no, phb->hose->global_number);
136 return;
137 }
138
139 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) {
140 pr_warn("%s: PE %d was assigned on PHB#%x\n",
141 __func__, pe_no, phb->hose->global_number);
142 return;
143 }
144
145 phb->ioda.pe_array[pe_no].phb = phb;
146 phb->ioda.pe_array[pe_no].pe_number = pe_no;
147}
148
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800149static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000150{
151 unsigned long pe;
152
153 do {
154 pe = find_next_zero_bit(phb->ioda.pe_alloc,
155 phb->ioda.total_pe, 0);
156 if (pe >= phb->ioda.total_pe)
157 return IODA_INVALID_PE;
158 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
159
Gavin Shan4cce9552013-04-25 19:21:00 +0000160 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000161 phb->ioda.pe_array[pe].pe_number = pe;
162 return pe;
163}
164
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800165static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000166{
167 WARN_ON(phb->ioda.pe_array[pe].pdev);
168
169 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
170 clear_bit(pe, phb->ioda.pe_alloc);
171}
172
Guo Chao262af552014-07-21 14:42:30 +1000173/* The default M64 BAR is shared by all PEs */
174static int pnv_ioda2_init_m64(struct pnv_phb *phb)
175{
176 const char *desc;
177 struct resource *r;
178 s64 rc;
179
180 /* Configure the default M64 BAR */
181 rc = opal_pci_set_phb_mem_window(phb->opal_id,
182 OPAL_M64_WINDOW_TYPE,
183 phb->ioda.m64_bar_idx,
184 phb->ioda.m64_base,
185 0, /* unused */
186 phb->ioda.m64_size);
187 if (rc != OPAL_SUCCESS) {
188 desc = "configuring";
189 goto fail;
190 }
191
192 /* Enable the default M64 BAR */
193 rc = opal_pci_phb_mmio_enable(phb->opal_id,
194 OPAL_M64_WINDOW_TYPE,
195 phb->ioda.m64_bar_idx,
196 OPAL_ENABLE_M64_SPLIT);
197 if (rc != OPAL_SUCCESS) {
198 desc = "enabling";
199 goto fail;
200 }
201
202 /* Mark the M64 BAR assigned */
203 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
204
205 /*
206 * Strip off the segment used by the reserved PE, which is
207 * expected to be 0 or last one of PE capabicity.
208 */
209 r = &phb->hose->mem_resources[1];
210 if (phb->ioda.reserved_pe == 0)
211 r->start += phb->ioda.m64_segsize;
212 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
213 r->end -= phb->ioda.m64_segsize;
214 else
215 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
216 phb->ioda.reserved_pe);
217
218 return 0;
219
220fail:
221 pr_warn(" Failure %lld %s M64 BAR#%d\n",
222 rc, desc, phb->ioda.m64_bar_idx);
223 opal_pci_phb_mmio_enable(phb->opal_id,
224 OPAL_M64_WINDOW_TYPE,
225 phb->ioda.m64_bar_idx,
226 OPAL_DISABLE_M64);
227 return -EIO;
228}
229
Gavin Shan5ef73562014-11-12 13:36:06 +1100230static void pnv_ioda2_reserve_m64_pe(struct pnv_phb *phb)
Guo Chao262af552014-07-21 14:42:30 +1000231{
232 resource_size_t sgsz = phb->ioda.m64_segsize;
233 struct pci_dev *pdev;
234 struct resource *r;
235 int base, step, i;
236
237 /*
238 * Root bus always has full M64 range and root port has
239 * M64 range used in reality. So we're checking root port
240 * instead of root bus.
241 */
242 list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100243 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
244 r = &pdev->resource[PCI_BRIDGE_RESOURCES + i];
Guo Chao262af552014-07-21 14:42:30 +1000245 if (!r->parent ||
246 !pnv_pci_is_mem_pref_64(r->flags))
247 continue;
248
249 base = (r->start - phb->ioda.m64_base) / sgsz;
250 for (step = 0; step < resource_size(r) / sgsz; step++)
Gavin Shan4b82ab12014-11-12 13:36:07 +1100251 pnv_ioda_reserve_pe(phb, base + step);
Guo Chao262af552014-07-21 14:42:30 +1000252 }
253 }
254}
255
256static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
257 struct pci_bus *bus, int all)
258{
259 resource_size_t segsz = phb->ioda.m64_segsize;
260 struct pci_dev *pdev;
261 struct resource *r;
262 struct pnv_ioda_pe *master_pe, *pe;
263 unsigned long size, *pe_alloc;
264 bool found;
265 int start, i, j;
266
267 /* Root bus shouldn't use M64 */
268 if (pci_is_root_bus(bus))
269 return IODA_INVALID_PE;
270
271 /* We support only one M64 window on each bus */
272 found = false;
273 pci_bus_for_each_resource(bus, r, i) {
274 if (r && r->parent &&
275 pnv_pci_is_mem_pref_64(r->flags)) {
276 found = true;
277 break;
278 }
279 }
280
281 /* No M64 window found ? */
282 if (!found)
283 return IODA_INVALID_PE;
284
285 /* Allocate bitmap */
286 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
287 pe_alloc = kzalloc(size, GFP_KERNEL);
288 if (!pe_alloc) {
289 pr_warn("%s: Out of memory !\n",
290 __func__);
291 return IODA_INVALID_PE;
292 }
293
294 /*
295 * Figure out reserved PE numbers by the PE
296 * the its child PEs.
297 */
298 start = (r->start - phb->ioda.m64_base) / segsz;
299 for (i = 0; i < resource_size(r) / segsz; i++)
300 set_bit(start + i, pe_alloc);
301
302 if (all)
303 goto done;
304
305 /*
306 * If the PE doesn't cover all subordinate buses,
307 * we need subtract from reserved PEs for children.
308 */
309 list_for_each_entry(pdev, &bus->devices, bus_list) {
310 if (!pdev->subordinate)
311 continue;
312
313 pci_bus_for_each_resource(pdev->subordinate, r, i) {
314 if (!r || !r->parent ||
315 !pnv_pci_is_mem_pref_64(r->flags))
316 continue;
317
318 start = (r->start - phb->ioda.m64_base) / segsz;
319 for (j = 0; j < resource_size(r) / segsz ; j++)
320 clear_bit(start + j, pe_alloc);
321 }
322 }
323
324 /*
325 * the current bus might not own M64 window and that's all
326 * contributed by its child buses. For the case, we needn't
327 * pick M64 dependent PE#.
328 */
329 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
330 kfree(pe_alloc);
331 return IODA_INVALID_PE;
332 }
333
334 /*
335 * Figure out the master PE and put all slave PEs to master
336 * PE's list to form compound PE.
337 */
338done:
339 master_pe = NULL;
340 i = -1;
341 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
342 phb->ioda.total_pe) {
343 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000344
345 if (!master_pe) {
346 pe->flags |= PNV_IODA_PE_MASTER;
347 INIT_LIST_HEAD(&pe->slaves);
348 master_pe = pe;
349 } else {
350 pe->flags |= PNV_IODA_PE_SLAVE;
351 pe->master = master_pe;
352 list_add_tail(&pe->list, &master_pe->slaves);
353 }
354 }
355
356 kfree(pe_alloc);
357 return master_pe->pe_number;
358}
359
360static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
361{
362 struct pci_controller *hose = phb->hose;
363 struct device_node *dn = hose->dn;
364 struct resource *res;
365 const u32 *r;
366 u64 pci_addr;
367
Gavin Shan1665c4a2014-11-12 13:36:04 +1100368 /* FIXME: Support M64 for P7IOC */
369 if (phb->type != PNV_PHB_IODA2) {
370 pr_info(" Not support M64 window\n");
371 return;
372 }
373
Guo Chao262af552014-07-21 14:42:30 +1000374 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
375 pr_info(" Firmware too old to support M64 window\n");
376 return;
377 }
378
379 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
380 if (!r) {
381 pr_info(" No <ibm,opal-m64-window> on %s\n",
382 dn->full_name);
383 return;
384 }
385
Guo Chao262af552014-07-21 14:42:30 +1000386 res = &hose->mem_resources[1];
387 res->start = of_translate_address(dn, r + 2);
388 res->end = res->start + of_read_number(r + 4, 2) - 1;
389 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
390 pci_addr = of_read_number(r, 2);
391 hose->mem_offset[1] = res->start - pci_addr;
392
393 phb->ioda.m64_size = resource_size(res);
394 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
395 phb->ioda.m64_base = pci_addr;
396
Wei Yange9863e62014-12-12 12:39:37 +0800397 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
398 res->start, res->end, pci_addr);
399
Guo Chao262af552014-07-21 14:42:30 +1000400 /* Use last M64 BAR to cover M64 window */
401 phb->ioda.m64_bar_idx = 15;
402 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shan5ef73562014-11-12 13:36:06 +1100403 phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000404 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
405}
406
Gavin Shan49dec922014-07-21 14:42:33 +1000407static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
408{
409 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
410 struct pnv_ioda_pe *slave;
411 s64 rc;
412
413 /* Fetch master PE */
414 if (pe->flags & PNV_IODA_PE_SLAVE) {
415 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100416 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
417 return;
418
Gavin Shan49dec922014-07-21 14:42:33 +1000419 pe_no = pe->pe_number;
420 }
421
422 /* Freeze master PE */
423 rc = opal_pci_eeh_freeze_set(phb->opal_id,
424 pe_no,
425 OPAL_EEH_ACTION_SET_FREEZE_ALL);
426 if (rc != OPAL_SUCCESS) {
427 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
428 __func__, rc, phb->hose->global_number, pe_no);
429 return;
430 }
431
432 /* Freeze slave PEs */
433 if (!(pe->flags & PNV_IODA_PE_MASTER))
434 return;
435
436 list_for_each_entry(slave, &pe->slaves, list) {
437 rc = opal_pci_eeh_freeze_set(phb->opal_id,
438 slave->pe_number,
439 OPAL_EEH_ACTION_SET_FREEZE_ALL);
440 if (rc != OPAL_SUCCESS)
441 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
442 __func__, rc, phb->hose->global_number,
443 slave->pe_number);
444 }
445}
446
Anton Blancharde51df2c2014-08-20 08:55:18 +1000447static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000448{
449 struct pnv_ioda_pe *pe, *slave;
450 s64 rc;
451
452 /* Find master PE */
453 pe = &phb->ioda.pe_array[pe_no];
454 if (pe->flags & PNV_IODA_PE_SLAVE) {
455 pe = pe->master;
456 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
457 pe_no = pe->pe_number;
458 }
459
460 /* Clear frozen state for master PE */
461 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
462 if (rc != OPAL_SUCCESS) {
463 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
464 __func__, rc, opt, phb->hose->global_number, pe_no);
465 return -EIO;
466 }
467
468 if (!(pe->flags & PNV_IODA_PE_MASTER))
469 return 0;
470
471 /* Clear frozen state for slave PEs */
472 list_for_each_entry(slave, &pe->slaves, list) {
473 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
474 slave->pe_number,
475 opt);
476 if (rc != OPAL_SUCCESS) {
477 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
478 __func__, rc, opt, phb->hose->global_number,
479 slave->pe_number);
480 return -EIO;
481 }
482 }
483
484 return 0;
485}
486
487static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
488{
489 struct pnv_ioda_pe *slave, *pe;
490 u8 fstate, state;
491 __be16 pcierr;
492 s64 rc;
493
494 /* Sanity check on PE number */
495 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
496 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
497
498 /*
499 * Fetch the master PE and the PE instance might be
500 * not initialized yet.
501 */
502 pe = &phb->ioda.pe_array[pe_no];
503 if (pe->flags & PNV_IODA_PE_SLAVE) {
504 pe = pe->master;
505 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
506 pe_no = pe->pe_number;
507 }
508
509 /* Check the master PE */
510 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
511 &state, &pcierr, NULL);
512 if (rc != OPAL_SUCCESS) {
513 pr_warn("%s: Failure %lld getting "
514 "PHB#%x-PE#%x state\n",
515 __func__, rc,
516 phb->hose->global_number, pe_no);
517 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
518 }
519
520 /* Check the slave PE */
521 if (!(pe->flags & PNV_IODA_PE_MASTER))
522 return state;
523
524 list_for_each_entry(slave, &pe->slaves, list) {
525 rc = opal_pci_eeh_freeze_status(phb->opal_id,
526 slave->pe_number,
527 &fstate,
528 &pcierr,
529 NULL);
530 if (rc != OPAL_SUCCESS) {
531 pr_warn("%s: Failure %lld getting "
532 "PHB#%x-PE#%x state\n",
533 __func__, rc,
534 phb->hose->global_number, slave->pe_number);
535 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
536 }
537
538 /*
539 * Override the result based on the ascending
540 * priority.
541 */
542 if (fstate > state)
543 state = fstate;
544 }
545
546 return state;
547}
548
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000549/* Currently those 2 are only used when MSIs are enabled, this will change
550 * but in the meantime, we need to protect them to avoid warnings
551 */
552#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800553static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000554{
555 struct pci_controller *hose = pci_bus_to_host(dev->bus);
556 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000557 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000558
559 if (!pdn)
560 return NULL;
561 if (pdn->pe_number == IODA_INVALID_PE)
562 return NULL;
563 return &phb->ioda.pe_array[pdn->pe_number];
564}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000565#endif /* CONFIG_PCI_MSI */
566
Gavin Shanb131a842014-11-12 13:36:08 +1100567static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
568 struct pnv_ioda_pe *parent,
569 struct pnv_ioda_pe *child,
570 bool is_add)
571{
572 const char *desc = is_add ? "adding" : "removing";
573 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
574 OPAL_REMOVE_PE_FROM_DOMAIN;
575 struct pnv_ioda_pe *slave;
576 long rc;
577
578 /* Parent PE affects child PE */
579 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
580 child->pe_number, op);
581 if (rc != OPAL_SUCCESS) {
582 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
583 rc, desc);
584 return -ENXIO;
585 }
586
587 if (!(child->flags & PNV_IODA_PE_MASTER))
588 return 0;
589
590 /* Compound case: parent PE affects slave PEs */
591 list_for_each_entry(slave, &child->slaves, list) {
592 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
593 slave->pe_number, op);
594 if (rc != OPAL_SUCCESS) {
595 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
596 rc, desc);
597 return -ENXIO;
598 }
599 }
600
601 return 0;
602}
603
604static int pnv_ioda_set_peltv(struct pnv_phb *phb,
605 struct pnv_ioda_pe *pe,
606 bool is_add)
607{
608 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800609 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100610 int ret;
611
612 /*
613 * Clear PE frozen state. If it's master PE, we need
614 * clear slave PE frozen state as well.
615 */
616 if (is_add) {
617 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
618 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
619 if (pe->flags & PNV_IODA_PE_MASTER) {
620 list_for_each_entry(slave, &pe->slaves, list)
621 opal_pci_eeh_freeze_clear(phb->opal_id,
622 slave->pe_number,
623 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
624 }
625 }
626
627 /*
628 * Associate PE in PELT. We need add the PE into the
629 * corresponding PELT-V as well. Otherwise, the error
630 * originated from the PE might contribute to other
631 * PEs.
632 */
633 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
634 if (ret)
635 return ret;
636
637 /* For compound PEs, any one affects all of them */
638 if (pe->flags & PNV_IODA_PE_MASTER) {
639 list_for_each_entry(slave, &pe->slaves, list) {
640 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
641 if (ret)
642 return ret;
643 }
644 }
645
646 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
647 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800648 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100649 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800650#ifdef CONFIG_PCI_IOV
651 else if (pe->flags & PNV_IODA_PE_VF)
652 pdev = pe->parent_dev->bus->self;
653#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100654 while (pdev) {
655 struct pci_dn *pdn = pci_get_pdn(pdev);
656 struct pnv_ioda_pe *parent;
657
658 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
659 parent = &phb->ioda.pe_array[pdn->pe_number];
660 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
661 if (ret)
662 return ret;
663 }
664
665 pdev = pdev->bus->self;
666 }
667
668 return 0;
669}
670
Wei Yang781a8682015-03-25 16:23:57 +0800671#ifdef CONFIG_PCI_IOV
672static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
673{
674 struct pci_dev *parent;
675 uint8_t bcomp, dcomp, fcomp;
676 int64_t rc;
677 long rid_end, rid;
678
679 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
680 if (pe->pbus) {
681 int count;
682
683 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
684 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
685 parent = pe->pbus->self;
686 if (pe->flags & PNV_IODA_PE_BUS_ALL)
687 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
688 else
689 count = 1;
690
691 switch(count) {
692 case 1: bcomp = OpalPciBusAll; break;
693 case 2: bcomp = OpalPciBus7Bits; break;
694 case 4: bcomp = OpalPciBus6Bits; break;
695 case 8: bcomp = OpalPciBus5Bits; break;
696 case 16: bcomp = OpalPciBus4Bits; break;
697 case 32: bcomp = OpalPciBus3Bits; break;
698 default:
699 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
700 count);
701 /* Do an exact match only */
702 bcomp = OpalPciBusAll;
703 }
704 rid_end = pe->rid + (count << 8);
705 } else {
706 if (pe->flags & PNV_IODA_PE_VF)
707 parent = pe->parent_dev;
708 else
709 parent = pe->pdev->bus->self;
710 bcomp = OpalPciBusAll;
711 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
712 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
713 rid_end = pe->rid + 1;
714 }
715
716 /* Clear the reverse map */
717 for (rid = pe->rid; rid < rid_end; rid++)
718 phb->ioda.pe_rmap[rid] = 0;
719
720 /* Release from all parents PELT-V */
721 while (parent) {
722 struct pci_dn *pdn = pci_get_pdn(parent);
723 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
724 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
725 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
726 /* XXX What to do in case of error ? */
727 }
728 parent = parent->bus->self;
729 }
730
731 opal_pci_eeh_freeze_set(phb->opal_id, pe->pe_number,
732 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
733
734 /* Disassociate PE in PELT */
735 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
736 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
737 if (rc)
738 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
739 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
740 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
741 if (rc)
742 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
743
744 pe->pbus = NULL;
745 pe->pdev = NULL;
746 pe->parent_dev = NULL;
747
748 return 0;
749}
750#endif /* CONFIG_PCI_IOV */
751
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800752static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000753{
754 struct pci_dev *parent;
755 uint8_t bcomp, dcomp, fcomp;
756 long rc, rid_end, rid;
757
758 /* Bus validation ? */
759 if (pe->pbus) {
760 int count;
761
762 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
763 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
764 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000765 if (pe->flags & PNV_IODA_PE_BUS_ALL)
766 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
767 else
768 count = 1;
769
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000770 switch(count) {
771 case 1: bcomp = OpalPciBusAll; break;
772 case 2: bcomp = OpalPciBus7Bits; break;
773 case 4: bcomp = OpalPciBus6Bits; break;
774 case 8: bcomp = OpalPciBus5Bits; break;
775 case 16: bcomp = OpalPciBus4Bits; break;
776 case 32: bcomp = OpalPciBus3Bits; break;
777 default:
Wei Yang781a8682015-03-25 16:23:57 +0800778 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
779 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000780 /* Do an exact match only */
781 bcomp = OpalPciBusAll;
782 }
783 rid_end = pe->rid + (count << 8);
784 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800785#ifdef CONFIG_PCI_IOV
786 if (pe->flags & PNV_IODA_PE_VF)
787 parent = pe->parent_dev;
788 else
789#endif /* CONFIG_PCI_IOV */
790 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000791 bcomp = OpalPciBusAll;
792 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
793 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
794 rid_end = pe->rid + 1;
795 }
796
Gavin Shan631ad692013-11-04 16:32:46 +0800797 /*
798 * Associate PE in PELT. We need add the PE into the
799 * corresponding PELT-V as well. Otherwise, the error
800 * originated from the PE might contribute to other
801 * PEs.
802 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000803 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
804 bcomp, dcomp, fcomp, OPAL_MAP_PE);
805 if (rc) {
806 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
807 return -ENXIO;
808 }
Gavin Shan631ad692013-11-04 16:32:46 +0800809
Gavin Shanb131a842014-11-12 13:36:08 +1100810 /* Configure PELTV */
811 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000812
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000813 /* Setup reverse map */
814 for (rid = pe->rid; rid < rid_end; rid++)
815 phb->ioda.pe_rmap[rid] = pe->pe_number;
816
817 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100818 if (phb->type != PNV_PHB_IODA1) {
819 pe->mve_number = 0;
820 goto out;
821 }
822
823 pe->mve_number = pe->pe_number;
824 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
825 if (rc != OPAL_SUCCESS) {
826 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
827 rc, pe->mve_number);
828 pe->mve_number = -1;
829 } else {
830 rc = opal_pci_set_mve_enable(phb->opal_id,
831 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000832 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100833 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000834 rc, pe->mve_number);
835 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000836 }
Gavin Shan4773f762014-11-12 13:36:09 +1100837 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000838
Gavin Shan4773f762014-11-12 13:36:09 +1100839out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000840 return 0;
841}
842
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800843static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
844 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000845{
846 struct pnv_ioda_pe *lpe;
847
Gavin Shan7ebdf952012-08-20 03:49:15 +0000848 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000849 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000850 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000851 return;
852 }
853 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000854 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000855}
856
857static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
858{
859 /* This is quite simplistic. The "base" weight of a device
860 * is 10. 0 means no DMA is to be accounted for it.
861 */
862
863 /* If it's a bridge, no DMA */
864 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
865 return 0;
866
867 /* Reduce the weight of slow USB controllers */
868 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
869 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
870 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
871 return 3;
872
873 /* Increase the weight of RAID (includes Obsidian) */
874 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
875 return 15;
876
877 /* Default */
878 return 10;
879}
880
Wei Yang781a8682015-03-25 16:23:57 +0800881#ifdef CONFIG_PCI_IOV
882static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
883{
884 struct pci_dn *pdn = pci_get_pdn(dev);
885 int i;
886 struct resource *res, res2;
887 resource_size_t size;
888 u16 num_vfs;
889
890 if (!dev->is_physfn)
891 return -EINVAL;
892
893 /*
894 * "offset" is in VFs. The M64 windows are sized so that when they
895 * are segmented, each segment is the same size as the IOV BAR.
896 * Each segment is in a separate PE, and the high order bits of the
897 * address are the PE number. Therefore, each VF's BAR is in a
898 * separate PE, and changing the IOV BAR start address changes the
899 * range of PEs the VFs are in.
900 */
901 num_vfs = pdn->num_vfs;
902 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
903 res = &dev->resource[i + PCI_IOV_RESOURCES];
904 if (!res->flags || !res->parent)
905 continue;
906
907 if (!pnv_pci_is_mem_pref_64(res->flags))
908 continue;
909
910 /*
911 * The actual IOV BAR range is determined by the start address
912 * and the actual size for num_vfs VFs BAR. This check is to
913 * make sure that after shifting, the range will not overlap
914 * with another device.
915 */
916 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
917 res2.flags = res->flags;
918 res2.start = res->start + (size * offset);
919 res2.end = res2.start + (size * num_vfs) - 1;
920
921 if (res2.end > res->end) {
922 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
923 i, &res2, res, num_vfs, offset);
924 return -EBUSY;
925 }
926 }
927
928 /*
929 * After doing so, there would be a "hole" in the /proc/iomem when
930 * offset is a positive value. It looks like the device return some
931 * mmio back to the system, which actually no one could use it.
932 */
933 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
934 res = &dev->resource[i + PCI_IOV_RESOURCES];
935 if (!res->flags || !res->parent)
936 continue;
937
938 if (!pnv_pci_is_mem_pref_64(res->flags))
939 continue;
940
941 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
942 res2 = *res;
943 res->start += size * offset;
944
945 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (enabling %d VFs shifted by %d)\n",
946 i, &res2, res, num_vfs, offset);
947 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
948 }
949 return 0;
950}
951#endif /* CONFIG_PCI_IOV */
952
Gavin Shanfb446ad2012-08-20 03:49:14 +0000953#if 0
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800954static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000955{
956 struct pci_controller *hose = pci_bus_to_host(dev->bus);
957 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000958 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000959 struct pnv_ioda_pe *pe;
960 int pe_num;
961
962 if (!pdn) {
963 pr_err("%s: Device tree node not associated properly\n",
964 pci_name(dev));
965 return NULL;
966 }
967 if (pdn->pe_number != IODA_INVALID_PE)
968 return NULL;
969
970 /* PE#0 has been pre-set */
971 if (dev->bus->number == 0)
972 pe_num = 0;
973 else
974 pe_num = pnv_ioda_alloc_pe(phb);
975 if (pe_num == IODA_INVALID_PE) {
976 pr_warning("%s: Not enough PE# available, disabling device\n",
977 pci_name(dev));
978 return NULL;
979 }
980
981 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
982 * pointer in the PE data structure, both should be destroyed at the
983 * same time. However, this needs to be looked at more closely again
984 * once we actually start removing things (Hotplug, SR-IOV, ...)
985 *
986 * At some point we want to remove the PDN completely anyways
987 */
988 pe = &phb->ioda.pe_array[pe_num];
989 pci_dev_get(dev);
990 pdn->pcidev = dev;
991 pdn->pe_number = pe_num;
992 pe->pdev = dev;
993 pe->pbus = NULL;
994 pe->tce32_seg = -1;
995 pe->mve_number = -1;
996 pe->rid = dev->bus->number << 8 | pdn->devfn;
997
998 pe_info(pe, "Associated device to PE\n");
999
1000 if (pnv_ioda_configure_pe(phb, pe)) {
1001 /* XXX What do we do here ? */
1002 if (pe_num)
1003 pnv_ioda_free_pe(phb, pe_num);
1004 pdn->pe_number = IODA_INVALID_PE;
1005 pe->pdev = NULL;
1006 pci_dev_put(dev);
1007 return NULL;
1008 }
1009
1010 /* Assign a DMA weight to the device */
1011 pe->dma_weight = pnv_ioda_dma_weight(dev);
1012 if (pe->dma_weight != 0) {
1013 phb->ioda.dma_weight += pe->dma_weight;
1014 phb->ioda.dma_pe_count++;
1015 }
1016
1017 /* Link the PE */
1018 pnv_ioda_link_pe_by_weight(phb, pe);
1019
1020 return pe;
1021}
Gavin Shanfb446ad2012-08-20 03:49:14 +00001022#endif /* Useful for SRIOV case */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001023
1024static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1025{
1026 struct pci_dev *dev;
1027
1028 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001029 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001030
1031 if (pdn == NULL) {
1032 pr_warn("%s: No device node associated with device !\n",
1033 pci_name(dev));
1034 continue;
1035 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001036 pdn->pe_number = pe->pe_number;
1037 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001038 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001039 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1040 }
1041}
1042
Gavin Shanfb446ad2012-08-20 03:49:14 +00001043/*
1044 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1045 * single PCI bus. Another one that contains the primary PCI bus and its
1046 * subordinate PCI devices and buses. The second type of PE is normally
1047 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1048 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001049static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001050{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001051 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001052 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001053 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +10001054 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001055
Guo Chao262af552014-07-21 14:42:30 +10001056 /* Check if PE is determined by M64 */
1057 if (phb->pick_m64_pe)
1058 pe_num = phb->pick_m64_pe(phb, bus, all);
1059
1060 /* The PE number isn't pinned by M64 */
1061 if (pe_num == IODA_INVALID_PE)
1062 pe_num = pnv_ioda_alloc_pe(phb);
1063
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001064 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001065 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1066 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001067 return;
1068 }
1069
1070 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +10001071 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001072 pe->pbus = bus;
1073 pe->pdev = NULL;
1074 pe->tce32_seg = -1;
1075 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001076 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001077 pe->dma_weight = 0;
1078
Gavin Shanfb446ad2012-08-20 03:49:14 +00001079 if (all)
1080 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
1081 bus->busn_res.start, bus->busn_res.end, pe_num);
1082 else
1083 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
1084 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001085
1086 if (pnv_ioda_configure_pe(phb, pe)) {
1087 /* XXX What do we do here ? */
1088 if (pe_num)
1089 pnv_ioda_free_pe(phb, pe_num);
1090 pe->pbus = NULL;
1091 return;
1092 }
1093
1094 /* Associate it with all child devices */
1095 pnv_ioda_setup_same_PE(bus, pe);
1096
Gavin Shan7ebdf952012-08-20 03:49:15 +00001097 /* Put PE to the list */
1098 list_add_tail(&pe->list, &phb->ioda.pe_list);
1099
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001100 /* Account for one DMA PE if at least one DMA capable device exist
1101 * below the bridge
1102 */
1103 if (pe->dma_weight != 0) {
1104 phb->ioda.dma_weight += pe->dma_weight;
1105 phb->ioda.dma_pe_count++;
1106 }
1107
1108 /* Link the PE */
1109 pnv_ioda_link_pe_by_weight(phb, pe);
1110}
1111
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001112static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001113{
1114 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001115
1116 pnv_ioda_setup_bus_PE(bus, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001117
1118 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001119 if (dev->subordinate) {
1120 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
1121 pnv_ioda_setup_bus_PE(dev->subordinate, 1);
1122 else
1123 pnv_ioda_setup_PEs(dev->subordinate);
1124 }
1125 }
1126}
1127
1128/*
1129 * Configure PEs so that the downstream PCI buses and devices
1130 * could have their associated PE#. Unfortunately, we didn't
1131 * figure out the way to identify the PLX bridge yet. So we
1132 * simply put the PCI bus and the subordinate behind the root
1133 * port to PE# here. The game rule here is expected to be changed
1134 * as soon as we can detected PLX bridge correctly.
1135 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001136static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001137{
1138 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001139 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001140
1141 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001142 phb = hose->private_data;
1143
1144 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +11001145 if (phb->reserve_m64_pe)
1146 phb->reserve_m64_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +10001147
Gavin Shanfb446ad2012-08-20 03:49:14 +00001148 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001149 }
1150}
1151
Gavin Shana8b2f822015-03-25 16:23:52 +08001152#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +08001153static int pnv_pci_vf_release_m64(struct pci_dev *pdev)
1154{
1155 struct pci_bus *bus;
1156 struct pci_controller *hose;
1157 struct pnv_phb *phb;
1158 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001159 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001160
1161 bus = pdev->bus;
1162 hose = pci_bus_to_host(bus);
1163 phb = hose->private_data;
1164 pdn = pci_get_pdn(pdev);
1165
Wei Yang02639b02015-03-25 16:23:59 +08001166 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
1167 for (j = 0; j < M64_PER_IOV; j++) {
1168 if (pdn->m64_wins[i][j] == IODA_INVALID_M64)
1169 continue;
1170 opal_pci_phb_mmio_enable(phb->opal_id,
1171 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 0);
1172 clear_bit(pdn->m64_wins[i][j], &phb->ioda.m64_bar_alloc);
1173 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1174 }
Wei Yang781a8682015-03-25 16:23:57 +08001175
1176 return 0;
1177}
1178
Wei Yang02639b02015-03-25 16:23:59 +08001179static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001180{
1181 struct pci_bus *bus;
1182 struct pci_controller *hose;
1183 struct pnv_phb *phb;
1184 struct pci_dn *pdn;
1185 unsigned int win;
1186 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001187 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001188 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001189 int total_vfs;
1190 resource_size_t size, start;
1191 int pe_num;
1192 int vf_groups;
1193 int vf_per_group;
Wei Yang781a8682015-03-25 16:23:57 +08001194
1195 bus = pdev->bus;
1196 hose = pci_bus_to_host(bus);
1197 phb = hose->private_data;
1198 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001199 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001200
1201 /* Initialize the m64_wins to IODA_INVALID_M64 */
1202 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yang02639b02015-03-25 16:23:59 +08001203 for (j = 0; j < M64_PER_IOV; j++)
1204 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1205
1206 if (pdn->m64_per_iov == M64_PER_IOV) {
1207 vf_groups = (num_vfs <= M64_PER_IOV) ? num_vfs: M64_PER_IOV;
1208 vf_per_group = (num_vfs <= M64_PER_IOV)? 1:
1209 roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1210 } else {
1211 vf_groups = 1;
1212 vf_per_group = 1;
1213 }
Wei Yang781a8682015-03-25 16:23:57 +08001214
1215 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1216 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1217 if (!res->flags || !res->parent)
1218 continue;
1219
1220 if (!pnv_pci_is_mem_pref_64(res->flags))
1221 continue;
1222
Wei Yang02639b02015-03-25 16:23:59 +08001223 for (j = 0; j < vf_groups; j++) {
1224 do {
1225 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1226 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001227
Wei Yang02639b02015-03-25 16:23:59 +08001228 if (win >= phb->ioda.m64_bar_idx + 1)
1229 goto m64_failed;
1230 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001231
Wei Yang02639b02015-03-25 16:23:59 +08001232 pdn->m64_wins[i][j] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001233
Wei Yang02639b02015-03-25 16:23:59 +08001234 if (pdn->m64_per_iov == M64_PER_IOV) {
1235 size = pci_iov_resource_size(pdev,
1236 PCI_IOV_RESOURCES + i);
1237 size = size * vf_per_group;
1238 start = res->start + size * j;
1239 } else {
1240 size = resource_size(res);
1241 start = res->start;
1242 }
1243
1244 /* Map the M64 here */
1245 if (pdn->m64_per_iov == M64_PER_IOV) {
1246 pe_num = pdn->offset + j;
1247 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1248 pe_num, OPAL_M64_WINDOW_TYPE,
1249 pdn->m64_wins[i][j], 0);
1250 }
1251
1252 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001253 OPAL_M64_WINDOW_TYPE,
Wei Yang02639b02015-03-25 16:23:59 +08001254 pdn->m64_wins[i][j],
1255 start,
Wei Yang781a8682015-03-25 16:23:57 +08001256 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001257 size);
Wei Yang781a8682015-03-25 16:23:57 +08001258
Wei Yang02639b02015-03-25 16:23:59 +08001259
1260 if (rc != OPAL_SUCCESS) {
1261 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1262 win, rc);
1263 goto m64_failed;
1264 }
1265
1266 if (pdn->m64_per_iov == M64_PER_IOV)
1267 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1268 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 2);
1269 else
1270 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1271 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 1);
1272
1273 if (rc != OPAL_SUCCESS) {
1274 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1275 win, rc);
1276 goto m64_failed;
1277 }
Wei Yang781a8682015-03-25 16:23:57 +08001278 }
1279 }
1280 return 0;
1281
1282m64_failed:
1283 pnv_pci_vf_release_m64(pdev);
1284 return -EBUSY;
1285}
1286
1287static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1288{
1289 struct pci_bus *bus;
1290 struct pci_controller *hose;
1291 struct pnv_phb *phb;
1292 struct iommu_table *tbl;
1293 unsigned long addr;
1294 int64_t rc;
1295
1296 bus = dev->bus;
1297 hose = pci_bus_to_host(bus);
1298 phb = hose->private_data;
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001299 tbl = pe->table_group.tables[0];
Wei Yang781a8682015-03-25 16:23:57 +08001300 addr = tbl->it_base;
1301
1302 opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1303 pe->pe_number << 1, 1, __pa(addr),
1304 0, 0x1000);
1305
1306 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1307 pe->pe_number,
1308 (pe->pe_number << 1) + 1,
1309 pe->tce_bypass_base,
1310 0);
1311 if (rc)
1312 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1313
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001314 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
1315 if (pe->table_group.group) {
1316 iommu_group_put(pe->table_group.group);
1317 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001318 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001319 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001320 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001321}
1322
Wei Yang02639b02015-03-25 16:23:59 +08001323static void pnv_ioda_release_vf_PE(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001324{
1325 struct pci_bus *bus;
1326 struct pci_controller *hose;
1327 struct pnv_phb *phb;
1328 struct pnv_ioda_pe *pe, *pe_n;
1329 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001330 u16 vf_index;
1331 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001332
1333 bus = pdev->bus;
1334 hose = pci_bus_to_host(bus);
1335 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001336 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001337
1338 if (!pdev->is_physfn)
1339 return;
1340
Wei Yang02639b02015-03-25 16:23:59 +08001341 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1342 int vf_group;
1343 int vf_per_group;
1344 int vf_index1;
1345
1346 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1347
1348 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++)
1349 for (vf_index = vf_group * vf_per_group;
1350 vf_index < (vf_group + 1) * vf_per_group &&
1351 vf_index < num_vfs;
1352 vf_index++)
1353 for (vf_index1 = vf_group * vf_per_group;
1354 vf_index1 < (vf_group + 1) * vf_per_group &&
1355 vf_index1 < num_vfs;
1356 vf_index1++){
1357
1358 rc = opal_pci_set_peltv(phb->opal_id,
1359 pdn->offset + vf_index,
1360 pdn->offset + vf_index1,
1361 OPAL_REMOVE_PE_FROM_DOMAIN);
1362
1363 if (rc)
1364 dev_warn(&pdev->dev, "%s: Failed to unlink same group PE#%d(%lld)\n",
1365 __func__,
1366 pdn->offset + vf_index1, rc);
1367 }
1368 }
1369
Wei Yang781a8682015-03-25 16:23:57 +08001370 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1371 if (pe->parent_dev != pdev)
1372 continue;
1373
1374 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1375
1376 /* Remove from list */
1377 mutex_lock(&phb->ioda.pe_list_mutex);
1378 list_del(&pe->list);
1379 mutex_unlock(&phb->ioda.pe_list_mutex);
1380
1381 pnv_ioda_deconfigure_pe(phb, pe);
1382
1383 pnv_ioda_free_pe(phb, pe->pe_number);
1384 }
1385}
1386
1387void pnv_pci_sriov_disable(struct pci_dev *pdev)
1388{
1389 struct pci_bus *bus;
1390 struct pci_controller *hose;
1391 struct pnv_phb *phb;
1392 struct pci_dn *pdn;
1393 struct pci_sriov *iov;
1394 u16 num_vfs;
1395
1396 bus = pdev->bus;
1397 hose = pci_bus_to_host(bus);
1398 phb = hose->private_data;
1399 pdn = pci_get_pdn(pdev);
1400 iov = pdev->sriov;
1401 num_vfs = pdn->num_vfs;
1402
1403 /* Release VF PEs */
Wei Yang02639b02015-03-25 16:23:59 +08001404 pnv_ioda_release_vf_PE(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001405
1406 if (phb->type == PNV_PHB_IODA2) {
Wei Yang02639b02015-03-25 16:23:59 +08001407 if (pdn->m64_per_iov == 1)
1408 pnv_pci_vf_resource_shift(pdev, -pdn->offset);
Wei Yang781a8682015-03-25 16:23:57 +08001409
1410 /* Release M64 windows */
1411 pnv_pci_vf_release_m64(pdev);
1412
1413 /* Release PE numbers */
1414 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1415 pdn->offset = 0;
1416 }
1417}
1418
1419static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1420 struct pnv_ioda_pe *pe);
1421static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1422{
1423 struct pci_bus *bus;
1424 struct pci_controller *hose;
1425 struct pnv_phb *phb;
1426 struct pnv_ioda_pe *pe;
1427 int pe_num;
1428 u16 vf_index;
1429 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001430 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001431
1432 bus = pdev->bus;
1433 hose = pci_bus_to_host(bus);
1434 phb = hose->private_data;
1435 pdn = pci_get_pdn(pdev);
1436
1437 if (!pdev->is_physfn)
1438 return;
1439
1440 /* Reserve PE for each VF */
1441 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
1442 pe_num = pdn->offset + vf_index;
1443
1444 pe = &phb->ioda.pe_array[pe_num];
1445 pe->pe_number = pe_num;
1446 pe->phb = phb;
1447 pe->flags = PNV_IODA_PE_VF;
1448 pe->pbus = NULL;
1449 pe->parent_dev = pdev;
1450 pe->tce32_seg = -1;
1451 pe->mve_number = -1;
1452 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1453 pci_iov_virtfn_devfn(pdev, vf_index);
1454
1455 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1456 hose->global_number, pdev->bus->number,
1457 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1458 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1459
1460 if (pnv_ioda_configure_pe(phb, pe)) {
1461 /* XXX What do we do here ? */
1462 if (pe_num)
1463 pnv_ioda_free_pe(phb, pe_num);
1464 pe->pdev = NULL;
1465 continue;
1466 }
1467
Wei Yang781a8682015-03-25 16:23:57 +08001468 /* Put PE to the list */
1469 mutex_lock(&phb->ioda.pe_list_mutex);
1470 list_add_tail(&pe->list, &phb->ioda.pe_list);
1471 mutex_unlock(&phb->ioda.pe_list_mutex);
1472
1473 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1474 }
Wei Yang02639b02015-03-25 16:23:59 +08001475
1476 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1477 int vf_group;
1478 int vf_per_group;
1479 int vf_index1;
1480
1481 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1482
1483 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++) {
1484 for (vf_index = vf_group * vf_per_group;
1485 vf_index < (vf_group + 1) * vf_per_group &&
1486 vf_index < num_vfs;
1487 vf_index++) {
1488 for (vf_index1 = vf_group * vf_per_group;
1489 vf_index1 < (vf_group + 1) * vf_per_group &&
1490 vf_index1 < num_vfs;
1491 vf_index1++) {
1492
1493 rc = opal_pci_set_peltv(phb->opal_id,
1494 pdn->offset + vf_index,
1495 pdn->offset + vf_index1,
1496 OPAL_ADD_PE_TO_DOMAIN);
1497
1498 if (rc)
1499 dev_warn(&pdev->dev, "%s: Failed to link same group PE#%d(%lld)\n",
1500 __func__,
1501 pdn->offset + vf_index1, rc);
1502 }
1503 }
1504 }
1505 }
Wei Yang781a8682015-03-25 16:23:57 +08001506}
1507
1508int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1509{
1510 struct pci_bus *bus;
1511 struct pci_controller *hose;
1512 struct pnv_phb *phb;
1513 struct pci_dn *pdn;
1514 int ret;
1515
1516 bus = pdev->bus;
1517 hose = pci_bus_to_host(bus);
1518 phb = hose->private_data;
1519 pdn = pci_get_pdn(pdev);
1520
1521 if (phb->type == PNV_PHB_IODA2) {
1522 /* Calculate available PE for required VFs */
1523 mutex_lock(&phb->ioda.pe_alloc_mutex);
1524 pdn->offset = bitmap_find_next_zero_area(
1525 phb->ioda.pe_alloc, phb->ioda.total_pe,
1526 0, num_vfs, 0);
1527 if (pdn->offset >= phb->ioda.total_pe) {
1528 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1529 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1530 pdn->offset = 0;
1531 return -EBUSY;
1532 }
1533 bitmap_set(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1534 pdn->num_vfs = num_vfs;
1535 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1536
1537 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001538 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001539 if (ret) {
1540 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1541 goto m64_failed;
1542 }
1543
1544 /*
1545 * When using one M64 BAR to map one IOV BAR, we need to shift
1546 * the IOV BAR according to the PE# allocated to the VFs.
1547 * Otherwise, the PE# for the VF will conflict with others.
1548 */
Wei Yang02639b02015-03-25 16:23:59 +08001549 if (pdn->m64_per_iov == 1) {
1550 ret = pnv_pci_vf_resource_shift(pdev, pdn->offset);
1551 if (ret)
1552 goto m64_failed;
1553 }
Wei Yang781a8682015-03-25 16:23:57 +08001554 }
1555
1556 /* Setup VF PEs */
1557 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1558
1559 return 0;
1560
1561m64_failed:
1562 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1563 pdn->offset = 0;
1564
1565 return ret;
1566}
1567
Gavin Shana8b2f822015-03-25 16:23:52 +08001568int pcibios_sriov_disable(struct pci_dev *pdev)
1569{
Wei Yang781a8682015-03-25 16:23:57 +08001570 pnv_pci_sriov_disable(pdev);
1571
Gavin Shana8b2f822015-03-25 16:23:52 +08001572 /* Release PCI data */
1573 remove_dev_pci_data(pdev);
1574 return 0;
1575}
1576
1577int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1578{
1579 /* Allocate PCI data */
1580 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001581
1582 pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001583 return 0;
1584}
1585#endif /* CONFIG_PCI_IOV */
1586
Gavin Shan959c9bd2013-04-25 19:21:02 +00001587static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001588{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001589 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001590 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001591
Gavin Shan959c9bd2013-04-25 19:21:02 +00001592 /*
1593 * The function can be called while the PE#
1594 * hasn't been assigned. Do nothing for the
1595 * case.
1596 */
1597 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1598 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001599
Gavin Shan959c9bd2013-04-25 19:21:02 +00001600 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001601 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001602 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001603 /*
1604 * Note: iommu_add_device() will fail here as
1605 * for physical PE: the device is already added by now;
1606 * for virtual PE: sysfs entries are not ready yet and
1607 * tce_iommu_bus_notifier will add the device to a group later.
1608 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001609}
1610
Daniel Axtens763d2d82015-04-28 15:12:07 +10001611static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001612{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001613 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1614 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001615 struct pci_dn *pdn = pci_get_pdn(pdev);
1616 struct pnv_ioda_pe *pe;
1617 uint64_t top;
1618 bool bypass = false;
1619
1620 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1621 return -ENODEV;;
1622
1623 pe = &phb->ioda.pe_array[pdn->pe_number];
1624 if (pe->tce_bypass_enabled) {
1625 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1626 bypass = (dma_mask >= top);
1627 }
1628
1629 if (bypass) {
1630 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1631 set_dma_ops(&pdev->dev, &dma_direct_ops);
1632 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
1633 } else {
1634 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1635 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001636 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001637 }
Brian W Harta32305b2014-07-31 14:24:37 -05001638 *pdev->dev.dma_mask = dma_mask;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001639 return 0;
1640}
1641
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001642static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
1643 struct pci_dev *pdev)
1644{
1645 struct pci_dn *pdn = pci_get_pdn(pdev);
1646 struct pnv_ioda_pe *pe;
1647 u64 end, mask;
1648
1649 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1650 return 0;
1651
1652 pe = &phb->ioda.pe_array[pdn->pe_number];
1653 if (!pe->tce_bypass_enabled)
1654 return __dma_get_required_mask(&pdev->dev);
1655
1656
1657 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1658 mask = 1ULL << (fls64(end) - 1);
1659 mask += mask - 1;
1660
1661 return mask;
1662}
1663
Gavin Shandff4a392014-07-15 17:00:55 +10001664static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001665 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001666{
1667 struct pci_dev *dev;
1668
1669 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001670 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001671 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001672
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001673 if (dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001674 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001675 }
1676}
1677
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001678static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
1679 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001680{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001681 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1682 &tbl->it_group_list, struct iommu_table_group_link,
1683 next);
1684 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001685 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001686 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001687 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1688 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001689 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001690 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001691
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001692 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1693 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1694 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001695
1696 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1697 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001698 start <<= shift;
1699 end <<= shift;
1700 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001701 start |= tbl->it_busno;
1702 end |= tbl->it_busno;
1703 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1704 /* p7ioc-style invalidation, 2 TCEs per write */
1705 start |= (1ull << 63);
1706 end |= (1ull << 63);
1707 inc = 16;
1708 } else {
1709 /* Default (older HW) */
1710 inc = 128;
1711 }
1712
1713 end |= inc - 1; /* round up end to be different than start */
1714
1715 mb(); /* Ensure above stores are visible */
1716 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001717 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001718 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001719 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001720 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001721 start += inc;
1722 }
1723
1724 /*
1725 * The iommu layer will do another mb() for us on build()
1726 * and we don't care on free()
1727 */
1728}
1729
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001730static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1731 long npages, unsigned long uaddr,
1732 enum dma_data_direction direction,
1733 struct dma_attrs *attrs)
1734{
1735 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1736 attrs);
1737
1738 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1739 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1740
1741 return ret;
1742}
1743
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001744#ifdef CONFIG_IOMMU_API
1745static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1746 unsigned long *hpa, enum dma_data_direction *direction)
1747{
1748 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1749
1750 if (!ret && (tbl->it_type &
1751 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1752 pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);
1753
1754 return ret;
1755}
1756#endif
1757
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001758static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1759 long npages)
1760{
1761 pnv_tce_free(tbl, index, npages);
1762
1763 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1764 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1765}
1766
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001767static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001768 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001769#ifdef CONFIG_IOMMU_API
1770 .exchange = pnv_ioda1_tce_xchg,
1771#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001772 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001773 .get = pnv_tce_get,
1774};
1775
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001776static inline void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_ioda_pe *pe)
1777{
1778 /* 01xb - invalidate TCEs that match the specified PE# */
1779 unsigned long val = (0x4ull << 60) | (pe->pe_number & 0xFF);
1780 struct pnv_phb *phb = pe->phb;
1781
1782 if (!phb->ioda.tce_inval_reg)
1783 return;
1784
1785 mb(); /* Ensure above stores are visible */
1786 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1787}
1788
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001789static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
1790 __be64 __iomem *invalidate, unsigned shift,
1791 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001792{
1793 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001794
1795 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001796 start = 0x2ull << 60;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001797 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001798 end = start;
1799
1800 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001801 start |= (index << shift);
1802 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001803 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001804 mb();
1805
1806 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001807 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001808 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001809 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001810 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001811 start += inc;
1812 }
1813}
1814
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001815static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1816 unsigned long index, unsigned long npages, bool rm)
1817{
1818 struct iommu_table_group_link *tgl;
1819
1820 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
1821 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1822 struct pnv_ioda_pe, table_group);
1823 __be64 __iomem *invalidate = rm ?
1824 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1825 pe->phb->ioda.tce_inval_reg;
1826
1827 pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
1828 invalidate, tbl->it_page_shift,
1829 index, npages);
1830 }
1831}
1832
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001833static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1834 long npages, unsigned long uaddr,
1835 enum dma_data_direction direction,
1836 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001837{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001838 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1839 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001840
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001841 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1842 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1843
1844 return ret;
1845}
1846
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001847#ifdef CONFIG_IOMMU_API
1848static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1849 unsigned long *hpa, enum dma_data_direction *direction)
1850{
1851 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1852
1853 if (!ret && (tbl->it_type &
1854 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1855 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1856
1857 return ret;
1858}
1859#endif
1860
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001861static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1862 long npages)
1863{
1864 pnv_tce_free(tbl, index, npages);
1865
1866 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1867 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001868}
1869
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001870static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001871 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001872#ifdef CONFIG_IOMMU_API
1873 .exchange = pnv_ioda2_tce_xchg,
1874#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001875 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001876 .get = pnv_tce_get,
1877};
1878
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001879static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1880 struct pnv_ioda_pe *pe, unsigned int base,
1881 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001882{
1883
1884 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001885 struct iommu_table *tbl;
1886 unsigned int i;
1887 int64_t rc;
1888 void *addr;
1889
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001890 /* XXX FIXME: Handle 64-bit only DMA devices */
1891 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1892 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1893
1894 /* We shouldn't already have a 32-bit DMA associated */
1895 if (WARN_ON(pe->tce32_seg >= 0))
1896 return;
1897
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001898 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001899 iommu_register_group(&pe->table_group, phb->hose->global_number,
1900 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001901 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001902
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001903 /* Grab a 32-bit TCE table */
1904 pe->tce32_seg = base;
1905 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1906 (base << 28), ((base + segs) << 28) - 1);
1907
1908 /* XXX Currently, we allocate one big contiguous table for the
1909 * TCEs. We only really need one chunk per 256M of TCE space
1910 * (ie per segment) but that's an optimization for later, it
1911 * requires some added smarts with our get/put_tce implementation
1912 */
1913 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1914 get_order(TCE32_TABLE_SIZE * segs));
1915 if (!tce_mem) {
1916 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1917 goto fail;
1918 }
1919 addr = page_address(tce_mem);
1920 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1921
1922 /* Configure HW */
1923 for (i = 0; i < segs; i++) {
1924 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1925 pe->pe_number,
1926 base + i, 1,
1927 __pa(addr) + TCE32_TABLE_SIZE * i,
1928 TCE32_TABLE_SIZE, 0x1000);
1929 if (rc) {
1930 pe_err(pe, " Failed to configure 32-bit TCE table,"
1931 " err %ld\n", rc);
1932 goto fail;
1933 }
1934 }
1935
1936 /* Setup linux iommu table */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001937 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001938 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001939
1940 /* OPAL variant of P7IOC SW invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001941 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10001942 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1943 TCE_PCI_SWINV_FREE |
1944 TCE_PCI_SWINV_PAIR);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001945
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001946 tbl->it_ops = &pnv_ioda1_iommu_ops;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001947 iommu_init_table(tbl, phb->hose->node);
1948
Wei Yang781a8682015-03-25 16:23:57 +08001949 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001950 /*
1951 * Setting table base here only for carrying iommu_group
1952 * further down to let iommu_add_device() do the job.
1953 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
1954 */
1955 set_iommu_table_base(&pe->pdev->dev, tbl);
1956 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001957 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001958 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001959
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001960 return;
1961 fail:
1962 /* XXX Failure: Try to fallback to 64-bit only ? */
1963 if (pe->tce32_seg >= 0)
1964 pe->tce32_seg = -1;
1965 if (tce_mem)
1966 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001967 if (tbl) {
1968 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
1969 iommu_free_table(tbl, "pnv");
1970 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001971}
1972
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10001973static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001974{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001975 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1976 int64_t rc;
1977
1978 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1979 if (enable) {
1980 phys_addr_t top = memblock_end_of_DRAM();
1981
1982 top = roundup_pow_of_two(top);
1983 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1984 pe->pe_number,
1985 window_id,
1986 pe->tce_bypass_base,
1987 top);
1988 } else {
1989 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1990 pe->pe_number,
1991 window_id,
1992 pe->tce_bypass_base,
1993 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001994 }
1995 if (rc)
1996 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1997 else
1998 pe->tce_bypass_enabled = enable;
1999}
2000
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002001#ifdef CONFIG_IOMMU_API
2002static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002003{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002004 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2005 table_group);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002006
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002007 iommu_take_ownership(table_group->tables[0]);
2008 pnv_pci_ioda2_set_bypass(pe, false);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002009}
2010
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002011static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2012{
2013 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2014 table_group);
2015
2016 iommu_release_ownership(table_group->tables[0]);
2017 pnv_pci_ioda2_set_bypass(pe, true);
2018}
2019
2020static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
2021 .take_ownership = pnv_ioda2_take_ownership,
2022 .release_ownership = pnv_ioda2_release_ownership,
2023};
2024#endif
2025
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002026static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2027{
2028 const __be64 *swinvp;
2029
2030 /* OPAL variant of PHB3 invalidated TCEs */
2031 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2032 if (!swinvp)
2033 return;
2034
2035 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2036 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2037}
2038
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002039static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift)
2040{
2041 struct page *tce_mem = NULL;
2042 __be64 *addr;
2043 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
2044
2045 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2046 if (!tce_mem) {
2047 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2048 return NULL;
2049 }
2050 addr = page_address(tce_mem);
2051 memset(addr, 0, 1UL << (order + PAGE_SHIFT));
2052
2053 return addr;
2054}
2055
2056static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2057 __u32 page_shift, __u64 window_size, struct iommu_table *tbl)
2058{
2059 void *addr;
2060 const unsigned window_shift = ilog2(window_size);
2061 unsigned entries_shift = window_shift - page_shift;
2062 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2063 const unsigned long tce_table_size = 1UL << table_shift;
2064
2065 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2066 return -EINVAL;
2067
2068 /* Allocate TCE table */
2069 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, table_shift);
2070 if (!addr)
2071 return -ENOMEM;
2072
2073 /* Setup linux iommu table */
2074 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2075 page_shift);
2076
2077 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2078 window_size, tce_table_size, bus_offset);
2079
2080 return 0;
2081}
2082
2083static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2084{
2085 if (!tbl->it_size)
2086 return;
2087
2088 free_pages(tbl->it_base, get_order(tbl->it_size << 3));
2089}
2090
Gavin Shan373f5652013-04-25 19:21:01 +00002091static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2092 struct pnv_ioda_pe *pe)
2093{
Gavin Shan373f5652013-04-25 19:21:01 +00002094 struct iommu_table *tbl;
Gavin Shan373f5652013-04-25 19:21:01 +00002095 int64_t rc;
2096
2097 /* We shouldn't already have a 32-bit DMA associated */
2098 if (WARN_ON(pe->tce32_seg >= 0))
2099 return;
2100
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002101 /* TVE #1 is selected by PCI address bit 59 */
2102 pe->tce_bypass_base = 1ull << 59;
2103
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002104 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002105 iommu_register_group(&pe->table_group, phb->hose->global_number,
2106 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002107 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002108
Gavin Shan373f5652013-04-25 19:21:01 +00002109 /* The PE will reserve all possible 32-bits space */
2110 pe->tce32_seg = 0;
Gavin Shan373f5652013-04-25 19:21:01 +00002111 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002112 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002113
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002114 /* Setup linux iommu table */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002115 rc = pnv_pci_ioda2_table_alloc_pages(pe->phb->hose->node,
2116 0, IOMMU_PAGE_SHIFT_4K, phb->ioda.m32_pci_base, tbl);
2117 if (rc) {
2118 pe_err(pe, "Failed to create 32-bit TCE table, err %ld", rc);
2119 goto fail;
2120 }
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002121
2122 tbl->it_ops = &pnv_ioda2_iommu_ops;
2123 iommu_init_table(tbl, phb->hose->node);
2124#ifdef CONFIG_IOMMU_API
2125 pe->table_group.ops = &pnv_pci_ioda2_ops;
2126#endif
2127
Gavin Shan373f5652013-04-25 19:21:01 +00002128 /*
2129 * Map TCE table through TVT. The TVE index is the PE number
2130 * shifted by 1 bit for 32-bits DMA space.
2131 */
2132 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002133 pe->pe_number << 1, 1, __pa(tbl->it_base),
2134 tbl->it_size << 3, 1ULL << tbl->it_page_shift);
Gavin Shan373f5652013-04-25 19:21:01 +00002135 if (rc) {
2136 pe_err(pe, "Failed to configure 32-bit TCE table,"
2137 " err %ld\n", rc);
2138 goto fail;
2139 }
2140
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002141 pnv_pci_ioda2_tce_invalidate_entire(pe);
2142
Gavin Shan373f5652013-04-25 19:21:01 +00002143 /* OPAL variant of PHB3 invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002144 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10002145 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002146
Wei Yang781a8682015-03-25 16:23:57 +08002147 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002148 /*
2149 * Setting table base here only for carrying iommu_group
2150 * further down to let iommu_add_device() do the job.
2151 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2152 */
2153 set_iommu_table_base(&pe->pdev->dev, tbl);
2154 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002155 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002156 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10002157
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002158 /* Also create a bypass window */
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -02002159 if (!pnv_iommu_bypass_disabled)
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002160 pnv_pci_ioda2_set_bypass(pe, true);
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -02002161
Gavin Shan373f5652013-04-25 19:21:01 +00002162 return;
2163fail:
2164 if (pe->tce32_seg >= 0)
2165 pe->tce32_seg = -1;
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002166 if (tbl) {
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002167 pnv_pci_ioda2_table_free_pages(tbl);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002168 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
2169 iommu_free_table(tbl, "pnv");
2170 }
Gavin Shan373f5652013-04-25 19:21:01 +00002171}
2172
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002173static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002174{
2175 struct pci_controller *hose = phb->hose;
2176 unsigned int residual, remaining, segs, tw, base;
2177 struct pnv_ioda_pe *pe;
2178
2179 /* If we have more PE# than segments available, hand out one
2180 * per PE until we run out and let the rest fail. If not,
2181 * then we assign at least one segment per PE, plus more based
2182 * on the amount of devices under that PE
2183 */
2184 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
2185 residual = 0;
2186 else
2187 residual = phb->ioda.tce32_count -
2188 phb->ioda.dma_pe_count;
2189
2190 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
2191 hose->global_number, phb->ioda.tce32_count);
2192 pr_info("PCI: %d PE# for a total weight of %d\n",
2193 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
2194
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002195 pnv_pci_ioda_setup_opal_tce_kill(phb);
2196
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002197 /* Walk our PE list and configure their DMA segments, hand them
2198 * out one base segment plus any residual segments based on
2199 * weight
2200 */
2201 remaining = phb->ioda.tce32_count;
2202 tw = phb->ioda.dma_weight;
2203 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00002204 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002205 if (!pe->dma_weight)
2206 continue;
2207 if (!remaining) {
2208 pe_warn(pe, "No DMA32 resources available\n");
2209 continue;
2210 }
2211 segs = 1;
2212 if (residual) {
2213 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
2214 if (segs > remaining)
2215 segs = remaining;
2216 }
Gavin Shan373f5652013-04-25 19:21:01 +00002217
2218 /*
2219 * For IODA2 compliant PHB3, we needn't care about the weight.
2220 * The all available 32-bits DMA space will be assigned to
2221 * the specific PE.
2222 */
2223 if (phb->type == PNV_PHB_IODA1) {
2224 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
2225 pe->dma_weight, segs);
2226 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
2227 } else {
2228 pe_info(pe, "Assign DMA32 space\n");
2229 segs = 0;
2230 pnv_pci_ioda2_setup_dma_pe(phb, pe);
2231 }
2232
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002233 remaining -= segs;
2234 base += segs;
2235 }
2236}
2237
2238#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002239static void pnv_ioda2_msi_eoi(struct irq_data *d)
2240{
2241 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2242 struct irq_chip *chip = irq_data_get_irq_chip(d);
2243 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2244 ioda.irq_chip);
2245 int64_t rc;
2246
2247 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2248 WARN_ON_ONCE(rc);
2249
2250 icp_native_eoi(d);
2251}
2252
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002253
2254static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2255{
2256 struct irq_data *idata;
2257 struct irq_chip *ichip;
2258
2259 if (phb->type != PNV_PHB_IODA2)
2260 return;
2261
2262 if (!phb->ioda.irq_chip_init) {
2263 /*
2264 * First time we setup an MSI IRQ, we need to setup the
2265 * corresponding IRQ chip to route correctly.
2266 */
2267 idata = irq_get_irq_data(virq);
2268 ichip = irq_data_get_irq_chip(idata);
2269 phb->ioda.irq_chip_init = 1;
2270 phb->ioda.irq_chip = *ichip;
2271 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2272 }
2273 irq_set_chip(virq, &phb->ioda.irq_chip);
2274}
2275
Ian Munsie80c49c72014-10-08 19:54:57 +11002276#ifdef CONFIG_CXL_BASE
2277
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002278struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002279{
2280 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2281
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002282 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002283}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002284EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002285
Ryan Grimm1212aa12015-01-19 11:52:50 -06002286int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002287{
2288 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2289 struct pnv_phb *phb = hose->private_data;
2290 struct pnv_ioda_pe *pe;
2291 int rc;
2292
2293 pe = pnv_ioda_get_pe(dev);
2294 if (!pe)
2295 return -ENODEV;
2296
2297 pe_info(pe, "Switching PHB to CXL\n");
2298
Ryan Grimm1212aa12015-01-19 11:52:50 -06002299 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11002300 if (rc)
2301 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2302
2303 return rc;
2304}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002305EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002306
2307/* Find PHB for cxl dev and allocate MSI hwirqs?
2308 * Returns the absolute hardware IRQ number
2309 */
2310int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2311{
2312 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2313 struct pnv_phb *phb = hose->private_data;
2314 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2315
2316 if (hwirq < 0) {
2317 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2318 return -ENOSPC;
2319 }
2320
2321 return phb->msi_base + hwirq;
2322}
2323EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2324
2325void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2326{
2327 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2328 struct pnv_phb *phb = hose->private_data;
2329
2330 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2331}
2332EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2333
2334void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2335 struct pci_dev *dev)
2336{
2337 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2338 struct pnv_phb *phb = hose->private_data;
2339 int i, hwirq;
2340
2341 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2342 if (!irqs->range[i])
2343 continue;
2344 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2345 i, irqs->offset[i],
2346 irqs->range[i]);
2347 hwirq = irqs->offset[i] - phb->msi_base;
2348 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2349 irqs->range[i]);
2350 }
2351}
2352EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2353
2354int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2355 struct pci_dev *dev, int num)
2356{
2357 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2358 struct pnv_phb *phb = hose->private_data;
2359 int i, hwirq, try;
2360
2361 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2362
2363 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2364 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2365 try = num;
2366 while (try) {
2367 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2368 if (hwirq >= 0)
2369 break;
2370 try /= 2;
2371 }
2372 if (!try)
2373 goto fail;
2374
2375 irqs->offset[i] = phb->msi_base + hwirq;
2376 irqs->range[i] = try;
2377 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2378 i, irqs->offset[i], irqs->range[i]);
2379 num -= try;
2380 }
2381 if (num)
2382 goto fail;
2383
2384 return 0;
2385fail:
2386 pnv_cxl_release_hwirq_ranges(irqs, dev);
2387 return -ENOSPC;
2388}
2389EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2390
2391int pnv_cxl_get_irq_count(struct pci_dev *dev)
2392{
2393 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2394 struct pnv_phb *phb = hose->private_data;
2395
2396 return phb->msi_bmp.irq_count;
2397}
2398EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2399
2400int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2401 unsigned int virq)
2402{
2403 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2404 struct pnv_phb *phb = hose->private_data;
2405 unsigned int xive_num = hwirq - phb->msi_base;
2406 struct pnv_ioda_pe *pe;
2407 int rc;
2408
2409 if (!(pe = pnv_ioda_get_pe(dev)))
2410 return -ENODEV;
2411
2412 /* Assign XIVE to PE */
2413 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2414 if (rc) {
2415 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2416 "hwirq 0x%x XIVE 0x%x PE\n",
2417 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2418 return -EIO;
2419 }
2420 set_msi_irq_chip(phb, virq);
2421
2422 return 0;
2423}
2424EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2425#endif
2426
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002427static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002428 unsigned int hwirq, unsigned int virq,
2429 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002430{
2431 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2432 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002433 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002434 int rc;
2435
2436 /* No PE assigned ? bail out ... no MSI for you ! */
2437 if (pe == NULL)
2438 return -ENXIO;
2439
2440 /* Check if we have an MVE */
2441 if (pe->mve_number < 0)
2442 return -ENXIO;
2443
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002444 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002445 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002446 is_64 = 0;
2447
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002448 /* Assign XIVE to PE */
2449 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2450 if (rc) {
2451 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2452 pci_name(dev), rc, xive_num);
2453 return -EIO;
2454 }
2455
2456 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002457 __be64 addr64;
2458
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002459 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2460 &addr64, &data);
2461 if (rc) {
2462 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2463 pci_name(dev), rc);
2464 return -EIO;
2465 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002466 msg->address_hi = be64_to_cpu(addr64) >> 32;
2467 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002468 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002469 __be32 addr32;
2470
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002471 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2472 &addr32, &data);
2473 if (rc) {
2474 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2475 pci_name(dev), rc);
2476 return -EIO;
2477 }
2478 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002479 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002480 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002481 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002482
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002483 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002484
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002485 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2486 " address=%x_%08x data=%x PE# %d\n",
2487 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2488 msg->address_hi, msg->address_lo, data, pe->pe_number);
2489
2490 return 0;
2491}
2492
2493static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2494{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002495 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002496 const __be32 *prop = of_get_property(phb->hose->dn,
2497 "ibm,opal-msi-ranges", NULL);
2498 if (!prop) {
2499 /* BML Fallback */
2500 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2501 }
2502 if (!prop)
2503 return;
2504
2505 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002506 count = be32_to_cpup(prop + 1);
2507 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002508 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2509 phb->hose->global_number);
2510 return;
2511 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002512
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002513 phb->msi_setup = pnv_pci_ioda_msi_setup;
2514 phb->msi32_support = 1;
2515 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002516 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002517}
2518#else
2519static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2520#endif /* CONFIG_PCI_MSI */
2521
Wei Yang6e628c72015-03-25 16:23:55 +08002522#ifdef CONFIG_PCI_IOV
2523static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2524{
2525 struct pci_controller *hose;
2526 struct pnv_phb *phb;
2527 struct resource *res;
2528 int i;
2529 resource_size_t size;
2530 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002531 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002532
2533 if (!pdev->is_physfn || pdev->is_added)
2534 return;
2535
2536 hose = pci_bus_to_host(pdev->bus);
2537 phb = hose->private_data;
2538
2539 pdn = pci_get_pdn(pdev);
2540 pdn->vfs_expanded = 0;
2541
Wei Yang5b88ec22015-03-25 16:23:58 +08002542 total_vfs = pci_sriov_get_totalvfs(pdev);
2543 pdn->m64_per_iov = 1;
2544 mul = phb->ioda.total_pe;
2545
2546 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2547 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2548 if (!res->flags || res->parent)
2549 continue;
2550 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2551 dev_warn(&pdev->dev, " non M64 VF BAR%d: %pR\n",
2552 i, res);
2553 continue;
2554 }
2555
2556 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
2557
2558 /* bigger than 64M */
2559 if (size > (1 << 26)) {
2560 dev_info(&pdev->dev, "PowerNV: VF BAR%d: %pR IOV size is bigger than 64M, roundup power2\n",
2561 i, res);
2562 pdn->m64_per_iov = M64_PER_IOV;
2563 mul = roundup_pow_of_two(total_vfs);
2564 break;
2565 }
2566 }
2567
Wei Yang6e628c72015-03-25 16:23:55 +08002568 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2569 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2570 if (!res->flags || res->parent)
2571 continue;
2572 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2573 dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
2574 i, res);
2575 continue;
2576 }
2577
2578 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
2579 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002580 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08002581 dev_dbg(&pdev->dev, " %pR\n", res);
2582 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08002583 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08002584 }
Wei Yang5b88ec22015-03-25 16:23:58 +08002585 pdn->vfs_expanded = mul;
Wei Yang6e628c72015-03-25 16:23:55 +08002586}
2587#endif /* CONFIG_PCI_IOV */
2588
Gavin Shan11685be2012-08-20 03:49:16 +00002589/*
2590 * This function is supposed to be called on basis of PE from top
2591 * to bottom style. So the the I/O or MMIO segment assigned to
2592 * parent PE could be overrided by its child PEs if necessary.
2593 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002594static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
2595 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00002596{
2597 struct pnv_phb *phb = hose->private_data;
2598 struct pci_bus_region region;
2599 struct resource *res;
2600 int i, index;
2601 int rc;
2602
2603 /*
2604 * NOTE: We only care PCI bus based PE for now. For PCI
2605 * device based PE, for example SRIOV sensitive VF should
2606 * be figured out later.
2607 */
2608 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2609
2610 pci_bus_for_each_resource(pe->pbus, res, i) {
2611 if (!res || !res->flags ||
2612 res->start > res->end)
2613 continue;
2614
2615 if (res->flags & IORESOURCE_IO) {
2616 region.start = res->start - phb->ioda.io_pci_base;
2617 region.end = res->end - phb->ioda.io_pci_base;
2618 index = region.start / phb->ioda.io_segsize;
2619
2620 while (index < phb->ioda.total_pe &&
2621 region.start <= region.end) {
2622 phb->ioda.io_segmap[index] = pe->pe_number;
2623 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2624 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2625 if (rc != OPAL_SUCCESS) {
2626 pr_err("%s: OPAL error %d when mapping IO "
2627 "segment #%d to PE#%d\n",
2628 __func__, rc, index, pe->pe_number);
2629 break;
2630 }
2631
2632 region.start += phb->ioda.io_segsize;
2633 index++;
2634 }
Gavin Shan027fa022015-03-27 11:29:00 +11002635 } else if ((res->flags & IORESOURCE_MEM) &&
2636 !pnv_pci_is_mem_pref_64(res->flags)) {
Gavin Shan11685be2012-08-20 03:49:16 +00002637 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002638 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002639 phb->ioda.m32_pci_base;
2640 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002641 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002642 phb->ioda.m32_pci_base;
2643 index = region.start / phb->ioda.m32_segsize;
2644
2645 while (index < phb->ioda.total_pe &&
2646 region.start <= region.end) {
2647 phb->ioda.m32_segmap[index] = pe->pe_number;
2648 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2649 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2650 if (rc != OPAL_SUCCESS) {
2651 pr_err("%s: OPAL error %d when mapping M32 "
2652 "segment#%d to PE#%d",
2653 __func__, rc, index, pe->pe_number);
2654 break;
2655 }
2656
2657 region.start += phb->ioda.m32_segsize;
2658 index++;
2659 }
2660 }
2661 }
2662}
2663
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002664static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00002665{
2666 struct pci_controller *tmp, *hose;
2667 struct pnv_phb *phb;
2668 struct pnv_ioda_pe *pe;
2669
2670 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2671 phb = hose->private_data;
2672 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2673 pnv_ioda_setup_pe_seg(hose, pe);
2674 }
2675 }
2676}
2677
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002678static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00002679{
2680 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00002681 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00002682
2683 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2684 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00002685
2686 /* Mark the PHB initialization done */
2687 phb = hose->private_data;
2688 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00002689 }
2690}
2691
Gavin Shan37c367f2013-06-20 18:13:25 +08002692static void pnv_pci_ioda_create_dbgfs(void)
2693{
2694#ifdef CONFIG_DEBUG_FS
2695 struct pci_controller *hose, *tmp;
2696 struct pnv_phb *phb;
2697 char name[16];
2698
2699 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2700 phb = hose->private_data;
2701
2702 sprintf(name, "PCI%04x", hose->global_number);
2703 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
2704 if (!phb->dbgfs)
2705 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
2706 __func__, hose->global_number);
2707 }
2708#endif /* CONFIG_DEBUG_FS */
2709}
2710
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002711static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00002712{
2713 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00002714 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00002715 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002716
Gavin Shan37c367f2013-06-20 18:13:25 +08002717 pnv_pci_ioda_create_dbgfs();
2718
Gavin Shane9cc17d2013-06-20 13:21:14 +08002719#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08002720 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04002721 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08002722#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00002723}
2724
Gavin Shan271fd032012-09-11 16:59:47 -06002725/*
2726 * Returns the alignment for I/O or memory windows for P2P
2727 * bridges. That actually depends on how PEs are segmented.
2728 * For now, we return I/O or M32 segment size for PE sensitive
2729 * P2P bridges. Otherwise, the default values (4KiB for I/O,
2730 * 1MiB for memory) will be returned.
2731 *
2732 * The current PCI bus might be put into one PE, which was
2733 * create against the parent PCI bridge. For that case, we
2734 * needn't enlarge the alignment so that we can save some
2735 * resources.
2736 */
2737static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2738 unsigned long type)
2739{
2740 struct pci_dev *bridge;
2741 struct pci_controller *hose = pci_bus_to_host(bus);
2742 struct pnv_phb *phb = hose->private_data;
2743 int num_pci_bridges = 0;
2744
2745 bridge = bus->self;
2746 while (bridge) {
2747 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2748 num_pci_bridges++;
2749 if (num_pci_bridges >= 2)
2750 return 1;
2751 }
2752
2753 bridge = bridge->bus->self;
2754 }
2755
Guo Chao262af552014-07-21 14:42:30 +10002756 /* We fail back to M32 if M64 isn't supported */
2757 if (phb->ioda.m64_segsize &&
2758 pnv_pci_is_mem_pref_64(type))
2759 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06002760 if (type & IORESOURCE_MEM)
2761 return phb->ioda.m32_segsize;
2762
2763 return phb->ioda.io_segsize;
2764}
2765
Wei Yang5350ab32015-03-25 16:23:56 +08002766#ifdef CONFIG_PCI_IOV
2767static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
2768 int resno)
2769{
2770 struct pci_dn *pdn = pci_get_pdn(pdev);
2771 resource_size_t align, iov_align;
2772
2773 iov_align = resource_size(&pdev->resource[resno]);
2774 if (iov_align)
2775 return iov_align;
2776
2777 align = pci_iov_resource_size(pdev, resno);
2778 if (pdn->vfs_expanded)
2779 return pdn->vfs_expanded * align;
2780
2781 return align;
2782}
2783#endif /* CONFIG_PCI_IOV */
2784
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002785/* Prevent enabling devices for which we couldn't properly
2786 * assign a PE
2787 */
Daniel Axtensc88c2a12015-03-31 16:00:41 +11002788static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002789{
Gavin Shandb1266c2012-08-20 03:49:18 +00002790 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2791 struct pnv_phb *phb = hose->private_data;
2792 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002793
Gavin Shandb1266c2012-08-20 03:49:18 +00002794 /* The function is probably called while the PEs have
2795 * not be created yet. For example, resource reassignment
2796 * during PCI probe period. We just skip the check if
2797 * PEs isn't ready.
2798 */
2799 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11002800 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00002801
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002802 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002803 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11002804 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00002805
Daniel Axtensc88c2a12015-03-31 16:00:41 +11002806 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002807}
2808
2809static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
2810 u32 devfn)
2811{
2812 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
2813}
2814
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10002815static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10002816{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10002817 struct pnv_phb *phb = hose->private_data;
2818
Gavin Shand1a85ee2014-09-30 12:39:05 +10002819 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10002820 OPAL_ASSERT_RESET);
2821}
2822
Daniel Axtens92ae0352015-04-28 15:12:05 +10002823static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
2824 .dma_dev_setup = pnv_pci_dma_dev_setup,
2825#ifdef CONFIG_PCI_MSI
2826 .setup_msi_irqs = pnv_setup_msi_irqs,
2827 .teardown_msi_irqs = pnv_teardown_msi_irqs,
2828#endif
2829 .enable_device_hook = pnv_pci_enable_device_hook,
2830 .window_alignment = pnv_pci_window_alignment,
2831 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
Daniel Axtens763d2d82015-04-28 15:12:07 +10002832 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10002833 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10002834};
2835
Anton Blancharde51df2c2014-08-20 08:55:18 +10002836static void __init pnv_pci_init_ioda_phb(struct device_node *np,
2837 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002838{
2839 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002840 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08002841 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10002842 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002843 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08002844 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002845 u64 phb_id;
2846 void *aux;
2847 long rc;
2848
Gavin Shan58d714e2013-07-31 16:47:00 +08002849 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002850
2851 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
2852 if (!prop64) {
2853 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
2854 return;
2855 }
2856 phb_id = be64_to_cpup(prop64);
2857 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
2858
Michael Ellermane39f223f2014-11-18 16:47:35 +11002859 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08002860
2861 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08002862 phb->hose = hose = pcibios_alloc_controller(np);
2863 if (!phb->hose) {
2864 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002865 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11002866 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002867 return;
2868 }
2869
2870 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08002871 prop32 = of_get_property(np, "bus-range", &len);
2872 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002873 hose->first_busno = be32_to_cpu(prop32[0]);
2874 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08002875 } else {
2876 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
2877 hose->first_busno = 0;
2878 hose->last_busno = 0xff;
2879 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002880 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08002881 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002882 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00002883 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08002884 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002885
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00002886 /* Detect specific models for error handling */
2887 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
2888 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00002889 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00002890 phb->model = PNV_PHB_MODEL_PHB3;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00002891 else
2892 phb->model = PNV_PHB_MODEL_UNKNOWN;
2893
Gavin Shanaa0c0332013-04-25 19:20:57 +00002894 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08002895 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002896
Gavin Shanaa0c0332013-04-25 19:20:57 +00002897 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002898 phb->regs = of_iomap(np, 0);
2899 if (phb->regs == NULL)
2900 pr_err(" Failed to map registers !\n");
2901
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002902 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08002903 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00002904 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08002905 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002906 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08002907 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
2908 if (prop32)
2909 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10002910
2911 /* Parse 64-bit MMIO range */
2912 pnv_ioda_parse_m64_window(phb);
2913
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002914 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00002915 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002916 phb->ioda.m32_size += 0x10000;
2917
2918 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002919 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002920 phb->ioda.io_size = hose->pci_io_size;
2921 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
2922 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
2923
Gavin Shanc35d2a82013-07-31 16:47:04 +08002924 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002925 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
2926 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00002927 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08002928 if (phb->type == PNV_PHB_IODA1) {
2929 iomap_off = size;
2930 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
2931 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002932 pemap_off = size;
2933 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11002934 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002935 phb->ioda.pe_alloc = aux;
2936 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08002937 if (phb->type == PNV_PHB_IODA1)
2938 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002939 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08002940 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002941
Gavin Shan7ebdf952012-08-20 03:49:15 +00002942 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002943 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08002944 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002945
2946 /* Calculate how many 32-bit TCE segments we have */
2947 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
2948
Gavin Shanaa0c0332013-04-25 19:20:57 +00002949#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002950 rc = opal_pci_set_phb_mem_window(opal->phb_id,
2951 window_type,
2952 window_num,
2953 starting_real_address,
2954 starting_pci_address,
2955 segment_size);
2956#endif
2957
Guo Chao262af552014-07-21 14:42:30 +10002958 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
2959 phb->ioda.total_pe, phb->ioda.reserved_pe,
2960 phb->ioda.m32_size, phb->ioda.m32_segsize);
2961 if (phb->ioda.m64_size)
2962 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
2963 phb->ioda.m64_size, phb->ioda.m64_segsize);
2964 if (phb->ioda.io_size)
2965 pr_info(" IO: 0x%x [segment=0x%x]\n",
2966 phb->ioda.io_size, phb->ioda.io_segsize);
2967
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002968
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002969 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10002970 phb->get_pe_state = pnv_ioda_get_pe_state;
2971 phb->freeze_pe = pnv_ioda_freeze_pe;
2972 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002973
2974 /* Setup RID -> PE mapping function */
2975 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
2976
2977 /* Setup TCEs */
2978 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10002979 phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002980
2981 /* Setup MSI support */
2982 pnv_pci_init_ioda_msis(phb);
2983
Gavin Shanc40a4212012-08-20 03:49:20 +00002984 /*
2985 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
2986 * to let the PCI core do resource assignment. It's supposed
2987 * that the PCI core will do correct I/O and MMIO alignment
2988 * for the P2P bridge bars so that each PCI bus (excluding
2989 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002990 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00002991 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Daniel Axtens92ae0352015-04-28 15:12:05 +10002992 hose->controller_ops = pnv_pci_ioda_controller_ops;
Michael Ellermanad30cb92015-04-14 09:29:23 +10002993
Wei Yang6e628c72015-03-25 16:23:55 +08002994#ifdef CONFIG_PCI_IOV
2995 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08002996 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10002997#endif
2998
Gavin Shanc40a4212012-08-20 03:49:20 +00002999 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003000
3001 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003002 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003003 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003004 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003005
3006 /* If we're running in kdump kerenl, the previous kerenl never
3007 * shutdown PCI devices correctly. We already got IODA table
3008 * cleaned out. So we have to issue PHB reset to stop all PCI
3009 * transactions from previous kerenl.
3010 */
3011 if (is_kdump_kernel()) {
3012 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003013 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3014 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003015 }
Guo Chao262af552014-07-21 14:42:30 +10003016
Gavin Shan9e9e8932014-11-12 13:36:05 +11003017 /* Remove M64 resource if we can't configure it successfully */
3018 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003019 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003020}
3021
Bjorn Helgaas67975002013-07-02 12:20:03 -06003022void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003023{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003024 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003025}
3026
3027void __init pnv_pci_init_ioda_hub(struct device_node *np)
3028{
3029 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003030 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003031 u64 hub_id;
3032
3033 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3034
3035 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3036 if (!prop64) {
3037 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3038 return;
3039 }
3040 hub_id = be64_to_cpup(prop64);
3041 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3042
3043 /* Count child PHBs */
3044 for_each_child_of_node(np, phbn) {
3045 /* Look for IODA1 PHBs */
3046 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003047 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003048 }
3049}