blob: 58ca7df068ebe6c1f44f3ebae275b18171733cf2 [file] [log] [blame]
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001/*
2 * Support PCI/PCIe on PowerNV platforms
3 *
4 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +000012#undef DEBUG
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000013
14#include <linux/kernel.h>
15#include <linux/pci.h>
Gavin Shan361f2a22014-04-24 18:00:25 +100016#include <linux/crash_dump.h>
Gavin Shan37c367f2013-06-20 18:13:25 +080017#include <linux/debugfs.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000018#include <linux/delay.h>
19#include <linux/string.h>
20#include <linux/init.h>
21#include <linux/bootmem.h>
22#include <linux/irq.h>
23#include <linux/io.h>
24#include <linux/msi.h>
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +110025#include <linux/memblock.h>
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +100026#include <linux/iommu.h>
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +100027#include <linux/rculist.h>
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +100028#include <linux/sizes.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000029
30#include <asm/sections.h>
31#include <asm/io.h>
32#include <asm/prom.h>
33#include <asm/pci-bridge.h>
34#include <asm/machdep.h>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000035#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000036#include <asm/ppc-pci.h>
37#include <asm/opal.h>
38#include <asm/iommu.h>
39#include <asm/tce.h>
Gavin Shan137436c2013-04-25 19:20:59 +000040#include <asm/xics.h>
Gavin Shan37c367f2013-06-20 18:13:25 +080041#include <asm/debug.h>
Guo Chao262af552014-07-21 14:42:30 +100042#include <asm/firmware.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110043#include <asm/pnv-pci.h>
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100044#include <asm/mmzone.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110045
Michael Neulingec249dd2015-05-27 16:07:16 +100046#include <misc/cxl-base.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000047
48#include "powernv.h"
49#include "pci.h"
50
Gavin Shan99451552016-05-05 12:02:13 +100051#define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */
52#define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */
Gavin Shanacce9712016-05-03 15:41:33 +100053#define PNV_IODA1_DMA32_SEGSIZE 0x10000000
Wei Yang781a8682015-03-25 16:23:57 +080054
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +100055#define POWERNV_IOMMU_DEFAULT_LEVELS 1
56#define POWERNV_IOMMU_MAX_LEVELS 5
57
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100058static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
59
Alexey Kardashevskiy7d623e42016-04-29 18:55:21 +100060void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
Joe Perches6d31c2f2014-09-21 10:55:06 -070061 const char *fmt, ...)
62{
63 struct va_format vaf;
64 va_list args;
65 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000066
Joe Perches6d31c2f2014-09-21 10:55:06 -070067 va_start(args, fmt);
68
69 vaf.fmt = fmt;
70 vaf.va = &args;
71
Wei Yang781a8682015-03-25 16:23:57 +080072 if (pe->flags & PNV_IODA_PE_DEV)
Joe Perches6d31c2f2014-09-21 10:55:06 -070073 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
Wei Yang781a8682015-03-25 16:23:57 +080074 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Joe Perches6d31c2f2014-09-21 10:55:06 -070075 sprintf(pfix, "%04x:%02x ",
76 pci_domain_nr(pe->pbus), pe->pbus->number);
Wei Yang781a8682015-03-25 16:23:57 +080077#ifdef CONFIG_PCI_IOV
78 else if (pe->flags & PNV_IODA_PE_VF)
79 sprintf(pfix, "%04x:%02x:%2x.%d",
80 pci_domain_nr(pe->parent_dev->bus),
81 (pe->rid & 0xff00) >> 8,
82 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
83#endif /* CONFIG_PCI_IOV*/
Joe Perches6d31c2f2014-09-21 10:55:06 -070084
85 printk("%spci %s: [PE# %.3d] %pV",
86 level, pfix, pe->pe_number, &vaf);
87
88 va_end(args);
89}
90
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020091static bool pnv_iommu_bypass_disabled __read_mostly;
92
93static int __init iommu_setup(char *str)
94{
95 if (!str)
96 return -EINVAL;
97
98 while (*str) {
99 if (!strncmp(str, "nobypass", 8)) {
100 pnv_iommu_bypass_disabled = true;
101 pr_info("PowerNV: IOMMU bypass window disabled.\n");
102 break;
103 }
104 str += strcspn(str, ",");
105 if (*str == ',')
106 str++;
107 }
108
109 return 0;
110}
111early_param("iommu", iommu_setup);
112
Guo Chao262af552014-07-21 14:42:30 +1000113static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
114{
115 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
116 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
117}
118
Gavin Shan1e916772016-05-03 15:41:36 +1000119static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
120{
121 phb->ioda.pe_array[pe_no].phb = phb;
122 phb->ioda.pe_array[pe_no].pe_number = pe_no;
123
124 return &phb->ioda.pe_array[pe_no];
125}
126
Gavin Shan4b82ab12014-11-12 13:36:07 +1100127static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
128{
Gavin Shan92b8f132016-05-03 15:41:24 +1000129 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100130 pr_warn("%s: Invalid PE %d on PHB#%x\n",
131 __func__, pe_no, phb->hose->global_number);
132 return;
133 }
134
Gavin Shane9dc4d72015-06-19 12:26:16 +1000135 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
136 pr_debug("%s: PE %d was reserved on PHB#%x\n",
137 __func__, pe_no, phb->hose->global_number);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100138
Gavin Shan1e916772016-05-03 15:41:36 +1000139 pnv_ioda_init_pe(phb, pe_no);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100140}
141
Gavin Shan1e916772016-05-03 15:41:36 +1000142static struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000143{
Gavin Shan9fcd6f42016-05-20 16:41:30 +1000144 unsigned long pe = phb->ioda.total_pe_num - 1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000145
Gavin Shan9fcd6f42016-05-20 16:41:30 +1000146 for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
147 if (!test_and_set_bit(pe, phb->ioda.pe_alloc))
148 return pnv_ioda_init_pe(phb, pe);
149 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000150
Gavin Shan9fcd6f42016-05-20 16:41:30 +1000151 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000152}
153
Gavin Shan1e916772016-05-03 15:41:36 +1000154static void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000155{
Gavin Shan1e916772016-05-03 15:41:36 +1000156 struct pnv_phb *phb = pe->phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000157
Gavin Shan1e916772016-05-03 15:41:36 +1000158 WARN_ON(pe->pdev);
159
160 memset(pe, 0, sizeof(struct pnv_ioda_pe));
161 clear_bit(pe->pe_number, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000162}
163
Guo Chao262af552014-07-21 14:42:30 +1000164/* The default M64 BAR is shared by all PEs */
165static int pnv_ioda2_init_m64(struct pnv_phb *phb)
166{
167 const char *desc;
168 struct resource *r;
169 s64 rc;
170
171 /* Configure the default M64 BAR */
172 rc = opal_pci_set_phb_mem_window(phb->opal_id,
173 OPAL_M64_WINDOW_TYPE,
174 phb->ioda.m64_bar_idx,
175 phb->ioda.m64_base,
176 0, /* unused */
177 phb->ioda.m64_size);
178 if (rc != OPAL_SUCCESS) {
179 desc = "configuring";
180 goto fail;
181 }
182
183 /* Enable the default M64 BAR */
184 rc = opal_pci_phb_mmio_enable(phb->opal_id,
185 OPAL_M64_WINDOW_TYPE,
186 phb->ioda.m64_bar_idx,
187 OPAL_ENABLE_M64_SPLIT);
188 if (rc != OPAL_SUCCESS) {
189 desc = "enabling";
190 goto fail;
191 }
192
193 /* Mark the M64 BAR assigned */
194 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
195
196 /*
Gavin Shan63803c32016-05-20 16:41:32 +1000197 * Exclude the segments for reserved and root bus PE, which
198 * are first or last two PEs.
Guo Chao262af552014-07-21 14:42:30 +1000199 */
200 r = &phb->hose->mem_resources[1];
Gavin Shan92b8f132016-05-03 15:41:24 +1000201 if (phb->ioda.reserved_pe_idx == 0)
Gavin Shan63803c32016-05-20 16:41:32 +1000202 r->start += (2 * phb->ioda.m64_segsize);
Gavin Shan92b8f132016-05-03 15:41:24 +1000203 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Gavin Shan63803c32016-05-20 16:41:32 +1000204 r->end -= (2 * phb->ioda.m64_segsize);
Guo Chao262af552014-07-21 14:42:30 +1000205 else
206 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
Gavin Shan92b8f132016-05-03 15:41:24 +1000207 phb->ioda.reserved_pe_idx);
Guo Chao262af552014-07-21 14:42:30 +1000208
209 return 0;
210
211fail:
212 pr_warn(" Failure %lld %s M64 BAR#%d\n",
213 rc, desc, phb->ioda.m64_bar_idx);
214 opal_pci_phb_mmio_enable(phb->opal_id,
215 OPAL_M64_WINDOW_TYPE,
216 phb->ioda.m64_bar_idx,
217 OPAL_DISABLE_M64);
218 return -EIO;
219}
220
Gavin Shanc4306702016-05-03 15:41:30 +1000221static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
Gavin Shan96a2f922015-06-19 12:26:17 +1000222 unsigned long *pe_bitmap)
Guo Chao262af552014-07-21 14:42:30 +1000223{
Gavin Shan96a2f922015-06-19 12:26:17 +1000224 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
225 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000226 struct resource *r;
Gavin Shan96a2f922015-06-19 12:26:17 +1000227 resource_size_t base, sgsz, start, end;
228 int segno, i;
Guo Chao262af552014-07-21 14:42:30 +1000229
Gavin Shan96a2f922015-06-19 12:26:17 +1000230 base = phb->ioda.m64_base;
231 sgsz = phb->ioda.m64_segsize;
232 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
233 r = &pdev->resource[i];
234 if (!r->parent || !pnv_pci_is_mem_pref_64(r->flags))
235 continue;
Guo Chao262af552014-07-21 14:42:30 +1000236
Gavin Shan96a2f922015-06-19 12:26:17 +1000237 start = _ALIGN_DOWN(r->start - base, sgsz);
238 end = _ALIGN_UP(r->end - base, sgsz);
239 for (segno = start / sgsz; segno < end / sgsz; segno++) {
240 if (pe_bitmap)
241 set_bit(segno, pe_bitmap);
242 else
243 pnv_ioda_reserve_pe(phb, segno);
Guo Chao262af552014-07-21 14:42:30 +1000244 }
245 }
246}
247
Gavin Shan99451552016-05-05 12:02:13 +1000248static int pnv_ioda1_init_m64(struct pnv_phb *phb)
249{
250 struct resource *r;
251 int index;
252
253 /*
254 * There are 16 M64 BARs, each of which has 8 segments. So
255 * there are as many M64 segments as the maximum number of
256 * PEs, which is 128.
257 */
258 for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
259 unsigned long base, segsz = phb->ioda.m64_segsize;
260 int64_t rc;
261
262 base = phb->ioda.m64_base +
263 index * PNV_IODA1_M64_SEGS * segsz;
264 rc = opal_pci_set_phb_mem_window(phb->opal_id,
265 OPAL_M64_WINDOW_TYPE, index, base, 0,
266 PNV_IODA1_M64_SEGS * segsz);
267 if (rc != OPAL_SUCCESS) {
268 pr_warn(" Error %lld setting M64 PHB#%d-BAR#%d\n",
269 rc, phb->hose->global_number, index);
270 goto fail;
271 }
272
273 rc = opal_pci_phb_mmio_enable(phb->opal_id,
274 OPAL_M64_WINDOW_TYPE, index,
275 OPAL_ENABLE_M64_SPLIT);
276 if (rc != OPAL_SUCCESS) {
277 pr_warn(" Error %lld enabling M64 PHB#%d-BAR#%d\n",
278 rc, phb->hose->global_number, index);
279 goto fail;
280 }
281 }
282
283 /*
Gavin Shan63803c32016-05-20 16:41:32 +1000284 * Exclude the segments for reserved and root bus PE, which
285 * are first or last two PEs.
Gavin Shan99451552016-05-05 12:02:13 +1000286 */
287 r = &phb->hose->mem_resources[1];
288 if (phb->ioda.reserved_pe_idx == 0)
Gavin Shan63803c32016-05-20 16:41:32 +1000289 r->start += (2 * phb->ioda.m64_segsize);
Gavin Shan99451552016-05-05 12:02:13 +1000290 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Gavin Shan63803c32016-05-20 16:41:32 +1000291 r->end -= (2 * phb->ioda.m64_segsize);
Gavin Shan99451552016-05-05 12:02:13 +1000292 else
293 WARN(1, "Wrong reserved PE#%d on PHB#%d\n",
294 phb->ioda.reserved_pe_idx, phb->hose->global_number);
295
296 return 0;
297
298fail:
299 for ( ; index >= 0; index--)
300 opal_pci_phb_mmio_enable(phb->opal_id,
301 OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
302
303 return -EIO;
304}
305
Gavin Shanc4306702016-05-03 15:41:30 +1000306static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
307 unsigned long *pe_bitmap,
308 bool all)
Guo Chao262af552014-07-21 14:42:30 +1000309{
Guo Chao262af552014-07-21 14:42:30 +1000310 struct pci_dev *pdev;
Gavin Shan96a2f922015-06-19 12:26:17 +1000311
312 list_for_each_entry(pdev, &bus->devices, bus_list) {
Gavin Shanc4306702016-05-03 15:41:30 +1000313 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
Gavin Shan96a2f922015-06-19 12:26:17 +1000314
315 if (all && pdev->subordinate)
Gavin Shanc4306702016-05-03 15:41:30 +1000316 pnv_ioda_reserve_m64_pe(pdev->subordinate,
317 pe_bitmap, all);
Gavin Shan96a2f922015-06-19 12:26:17 +1000318 }
319}
320
Gavin Shan1e916772016-05-03 15:41:36 +1000321static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
Guo Chao262af552014-07-21 14:42:30 +1000322{
Gavin Shan26ba2482015-06-19 12:26:19 +1000323 struct pci_controller *hose = pci_bus_to_host(bus);
324 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000325 struct pnv_ioda_pe *master_pe, *pe;
326 unsigned long size, *pe_alloc;
Gavin Shan26ba2482015-06-19 12:26:19 +1000327 int i;
Guo Chao262af552014-07-21 14:42:30 +1000328
329 /* Root bus shouldn't use M64 */
330 if (pci_is_root_bus(bus))
Gavin Shan1e916772016-05-03 15:41:36 +1000331 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000332
Guo Chao262af552014-07-21 14:42:30 +1000333 /* Allocate bitmap */
Gavin Shan92b8f132016-05-03 15:41:24 +1000334 size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
Guo Chao262af552014-07-21 14:42:30 +1000335 pe_alloc = kzalloc(size, GFP_KERNEL);
336 if (!pe_alloc) {
337 pr_warn("%s: Out of memory !\n",
338 __func__);
Gavin Shan1e916772016-05-03 15:41:36 +1000339 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000340 }
341
Gavin Shan26ba2482015-06-19 12:26:19 +1000342 /* Figure out reserved PE numbers by the PE */
Gavin Shanc4306702016-05-03 15:41:30 +1000343 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
Guo Chao262af552014-07-21 14:42:30 +1000344
345 /*
346 * the current bus might not own M64 window and that's all
347 * contributed by its child buses. For the case, we needn't
348 * pick M64 dependent PE#.
349 */
Gavin Shan92b8f132016-05-03 15:41:24 +1000350 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
Guo Chao262af552014-07-21 14:42:30 +1000351 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000352 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000353 }
354
355 /*
356 * Figure out the master PE and put all slave PEs to master
357 * PE's list to form compound PE.
358 */
Guo Chao262af552014-07-21 14:42:30 +1000359 master_pe = NULL;
360 i = -1;
Gavin Shan92b8f132016-05-03 15:41:24 +1000361 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
362 phb->ioda.total_pe_num) {
Guo Chao262af552014-07-21 14:42:30 +1000363 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000364
Gavin Shan93289d82016-05-03 15:41:29 +1000365 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
Guo Chao262af552014-07-21 14:42:30 +1000366 if (!master_pe) {
367 pe->flags |= PNV_IODA_PE_MASTER;
368 INIT_LIST_HEAD(&pe->slaves);
369 master_pe = pe;
370 } else {
371 pe->flags |= PNV_IODA_PE_SLAVE;
372 pe->master = master_pe;
373 list_add_tail(&pe->list, &master_pe->slaves);
374 }
Gavin Shan99451552016-05-05 12:02:13 +1000375
376 /*
377 * P7IOC supports M64DT, which helps mapping M64 segment
378 * to one particular PE#. However, PHB3 has fixed mapping
379 * between M64 segment and PE#. In order to have same logic
380 * for P7IOC and PHB3, we enforce fixed mapping between M64
381 * segment and PE# on P7IOC.
382 */
383 if (phb->type == PNV_PHB_IODA1) {
384 int64_t rc;
385
386 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
387 pe->pe_number, OPAL_M64_WINDOW_TYPE,
388 pe->pe_number / PNV_IODA1_M64_SEGS,
389 pe->pe_number % PNV_IODA1_M64_SEGS);
390 if (rc != OPAL_SUCCESS)
391 pr_warn("%s: Error %lld mapping M64 for PHB#%d-PE#%d\n",
392 __func__, rc, phb->hose->global_number,
393 pe->pe_number);
394 }
Guo Chao262af552014-07-21 14:42:30 +1000395 }
396
397 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000398 return master_pe;
Guo Chao262af552014-07-21 14:42:30 +1000399}
400
401static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
402{
403 struct pci_controller *hose = phb->hose;
404 struct device_node *dn = hose->dn;
405 struct resource *res;
406 const u32 *r;
407 u64 pci_addr;
408
Gavin Shan99451552016-05-05 12:02:13 +1000409 if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
Gavin Shan1665c4a2014-11-12 13:36:04 +1100410 pr_info(" Not support M64 window\n");
411 return;
412 }
413
Stewart Smithe4d54f72015-12-09 17:18:20 +1100414 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
Guo Chao262af552014-07-21 14:42:30 +1000415 pr_info(" Firmware too old to support M64 window\n");
416 return;
417 }
418
419 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
420 if (!r) {
421 pr_info(" No <ibm,opal-m64-window> on %s\n",
422 dn->full_name);
423 return;
424 }
425
Guo Chao262af552014-07-21 14:42:30 +1000426 res = &hose->mem_resources[1];
Gavin Shane80c4e72015-10-22 12:03:08 +1100427 res->name = dn->full_name;
Guo Chao262af552014-07-21 14:42:30 +1000428 res->start = of_translate_address(dn, r + 2);
429 res->end = res->start + of_read_number(r + 4, 2) - 1;
430 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
431 pci_addr = of_read_number(r, 2);
432 hose->mem_offset[1] = res->start - pci_addr;
433
434 phb->ioda.m64_size = resource_size(res);
Gavin Shan92b8f132016-05-03 15:41:24 +1000435 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
Guo Chao262af552014-07-21 14:42:30 +1000436 phb->ioda.m64_base = pci_addr;
437
Wei Yange9863e62014-12-12 12:39:37 +0800438 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
439 res->start, res->end, pci_addr);
440
Guo Chao262af552014-07-21 14:42:30 +1000441 /* Use last M64 BAR to cover M64 window */
442 phb->ioda.m64_bar_idx = 15;
Gavin Shan99451552016-05-05 12:02:13 +1000443 if (phb->type == PNV_PHB_IODA1)
444 phb->init_m64 = pnv_ioda1_init_m64;
445 else
446 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shanc4306702016-05-03 15:41:30 +1000447 phb->reserve_m64_pe = pnv_ioda_reserve_m64_pe;
448 phb->pick_m64_pe = pnv_ioda_pick_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000449}
450
Gavin Shan49dec922014-07-21 14:42:33 +1000451static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
452{
453 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
454 struct pnv_ioda_pe *slave;
455 s64 rc;
456
457 /* Fetch master PE */
458 if (pe->flags & PNV_IODA_PE_SLAVE) {
459 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100460 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
461 return;
462
Gavin Shan49dec922014-07-21 14:42:33 +1000463 pe_no = pe->pe_number;
464 }
465
466 /* Freeze master PE */
467 rc = opal_pci_eeh_freeze_set(phb->opal_id,
468 pe_no,
469 OPAL_EEH_ACTION_SET_FREEZE_ALL);
470 if (rc != OPAL_SUCCESS) {
471 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
472 __func__, rc, phb->hose->global_number, pe_no);
473 return;
474 }
475
476 /* Freeze slave PEs */
477 if (!(pe->flags & PNV_IODA_PE_MASTER))
478 return;
479
480 list_for_each_entry(slave, &pe->slaves, list) {
481 rc = opal_pci_eeh_freeze_set(phb->opal_id,
482 slave->pe_number,
483 OPAL_EEH_ACTION_SET_FREEZE_ALL);
484 if (rc != OPAL_SUCCESS)
485 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
486 __func__, rc, phb->hose->global_number,
487 slave->pe_number);
488 }
489}
490
Anton Blancharde51df2c2014-08-20 08:55:18 +1000491static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000492{
493 struct pnv_ioda_pe *pe, *slave;
494 s64 rc;
495
496 /* Find master PE */
497 pe = &phb->ioda.pe_array[pe_no];
498 if (pe->flags & PNV_IODA_PE_SLAVE) {
499 pe = pe->master;
500 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
501 pe_no = pe->pe_number;
502 }
503
504 /* Clear frozen state for master PE */
505 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
506 if (rc != OPAL_SUCCESS) {
507 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
508 __func__, rc, opt, phb->hose->global_number, pe_no);
509 return -EIO;
510 }
511
512 if (!(pe->flags & PNV_IODA_PE_MASTER))
513 return 0;
514
515 /* Clear frozen state for slave PEs */
516 list_for_each_entry(slave, &pe->slaves, list) {
517 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
518 slave->pe_number,
519 opt);
520 if (rc != OPAL_SUCCESS) {
521 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
522 __func__, rc, opt, phb->hose->global_number,
523 slave->pe_number);
524 return -EIO;
525 }
526 }
527
528 return 0;
529}
530
531static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
532{
533 struct pnv_ioda_pe *slave, *pe;
534 u8 fstate, state;
535 __be16 pcierr;
536 s64 rc;
537
538 /* Sanity check on PE number */
Gavin Shan92b8f132016-05-03 15:41:24 +1000539 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
Gavin Shan49dec922014-07-21 14:42:33 +1000540 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
541
542 /*
543 * Fetch the master PE and the PE instance might be
544 * not initialized yet.
545 */
546 pe = &phb->ioda.pe_array[pe_no];
547 if (pe->flags & PNV_IODA_PE_SLAVE) {
548 pe = pe->master;
549 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
550 pe_no = pe->pe_number;
551 }
552
553 /* Check the master PE */
554 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
555 &state, &pcierr, NULL);
556 if (rc != OPAL_SUCCESS) {
557 pr_warn("%s: Failure %lld getting "
558 "PHB#%x-PE#%x state\n",
559 __func__, rc,
560 phb->hose->global_number, pe_no);
561 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
562 }
563
564 /* Check the slave PE */
565 if (!(pe->flags & PNV_IODA_PE_MASTER))
566 return state;
567
568 list_for_each_entry(slave, &pe->slaves, list) {
569 rc = opal_pci_eeh_freeze_status(phb->opal_id,
570 slave->pe_number,
571 &fstate,
572 &pcierr,
573 NULL);
574 if (rc != OPAL_SUCCESS) {
575 pr_warn("%s: Failure %lld getting "
576 "PHB#%x-PE#%x state\n",
577 __func__, rc,
578 phb->hose->global_number, slave->pe_number);
579 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
580 }
581
582 /*
583 * Override the result based on the ascending
584 * priority.
585 */
586 if (fstate > state)
587 state = fstate;
588 }
589
590 return state;
591}
592
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000593/* Currently those 2 are only used when MSIs are enabled, this will change
594 * but in the meantime, we need to protect them to avoid warnings
595 */
596#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800597static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000598{
599 struct pci_controller *hose = pci_bus_to_host(dev->bus);
600 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000601 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000602
603 if (!pdn)
604 return NULL;
605 if (pdn->pe_number == IODA_INVALID_PE)
606 return NULL;
607 return &phb->ioda.pe_array[pdn->pe_number];
608}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000609#endif /* CONFIG_PCI_MSI */
610
Gavin Shanb131a842014-11-12 13:36:08 +1100611static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
612 struct pnv_ioda_pe *parent,
613 struct pnv_ioda_pe *child,
614 bool is_add)
615{
616 const char *desc = is_add ? "adding" : "removing";
617 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
618 OPAL_REMOVE_PE_FROM_DOMAIN;
619 struct pnv_ioda_pe *slave;
620 long rc;
621
622 /* Parent PE affects child PE */
623 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
624 child->pe_number, op);
625 if (rc != OPAL_SUCCESS) {
626 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
627 rc, desc);
628 return -ENXIO;
629 }
630
631 if (!(child->flags & PNV_IODA_PE_MASTER))
632 return 0;
633
634 /* Compound case: parent PE affects slave PEs */
635 list_for_each_entry(slave, &child->slaves, list) {
636 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
637 slave->pe_number, op);
638 if (rc != OPAL_SUCCESS) {
639 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
640 rc, desc);
641 return -ENXIO;
642 }
643 }
644
645 return 0;
646}
647
648static int pnv_ioda_set_peltv(struct pnv_phb *phb,
649 struct pnv_ioda_pe *pe,
650 bool is_add)
651{
652 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800653 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100654 int ret;
655
656 /*
657 * Clear PE frozen state. If it's master PE, we need
658 * clear slave PE frozen state as well.
659 */
660 if (is_add) {
661 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
662 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
663 if (pe->flags & PNV_IODA_PE_MASTER) {
664 list_for_each_entry(slave, &pe->slaves, list)
665 opal_pci_eeh_freeze_clear(phb->opal_id,
666 slave->pe_number,
667 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
668 }
669 }
670
671 /*
672 * Associate PE in PELT. We need add the PE into the
673 * corresponding PELT-V as well. Otherwise, the error
674 * originated from the PE might contribute to other
675 * PEs.
676 */
677 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
678 if (ret)
679 return ret;
680
681 /* For compound PEs, any one affects all of them */
682 if (pe->flags & PNV_IODA_PE_MASTER) {
683 list_for_each_entry(slave, &pe->slaves, list) {
684 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
685 if (ret)
686 return ret;
687 }
688 }
689
690 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
691 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800692 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100693 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800694#ifdef CONFIG_PCI_IOV
695 else if (pe->flags & PNV_IODA_PE_VF)
Gavin Shan283e2d82015-06-22 13:45:47 +1000696 pdev = pe->parent_dev;
Wei Yang781a8682015-03-25 16:23:57 +0800697#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100698 while (pdev) {
699 struct pci_dn *pdn = pci_get_pdn(pdev);
700 struct pnv_ioda_pe *parent;
701
702 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
703 parent = &phb->ioda.pe_array[pdn->pe_number];
704 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
705 if (ret)
706 return ret;
707 }
708
709 pdev = pdev->bus->self;
710 }
711
712 return 0;
713}
714
Wei Yang781a8682015-03-25 16:23:57 +0800715static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
716{
717 struct pci_dev *parent;
718 uint8_t bcomp, dcomp, fcomp;
719 int64_t rc;
720 long rid_end, rid;
721
722 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
723 if (pe->pbus) {
724 int count;
725
726 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
727 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
728 parent = pe->pbus->self;
729 if (pe->flags & PNV_IODA_PE_BUS_ALL)
730 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
731 else
732 count = 1;
733
734 switch(count) {
735 case 1: bcomp = OpalPciBusAll; break;
736 case 2: bcomp = OpalPciBus7Bits; break;
737 case 4: bcomp = OpalPciBus6Bits; break;
738 case 8: bcomp = OpalPciBus5Bits; break;
739 case 16: bcomp = OpalPciBus4Bits; break;
740 case 32: bcomp = OpalPciBus3Bits; break;
741 default:
742 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
743 count);
744 /* Do an exact match only */
745 bcomp = OpalPciBusAll;
746 }
747 rid_end = pe->rid + (count << 8);
748 } else {
Gavin Shan93e01a52016-05-20 16:41:34 +1000749#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +0800750 if (pe->flags & PNV_IODA_PE_VF)
751 parent = pe->parent_dev;
752 else
Gavin Shan93e01a52016-05-20 16:41:34 +1000753#endif
Wei Yang781a8682015-03-25 16:23:57 +0800754 parent = pe->pdev->bus->self;
755 bcomp = OpalPciBusAll;
756 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
757 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
758 rid_end = pe->rid + 1;
759 }
760
761 /* Clear the reverse map */
762 for (rid = pe->rid; rid < rid_end; rid++)
Gavin Shanc1275622016-05-20 16:41:29 +1000763 phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
Wei Yang781a8682015-03-25 16:23:57 +0800764
765 /* Release from all parents PELT-V */
766 while (parent) {
767 struct pci_dn *pdn = pci_get_pdn(parent);
768 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
769 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
770 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
771 /* XXX What to do in case of error ? */
772 }
773 parent = parent->bus->self;
774 }
775
Gavin Shanf951e512015-06-23 17:01:13 +1000776 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
Wei Yang781a8682015-03-25 16:23:57 +0800777 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
778
779 /* Disassociate PE in PELT */
780 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
781 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
782 if (rc)
783 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
784 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
785 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
786 if (rc)
787 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
788
789 pe->pbus = NULL;
790 pe->pdev = NULL;
Gavin Shan93e01a52016-05-20 16:41:34 +1000791#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +0800792 pe->parent_dev = NULL;
Gavin Shan93e01a52016-05-20 16:41:34 +1000793#endif
Wei Yang781a8682015-03-25 16:23:57 +0800794
795 return 0;
796}
Wei Yang781a8682015-03-25 16:23:57 +0800797
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800798static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000799{
800 struct pci_dev *parent;
801 uint8_t bcomp, dcomp, fcomp;
802 long rc, rid_end, rid;
803
804 /* Bus validation ? */
805 if (pe->pbus) {
806 int count;
807
808 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
809 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
810 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000811 if (pe->flags & PNV_IODA_PE_BUS_ALL)
812 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
813 else
814 count = 1;
815
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000816 switch(count) {
817 case 1: bcomp = OpalPciBusAll; break;
818 case 2: bcomp = OpalPciBus7Bits; break;
819 case 4: bcomp = OpalPciBus6Bits; break;
820 case 8: bcomp = OpalPciBus5Bits; break;
821 case 16: bcomp = OpalPciBus4Bits; break;
822 case 32: bcomp = OpalPciBus3Bits; break;
823 default:
Wei Yang781a8682015-03-25 16:23:57 +0800824 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
825 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000826 /* Do an exact match only */
827 bcomp = OpalPciBusAll;
828 }
829 rid_end = pe->rid + (count << 8);
830 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800831#ifdef CONFIG_PCI_IOV
832 if (pe->flags & PNV_IODA_PE_VF)
833 parent = pe->parent_dev;
834 else
835#endif /* CONFIG_PCI_IOV */
836 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000837 bcomp = OpalPciBusAll;
838 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
839 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
840 rid_end = pe->rid + 1;
841 }
842
Gavin Shan631ad692013-11-04 16:32:46 +0800843 /*
844 * Associate PE in PELT. We need add the PE into the
845 * corresponding PELT-V as well. Otherwise, the error
846 * originated from the PE might contribute to other
847 * PEs.
848 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000849 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
850 bcomp, dcomp, fcomp, OPAL_MAP_PE);
851 if (rc) {
852 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
853 return -ENXIO;
854 }
Gavin Shan631ad692013-11-04 16:32:46 +0800855
Alistair Popple5d2aa712015-12-17 13:43:13 +1100856 /*
857 * Configure PELTV. NPUs don't have a PELTV table so skip
858 * configuration on them.
859 */
860 if (phb->type != PNV_PHB_NPU)
861 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000862
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000863 /* Setup reverse map */
864 for (rid = pe->rid; rid < rid_end; rid++)
865 phb->ioda.pe_rmap[rid] = pe->pe_number;
866
867 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100868 if (phb->type != PNV_PHB_IODA1) {
869 pe->mve_number = 0;
870 goto out;
871 }
872
873 pe->mve_number = pe->pe_number;
874 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
875 if (rc != OPAL_SUCCESS) {
876 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
877 rc, pe->mve_number);
878 pe->mve_number = -1;
879 } else {
880 rc = opal_pci_set_mve_enable(phb->opal_id,
881 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000882 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100883 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000884 rc, pe->mve_number);
885 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000886 }
Gavin Shan4773f762014-11-12 13:36:09 +1100887 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000888
Gavin Shan4773f762014-11-12 13:36:09 +1100889out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000890 return 0;
891}
892
Wei Yang781a8682015-03-25 16:23:57 +0800893#ifdef CONFIG_PCI_IOV
894static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
895{
896 struct pci_dn *pdn = pci_get_pdn(dev);
897 int i;
898 struct resource *res, res2;
899 resource_size_t size;
900 u16 num_vfs;
901
902 if (!dev->is_physfn)
903 return -EINVAL;
904
905 /*
906 * "offset" is in VFs. The M64 windows are sized so that when they
907 * are segmented, each segment is the same size as the IOV BAR.
908 * Each segment is in a separate PE, and the high order bits of the
909 * address are the PE number. Therefore, each VF's BAR is in a
910 * separate PE, and changing the IOV BAR start address changes the
911 * range of PEs the VFs are in.
912 */
913 num_vfs = pdn->num_vfs;
914 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
915 res = &dev->resource[i + PCI_IOV_RESOURCES];
916 if (!res->flags || !res->parent)
917 continue;
918
Wei Yang781a8682015-03-25 16:23:57 +0800919 /*
920 * The actual IOV BAR range is determined by the start address
921 * and the actual size for num_vfs VFs BAR. This check is to
922 * make sure that after shifting, the range will not overlap
923 * with another device.
924 */
925 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
926 res2.flags = res->flags;
927 res2.start = res->start + (size * offset);
928 res2.end = res2.start + (size * num_vfs) - 1;
929
930 if (res2.end > res->end) {
931 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
932 i, &res2, res, num_vfs, offset);
933 return -EBUSY;
934 }
935 }
936
937 /*
938 * After doing so, there would be a "hole" in the /proc/iomem when
939 * offset is a positive value. It looks like the device return some
940 * mmio back to the system, which actually no one could use it.
941 */
942 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
943 res = &dev->resource[i + PCI_IOV_RESOURCES];
944 if (!res->flags || !res->parent)
945 continue;
946
Wei Yang781a8682015-03-25 16:23:57 +0800947 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
948 res2 = *res;
949 res->start += size * offset;
950
Wei Yang74703cc2015-07-20 18:14:58 +0800951 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
952 i, &res2, res, (offset > 0) ? "En" : "Dis",
953 num_vfs, offset);
Wei Yang781a8682015-03-25 16:23:57 +0800954 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
955 }
956 return 0;
957}
958#endif /* CONFIG_PCI_IOV */
959
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800960static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000961{
962 struct pci_controller *hose = pci_bus_to_host(dev->bus);
963 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000964 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000965 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000966
967 if (!pdn) {
968 pr_err("%s: Device tree node not associated properly\n",
969 pci_name(dev));
970 return NULL;
971 }
972 if (pdn->pe_number != IODA_INVALID_PE)
973 return NULL;
974
Gavin Shan1e916772016-05-03 15:41:36 +1000975 pe = pnv_ioda_alloc_pe(phb);
976 if (!pe) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000977 pr_warning("%s: Not enough PE# available, disabling device\n",
978 pci_name(dev));
979 return NULL;
980 }
981
982 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
983 * pointer in the PE data structure, both should be destroyed at the
984 * same time. However, this needs to be looked at more closely again
985 * once we actually start removing things (Hotplug, SR-IOV, ...)
986 *
987 * At some point we want to remove the PDN completely anyways
988 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000989 pci_dev_get(dev);
990 pdn->pcidev = dev;
Gavin Shan1e916772016-05-03 15:41:36 +1000991 pdn->pe_number = pe->pe_number;
Alistair Popple5d2aa712015-12-17 13:43:13 +1100992 pe->flags = PNV_IODA_PE_DEV;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000993 pe->pdev = dev;
994 pe->pbus = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000995 pe->mve_number = -1;
996 pe->rid = dev->bus->number << 8 | pdn->devfn;
997
998 pe_info(pe, "Associated device to PE\n");
999
1000 if (pnv_ioda_configure_pe(phb, pe)) {
1001 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001002 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001003 pdn->pe_number = IODA_INVALID_PE;
1004 pe->pdev = NULL;
1005 pci_dev_put(dev);
1006 return NULL;
1007 }
1008
Alexey Kardashevskiy1d4e89c2016-05-12 15:47:10 +10001009 /* Put PE to the list */
1010 list_add_tail(&pe->list, &phb->ioda.pe_list);
1011
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001012 return pe;
1013}
1014
1015static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1016{
1017 struct pci_dev *dev;
1018
1019 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001020 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001021
1022 if (pdn == NULL) {
1023 pr_warn("%s: No device node associated with device !\n",
1024 pci_name(dev));
1025 continue;
1026 }
Gavin Shanccd1c192016-05-20 16:41:31 +10001027
1028 /*
1029 * In partial hotplug case, the PCI device might be still
1030 * associated with the PE and needn't attach it to the PE
1031 * again.
1032 */
1033 if (pdn->pe_number != IODA_INVALID_PE)
1034 continue;
1035
Alistair Popple94973b22015-12-17 13:43:11 +11001036 pdn->pcidev = dev;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001037 pdn->pe_number = pe->pe_number;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001038 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001039 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1040 }
1041}
1042
Gavin Shanfb446ad2012-08-20 03:49:14 +00001043/*
1044 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1045 * single PCI bus. Another one that contains the primary PCI bus and its
1046 * subordinate PCI devices and buses. The second type of PE is normally
1047 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1048 */
Gavin Shan1e916772016-05-03 15:41:36 +10001049static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001050{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001051 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001052 struct pnv_phb *phb = hose->private_data;
Gavin Shan1e916772016-05-03 15:41:36 +10001053 struct pnv_ioda_pe *pe = NULL;
Gavin Shanccd1c192016-05-20 16:41:31 +10001054 unsigned int pe_num;
1055
1056 /*
1057 * In partial hotplug case, the PE instance might be still alive.
1058 * We should reuse it instead of allocating a new one.
1059 */
1060 pe_num = phb->ioda.pe_rmap[bus->number << 8];
1061 if (pe_num != IODA_INVALID_PE) {
1062 pe = &phb->ioda.pe_array[pe_num];
1063 pnv_ioda_setup_same_PE(bus, pe);
1064 return NULL;
1065 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001066
Gavin Shan63803c32016-05-20 16:41:32 +10001067 /* PE number for root bus should have been reserved */
1068 if (pci_is_root_bus(bus) &&
1069 phb->ioda.root_pe_idx != IODA_INVALID_PE)
1070 pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
1071
Guo Chao262af552014-07-21 14:42:30 +10001072 /* Check if PE is determined by M64 */
Gavin Shan63803c32016-05-20 16:41:32 +10001073 if (!pe && phb->pick_m64_pe)
Gavin Shan1e916772016-05-03 15:41:36 +10001074 pe = phb->pick_m64_pe(bus, all);
Guo Chao262af552014-07-21 14:42:30 +10001075
1076 /* The PE number isn't pinned by M64 */
Gavin Shan1e916772016-05-03 15:41:36 +10001077 if (!pe)
1078 pe = pnv_ioda_alloc_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +10001079
Gavin Shan1e916772016-05-03 15:41:36 +10001080 if (!pe) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001081 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1082 __func__, pci_domain_nr(bus), bus->number);
Gavin Shan1e916772016-05-03 15:41:36 +10001083 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001084 }
1085
Guo Chao262af552014-07-21 14:42:30 +10001086 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001087 pe->pbus = bus;
1088 pe->pdev = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001089 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001090 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001091
Gavin Shanfb446ad2012-08-20 03:49:14 +00001092 if (all)
1093 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001094 bus->busn_res.start, bus->busn_res.end, pe->pe_number);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001095 else
1096 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001097 bus->busn_res.start, pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001098
1099 if (pnv_ioda_configure_pe(phb, pe)) {
1100 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001101 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001102 pe->pbus = NULL;
Gavin Shan1e916772016-05-03 15:41:36 +10001103 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001104 }
1105
1106 /* Associate it with all child devices */
1107 pnv_ioda_setup_same_PE(bus, pe);
1108
Gavin Shan7ebdf952012-08-20 03:49:15 +00001109 /* Put PE to the list */
1110 list_add_tail(&pe->list, &phb->ioda.pe_list);
Gavin Shan1e916772016-05-03 15:41:36 +10001111
1112 return pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001113}
1114
Alistair Poppleb5215492016-01-11 16:53:49 +11001115static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev)
Alistair Popple5d2aa712015-12-17 13:43:13 +11001116{
Alistair Poppleb5215492016-01-11 16:53:49 +11001117 int pe_num, found_pe = false, rc;
1118 long rid;
1119 struct pnv_ioda_pe *pe;
1120 struct pci_dev *gpu_pdev;
1121 struct pci_dn *npu_pdn;
1122 struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus);
1123 struct pnv_phb *phb = hose->private_data;
1124
1125 /*
1126 * Due to a hardware errata PE#0 on the NPU is reserved for
1127 * error handling. This means we only have three PEs remaining
1128 * which need to be assigned to four links, implying some
1129 * links must share PEs.
1130 *
1131 * To achieve this we assign PEs such that NPUs linking the
1132 * same GPU get assigned the same PE.
1133 */
1134 gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10001135 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
Alistair Poppleb5215492016-01-11 16:53:49 +11001136 pe = &phb->ioda.pe_array[pe_num];
1137 if (!pe->pdev)
1138 continue;
1139
1140 if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) {
1141 /*
1142 * This device has the same peer GPU so should
1143 * be assigned the same PE as the existing
1144 * peer NPU.
1145 */
1146 dev_info(&npu_pdev->dev,
1147 "Associating to existing PE %d\n", pe_num);
1148 pci_dev_get(npu_pdev);
1149 npu_pdn = pci_get_pdn(npu_pdev);
1150 rid = npu_pdev->bus->number << 8 | npu_pdn->devfn;
1151 npu_pdn->pcidev = npu_pdev;
1152 npu_pdn->pe_number = pe_num;
Alistair Poppleb5215492016-01-11 16:53:49 +11001153 phb->ioda.pe_rmap[rid] = pe->pe_number;
1154
1155 /* Map the PE to this link */
1156 rc = opal_pci_set_pe(phb->opal_id, pe_num, rid,
1157 OpalPciBusAll,
1158 OPAL_COMPARE_RID_DEVICE_NUMBER,
1159 OPAL_COMPARE_RID_FUNCTION_NUMBER,
1160 OPAL_MAP_PE);
1161 WARN_ON(rc != OPAL_SUCCESS);
1162 found_pe = true;
1163 break;
1164 }
1165 }
1166
1167 if (!found_pe)
1168 /*
1169 * Could not find an existing PE so allocate a new
1170 * one.
1171 */
1172 return pnv_ioda_setup_dev_PE(npu_pdev);
1173 else
1174 return pe;
1175}
1176
1177static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus)
1178{
Alistair Popple5d2aa712015-12-17 13:43:13 +11001179 struct pci_dev *pdev;
1180
1181 list_for_each_entry(pdev, &bus->devices, bus_list)
Alistair Poppleb5215492016-01-11 16:53:49 +11001182 pnv_ioda_setup_npu_PE(pdev);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001183}
1184
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001185static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001186{
1187 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001188 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001189
1190 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001191 phb = hose->private_data;
Alistair Popple08f48f32016-01-11 16:53:50 +11001192 if (phb->type == PNV_PHB_NPU) {
1193 /* PE#0 is needed for error reporting */
1194 pnv_ioda_reserve_pe(phb, 0);
Alistair Poppleb5215492016-01-11 16:53:49 +11001195 pnv_ioda_setup_npu_PEs(hose->bus);
Gavin Shanccd1c192016-05-20 16:41:31 +10001196 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001197 }
1198}
1199
Gavin Shana8b2f822015-03-25 16:23:52 +08001200#ifdef CONFIG_PCI_IOV
Wei Yangee8222f2015-10-22 09:22:16 +08001201static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001202{
1203 struct pci_bus *bus;
1204 struct pci_controller *hose;
1205 struct pnv_phb *phb;
1206 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001207 int i, j;
Wei Yangee8222f2015-10-22 09:22:16 +08001208 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001209
1210 bus = pdev->bus;
1211 hose = pci_bus_to_host(bus);
1212 phb = hose->private_data;
1213 pdn = pci_get_pdn(pdev);
1214
Wei Yangee8222f2015-10-22 09:22:16 +08001215 if (pdn->m64_single_mode)
1216 m64_bars = num_vfs;
1217 else
1218 m64_bars = 1;
1219
Wei Yang02639b02015-03-25 16:23:59 +08001220 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yangee8222f2015-10-22 09:22:16 +08001221 for (j = 0; j < m64_bars; j++) {
1222 if (pdn->m64_map[j][i] == IODA_INVALID_M64)
Wei Yang02639b02015-03-25 16:23:59 +08001223 continue;
1224 opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001225 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0);
1226 clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc);
1227 pdn->m64_map[j][i] = IODA_INVALID_M64;
Wei Yang02639b02015-03-25 16:23:59 +08001228 }
Wei Yang781a8682015-03-25 16:23:57 +08001229
Wei Yangee8222f2015-10-22 09:22:16 +08001230 kfree(pdn->m64_map);
Wei Yang781a8682015-03-25 16:23:57 +08001231 return 0;
1232}
1233
Wei Yang02639b02015-03-25 16:23:59 +08001234static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001235{
1236 struct pci_bus *bus;
1237 struct pci_controller *hose;
1238 struct pnv_phb *phb;
1239 struct pci_dn *pdn;
1240 unsigned int win;
1241 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001242 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001243 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001244 int total_vfs;
1245 resource_size_t size, start;
1246 int pe_num;
Wei Yangee8222f2015-10-22 09:22:16 +08001247 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001248
1249 bus = pdev->bus;
1250 hose = pci_bus_to_host(bus);
1251 phb = hose->private_data;
1252 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001253 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001254
Wei Yangee8222f2015-10-22 09:22:16 +08001255 if (pdn->m64_single_mode)
1256 m64_bars = num_vfs;
1257 else
1258 m64_bars = 1;
Wei Yang02639b02015-03-25 16:23:59 +08001259
Wei Yangee8222f2015-10-22 09:22:16 +08001260 pdn->m64_map = kmalloc(sizeof(*pdn->m64_map) * m64_bars, GFP_KERNEL);
1261 if (!pdn->m64_map)
1262 return -ENOMEM;
1263 /* Initialize the m64_map to IODA_INVALID_M64 */
1264 for (i = 0; i < m64_bars ; i++)
1265 for (j = 0; j < PCI_SRIOV_NUM_BARS; j++)
1266 pdn->m64_map[i][j] = IODA_INVALID_M64;
1267
Wei Yang781a8682015-03-25 16:23:57 +08001268
1269 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1270 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1271 if (!res->flags || !res->parent)
1272 continue;
1273
Wei Yangee8222f2015-10-22 09:22:16 +08001274 for (j = 0; j < m64_bars; j++) {
Wei Yang02639b02015-03-25 16:23:59 +08001275 do {
1276 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1277 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001278
Wei Yang02639b02015-03-25 16:23:59 +08001279 if (win >= phb->ioda.m64_bar_idx + 1)
1280 goto m64_failed;
1281 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001282
Wei Yangee8222f2015-10-22 09:22:16 +08001283 pdn->m64_map[j][i] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001284
Wei Yangee8222f2015-10-22 09:22:16 +08001285 if (pdn->m64_single_mode) {
Wei Yang02639b02015-03-25 16:23:59 +08001286 size = pci_iov_resource_size(pdev,
1287 PCI_IOV_RESOURCES + i);
Wei Yang02639b02015-03-25 16:23:59 +08001288 start = res->start + size * j;
1289 } else {
1290 size = resource_size(res);
1291 start = res->start;
1292 }
1293
1294 /* Map the M64 here */
Wei Yangee8222f2015-10-22 09:22:16 +08001295 if (pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001296 pe_num = pdn->pe_num_map[j];
Wei Yang02639b02015-03-25 16:23:59 +08001297 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1298 pe_num, OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001299 pdn->m64_map[j][i], 0);
Wei Yang02639b02015-03-25 16:23:59 +08001300 }
1301
1302 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001303 OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001304 pdn->m64_map[j][i],
Wei Yang02639b02015-03-25 16:23:59 +08001305 start,
Wei Yang781a8682015-03-25 16:23:57 +08001306 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001307 size);
Wei Yang781a8682015-03-25 16:23:57 +08001308
Wei Yang02639b02015-03-25 16:23:59 +08001309
1310 if (rc != OPAL_SUCCESS) {
1311 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1312 win, rc);
1313 goto m64_failed;
1314 }
1315
Wei Yangee8222f2015-10-22 09:22:16 +08001316 if (pdn->m64_single_mode)
Wei Yang02639b02015-03-25 16:23:59 +08001317 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001318 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2);
Wei Yang02639b02015-03-25 16:23:59 +08001319 else
1320 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001321 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1);
Wei Yang02639b02015-03-25 16:23:59 +08001322
1323 if (rc != OPAL_SUCCESS) {
1324 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1325 win, rc);
1326 goto m64_failed;
1327 }
Wei Yang781a8682015-03-25 16:23:57 +08001328 }
1329 }
1330 return 0;
1331
1332m64_failed:
Wei Yangee8222f2015-10-22 09:22:16 +08001333 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001334 return -EBUSY;
1335}
1336
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001337static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1338 int num);
1339static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1340
Wei Yang781a8682015-03-25 16:23:57 +08001341static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1342{
Wei Yang781a8682015-03-25 16:23:57 +08001343 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001344 int64_t rc;
1345
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001346 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001347 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001348 if (rc)
1349 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1350
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001351 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001352 if (pe->table_group.group) {
1353 iommu_group_put(pe->table_group.group);
1354 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001355 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001356 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001357 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001358}
1359
Wei Yangee8222f2015-10-22 09:22:16 +08001360static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
Wei Yang781a8682015-03-25 16:23:57 +08001361{
1362 struct pci_bus *bus;
1363 struct pci_controller *hose;
1364 struct pnv_phb *phb;
1365 struct pnv_ioda_pe *pe, *pe_n;
1366 struct pci_dn *pdn;
1367
1368 bus = pdev->bus;
1369 hose = pci_bus_to_host(bus);
1370 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001371 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001372
1373 if (!pdev->is_physfn)
1374 return;
1375
Wei Yang781a8682015-03-25 16:23:57 +08001376 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1377 if (pe->parent_dev != pdev)
1378 continue;
1379
1380 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1381
1382 /* Remove from list */
1383 mutex_lock(&phb->ioda.pe_list_mutex);
1384 list_del(&pe->list);
1385 mutex_unlock(&phb->ioda.pe_list_mutex);
1386
1387 pnv_ioda_deconfigure_pe(phb, pe);
1388
Gavin Shan1e916772016-05-03 15:41:36 +10001389 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001390 }
1391}
1392
1393void pnv_pci_sriov_disable(struct pci_dev *pdev)
1394{
1395 struct pci_bus *bus;
1396 struct pci_controller *hose;
1397 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001398 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001399 struct pci_dn *pdn;
1400 struct pci_sriov *iov;
Wei Yangbe283ee2015-10-22 09:22:19 +08001401 u16 num_vfs, i;
Wei Yang781a8682015-03-25 16:23:57 +08001402
1403 bus = pdev->bus;
1404 hose = pci_bus_to_host(bus);
1405 phb = hose->private_data;
1406 pdn = pci_get_pdn(pdev);
1407 iov = pdev->sriov;
1408 num_vfs = pdn->num_vfs;
1409
1410 /* Release VF PEs */
Wei Yangee8222f2015-10-22 09:22:16 +08001411 pnv_ioda_release_vf_PE(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001412
1413 if (phb->type == PNV_PHB_IODA2) {
Wei Yangee8222f2015-10-22 09:22:16 +08001414 if (!pdn->m64_single_mode)
Wei Yangbe283ee2015-10-22 09:22:19 +08001415 pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001416
1417 /* Release M64 windows */
Wei Yangee8222f2015-10-22 09:22:16 +08001418 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001419
1420 /* Release PE numbers */
Wei Yangbe283ee2015-10-22 09:22:19 +08001421 if (pdn->m64_single_mode) {
1422 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001423 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1424 continue;
1425
1426 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1427 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001428 }
1429 } else
1430 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1431 /* Releasing pe_num_map */
1432 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001433 }
1434}
1435
1436static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1437 struct pnv_ioda_pe *pe);
1438static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1439{
1440 struct pci_bus *bus;
1441 struct pci_controller *hose;
1442 struct pnv_phb *phb;
1443 struct pnv_ioda_pe *pe;
1444 int pe_num;
1445 u16 vf_index;
1446 struct pci_dn *pdn;
1447
1448 bus = pdev->bus;
1449 hose = pci_bus_to_host(bus);
1450 phb = hose->private_data;
1451 pdn = pci_get_pdn(pdev);
1452
1453 if (!pdev->is_physfn)
1454 return;
1455
1456 /* Reserve PE for each VF */
1457 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001458 if (pdn->m64_single_mode)
1459 pe_num = pdn->pe_num_map[vf_index];
1460 else
1461 pe_num = *pdn->pe_num_map + vf_index;
Wei Yang781a8682015-03-25 16:23:57 +08001462
1463 pe = &phb->ioda.pe_array[pe_num];
1464 pe->pe_number = pe_num;
1465 pe->phb = phb;
1466 pe->flags = PNV_IODA_PE_VF;
1467 pe->pbus = NULL;
1468 pe->parent_dev = pdev;
Wei Yang781a8682015-03-25 16:23:57 +08001469 pe->mve_number = -1;
1470 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1471 pci_iov_virtfn_devfn(pdev, vf_index);
1472
1473 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1474 hose->global_number, pdev->bus->number,
1475 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1476 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1477
1478 if (pnv_ioda_configure_pe(phb, pe)) {
1479 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001480 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001481 pe->pdev = NULL;
1482 continue;
1483 }
1484
Wei Yang781a8682015-03-25 16:23:57 +08001485 /* Put PE to the list */
1486 mutex_lock(&phb->ioda.pe_list_mutex);
1487 list_add_tail(&pe->list, &phb->ioda.pe_list);
1488 mutex_unlock(&phb->ioda.pe_list_mutex);
1489
1490 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1491 }
1492}
1493
1494int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1495{
1496 struct pci_bus *bus;
1497 struct pci_controller *hose;
1498 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001499 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001500 struct pci_dn *pdn;
1501 int ret;
Wei Yangbe283ee2015-10-22 09:22:19 +08001502 u16 i;
Wei Yang781a8682015-03-25 16:23:57 +08001503
1504 bus = pdev->bus;
1505 hose = pci_bus_to_host(bus);
1506 phb = hose->private_data;
1507 pdn = pci_get_pdn(pdev);
1508
1509 if (phb->type == PNV_PHB_IODA2) {
Wei Yangb0331852015-10-22 09:22:14 +08001510 if (!pdn->vfs_expanded) {
1511 dev_info(&pdev->dev, "don't support this SRIOV device"
1512 " with non 64bit-prefetchable IOV BAR\n");
1513 return -ENOSPC;
1514 }
1515
Wei Yangee8222f2015-10-22 09:22:16 +08001516 /*
1517 * When M64 BARs functions in Single PE mode, the number of VFs
1518 * could be enabled must be less than the number of M64 BARs.
1519 */
1520 if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) {
1521 dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n");
1522 return -EBUSY;
1523 }
1524
Wei Yangbe283ee2015-10-22 09:22:19 +08001525 /* Allocating pe_num_map */
1526 if (pdn->m64_single_mode)
1527 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map) * num_vfs,
1528 GFP_KERNEL);
1529 else
1530 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL);
1531
1532 if (!pdn->pe_num_map)
1533 return -ENOMEM;
1534
1535 if (pdn->m64_single_mode)
1536 for (i = 0; i < num_vfs; i++)
1537 pdn->pe_num_map[i] = IODA_INVALID_PE;
1538
Wei Yang781a8682015-03-25 16:23:57 +08001539 /* Calculate available PE for required VFs */
Wei Yangbe283ee2015-10-22 09:22:19 +08001540 if (pdn->m64_single_mode) {
1541 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001542 pe = pnv_ioda_alloc_pe(phb);
1543 if (!pe) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001544 ret = -EBUSY;
1545 goto m64_failed;
1546 }
Gavin Shan1e916772016-05-03 15:41:36 +10001547
1548 pdn->pe_num_map[i] = pe->pe_number;
Wei Yangbe283ee2015-10-22 09:22:19 +08001549 }
1550 } else {
1551 mutex_lock(&phb->ioda.pe_alloc_mutex);
1552 *pdn->pe_num_map = bitmap_find_next_zero_area(
Gavin Shan92b8f132016-05-03 15:41:24 +10001553 phb->ioda.pe_alloc, phb->ioda.total_pe_num,
Wei Yangbe283ee2015-10-22 09:22:19 +08001554 0, num_vfs, 0);
Gavin Shan92b8f132016-05-03 15:41:24 +10001555 if (*pdn->pe_num_map >= phb->ioda.total_pe_num) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001556 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1557 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1558 kfree(pdn->pe_num_map);
1559 return -EBUSY;
1560 }
1561 bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001562 mutex_unlock(&phb->ioda.pe_alloc_mutex);
Wei Yang781a8682015-03-25 16:23:57 +08001563 }
Wei Yang781a8682015-03-25 16:23:57 +08001564 pdn->num_vfs = num_vfs;
Wei Yang781a8682015-03-25 16:23:57 +08001565
1566 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001567 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001568 if (ret) {
1569 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1570 goto m64_failed;
1571 }
1572
1573 /*
1574 * When using one M64 BAR to map one IOV BAR, we need to shift
1575 * the IOV BAR according to the PE# allocated to the VFs.
1576 * Otherwise, the PE# for the VF will conflict with others.
1577 */
Wei Yangee8222f2015-10-22 09:22:16 +08001578 if (!pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001579 ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map);
Wei Yang02639b02015-03-25 16:23:59 +08001580 if (ret)
1581 goto m64_failed;
1582 }
Wei Yang781a8682015-03-25 16:23:57 +08001583 }
1584
1585 /* Setup VF PEs */
1586 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1587
1588 return 0;
1589
1590m64_failed:
Wei Yangbe283ee2015-10-22 09:22:19 +08001591 if (pdn->m64_single_mode) {
1592 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001593 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1594 continue;
1595
1596 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1597 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001598 }
1599 } else
1600 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1601
1602 /* Releasing pe_num_map */
1603 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001604
1605 return ret;
1606}
1607
Gavin Shana8b2f822015-03-25 16:23:52 +08001608int pcibios_sriov_disable(struct pci_dev *pdev)
1609{
Wei Yang781a8682015-03-25 16:23:57 +08001610 pnv_pci_sriov_disable(pdev);
1611
Gavin Shana8b2f822015-03-25 16:23:52 +08001612 /* Release PCI data */
1613 remove_dev_pci_data(pdev);
1614 return 0;
1615}
1616
1617int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1618{
1619 /* Allocate PCI data */
1620 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001621
Wei Yangee8222f2015-10-22 09:22:16 +08001622 return pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001623}
1624#endif /* CONFIG_PCI_IOV */
1625
Gavin Shan959c9bd2013-04-25 19:21:02 +00001626static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001627{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001628 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001629 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001630
Gavin Shan959c9bd2013-04-25 19:21:02 +00001631 /*
1632 * The function can be called while the PE#
1633 * hasn't been assigned. Do nothing for the
1634 * case.
1635 */
1636 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1637 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001638
Gavin Shan959c9bd2013-04-25 19:21:02 +00001639 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001640 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiy0e1ffef2015-08-27 16:01:16 +10001641 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001642 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001643 /*
1644 * Note: iommu_add_device() will fail here as
1645 * for physical PE: the device is already added by now;
1646 * for virtual PE: sysfs entries are not ready yet and
1647 * tce_iommu_bus_notifier will add the device to a group later.
1648 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001649}
1650
Daniel Axtens763d2d82015-04-28 15:12:07 +10001651static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001652{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001653 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1654 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001655 struct pci_dn *pdn = pci_get_pdn(pdev);
1656 struct pnv_ioda_pe *pe;
1657 uint64_t top;
1658 bool bypass = false;
1659
1660 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1661 return -ENODEV;;
1662
1663 pe = &phb->ioda.pe_array[pdn->pe_number];
1664 if (pe->tce_bypass_enabled) {
1665 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1666 bypass = (dma_mask >= top);
1667 }
1668
1669 if (bypass) {
1670 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1671 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001672 } else {
1673 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1674 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001675 }
Brian W Harta32305b2014-07-31 14:24:37 -05001676 *pdev->dev.dma_mask = dma_mask;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001677
1678 /* Update peer npu devices */
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10001679 pnv_npu_try_dma_set_bypass(pdev, bypass);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001680
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001681 return 0;
1682}
1683
Andrew Donnellan535229822015-08-07 13:45:54 +10001684static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001685{
Andrew Donnellan535229822015-08-07 13:45:54 +10001686 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1687 struct pnv_phb *phb = hose->private_data;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001688 struct pci_dn *pdn = pci_get_pdn(pdev);
1689 struct pnv_ioda_pe *pe;
1690 u64 end, mask;
1691
1692 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1693 return 0;
1694
1695 pe = &phb->ioda.pe_array[pdn->pe_number];
1696 if (!pe->tce_bypass_enabled)
1697 return __dma_get_required_mask(&pdev->dev);
1698
1699
1700 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1701 mask = 1ULL << (fls64(end) - 1);
1702 mask += mask - 1;
1703
1704 return mask;
1705}
1706
Gavin Shandff4a392014-07-15 17:00:55 +10001707static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001708 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001709{
1710 struct pci_dev *dev;
1711
1712 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001713 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001714 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001715 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001716
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001717 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001718 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001719 }
1720}
1721
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001722static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
1723 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001724{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001725 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1726 &tbl->it_group_list, struct iommu_table_group_link,
1727 next);
1728 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001729 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001730 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001731 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1732 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001733 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001734 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001735
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001736 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1737 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1738 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001739
1740 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1741 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001742 start <<= shift;
1743 end <<= shift;
1744 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001745 start |= tbl->it_busno;
1746 end |= tbl->it_busno;
1747 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1748 /* p7ioc-style invalidation, 2 TCEs per write */
1749 start |= (1ull << 63);
1750 end |= (1ull << 63);
1751 inc = 16;
1752 } else {
1753 /* Default (older HW) */
1754 inc = 128;
1755 }
1756
1757 end |= inc - 1; /* round up end to be different than start */
1758
1759 mb(); /* Ensure above stores are visible */
1760 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001761 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001762 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001763 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001764 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001765 start += inc;
1766 }
1767
1768 /*
1769 * The iommu layer will do another mb() for us on build()
1770 * and we don't care on free()
1771 */
1772}
1773
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001774static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1775 long npages, unsigned long uaddr,
1776 enum dma_data_direction direction,
1777 struct dma_attrs *attrs)
1778{
1779 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1780 attrs);
1781
1782 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1783 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1784
1785 return ret;
1786}
1787
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001788#ifdef CONFIG_IOMMU_API
1789static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1790 unsigned long *hpa, enum dma_data_direction *direction)
1791{
1792 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1793
1794 if (!ret && (tbl->it_type &
1795 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1796 pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);
1797
1798 return ret;
1799}
1800#endif
1801
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001802static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1803 long npages)
1804{
1805 pnv_tce_free(tbl, index, npages);
1806
1807 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1808 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1809}
1810
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001811static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001812 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001813#ifdef CONFIG_IOMMU_API
1814 .exchange = pnv_ioda1_tce_xchg,
1815#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001816 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001817 .get = pnv_tce_get,
1818};
1819
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001820#define TCE_KILL_INVAL_ALL PPC_BIT(0)
Alexey Kardashevskiybef92532016-04-29 18:55:17 +10001821#define TCE_KILL_INVAL_PE PPC_BIT(1)
1822#define TCE_KILL_INVAL_TCE PPC_BIT(2)
1823
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001824void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_phb *phb, bool rm)
1825{
1826 const unsigned long val = TCE_KILL_INVAL_ALL;
1827
1828 mb(); /* Ensure previous TCE table stores are visible */
1829 if (rm)
1830 __raw_rm_writeq(cpu_to_be64(val),
1831 (__be64 __iomem *)
1832 phb->ioda.tce_inval_reg_phys);
1833 else
1834 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1835}
1836
Alexey Kardashevskiya7cf13c2016-04-29 18:55:16 +10001837static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001838{
1839 /* 01xb - invalidate TCEs that match the specified PE# */
Alexey Kardashevskiybef92532016-04-29 18:55:17 +10001840 unsigned long val = TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001841 struct pnv_phb *phb = pe->phb;
1842
1843 if (!phb->ioda.tce_inval_reg)
1844 return;
1845
1846 mb(); /* Ensure above stores are visible */
1847 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1848}
1849
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001850static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
1851 __be64 __iomem *invalidate, unsigned shift,
1852 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001853{
1854 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001855
1856 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiybef92532016-04-29 18:55:17 +10001857 start = TCE_KILL_INVAL_TCE;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001858 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001859 end = start;
1860
1861 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001862 start |= (index << shift);
1863 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001864 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001865 mb();
1866
1867 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001868 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001869 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001870 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001871 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001872 start += inc;
1873 }
1874}
1875
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001876static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1877 unsigned long index, unsigned long npages, bool rm)
1878{
1879 struct iommu_table_group_link *tgl;
1880
1881 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
1882 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1883 struct pnv_ioda_pe, table_group);
1884 __be64 __iomem *invalidate = rm ?
1885 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1886 pe->phb->ioda.tce_inval_reg;
1887
Alexey Kardashevskiy85674862016-04-29 18:55:23 +10001888 if (pe->phb->type == PNV_PHB_NPU) {
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001889 /*
1890 * The NVLink hardware does not support TCE kill
1891 * per TCE entry so we have to invalidate
1892 * the entire cache for it.
1893 */
Alexey Kardashevskiy85674862016-04-29 18:55:23 +10001894 pnv_pci_ioda2_tce_invalidate_entire(pe->phb, rm);
1895 continue;
1896 }
1897 pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
1898 invalidate, tbl->it_page_shift,
1899 index, npages);
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001900 }
1901}
1902
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001903static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1904 long npages, unsigned long uaddr,
1905 enum dma_data_direction direction,
1906 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001907{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001908 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1909 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001910
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001911 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1912 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1913
1914 return ret;
1915}
1916
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001917#ifdef CONFIG_IOMMU_API
1918static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1919 unsigned long *hpa, enum dma_data_direction *direction)
1920{
1921 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1922
1923 if (!ret && (tbl->it_type &
1924 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1925 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1926
1927 return ret;
1928}
1929#endif
1930
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001931static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1932 long npages)
1933{
1934 pnv_tce_free(tbl, index, npages);
1935
1936 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1937 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001938}
1939
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001940static void pnv_ioda2_table_free(struct iommu_table *tbl)
1941{
1942 pnv_pci_ioda2_table_free_pages(tbl);
1943 iommu_free_table(tbl, "pnv");
1944}
1945
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001946static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001947 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001948#ifdef CONFIG_IOMMU_API
1949 .exchange = pnv_ioda2_tce_xchg,
1950#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001951 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001952 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001953 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001954};
1955
Gavin Shan801846d2016-05-03 15:41:34 +10001956static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data)
1957{
1958 unsigned int *weight = (unsigned int *)data;
1959
1960 /* This is quite simplistic. The "base" weight of a device
1961 * is 10. 0 means no DMA is to be accounted for it.
1962 */
1963 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
1964 return 0;
1965
1966 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
1967 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
1968 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
1969 *weight += 3;
1970 else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
1971 *weight += 15;
1972 else
1973 *weight += 10;
1974
1975 return 0;
1976}
1977
1978static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe)
1979{
1980 unsigned int weight = 0;
1981
1982 /* SRIOV VF has same DMA32 weight as its PF */
1983#ifdef CONFIG_PCI_IOV
1984 if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) {
1985 pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight);
1986 return weight;
1987 }
1988#endif
1989
1990 if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) {
1991 pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight);
1992 } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) {
1993 struct pci_dev *pdev;
1994
1995 list_for_each_entry(pdev, &pe->pbus->devices, bus_list)
1996 pnv_pci_ioda_dev_dma_weight(pdev, &weight);
1997 } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) {
1998 pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight);
1999 }
2000
2001 return weight;
2002}
2003
Gavin Shanb30d9362016-05-03 15:41:32 +10002004static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
Gavin Shan2b923ed2016-05-05 12:04:16 +10002005 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002006{
2007
2008 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002009 struct iommu_table *tbl;
Gavin Shan2b923ed2016-05-05 12:04:16 +10002010 unsigned int weight, total_weight = 0;
2011 unsigned int tce32_segsz, base, segs, avail, i;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002012 int64_t rc;
2013 void *addr;
2014
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002015 /* XXX FIXME: Handle 64-bit only DMA devices */
2016 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
2017 /* XXX FIXME: Allocate multi-level tables on PHB3 */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002018 weight = pnv_pci_ioda_pe_dma_weight(pe);
2019 if (!weight)
2020 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002021
Gavin Shan2b923ed2016-05-05 12:04:16 +10002022 pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight,
2023 &total_weight);
2024 segs = (weight * phb->ioda.dma32_count) / total_weight;
2025 if (!segs)
2026 segs = 1;
2027
2028 /*
2029 * Allocate contiguous DMA32 segments. We begin with the expected
2030 * number of segments. With one more attempt, the number of DMA32
2031 * segments to be allocated is decreased by one until one segment
2032 * is allocated successfully.
2033 */
2034 do {
2035 for (base = 0; base <= phb->ioda.dma32_count - segs; base++) {
2036 for (avail = 0, i = base; i < base + segs; i++) {
2037 if (phb->ioda.dma32_segmap[i] ==
2038 IODA_INVALID_PE)
2039 avail++;
2040 }
2041
2042 if (avail == segs)
2043 goto found;
2044 }
2045 } while (--segs);
2046
2047 if (!segs) {
2048 pe_warn(pe, "No available DMA32 segments\n");
2049 return;
2050 }
2051
2052found:
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002053 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002054 iommu_register_group(&pe->table_group, phb->hose->global_number,
2055 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002056 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002057
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002058 /* Grab a 32-bit TCE table */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002059 pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
2060 weight, total_weight, base, segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002061 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
Gavin Shanacce9712016-05-03 15:41:33 +10002062 base * PNV_IODA1_DMA32_SEGSIZE,
2063 (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002064
2065 /* XXX Currently, we allocate one big contiguous table for the
2066 * TCEs. We only really need one chunk per 256M of TCE space
2067 * (ie per segment) but that's an optimization for later, it
2068 * requires some added smarts with our get/put_tce implementation
Gavin Shanacce9712016-05-03 15:41:33 +10002069 *
2070 * Each TCE page is 4KB in size and each TCE entry occupies 8
2071 * bytes
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002072 */
Gavin Shanacce9712016-05-03 15:41:33 +10002073 tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002074 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
Gavin Shanacce9712016-05-03 15:41:33 +10002075 get_order(tce32_segsz * segs));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002076 if (!tce_mem) {
2077 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
2078 goto fail;
2079 }
2080 addr = page_address(tce_mem);
Gavin Shanacce9712016-05-03 15:41:33 +10002081 memset(addr, 0, tce32_segsz * segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002082
2083 /* Configure HW */
2084 for (i = 0; i < segs; i++) {
2085 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2086 pe->pe_number,
2087 base + i, 1,
Gavin Shanacce9712016-05-03 15:41:33 +10002088 __pa(addr) + tce32_segsz * i,
2089 tce32_segsz, IOMMU_PAGE_SIZE_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002090 if (rc) {
2091 pe_err(pe, " Failed to configure 32-bit TCE table,"
2092 " err %ld\n", rc);
2093 goto fail;
2094 }
2095 }
2096
Gavin Shan2b923ed2016-05-05 12:04:16 +10002097 /* Setup DMA32 segment mapping */
2098 for (i = base; i < base + segs; i++)
2099 phb->ioda.dma32_segmap[i] = pe->pe_number;
2100
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002101 /* Setup linux iommu table */
Gavin Shanacce9712016-05-03 15:41:33 +10002102 pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs,
2103 base * PNV_IODA1_DMA32_SEGSIZE,
2104 IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002105
2106 /* OPAL variant of P7IOC SW invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002107 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10002108 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
2109 TCE_PCI_SWINV_FREE |
2110 TCE_PCI_SWINV_PAIR);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002111
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002112 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002113 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
2114 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002115 iommu_init_table(tbl, phb->hose->node);
2116
Wei Yang781a8682015-03-25 16:23:57 +08002117 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002118 /*
2119 * Setting table base here only for carrying iommu_group
2120 * further down to let iommu_add_device() do the job.
2121 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2122 */
2123 set_iommu_table_base(&pe->pdev->dev, tbl);
2124 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002125 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002126 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10002127
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002128 return;
2129 fail:
2130 /* XXX Failure: Try to fallback to 64-bit only ? */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002131 if (tce_mem)
Gavin Shanacce9712016-05-03 15:41:33 +10002132 __free_pages(tce_mem, get_order(tce32_segsz * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002133 if (tbl) {
2134 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
2135 iommu_free_table(tbl, "pnv");
2136 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002137}
2138
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002139static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
2140 int num, struct iommu_table *tbl)
2141{
2142 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2143 table_group);
2144 struct pnv_phb *phb = pe->phb;
2145 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002146 const unsigned long size = tbl->it_indirect_levels ?
2147 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002148 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
2149 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
2150
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002151 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002152 start_addr, start_addr + win_size - 1,
2153 IOMMU_PAGE_SIZE(tbl));
2154
2155 /*
2156 * Map TCE table through TVT. The TVE index is the PE number
2157 * shifted by 1 bit for 32-bits DMA space.
2158 */
2159 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2160 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002161 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002162 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002163 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002164 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002165 IOMMU_PAGE_SIZE(tbl));
2166 if (rc) {
2167 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
2168 return rc;
2169 }
2170
2171 pnv_pci_link_table_and_group(phb->hose->node, num,
2172 tbl, &pe->table_group);
Alexey Kardashevskiya7cf13c2016-04-29 18:55:16 +10002173 pnv_pci_ioda2_tce_invalidate_pe(pe);
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002174
2175 return 0;
2176}
2177
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002178static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002179{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002180 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2181 int64_t rc;
2182
2183 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2184 if (enable) {
2185 phys_addr_t top = memblock_end_of_DRAM();
2186
2187 top = roundup_pow_of_two(top);
2188 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2189 pe->pe_number,
2190 window_id,
2191 pe->tce_bypass_base,
2192 top);
2193 } else {
2194 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2195 pe->pe_number,
2196 window_id,
2197 pe->tce_bypass_base,
2198 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002199 }
2200 if (rc)
2201 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2202 else
2203 pe->tce_bypass_enabled = enable;
2204}
2205
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002206static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2207 __u32 page_shift, __u64 window_size, __u32 levels,
2208 struct iommu_table *tbl);
2209
2210static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2211 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2212 struct iommu_table **ptbl)
2213{
2214 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2215 table_group);
2216 int nid = pe->phb->hose->node;
2217 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2218 long ret;
2219 struct iommu_table *tbl;
2220
2221 tbl = pnv_pci_table_alloc(nid);
2222 if (!tbl)
2223 return -ENOMEM;
2224
2225 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2226 bus_offset, page_shift, window_size,
2227 levels, tbl);
2228 if (ret) {
2229 iommu_free_table(tbl, "pnv");
2230 return ret;
2231 }
2232
2233 tbl->it_ops = &pnv_ioda2_iommu_ops;
2234 if (pe->phb->ioda.tce_inval_reg)
2235 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2236
2237 *ptbl = tbl;
2238
2239 return 0;
2240}
2241
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002242static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2243{
2244 struct iommu_table *tbl = NULL;
2245 long rc;
2246
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002247 /*
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002248 * crashkernel= specifies the kdump kernel's maximum memory at
2249 * some offset and there is no guaranteed the result is a power
2250 * of 2, which will cause errors later.
2251 */
2252 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
2253
2254 /*
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002255 * In memory constrained environments, e.g. kdump kernel, the
2256 * DMA window can be larger than available memory, which will
2257 * cause errors later.
2258 */
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002259 const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002260
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002261 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2262 IOMMU_PAGE_SHIFT_4K,
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002263 window_size,
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002264 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2265 if (rc) {
2266 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2267 rc);
2268 return rc;
2269 }
2270
2271 iommu_init_table(tbl, pe->phb->hose->node);
2272
2273 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2274 if (rc) {
2275 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2276 rc);
2277 pnv_ioda2_table_free(tbl);
2278 return rc;
2279 }
2280
2281 if (!pnv_iommu_bypass_disabled)
2282 pnv_pci_ioda2_set_bypass(pe, true);
2283
2284 /* OPAL variant of PHB3 invalidated TCEs */
2285 if (pe->phb->ioda.tce_inval_reg)
2286 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2287
2288 /*
2289 * Setting table base here only for carrying iommu_group
2290 * further down to let iommu_add_device() do the job.
2291 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2292 */
2293 if (pe->flags & PNV_IODA_PE_DEV)
2294 set_iommu_table_base(&pe->pdev->dev, tbl);
2295
2296 return 0;
2297}
2298
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002299#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2300static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2301 int num)
2302{
2303 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2304 table_group);
2305 struct pnv_phb *phb = pe->phb;
2306 long ret;
2307
2308 pe_info(pe, "Removing DMA window #%d\n", num);
2309
2310 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2311 (pe->pe_number << 1) + num,
2312 0/* levels */, 0/* table address */,
2313 0/* table size */, 0/* page size */);
2314 if (ret)
2315 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2316 else
Alexey Kardashevskiya7cf13c2016-04-29 18:55:16 +10002317 pnv_pci_ioda2_tce_invalidate_pe(pe);
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002318
2319 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2320
2321 return ret;
2322}
2323#endif
2324
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002325#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002326static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2327 __u64 window_size, __u32 levels)
2328{
2329 unsigned long bytes = 0;
2330 const unsigned window_shift = ilog2(window_size);
2331 unsigned entries_shift = window_shift - page_shift;
2332 unsigned table_shift = entries_shift + 3;
2333 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2334 unsigned long direct_table_size;
2335
2336 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2337 (window_size > memory_hotplug_max()) ||
2338 !is_power_of_2(window_size))
2339 return 0;
2340
2341 /* Calculate a direct table size from window_size and levels */
2342 entries_shift = (entries_shift + levels - 1) / levels;
2343 table_shift = entries_shift + 3;
2344 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2345 direct_table_size = 1UL << table_shift;
2346
2347 for ( ; levels; --levels) {
2348 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2349
2350 tce_table_size /= direct_table_size;
2351 tce_table_size <<= 3;
2352 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2353 }
2354
2355 return bytes;
2356}
2357
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002358static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002359{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002360 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2361 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002362 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2363 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002364
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002365 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002366 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2367 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002368}
2369
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002370static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2371{
2372 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2373 table_group);
2374
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002375 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002376}
2377
2378static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002379 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002380 .create_table = pnv_pci_ioda2_create_table,
2381 .set_window = pnv_pci_ioda2_set_window,
2382 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002383 .take_ownership = pnv_ioda2_take_ownership,
2384 .release_ownership = pnv_ioda2_release_ownership,
2385};
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002386
2387static int gpe_table_group_to_npe_cb(struct device *dev, void *opaque)
2388{
2389 struct pci_controller *hose;
2390 struct pnv_phb *phb;
2391 struct pnv_ioda_pe **ptmppe = opaque;
2392 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
2393 struct pci_dn *pdn = pci_get_pdn(pdev);
2394
2395 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2396 return 0;
2397
2398 hose = pci_bus_to_host(pdev->bus);
2399 phb = hose->private_data;
2400 if (phb->type != PNV_PHB_NPU)
2401 return 0;
2402
2403 *ptmppe = &phb->ioda.pe_array[pdn->pe_number];
2404
2405 return 1;
2406}
2407
2408/*
2409 * This returns PE of associated NPU.
2410 * This assumes that NPU is in the same IOMMU group with GPU and there is
2411 * no other PEs.
2412 */
2413static struct pnv_ioda_pe *gpe_table_group_to_npe(
2414 struct iommu_table_group *table_group)
2415{
2416 struct pnv_ioda_pe *npe = NULL;
2417 int ret = iommu_group_for_each_dev(table_group->group, &npe,
2418 gpe_table_group_to_npe_cb);
2419
2420 BUG_ON(!ret || !npe);
2421
2422 return npe;
2423}
2424
2425static long pnv_pci_ioda2_npu_set_window(struct iommu_table_group *table_group,
2426 int num, struct iommu_table *tbl)
2427{
2428 long ret = pnv_pci_ioda2_set_window(table_group, num, tbl);
2429
2430 if (ret)
2431 return ret;
2432
2433 ret = pnv_npu_set_window(gpe_table_group_to_npe(table_group), num, tbl);
2434 if (ret)
2435 pnv_pci_ioda2_unset_window(table_group, num);
2436
2437 return ret;
2438}
2439
2440static long pnv_pci_ioda2_npu_unset_window(
2441 struct iommu_table_group *table_group,
2442 int num)
2443{
2444 long ret = pnv_pci_ioda2_unset_window(table_group, num);
2445
2446 if (ret)
2447 return ret;
2448
2449 return pnv_npu_unset_window(gpe_table_group_to_npe(table_group), num);
2450}
2451
2452static void pnv_ioda2_npu_take_ownership(struct iommu_table_group *table_group)
2453{
2454 /*
2455 * Detach NPU first as pnv_ioda2_take_ownership() will destroy
2456 * the iommu_table if 32bit DMA is enabled.
2457 */
2458 pnv_npu_take_ownership(gpe_table_group_to_npe(table_group));
2459 pnv_ioda2_take_ownership(table_group);
2460}
2461
2462static struct iommu_table_group_ops pnv_pci_ioda2_npu_ops = {
2463 .get_table_size = pnv_pci_ioda2_get_table_size,
2464 .create_table = pnv_pci_ioda2_create_table,
2465 .set_window = pnv_pci_ioda2_npu_set_window,
2466 .unset_window = pnv_pci_ioda2_npu_unset_window,
2467 .take_ownership = pnv_ioda2_npu_take_ownership,
2468 .release_ownership = pnv_ioda2_release_ownership,
2469};
2470
2471static void pnv_pci_ioda_setup_iommu_api(void)
2472{
2473 struct pci_controller *hose, *tmp;
2474 struct pnv_phb *phb;
2475 struct pnv_ioda_pe *pe, *gpe;
2476
2477 /*
2478 * Now we have all PHBs discovered, time to add NPU devices to
2479 * the corresponding IOMMU groups.
2480 */
2481 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2482 phb = hose->private_data;
2483
2484 if (phb->type != PNV_PHB_NPU)
2485 continue;
2486
2487 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2488 gpe = pnv_pci_npu_setup_iommu(pe);
2489 if (gpe)
2490 gpe->table_group.ops = &pnv_pci_ioda2_npu_ops;
2491 }
2492 }
2493}
2494#else /* !CONFIG_IOMMU_API */
2495static void pnv_pci_ioda_setup_iommu_api(void) { };
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002496#endif
2497
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002498static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2499{
2500 const __be64 *swinvp;
2501
2502 /* OPAL variant of PHB3 invalidated TCEs */
2503 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2504 if (!swinvp)
2505 return;
2506
2507 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2508 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2509}
2510
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002511static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2512 unsigned levels, unsigned long limit,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002513 unsigned long *current_offset, unsigned long *total_allocated)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002514{
2515 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002516 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002517 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002518 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2519 unsigned entries = 1UL << (shift - 3);
2520 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002521
2522 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2523 if (!tce_mem) {
2524 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2525 return NULL;
2526 }
2527 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002528 memset(addr, 0, allocated);
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002529 *total_allocated += allocated;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002530
2531 --levels;
2532 if (!levels) {
2533 *current_offset += allocated;
2534 return addr;
2535 }
2536
2537 for (i = 0; i < entries; ++i) {
2538 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002539 levels, limit, current_offset, total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002540 if (!tmp)
2541 break;
2542
2543 addr[i] = cpu_to_be64(__pa(tmp) |
2544 TCE_PCI_READ | TCE_PCI_WRITE);
2545
2546 if (*current_offset >= limit)
2547 break;
2548 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002549
2550 return addr;
2551}
2552
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002553static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2554 unsigned long size, unsigned level);
2555
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002556static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002557 __u32 page_shift, __u64 window_size, __u32 levels,
2558 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002559{
2560 void *addr;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002561 unsigned long offset = 0, level_shift, total_allocated = 0;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002562 const unsigned window_shift = ilog2(window_size);
2563 unsigned entries_shift = window_shift - page_shift;
2564 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2565 const unsigned long tce_table_size = 1UL << table_shift;
2566
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002567 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2568 return -EINVAL;
2569
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002570 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2571 return -EINVAL;
2572
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002573 /* Adjust direct table size from window_size and levels */
2574 entries_shift = (entries_shift + levels - 1) / levels;
2575 level_shift = entries_shift + 3;
2576 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2577
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002578 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002579 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002580 levels, tce_table_size, &offset, &total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002581
2582 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002583 if (!addr)
2584 return -ENOMEM;
2585
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002586 /*
2587 * First level was allocated but some lower level failed as
2588 * we did not allocate as much as we wanted,
2589 * release partially allocated table.
2590 */
2591 if (offset < tce_table_size) {
2592 pnv_pci_ioda2_table_do_free_pages(addr,
2593 1ULL << (level_shift - 3), levels - 1);
2594 return -ENOMEM;
2595 }
2596
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002597 /* Setup linux iommu table */
2598 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2599 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002600 tbl->it_level_size = 1ULL << (level_shift - 3);
2601 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002602 tbl->it_allocated_size = total_allocated;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002603
2604 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2605 window_size, tce_table_size, bus_offset);
2606
2607 return 0;
2608}
2609
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002610static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2611 unsigned long size, unsigned level)
2612{
2613 const unsigned long addr_ul = (unsigned long) addr &
2614 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2615
2616 if (level) {
2617 long i;
2618 u64 *tmp = (u64 *) addr_ul;
2619
2620 for (i = 0; i < size; ++i) {
2621 unsigned long hpa = be64_to_cpu(tmp[i]);
2622
2623 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2624 continue;
2625
2626 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2627 level - 1);
2628 }
2629 }
2630
2631 free_pages(addr_ul, get_order(size << 3));
2632}
2633
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002634static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2635{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002636 const unsigned long size = tbl->it_indirect_levels ?
2637 tbl->it_level_size : tbl->it_size;
2638
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002639 if (!tbl->it_size)
2640 return;
2641
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002642 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2643 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002644}
2645
Gavin Shan373f5652013-04-25 19:21:01 +00002646static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2647 struct pnv_ioda_pe *pe)
2648{
Gavin Shan373f5652013-04-25 19:21:01 +00002649 int64_t rc;
2650
Gavin Shanccd1c192016-05-20 16:41:31 +10002651 if (!pnv_pci_ioda_pe_dma_weight(pe))
2652 return;
2653
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002654 /* TVE #1 is selected by PCI address bit 59 */
2655 pe->tce_bypass_base = 1ull << 59;
2656
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002657 iommu_register_group(&pe->table_group, phb->hose->global_number,
2658 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002659
Gavin Shan373f5652013-04-25 19:21:01 +00002660 /* The PE will reserve all possible 32-bits space */
Gavin Shan373f5652013-04-25 19:21:01 +00002661 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002662 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002663
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002664 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002665 pe->table_group.tce32_start = 0;
2666 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2667 pe->table_group.max_dynamic_windows_supported =
2668 IOMMU_TABLE_GROUP_MAX_TABLES;
2669 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2670 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002671#ifdef CONFIG_IOMMU_API
2672 pe->table_group.ops = &pnv_pci_ioda2_ops;
2673#endif
2674
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002675 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan801846d2016-05-03 15:41:34 +10002676 if (rc)
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002677 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002678
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002679 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002680 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002681 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002682 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002683}
2684
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002685#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002686static void pnv_ioda2_msi_eoi(struct irq_data *d)
2687{
2688 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2689 struct irq_chip *chip = irq_data_get_irq_chip(d);
2690 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2691 ioda.irq_chip);
2692 int64_t rc;
2693
2694 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2695 WARN_ON_ONCE(rc);
2696
2697 icp_native_eoi(d);
2698}
2699
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002700
2701static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2702{
2703 struct irq_data *idata;
2704 struct irq_chip *ichip;
2705
2706 if (phb->type != PNV_PHB_IODA2)
2707 return;
2708
2709 if (!phb->ioda.irq_chip_init) {
2710 /*
2711 * First time we setup an MSI IRQ, we need to setup the
2712 * corresponding IRQ chip to route correctly.
2713 */
2714 idata = irq_get_irq_data(virq);
2715 ichip = irq_data_get_irq_chip(idata);
2716 phb->ioda.irq_chip_init = 1;
2717 phb->ioda.irq_chip = *ichip;
2718 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2719 }
2720 irq_set_chip(virq, &phb->ioda.irq_chip);
2721}
2722
Ian Munsie80c49c72014-10-08 19:54:57 +11002723#ifdef CONFIG_CXL_BASE
2724
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002725struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002726{
2727 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2728
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002729 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002730}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002731EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002732
Ryan Grimm1212aa12015-01-19 11:52:50 -06002733int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002734{
2735 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2736 struct pnv_phb *phb = hose->private_data;
2737 struct pnv_ioda_pe *pe;
2738 int rc;
2739
2740 pe = pnv_ioda_get_pe(dev);
2741 if (!pe)
2742 return -ENODEV;
2743
2744 pe_info(pe, "Switching PHB to CXL\n");
2745
Ryan Grimm1212aa12015-01-19 11:52:50 -06002746 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsieb385c9e2016-06-08 15:09:54 +10002747 if (rc == OPAL_UNSUPPORTED)
2748 dev_err(&dev->dev, "Required cxl mode not supported by firmware - update skiboot\n");
2749 else if (rc)
Ian Munsie80c49c72014-10-08 19:54:57 +11002750 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2751
2752 return rc;
2753}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002754EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002755
2756/* Find PHB for cxl dev and allocate MSI hwirqs?
2757 * Returns the absolute hardware IRQ number
2758 */
2759int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2760{
2761 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2762 struct pnv_phb *phb = hose->private_data;
2763 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2764
2765 if (hwirq < 0) {
2766 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2767 return -ENOSPC;
2768 }
2769
2770 return phb->msi_base + hwirq;
2771}
2772EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2773
2774void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2775{
2776 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2777 struct pnv_phb *phb = hose->private_data;
2778
2779 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2780}
2781EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2782
2783void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2784 struct pci_dev *dev)
2785{
2786 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2787 struct pnv_phb *phb = hose->private_data;
2788 int i, hwirq;
2789
2790 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2791 if (!irqs->range[i])
2792 continue;
2793 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2794 i, irqs->offset[i],
2795 irqs->range[i]);
2796 hwirq = irqs->offset[i] - phb->msi_base;
2797 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2798 irqs->range[i]);
2799 }
2800}
2801EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2802
2803int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2804 struct pci_dev *dev, int num)
2805{
2806 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2807 struct pnv_phb *phb = hose->private_data;
2808 int i, hwirq, try;
2809
2810 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2811
2812 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2813 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2814 try = num;
2815 while (try) {
2816 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2817 if (hwirq >= 0)
2818 break;
2819 try /= 2;
2820 }
2821 if (!try)
2822 goto fail;
2823
2824 irqs->offset[i] = phb->msi_base + hwirq;
2825 irqs->range[i] = try;
2826 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2827 i, irqs->offset[i], irqs->range[i]);
2828 num -= try;
2829 }
2830 if (num)
2831 goto fail;
2832
2833 return 0;
2834fail:
2835 pnv_cxl_release_hwirq_ranges(irqs, dev);
2836 return -ENOSPC;
2837}
2838EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2839
2840int pnv_cxl_get_irq_count(struct pci_dev *dev)
2841{
2842 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2843 struct pnv_phb *phb = hose->private_data;
2844
2845 return phb->msi_bmp.irq_count;
2846}
2847EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2848
2849int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2850 unsigned int virq)
2851{
2852 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2853 struct pnv_phb *phb = hose->private_data;
2854 unsigned int xive_num = hwirq - phb->msi_base;
2855 struct pnv_ioda_pe *pe;
2856 int rc;
2857
2858 if (!(pe = pnv_ioda_get_pe(dev)))
2859 return -ENODEV;
2860
2861 /* Assign XIVE to PE */
2862 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2863 if (rc) {
2864 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2865 "hwirq 0x%x XIVE 0x%x PE\n",
2866 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2867 return -EIO;
2868 }
2869 set_msi_irq_chip(phb, virq);
2870
2871 return 0;
2872}
2873EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2874#endif
2875
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002876static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002877 unsigned int hwirq, unsigned int virq,
2878 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002879{
2880 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2881 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002882 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002883 int rc;
2884
2885 /* No PE assigned ? bail out ... no MSI for you ! */
2886 if (pe == NULL)
2887 return -ENXIO;
2888
2889 /* Check if we have an MVE */
2890 if (pe->mve_number < 0)
2891 return -ENXIO;
2892
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002893 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002894 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002895 is_64 = 0;
2896
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002897 /* Assign XIVE to PE */
2898 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2899 if (rc) {
2900 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2901 pci_name(dev), rc, xive_num);
2902 return -EIO;
2903 }
2904
2905 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002906 __be64 addr64;
2907
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002908 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2909 &addr64, &data);
2910 if (rc) {
2911 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2912 pci_name(dev), rc);
2913 return -EIO;
2914 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002915 msg->address_hi = be64_to_cpu(addr64) >> 32;
2916 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002917 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002918 __be32 addr32;
2919
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002920 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2921 &addr32, &data);
2922 if (rc) {
2923 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2924 pci_name(dev), rc);
2925 return -EIO;
2926 }
2927 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002928 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002929 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002930 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002931
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002932 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002933
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002934 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2935 " address=%x_%08x data=%x PE# %d\n",
2936 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2937 msg->address_hi, msg->address_lo, data, pe->pe_number);
2938
2939 return 0;
2940}
2941
2942static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2943{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002944 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002945 const __be32 *prop = of_get_property(phb->hose->dn,
2946 "ibm,opal-msi-ranges", NULL);
2947 if (!prop) {
2948 /* BML Fallback */
2949 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2950 }
2951 if (!prop)
2952 return;
2953
2954 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002955 count = be32_to_cpup(prop + 1);
2956 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002957 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2958 phb->hose->global_number);
2959 return;
2960 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002961
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002962 phb->msi_setup = pnv_pci_ioda_msi_setup;
2963 phb->msi32_support = 1;
2964 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002965 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002966}
2967#else
2968static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2969#endif /* CONFIG_PCI_MSI */
2970
Wei Yang6e628c72015-03-25 16:23:55 +08002971#ifdef CONFIG_PCI_IOV
2972static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2973{
Wei Yangf2dd0af2015-10-22 09:22:17 +08002974 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
2975 struct pnv_phb *phb = hose->private_data;
2976 const resource_size_t gate = phb->ioda.m64_segsize >> 2;
Wei Yang6e628c72015-03-25 16:23:55 +08002977 struct resource *res;
2978 int i;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002979 resource_size_t size, total_vf_bar_sz;
Wei Yang6e628c72015-03-25 16:23:55 +08002980 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002981 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002982
2983 if (!pdev->is_physfn || pdev->is_added)
2984 return;
2985
Wei Yang6e628c72015-03-25 16:23:55 +08002986 pdn = pci_get_pdn(pdev);
2987 pdn->vfs_expanded = 0;
Wei Yangee8222f2015-10-22 09:22:16 +08002988 pdn->m64_single_mode = false;
Wei Yang6e628c72015-03-25 16:23:55 +08002989
Wei Yang5b88ec22015-03-25 16:23:58 +08002990 total_vfs = pci_sriov_get_totalvfs(pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10002991 mul = phb->ioda.total_pe_num;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002992 total_vf_bar_sz = 0;
Wei Yang5b88ec22015-03-25 16:23:58 +08002993
2994 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2995 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2996 if (!res->flags || res->parent)
2997 continue;
2998 if (!pnv_pci_is_mem_pref_64(res->flags)) {
Wei Yangb0331852015-10-22 09:22:14 +08002999 dev_warn(&pdev->dev, "Don't support SR-IOV with"
3000 " non M64 VF BAR%d: %pR. \n",
Wei Yang5b88ec22015-03-25 16:23:58 +08003001 i, res);
Wei Yangb0331852015-10-22 09:22:14 +08003002 goto truncate_iov;
Wei Yang5b88ec22015-03-25 16:23:58 +08003003 }
3004
Wei Yangdfcc8d42015-10-22 09:22:18 +08003005 total_vf_bar_sz += pci_iov_resource_size(pdev,
3006 i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08003007
Wei Yangf2dd0af2015-10-22 09:22:17 +08003008 /*
3009 * If bigger than quarter of M64 segment size, just round up
3010 * power of two.
3011 *
3012 * Generally, one M64 BAR maps one IOV BAR. To avoid conflict
3013 * with other devices, IOV BAR size is expanded to be
3014 * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64
3015 * segment size , the expanded size would equal to half of the
3016 * whole M64 space size, which will exhaust the M64 Space and
3017 * limit the system flexibility. This is a design decision to
3018 * set the boundary to quarter of the M64 segment size.
3019 */
Wei Yangdfcc8d42015-10-22 09:22:18 +08003020 if (total_vf_bar_sz > gate) {
Wei Yang5b88ec22015-03-25 16:23:58 +08003021 mul = roundup_pow_of_two(total_vfs);
Wei Yangdfcc8d42015-10-22 09:22:18 +08003022 dev_info(&pdev->dev,
3023 "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n",
3024 total_vf_bar_sz, gate, mul);
Wei Yangee8222f2015-10-22 09:22:16 +08003025 pdn->m64_single_mode = true;
Wei Yang5b88ec22015-03-25 16:23:58 +08003026 break;
3027 }
3028 }
3029
Wei Yang6e628c72015-03-25 16:23:55 +08003030 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
3031 res = &pdev->resource[i + PCI_IOV_RESOURCES];
3032 if (!res->flags || res->parent)
3033 continue;
Wei Yang6e628c72015-03-25 16:23:55 +08003034
Wei Yang6e628c72015-03-25 16:23:55 +08003035 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yangee8222f2015-10-22 09:22:16 +08003036 /*
3037 * On PHB3, the minimum size alignment of M64 BAR in single
3038 * mode is 32MB.
3039 */
3040 if (pdn->m64_single_mode && (size < SZ_32M))
3041 goto truncate_iov;
3042 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
Wei Yang5b88ec22015-03-25 16:23:58 +08003043 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08003044 dev_dbg(&pdev->dev, " %pR\n", res);
3045 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08003046 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08003047 }
Wei Yang5b88ec22015-03-25 16:23:58 +08003048 pdn->vfs_expanded = mul;
Wei Yangb0331852015-10-22 09:22:14 +08003049
3050 return;
3051
3052truncate_iov:
3053 /* To save MMIO space, IOV BAR is truncated. */
3054 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
3055 res = &pdev->resource[i + PCI_IOV_RESOURCES];
3056 res->flags = 0;
3057 res->end = res->start - 1;
3058 }
Wei Yang6e628c72015-03-25 16:23:55 +08003059}
3060#endif /* CONFIG_PCI_IOV */
3061
Gavin Shan23e79422016-05-03 15:41:27 +10003062static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
3063 struct resource *res)
3064{
3065 struct pnv_phb *phb = pe->phb;
3066 struct pci_bus_region region;
3067 int index;
3068 int64_t rc;
3069
3070 if (!res || !res->flags || res->start > res->end)
3071 return;
3072
3073 if (res->flags & IORESOURCE_IO) {
3074 region.start = res->start - phb->ioda.io_pci_base;
3075 region.end = res->end - phb->ioda.io_pci_base;
3076 index = region.start / phb->ioda.io_segsize;
3077
3078 while (index < phb->ioda.total_pe_num &&
3079 region.start <= region.end) {
3080 phb->ioda.io_segmap[index] = pe->pe_number;
3081 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3082 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
3083 if (rc != OPAL_SUCCESS) {
3084 pr_err("%s: Error %lld mapping IO segment#%d to PE#%d\n",
3085 __func__, rc, index, pe->pe_number);
3086 break;
3087 }
3088
3089 region.start += phb->ioda.io_segsize;
3090 index++;
3091 }
3092 } else if ((res->flags & IORESOURCE_MEM) &&
3093 !pnv_pci_is_mem_pref_64(res->flags)) {
3094 region.start = res->start -
3095 phb->hose->mem_offset[0] -
3096 phb->ioda.m32_pci_base;
3097 region.end = res->end -
3098 phb->hose->mem_offset[0] -
3099 phb->ioda.m32_pci_base;
3100 index = region.start / phb->ioda.m32_segsize;
3101
3102 while (index < phb->ioda.total_pe_num &&
3103 region.start <= region.end) {
3104 phb->ioda.m32_segmap[index] = pe->pe_number;
3105 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3106 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
3107 if (rc != OPAL_SUCCESS) {
3108 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%d",
3109 __func__, rc, index, pe->pe_number);
3110 break;
3111 }
3112
3113 region.start += phb->ioda.m32_segsize;
3114 index++;
3115 }
3116 }
3117}
3118
Gavin Shan11685be2012-08-20 03:49:16 +00003119/*
3120 * This function is supposed to be called on basis of PE from top
3121 * to bottom style. So the the I/O or MMIO segment assigned to
3122 * parent PE could be overrided by its child PEs if necessary.
3123 */
Gavin Shan23e79422016-05-03 15:41:27 +10003124static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00003125{
Gavin Shan69d733e2016-05-03 15:41:28 +10003126 struct pci_dev *pdev;
Gavin Shan23e79422016-05-03 15:41:27 +10003127 int i;
Gavin Shan11685be2012-08-20 03:49:16 +00003128
3129 /*
3130 * NOTE: We only care PCI bus based PE for now. For PCI
3131 * device based PE, for example SRIOV sensitive VF should
3132 * be figured out later.
3133 */
3134 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
3135
Gavin Shan69d733e2016-05-03 15:41:28 +10003136 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
3137 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
3138 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
3139
3140 /*
3141 * If the PE contains all subordinate PCI buses, the
3142 * windows of the child bridges should be mapped to
3143 * the PE as well.
3144 */
3145 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
3146 continue;
3147 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
3148 pnv_ioda_setup_pe_res(pe,
3149 &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
3150 }
Gavin Shan11685be2012-08-20 03:49:16 +00003151}
3152
Gavin Shan37c367f2013-06-20 18:13:25 +08003153static void pnv_pci_ioda_create_dbgfs(void)
3154{
3155#ifdef CONFIG_DEBUG_FS
3156 struct pci_controller *hose, *tmp;
3157 struct pnv_phb *phb;
3158 char name[16];
3159
3160 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3161 phb = hose->private_data;
3162
Gavin Shanccd1c192016-05-20 16:41:31 +10003163 /* Notify initialization of PHB done */
3164 phb->initialized = 1;
3165
Gavin Shan37c367f2013-06-20 18:13:25 +08003166 sprintf(name, "PCI%04x", hose->global_number);
3167 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
3168 if (!phb->dbgfs)
3169 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
3170 __func__, hose->global_number);
3171 }
3172#endif /* CONFIG_DEBUG_FS */
3173}
3174
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003175static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00003176{
3177 pnv_pci_ioda_setup_PEs();
Gavin Shanccd1c192016-05-20 16:41:31 +10003178 pnv_pci_ioda_setup_iommu_api();
Gavin Shan37c367f2013-06-20 18:13:25 +08003179 pnv_pci_ioda_create_dbgfs();
3180
Gavin Shane9cc17d2013-06-20 13:21:14 +08003181#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08003182 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04003183 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003184#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00003185}
3186
Gavin Shan271fd032012-09-11 16:59:47 -06003187/*
3188 * Returns the alignment for I/O or memory windows for P2P
3189 * bridges. That actually depends on how PEs are segmented.
3190 * For now, we return I/O or M32 segment size for PE sensitive
3191 * P2P bridges. Otherwise, the default values (4KiB for I/O,
3192 * 1MiB for memory) will be returned.
3193 *
3194 * The current PCI bus might be put into one PE, which was
3195 * create against the parent PCI bridge. For that case, we
3196 * needn't enlarge the alignment so that we can save some
3197 * resources.
3198 */
3199static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
3200 unsigned long type)
3201{
3202 struct pci_dev *bridge;
3203 struct pci_controller *hose = pci_bus_to_host(bus);
3204 struct pnv_phb *phb = hose->private_data;
3205 int num_pci_bridges = 0;
3206
3207 bridge = bus->self;
3208 while (bridge) {
3209 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
3210 num_pci_bridges++;
3211 if (num_pci_bridges >= 2)
3212 return 1;
3213 }
3214
3215 bridge = bridge->bus->self;
3216 }
3217
Guo Chao262af552014-07-21 14:42:30 +10003218 /* We fail back to M32 if M64 isn't supported */
3219 if (phb->ioda.m64_segsize &&
3220 pnv_pci_is_mem_pref_64(type))
3221 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06003222 if (type & IORESOURCE_MEM)
3223 return phb->ioda.m32_segsize;
3224
3225 return phb->ioda.io_segsize;
3226}
3227
Gavin Shan40e2a472016-05-20 16:41:33 +10003228/*
3229 * We are updating root port or the upstream port of the
3230 * bridge behind the root port with PHB's windows in order
3231 * to accommodate the changes on required resources during
3232 * PCI (slot) hotplug, which is connected to either root
3233 * port or the downstream ports of PCIe switch behind the
3234 * root port.
3235 */
3236static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
3237 unsigned long type)
3238{
3239 struct pci_controller *hose = pci_bus_to_host(bus);
3240 struct pnv_phb *phb = hose->private_data;
3241 struct pci_dev *bridge = bus->self;
3242 struct resource *r, *w;
3243 bool msi_region = false;
3244 int i;
3245
3246 /* Check if we need apply fixup to the bridge's windows */
3247 if (!pci_is_root_bus(bridge->bus) &&
3248 !pci_is_root_bus(bridge->bus->self->bus))
3249 return;
3250
3251 /* Fixup the resources */
3252 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
3253 r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
3254 if (!r->flags || !r->parent)
3255 continue;
3256
3257 w = NULL;
3258 if (r->flags & type & IORESOURCE_IO)
3259 w = &hose->io_resource;
3260 else if (pnv_pci_is_mem_pref_64(r->flags) &&
3261 (type & IORESOURCE_PREFETCH) &&
3262 phb->ioda.m64_segsize)
3263 w = &hose->mem_resources[1];
3264 else if (r->flags & type & IORESOURCE_MEM) {
3265 w = &hose->mem_resources[0];
3266 msi_region = true;
3267 }
3268
3269 r->start = w->start;
3270 r->end = w->end;
3271
3272 /* The 64KB 32-bits MSI region shouldn't be included in
3273 * the 32-bits bridge window. Otherwise, we can see strange
3274 * issues. One of them is EEH error observed on Garrison.
3275 *
3276 * Exclude top 1MB region which is the minimal alignment of
3277 * 32-bits bridge window.
3278 */
3279 if (msi_region) {
3280 r->end += 0x10000;
3281 r->end -= 0x100000;
3282 }
3283 }
3284}
3285
Gavin Shanccd1c192016-05-20 16:41:31 +10003286static void pnv_pci_setup_bridge(struct pci_bus *bus, unsigned long type)
3287{
3288 struct pci_controller *hose = pci_bus_to_host(bus);
3289 struct pnv_phb *phb = hose->private_data;
3290 struct pci_dev *bridge = bus->self;
3291 struct pnv_ioda_pe *pe;
3292 bool all = (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
3293
Gavin Shan40e2a472016-05-20 16:41:33 +10003294 /* Extend bridge's windows if necessary */
3295 pnv_pci_fixup_bridge_resources(bus, type);
3296
Gavin Shan63803c32016-05-20 16:41:32 +10003297 /* The PE for root bus should be realized before any one else */
3298 if (!phb->ioda.root_pe_populated) {
3299 pe = pnv_ioda_setup_bus_PE(phb->hose->bus, false);
3300 if (pe) {
3301 phb->ioda.root_pe_idx = pe->pe_number;
3302 phb->ioda.root_pe_populated = true;
3303 }
3304 }
3305
Gavin Shanccd1c192016-05-20 16:41:31 +10003306 /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
3307 if (list_empty(&bus->devices))
3308 return;
3309
3310 /* Reserve PEs according to used M64 resources */
3311 if (phb->reserve_m64_pe)
3312 phb->reserve_m64_pe(bus, NULL, all);
3313
3314 /*
3315 * Assign PE. We might run here because of partial hotplug.
3316 * For the case, we just pick up the existing PE and should
3317 * not allocate resources again.
3318 */
3319 pe = pnv_ioda_setup_bus_PE(bus, all);
3320 if (!pe)
3321 return;
3322
3323 pnv_ioda_setup_pe_seg(pe);
3324 switch (phb->type) {
3325 case PNV_PHB_IODA1:
3326 pnv_pci_ioda1_setup_dma_pe(phb, pe);
3327 break;
3328 case PNV_PHB_IODA2:
3329 pnv_pci_ioda2_setup_dma_pe(phb, pe);
3330 break;
3331 default:
3332 pr_warn("%s: No DMA for PHB#%d (type %d)\n",
3333 __func__, phb->hose->global_number, phb->type);
3334 }
3335}
3336
Wei Yang5350ab32015-03-25 16:23:56 +08003337#ifdef CONFIG_PCI_IOV
3338static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
3339 int resno)
3340{
Wei Yangee8222f2015-10-22 09:22:16 +08003341 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3342 struct pnv_phb *phb = hose->private_data;
Wei Yang5350ab32015-03-25 16:23:56 +08003343 struct pci_dn *pdn = pci_get_pdn(pdev);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003344 resource_size_t align;
Wei Yang5350ab32015-03-25 16:23:56 +08003345
Wei Yang7fbe7a92015-10-22 09:22:15 +08003346 /*
3347 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
3348 * SR-IOV. While from hardware perspective, the range mapped by M64
3349 * BAR should be size aligned.
3350 *
Wei Yangee8222f2015-10-22 09:22:16 +08003351 * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra
3352 * powernv-specific hardware restriction is gone. But if just use the
3353 * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with
3354 * in one segment of M64 #15, which introduces the PE conflict between
3355 * PF and VF. Based on this, the minimum alignment of an IOV BAR is
3356 * m64_segsize.
3357 *
Wei Yang7fbe7a92015-10-22 09:22:15 +08003358 * This function returns the total IOV BAR size if M64 BAR is in
3359 * Shared PE mode or just VF BAR size if not.
Wei Yangee8222f2015-10-22 09:22:16 +08003360 * If the M64 BAR is in Single PE mode, return the VF BAR size or
3361 * M64 segment size if IOV BAR size is less.
Wei Yang7fbe7a92015-10-22 09:22:15 +08003362 */
Wei Yang5350ab32015-03-25 16:23:56 +08003363 align = pci_iov_resource_size(pdev, resno);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003364 if (!pdn->vfs_expanded)
3365 return align;
Wei Yangee8222f2015-10-22 09:22:16 +08003366 if (pdn->m64_single_mode)
3367 return max(align, (resource_size_t)phb->ioda.m64_segsize);
Wei Yang5350ab32015-03-25 16:23:56 +08003368
Wei Yang7fbe7a92015-10-22 09:22:15 +08003369 return pdn->vfs_expanded * align;
Wei Yang5350ab32015-03-25 16:23:56 +08003370}
3371#endif /* CONFIG_PCI_IOV */
3372
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003373/* Prevent enabling devices for which we couldn't properly
3374 * assign a PE
3375 */
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003376static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003377{
Gavin Shandb1266c2012-08-20 03:49:18 +00003378 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3379 struct pnv_phb *phb = hose->private_data;
3380 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003381
Gavin Shandb1266c2012-08-20 03:49:18 +00003382 /* The function is probably called while the PEs have
3383 * not be created yet. For example, resource reassignment
3384 * during PCI probe period. We just skip the check if
3385 * PEs isn't ready.
3386 */
3387 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003388 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003389
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003390 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003391 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003392 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003393
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003394 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003395}
3396
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003397static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003398{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003399 struct pnv_phb *phb = hose->private_data;
3400
Gavin Shand1a85ee2014-09-30 12:39:05 +10003401 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003402 OPAL_ASSERT_RESET);
3403}
3404
Daniel Axtens92ae0352015-04-28 15:12:05 +10003405static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003406 .dma_dev_setup = pnv_pci_dma_dev_setup,
3407 .dma_bus_setup = pnv_pci_dma_bus_setup,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003408#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003409 .setup_msi_irqs = pnv_setup_msi_irqs,
3410 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003411#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003412 .enable_device_hook = pnv_pci_enable_device_hook,
3413 .window_alignment = pnv_pci_window_alignment,
Gavin Shanccd1c192016-05-20 16:41:31 +10003414 .setup_bridge = pnv_pci_setup_bridge,
Gavin Shancb4224c2016-05-03 15:41:21 +10003415 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3416 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3417 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3418 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003419};
3420
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003421static int pnv_npu_dma_set_mask(struct pci_dev *npdev, u64 dma_mask)
3422{
3423 dev_err_once(&npdev->dev,
3424 "%s operation unsupported for NVLink devices\n",
3425 __func__);
3426 return -EPERM;
3427}
3428
Alistair Popple5d2aa712015-12-17 13:43:13 +11003429static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003430 .dma_dev_setup = pnv_pci_dma_dev_setup,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003431#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003432 .setup_msi_irqs = pnv_setup_msi_irqs,
3433 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003434#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003435 .enable_device_hook = pnv_pci_enable_device_hook,
3436 .window_alignment = pnv_pci_window_alignment,
3437 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3438 .dma_set_mask = pnv_npu_dma_set_mask,
3439 .shutdown = pnv_pci_ioda_shutdown,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003440};
3441
Anton Blancharde51df2c2014-08-20 08:55:18 +10003442static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3443 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003444{
3445 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003446 struct pnv_phb *phb;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003447 unsigned long size, m64map_off, m32map_off, pemap_off;
3448 unsigned long iomap_off = 0, dma32map_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10003449 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003450 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003451 int len;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003452 unsigned int segno;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003453 u64 phb_id;
3454 void *aux;
3455 long rc;
3456
Gavin Shan58d714e2013-07-31 16:47:00 +08003457 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003458
3459 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3460 if (!prop64) {
3461 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3462 return;
3463 }
3464 phb_id = be64_to_cpup(prop64);
3465 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3466
Michael Ellermane39f223f2014-11-18 16:47:35 +11003467 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003468
3469 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003470 phb->hose = hose = pcibios_alloc_controller(np);
3471 if (!phb->hose) {
3472 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003473 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003474 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003475 return;
3476 }
3477
3478 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003479 prop32 = of_get_property(np, "bus-range", &len);
3480 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003481 hose->first_busno = be32_to_cpu(prop32[0]);
3482 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003483 } else {
3484 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3485 hose->first_busno = 0;
3486 hose->last_busno = 0xff;
3487 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003488 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003489 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003490 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003491 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003492 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003493
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003494 /* Detect specific models for error handling */
3495 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3496 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003497 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003498 phb->model = PNV_PHB_MODEL_PHB3;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003499 else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
3500 phb->model = PNV_PHB_MODEL_NPU;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003501 else
3502 phb->model = PNV_PHB_MODEL_UNKNOWN;
3503
Gavin Shanaa0c0332013-04-25 19:20:57 +00003504 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003505 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003506
Gavin Shanaa0c0332013-04-25 19:20:57 +00003507 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003508 phb->regs = of_iomap(np, 0);
3509 if (phb->regs == NULL)
3510 pr_err(" Failed to map registers !\n");
3511
Gavin Shan577c8c82016-05-20 16:41:28 +10003512 /* Initialize TCE kill register */
3513 pnv_pci_ioda_setup_opal_tce_kill(phb);
3514
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003515 /* Initialize more IODA stuff */
Gavin Shan92b8f132016-05-03 15:41:24 +10003516 phb->ioda.total_pe_num = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003517 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003518 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003519 phb->ioda.total_pe_num = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003520 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3521 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003522 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003523
Gavin Shanc1275622016-05-20 16:41:29 +10003524 /* Invalidate RID to PE# mapping */
3525 for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
3526 phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
3527
Guo Chao262af552014-07-21 14:42:30 +10003528 /* Parse 64-bit MMIO range */
3529 pnv_ioda_parse_m64_window(phb);
3530
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003531 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003532 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003533 phb->ioda.m32_size += 0x10000;
3534
Gavin Shan92b8f132016-05-03 15:41:24 +10003535 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003536 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003537 phb->ioda.io_size = hose->pci_io_size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003538 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003539 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3540
Gavin Shan2b923ed2016-05-05 12:04:16 +10003541 /* Calculate how many 32-bit TCE segments we have */
3542 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
3543 PNV_IODA1_DMA32_SEGSIZE;
3544
Gavin Shanc35d2a82013-07-31 16:47:04 +08003545 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Alexey Kardashevskiy92a86752016-05-12 15:47:09 +10003546 size = _ALIGN_UP(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
3547 sizeof(unsigned long));
Gavin Shan93289d82016-05-03 15:41:29 +10003548 m64map_off = size;
3549 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003550 m32map_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003551 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003552 if (phb->type == PNV_PHB_IODA1) {
3553 iomap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003554 size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
Gavin Shan2b923ed2016-05-05 12:04:16 +10003555 dma32map_off = size;
3556 size += phb->ioda.dma32_count *
3557 sizeof(phb->ioda.dma32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003558 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003559 pemap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003560 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003561 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003562 phb->ioda.pe_alloc = aux;
Gavin Shan93289d82016-05-03 15:41:29 +10003563 phb->ioda.m64_segmap = aux + m64map_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003564 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shan93289d82016-05-03 15:41:29 +10003565 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
3566 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003567 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan93289d82016-05-03 15:41:29 +10003568 }
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003569 if (phb->type == PNV_PHB_IODA1) {
Gavin Shanc35d2a82013-07-31 16:47:04 +08003570 phb->ioda.io_segmap = aux + iomap_off;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003571 for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
3572 phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003573
3574 phb->ioda.dma32_segmap = aux + dma32map_off;
3575 for (segno = 0; segno < phb->ioda.dma32_count; segno++)
3576 phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003577 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003578 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan63803c32016-05-20 16:41:32 +10003579
3580 /*
3581 * Choose PE number for root bus, which shouldn't have
3582 * M64 resources consumed by its child devices. To pick
3583 * the PE number adjacent to the reserved one if possible.
3584 */
3585 pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
3586 if (phb->ioda.reserved_pe_idx == 0) {
3587 phb->ioda.root_pe_idx = 1;
3588 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3589 } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
3590 phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
3591 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3592 } else {
3593 phb->ioda.root_pe_idx = IODA_INVALID_PE;
3594 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003595
3596 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003597 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003598
3599 /* Calculate how many 32-bit TCE segments we have */
Gavin Shan2b923ed2016-05-05 12:04:16 +10003600 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
Gavin Shanacce9712016-05-03 15:41:33 +10003601 PNV_IODA1_DMA32_SEGSIZE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003602
Gavin Shanaa0c0332013-04-25 19:20:57 +00003603#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003604 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3605 window_type,
3606 window_num,
3607 starting_real_address,
3608 starting_pci_address,
3609 segment_size);
3610#endif
3611
Guo Chao262af552014-07-21 14:42:30 +10003612 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
Gavin Shan92b8f132016-05-03 15:41:24 +10003613 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
Guo Chao262af552014-07-21 14:42:30 +10003614 phb->ioda.m32_size, phb->ioda.m32_segsize);
3615 if (phb->ioda.m64_size)
3616 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3617 phb->ioda.m64_size, phb->ioda.m64_segsize);
3618 if (phb->ioda.io_size)
3619 pr_info(" IO: 0x%x [segment=0x%x]\n",
3620 phb->ioda.io_size, phb->ioda.io_segsize);
3621
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003622
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003623 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003624 phb->get_pe_state = pnv_ioda_get_pe_state;
3625 phb->freeze_pe = pnv_ioda_freeze_pe;
3626 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003627
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003628 /* Setup MSI support */
3629 pnv_pci_init_ioda_msis(phb);
3630
Gavin Shanc40a4212012-08-20 03:49:20 +00003631 /*
3632 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3633 * to let the PCI core do resource assignment. It's supposed
3634 * that the PCI core will do correct I/O and MMIO alignment
3635 * for the P2P bridge bars so that each PCI bus (excluding
3636 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003637 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003638 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003639
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003640 if (phb->type == PNV_PHB_NPU) {
Alistair Popple5d2aa712015-12-17 13:43:13 +11003641 hose->controller_ops = pnv_npu_ioda_controller_ops;
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003642 } else {
3643 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003644 hose->controller_ops = pnv_pci_ioda_controller_ops;
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003645 }
Michael Ellermanad30cb92015-04-14 09:29:23 +10003646
Wei Yang6e628c72015-03-25 16:23:55 +08003647#ifdef CONFIG_PCI_IOV
3648 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003649 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003650#endif
3651
Gavin Shanc40a4212012-08-20 03:49:20 +00003652 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003653
3654 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003655 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003656 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003657 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003658
3659 /* If we're running in kdump kerenl, the previous kerenl never
3660 * shutdown PCI devices correctly. We already got IODA table
3661 * cleaned out. So we have to issue PHB reset to stop all PCI
3662 * transactions from previous kerenl.
3663 */
3664 if (is_kdump_kernel()) {
3665 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003666 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3667 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003668 }
Guo Chao262af552014-07-21 14:42:30 +10003669
Gavin Shan9e9e8932014-11-12 13:36:05 +11003670 /* Remove M64 resource if we can't configure it successfully */
3671 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003672 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003673}
3674
Bjorn Helgaas67975002013-07-02 12:20:03 -06003675void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003676{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003677 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003678}
3679
Alistair Popple5d2aa712015-12-17 13:43:13 +11003680void __init pnv_pci_init_npu_phb(struct device_node *np)
3681{
3682 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU);
3683}
3684
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003685void __init pnv_pci_init_ioda_hub(struct device_node *np)
3686{
3687 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003688 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003689 u64 hub_id;
3690
3691 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3692
3693 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3694 if (!prop64) {
3695 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3696 return;
3697 }
3698 hub_id = be64_to_cpup(prop64);
3699 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3700
3701 /* Count child PHBs */
3702 for_each_child_of_node(np, phbn) {
3703 /* Look for IODA1 PHBs */
3704 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003705 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003706 }
3707}