]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/powerpc/platforms/powernv/pci-ioda.c
powerpc/iommu/vfio_spapr_tce: Cleanup iommu_table disposal
[mirror_ubuntu-zesty-kernel.git] / arch / powerpc / platforms / powernv / pci-ioda.c
CommitLineData
184cd4a3
BH
1/*
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
cee72d5b 12#undef DEBUG
184cd4a3
BH
13
14#include <linux/kernel.h>
15#include <linux/pci.h>
361f2a2a 16#include <linux/crash_dump.h>
37c367f2 17#include <linux/debugfs.h>
184cd4a3
BH
18#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>
cd15b048 25#include <linux/memblock.h>
ac9a5889 26#include <linux/iommu.h>
e57080f1 27#include <linux/rculist.h>
4793d65d 28#include <linux/sizes.h>
184cd4a3
BH
29
30#include <asm/sections.h>
31#include <asm/io.h>
32#include <asm/prom.h>
33#include <asm/pci-bridge.h>
34#include <asm/machdep.h>
fb1b55d6 35#include <asm/msi_bitmap.h>
184cd4a3
BH
36#include <asm/ppc-pci.h>
37#include <asm/opal.h>
38#include <asm/iommu.h>
39#include <asm/tce.h>
137436c9 40#include <asm/xics.h>
37c367f2 41#include <asm/debug.h>
262af557 42#include <asm/firmware.h>
80c49c7e 43#include <asm/pnv-pci.h>
aca6913f 44#include <asm/mmzone.h>
80c49c7e 45
ec249dd8 46#include <misc/cxl-base.h>
184cd4a3
BH
47
48#include "powernv.h"
49#include "pci.h"
50
99451551
GS
51#define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */
52#define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */
acce971c 53#define PNV_IODA1_DMA32_SEGSIZE 0x10000000
781a868f 54
bbb845c4
AK
55#define POWERNV_IOMMU_DEFAULT_LEVELS 1
56#define POWERNV_IOMMU_MAX_LEVELS 5
57
9497a1c1 58static const char * const pnv_phb_names[] = { "IODA1", "IODA2", "NPU" };
aca6913f
AK
59static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
60
7d623e42 61void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
6d31c2fa
JP
62 const char *fmt, ...)
63{
64 struct va_format vaf;
65 va_list args;
66 char pfix[32];
67
68 va_start(args, fmt);
69
70 vaf.fmt = fmt;
71 vaf.va = &args;
72
781a868f 73 if (pe->flags & PNV_IODA_PE_DEV)
6d31c2fa 74 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
781a868f 75 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
6d31c2fa
JP
76 sprintf(pfix, "%04x:%02x ",
77 pci_domain_nr(pe->pbus), pe->pbus->number);
781a868f
WY
78#ifdef CONFIG_PCI_IOV
79 else if (pe->flags & PNV_IODA_PE_VF)
80 sprintf(pfix, "%04x:%02x:%2x.%d",
81 pci_domain_nr(pe->parent_dev->bus),
82 (pe->rid & 0xff00) >> 8,
83 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
84#endif /* CONFIG_PCI_IOV*/
6d31c2fa 85
1f52f176 86 printk("%spci %s: [PE# %.2x] %pV",
6d31c2fa
JP
87 level, pfix, pe->pe_number, &vaf);
88
89 va_end(args);
90}
184cd4a3 91
4e287840
TLSC
92static bool pnv_iommu_bypass_disabled __read_mostly;
93
94static int __init iommu_setup(char *str)
95{
96 if (!str)
97 return -EINVAL;
98
99 while (*str) {
100 if (!strncmp(str, "nobypass", 8)) {
101 pnv_iommu_bypass_disabled = true;
102 pr_info("PowerNV: IOMMU bypass window disabled.\n");
103 break;
104 }
105 str += strcspn(str, ",");
106 if (*str == ',')
107 str++;
108 }
109
110 return 0;
111}
112early_param("iommu", iommu_setup);
113
5958d19a 114static inline bool pnv_pci_is_m64(struct pnv_phb *phb, struct resource *r)
262af557 115{
5958d19a
BH
116 /*
117 * WARNING: We cannot rely on the resource flags. The Linux PCI
118 * allocation code sometimes decides to put a 64-bit prefetchable
119 * BAR in the 32-bit window, so we have to compare the addresses.
120 *
121 * For simplicity we only test resource start.
122 */
123 return (r->start >= phb->ioda.m64_base &&
124 r->start < (phb->ioda.m64_base + phb->ioda.m64_size));
262af557
GC
125}
126
b79331a5
RC
127static inline bool pnv_pci_is_m64_flags(unsigned long resource_flags)
128{
129 unsigned long flags = (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
130
131 return (resource_flags & flags) == flags;
132}
133
1e916772
GS
134static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
135{
313483dd
GS
136 s64 rc;
137
1e916772
GS
138 phb->ioda.pe_array[pe_no].phb = phb;
139 phb->ioda.pe_array[pe_no].pe_number = pe_no;
140
313483dd
GS
141 /*
142 * Clear the PE frozen state as it might be put into frozen state
143 * in the last PCI remove path. It's not harmful to do so when the
144 * PE is already in unfrozen state.
145 */
146 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
147 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
d4791db5 148 if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
1f52f176 149 pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n",
313483dd
GS
150 __func__, rc, phb->hose->global_number, pe_no);
151
1e916772
GS
152 return &phb->ioda.pe_array[pe_no];
153}
154
4b82ab18
GS
155static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
156{
92b8f137 157 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
1f52f176 158 pr_warn("%s: Invalid PE %x on PHB#%x\n",
4b82ab18
GS
159 __func__, pe_no, phb->hose->global_number);
160 return;
161 }
162
e9dc4d7f 163 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
1f52f176 164 pr_debug("%s: PE %x was reserved on PHB#%x\n",
e9dc4d7f 165 __func__, pe_no, phb->hose->global_number);
4b82ab18 166
1e916772 167 pnv_ioda_init_pe(phb, pe_no);
4b82ab18
GS
168}
169
1e916772 170static struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb)
184cd4a3 171{
60964816 172 long pe;
184cd4a3 173
9fcd6f4a
GS
174 for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
175 if (!test_and_set_bit(pe, phb->ioda.pe_alloc))
176 return pnv_ioda_init_pe(phb, pe);
177 }
184cd4a3 178
9fcd6f4a 179 return NULL;
184cd4a3
BH
180}
181
1e916772 182static void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
184cd4a3 183{
1e916772 184 struct pnv_phb *phb = pe->phb;
caa58f80 185 unsigned int pe_num = pe->pe_number;
1e916772
GS
186
187 WARN_ON(pe->pdev);
184cd4a3 188
1e916772 189 memset(pe, 0, sizeof(struct pnv_ioda_pe));
caa58f80 190 clear_bit(pe_num, phb->ioda.pe_alloc);
184cd4a3
BH
191}
192
262af557
GC
193/* The default M64 BAR is shared by all PEs */
194static int pnv_ioda2_init_m64(struct pnv_phb *phb)
195{
196 const char *desc;
197 struct resource *r;
198 s64 rc;
199
200 /* Configure the default M64 BAR */
201 rc = opal_pci_set_phb_mem_window(phb->opal_id,
202 OPAL_M64_WINDOW_TYPE,
203 phb->ioda.m64_bar_idx,
204 phb->ioda.m64_base,
205 0, /* unused */
206 phb->ioda.m64_size);
207 if (rc != OPAL_SUCCESS) {
208 desc = "configuring";
209 goto fail;
210 }
211
212 /* Enable the default M64 BAR */
213 rc = opal_pci_phb_mmio_enable(phb->opal_id,
214 OPAL_M64_WINDOW_TYPE,
215 phb->ioda.m64_bar_idx,
216 OPAL_ENABLE_M64_SPLIT);
217 if (rc != OPAL_SUCCESS) {
218 desc = "enabling";
219 goto fail;
220 }
221
262af557 222 /*
63803c39
GS
223 * Exclude the segments for reserved and root bus PE, which
224 * are first or last two PEs.
262af557
GC
225 */
226 r = &phb->hose->mem_resources[1];
92b8f137 227 if (phb->ioda.reserved_pe_idx == 0)
63803c39 228 r->start += (2 * phb->ioda.m64_segsize);
92b8f137 229 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
63803c39 230 r->end -= (2 * phb->ioda.m64_segsize);
262af557 231 else
1f52f176 232 pr_warn(" Cannot strip M64 segment for reserved PE#%x\n",
92b8f137 233 phb->ioda.reserved_pe_idx);
262af557
GC
234
235 return 0;
236
237fail:
238 pr_warn(" Failure %lld %s M64 BAR#%d\n",
239 rc, desc, phb->ioda.m64_bar_idx);
240 opal_pci_phb_mmio_enable(phb->opal_id,
241 OPAL_M64_WINDOW_TYPE,
242 phb->ioda.m64_bar_idx,
243 OPAL_DISABLE_M64);
244 return -EIO;
245}
246
c430670a 247static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
96a2f92b 248 unsigned long *pe_bitmap)
262af557 249{
96a2f92b
GS
250 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
251 struct pnv_phb *phb = hose->private_data;
262af557 252 struct resource *r;
96a2f92b
GS
253 resource_size_t base, sgsz, start, end;
254 int segno, i;
255
256 base = phb->ioda.m64_base;
257 sgsz = phb->ioda.m64_segsize;
258 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
259 r = &pdev->resource[i];
5958d19a 260 if (!r->parent || !pnv_pci_is_m64(phb, r))
96a2f92b 261 continue;
262af557 262
96a2f92b
GS
263 start = _ALIGN_DOWN(r->start - base, sgsz);
264 end = _ALIGN_UP(r->end - base, sgsz);
265 for (segno = start / sgsz; segno < end / sgsz; segno++) {
266 if (pe_bitmap)
267 set_bit(segno, pe_bitmap);
268 else
269 pnv_ioda_reserve_pe(phb, segno);
262af557
GC
270 }
271 }
272}
273
99451551
GS
274static int pnv_ioda1_init_m64(struct pnv_phb *phb)
275{
276 struct resource *r;
277 int index;
278
279 /*
280 * There are 16 M64 BARs, each of which has 8 segments. So
281 * there are as many M64 segments as the maximum number of
282 * PEs, which is 128.
283 */
284 for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
285 unsigned long base, segsz = phb->ioda.m64_segsize;
286 int64_t rc;
287
288 base = phb->ioda.m64_base +
289 index * PNV_IODA1_M64_SEGS * segsz;
290 rc = opal_pci_set_phb_mem_window(phb->opal_id,
291 OPAL_M64_WINDOW_TYPE, index, base, 0,
292 PNV_IODA1_M64_SEGS * segsz);
293 if (rc != OPAL_SUCCESS) {
1f52f176 294 pr_warn(" Error %lld setting M64 PHB#%x-BAR#%d\n",
99451551
GS
295 rc, phb->hose->global_number, index);
296 goto fail;
297 }
298
299 rc = opal_pci_phb_mmio_enable(phb->opal_id,
300 OPAL_M64_WINDOW_TYPE, index,
301 OPAL_ENABLE_M64_SPLIT);
302 if (rc != OPAL_SUCCESS) {
1f52f176 303 pr_warn(" Error %lld enabling M64 PHB#%x-BAR#%d\n",
99451551
GS
304 rc, phb->hose->global_number, index);
305 goto fail;
306 }
307 }
308
309 /*
63803c39
GS
310 * Exclude the segments for reserved and root bus PE, which
311 * are first or last two PEs.
99451551
GS
312 */
313 r = &phb->hose->mem_resources[1];
314 if (phb->ioda.reserved_pe_idx == 0)
63803c39 315 r->start += (2 * phb->ioda.m64_segsize);
99451551 316 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
63803c39 317 r->end -= (2 * phb->ioda.m64_segsize);
99451551 318 else
1f52f176 319 WARN(1, "Wrong reserved PE#%x on PHB#%x\n",
99451551
GS
320 phb->ioda.reserved_pe_idx, phb->hose->global_number);
321
322 return 0;
323
324fail:
325 for ( ; index >= 0; index--)
326 opal_pci_phb_mmio_enable(phb->opal_id,
327 OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
328
329 return -EIO;
330}
331
c430670a
GS
332static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
333 unsigned long *pe_bitmap,
334 bool all)
262af557 335{
262af557 336 struct pci_dev *pdev;
96a2f92b
GS
337
338 list_for_each_entry(pdev, &bus->devices, bus_list) {
c430670a 339 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
96a2f92b
GS
340
341 if (all && pdev->subordinate)
c430670a
GS
342 pnv_ioda_reserve_m64_pe(pdev->subordinate,
343 pe_bitmap, all);
96a2f92b
GS
344 }
345}
346
1e916772 347static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
262af557 348{
26ba248d
GS
349 struct pci_controller *hose = pci_bus_to_host(bus);
350 struct pnv_phb *phb = hose->private_data;
262af557
GC
351 struct pnv_ioda_pe *master_pe, *pe;
352 unsigned long size, *pe_alloc;
26ba248d 353 int i;
262af557
GC
354
355 /* Root bus shouldn't use M64 */
356 if (pci_is_root_bus(bus))
1e916772 357 return NULL;
262af557 358
262af557 359 /* Allocate bitmap */
92b8f137 360 size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
262af557
GC
361 pe_alloc = kzalloc(size, GFP_KERNEL);
362 if (!pe_alloc) {
363 pr_warn("%s: Out of memory !\n",
364 __func__);
1e916772 365 return NULL;
262af557
GC
366 }
367
26ba248d 368 /* Figure out reserved PE numbers by the PE */
c430670a 369 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
262af557
GC
370
371 /*
372 * the current bus might not own M64 window and that's all
373 * contributed by its child buses. For the case, we needn't
374 * pick M64 dependent PE#.
375 */
92b8f137 376 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
262af557 377 kfree(pe_alloc);
1e916772 378 return NULL;
262af557
GC
379 }
380
381 /*
382 * Figure out the master PE and put all slave PEs to master
383 * PE's list to form compound PE.
384 */
262af557
GC
385 master_pe = NULL;
386 i = -1;
92b8f137
GS
387 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
388 phb->ioda.total_pe_num) {
262af557 389 pe = &phb->ioda.pe_array[i];
262af557 390
93289d8c 391 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
262af557
GC
392 if (!master_pe) {
393 pe->flags |= PNV_IODA_PE_MASTER;
394 INIT_LIST_HEAD(&pe->slaves);
395 master_pe = pe;
396 } else {
397 pe->flags |= PNV_IODA_PE_SLAVE;
398 pe->master = master_pe;
399 list_add_tail(&pe->list, &master_pe->slaves);
400 }
99451551
GS
401
402 /*
403 * P7IOC supports M64DT, which helps mapping M64 segment
404 * to one particular PE#. However, PHB3 has fixed mapping
405 * between M64 segment and PE#. In order to have same logic
406 * for P7IOC and PHB3, we enforce fixed mapping between M64
407 * segment and PE# on P7IOC.
408 */
409 if (phb->type == PNV_PHB_IODA1) {
410 int64_t rc;
411
412 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
413 pe->pe_number, OPAL_M64_WINDOW_TYPE,
414 pe->pe_number / PNV_IODA1_M64_SEGS,
415 pe->pe_number % PNV_IODA1_M64_SEGS);
416 if (rc != OPAL_SUCCESS)
1f52f176 417 pr_warn("%s: Error %lld mapping M64 for PHB#%x-PE#%x\n",
99451551
GS
418 __func__, rc, phb->hose->global_number,
419 pe->pe_number);
420 }
262af557
GC
421 }
422
423 kfree(pe_alloc);
1e916772 424 return master_pe;
262af557
GC
425}
426
427static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
428{
429 struct pci_controller *hose = phb->hose;
430 struct device_node *dn = hose->dn;
431 struct resource *res;
a1339faf 432 u32 m64_range[2], i;
0e7736c6 433 const __be32 *r;
262af557
GC
434 u64 pci_addr;
435
99451551 436 if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
1665c4a8
GS
437 pr_info(" Not support M64 window\n");
438 return;
439 }
440
e4d54f71 441 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
262af557
GC
442 pr_info(" Firmware too old to support M64 window\n");
443 return;
444 }
445
446 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
447 if (!r) {
448 pr_info(" No <ibm,opal-m64-window> on %s\n",
449 dn->full_name);
450 return;
451 }
452
a1339faf
BH
453 /*
454 * Find the available M64 BAR range and pickup the last one for
455 * covering the whole 64-bits space. We support only one range.
456 */
457 if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges",
458 m64_range, 2)) {
459 /* In absence of the property, assume 0..15 */
460 m64_range[0] = 0;
461 m64_range[1] = 16;
462 }
463 /* We only support 64 bits in our allocator */
464 if (m64_range[1] > 63) {
465 pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",
466 __func__, m64_range[1], phb->hose->global_number);
467 m64_range[1] = 63;
468 }
469 /* Empty range, no m64 */
470 if (m64_range[1] <= m64_range[0]) {
471 pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",
472 __func__, phb->hose->global_number);
473 return;
474 }
475
476 /* Configure M64 informations */
262af557 477 res = &hose->mem_resources[1];
e80c4e7c 478 res->name = dn->full_name;
262af557
GC
479 res->start = of_translate_address(dn, r + 2);
480 res->end = res->start + of_read_number(r + 4, 2) - 1;
481 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
482 pci_addr = of_read_number(r, 2);
483 hose->mem_offset[1] = res->start - pci_addr;
484
485 phb->ioda.m64_size = resource_size(res);
92b8f137 486 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
262af557
GC
487 phb->ioda.m64_base = pci_addr;
488
a1339faf
BH
489 /* This lines up nicely with the display from processing OF ranges */
490 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",
491 res->start, res->end, pci_addr, m64_range[0],
492 m64_range[0] + m64_range[1] - 1);
493
494 /* Mark all M64 used up by default */
495 phb->ioda.m64_bar_alloc = (unsigned long)-1;
e9863e68 496
262af557 497 /* Use last M64 BAR to cover M64 window */
a1339faf
BH
498 m64_range[1]--;
499 phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1];
500
501 pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx);
502
503 /* Mark remaining ones free */
504 for (i = m64_range[0]; i < m64_range[1]; i++)
505 clear_bit(i, &phb->ioda.m64_bar_alloc);
506
507 /*
508 * Setup init functions for M64 based on IODA version, IODA3 uses
509 * the IODA2 code.
510 */
99451551
GS
511 if (phb->type == PNV_PHB_IODA1)
512 phb->init_m64 = pnv_ioda1_init_m64;
513 else
514 phb->init_m64 = pnv_ioda2_init_m64;
c430670a
GS
515 phb->reserve_m64_pe = pnv_ioda_reserve_m64_pe;
516 phb->pick_m64_pe = pnv_ioda_pick_m64_pe;
262af557
GC
517}
518
49dec922
GS
519static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
520{
521 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
522 struct pnv_ioda_pe *slave;
523 s64 rc;
524
525 /* Fetch master PE */
526 if (pe->flags & PNV_IODA_PE_SLAVE) {
527 pe = pe->master;
ec8e4e9d
GS
528 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
529 return;
530
49dec922
GS
531 pe_no = pe->pe_number;
532 }
533
534 /* Freeze master PE */
535 rc = opal_pci_eeh_freeze_set(phb->opal_id,
536 pe_no,
537 OPAL_EEH_ACTION_SET_FREEZE_ALL);
538 if (rc != OPAL_SUCCESS) {
539 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
540 __func__, rc, phb->hose->global_number, pe_no);
541 return;
542 }
543
544 /* Freeze slave PEs */
545 if (!(pe->flags & PNV_IODA_PE_MASTER))
546 return;
547
548 list_for_each_entry(slave, &pe->slaves, list) {
549 rc = opal_pci_eeh_freeze_set(phb->opal_id,
550 slave->pe_number,
551 OPAL_EEH_ACTION_SET_FREEZE_ALL);
552 if (rc != OPAL_SUCCESS)
553 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
554 __func__, rc, phb->hose->global_number,
555 slave->pe_number);
556 }
557}
558
e51df2c1 559static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
49dec922
GS
560{
561 struct pnv_ioda_pe *pe, *slave;
562 s64 rc;
563
564 /* Find master PE */
565 pe = &phb->ioda.pe_array[pe_no];
566 if (pe->flags & PNV_IODA_PE_SLAVE) {
567 pe = pe->master;
568 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
569 pe_no = pe->pe_number;
570 }
571
572 /* Clear frozen state for master PE */
573 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
574 if (rc != OPAL_SUCCESS) {
575 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
576 __func__, rc, opt, phb->hose->global_number, pe_no);
577 return -EIO;
578 }
579
580 if (!(pe->flags & PNV_IODA_PE_MASTER))
581 return 0;
582
583 /* Clear frozen state for slave PEs */
584 list_for_each_entry(slave, &pe->slaves, list) {
585 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
586 slave->pe_number,
587 opt);
588 if (rc != OPAL_SUCCESS) {
589 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
590 __func__, rc, opt, phb->hose->global_number,
591 slave->pe_number);
592 return -EIO;
593 }
594 }
595
596 return 0;
597}
598
599static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
600{
601 struct pnv_ioda_pe *slave, *pe;
602 u8 fstate, state;
603 __be16 pcierr;
604 s64 rc;
605
606 /* Sanity check on PE number */
92b8f137 607 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
49dec922
GS
608 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
609
610 /*
611 * Fetch the master PE and the PE instance might be
612 * not initialized yet.
613 */
614 pe = &phb->ioda.pe_array[pe_no];
615 if (pe->flags & PNV_IODA_PE_SLAVE) {
616 pe = pe->master;
617 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
618 pe_no = pe->pe_number;
619 }
620
621 /* Check the master PE */
622 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
623 &state, &pcierr, NULL);
624 if (rc != OPAL_SUCCESS) {
625 pr_warn("%s: Failure %lld getting "
626 "PHB#%x-PE#%x state\n",
627 __func__, rc,
628 phb->hose->global_number, pe_no);
629 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
630 }
631
632 /* Check the slave PE */
633 if (!(pe->flags & PNV_IODA_PE_MASTER))
634 return state;
635
636 list_for_each_entry(slave, &pe->slaves, list) {
637 rc = opal_pci_eeh_freeze_status(phb->opal_id,
638 slave->pe_number,
639 &fstate,
640 &pcierr,
641 NULL);
642 if (rc != OPAL_SUCCESS) {
643 pr_warn("%s: Failure %lld getting "
644 "PHB#%x-PE#%x state\n",
645 __func__, rc,
646 phb->hose->global_number, slave->pe_number);
647 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
648 }
649
650 /*
651 * Override the result based on the ascending
652 * priority.
653 */
654 if (fstate > state)
655 state = fstate;
656 }
657
658 return state;
659}
660
184cd4a3
BH
661/* Currently those 2 are only used when MSIs are enabled, this will change
662 * but in the meantime, we need to protect them to avoid warnings
663 */
664#ifdef CONFIG_PCI_MSI
f456834a 665struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
184cd4a3
BH
666{
667 struct pci_controller *hose = pci_bus_to_host(dev->bus);
668 struct pnv_phb *phb = hose->private_data;
b72c1f65 669 struct pci_dn *pdn = pci_get_pdn(dev);
184cd4a3
BH
670
671 if (!pdn)
672 return NULL;
673 if (pdn->pe_number == IODA_INVALID_PE)
674 return NULL;
675 return &phb->ioda.pe_array[pdn->pe_number];
676}
184cd4a3
BH
677#endif /* CONFIG_PCI_MSI */
678
b131a842
GS
679static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
680 struct pnv_ioda_pe *parent,
681 struct pnv_ioda_pe *child,
682 bool is_add)
683{
684 const char *desc = is_add ? "adding" : "removing";
685 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
686 OPAL_REMOVE_PE_FROM_DOMAIN;
687 struct pnv_ioda_pe *slave;
688 long rc;
689
690 /* Parent PE affects child PE */
691 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
692 child->pe_number, op);
693 if (rc != OPAL_SUCCESS) {
694 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
695 rc, desc);
696 return -ENXIO;
697 }
698
699 if (!(child->flags & PNV_IODA_PE_MASTER))
700 return 0;
701
702 /* Compound case: parent PE affects slave PEs */
703 list_for_each_entry(slave, &child->slaves, list) {
704 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
705 slave->pe_number, op);
706 if (rc != OPAL_SUCCESS) {
707 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
708 rc, desc);
709 return -ENXIO;
710 }
711 }
712
713 return 0;
714}
715
716static int pnv_ioda_set_peltv(struct pnv_phb *phb,
717 struct pnv_ioda_pe *pe,
718 bool is_add)
719{
720 struct pnv_ioda_pe *slave;
781a868f 721 struct pci_dev *pdev = NULL;
b131a842
GS
722 int ret;
723
724 /*
725 * Clear PE frozen state. If it's master PE, we need
726 * clear slave PE frozen state as well.
727 */
728 if (is_add) {
729 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
730 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
731 if (pe->flags & PNV_IODA_PE_MASTER) {
732 list_for_each_entry(slave, &pe->slaves, list)
733 opal_pci_eeh_freeze_clear(phb->opal_id,
734 slave->pe_number,
735 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
736 }
737 }
738
739 /*
740 * Associate PE in PELT. We need add the PE into the
741 * corresponding PELT-V as well. Otherwise, the error
742 * originated from the PE might contribute to other
743 * PEs.
744 */
745 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
746 if (ret)
747 return ret;
748
749 /* For compound PEs, any one affects all of them */
750 if (pe->flags & PNV_IODA_PE_MASTER) {
751 list_for_each_entry(slave, &pe->slaves, list) {
752 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
753 if (ret)
754 return ret;
755 }
756 }
757
758 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
759 pdev = pe->pbus->self;
781a868f 760 else if (pe->flags & PNV_IODA_PE_DEV)
b131a842 761 pdev = pe->pdev->bus->self;
781a868f
WY
762#ifdef CONFIG_PCI_IOV
763 else if (pe->flags & PNV_IODA_PE_VF)
283e2d8a 764 pdev = pe->parent_dev;
781a868f 765#endif /* CONFIG_PCI_IOV */
b131a842
GS
766 while (pdev) {
767 struct pci_dn *pdn = pci_get_pdn(pdev);
768 struct pnv_ioda_pe *parent;
769
770 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
771 parent = &phb->ioda.pe_array[pdn->pe_number];
772 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
773 if (ret)
774 return ret;
775 }
776
777 pdev = pdev->bus->self;
778 }
779
780 return 0;
781}
782
781a868f
WY
783static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
784{
785 struct pci_dev *parent;
786 uint8_t bcomp, dcomp, fcomp;
787 int64_t rc;
788 long rid_end, rid;
789
790 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
791 if (pe->pbus) {
792 int count;
793
794 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
795 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
796 parent = pe->pbus->self;
797 if (pe->flags & PNV_IODA_PE_BUS_ALL)
798 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
799 else
800 count = 1;
801
802 switch(count) {
803 case 1: bcomp = OpalPciBusAll; break;
804 case 2: bcomp = OpalPciBus7Bits; break;
805 case 4: bcomp = OpalPciBus6Bits; break;
806 case 8: bcomp = OpalPciBus5Bits; break;
807 case 16: bcomp = OpalPciBus4Bits; break;
808 case 32: bcomp = OpalPciBus3Bits; break;
809 default:
810 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
811 count);
812 /* Do an exact match only */
813 bcomp = OpalPciBusAll;
814 }
815 rid_end = pe->rid + (count << 8);
816 } else {
93e01a50 817#ifdef CONFIG_PCI_IOV
781a868f
WY
818 if (pe->flags & PNV_IODA_PE_VF)
819 parent = pe->parent_dev;
820 else
93e01a50 821#endif
781a868f
WY
822 parent = pe->pdev->bus->self;
823 bcomp = OpalPciBusAll;
824 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
825 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
826 rid_end = pe->rid + 1;
827 }
828
829 /* Clear the reverse map */
830 for (rid = pe->rid; rid < rid_end; rid++)
c127562a 831 phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
781a868f
WY
832
833 /* Release from all parents PELT-V */
834 while (parent) {
835 struct pci_dn *pdn = pci_get_pdn(parent);
836 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
837 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
838 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
839 /* XXX What to do in case of error ? */
840 }
841 parent = parent->bus->self;
842 }
843
f951e510 844 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
781a868f
WY
845 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
846
847 /* Disassociate PE in PELT */
848 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
849 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
850 if (rc)
851 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
852 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
853 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
854 if (rc)
855 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
856
857 pe->pbus = NULL;
858 pe->pdev = NULL;
93e01a50 859#ifdef CONFIG_PCI_IOV
781a868f 860 pe->parent_dev = NULL;
93e01a50 861#endif
781a868f
WY
862
863 return 0;
864}
781a868f 865
cad5cef6 866static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
184cd4a3
BH
867{
868 struct pci_dev *parent;
869 uint8_t bcomp, dcomp, fcomp;
870 long rc, rid_end, rid;
871
872 /* Bus validation ? */
873 if (pe->pbus) {
874 int count;
875
876 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
877 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
878 parent = pe->pbus->self;
fb446ad0
GS
879 if (pe->flags & PNV_IODA_PE_BUS_ALL)
880 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
881 else
882 count = 1;
883
184cd4a3
BH
884 switch(count) {
885 case 1: bcomp = OpalPciBusAll; break;
886 case 2: bcomp = OpalPciBus7Bits; break;
887 case 4: bcomp = OpalPciBus6Bits; break;
888 case 8: bcomp = OpalPciBus5Bits; break;
889 case 16: bcomp = OpalPciBus4Bits; break;
890 case 32: bcomp = OpalPciBus3Bits; break;
891 default:
781a868f
WY
892 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
893 count);
184cd4a3
BH
894 /* Do an exact match only */
895 bcomp = OpalPciBusAll;
896 }
897 rid_end = pe->rid + (count << 8);
898 } else {
781a868f
WY
899#ifdef CONFIG_PCI_IOV
900 if (pe->flags & PNV_IODA_PE_VF)
901 parent = pe->parent_dev;
902 else
903#endif /* CONFIG_PCI_IOV */
904 parent = pe->pdev->bus->self;
184cd4a3
BH
905 bcomp = OpalPciBusAll;
906 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
907 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
908 rid_end = pe->rid + 1;
909 }
910
631ad691
GS
911 /*
912 * Associate PE in PELT. We need add the PE into the
913 * corresponding PELT-V as well. Otherwise, the error
914 * originated from the PE might contribute to other
915 * PEs.
916 */
184cd4a3
BH
917 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
918 bcomp, dcomp, fcomp, OPAL_MAP_PE);
919 if (rc) {
920 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
921 return -ENXIO;
922 }
631ad691 923
5d2aa710
AP
924 /*
925 * Configure PELTV. NPUs don't have a PELTV table so skip
926 * configuration on them.
927 */
928 if (phb->type != PNV_PHB_NPU)
929 pnv_ioda_set_peltv(phb, pe, true);
184cd4a3 930
184cd4a3
BH
931 /* Setup reverse map */
932 for (rid = pe->rid; rid < rid_end; rid++)
933 phb->ioda.pe_rmap[rid] = pe->pe_number;
934
935 /* Setup one MVTs on IODA1 */
4773f76b
GS
936 if (phb->type != PNV_PHB_IODA1) {
937 pe->mve_number = 0;
938 goto out;
939 }
940
941 pe->mve_number = pe->pe_number;
942 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
943 if (rc != OPAL_SUCCESS) {
1f52f176 944 pe_err(pe, "OPAL error %ld setting up MVE %x\n",
4773f76b
GS
945 rc, pe->mve_number);
946 pe->mve_number = -1;
947 } else {
948 rc = opal_pci_set_mve_enable(phb->opal_id,
949 pe->mve_number, OPAL_ENABLE_MVE);
184cd4a3 950 if (rc) {
1f52f176 951 pe_err(pe, "OPAL error %ld enabling MVE %x\n",
184cd4a3
BH
952 rc, pe->mve_number);
953 pe->mve_number = -1;
184cd4a3 954 }
4773f76b 955 }
184cd4a3 956
4773f76b 957out:
184cd4a3
BH
958 return 0;
959}
960
781a868f
WY
961#ifdef CONFIG_PCI_IOV
962static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
963{
964 struct pci_dn *pdn = pci_get_pdn(dev);
965 int i;
966 struct resource *res, res2;
967 resource_size_t size;
968 u16 num_vfs;
969
970 if (!dev->is_physfn)
971 return -EINVAL;
972
973 /*
974 * "offset" is in VFs. The M64 windows are sized so that when they
975 * are segmented, each segment is the same size as the IOV BAR.
976 * Each segment is in a separate PE, and the high order bits of the
977 * address are the PE number. Therefore, each VF's BAR is in a
978 * separate PE, and changing the IOV BAR start address changes the
979 * range of PEs the VFs are in.
980 */
981 num_vfs = pdn->num_vfs;
982 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
983 res = &dev->resource[i + PCI_IOV_RESOURCES];
984 if (!res->flags || !res->parent)
985 continue;
986
781a868f
WY
987 /*
988 * The actual IOV BAR range is determined by the start address
989 * and the actual size for num_vfs VFs BAR. This check is to
990 * make sure that after shifting, the range will not overlap
991 * with another device.
992 */
993 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
994 res2.flags = res->flags;
995 res2.start = res->start + (size * offset);
996 res2.end = res2.start + (size * num_vfs) - 1;
997
998 if (res2.end > res->end) {
999 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
1000 i, &res2, res, num_vfs, offset);
1001 return -EBUSY;
1002 }
1003 }
1004
1005 /*
1006 * After doing so, there would be a "hole" in the /proc/iomem when
1007 * offset is a positive value. It looks like the device return some
1008 * mmio back to the system, which actually no one could use it.
1009 */
1010 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1011 res = &dev->resource[i + PCI_IOV_RESOURCES];
1012 if (!res->flags || !res->parent)
1013 continue;
1014
781a868f
WY
1015 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
1016 res2 = *res;
1017 res->start += size * offset;
1018
74703cc4
WY
1019 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
1020 i, &res2, res, (offset > 0) ? "En" : "Dis",
1021 num_vfs, offset);
781a868f
WY
1022 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
1023 }
1024 return 0;
1025}
1026#endif /* CONFIG_PCI_IOV */
1027
cad5cef6 1028static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
184cd4a3
BH
1029{
1030 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1031 struct pnv_phb *phb = hose->private_data;
b72c1f65 1032 struct pci_dn *pdn = pci_get_pdn(dev);
184cd4a3 1033 struct pnv_ioda_pe *pe;
184cd4a3
BH
1034
1035 if (!pdn) {
1036 pr_err("%s: Device tree node not associated properly\n",
1037 pci_name(dev));
1038 return NULL;
1039 }
1040 if (pdn->pe_number != IODA_INVALID_PE)
1041 return NULL;
1042
1e916772
GS
1043 pe = pnv_ioda_alloc_pe(phb);
1044 if (!pe) {
184cd4a3
BH
1045 pr_warning("%s: Not enough PE# available, disabling device\n",
1046 pci_name(dev));
1047 return NULL;
1048 }
1049
1050 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
1051 * pointer in the PE data structure, both should be destroyed at the
1052 * same time. However, this needs to be looked at more closely again
1053 * once we actually start removing things (Hotplug, SR-IOV, ...)
1054 *
1055 * At some point we want to remove the PDN completely anyways
1056 */
184cd4a3
BH
1057 pci_dev_get(dev);
1058 pdn->pcidev = dev;
1e916772 1059 pdn->pe_number = pe->pe_number;
5d2aa710 1060 pe->flags = PNV_IODA_PE_DEV;
184cd4a3
BH
1061 pe->pdev = dev;
1062 pe->pbus = NULL;
184cd4a3
BH
1063 pe->mve_number = -1;
1064 pe->rid = dev->bus->number << 8 | pdn->devfn;
1065
1066 pe_info(pe, "Associated device to PE\n");
1067
1068 if (pnv_ioda_configure_pe(phb, pe)) {
1069 /* XXX What do we do here ? */
1e916772 1070 pnv_ioda_free_pe(pe);
184cd4a3
BH
1071 pdn->pe_number = IODA_INVALID_PE;
1072 pe->pdev = NULL;
1073 pci_dev_put(dev);
1074 return NULL;
1075 }
1076
1d4e89cf
AK
1077 /* Put PE to the list */
1078 list_add_tail(&pe->list, &phb->ioda.pe_list);
1079
184cd4a3
BH
1080 return pe;
1081}
1082
1083static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1084{
1085 struct pci_dev *dev;
1086
1087 list_for_each_entry(dev, &bus->devices, bus_list) {
b72c1f65 1088 struct pci_dn *pdn = pci_get_pdn(dev);
184cd4a3
BH
1089
1090 if (pdn == NULL) {
1091 pr_warn("%s: No device node associated with device !\n",
1092 pci_name(dev));
1093 continue;
1094 }
ccd1c191
GS
1095
1096 /*
1097 * In partial hotplug case, the PCI device might be still
1098 * associated with the PE and needn't attach it to the PE
1099 * again.
1100 */
1101 if (pdn->pe_number != IODA_INVALID_PE)
1102 continue;
1103
c5f7700b 1104 pe->device_count++;
94973b24 1105 pdn->pcidev = dev;
184cd4a3 1106 pdn->pe_number = pe->pe_number;
fb446ad0 1107 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
184cd4a3
BH
1108 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1109 }
1110}
1111
fb446ad0
GS
1112/*
1113 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1114 * single PCI bus. Another one that contains the primary PCI bus and its
1115 * subordinate PCI devices and buses. The second type of PE is normally
1116 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1117 */
1e916772 1118static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
184cd4a3 1119{
fb446ad0 1120 struct pci_controller *hose = pci_bus_to_host(bus);
184cd4a3 1121 struct pnv_phb *phb = hose->private_data;
1e916772 1122 struct pnv_ioda_pe *pe = NULL;
ccd1c191
GS
1123 unsigned int pe_num;
1124
1125 /*
1126 * In partial hotplug case, the PE instance might be still alive.
1127 * We should reuse it instead of allocating a new one.
1128 */
1129 pe_num = phb->ioda.pe_rmap[bus->number << 8];
1130 if (pe_num != IODA_INVALID_PE) {
1131 pe = &phb->ioda.pe_array[pe_num];
1132 pnv_ioda_setup_same_PE(bus, pe);
1133 return NULL;
1134 }
262af557 1135
63803c39
GS
1136 /* PE number for root bus should have been reserved */
1137 if (pci_is_root_bus(bus) &&
1138 phb->ioda.root_pe_idx != IODA_INVALID_PE)
1139 pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
1140
262af557 1141 /* Check if PE is determined by M64 */
63803c39 1142 if (!pe && phb->pick_m64_pe)
1e916772 1143 pe = phb->pick_m64_pe(bus, all);
262af557
GC
1144
1145 /* The PE number isn't pinned by M64 */
1e916772
GS
1146 if (!pe)
1147 pe = pnv_ioda_alloc_pe(phb);
184cd4a3 1148
1e916772 1149 if (!pe) {
fb446ad0
GS
1150 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1151 __func__, pci_domain_nr(bus), bus->number);
1e916772 1152 return NULL;
184cd4a3
BH
1153 }
1154
262af557 1155 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
184cd4a3
BH
1156 pe->pbus = bus;
1157 pe->pdev = NULL;
184cd4a3 1158 pe->mve_number = -1;
b918c62e 1159 pe->rid = bus->busn_res.start << 8;
184cd4a3 1160
fb446ad0 1161 if (all)
1f52f176 1162 pe_info(pe, "Secondary bus %d..%d associated with PE#%x\n",
1e916772 1163 bus->busn_res.start, bus->busn_res.end, pe->pe_number);
fb446ad0 1164 else
1f52f176 1165 pe_info(pe, "Secondary bus %d associated with PE#%x\n",
1e916772 1166 bus->busn_res.start, pe->pe_number);
184cd4a3
BH
1167
1168 if (pnv_ioda_configure_pe(phb, pe)) {
1169 /* XXX What do we do here ? */
1e916772 1170 pnv_ioda_free_pe(pe);
184cd4a3 1171 pe->pbus = NULL;
1e916772 1172 return NULL;
184cd4a3
BH
1173 }
1174
1175 /* Associate it with all child devices */
1176 pnv_ioda_setup_same_PE(bus, pe);
1177
7ebdf956
GS
1178 /* Put PE to the list */
1179 list_add_tail(&pe->list, &phb->ioda.pe_list);
1e916772
GS
1180
1181 return pe;
184cd4a3
BH
1182}
1183
b521549a
AP
1184static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev)
1185{
1186 int pe_num, found_pe = false, rc;
1187 long rid;
1188 struct pnv_ioda_pe *pe;
1189 struct pci_dev *gpu_pdev;
1190 struct pci_dn *npu_pdn;
1191 struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus);
1192 struct pnv_phb *phb = hose->private_data;
1193
1194 /*
1195 * Due to a hardware errata PE#0 on the NPU is reserved for
1196 * error handling. This means we only have three PEs remaining
1197 * which need to be assigned to four links, implying some
1198 * links must share PEs.
1199 *
1200 * To achieve this we assign PEs such that NPUs linking the
1201 * same GPU get assigned the same PE.
1202 */
1203 gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev);
92b8f137 1204 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
b521549a
AP
1205 pe = &phb->ioda.pe_array[pe_num];
1206 if (!pe->pdev)
1207 continue;
1208
1209 if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) {
1210 /*
1211 * This device has the same peer GPU so should
1212 * be assigned the same PE as the existing
1213 * peer NPU.
1214 */
1215 dev_info(&npu_pdev->dev,
1f52f176 1216 "Associating to existing PE %x\n", pe_num);
b521549a
AP
1217 pci_dev_get(npu_pdev);
1218 npu_pdn = pci_get_pdn(npu_pdev);
1219 rid = npu_pdev->bus->number << 8 | npu_pdn->devfn;
1220 npu_pdn->pcidev = npu_pdev;
1221 npu_pdn->pe_number = pe_num;
b521549a
AP
1222 phb->ioda.pe_rmap[rid] = pe->pe_number;
1223
1224 /* Map the PE to this link */
1225 rc = opal_pci_set_pe(phb->opal_id, pe_num, rid,
1226 OpalPciBusAll,
1227 OPAL_COMPARE_RID_DEVICE_NUMBER,
1228 OPAL_COMPARE_RID_FUNCTION_NUMBER,
1229 OPAL_MAP_PE);
1230 WARN_ON(rc != OPAL_SUCCESS);
1231 found_pe = true;
1232 break;
1233 }
1234 }
1235
1236 if (!found_pe)
1237 /*
1238 * Could not find an existing PE so allocate a new
1239 * one.
1240 */
1241 return pnv_ioda_setup_dev_PE(npu_pdev);
1242 else
1243 return pe;
1244}
1245
1246static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus)
5d2aa710 1247{
5d2aa710
AP
1248 struct pci_dev *pdev;
1249
1250 list_for_each_entry(pdev, &bus->devices, bus_list)
b521549a 1251 pnv_ioda_setup_npu_PE(pdev);
5d2aa710
AP
1252}
1253
cad5cef6 1254static void pnv_pci_ioda_setup_PEs(void)
fb446ad0
GS
1255{
1256 struct pci_controller *hose, *tmp;
262af557 1257 struct pnv_phb *phb;
fb446ad0
GS
1258
1259 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
262af557 1260 phb = hose->private_data;
08f48f32
AP
1261 if (phb->type == PNV_PHB_NPU) {
1262 /* PE#0 is needed for error reporting */
1263 pnv_ioda_reserve_pe(phb, 0);
b521549a 1264 pnv_ioda_setup_npu_PEs(hose->bus);
c1c4dcc2
AP
1265 if (phb->model == PNV_PHB_MODEL_NPU2)
1266 pnv_npu2_init(phb);
ccd1c191 1267 }
184cd4a3
BH
1268 }
1269}
1270
a8b2f828 1271#ifdef CONFIG_PCI_IOV
ee8222fe 1272static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
781a868f
WY
1273{
1274 struct pci_bus *bus;
1275 struct pci_controller *hose;
1276 struct pnv_phb *phb;
1277 struct pci_dn *pdn;
02639b0e 1278 int i, j;
ee8222fe 1279 int m64_bars;
781a868f
WY
1280
1281 bus = pdev->bus;
1282 hose = pci_bus_to_host(bus);
1283 phb = hose->private_data;
1284 pdn = pci_get_pdn(pdev);
1285
ee8222fe
WY
1286 if (pdn->m64_single_mode)
1287 m64_bars = num_vfs;
1288 else
1289 m64_bars = 1;
1290
02639b0e 1291 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
ee8222fe
WY
1292 for (j = 0; j < m64_bars; j++) {
1293 if (pdn->m64_map[j][i] == IODA_INVALID_M64)
02639b0e
WY
1294 continue;
1295 opal_pci_phb_mmio_enable(phb->opal_id,
ee8222fe
WY
1296 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0);
1297 clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc);
1298 pdn->m64_map[j][i] = IODA_INVALID_M64;
02639b0e 1299 }
781a868f 1300
ee8222fe 1301 kfree(pdn->m64_map);
781a868f
WY
1302 return 0;
1303}
1304
02639b0e 1305static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
781a868f
WY
1306{
1307 struct pci_bus *bus;
1308 struct pci_controller *hose;
1309 struct pnv_phb *phb;
1310 struct pci_dn *pdn;
1311 unsigned int win;
1312 struct resource *res;
02639b0e 1313 int i, j;
781a868f 1314 int64_t rc;
02639b0e
WY
1315 int total_vfs;
1316 resource_size_t size, start;
1317 int pe_num;
ee8222fe 1318 int m64_bars;
781a868f
WY
1319
1320 bus = pdev->bus;
1321 hose = pci_bus_to_host(bus);
1322 phb = hose->private_data;
1323 pdn = pci_get_pdn(pdev);
02639b0e 1324 total_vfs = pci_sriov_get_totalvfs(pdev);
781a868f 1325
ee8222fe
WY
1326 if (pdn->m64_single_mode)
1327 m64_bars = num_vfs;
1328 else
1329 m64_bars = 1;
1330
1331 pdn->m64_map = kmalloc(sizeof(*pdn->m64_map) * m64_bars, GFP_KERNEL);
1332 if (!pdn->m64_map)
1333 return -ENOMEM;
1334 /* Initialize the m64_map to IODA_INVALID_M64 */
1335 for (i = 0; i < m64_bars ; i++)
1336 for (j = 0; j < PCI_SRIOV_NUM_BARS; j++)
1337 pdn->m64_map[i][j] = IODA_INVALID_M64;
02639b0e 1338
781a868f
WY
1339
1340 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1341 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1342 if (!res->flags || !res->parent)
1343 continue;
1344
ee8222fe 1345 for (j = 0; j < m64_bars; j++) {
02639b0e
WY
1346 do {
1347 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1348 phb->ioda.m64_bar_idx + 1, 0);
1349
1350 if (win >= phb->ioda.m64_bar_idx + 1)
1351 goto m64_failed;
1352 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
1353
ee8222fe 1354 pdn->m64_map[j][i] = win;
02639b0e 1355
ee8222fe 1356 if (pdn->m64_single_mode) {
02639b0e
WY
1357 size = pci_iov_resource_size(pdev,
1358 PCI_IOV_RESOURCES + i);
02639b0e
WY
1359 start = res->start + size * j;
1360 } else {
1361 size = resource_size(res);
1362 start = res->start;
1363 }
1364
1365 /* Map the M64 here */
ee8222fe 1366 if (pdn->m64_single_mode) {
be283eeb 1367 pe_num = pdn->pe_num_map[j];
02639b0e
WY
1368 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1369 pe_num, OPAL_M64_WINDOW_TYPE,
ee8222fe 1370 pdn->m64_map[j][i], 0);
02639b0e
WY
1371 }
1372
1373 rc = opal_pci_set_phb_mem_window(phb->opal_id,
1374 OPAL_M64_WINDOW_TYPE,
ee8222fe 1375 pdn->m64_map[j][i],
02639b0e
WY
1376 start,
1377 0, /* unused */
1378 size);
781a868f 1379
781a868f 1380
02639b0e
WY
1381 if (rc != OPAL_SUCCESS) {
1382 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1383 win, rc);
1384 goto m64_failed;
1385 }
781a868f 1386
ee8222fe 1387 if (pdn->m64_single_mode)
02639b0e 1388 rc = opal_pci_phb_mmio_enable(phb->opal_id,
ee8222fe 1389 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2);
02639b0e
WY
1390 else
1391 rc = opal_pci_phb_mmio_enable(phb->opal_id,
ee8222fe 1392 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1);
781a868f 1393
02639b0e
WY
1394 if (rc != OPAL_SUCCESS) {
1395 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1396 win, rc);
1397 goto m64_failed;
1398 }
781a868f
WY
1399 }
1400 }
1401 return 0;
1402
1403m64_failed:
ee8222fe 1404 pnv_pci_vf_release_m64(pdev, num_vfs);
781a868f
WY
1405 return -EBUSY;
1406}
1407
c035e37b
AK
1408static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1409 int num);
1410static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1411
781a868f
WY
1412static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1413{
781a868f 1414 struct iommu_table *tbl;
781a868f
WY
1415 int64_t rc;
1416
b348aa65 1417 tbl = pe->table_group.tables[0];
c035e37b 1418 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
781a868f
WY
1419 if (rc)
1420 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1421
c035e37b 1422 pnv_pci_ioda2_set_bypass(pe, false);
0eaf4def
AK
1423 if (pe->table_group.group) {
1424 iommu_group_put(pe->table_group.group);
1425 BUG_ON(pe->table_group.group);
ac9a5889 1426 }
781a868f 1427 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
781a868f
WY
1428}
1429
ee8222fe 1430static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
781a868f
WY
1431{
1432 struct pci_bus *bus;
1433 struct pci_controller *hose;
1434 struct pnv_phb *phb;
1435 struct pnv_ioda_pe *pe, *pe_n;
1436 struct pci_dn *pdn;
1437
1438 bus = pdev->bus;
1439 hose = pci_bus_to_host(bus);
1440 phb = hose->private_data;
02639b0e 1441 pdn = pci_get_pdn(pdev);
781a868f
WY
1442
1443 if (!pdev->is_physfn)
1444 return;
1445
781a868f
WY
1446 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1447 if (pe->parent_dev != pdev)
1448 continue;
1449
1450 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1451
1452 /* Remove from list */
1453 mutex_lock(&phb->ioda.pe_list_mutex);
1454 list_del(&pe->list);
1455 mutex_unlock(&phb->ioda.pe_list_mutex);
1456
1457 pnv_ioda_deconfigure_pe(phb, pe);
1458
1e916772 1459 pnv_ioda_free_pe(pe);
781a868f
WY
1460 }
1461}
1462
1463void pnv_pci_sriov_disable(struct pci_dev *pdev)
1464{
1465 struct pci_bus *bus;
1466 struct pci_controller *hose;
1467 struct pnv_phb *phb;
1e916772 1468 struct pnv_ioda_pe *pe;
781a868f
WY
1469 struct pci_dn *pdn;
1470 struct pci_sriov *iov;
be283eeb 1471 u16 num_vfs, i;
781a868f
WY
1472
1473 bus = pdev->bus;
1474 hose = pci_bus_to_host(bus);
1475 phb = hose->private_data;
1476 pdn = pci_get_pdn(pdev);
1477 iov = pdev->sriov;
1478 num_vfs = pdn->num_vfs;
1479
1480 /* Release VF PEs */
ee8222fe 1481 pnv_ioda_release_vf_PE(pdev);
781a868f
WY
1482
1483 if (phb->type == PNV_PHB_IODA2) {
ee8222fe 1484 if (!pdn->m64_single_mode)
be283eeb 1485 pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map);
781a868f
WY
1486
1487 /* Release M64 windows */
ee8222fe 1488 pnv_pci_vf_release_m64(pdev, num_vfs);
781a868f
WY
1489
1490 /* Release PE numbers */
be283eeb
WY
1491 if (pdn->m64_single_mode) {
1492 for (i = 0; i < num_vfs; i++) {
1e916772
GS
1493 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1494 continue;
1495
1496 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1497 pnv_ioda_free_pe(pe);
be283eeb
WY
1498 }
1499 } else
1500 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1501 /* Releasing pe_num_map */
1502 kfree(pdn->pe_num_map);
781a868f
WY
1503 }
1504}
1505
1506static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1507 struct pnv_ioda_pe *pe);
1508static void pnv_ioda_setup_vf_PE(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 pnv_ioda_pe *pe;
1514 int pe_num;
1515 u16 vf_index;
1516 struct pci_dn *pdn;
1517
1518 bus = pdev->bus;
1519 hose = pci_bus_to_host(bus);
1520 phb = hose->private_data;
1521 pdn = pci_get_pdn(pdev);
1522
1523 if (!pdev->is_physfn)
1524 return;
1525
1526 /* Reserve PE for each VF */
1527 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
be283eeb
WY
1528 if (pdn->m64_single_mode)
1529 pe_num = pdn->pe_num_map[vf_index];
1530 else
1531 pe_num = *pdn->pe_num_map + vf_index;
781a868f
WY
1532
1533 pe = &phb->ioda.pe_array[pe_num];
1534 pe->pe_number = pe_num;
1535 pe->phb = phb;
1536 pe->flags = PNV_IODA_PE_VF;
1537 pe->pbus = NULL;
1538 pe->parent_dev = pdev;
781a868f
WY
1539 pe->mve_number = -1;
1540 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1541 pci_iov_virtfn_devfn(pdev, vf_index);
1542
1f52f176 1543 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%x\n",
781a868f
WY
1544 hose->global_number, pdev->bus->number,
1545 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1546 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1547
1548 if (pnv_ioda_configure_pe(phb, pe)) {
1549 /* XXX What do we do here ? */
1e916772 1550 pnv_ioda_free_pe(pe);
781a868f
WY
1551 pe->pdev = NULL;
1552 continue;
1553 }
1554
781a868f
WY
1555 /* Put PE to the list */
1556 mutex_lock(&phb->ioda.pe_list_mutex);
1557 list_add_tail(&pe->list, &phb->ioda.pe_list);
1558 mutex_unlock(&phb->ioda.pe_list_mutex);
1559
1560 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1561 }
1562}
1563
1564int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1565{
1566 struct pci_bus *bus;
1567 struct pci_controller *hose;
1568 struct pnv_phb *phb;
1e916772 1569 struct pnv_ioda_pe *pe;
781a868f
WY
1570 struct pci_dn *pdn;
1571 int ret;
be283eeb 1572 u16 i;
781a868f
WY
1573
1574 bus = pdev->bus;
1575 hose = pci_bus_to_host(bus);
1576 phb = hose->private_data;
1577 pdn = pci_get_pdn(pdev);
1578
1579 if (phb->type == PNV_PHB_IODA2) {
b0331854
WY
1580 if (!pdn->vfs_expanded) {
1581 dev_info(&pdev->dev, "don't support this SRIOV device"
1582 " with non 64bit-prefetchable IOV BAR\n");
1583 return -ENOSPC;
1584 }
1585
ee8222fe
WY
1586 /*
1587 * When M64 BARs functions in Single PE mode, the number of VFs
1588 * could be enabled must be less than the number of M64 BARs.
1589 */
1590 if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) {
1591 dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n");
1592 return -EBUSY;
1593 }
1594
be283eeb
WY
1595 /* Allocating pe_num_map */
1596 if (pdn->m64_single_mode)
1597 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map) * num_vfs,
1598 GFP_KERNEL);
1599 else
1600 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL);
1601
1602 if (!pdn->pe_num_map)
1603 return -ENOMEM;
1604
1605 if (pdn->m64_single_mode)
1606 for (i = 0; i < num_vfs; i++)
1607 pdn->pe_num_map[i] = IODA_INVALID_PE;
1608
781a868f 1609 /* Calculate available PE for required VFs */
be283eeb
WY
1610 if (pdn->m64_single_mode) {
1611 for (i = 0; i < num_vfs; i++) {
1e916772
GS
1612 pe = pnv_ioda_alloc_pe(phb);
1613 if (!pe) {
be283eeb
WY
1614 ret = -EBUSY;
1615 goto m64_failed;
1616 }
1e916772
GS
1617
1618 pdn->pe_num_map[i] = pe->pe_number;
be283eeb
WY
1619 }
1620 } else {
1621 mutex_lock(&phb->ioda.pe_alloc_mutex);
1622 *pdn->pe_num_map = bitmap_find_next_zero_area(
92b8f137 1623 phb->ioda.pe_alloc, phb->ioda.total_pe_num,
be283eeb 1624 0, num_vfs, 0);
92b8f137 1625 if (*pdn->pe_num_map >= phb->ioda.total_pe_num) {
be283eeb
WY
1626 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1627 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1628 kfree(pdn->pe_num_map);
1629 return -EBUSY;
1630 }
1631 bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
781a868f 1632 mutex_unlock(&phb->ioda.pe_alloc_mutex);
781a868f 1633 }
781a868f 1634 pdn->num_vfs = num_vfs;
781a868f
WY
1635
1636 /* Assign M64 window accordingly */
02639b0e 1637 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
781a868f
WY
1638 if (ret) {
1639 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1640 goto m64_failed;
1641 }
1642
1643 /*
1644 * When using one M64 BAR to map one IOV BAR, we need to shift
1645 * the IOV BAR according to the PE# allocated to the VFs.
1646 * Otherwise, the PE# for the VF will conflict with others.
1647 */
ee8222fe 1648 if (!pdn->m64_single_mode) {
be283eeb 1649 ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map);
02639b0e
WY
1650 if (ret)
1651 goto m64_failed;
1652 }
781a868f
WY
1653 }
1654
1655 /* Setup VF PEs */
1656 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1657
1658 return 0;
1659
1660m64_failed:
be283eeb
WY
1661 if (pdn->m64_single_mode) {
1662 for (i = 0; i < num_vfs; i++) {
1e916772
GS
1663 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1664 continue;
1665
1666 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1667 pnv_ioda_free_pe(pe);
be283eeb
WY
1668 }
1669 } else
1670 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1671
1672 /* Releasing pe_num_map */
1673 kfree(pdn->pe_num_map);
781a868f
WY
1674
1675 return ret;
1676}
1677
a8b2f828
GS
1678int pcibios_sriov_disable(struct pci_dev *pdev)
1679{
781a868f
WY
1680 pnv_pci_sriov_disable(pdev);
1681
a8b2f828
GS
1682 /* Release PCI data */
1683 remove_dev_pci_data(pdev);
1684 return 0;
1685}
1686
1687int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1688{
1689 /* Allocate PCI data */
1690 add_dev_pci_data(pdev);
781a868f 1691
ee8222fe 1692 return pnv_pci_sriov_enable(pdev, num_vfs);
a8b2f828
GS
1693}
1694#endif /* CONFIG_PCI_IOV */
1695
959c9bdd 1696static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
184cd4a3 1697{
b72c1f65 1698 struct pci_dn *pdn = pci_get_pdn(pdev);
959c9bdd 1699 struct pnv_ioda_pe *pe;
184cd4a3 1700
959c9bdd
GS
1701 /*
1702 * The function can be called while the PE#
1703 * hasn't been assigned. Do nothing for the
1704 * case.
1705 */
1706 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1707 return;
184cd4a3 1708
959c9bdd 1709 pe = &phb->ioda.pe_array[pdn->pe_number];
cd15b048 1710 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
0e1ffef0 1711 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
b348aa65 1712 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
4617082e
AK
1713 /*
1714 * Note: iommu_add_device() will fail here as
1715 * for physical PE: the device is already added by now;
1716 * for virtual PE: sysfs entries are not ready yet and
1717 * tce_iommu_bus_notifier will add the device to a group later.
1718 */
184cd4a3
BH
1719}
1720
763d2d8d 1721static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
cd15b048 1722{
763d2d8d
DA
1723 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1724 struct pnv_phb *phb = hose->private_data;
cd15b048
BH
1725 struct pci_dn *pdn = pci_get_pdn(pdev);
1726 struct pnv_ioda_pe *pe;
1727 uint64_t top;
1728 bool bypass = false;
1729
1730 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1731 return -ENODEV;;
1732
1733 pe = &phb->ioda.pe_array[pdn->pe_number];
1734 if (pe->tce_bypass_enabled) {
1735 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1736 bypass = (dma_mask >= top);
1737 }
1738
1739 if (bypass) {
1740 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1741 set_dma_ops(&pdev->dev, &dma_direct_ops);
cd15b048
BH
1742 } else {
1743 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1744 set_dma_ops(&pdev->dev, &dma_iommu_ops);
cd15b048 1745 }
a32305bf 1746 *pdev->dev.dma_mask = dma_mask;
5d2aa710
AP
1747
1748 /* Update peer npu devices */
f9f83456 1749 pnv_npu_try_dma_set_bypass(pdev, bypass);
5d2aa710 1750
cd15b048
BH
1751 return 0;
1752}
1753
53522982 1754static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
fe7e85c6 1755{
53522982
AD
1756 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1757 struct pnv_phb *phb = hose->private_data;
fe7e85c6
GS
1758 struct pci_dn *pdn = pci_get_pdn(pdev);
1759 struct pnv_ioda_pe *pe;
1760 u64 end, mask;
1761
1762 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1763 return 0;
1764
1765 pe = &phb->ioda.pe_array[pdn->pe_number];
1766 if (!pe->tce_bypass_enabled)
1767 return __dma_get_required_mask(&pdev->dev);
1768
1769
1770 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1771 mask = 1ULL << (fls64(end) - 1);
1772 mask += mask - 1;
1773
1774 return mask;
1775}
1776
dff4a39e 1777static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
5c42257b
AK
1778 struct pci_bus *bus,
1779 bool add_to_group)
74251fe2
BH
1780{
1781 struct pci_dev *dev;
1782
1783 list_for_each_entry(dev, &bus->devices, bus_list) {
b348aa65 1784 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
e91c2511 1785 set_dma_offset(&dev->dev, pe->tce_bypass_base);
5c42257b
AK
1786 if (add_to_group)
1787 iommu_add_device(&dev->dev);
dff4a39e 1788
5c89a87d 1789 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
5c42257b
AK
1790 pnv_ioda_setup_bus_dma(pe, dev->subordinate,
1791 add_to_group);
74251fe2
BH
1792 }
1793}
1794
fd141d1a
BH
1795static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb,
1796 bool real_mode)
1797{
1798 return real_mode ? (__be64 __iomem *)(phb->regs_phys + 0x210) :
1799 (phb->regs + 0x210);
1800}
1801
a34ab7c3 1802static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table *tbl,
decbda25 1803 unsigned long index, unsigned long npages, bool rm)
4cce9550 1804{
0eaf4def
AK
1805 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1806 &tbl->it_group_list, struct iommu_table_group_link,
1807 next);
1808 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
b348aa65 1809 struct pnv_ioda_pe, table_group);
fd141d1a 1810 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
4cce9550
GS
1811 unsigned long start, end, inc;
1812
decbda25
AK
1813 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1814 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1815 npages - 1);
4cce9550 1816
08acce1c
BH
1817 /* p7ioc-style invalidation, 2 TCEs per write */
1818 start |= (1ull << 63);
1819 end |= (1ull << 63);
1820 inc = 16;
4cce9550
GS
1821 end |= inc - 1; /* round up end to be different than start */
1822
1823 mb(); /* Ensure above stores are visible */
1824 while (start <= end) {
8e0a1611 1825 if (rm)
3ad26e5c 1826 __raw_rm_writeq(cpu_to_be64(start), invalidate);
8e0a1611 1827 else
3ad26e5c 1828 __raw_writeq(cpu_to_be64(start), invalidate);
4cce9550
GS
1829 start += inc;
1830 }
1831
1832 /*
1833 * The iommu layer will do another mb() for us on build()
1834 * and we don't care on free()
1835 */
1836}
1837
decbda25
AK
1838static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1839 long npages, unsigned long uaddr,
1840 enum dma_data_direction direction,
00085f1e 1841 unsigned long attrs)
decbda25
AK
1842{
1843 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1844 attrs);
1845
08acce1c 1846 if (!ret)
a34ab7c3 1847 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
decbda25
AK
1848
1849 return ret;
1850}
1851
05c6cfb9
AK
1852#ifdef CONFIG_IOMMU_API
1853static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1854 unsigned long *hpa, enum dma_data_direction *direction)
1855{
1856 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1857
08acce1c 1858 if (!ret)
a34ab7c3 1859 pnv_pci_p7ioc_tce_invalidate(tbl, index, 1, false);
05c6cfb9
AK
1860
1861 return ret;
1862}
55c89185
AK
1863
1864static int pnv_ioda1_tce_xchg_rm(struct iommu_table *tbl, long index,
1865 unsigned long *hpa, enum dma_data_direction *direction)
1866{
1867 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1868
1869 if (!ret)
1870 pnv_pci_p7ioc_tce_invalidate(tbl, index, 1, true);
1871
1872 return ret;
1873}
05c6cfb9
AK
1874#endif
1875
decbda25
AK
1876static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1877 long npages)
1878{
1879 pnv_tce_free(tbl, index, npages);
1880
08acce1c 1881 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
decbda25
AK
1882}
1883
da004c36 1884static struct iommu_table_ops pnv_ioda1_iommu_ops = {
decbda25 1885 .set = pnv_ioda1_tce_build,
05c6cfb9
AK
1886#ifdef CONFIG_IOMMU_API
1887 .exchange = pnv_ioda1_tce_xchg,
55c89185 1888 .exchange_rm = pnv_ioda1_tce_xchg_rm,
05c6cfb9 1889#endif
decbda25 1890 .clear = pnv_ioda1_tce_free,
da004c36
AK
1891 .get = pnv_tce_get,
1892};
1893
a34ab7c3
BH
1894#define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)
1895#define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)
1896#define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)
bef9253f 1897
87c9c5b4 1898static void pnv_pci_phb3_tce_invalidate_entire(struct pnv_phb *phb, bool rm)
0bbcdb43 1899{
fd141d1a 1900 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(phb, rm);
a34ab7c3 1901 const unsigned long val = PHB3_TCE_KILL_INVAL_ALL;
0bbcdb43
AK
1902
1903 mb(); /* Ensure previous TCE table stores are visible */
1904 if (rm)
fd141d1a 1905 __raw_rm_writeq(cpu_to_be64(val), invalidate);
0bbcdb43 1906 else
fd141d1a 1907 __raw_writeq(cpu_to_be64(val), invalidate);
0bbcdb43
AK
1908}
1909
a34ab7c3 1910static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)
5780fb04
AK
1911{
1912 /* 01xb - invalidate TCEs that match the specified PE# */
fd141d1a 1913 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, false);
a34ab7c3 1914 unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
5780fb04
AK
1915
1916 mb(); /* Ensure above stores are visible */
fd141d1a 1917 __raw_writeq(cpu_to_be64(val), invalidate);
5780fb04
AK
1918}
1919
fd141d1a
BH
1920static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe, bool rm,
1921 unsigned shift, unsigned long index,
1922 unsigned long npages)
4cce9550 1923{
4d902195 1924 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
4cce9550 1925 unsigned long start, end, inc;
4cce9550
GS
1926
1927 /* We'll invalidate DMA address in PE scope */
a34ab7c3 1928 start = PHB3_TCE_KILL_INVAL_ONE;
fd141d1a 1929 start |= (pe->pe_number & 0xFF);
4cce9550
GS
1930 end = start;
1931
1932 /* Figure out the start, end and step */
decbda25
AK
1933 start |= (index << shift);
1934 end |= ((index + npages - 1) << shift);
b0376c9b 1935 inc = (0x1ull << shift);
4cce9550
GS
1936 mb();
1937
1938 while (start <= end) {
8e0a1611 1939 if (rm)
3ad26e5c 1940 __raw_rm_writeq(cpu_to_be64(start), invalidate);
8e0a1611 1941 else
3ad26e5c 1942 __raw_writeq(cpu_to_be64(start), invalidate);
4cce9550
GS
1943 start += inc;
1944 }
1945}
1946
f0228c41
BH
1947static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
1948{
1949 struct pnv_phb *phb = pe->phb;
1950
1951 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1952 pnv_pci_phb3_tce_invalidate_pe(pe);
1953 else
1954 opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE,
1955 pe->pe_number, 0, 0, 0);
1956}
1957
e57080f1
AK
1958static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1959 unsigned long index, unsigned long npages, bool rm)
1960{
1961 struct iommu_table_group_link *tgl;
1962
55c89185 1963 list_for_each_entry_lockless(tgl, &tbl->it_group_list, next) {
e57080f1
AK
1964 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1965 struct pnv_ioda_pe, table_group);
f0228c41
BH
1966 struct pnv_phb *phb = pe->phb;
1967 unsigned int shift = tbl->it_page_shift;
1968
4aa71ef1
AP
1969 /*
1970 * NVLink1 can use the TCE kill register directly as
1971 * it's the same as PHB3. NVLink2 is different and
1972 * should go via the OPAL call.
1973 */
1974 if (phb->model == PNV_PHB_MODEL_NPU) {
0bbcdb43
AK
1975 /*
1976 * The NVLink hardware does not support TCE kill
1977 * per TCE entry so we have to invalidate
1978 * the entire cache for it.
1979 */
f0228c41 1980 pnv_pci_phb3_tce_invalidate_entire(phb, rm);
85674868
AK
1981 continue;
1982 }
f0228c41
BH
1983 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1984 pnv_pci_phb3_tce_invalidate(pe, rm, shift,
1985 index, npages);
f0228c41
BH
1986 else
1987 opal_pci_tce_kill(phb->opal_id,
1988 OPAL_PCI_TCE_KILL_PAGES,
1989 pe->pe_number, 1u << shift,
1990 index << shift, npages);
e57080f1
AK
1991 }
1992}
1993
87c9c5b4
AP
1994void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_phb *phb, bool rm)
1995{
1996 if (phb->model == PNV_PHB_MODEL_NPU || phb->model == PNV_PHB_MODEL_PHB3)
1997 pnv_pci_phb3_tce_invalidate_entire(phb, rm);
1998 else
1999 opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL, 0, 0, 0, 0);
2000}
2001
decbda25
AK
2002static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
2003 long npages, unsigned long uaddr,
2004 enum dma_data_direction direction,
00085f1e 2005 unsigned long attrs)
4cce9550 2006{
decbda25
AK
2007 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
2008 attrs);
4cce9550 2009
08acce1c 2010 if (!ret)
decbda25
AK
2011 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
2012
2013 return ret;
2014}
2015
05c6cfb9
AK
2016#ifdef CONFIG_IOMMU_API
2017static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
2018 unsigned long *hpa, enum dma_data_direction *direction)
2019{
2020 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
2021
08acce1c 2022 if (!ret)
05c6cfb9
AK
2023 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
2024
2025 return ret;
2026}
55c89185
AK
2027
2028static int pnv_ioda2_tce_xchg_rm(struct iommu_table *tbl, long index,
2029 unsigned long *hpa, enum dma_data_direction *direction)
2030{
2031 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
2032
2033 if (!ret)
2034 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, true);
2035
2036 return ret;
2037}
05c6cfb9
AK
2038#endif
2039
decbda25
AK
2040static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
2041 long npages)
2042{
2043 pnv_tce_free(tbl, index, npages);
2044
08acce1c 2045 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
4cce9550
GS
2046}
2047
4793d65d
AK
2048static void pnv_ioda2_table_free(struct iommu_table *tbl)
2049{
2050 pnv_pci_ioda2_table_free_pages(tbl);
4793d65d
AK
2051}
2052
da004c36 2053static struct iommu_table_ops pnv_ioda2_iommu_ops = {
decbda25 2054 .set = pnv_ioda2_tce_build,
05c6cfb9
AK
2055#ifdef CONFIG_IOMMU_API
2056 .exchange = pnv_ioda2_tce_xchg,
55c89185 2057 .exchange_rm = pnv_ioda2_tce_xchg_rm,
05c6cfb9 2058#endif
decbda25 2059 .clear = pnv_ioda2_tce_free,
da004c36 2060 .get = pnv_tce_get,
4793d65d 2061 .free = pnv_ioda2_table_free,
da004c36
AK
2062};
2063
801846d1
GS
2064static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data)
2065{
2066 unsigned int *weight = (unsigned int *)data;
2067
2068 /* This is quite simplistic. The "base" weight of a device
2069 * is 10. 0 means no DMA is to be accounted for it.
2070 */
2071 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
2072 return 0;
2073
2074 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
2075 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
2076 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
2077 *weight += 3;
2078 else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
2079 *weight += 15;
2080 else
2081 *weight += 10;
2082
2083 return 0;
2084}
2085
2086static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe)
2087{
2088 unsigned int weight = 0;
2089
2090 /* SRIOV VF has same DMA32 weight as its PF */
2091#ifdef CONFIG_PCI_IOV
2092 if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) {
2093 pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight);
2094 return weight;
2095 }
2096#endif
2097
2098 if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) {
2099 pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight);
2100 } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) {
2101 struct pci_dev *pdev;
2102
2103 list_for_each_entry(pdev, &pe->pbus->devices, bus_list)
2104 pnv_pci_ioda_dev_dma_weight(pdev, &weight);
2105 } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) {
2106 pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight);
2107 }
2108
2109 return weight;
2110}
2111
b30d936f 2112static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
2b923ed1 2113 struct pnv_ioda_pe *pe)
184cd4a3
BH
2114{
2115
2116 struct page *tce_mem = NULL;
184cd4a3 2117 struct iommu_table *tbl;
2b923ed1
GS
2118 unsigned int weight, total_weight = 0;
2119 unsigned int tce32_segsz, base, segs, avail, i;
184cd4a3
BH
2120 int64_t rc;
2121 void *addr;
2122
184cd4a3
BH
2123 /* XXX FIXME: Handle 64-bit only DMA devices */
2124 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
2125 /* XXX FIXME: Allocate multi-level tables on PHB3 */
2b923ed1
GS
2126 weight = pnv_pci_ioda_pe_dma_weight(pe);
2127 if (!weight)
2128 return;
2129
2130 pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight,
2131 &total_weight);
2132 segs = (weight * phb->ioda.dma32_count) / total_weight;
2133 if (!segs)
2134 segs = 1;
184cd4a3 2135
2b923ed1
GS
2136 /*
2137 * Allocate contiguous DMA32 segments. We begin with the expected
2138 * number of segments. With one more attempt, the number of DMA32
2139 * segments to be allocated is decreased by one until one segment
2140 * is allocated successfully.
2141 */
2142 do {
2143 for (base = 0; base <= phb->ioda.dma32_count - segs; base++) {
2144 for (avail = 0, i = base; i < base + segs; i++) {
2145 if (phb->ioda.dma32_segmap[i] ==
2146 IODA_INVALID_PE)
2147 avail++;
2148 }
2149
2150 if (avail == segs)
2151 goto found;
2152 }
2153 } while (--segs);
2154
2155 if (!segs) {
2156 pe_warn(pe, "No available DMA32 segments\n");
2157 return;
2158 }
2159
2160found:
0eaf4def 2161 tbl = pnv_pci_table_alloc(phb->hose->node);
b348aa65
AK
2162 iommu_register_group(&pe->table_group, phb->hose->global_number,
2163 pe->pe_number);
0eaf4def 2164 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
c5773822 2165
184cd4a3 2166 /* Grab a 32-bit TCE table */
2b923ed1
GS
2167 pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
2168 weight, total_weight, base, segs);
184cd4a3 2169 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
acce971c
GS
2170 base * PNV_IODA1_DMA32_SEGSIZE,
2171 (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1);
184cd4a3
BH
2172
2173 /* XXX Currently, we allocate one big contiguous table for the
2174 * TCEs. We only really need one chunk per 256M of TCE space
2175 * (ie per segment) but that's an optimization for later, it
2176 * requires some added smarts with our get/put_tce implementation
acce971c
GS
2177 *
2178 * Each TCE page is 4KB in size and each TCE entry occupies 8
2179 * bytes
184cd4a3 2180 */
acce971c 2181 tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3);
184cd4a3 2182 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
acce971c 2183 get_order(tce32_segsz * segs));
184cd4a3
BH
2184 if (!tce_mem) {
2185 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
2186 goto fail;
2187 }
2188 addr = page_address(tce_mem);
acce971c 2189 memset(addr, 0, tce32_segsz * segs);
184cd4a3
BH
2190
2191 /* Configure HW */
2192 for (i = 0; i < segs; i++) {
2193 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2194 pe->pe_number,
2195 base + i, 1,
acce971c
GS
2196 __pa(addr) + tce32_segsz * i,
2197 tce32_segsz, IOMMU_PAGE_SIZE_4K);
184cd4a3
BH
2198 if (rc) {
2199 pe_err(pe, " Failed to configure 32-bit TCE table,"
2200 " err %ld\n", rc);
2201 goto fail;
2202 }
2203 }
2204
2b923ed1
GS
2205 /* Setup DMA32 segment mapping */
2206 for (i = base; i < base + segs; i++)
2207 phb->ioda.dma32_segmap[i] = pe->pe_number;
2208
184cd4a3 2209 /* Setup linux iommu table */
acce971c
GS
2210 pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs,
2211 base * PNV_IODA1_DMA32_SEGSIZE,
2212 IOMMU_PAGE_SHIFT_4K);
184cd4a3 2213
da004c36 2214 tbl->it_ops = &pnv_ioda1_iommu_ops;
4793d65d
AK
2215 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
2216 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
184cd4a3
BH
2217 iommu_init_table(tbl, phb->hose->node);
2218
781a868f 2219 if (pe->flags & PNV_IODA_PE_DEV) {
4617082e
AK
2220 /*
2221 * Setting table base here only for carrying iommu_group
2222 * further down to let iommu_add_device() do the job.
2223 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2224 */
2225 set_iommu_table_base(&pe->pdev->dev, tbl);
2226 iommu_add_device(&pe->pdev->dev);
c5773822 2227 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
5c42257b 2228 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
74251fe2 2229
184cd4a3
BH
2230 return;
2231 fail:
2232 /* XXX Failure: Try to fallback to 64-bit only ? */
184cd4a3 2233 if (tce_mem)
acce971c 2234 __free_pages(tce_mem, get_order(tce32_segsz * segs));
0eaf4def
AK
2235 if (tbl) {
2236 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
2237 iommu_free_table(tbl, "pnv");
2238 }
184cd4a3
BH
2239}
2240
43cb60ab
AK
2241static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
2242 int num, struct iommu_table *tbl)
2243{
2244 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2245 table_group);
2246 struct pnv_phb *phb = pe->phb;
2247 int64_t rc;
bbb845c4
AK
2248 const unsigned long size = tbl->it_indirect_levels ?
2249 tbl->it_level_size : tbl->it_size;
43cb60ab
AK
2250 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
2251 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
2252
4793d65d 2253 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
43cb60ab
AK
2254 start_addr, start_addr + win_size - 1,
2255 IOMMU_PAGE_SIZE(tbl));
2256
2257 /*
2258 * Map TCE table through TVT. The TVE index is the PE number
2259 * shifted by 1 bit for 32-bits DMA space.
2260 */
2261 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2262 pe->pe_number,
4793d65d 2263 (pe->pe_number << 1) + num,
bbb845c4 2264 tbl->it_indirect_levels + 1,
43cb60ab 2265 __pa(tbl->it_base),
bbb845c4 2266 size << 3,
43cb60ab
AK
2267 IOMMU_PAGE_SIZE(tbl));
2268 if (rc) {
2269 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
2270 return rc;
2271 }
2272
2273 pnv_pci_link_table_and_group(phb->hose->node, num,
2274 tbl, &pe->table_group);
ed7d9a1d 2275 pnv_pci_ioda2_tce_invalidate_pe(pe);
43cb60ab
AK
2276
2277 return 0;
2278}
2279
f87a8864 2280static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
cd15b048 2281{
cd15b048
BH
2282 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2283 int64_t rc;
2284
2285 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2286 if (enable) {
2287 phys_addr_t top = memblock_end_of_DRAM();
2288
2289 top = roundup_pow_of_two(top);
2290 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2291 pe->pe_number,
2292 window_id,
2293 pe->tce_bypass_base,
2294 top);
2295 } else {
2296 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2297 pe->pe_number,
2298 window_id,
2299 pe->tce_bypass_base,
2300 0);
cd15b048
BH
2301 }
2302 if (rc)
2303 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2304 else
2305 pe->tce_bypass_enabled = enable;
2306}
2307
4793d65d
AK
2308static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2309 __u32 page_shift, __u64 window_size, __u32 levels,
2310 struct iommu_table *tbl);
2311
2312static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2313 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2314 struct iommu_table **ptbl)
2315{
2316 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2317 table_group);
2318 int nid = pe->phb->hose->node;
2319 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2320 long ret;
2321 struct iommu_table *tbl;
2322
2323 tbl = pnv_pci_table_alloc(nid);
2324 if (!tbl)
2325 return -ENOMEM;
2326
2ad1ce60
AK
2327 tbl->it_ops = &pnv_ioda2_iommu_ops;
2328
4793d65d
AK
2329 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2330 bus_offset, page_shift, window_size,
2331 levels, tbl);
2332 if (ret) {
2333 iommu_free_table(tbl, "pnv");
2334 return ret;
2335 }
2336
4793d65d
AK
2337 *ptbl = tbl;
2338
2339 return 0;
2340}
2341
46d3e1e1
AK
2342static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2343{
2344 struct iommu_table *tbl = NULL;
2345 long rc;
2346
fa144869
NA
2347 /*
2348 * crashkernel= specifies the kdump kernel's maximum memory at
2349 * some offset and there is no guaranteed the result is a power
2350 * of 2, which will cause errors later.
2351 */
2352 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
2353
bb005455
NA
2354 /*
2355 * In memory constrained environments, e.g. kdump kernel, the
2356 * DMA window can be larger than available memory, which will
2357 * cause errors later.
2358 */
fa144869 2359 const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
bb005455 2360
46d3e1e1
AK
2361 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2362 IOMMU_PAGE_SHIFT_4K,
bb005455 2363 window_size,
46d3e1e1
AK
2364 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2365 if (rc) {
2366 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2367 rc);
2368 return rc;
2369 }
2370
2371 iommu_init_table(tbl, pe->phb->hose->node);
2372
2373 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2374 if (rc) {
2375 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2376 rc);
2ad1ce60 2377 iommu_free_table(tbl, "");
46d3e1e1
AK
2378 return rc;
2379 }
2380
2381 if (!pnv_iommu_bypass_disabled)
2382 pnv_pci_ioda2_set_bypass(pe, true);
2383
46d3e1e1
AK
2384 /*
2385 * Setting table base here only for carrying iommu_group
2386 * further down to let iommu_add_device() do the job.
2387 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2388 */
2389 if (pe->flags & PNV_IODA_PE_DEV)
2390 set_iommu_table_base(&pe->pdev->dev, tbl);
2391
2392 return 0;
2393}
2394
b5926430
AK
2395#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2396static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2397 int num)
2398{
2399 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2400 table_group);
2401 struct pnv_phb *phb = pe->phb;
2402 long ret;
2403
2404 pe_info(pe, "Removing DMA window #%d\n", num);
2405
2406 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2407 (pe->pe_number << 1) + num,
2408 0/* levels */, 0/* table address */,
2409 0/* table size */, 0/* page size */);
2410 if (ret)
2411 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2412 else
ed7d9a1d 2413 pnv_pci_ioda2_tce_invalidate_pe(pe);
b5926430
AK
2414
2415 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2416
2417 return ret;
2418}
2419#endif
2420
f87a8864 2421#ifdef CONFIG_IOMMU_API
00547193
AK
2422static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2423 __u64 window_size, __u32 levels)
2424{
2425 unsigned long bytes = 0;
2426 const unsigned window_shift = ilog2(window_size);
2427 unsigned entries_shift = window_shift - page_shift;
2428 unsigned table_shift = entries_shift + 3;
2429 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2430 unsigned long direct_table_size;
2431
2432 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2433 (window_size > memory_hotplug_max()) ||
2434 !is_power_of_2(window_size))
2435 return 0;
2436
2437 /* Calculate a direct table size from window_size and levels */
2438 entries_shift = (entries_shift + levels - 1) / levels;
2439 table_shift = entries_shift + 3;
2440 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2441 direct_table_size = 1UL << table_shift;
2442
2443 for ( ; levels; --levels) {
2444 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2445
2446 tce_table_size /= direct_table_size;
2447 tce_table_size <<= 3;
2448 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2449 }
2450
2451 return bytes;
2452}
2453
f87a8864 2454static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
cd15b048 2455{
f87a8864
AK
2456 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2457 table_group);
46d3e1e1
AK
2458 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2459 struct iommu_table *tbl = pe->table_group.tables[0];
cd15b048 2460
f87a8864 2461 pnv_pci_ioda2_set_bypass(pe, false);
46d3e1e1 2462 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
5c42257b
AK
2463 if (pe->pbus)
2464 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
2ad1ce60 2465 iommu_free_table(tbl, "pnv");
f87a8864 2466}
cd15b048 2467
f87a8864
AK
2468static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2469{
2470 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2471 table_group);
2472
46d3e1e1 2473 pnv_pci_ioda2_setup_default_config(pe);
5c42257b
AK
2474 if (pe->pbus)
2475 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
cd15b048
BH
2476}
2477
f87a8864 2478static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
00547193 2479 .get_table_size = pnv_pci_ioda2_get_table_size,
4793d65d
AK
2480 .create_table = pnv_pci_ioda2_create_table,
2481 .set_window = pnv_pci_ioda2_set_window,
2482 .unset_window = pnv_pci_ioda2_unset_window,
f87a8864
AK
2483 .take_ownership = pnv_ioda2_take_ownership,
2484 .release_ownership = pnv_ioda2_release_ownership,
2485};
b5cb9ab1
AK
2486
2487static int gpe_table_group_to_npe_cb(struct device *dev, void *opaque)
2488{
2489 struct pci_controller *hose;
2490 struct pnv_phb *phb;
2491 struct pnv_ioda_pe **ptmppe = opaque;
2492 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
2493 struct pci_dn *pdn = pci_get_pdn(pdev);
2494
2495 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2496 return 0;
2497
2498 hose = pci_bus_to_host(pdev->bus);
2499 phb = hose->private_data;
2500 if (phb->type != PNV_PHB_NPU)
2501 return 0;
2502
2503 *ptmppe = &phb->ioda.pe_array[pdn->pe_number];
2504
2505 return 1;
2506}
2507
2508/*
2509 * This returns PE of associated NPU.
2510 * This assumes that NPU is in the same IOMMU group with GPU and there is
2511 * no other PEs.
2512 */
2513static struct pnv_ioda_pe *gpe_table_group_to_npe(
2514 struct iommu_table_group *table_group)
2515{
2516 struct pnv_ioda_pe *npe = NULL;
2517 int ret = iommu_group_for_each_dev(table_group->group, &npe,
2518 gpe_table_group_to_npe_cb);
2519
2520 BUG_ON(!ret || !npe);
2521
2522 return npe;
2523}
2524
2525static long pnv_pci_ioda2_npu_set_window(struct iommu_table_group *table_group,
2526 int num, struct iommu_table *tbl)
2527{
2528 long ret = pnv_pci_ioda2_set_window(table_group, num, tbl);
2529
2530 if (ret)
2531 return ret;
2532
2533 ret = pnv_npu_set_window(gpe_table_group_to_npe(table_group), num, tbl);
2534 if (ret)
2535 pnv_pci_ioda2_unset_window(table_group, num);
2536
2537 return ret;
2538}
2539
2540static long pnv_pci_ioda2_npu_unset_window(
2541 struct iommu_table_group *table_group,
2542 int num)
2543{
2544 long ret = pnv_pci_ioda2_unset_window(table_group, num);
2545
2546 if (ret)
2547 return ret;
2548
2549 return pnv_npu_unset_window(gpe_table_group_to_npe(table_group), num);
2550}
2551
2552static void pnv_ioda2_npu_take_ownership(struct iommu_table_group *table_group)
2553{
2554 /*
2555 * Detach NPU first as pnv_ioda2_take_ownership() will destroy
2556 * the iommu_table if 32bit DMA is enabled.
2557 */
2558 pnv_npu_take_ownership(gpe_table_group_to_npe(table_group));
2559 pnv_ioda2_take_ownership(table_group);
2560}
2561
2562static struct iommu_table_group_ops pnv_pci_ioda2_npu_ops = {
2563 .get_table_size = pnv_pci_ioda2_get_table_size,
2564 .create_table = pnv_pci_ioda2_create_table,
2565 .set_window = pnv_pci_ioda2_npu_set_window,
2566 .unset_window = pnv_pci_ioda2_npu_unset_window,
2567 .take_ownership = pnv_ioda2_npu_take_ownership,
2568 .release_ownership = pnv_ioda2_release_ownership,
2569};
2570
2571static void pnv_pci_ioda_setup_iommu_api(void)
2572{
2573 struct pci_controller *hose, *tmp;
2574 struct pnv_phb *phb;
2575 struct pnv_ioda_pe *pe, *gpe;
2576
2577 /*
2578 * Now we have all PHBs discovered, time to add NPU devices to
2579 * the corresponding IOMMU groups.
2580 */
2581 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2582 phb = hose->private_data;
2583
2584 if (phb->type != PNV_PHB_NPU)
2585 continue;
2586
2587 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2588 gpe = pnv_pci_npu_setup_iommu(pe);
2589 if (gpe)
2590 gpe->table_group.ops = &pnv_pci_ioda2_npu_ops;
2591 }
2592 }
2593}
2594#else /* !CONFIG_IOMMU_API */
2595static void pnv_pci_ioda_setup_iommu_api(void) { };
f87a8864
AK
2596#endif
2597
bbb845c4
AK
2598static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2599 unsigned levels, unsigned long limit,
3ba3a73e 2600 unsigned long *current_offset, unsigned long *total_allocated)
373f5657
GS
2601{
2602 struct page *tce_mem = NULL;
bbb845c4 2603 __be64 *addr, *tmp;
aca6913f 2604 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
bbb845c4
AK
2605 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2606 unsigned entries = 1UL << (shift - 3);
2607 long i;
aca6913f
AK
2608
2609 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2610 if (!tce_mem) {
2611 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2612 return NULL;
2613 }
2614 addr = page_address(tce_mem);
bbb845c4 2615 memset(addr, 0, allocated);
3ba3a73e 2616 *total_allocated += allocated;
bbb845c4
AK
2617
2618 --levels;
2619 if (!levels) {
2620 *current_offset += allocated;
2621 return addr;
2622 }
2623
2624 for (i = 0; i < entries; ++i) {
2625 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
3ba3a73e 2626 levels, limit, current_offset, total_allocated);
bbb845c4
AK
2627 if (!tmp)
2628 break;
2629
2630 addr[i] = cpu_to_be64(__pa(tmp) |
2631 TCE_PCI_READ | TCE_PCI_WRITE);
2632
2633 if (*current_offset >= limit)
2634 break;
2635 }
aca6913f
AK
2636
2637 return addr;
2638}
2639
bbb845c4
AK
2640static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2641 unsigned long size, unsigned level);
2642
aca6913f 2643static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
bbb845c4
AK
2644 __u32 page_shift, __u64 window_size, __u32 levels,
2645 struct iommu_table *tbl)
aca6913f 2646{
373f5657 2647 void *addr;
3ba3a73e 2648 unsigned long offset = 0, level_shift, total_allocated = 0;
aca6913f
AK
2649 const unsigned window_shift = ilog2(window_size);
2650 unsigned entries_shift = window_shift - page_shift;
2651 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2652 const unsigned long tce_table_size = 1UL << table_shift;
2653
bbb845c4
AK
2654 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2655 return -EINVAL;
2656
aca6913f
AK
2657 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2658 return -EINVAL;
2659
bbb845c4
AK
2660 /* Adjust direct table size from window_size and levels */
2661 entries_shift = (entries_shift + levels - 1) / levels;
2662 level_shift = entries_shift + 3;
2663 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2664
aca6913f 2665 /* Allocate TCE table */
bbb845c4 2666 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
3ba3a73e 2667 levels, tce_table_size, &offset, &total_allocated);
bbb845c4
AK
2668
2669 /* addr==NULL means that the first level allocation failed */
aca6913f
AK
2670 if (!addr)
2671 return -ENOMEM;
2672
bbb845c4
AK
2673 /*
2674 * First level was allocated but some lower level failed as
2675 * we did not allocate as much as we wanted,
2676 * release partially allocated table.
2677 */
2678 if (offset < tce_table_size) {
2679 pnv_pci_ioda2_table_do_free_pages(addr,
2680 1ULL << (level_shift - 3), levels - 1);
2681 return -ENOMEM;
2682 }
2683
aca6913f
AK
2684 /* Setup linux iommu table */
2685 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2686 page_shift);
bbb845c4
AK
2687 tbl->it_level_size = 1ULL << (level_shift - 3);
2688 tbl->it_indirect_levels = levels - 1;
3ba3a73e 2689 tbl->it_allocated_size = total_allocated;
aca6913f
AK
2690
2691 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2692 window_size, tce_table_size, bus_offset);
2693
2694 return 0;
2695}
2696
bbb845c4
AK
2697static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2698 unsigned long size, unsigned level)
2699{
2700 const unsigned long addr_ul = (unsigned long) addr &
2701 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2702
2703 if (level) {
2704 long i;
2705 u64 *tmp = (u64 *) addr_ul;
2706
2707 for (i = 0; i < size; ++i) {
2708 unsigned long hpa = be64_to_cpu(tmp[i]);
2709
2710 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2711 continue;
2712
2713 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2714 level - 1);
2715 }
2716 }
2717
2718 free_pages(addr_ul, get_order(size << 3));
2719}
2720
aca6913f
AK
2721static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2722{
bbb845c4
AK
2723 const unsigned long size = tbl->it_indirect_levels ?
2724 tbl->it_level_size : tbl->it_size;
2725
aca6913f
AK
2726 if (!tbl->it_size)
2727 return;
2728
bbb845c4
AK
2729 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2730 tbl->it_indirect_levels);
aca6913f
AK
2731}
2732
2733static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2734 struct pnv_ioda_pe *pe)
2735{
373f5657
GS
2736 int64_t rc;
2737
ccd1c191
GS
2738 if (!pnv_pci_ioda_pe_dma_weight(pe))
2739 return;
2740
f87a8864
AK
2741 /* TVE #1 is selected by PCI address bit 59 */
2742 pe->tce_bypass_base = 1ull << 59;
2743
b348aa65
AK
2744 iommu_register_group(&pe->table_group, phb->hose->global_number,
2745 pe->pe_number);
c5773822 2746
373f5657 2747 /* The PE will reserve all possible 32-bits space */
373f5657 2748 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
aca6913f 2749 phb->ioda.m32_pci_base);
373f5657 2750
aca6913f 2751 /* Setup linux iommu table */
4793d65d
AK
2752 pe->table_group.tce32_start = 0;
2753 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2754 pe->table_group.max_dynamic_windows_supported =
2755 IOMMU_TABLE_GROUP_MAX_TABLES;
2756 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2757 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
e5aad1e6
AK
2758#ifdef CONFIG_IOMMU_API
2759 pe->table_group.ops = &pnv_pci_ioda2_ops;
2760#endif
2761
46d3e1e1 2762 rc = pnv_pci_ioda2_setup_default_config(pe);
801846d1 2763 if (rc)
46d3e1e1 2764 return;
373f5657 2765
46d3e1e1 2766 if (pe->flags & PNV_IODA_PE_DEV)
4617082e 2767 iommu_add_device(&pe->pdev->dev);
46d3e1e1 2768 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
5c42257b 2769 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
373f5657
GS
2770}
2771
184cd4a3 2772#ifdef CONFIG_PCI_MSI
4ee11c1a 2773int64_t pnv_opal_pci_msi_eoi(struct irq_chip *chip, unsigned int hw_irq)
137436c9 2774{
137436c9
GS
2775 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2776 ioda.irq_chip);
4ee11c1a
SW
2777
2778 return opal_pci_msi_eoi(phb->opal_id, hw_irq);
2779}
2780
2781static void pnv_ioda2_msi_eoi(struct irq_data *d)
2782{
137436c9 2783 int64_t rc;
4ee11c1a
SW
2784 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2785 struct irq_chip *chip = irq_data_get_irq_chip(d);
137436c9 2786
4ee11c1a 2787 rc = pnv_opal_pci_msi_eoi(chip, hw_irq);
137436c9
GS
2788 WARN_ON_ONCE(rc);
2789
2790 icp_native_eoi(d);
2791}
2792
fd9a1c26 2793
f456834a 2794void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
fd9a1c26
IM
2795{
2796 struct irq_data *idata;
2797 struct irq_chip *ichip;
2798
fb111334
BH
2799 /* The MSI EOI OPAL call is only needed on PHB3 */
2800 if (phb->model != PNV_PHB_MODEL_PHB3)
fd9a1c26
IM
2801 return;
2802
2803 if (!phb->ioda.irq_chip_init) {
2804 /*
2805 * First time we setup an MSI IRQ, we need to setup the
2806 * corresponding IRQ chip to route correctly.
2807 */
2808 idata = irq_get_irq_data(virq);
2809 ichip = irq_data_get_irq_chip(idata);
2810 phb->ioda.irq_chip_init = 1;
2811 phb->ioda.irq_chip = *ichip;
2812 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2813 }
2814 irq_set_chip(virq, &phb->ioda.irq_chip);
2815}
2816
4ee11c1a
SW
2817/*
2818 * Returns true iff chip is something that we could call
2819 * pnv_opal_pci_msi_eoi for.
2820 */
2821bool is_pnv_opal_msi(struct irq_chip *chip)
2822{
2823 return chip->irq_eoi == pnv_ioda2_msi_eoi;
2824}
2825EXPORT_SYMBOL_GPL(is_pnv_opal_msi);
2826
184cd4a3 2827static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
137436c9
GS
2828 unsigned int hwirq, unsigned int virq,
2829 unsigned int is_64, struct msi_msg *msg)
184cd4a3
BH
2830{
2831 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2832 unsigned int xive_num = hwirq - phb->msi_base;
3a1a4661 2833 __be32 data;
184cd4a3
BH
2834 int rc;
2835
2836 /* No PE assigned ? bail out ... no MSI for you ! */
2837 if (pe == NULL)
2838 return -ENXIO;
2839
2840 /* Check if we have an MVE */
2841 if (pe->mve_number < 0)
2842 return -ENXIO;
2843
b72c1f65 2844 /* Force 32-bit MSI on some broken devices */
36074381 2845 if (dev->no_64bit_msi)
b72c1f65
BH
2846 is_64 = 0;
2847
184cd4a3
BH
2848 /* Assign XIVE to PE */
2849 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2850 if (rc) {
2851 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2852 pci_name(dev), rc, xive_num);
2853 return -EIO;
2854 }
2855
2856 if (is_64) {
3a1a4661
BH
2857 __be64 addr64;
2858
184cd4a3
BH
2859 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2860 &addr64, &data);
2861 if (rc) {
2862 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2863 pci_name(dev), rc);
2864 return -EIO;
2865 }
3a1a4661
BH
2866 msg->address_hi = be64_to_cpu(addr64) >> 32;
2867 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
184cd4a3 2868 } else {
3a1a4661
BH
2869 __be32 addr32;
2870
184cd4a3
BH
2871 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2872 &addr32, &data);
2873 if (rc) {
2874 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2875 pci_name(dev), rc);
2876 return -EIO;
2877 }
2878 msg->address_hi = 0;
3a1a4661 2879 msg->address_lo = be32_to_cpu(addr32);
184cd4a3 2880 }
3a1a4661 2881 msg->data = be32_to_cpu(data);
184cd4a3 2882
f456834a 2883 pnv_set_msi_irq_chip(phb, virq);
137436c9 2884
184cd4a3 2885 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1f52f176 2886 " address=%x_%08x data=%x PE# %x\n",
184cd4a3
BH
2887 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2888 msg->address_hi, msg->address_lo, data, pe->pe_number);
2889
2890 return 0;
2891}
2892
2893static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2894{
fb1b55d6 2895 unsigned int count;
184cd4a3
BH
2896 const __be32 *prop = of_get_property(phb->hose->dn,
2897 "ibm,opal-msi-ranges", NULL);
2898 if (!prop) {
2899 /* BML Fallback */
2900 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2901 }
2902 if (!prop)
2903 return;
2904
2905 phb->msi_base = be32_to_cpup(prop);
fb1b55d6
GS
2906 count = be32_to_cpup(prop + 1);
2907 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
184cd4a3
BH
2908 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2909 phb->hose->global_number);
2910 return;
2911 }
fb1b55d6 2912
184cd4a3
BH
2913 phb->msi_setup = pnv_pci_ioda_msi_setup;
2914 phb->msi32_support = 1;
2915 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
fb1b55d6 2916 count, phb->msi_base);
184cd4a3
BH
2917}
2918#else
2919static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2920#endif /* CONFIG_PCI_MSI */
2921
6e628c7d
WY
2922#ifdef CONFIG_PCI_IOV
2923static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2924{
f2dd0afe
WY
2925 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
2926 struct pnv_phb *phb = hose->private_data;
2927 const resource_size_t gate = phb->ioda.m64_segsize >> 2;
6e628c7d
WY
2928 struct resource *res;
2929 int i;
dfcc8d45 2930 resource_size_t size, total_vf_bar_sz;
6e628c7d 2931 struct pci_dn *pdn;
5b88ec22 2932 int mul, total_vfs;
6e628c7d
WY
2933
2934 if (!pdev->is_physfn || pdev->is_added)
2935 return;
2936
6e628c7d
WY
2937 pdn = pci_get_pdn(pdev);
2938 pdn->vfs_expanded = 0;
ee8222fe 2939 pdn->m64_single_mode = false;
6e628c7d 2940
5b88ec22 2941 total_vfs = pci_sriov_get_totalvfs(pdev);
92b8f137 2942 mul = phb->ioda.total_pe_num;
dfcc8d45 2943 total_vf_bar_sz = 0;
5b88ec22
WY
2944
2945 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2946 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2947 if (!res->flags || res->parent)
2948 continue;
b79331a5 2949 if (!pnv_pci_is_m64_flags(res->flags)) {
b0331854
WY
2950 dev_warn(&pdev->dev, "Don't support SR-IOV with"
2951 " non M64 VF BAR%d: %pR. \n",
5b88ec22 2952 i, res);
b0331854 2953 goto truncate_iov;
5b88ec22
WY
2954 }
2955
dfcc8d45
WY
2956 total_vf_bar_sz += pci_iov_resource_size(pdev,
2957 i + PCI_IOV_RESOURCES);
5b88ec22 2958
f2dd0afe
WY
2959 /*
2960 * If bigger than quarter of M64 segment size, just round up
2961 * power of two.
2962 *
2963 * Generally, one M64 BAR maps one IOV BAR. To avoid conflict
2964 * with other devices, IOV BAR size is expanded to be
2965 * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64
2966 * segment size , the expanded size would equal to half of the
2967 * whole M64 space size, which will exhaust the M64 Space and
2968 * limit the system flexibility. This is a design decision to
2969 * set the boundary to quarter of the M64 segment size.
2970 */
dfcc8d45 2971 if (total_vf_bar_sz > gate) {
5b88ec22 2972 mul = roundup_pow_of_two(total_vfs);
dfcc8d45
WY
2973 dev_info(&pdev->dev,
2974 "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n",
2975 total_vf_bar_sz, gate, mul);
ee8222fe 2976 pdn->m64_single_mode = true;
5b88ec22
WY
2977 break;
2978 }
2979 }
2980
6e628c7d
WY
2981 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2982 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2983 if (!res->flags || res->parent)
2984 continue;
6e628c7d 2985
6e628c7d 2986 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
ee8222fe
WY
2987 /*
2988 * On PHB3, the minimum size alignment of M64 BAR in single
2989 * mode is 32MB.
2990 */
2991 if (pdn->m64_single_mode && (size < SZ_32M))
2992 goto truncate_iov;
2993 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
5b88ec22 2994 res->end = res->start + size * mul - 1;
6e628c7d
WY
2995 dev_dbg(&pdev->dev, " %pR\n", res);
2996 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
5b88ec22 2997 i, res, mul);
6e628c7d 2998 }
5b88ec22 2999 pdn->vfs_expanded = mul;
b0331854
WY
3000
3001 return;
3002
3003truncate_iov:
3004 /* To save MMIO space, IOV BAR is truncated. */
3005 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
3006 res = &pdev->resource[i + PCI_IOV_RESOURCES];
3007 res->flags = 0;
3008 res->end = res->start - 1;
3009 }
6e628c7d
WY
3010}
3011#endif /* CONFIG_PCI_IOV */
3012
23e79425
GS
3013static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
3014 struct resource *res)
3015{
3016 struct pnv_phb *phb = pe->phb;
3017 struct pci_bus_region region;
3018 int index;
3019 int64_t rc;
3020
3021 if (!res || !res->flags || res->start > res->end)
3022 return;
3023
3024 if (res->flags & IORESOURCE_IO) {
3025 region.start = res->start - phb->ioda.io_pci_base;
3026 region.end = res->end - phb->ioda.io_pci_base;
3027 index = region.start / phb->ioda.io_segsize;
3028
3029 while (index < phb->ioda.total_pe_num &&
3030 region.start <= region.end) {
3031 phb->ioda.io_segmap[index] = pe->pe_number;
3032 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3033 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
3034 if (rc != OPAL_SUCCESS) {
1f52f176 3035 pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",
23e79425
GS
3036 __func__, rc, index, pe->pe_number);
3037 break;
3038 }
3039
3040 region.start += phb->ioda.io_segsize;
3041 index++;
3042 }
3043 } else if ((res->flags & IORESOURCE_MEM) &&
5958d19a 3044 !pnv_pci_is_m64(phb, res)) {
23e79425
GS
3045 region.start = res->start -
3046 phb->hose->mem_offset[0] -
3047 phb->ioda.m32_pci_base;
3048 region.end = res->end -
3049 phb->hose->mem_offset[0] -
3050 phb->ioda.m32_pci_base;
3051 index = region.start / phb->ioda.m32_segsize;
3052
3053 while (index < phb->ioda.total_pe_num &&
3054 region.start <= region.end) {
3055 phb->ioda.m32_segmap[index] = pe->pe_number;
3056 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3057 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
3058 if (rc != OPAL_SUCCESS) {
1f52f176 3059 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",
23e79425
GS
3060 __func__, rc, index, pe->pe_number);
3061 break;
3062 }
3063
3064 region.start += phb->ioda.m32_segsize;
3065 index++;
3066 }
3067 }
3068}
3069
11685bec
GS
3070/*
3071 * This function is supposed to be called on basis of PE from top
3072 * to bottom style. So the the I/O or MMIO segment assigned to
3073 * parent PE could be overrided by its child PEs if necessary.
3074 */
23e79425 3075static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
11685bec 3076{
69d733e7 3077 struct pci_dev *pdev;
23e79425 3078 int i;
11685bec
GS
3079
3080 /*
3081 * NOTE: We only care PCI bus based PE for now. For PCI
3082 * device based PE, for example SRIOV sensitive VF should
3083 * be figured out later.
3084 */
3085 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
3086
69d733e7
GS
3087 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
3088 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
3089 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
3090
3091 /*
3092 * If the PE contains all subordinate PCI buses, the
3093 * windows of the child bridges should be mapped to
3094 * the PE as well.
3095 */
3096 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
3097 continue;
3098 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
3099 pnv_ioda_setup_pe_res(pe,
3100 &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
3101 }
11685bec
GS
3102}
3103
98b665da
RC
3104#ifdef CONFIG_DEBUG_FS
3105static int pnv_pci_diag_data_set(void *data, u64 val)
3106{
3107 struct pci_controller *hose;
3108 struct pnv_phb *phb;
3109 s64 ret;
3110
3111 if (val != 1ULL)
3112 return -EINVAL;
3113
3114 hose = (struct pci_controller *)data;
3115 if (!hose || !hose->private_data)
3116 return -ENODEV;
3117
3118 phb = hose->private_data;
3119
3120 /* Retrieve the diag data from firmware */
3121 ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob,
3122 PNV_PCI_DIAG_BUF_SIZE);
3123 if (ret != OPAL_SUCCESS)
3124 return -EIO;
3125
3126 /* Print the diag data to the kernel log */
3127 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob);
3128 return 0;
3129}
3130
3131DEFINE_SIMPLE_ATTRIBUTE(pnv_pci_diag_data_fops, NULL,
3132 pnv_pci_diag_data_set, "%llu\n");
3133
3134#endif /* CONFIG_DEBUG_FS */
3135
37c367f2
GS
3136static void pnv_pci_ioda_create_dbgfs(void)
3137{
3138#ifdef CONFIG_DEBUG_FS
3139 struct pci_controller *hose, *tmp;
3140 struct pnv_phb *phb;
3141 char name[16];
3142
3143 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3144 phb = hose->private_data;
3145
ccd1c191
GS
3146 /* Notify initialization of PHB done */
3147 phb->initialized = 1;
3148
37c367f2
GS
3149 sprintf(name, "PCI%04x", hose->global_number);
3150 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
98b665da 3151 if (!phb->dbgfs) {
37c367f2
GS
3152 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
3153 __func__, hose->global_number);
98b665da
RC
3154 continue;
3155 }
3156
3157 debugfs_create_file("dump_diag_regs", 0200, phb->dbgfs, hose,
3158 &pnv_pci_diag_data_fops);
37c367f2
GS
3159 }
3160#endif /* CONFIG_DEBUG_FS */
3161}
3162
cad5cef6 3163static void pnv_pci_ioda_fixup(void)
fb446ad0
GS
3164{
3165 pnv_pci_ioda_setup_PEs();
ccd1c191 3166 pnv_pci_ioda_setup_iommu_api();
37c367f2
GS
3167 pnv_pci_ioda_create_dbgfs();
3168
e9cc17d4 3169#ifdef CONFIG_EEH
e9cc17d4 3170 eeh_init();
dadcd6d6 3171 eeh_addr_cache_build();
e9cc17d4 3172#endif
fb446ad0
GS
3173}
3174
271fd03a
GS
3175/*
3176 * Returns the alignment for I/O or memory windows for P2P
3177 * bridges. That actually depends on how PEs are segmented.
3178 * For now, we return I/O or M32 segment size for PE sensitive
3179 * P2P bridges. Otherwise, the default values (4KiB for I/O,
3180 * 1MiB for memory) will be returned.
3181 *
3182 * The current PCI bus might be put into one PE, which was
3183 * create against the parent PCI bridge. For that case, we
3184 * needn't enlarge the alignment so that we can save some
3185 * resources.
3186 */
3187static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
3188 unsigned long type)
3189{
3190 struct pci_dev *bridge;
3191 struct pci_controller *hose = pci_bus_to_host(bus);
3192 struct pnv_phb *phb = hose->private_data;
3193 int num_pci_bridges = 0;
3194
3195 bridge = bus->self;
3196 while (bridge) {
3197 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
3198 num_pci_bridges++;
3199 if (num_pci_bridges >= 2)
3200 return 1;
3201 }
3202
3203 bridge = bridge->bus->self;
3204 }
3205
5958d19a
BH
3206 /*
3207 * We fall back to M32 if M64 isn't supported. We enforce the M64
3208 * alignment for any 64-bit resource, PCIe doesn't care and
3209 * bridges only do 64-bit prefetchable anyway.
3210 */
b79331a5 3211 if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type))
262af557 3212 return phb->ioda.m64_segsize;
271fd03a
GS
3213 if (type & IORESOURCE_MEM)
3214 return phb->ioda.m32_segsize;
3215
3216 return phb->ioda.io_segsize;
3217}
3218
40e2a47e
GS
3219/*
3220 * We are updating root port or the upstream port of the
3221 * bridge behind the root port with PHB's windows in order
3222 * to accommodate the changes on required resources during
3223 * PCI (slot) hotplug, which is connected to either root
3224 * port or the downstream ports of PCIe switch behind the
3225 * root port.
3226 */
3227static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
3228 unsigned long type)
3229{
3230 struct pci_controller *hose = pci_bus_to_host(bus);
3231 struct pnv_phb *phb = hose->private_data;
3232 struct pci_dev *bridge = bus->self;
3233 struct resource *r, *w;
3234 bool msi_region = false;
3235 int i;
3236
3237 /* Check if we need apply fixup to the bridge's windows */
3238 if (!pci_is_root_bus(bridge->bus) &&
3239 !pci_is_root_bus(bridge->bus->self->bus))
3240 return;
3241
3242 /* Fixup the resources */
3243 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
3244 r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
3245 if (!r->flags || !r->parent)
3246 continue;
3247
3248 w = NULL;
3249 if (r->flags & type & IORESOURCE_IO)
3250 w = &hose->io_resource;
5958d19a 3251 else if (pnv_pci_is_m64(phb, r) &&
40e2a47e
GS
3252 (type & IORESOURCE_PREFETCH) &&
3253 phb->ioda.m64_segsize)
3254 w = &hose->mem_resources[1];
3255 else if (r->flags & type & IORESOURCE_MEM) {
3256 w = &hose->mem_resources[0];
3257 msi_region = true;
3258 }
3259
3260 r->start = w->start;
3261 r->end = w->end;
3262
3263 /* The 64KB 32-bits MSI region shouldn't be included in
3264 * the 32-bits bridge window. Otherwise, we can see strange
3265 * issues. One of them is EEH error observed on Garrison.
3266 *
3267 * Exclude top 1MB region which is the minimal alignment of
3268 * 32-bits bridge window.
3269 */
3270 if (msi_region) {
3271 r->end += 0x10000;
3272 r->end -= 0x100000;
3273 }
3274 }
3275}
3276
ccd1c191
GS
3277static void pnv_pci_setup_bridge(struct pci_bus *bus, unsigned long type)
3278{
3279 struct pci_controller *hose = pci_bus_to_host(bus);
3280 struct pnv_phb *phb = hose->private_data;
3281 struct pci_dev *bridge = bus->self;
3282 struct pnv_ioda_pe *pe;
3283 bool all = (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
3284
40e2a47e
GS
3285 /* Extend bridge's windows if necessary */
3286 pnv_pci_fixup_bridge_resources(bus, type);
3287
63803c39
GS
3288 /* The PE for root bus should be realized before any one else */
3289 if (!phb->ioda.root_pe_populated) {
3290 pe = pnv_ioda_setup_bus_PE(phb->hose->bus, false);
3291 if (pe) {
3292 phb->ioda.root_pe_idx = pe->pe_number;
3293 phb->ioda.root_pe_populated = true;
3294 }
3295 }
3296
ccd1c191
GS
3297 /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
3298 if (list_empty(&bus->devices))
3299 return;
3300
3301 /* Reserve PEs according to used M64 resources */
3302 if (phb->reserve_m64_pe)
3303 phb->reserve_m64_pe(bus, NULL, all);
3304
3305 /*
3306 * Assign PE. We might run here because of partial hotplug.
3307 * For the case, we just pick up the existing PE and should
3308 * not allocate resources again.
3309 */
3310 pe = pnv_ioda_setup_bus_PE(bus, all);
3311 if (!pe)
3312 return;
3313
3314 pnv_ioda_setup_pe_seg(pe);
3315 switch (phb->type) {
3316 case PNV_PHB_IODA1:
3317 pnv_pci_ioda1_setup_dma_pe(phb, pe);
3318 break;
3319 case PNV_PHB_IODA2:
3320 pnv_pci_ioda2_setup_dma_pe(phb, pe);
3321 break;
3322 default:
1f52f176 3323 pr_warn("%s: No DMA for PHB#%x (type %d)\n",
ccd1c191
GS
3324 __func__, phb->hose->global_number, phb->type);
3325 }
3326}
3327
5350ab3f
WY
3328#ifdef CONFIG_PCI_IOV
3329static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
3330 int resno)
3331{
ee8222fe
WY
3332 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3333 struct pnv_phb *phb = hose->private_data;
5350ab3f 3334 struct pci_dn *pdn = pci_get_pdn(pdev);
7fbe7a93 3335 resource_size_t align;
5350ab3f 3336
7fbe7a93
WY
3337 /*
3338 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
3339 * SR-IOV. While from hardware perspective, the range mapped by M64
3340 * BAR should be size aligned.
3341 *
ee8222fe
WY
3342 * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra
3343 * powernv-specific hardware restriction is gone. But if just use the
3344 * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with
3345 * in one segment of M64 #15, which introduces the PE conflict between
3346 * PF and VF. Based on this, the minimum alignment of an IOV BAR is
3347 * m64_segsize.
3348 *
7fbe7a93
WY
3349 * This function returns the total IOV BAR size if M64 BAR is in
3350 * Shared PE mode or just VF BAR size if not.
ee8222fe
WY
3351 * If the M64 BAR is in Single PE mode, return the VF BAR size or
3352 * M64 segment size if IOV BAR size is less.
7fbe7a93 3353 */
5350ab3f 3354 align = pci_iov_resource_size(pdev, resno);
7fbe7a93
WY
3355 if (!pdn->vfs_expanded)
3356 return align;
ee8222fe
WY
3357 if (pdn->m64_single_mode)
3358 return max(align, (resource_size_t)phb->ioda.m64_segsize);
5350ab3f 3359
7fbe7a93 3360 return pdn->vfs_expanded * align;
5350ab3f
WY
3361}
3362#endif /* CONFIG_PCI_IOV */
3363
184cd4a3
BH
3364/* Prevent enabling devices for which we couldn't properly
3365 * assign a PE
3366 */
4361b034 3367bool pnv_pci_enable_device_hook(struct pci_dev *dev)
184cd4a3 3368{
db1266c8
GS
3369 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3370 struct pnv_phb *phb = hose->private_data;
3371 struct pci_dn *pdn;
184cd4a3 3372
db1266c8
GS
3373 /* The function is probably called while the PEs have
3374 * not be created yet. For example, resource reassignment
3375 * during PCI probe period. We just skip the check if
3376 * PEs isn't ready.
3377 */
3378 if (!phb->initialized)
c88c2a18 3379 return true;
db1266c8 3380
b72c1f65 3381 pdn = pci_get_pdn(dev);
184cd4a3 3382 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
c88c2a18 3383 return false;
db1266c8 3384
c88c2a18 3385 return true;
184cd4a3
BH
3386}
3387
c5f7700b
GS
3388static long pnv_pci_ioda1_unset_window(struct iommu_table_group *table_group,
3389 int num)
3390{
3391 struct pnv_ioda_pe *pe = container_of(table_group,
3392 struct pnv_ioda_pe, table_group);
3393 struct pnv_phb *phb = pe->phb;
3394 unsigned int idx;
3395 long rc;
3396
3397 pe_info(pe, "Removing DMA window #%d\n", num);
3398 for (idx = 0; idx < phb->ioda.dma32_count; idx++) {
3399 if (phb->ioda.dma32_segmap[idx] != pe->pe_number)
3400 continue;
3401
3402 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
3403 idx, 0, 0ul, 0ul, 0ul);
3404 if (rc != OPAL_SUCCESS) {
3405 pe_warn(pe, "Failure %ld unmapping DMA32 segment#%d\n",
3406 rc, idx);
3407 return rc;
3408 }
3409
3410 phb->ioda.dma32_segmap[idx] = IODA_INVALID_PE;
3411 }
3412
3413 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
3414 return OPAL_SUCCESS;
3415}
3416
3417static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe)
3418{
3419 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
3420 struct iommu_table *tbl = pe->table_group.tables[0];
3421 int64_t rc;
3422
3423 if (!weight)
3424 return;
3425
3426 rc = pnv_pci_ioda1_unset_window(&pe->table_group, 0);
3427 if (rc != OPAL_SUCCESS)
3428 return;
3429
a34ab7c3 3430 pnv_pci_p7ioc_tce_invalidate(tbl, tbl->it_offset, tbl->it_size, false);
c5f7700b
GS
3431 if (pe->table_group.group) {
3432 iommu_group_put(pe->table_group.group);
3433 WARN_ON(pe->table_group.group);
3434 }
3435
3436 free_pages(tbl->it_base, get_order(tbl->it_size << 3));
3437 iommu_free_table(tbl, "pnv");
3438}
3439
3440static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
3441{
3442 struct iommu_table *tbl = pe->table_group.tables[0];
3443 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
3444#ifdef CONFIG_IOMMU_API
3445 int64_t rc;
3446#endif
3447
3448 if (!weight)
3449 return;
3450
3451#ifdef CONFIG_IOMMU_API
3452 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
3453 if (rc)
3454 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
3455#endif
3456
3457 pnv_pci_ioda2_set_bypass(pe, false);
3458 if (pe->table_group.group) {
3459 iommu_group_put(pe->table_group.group);
3460 WARN_ON(pe->table_group.group);
3461 }
3462
3463 pnv_pci_ioda2_table_free_pages(tbl);
3464 iommu_free_table(tbl, "pnv");
3465}
3466
3467static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
3468 unsigned short win,
3469 unsigned int *map)
3470{
3471 struct pnv_phb *phb = pe->phb;
3472 int idx;
3473 int64_t rc;
3474
3475 for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
3476 if (map[idx] != pe->pe_number)
3477 continue;
3478
3479 if (win == OPAL_M64_WINDOW_TYPE)
3480 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3481 phb->ioda.reserved_pe_idx, win,
3482 idx / PNV_IODA1_M64_SEGS,
3483 idx % PNV_IODA1_M64_SEGS);
3484 else
3485 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3486 phb->ioda.reserved_pe_idx, win, 0, idx);
3487
3488 if (rc != OPAL_SUCCESS)
3489 pe_warn(pe, "Error %ld unmapping (%d) segment#%d\n",
3490 rc, win, idx);
3491
3492 map[idx] = IODA_INVALID_PE;
3493 }
3494}
3495
3496static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
3497{
3498 struct pnv_phb *phb = pe->phb;
3499
3500 if (phb->type == PNV_PHB_IODA1) {
3501 pnv_ioda_free_pe_seg(pe, OPAL_IO_WINDOW_TYPE,
3502 phb->ioda.io_segmap);
3503 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
3504 phb->ioda.m32_segmap);
3505 pnv_ioda_free_pe_seg(pe, OPAL_M64_WINDOW_TYPE,
3506 phb->ioda.m64_segmap);
3507 } else if (phb->type == PNV_PHB_IODA2) {
3508 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
3509 phb->ioda.m32_segmap);
3510 }
3511}
3512
3513static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
3514{
3515 struct pnv_phb *phb = pe->phb;
3516 struct pnv_ioda_pe *slave, *tmp;
3517
c5f7700b
GS
3518 list_del(&pe->list);
3519 switch (phb->type) {
3520 case PNV_PHB_IODA1:
3521 pnv_pci_ioda1_release_pe_dma(pe);
3522 break;
3523 case PNV_PHB_IODA2:
3524 pnv_pci_ioda2_release_pe_dma(pe);
3525 break;
3526 default:
3527 WARN_ON(1);
3528 }
3529
3530 pnv_ioda_release_pe_seg(pe);
3531 pnv_ioda_deconfigure_pe(pe->phb, pe);
b314427a
GS
3532
3533 /* Release slave PEs in the compound PE */
3534 if (pe->flags & PNV_IODA_PE_MASTER) {
3535 list_for_each_entry_safe(slave, tmp, &pe->slaves, list) {
3536 list_del(&slave->list);
3537 pnv_ioda_free_pe(slave);
3538 }
3539 }
3540
6eaed166
GS
3541 /*
3542 * The PE for root bus can be removed because of hotplug in EEH
3543 * recovery for fenced PHB error. We need to mark the PE dead so
3544 * that it can be populated again in PCI hot add path. The PE
3545 * shouldn't be destroyed as it's the global reserved resource.
3546 */
3547 if (phb->ioda.root_pe_populated &&
3548 phb->ioda.root_pe_idx == pe->pe_number)
3549 phb->ioda.root_pe_populated = false;
3550 else
3551 pnv_ioda_free_pe(pe);
c5f7700b
GS
3552}
3553
3554static void pnv_pci_release_device(struct pci_dev *pdev)
3555{
3556 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3557 struct pnv_phb *phb = hose->private_data;
3558 struct pci_dn *pdn = pci_get_pdn(pdev);
3559 struct pnv_ioda_pe *pe;
3560
3561 if (pdev->is_virtfn)
3562 return;
3563
3564 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
3565 return;
3566
29bf282d
GS
3567 /*
3568 * PCI hotplug can happen as part of EEH error recovery. The @pdn
3569 * isn't removed and added afterwards in this scenario. We should
3570 * set the PE number in @pdn to an invalid one. Otherwise, the PE's
3571 * device count is decreased on removing devices while failing to
3572 * be increased on adding devices. It leads to unbalanced PE's device
3573 * count and eventually make normal PCI hotplug path broken.
3574 */
c5f7700b 3575 pe = &phb->ioda.pe_array[pdn->pe_number];
29bf282d
GS
3576 pdn->pe_number = IODA_INVALID_PE;
3577
c5f7700b
GS
3578 WARN_ON(--pe->device_count < 0);
3579 if (pe->device_count == 0)
3580 pnv_ioda_release_pe(pe);
3581}
3582
7a8e6bbf 3583static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
73ed148a 3584{
7a8e6bbf
MN
3585 struct pnv_phb *phb = hose->private_data;
3586
d1a85eee 3587 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
73ed148a
BH
3588 OPAL_ASSERT_RESET);
3589}
3590
92ae0353 3591static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
cb4224c5
GS
3592 .dma_dev_setup = pnv_pci_dma_dev_setup,
3593 .dma_bus_setup = pnv_pci_dma_bus_setup,
92ae0353 3594#ifdef CONFIG_PCI_MSI
cb4224c5
GS
3595 .setup_msi_irqs = pnv_setup_msi_irqs,
3596 .teardown_msi_irqs = pnv_teardown_msi_irqs,
92ae0353 3597#endif
cb4224c5 3598 .enable_device_hook = pnv_pci_enable_device_hook,
c5f7700b 3599 .release_device = pnv_pci_release_device,
cb4224c5 3600 .window_alignment = pnv_pci_window_alignment,
ccd1c191 3601 .setup_bridge = pnv_pci_setup_bridge,
cb4224c5
GS
3602 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3603 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3604 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3605 .shutdown = pnv_pci_ioda_shutdown,
92ae0353
DA
3606};
3607
f9f83456
AK
3608static int pnv_npu_dma_set_mask(struct pci_dev *npdev, u64 dma_mask)
3609{
3610 dev_err_once(&npdev->dev,
3611 "%s operation unsupported for NVLink devices\n",
3612 __func__);
3613 return -EPERM;
3614}
3615
5d2aa710 3616static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
cb4224c5 3617 .dma_dev_setup = pnv_pci_dma_dev_setup,
5d2aa710 3618#ifdef CONFIG_PCI_MSI
cb4224c5
GS
3619 .setup_msi_irqs = pnv_setup_msi_irqs,
3620 .teardown_msi_irqs = pnv_teardown_msi_irqs,
5d2aa710 3621#endif
cb4224c5
GS
3622 .enable_device_hook = pnv_pci_enable_device_hook,
3623 .window_alignment = pnv_pci_window_alignment,
3624 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3625 .dma_set_mask = pnv_npu_dma_set_mask,
3626 .shutdown = pnv_pci_ioda_shutdown,
5d2aa710
AP
3627};
3628
4361b034
IM
3629#ifdef CONFIG_CXL_BASE
3630const struct pci_controller_ops pnv_cxl_cx4_ioda_controller_ops = {
3631 .dma_dev_setup = pnv_pci_dma_dev_setup,
3632 .dma_bus_setup = pnv_pci_dma_bus_setup,
a2f67d5e
IM
3633#ifdef CONFIG_PCI_MSI
3634 .setup_msi_irqs = pnv_cxl_cx4_setup_msi_irqs,
3635 .teardown_msi_irqs = pnv_cxl_cx4_teardown_msi_irqs,
3636#endif
4361b034
IM
3637 .enable_device_hook = pnv_cxl_enable_device_hook,
3638 .disable_device = pnv_cxl_disable_device,
3639 .release_device = pnv_pci_release_device,
3640 .window_alignment = pnv_pci_window_alignment,
3641 .setup_bridge = pnv_pci_setup_bridge,
3642 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3643 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3644 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3645 .shutdown = pnv_pci_ioda_shutdown,
3646};
3647#endif
3648
e51df2c1
AB
3649static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3650 u64 hub_id, int ioda_type)
184cd4a3
BH
3651{
3652 struct pci_controller *hose;
184cd4a3 3653 struct pnv_phb *phb;
2b923ed1
GS
3654 unsigned long size, m64map_off, m32map_off, pemap_off;
3655 unsigned long iomap_off = 0, dma32map_off = 0;
fd141d1a 3656 struct resource r;
c681b93c 3657 const __be64 *prop64;
3a1a4661 3658 const __be32 *prop32;
f1b7cc3e 3659 int len;
3fa23ff8 3660 unsigned int segno;
184cd4a3
BH
3661 u64 phb_id;
3662 void *aux;
3663 long rc;
3664
08a45b32
BH
3665 if (!of_device_is_available(np))
3666 return;
3667
9497a1c1
GS
3668 pr_info("Initializing %s PHB (%s)\n",
3669 pnv_phb_names[ioda_type], of_node_full_name(np));
184cd4a3
BH
3670
3671 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3672 if (!prop64) {
3673 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3674 return;
3675 }
3676 phb_id = be64_to_cpup(prop64);
3677 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3678
e39f223f 3679 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
58d714ec
GS
3680
3681 /* Allocate PCI controller */
58d714ec
GS
3682 phb->hose = hose = pcibios_alloc_controller(np);
3683 if (!phb->hose) {
3684 pr_err(" Can't allocate PCI controller for %s\n",
184cd4a3 3685 np->full_name);
e39f223f 3686 memblock_free(__pa(phb), sizeof(struct pnv_phb));
184cd4a3
BH
3687 return;
3688 }
3689
3690 spin_lock_init(&phb->lock);
f1b7cc3e
GS
3691 prop32 = of_get_property(np, "bus-range", &len);
3692 if (prop32 && len == 8) {
3a1a4661
BH
3693 hose->first_busno = be32_to_cpu(prop32[0]);
3694 hose->last_busno = be32_to_cpu(prop32[1]);
f1b7cc3e
GS
3695 } else {
3696 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3697 hose->first_busno = 0;
3698 hose->last_busno = 0xff;
3699 }
184cd4a3 3700 hose->private_data = phb;
e9cc17d4 3701 phb->hub_id = hub_id;
184cd4a3 3702 phb->opal_id = phb_id;
aa0c033f 3703 phb->type = ioda_type;
781a868f 3704 mutex_init(&phb->ioda.pe_alloc_mutex);
184cd4a3 3705
cee72d5b
BH
3706 /* Detect specific models for error handling */
3707 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3708 phb->model = PNV_PHB_MODEL_P7IOC;
f3d40c25 3709 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
aa0c033f 3710 phb->model = PNV_PHB_MODEL_PHB3;
5d2aa710
AP
3711 else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
3712 phb->model = PNV_PHB_MODEL_NPU;
4aa71ef1
AP
3713 else if (of_device_is_compatible(np, "ibm,power9-npu-pciex"))
3714 phb->model = PNV_PHB_MODEL_NPU2;
cee72d5b
BH
3715 else
3716 phb->model = PNV_PHB_MODEL_UNKNOWN;
3717
aa0c033f 3718 /* Parse 32-bit and IO ranges (if any) */
2f1ec02e 3719 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
184cd4a3 3720
aa0c033f 3721 /* Get registers */
fd141d1a
BH
3722 if (!of_address_to_resource(np, 0, &r)) {
3723 phb->regs_phys = r.start;
3724 phb->regs = ioremap(r.start, resource_size(&r));
3725 if (phb->regs == NULL)
3726 pr_err(" Failed to map registers !\n");
3727 }
577c8c88 3728
184cd4a3 3729 /* Initialize more IODA stuff */
92b8f137 3730 phb->ioda.total_pe_num = 1;
aa0c033f 3731 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
36954dc7 3732 if (prop32)
92b8f137 3733 phb->ioda.total_pe_num = be32_to_cpup(prop32);
36954dc7
GS
3734 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3735 if (prop32)
92b8f137 3736 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
262af557 3737
c127562a
GS
3738 /* Invalidate RID to PE# mapping */
3739 for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
3740 phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
3741
262af557
GC
3742 /* Parse 64-bit MMIO range */
3743 pnv_ioda_parse_m64_window(phb);
3744
184cd4a3 3745 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
aa0c033f 3746 /* FW Has already off top 64k of M32 space (MSI space) */
184cd4a3
BH
3747 phb->ioda.m32_size += 0x10000;
3748
92b8f137 3749 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
3fd47f06 3750 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
184cd4a3 3751 phb->ioda.io_size = hose->pci_io_size;
92b8f137 3752 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
184cd4a3
BH
3753 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3754
2b923ed1
GS
3755 /* Calculate how many 32-bit TCE segments we have */
3756 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
3757 PNV_IODA1_DMA32_SEGSIZE;
3758
c35d2a8c 3759 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
92a86756
AK
3760 size = _ALIGN_UP(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
3761 sizeof(unsigned long));
93289d8c
GS
3762 m64map_off = size;
3763 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
184cd4a3 3764 m32map_off = size;
92b8f137 3765 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
c35d2a8c
GS
3766 if (phb->type == PNV_PHB_IODA1) {
3767 iomap_off = size;
92b8f137 3768 size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
2b923ed1
GS
3769 dma32map_off = size;
3770 size += phb->ioda.dma32_count *
3771 sizeof(phb->ioda.dma32_segmap[0]);
c35d2a8c 3772 }
184cd4a3 3773 pemap_off = size;
92b8f137 3774 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
e39f223f 3775 aux = memblock_virt_alloc(size, 0);
184cd4a3 3776 phb->ioda.pe_alloc = aux;
93289d8c 3777 phb->ioda.m64_segmap = aux + m64map_off;
184cd4a3 3778 phb->ioda.m32_segmap = aux + m32map_off;
93289d8c
GS
3779 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
3780 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
3fa23ff8 3781 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
93289d8c 3782 }
3fa23ff8 3783 if (phb->type == PNV_PHB_IODA1) {
c35d2a8c 3784 phb->ioda.io_segmap = aux + iomap_off;
3fa23ff8
GS
3785 for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
3786 phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
2b923ed1
GS
3787
3788 phb->ioda.dma32_segmap = aux + dma32map_off;
3789 for (segno = 0; segno < phb->ioda.dma32_count; segno++)
3790 phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE;
3fa23ff8 3791 }
184cd4a3 3792 phb->ioda.pe_array = aux + pemap_off;
63803c39
GS
3793
3794 /*
3795 * Choose PE number for root bus, which shouldn't have
3796 * M64 resources consumed by its child devices. To pick
3797 * the PE number adjacent to the reserved one if possible.
3798 */
3799 pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
3800 if (phb->ioda.reserved_pe_idx == 0) {
3801 phb->ioda.root_pe_idx = 1;
3802 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3803 } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
3804 phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
3805 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3806 } else {
3807 phb->ioda.root_pe_idx = IODA_INVALID_PE;
3808 }
184cd4a3
BH
3809
3810 INIT_LIST_HEAD(&phb->ioda.pe_list);
781a868f 3811 mutex_init(&phb->ioda.pe_list_mutex);
184cd4a3
BH
3812
3813 /* Calculate how many 32-bit TCE segments we have */
2b923ed1 3814 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
acce971c 3815 PNV_IODA1_DMA32_SEGSIZE;
184cd4a3 3816
aa0c033f 3817#if 0 /* We should really do that ... */
184cd4a3
BH
3818 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3819 window_type,
3820 window_num,
3821 starting_real_address,
3822 starting_pci_address,
3823 segment_size);
3824#endif
3825
262af557 3826 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
92b8f137 3827 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
262af557
GC
3828 phb->ioda.m32_size, phb->ioda.m32_segsize);
3829 if (phb->ioda.m64_size)
3830 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3831 phb->ioda.m64_size, phb->ioda.m64_segsize);
3832 if (phb->ioda.io_size)
3833 pr_info(" IO: 0x%x [segment=0x%x]\n",
3834 phb->ioda.io_size, phb->ioda.io_segsize);
3835
184cd4a3 3836
184cd4a3 3837 phb->hose->ops = &pnv_pci_ops;
49dec922
GS
3838 phb->get_pe_state = pnv_ioda_get_pe_state;
3839 phb->freeze_pe = pnv_ioda_freeze_pe;
3840 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
184cd4a3 3841
184cd4a3
BH
3842 /* Setup MSI support */
3843 pnv_pci_init_ioda_msis(phb);
3844
c40a4210
GS
3845 /*
3846 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3847 * to let the PCI core do resource assignment. It's supposed
3848 * that the PCI core will do correct I/O and MMIO alignment
3849 * for the P2P bridge bars so that each PCI bus (excluding
3850 * the child P2P bridges) can form individual PE.
184cd4a3 3851 */
fb446ad0 3852 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
5d2aa710 3853
f9f83456 3854 if (phb->type == PNV_PHB_NPU) {
5d2aa710 3855 hose->controller_ops = pnv_npu_ioda_controller_ops;
f9f83456
AK
3856 } else {
3857 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
5d2aa710 3858 hose->controller_ops = pnv_pci_ioda_controller_ops;
f9f83456 3859 }
ad30cb99 3860
6e628c7d
WY
3861#ifdef CONFIG_PCI_IOV
3862 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
5350ab3f 3863 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
ad30cb99
ME
3864#endif
3865
c40a4210 3866 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
184cd4a3
BH
3867
3868 /* Reset IODA tables to a clean state */
d1a85eee 3869 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
184cd4a3 3870 if (rc)
f11fe552 3871 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
361f2a2a 3872
6060e9ea
AD
3873 /*
3874 * If we're running in kdump kernel, the previous kernel never
361f2a2a
GS
3875 * shutdown PCI devices correctly. We already got IODA table
3876 * cleaned out. So we have to issue PHB reset to stop all PCI
6060e9ea 3877 * transactions from previous kernel.
361f2a2a
GS
3878 */
3879 if (is_kdump_kernel()) {
3880 pr_info(" Issue PHB reset ...\n");
cadf364d
GS
3881 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3882 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
361f2a2a 3883 }
262af557 3884
9e9e8935
GS
3885 /* Remove M64 resource if we can't configure it successfully */
3886 if (!phb->init_m64 || phb->init_m64(phb))
262af557 3887 hose->mem_resources[1].flags = 0;
aa0c033f
GS
3888}
3889
67975005 3890void __init pnv_pci_init_ioda2_phb(struct device_node *np)
aa0c033f 3891{
e9cc17d4 3892 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
184cd4a3
BH
3893}
3894
5d2aa710
AP
3895void __init pnv_pci_init_npu_phb(struct device_node *np)
3896{
3897 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU);
3898}
3899
184cd4a3
BH
3900void __init pnv_pci_init_ioda_hub(struct device_node *np)
3901{
3902 struct device_node *phbn;
c681b93c 3903 const __be64 *prop64;
184cd4a3
BH
3904 u64 hub_id;
3905
3906 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3907
3908 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3909 if (!prop64) {
3910 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3911 return;
3912 }
3913 hub_id = be64_to_cpup(prop64);
3914 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3915
3916 /* Count child PHBs */
3917 for_each_child_of_node(np, phbn) {
3918 /* Look for IODA1 PHBs */
3919 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
e9cc17d4 3920 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
184cd4a3
BH
3921 }
3922}