blob: c9494eacef512d13f9170ce71bbde5ffdb6458e5 [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
Gavin Shan9497a1c2016-06-21 12:35:56 +100058static const char * const pnv_phb_names[] = { "IODA1", "IODA2", "NPU" };
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100059static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
60
Alexey Kardashevskiy7d623e42016-04-29 18:55:21 +100061void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
Joe Perches6d31c2f2014-09-21 10:55:06 -070062 const char *fmt, ...)
63{
64 struct va_format vaf;
65 va_list args;
66 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000067
Joe Perches6d31c2f2014-09-21 10:55:06 -070068 va_start(args, fmt);
69
70 vaf.fmt = fmt;
71 vaf.va = &args;
72
Wei Yang781a8682015-03-25 16:23:57 +080073 if (pe->flags & PNV_IODA_PE_DEV)
Joe Perches6d31c2f2014-09-21 10:55:06 -070074 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
Wei Yang781a8682015-03-25 16:23:57 +080075 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Joe Perches6d31c2f2014-09-21 10:55:06 -070076 sprintf(pfix, "%04x:%02x ",
77 pci_domain_nr(pe->pbus), pe->pbus->number);
Wei Yang781a8682015-03-25 16:23:57 +080078#ifdef CONFIG_PCI_IOV
79 else if (pe->flags & PNV_IODA_PE_VF)
80 sprintf(pfix, "%04x:%02x:%2x.%d",
81 pci_domain_nr(pe->parent_dev->bus),
82 (pe->rid & 0xff00) >> 8,
83 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
84#endif /* CONFIG_PCI_IOV*/
Joe Perches6d31c2f2014-09-21 10:55:06 -070085
86 printk("%spci %s: [PE# %.3d] %pV",
87 level, pfix, pe->pe_number, &vaf);
88
89 va_end(args);
90}
91
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020092static bool pnv_iommu_bypass_disabled __read_mostly;
93
94static int __init iommu_setup(char *str)
95{
96 if (!str)
97 return -EINVAL;
98
99 while (*str) {
100 if (!strncmp(str, "nobypass", 8)) {
101 pnv_iommu_bypass_disabled = true;
102 pr_info("PowerNV: IOMMU bypass window disabled.\n");
103 break;
104 }
105 str += strcspn(str, ",");
106 if (*str == ',')
107 str++;
108 }
109
110 return 0;
111}
112early_param("iommu", iommu_setup);
113
Guo Chao262af552014-07-21 14:42:30 +1000114static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
115{
116 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
117 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
118}
119
Gavin Shan1e916772016-05-03 15:41:36 +1000120static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
121{
122 phb->ioda.pe_array[pe_no].phb = phb;
123 phb->ioda.pe_array[pe_no].pe_number = pe_no;
124
125 return &phb->ioda.pe_array[pe_no];
126}
127
Gavin Shan4b82ab12014-11-12 13:36:07 +1100128static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
129{
Gavin Shan92b8f132016-05-03 15:41:24 +1000130 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100131 pr_warn("%s: Invalid PE %d on PHB#%x\n",
132 __func__, pe_no, phb->hose->global_number);
133 return;
134 }
135
Gavin Shane9dc4d72015-06-19 12:26:16 +1000136 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
137 pr_debug("%s: PE %d was reserved on PHB#%x\n",
138 __func__, pe_no, phb->hose->global_number);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100139
Gavin Shan1e916772016-05-03 15:41:36 +1000140 pnv_ioda_init_pe(phb, pe_no);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100141}
142
Gavin Shan1e916772016-05-03 15:41:36 +1000143static struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000144{
Gavin Shan9fcd6f42016-05-20 16:41:30 +1000145 unsigned long pe = phb->ioda.total_pe_num - 1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000146
Gavin Shan9fcd6f42016-05-20 16:41:30 +1000147 for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
148 if (!test_and_set_bit(pe, phb->ioda.pe_alloc))
149 return pnv_ioda_init_pe(phb, pe);
150 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000151
Gavin Shan9fcd6f42016-05-20 16:41:30 +1000152 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000153}
154
Gavin Shan1e916772016-05-03 15:41:36 +1000155static void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000156{
Gavin Shan1e916772016-05-03 15:41:36 +1000157 struct pnv_phb *phb = pe->phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000158
Gavin Shan1e916772016-05-03 15:41:36 +1000159 WARN_ON(pe->pdev);
160
161 memset(pe, 0, sizeof(struct pnv_ioda_pe));
162 clear_bit(pe->pe_number, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000163}
164
Guo Chao262af552014-07-21 14:42:30 +1000165/* The default M64 BAR is shared by all PEs */
166static int pnv_ioda2_init_m64(struct pnv_phb *phb)
167{
168 const char *desc;
169 struct resource *r;
170 s64 rc;
171
172 /* Configure the default M64 BAR */
173 rc = opal_pci_set_phb_mem_window(phb->opal_id,
174 OPAL_M64_WINDOW_TYPE,
175 phb->ioda.m64_bar_idx,
176 phb->ioda.m64_base,
177 0, /* unused */
178 phb->ioda.m64_size);
179 if (rc != OPAL_SUCCESS) {
180 desc = "configuring";
181 goto fail;
182 }
183
184 /* Enable the default M64 BAR */
185 rc = opal_pci_phb_mmio_enable(phb->opal_id,
186 OPAL_M64_WINDOW_TYPE,
187 phb->ioda.m64_bar_idx,
188 OPAL_ENABLE_M64_SPLIT);
189 if (rc != OPAL_SUCCESS) {
190 desc = "enabling";
191 goto fail;
192 }
193
194 /* Mark the M64 BAR assigned */
195 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
196
197 /*
Gavin Shan63803c32016-05-20 16:41:32 +1000198 * Exclude the segments for reserved and root bus PE, which
199 * are first or last two PEs.
Guo Chao262af552014-07-21 14:42:30 +1000200 */
201 r = &phb->hose->mem_resources[1];
Gavin Shan92b8f132016-05-03 15:41:24 +1000202 if (phb->ioda.reserved_pe_idx == 0)
Gavin Shan63803c32016-05-20 16:41:32 +1000203 r->start += (2 * phb->ioda.m64_segsize);
Gavin Shan92b8f132016-05-03 15:41:24 +1000204 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Gavin Shan63803c32016-05-20 16:41:32 +1000205 r->end -= (2 * phb->ioda.m64_segsize);
Guo Chao262af552014-07-21 14:42:30 +1000206 else
207 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
Gavin Shan92b8f132016-05-03 15:41:24 +1000208 phb->ioda.reserved_pe_idx);
Guo Chao262af552014-07-21 14:42:30 +1000209
210 return 0;
211
212fail:
213 pr_warn(" Failure %lld %s M64 BAR#%d\n",
214 rc, desc, phb->ioda.m64_bar_idx);
215 opal_pci_phb_mmio_enable(phb->opal_id,
216 OPAL_M64_WINDOW_TYPE,
217 phb->ioda.m64_bar_idx,
218 OPAL_DISABLE_M64);
219 return -EIO;
220}
221
Gavin Shanc4306702016-05-03 15:41:30 +1000222static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
Gavin Shan96a2f922015-06-19 12:26:17 +1000223 unsigned long *pe_bitmap)
Guo Chao262af552014-07-21 14:42:30 +1000224{
Gavin Shan96a2f922015-06-19 12:26:17 +1000225 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
226 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000227 struct resource *r;
Gavin Shan96a2f922015-06-19 12:26:17 +1000228 resource_size_t base, sgsz, start, end;
229 int segno, i;
Guo Chao262af552014-07-21 14:42:30 +1000230
Gavin Shan96a2f922015-06-19 12:26:17 +1000231 base = phb->ioda.m64_base;
232 sgsz = phb->ioda.m64_segsize;
233 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
234 r = &pdev->resource[i];
235 if (!r->parent || !pnv_pci_is_mem_pref_64(r->flags))
236 continue;
Guo Chao262af552014-07-21 14:42:30 +1000237
Gavin Shan96a2f922015-06-19 12:26:17 +1000238 start = _ALIGN_DOWN(r->start - base, sgsz);
239 end = _ALIGN_UP(r->end - base, sgsz);
240 for (segno = start / sgsz; segno < end / sgsz; segno++) {
241 if (pe_bitmap)
242 set_bit(segno, pe_bitmap);
243 else
244 pnv_ioda_reserve_pe(phb, segno);
Guo Chao262af552014-07-21 14:42:30 +1000245 }
246 }
247}
248
Gavin Shan99451552016-05-05 12:02:13 +1000249static int pnv_ioda1_init_m64(struct pnv_phb *phb)
250{
251 struct resource *r;
252 int index;
253
254 /*
255 * There are 16 M64 BARs, each of which has 8 segments. So
256 * there are as many M64 segments as the maximum number of
257 * PEs, which is 128.
258 */
259 for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
260 unsigned long base, segsz = phb->ioda.m64_segsize;
261 int64_t rc;
262
263 base = phb->ioda.m64_base +
264 index * PNV_IODA1_M64_SEGS * segsz;
265 rc = opal_pci_set_phb_mem_window(phb->opal_id,
266 OPAL_M64_WINDOW_TYPE, index, base, 0,
267 PNV_IODA1_M64_SEGS * segsz);
268 if (rc != OPAL_SUCCESS) {
269 pr_warn(" Error %lld setting M64 PHB#%d-BAR#%d\n",
270 rc, phb->hose->global_number, index);
271 goto fail;
272 }
273
274 rc = opal_pci_phb_mmio_enable(phb->opal_id,
275 OPAL_M64_WINDOW_TYPE, index,
276 OPAL_ENABLE_M64_SPLIT);
277 if (rc != OPAL_SUCCESS) {
278 pr_warn(" Error %lld enabling M64 PHB#%d-BAR#%d\n",
279 rc, phb->hose->global_number, index);
280 goto fail;
281 }
282 }
283
284 /*
Gavin Shan63803c32016-05-20 16:41:32 +1000285 * Exclude the segments for reserved and root bus PE, which
286 * are first or last two PEs.
Gavin Shan99451552016-05-05 12:02:13 +1000287 */
288 r = &phb->hose->mem_resources[1];
289 if (phb->ioda.reserved_pe_idx == 0)
Gavin Shan63803c32016-05-20 16:41:32 +1000290 r->start += (2 * phb->ioda.m64_segsize);
Gavin Shan99451552016-05-05 12:02:13 +1000291 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Gavin Shan63803c32016-05-20 16:41:32 +1000292 r->end -= (2 * phb->ioda.m64_segsize);
Gavin Shan99451552016-05-05 12:02:13 +1000293 else
294 WARN(1, "Wrong reserved PE#%d on PHB#%d\n",
295 phb->ioda.reserved_pe_idx, phb->hose->global_number);
296
297 return 0;
298
299fail:
300 for ( ; index >= 0; index--)
301 opal_pci_phb_mmio_enable(phb->opal_id,
302 OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
303
304 return -EIO;
305}
306
Gavin Shanc4306702016-05-03 15:41:30 +1000307static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
308 unsigned long *pe_bitmap,
309 bool all)
Guo Chao262af552014-07-21 14:42:30 +1000310{
Guo Chao262af552014-07-21 14:42:30 +1000311 struct pci_dev *pdev;
Gavin Shan96a2f922015-06-19 12:26:17 +1000312
313 list_for_each_entry(pdev, &bus->devices, bus_list) {
Gavin Shanc4306702016-05-03 15:41:30 +1000314 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
Gavin Shan96a2f922015-06-19 12:26:17 +1000315
316 if (all && pdev->subordinate)
Gavin Shanc4306702016-05-03 15:41:30 +1000317 pnv_ioda_reserve_m64_pe(pdev->subordinate,
318 pe_bitmap, all);
Gavin Shan96a2f922015-06-19 12:26:17 +1000319 }
320}
321
Gavin Shan1e916772016-05-03 15:41:36 +1000322static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
Guo Chao262af552014-07-21 14:42:30 +1000323{
Gavin Shan26ba2482015-06-19 12:26:19 +1000324 struct pci_controller *hose = pci_bus_to_host(bus);
325 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000326 struct pnv_ioda_pe *master_pe, *pe;
327 unsigned long size, *pe_alloc;
Gavin Shan26ba2482015-06-19 12:26:19 +1000328 int i;
Guo Chao262af552014-07-21 14:42:30 +1000329
330 /* Root bus shouldn't use M64 */
331 if (pci_is_root_bus(bus))
Gavin Shan1e916772016-05-03 15:41:36 +1000332 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000333
Guo Chao262af552014-07-21 14:42:30 +1000334 /* Allocate bitmap */
Gavin Shan92b8f132016-05-03 15:41:24 +1000335 size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
Guo Chao262af552014-07-21 14:42:30 +1000336 pe_alloc = kzalloc(size, GFP_KERNEL);
337 if (!pe_alloc) {
338 pr_warn("%s: Out of memory !\n",
339 __func__);
Gavin Shan1e916772016-05-03 15:41:36 +1000340 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000341 }
342
Gavin Shan26ba2482015-06-19 12:26:19 +1000343 /* Figure out reserved PE numbers by the PE */
Gavin Shanc4306702016-05-03 15:41:30 +1000344 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
Guo Chao262af552014-07-21 14:42:30 +1000345
346 /*
347 * the current bus might not own M64 window and that's all
348 * contributed by its child buses. For the case, we needn't
349 * pick M64 dependent PE#.
350 */
Gavin Shan92b8f132016-05-03 15:41:24 +1000351 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
Guo Chao262af552014-07-21 14:42:30 +1000352 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000353 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000354 }
355
356 /*
357 * Figure out the master PE and put all slave PEs to master
358 * PE's list to form compound PE.
359 */
Guo Chao262af552014-07-21 14:42:30 +1000360 master_pe = NULL;
361 i = -1;
Gavin Shan92b8f132016-05-03 15:41:24 +1000362 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
363 phb->ioda.total_pe_num) {
Guo Chao262af552014-07-21 14:42:30 +1000364 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000365
Gavin Shan93289d82016-05-03 15:41:29 +1000366 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
Guo Chao262af552014-07-21 14:42:30 +1000367 if (!master_pe) {
368 pe->flags |= PNV_IODA_PE_MASTER;
369 INIT_LIST_HEAD(&pe->slaves);
370 master_pe = pe;
371 } else {
372 pe->flags |= PNV_IODA_PE_SLAVE;
373 pe->master = master_pe;
374 list_add_tail(&pe->list, &master_pe->slaves);
375 }
Gavin Shan99451552016-05-05 12:02:13 +1000376
377 /*
378 * P7IOC supports M64DT, which helps mapping M64 segment
379 * to one particular PE#. However, PHB3 has fixed mapping
380 * between M64 segment and PE#. In order to have same logic
381 * for P7IOC and PHB3, we enforce fixed mapping between M64
382 * segment and PE# on P7IOC.
383 */
384 if (phb->type == PNV_PHB_IODA1) {
385 int64_t rc;
386
387 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
388 pe->pe_number, OPAL_M64_WINDOW_TYPE,
389 pe->pe_number / PNV_IODA1_M64_SEGS,
390 pe->pe_number % PNV_IODA1_M64_SEGS);
391 if (rc != OPAL_SUCCESS)
392 pr_warn("%s: Error %lld mapping M64 for PHB#%d-PE#%d\n",
393 __func__, rc, phb->hose->global_number,
394 pe->pe_number);
395 }
Guo Chao262af552014-07-21 14:42:30 +1000396 }
397
398 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000399 return master_pe;
Guo Chao262af552014-07-21 14:42:30 +1000400}
401
402static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
403{
404 struct pci_controller *hose = phb->hose;
405 struct device_node *dn = hose->dn;
406 struct resource *res;
407 const u32 *r;
408 u64 pci_addr;
409
Gavin Shan99451552016-05-05 12:02:13 +1000410 if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
Gavin Shan1665c4a2014-11-12 13:36:04 +1100411 pr_info(" Not support M64 window\n");
412 return;
413 }
414
Stewart Smithe4d54f72015-12-09 17:18:20 +1100415 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
Guo Chao262af552014-07-21 14:42:30 +1000416 pr_info(" Firmware too old to support M64 window\n");
417 return;
418 }
419
420 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
421 if (!r) {
422 pr_info(" No <ibm,opal-m64-window> on %s\n",
423 dn->full_name);
424 return;
425 }
426
Guo Chao262af552014-07-21 14:42:30 +1000427 res = &hose->mem_resources[1];
Gavin Shane80c4e72015-10-22 12:03:08 +1100428 res->name = dn->full_name;
Guo Chao262af552014-07-21 14:42:30 +1000429 res->start = of_translate_address(dn, r + 2);
430 res->end = res->start + of_read_number(r + 4, 2) - 1;
431 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
432 pci_addr = of_read_number(r, 2);
433 hose->mem_offset[1] = res->start - pci_addr;
434
435 phb->ioda.m64_size = resource_size(res);
Gavin Shan92b8f132016-05-03 15:41:24 +1000436 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
Guo Chao262af552014-07-21 14:42:30 +1000437 phb->ioda.m64_base = pci_addr;
438
Wei Yange9863e62014-12-12 12:39:37 +0800439 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
440 res->start, res->end, pci_addr);
441
Guo Chao262af552014-07-21 14:42:30 +1000442 /* Use last M64 BAR to cover M64 window */
443 phb->ioda.m64_bar_idx = 15;
Gavin Shan99451552016-05-05 12:02:13 +1000444 if (phb->type == PNV_PHB_IODA1)
445 phb->init_m64 = pnv_ioda1_init_m64;
446 else
447 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shanc4306702016-05-03 15:41:30 +1000448 phb->reserve_m64_pe = pnv_ioda_reserve_m64_pe;
449 phb->pick_m64_pe = pnv_ioda_pick_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000450}
451
Gavin Shan49dec922014-07-21 14:42:33 +1000452static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
453{
454 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
455 struct pnv_ioda_pe *slave;
456 s64 rc;
457
458 /* Fetch master PE */
459 if (pe->flags & PNV_IODA_PE_SLAVE) {
460 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100461 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
462 return;
463
Gavin Shan49dec922014-07-21 14:42:33 +1000464 pe_no = pe->pe_number;
465 }
466
467 /* Freeze master PE */
468 rc = opal_pci_eeh_freeze_set(phb->opal_id,
469 pe_no,
470 OPAL_EEH_ACTION_SET_FREEZE_ALL);
471 if (rc != OPAL_SUCCESS) {
472 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
473 __func__, rc, phb->hose->global_number, pe_no);
474 return;
475 }
476
477 /* Freeze slave PEs */
478 if (!(pe->flags & PNV_IODA_PE_MASTER))
479 return;
480
481 list_for_each_entry(slave, &pe->slaves, list) {
482 rc = opal_pci_eeh_freeze_set(phb->opal_id,
483 slave->pe_number,
484 OPAL_EEH_ACTION_SET_FREEZE_ALL);
485 if (rc != OPAL_SUCCESS)
486 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
487 __func__, rc, phb->hose->global_number,
488 slave->pe_number);
489 }
490}
491
Anton Blancharde51df2c2014-08-20 08:55:18 +1000492static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000493{
494 struct pnv_ioda_pe *pe, *slave;
495 s64 rc;
496
497 /* Find master PE */
498 pe = &phb->ioda.pe_array[pe_no];
499 if (pe->flags & PNV_IODA_PE_SLAVE) {
500 pe = pe->master;
501 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
502 pe_no = pe->pe_number;
503 }
504
505 /* Clear frozen state for master PE */
506 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
507 if (rc != OPAL_SUCCESS) {
508 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
509 __func__, rc, opt, phb->hose->global_number, pe_no);
510 return -EIO;
511 }
512
513 if (!(pe->flags & PNV_IODA_PE_MASTER))
514 return 0;
515
516 /* Clear frozen state for slave PEs */
517 list_for_each_entry(slave, &pe->slaves, list) {
518 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
519 slave->pe_number,
520 opt);
521 if (rc != OPAL_SUCCESS) {
522 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
523 __func__, rc, opt, phb->hose->global_number,
524 slave->pe_number);
525 return -EIO;
526 }
527 }
528
529 return 0;
530}
531
532static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
533{
534 struct pnv_ioda_pe *slave, *pe;
535 u8 fstate, state;
536 __be16 pcierr;
537 s64 rc;
538
539 /* Sanity check on PE number */
Gavin Shan92b8f132016-05-03 15:41:24 +1000540 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
Gavin Shan49dec922014-07-21 14:42:33 +1000541 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
542
543 /*
544 * Fetch the master PE and the PE instance might be
545 * not initialized yet.
546 */
547 pe = &phb->ioda.pe_array[pe_no];
548 if (pe->flags & PNV_IODA_PE_SLAVE) {
549 pe = pe->master;
550 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
551 pe_no = pe->pe_number;
552 }
553
554 /* Check the master PE */
555 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
556 &state, &pcierr, NULL);
557 if (rc != OPAL_SUCCESS) {
558 pr_warn("%s: Failure %lld getting "
559 "PHB#%x-PE#%x state\n",
560 __func__, rc,
561 phb->hose->global_number, pe_no);
562 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
563 }
564
565 /* Check the slave PE */
566 if (!(pe->flags & PNV_IODA_PE_MASTER))
567 return state;
568
569 list_for_each_entry(slave, &pe->slaves, list) {
570 rc = opal_pci_eeh_freeze_status(phb->opal_id,
571 slave->pe_number,
572 &fstate,
573 &pcierr,
574 NULL);
575 if (rc != OPAL_SUCCESS) {
576 pr_warn("%s: Failure %lld getting "
577 "PHB#%x-PE#%x state\n",
578 __func__, rc,
579 phb->hose->global_number, slave->pe_number);
580 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
581 }
582
583 /*
584 * Override the result based on the ascending
585 * priority.
586 */
587 if (fstate > state)
588 state = fstate;
589 }
590
591 return state;
592}
593
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000594/* Currently those 2 are only used when MSIs are enabled, this will change
595 * but in the meantime, we need to protect them to avoid warnings
596 */
597#ifdef CONFIG_PCI_MSI
Ian Munsief4568342016-07-14 07:17:00 +1000598struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000599{
600 struct pci_controller *hose = pci_bus_to_host(dev->bus);
601 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000602 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000603
604 if (!pdn)
605 return NULL;
606 if (pdn->pe_number == IODA_INVALID_PE)
607 return NULL;
608 return &phb->ioda.pe_array[pdn->pe_number];
609}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000610#endif /* CONFIG_PCI_MSI */
611
Gavin Shanb131a842014-11-12 13:36:08 +1100612static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
613 struct pnv_ioda_pe *parent,
614 struct pnv_ioda_pe *child,
615 bool is_add)
616{
617 const char *desc = is_add ? "adding" : "removing";
618 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
619 OPAL_REMOVE_PE_FROM_DOMAIN;
620 struct pnv_ioda_pe *slave;
621 long rc;
622
623 /* Parent PE affects child PE */
624 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
625 child->pe_number, op);
626 if (rc != OPAL_SUCCESS) {
627 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
628 rc, desc);
629 return -ENXIO;
630 }
631
632 if (!(child->flags & PNV_IODA_PE_MASTER))
633 return 0;
634
635 /* Compound case: parent PE affects slave PEs */
636 list_for_each_entry(slave, &child->slaves, list) {
637 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
638 slave->pe_number, op);
639 if (rc != OPAL_SUCCESS) {
640 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
641 rc, desc);
642 return -ENXIO;
643 }
644 }
645
646 return 0;
647}
648
649static int pnv_ioda_set_peltv(struct pnv_phb *phb,
650 struct pnv_ioda_pe *pe,
651 bool is_add)
652{
653 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800654 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100655 int ret;
656
657 /*
658 * Clear PE frozen state. If it's master PE, we need
659 * clear slave PE frozen state as well.
660 */
661 if (is_add) {
662 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
663 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
664 if (pe->flags & PNV_IODA_PE_MASTER) {
665 list_for_each_entry(slave, &pe->slaves, list)
666 opal_pci_eeh_freeze_clear(phb->opal_id,
667 slave->pe_number,
668 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
669 }
670 }
671
672 /*
673 * Associate PE in PELT. We need add the PE into the
674 * corresponding PELT-V as well. Otherwise, the error
675 * originated from the PE might contribute to other
676 * PEs.
677 */
678 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
679 if (ret)
680 return ret;
681
682 /* For compound PEs, any one affects all of them */
683 if (pe->flags & PNV_IODA_PE_MASTER) {
684 list_for_each_entry(slave, &pe->slaves, list) {
685 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
686 if (ret)
687 return ret;
688 }
689 }
690
691 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
692 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800693 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100694 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800695#ifdef CONFIG_PCI_IOV
696 else if (pe->flags & PNV_IODA_PE_VF)
Gavin Shan283e2d82015-06-22 13:45:47 +1000697 pdev = pe->parent_dev;
Wei Yang781a8682015-03-25 16:23:57 +0800698#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100699 while (pdev) {
700 struct pci_dn *pdn = pci_get_pdn(pdev);
701 struct pnv_ioda_pe *parent;
702
703 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
704 parent = &phb->ioda.pe_array[pdn->pe_number];
705 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
706 if (ret)
707 return ret;
708 }
709
710 pdev = pdev->bus->self;
711 }
712
713 return 0;
714}
715
Wei Yang781a8682015-03-25 16:23:57 +0800716static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
717{
718 struct pci_dev *parent;
719 uint8_t bcomp, dcomp, fcomp;
720 int64_t rc;
721 long rid_end, rid;
722
723 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
724 if (pe->pbus) {
725 int count;
726
727 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
728 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
729 parent = pe->pbus->self;
730 if (pe->flags & PNV_IODA_PE_BUS_ALL)
731 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
732 else
733 count = 1;
734
735 switch(count) {
736 case 1: bcomp = OpalPciBusAll; break;
737 case 2: bcomp = OpalPciBus7Bits; break;
738 case 4: bcomp = OpalPciBus6Bits; break;
739 case 8: bcomp = OpalPciBus5Bits; break;
740 case 16: bcomp = OpalPciBus4Bits; break;
741 case 32: bcomp = OpalPciBus3Bits; break;
742 default:
743 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
744 count);
745 /* Do an exact match only */
746 bcomp = OpalPciBusAll;
747 }
748 rid_end = pe->rid + (count << 8);
749 } else {
Gavin Shan93e01a52016-05-20 16:41:34 +1000750#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +0800751 if (pe->flags & PNV_IODA_PE_VF)
752 parent = pe->parent_dev;
753 else
Gavin Shan93e01a52016-05-20 16:41:34 +1000754#endif
Wei Yang781a8682015-03-25 16:23:57 +0800755 parent = pe->pdev->bus->self;
756 bcomp = OpalPciBusAll;
757 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
758 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
759 rid_end = pe->rid + 1;
760 }
761
762 /* Clear the reverse map */
763 for (rid = pe->rid; rid < rid_end; rid++)
Gavin Shanc1275622016-05-20 16:41:29 +1000764 phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
Wei Yang781a8682015-03-25 16:23:57 +0800765
766 /* Release from all parents PELT-V */
767 while (parent) {
768 struct pci_dn *pdn = pci_get_pdn(parent);
769 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
770 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
771 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
772 /* XXX What to do in case of error ? */
773 }
774 parent = parent->bus->self;
775 }
776
Gavin Shanf951e512015-06-23 17:01:13 +1000777 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
Wei Yang781a8682015-03-25 16:23:57 +0800778 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
779
780 /* Disassociate PE in PELT */
781 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
782 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
783 if (rc)
784 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
785 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
786 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
787 if (rc)
788 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
789
790 pe->pbus = NULL;
791 pe->pdev = NULL;
Gavin Shan93e01a52016-05-20 16:41:34 +1000792#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +0800793 pe->parent_dev = NULL;
Gavin Shan93e01a52016-05-20 16:41:34 +1000794#endif
Wei Yang781a8682015-03-25 16:23:57 +0800795
796 return 0;
797}
Wei Yang781a8682015-03-25 16:23:57 +0800798
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800799static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000800{
801 struct pci_dev *parent;
802 uint8_t bcomp, dcomp, fcomp;
803 long rc, rid_end, rid;
804
805 /* Bus validation ? */
806 if (pe->pbus) {
807 int count;
808
809 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
810 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
811 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000812 if (pe->flags & PNV_IODA_PE_BUS_ALL)
813 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
814 else
815 count = 1;
816
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000817 switch(count) {
818 case 1: bcomp = OpalPciBusAll; break;
819 case 2: bcomp = OpalPciBus7Bits; break;
820 case 4: bcomp = OpalPciBus6Bits; break;
821 case 8: bcomp = OpalPciBus5Bits; break;
822 case 16: bcomp = OpalPciBus4Bits; break;
823 case 32: bcomp = OpalPciBus3Bits; break;
824 default:
Wei Yang781a8682015-03-25 16:23:57 +0800825 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
826 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000827 /* Do an exact match only */
828 bcomp = OpalPciBusAll;
829 }
830 rid_end = pe->rid + (count << 8);
831 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800832#ifdef CONFIG_PCI_IOV
833 if (pe->flags & PNV_IODA_PE_VF)
834 parent = pe->parent_dev;
835 else
836#endif /* CONFIG_PCI_IOV */
837 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000838 bcomp = OpalPciBusAll;
839 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
840 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
841 rid_end = pe->rid + 1;
842 }
843
Gavin Shan631ad692013-11-04 16:32:46 +0800844 /*
845 * Associate PE in PELT. We need add the PE into the
846 * corresponding PELT-V as well. Otherwise, the error
847 * originated from the PE might contribute to other
848 * PEs.
849 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000850 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
851 bcomp, dcomp, fcomp, OPAL_MAP_PE);
852 if (rc) {
853 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
854 return -ENXIO;
855 }
Gavin Shan631ad692013-11-04 16:32:46 +0800856
Alistair Popple5d2aa712015-12-17 13:43:13 +1100857 /*
858 * Configure PELTV. NPUs don't have a PELTV table so skip
859 * configuration on them.
860 */
861 if (phb->type != PNV_PHB_NPU)
862 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000863
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000864 /* Setup reverse map */
865 for (rid = pe->rid; rid < rid_end; rid++)
866 phb->ioda.pe_rmap[rid] = pe->pe_number;
867
868 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100869 if (phb->type != PNV_PHB_IODA1) {
870 pe->mve_number = 0;
871 goto out;
872 }
873
874 pe->mve_number = pe->pe_number;
875 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
876 if (rc != OPAL_SUCCESS) {
877 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
878 rc, pe->mve_number);
879 pe->mve_number = -1;
880 } else {
881 rc = opal_pci_set_mve_enable(phb->opal_id,
882 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000883 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100884 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000885 rc, pe->mve_number);
886 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000887 }
Gavin Shan4773f762014-11-12 13:36:09 +1100888 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000889
Gavin Shan4773f762014-11-12 13:36:09 +1100890out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000891 return 0;
892}
893
Wei Yang781a8682015-03-25 16:23:57 +0800894#ifdef CONFIG_PCI_IOV
895static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
896{
897 struct pci_dn *pdn = pci_get_pdn(dev);
898 int i;
899 struct resource *res, res2;
900 resource_size_t size;
901 u16 num_vfs;
902
903 if (!dev->is_physfn)
904 return -EINVAL;
905
906 /*
907 * "offset" is in VFs. The M64 windows are sized so that when they
908 * are segmented, each segment is the same size as the IOV BAR.
909 * Each segment is in a separate PE, and the high order bits of the
910 * address are the PE number. Therefore, each VF's BAR is in a
911 * separate PE, and changing the IOV BAR start address changes the
912 * range of PEs the VFs are in.
913 */
914 num_vfs = pdn->num_vfs;
915 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
916 res = &dev->resource[i + PCI_IOV_RESOURCES];
917 if (!res->flags || !res->parent)
918 continue;
919
Wei Yang781a8682015-03-25 16:23:57 +0800920 /*
921 * The actual IOV BAR range is determined by the start address
922 * and the actual size for num_vfs VFs BAR. This check is to
923 * make sure that after shifting, the range will not overlap
924 * with another device.
925 */
926 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
927 res2.flags = res->flags;
928 res2.start = res->start + (size * offset);
929 res2.end = res2.start + (size * num_vfs) - 1;
930
931 if (res2.end > res->end) {
932 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
933 i, &res2, res, num_vfs, offset);
934 return -EBUSY;
935 }
936 }
937
938 /*
939 * After doing so, there would be a "hole" in the /proc/iomem when
940 * offset is a positive value. It looks like the device return some
941 * mmio back to the system, which actually no one could use it.
942 */
943 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
944 res = &dev->resource[i + PCI_IOV_RESOURCES];
945 if (!res->flags || !res->parent)
946 continue;
947
Wei Yang781a8682015-03-25 16:23:57 +0800948 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
949 res2 = *res;
950 res->start += size * offset;
951
Wei Yang74703cc2015-07-20 18:14:58 +0800952 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
953 i, &res2, res, (offset > 0) ? "En" : "Dis",
954 num_vfs, offset);
Wei Yang781a8682015-03-25 16:23:57 +0800955 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
956 }
957 return 0;
958}
959#endif /* CONFIG_PCI_IOV */
960
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800961static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000962{
963 struct pci_controller *hose = pci_bus_to_host(dev->bus);
964 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000965 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000966 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000967
968 if (!pdn) {
969 pr_err("%s: Device tree node not associated properly\n",
970 pci_name(dev));
971 return NULL;
972 }
973 if (pdn->pe_number != IODA_INVALID_PE)
974 return NULL;
975
Gavin Shan1e916772016-05-03 15:41:36 +1000976 pe = pnv_ioda_alloc_pe(phb);
977 if (!pe) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000978 pr_warning("%s: Not enough PE# available, disabling device\n",
979 pci_name(dev));
980 return NULL;
981 }
982
983 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
984 * pointer in the PE data structure, both should be destroyed at the
985 * same time. However, this needs to be looked at more closely again
986 * once we actually start removing things (Hotplug, SR-IOV, ...)
987 *
988 * At some point we want to remove the PDN completely anyways
989 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000990 pci_dev_get(dev);
991 pdn->pcidev = dev;
Gavin Shan1e916772016-05-03 15:41:36 +1000992 pdn->pe_number = pe->pe_number;
Alistair Popple5d2aa712015-12-17 13:43:13 +1100993 pe->flags = PNV_IODA_PE_DEV;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000994 pe->pdev = dev;
995 pe->pbus = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000996 pe->mve_number = -1;
997 pe->rid = dev->bus->number << 8 | pdn->devfn;
998
999 pe_info(pe, "Associated device to PE\n");
1000
1001 if (pnv_ioda_configure_pe(phb, pe)) {
1002 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001003 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001004 pdn->pe_number = IODA_INVALID_PE;
1005 pe->pdev = NULL;
1006 pci_dev_put(dev);
1007 return NULL;
1008 }
1009
Alexey Kardashevskiy1d4e89c2016-05-12 15:47:10 +10001010 /* Put PE to the list */
1011 list_add_tail(&pe->list, &phb->ioda.pe_list);
1012
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001013 return pe;
1014}
1015
1016static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1017{
1018 struct pci_dev *dev;
1019
1020 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001021 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001022
1023 if (pdn == NULL) {
1024 pr_warn("%s: No device node associated with device !\n",
1025 pci_name(dev));
1026 continue;
1027 }
Gavin Shanccd1c192016-05-20 16:41:31 +10001028
1029 /*
1030 * In partial hotplug case, the PCI device might be still
1031 * associated with the PE and needn't attach it to the PE
1032 * again.
1033 */
1034 if (pdn->pe_number != IODA_INVALID_PE)
1035 continue;
1036
Gavin Shanc5f77002016-05-20 16:41:35 +10001037 pe->device_count++;
Alistair Popple94973b22015-12-17 13:43:11 +11001038 pdn->pcidev = dev;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001039 pdn->pe_number = pe->pe_number;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001040 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001041 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1042 }
1043}
1044
Gavin Shanfb446ad2012-08-20 03:49:14 +00001045/*
1046 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1047 * single PCI bus. Another one that contains the primary PCI bus and its
1048 * subordinate PCI devices and buses. The second type of PE is normally
1049 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1050 */
Gavin Shan1e916772016-05-03 15:41:36 +10001051static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001052{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001053 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001054 struct pnv_phb *phb = hose->private_data;
Gavin Shan1e916772016-05-03 15:41:36 +10001055 struct pnv_ioda_pe *pe = NULL;
Gavin Shanccd1c192016-05-20 16:41:31 +10001056 unsigned int pe_num;
1057
1058 /*
1059 * In partial hotplug case, the PE instance might be still alive.
1060 * We should reuse it instead of allocating a new one.
1061 */
1062 pe_num = phb->ioda.pe_rmap[bus->number << 8];
1063 if (pe_num != IODA_INVALID_PE) {
1064 pe = &phb->ioda.pe_array[pe_num];
1065 pnv_ioda_setup_same_PE(bus, pe);
1066 return NULL;
1067 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001068
Gavin Shan63803c32016-05-20 16:41:32 +10001069 /* PE number for root bus should have been reserved */
1070 if (pci_is_root_bus(bus) &&
1071 phb->ioda.root_pe_idx != IODA_INVALID_PE)
1072 pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
1073
Guo Chao262af552014-07-21 14:42:30 +10001074 /* Check if PE is determined by M64 */
Gavin Shan63803c32016-05-20 16:41:32 +10001075 if (!pe && phb->pick_m64_pe)
Gavin Shan1e916772016-05-03 15:41:36 +10001076 pe = phb->pick_m64_pe(bus, all);
Guo Chao262af552014-07-21 14:42:30 +10001077
1078 /* The PE number isn't pinned by M64 */
Gavin Shan1e916772016-05-03 15:41:36 +10001079 if (!pe)
1080 pe = pnv_ioda_alloc_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +10001081
Gavin Shan1e916772016-05-03 15:41:36 +10001082 if (!pe) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001083 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1084 __func__, pci_domain_nr(bus), bus->number);
Gavin Shan1e916772016-05-03 15:41:36 +10001085 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001086 }
1087
Guo Chao262af552014-07-21 14:42:30 +10001088 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001089 pe->pbus = bus;
1090 pe->pdev = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001091 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001092 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001093
Gavin Shanfb446ad2012-08-20 03:49:14 +00001094 if (all)
1095 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001096 bus->busn_res.start, bus->busn_res.end, pe->pe_number);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001097 else
1098 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001099 bus->busn_res.start, pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001100
1101 if (pnv_ioda_configure_pe(phb, pe)) {
1102 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001103 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001104 pe->pbus = NULL;
Gavin Shan1e916772016-05-03 15:41:36 +10001105 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001106 }
1107
1108 /* Associate it with all child devices */
1109 pnv_ioda_setup_same_PE(bus, pe);
1110
Gavin Shan7ebdf952012-08-20 03:49:15 +00001111 /* Put PE to the list */
1112 list_add_tail(&pe->list, &phb->ioda.pe_list);
Gavin Shan1e916772016-05-03 15:41:36 +10001113
1114 return pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001115}
1116
Alistair Poppleb5215492016-01-11 16:53:49 +11001117static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev)
Alistair Popple5d2aa712015-12-17 13:43:13 +11001118{
Alistair Poppleb5215492016-01-11 16:53:49 +11001119 int pe_num, found_pe = false, rc;
1120 long rid;
1121 struct pnv_ioda_pe *pe;
1122 struct pci_dev *gpu_pdev;
1123 struct pci_dn *npu_pdn;
1124 struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus);
1125 struct pnv_phb *phb = hose->private_data;
1126
1127 /*
1128 * Due to a hardware errata PE#0 on the NPU is reserved for
1129 * error handling. This means we only have three PEs remaining
1130 * which need to be assigned to four links, implying some
1131 * links must share PEs.
1132 *
1133 * To achieve this we assign PEs such that NPUs linking the
1134 * same GPU get assigned the same PE.
1135 */
1136 gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10001137 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
Alistair Poppleb5215492016-01-11 16:53:49 +11001138 pe = &phb->ioda.pe_array[pe_num];
1139 if (!pe->pdev)
1140 continue;
1141
1142 if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) {
1143 /*
1144 * This device has the same peer GPU so should
1145 * be assigned the same PE as the existing
1146 * peer NPU.
1147 */
1148 dev_info(&npu_pdev->dev,
1149 "Associating to existing PE %d\n", pe_num);
1150 pci_dev_get(npu_pdev);
1151 npu_pdn = pci_get_pdn(npu_pdev);
1152 rid = npu_pdev->bus->number << 8 | npu_pdn->devfn;
1153 npu_pdn->pcidev = npu_pdev;
1154 npu_pdn->pe_number = pe_num;
Alistair Poppleb5215492016-01-11 16:53:49 +11001155 phb->ioda.pe_rmap[rid] = pe->pe_number;
1156
1157 /* Map the PE to this link */
1158 rc = opal_pci_set_pe(phb->opal_id, pe_num, rid,
1159 OpalPciBusAll,
1160 OPAL_COMPARE_RID_DEVICE_NUMBER,
1161 OPAL_COMPARE_RID_FUNCTION_NUMBER,
1162 OPAL_MAP_PE);
1163 WARN_ON(rc != OPAL_SUCCESS);
1164 found_pe = true;
1165 break;
1166 }
1167 }
1168
1169 if (!found_pe)
1170 /*
1171 * Could not find an existing PE so allocate a new
1172 * one.
1173 */
1174 return pnv_ioda_setup_dev_PE(npu_pdev);
1175 else
1176 return pe;
1177}
1178
1179static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus)
1180{
Alistair Popple5d2aa712015-12-17 13:43:13 +11001181 struct pci_dev *pdev;
1182
1183 list_for_each_entry(pdev, &bus->devices, bus_list)
Alistair Poppleb5215492016-01-11 16:53:49 +11001184 pnv_ioda_setup_npu_PE(pdev);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001185}
1186
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001187static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001188{
1189 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001190 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001191
1192 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001193 phb = hose->private_data;
Alistair Popple08f48f32016-01-11 16:53:50 +11001194 if (phb->type == PNV_PHB_NPU) {
1195 /* PE#0 is needed for error reporting */
1196 pnv_ioda_reserve_pe(phb, 0);
Alistair Poppleb5215492016-01-11 16:53:49 +11001197 pnv_ioda_setup_npu_PEs(hose->bus);
Gavin Shanccd1c192016-05-20 16:41:31 +10001198 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001199 }
1200}
1201
Gavin Shana8b2f822015-03-25 16:23:52 +08001202#ifdef CONFIG_PCI_IOV
Wei Yangee8222f2015-10-22 09:22:16 +08001203static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001204{
1205 struct pci_bus *bus;
1206 struct pci_controller *hose;
1207 struct pnv_phb *phb;
1208 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001209 int i, j;
Wei Yangee8222f2015-10-22 09:22:16 +08001210 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001211
1212 bus = pdev->bus;
1213 hose = pci_bus_to_host(bus);
1214 phb = hose->private_data;
1215 pdn = pci_get_pdn(pdev);
1216
Wei Yangee8222f2015-10-22 09:22:16 +08001217 if (pdn->m64_single_mode)
1218 m64_bars = num_vfs;
1219 else
1220 m64_bars = 1;
1221
Wei Yang02639b02015-03-25 16:23:59 +08001222 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yangee8222f2015-10-22 09:22:16 +08001223 for (j = 0; j < m64_bars; j++) {
1224 if (pdn->m64_map[j][i] == IODA_INVALID_M64)
Wei Yang02639b02015-03-25 16:23:59 +08001225 continue;
1226 opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001227 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0);
1228 clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc);
1229 pdn->m64_map[j][i] = IODA_INVALID_M64;
Wei Yang02639b02015-03-25 16:23:59 +08001230 }
Wei Yang781a8682015-03-25 16:23:57 +08001231
Wei Yangee8222f2015-10-22 09:22:16 +08001232 kfree(pdn->m64_map);
Wei Yang781a8682015-03-25 16:23:57 +08001233 return 0;
1234}
1235
Wei Yang02639b02015-03-25 16:23:59 +08001236static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001237{
1238 struct pci_bus *bus;
1239 struct pci_controller *hose;
1240 struct pnv_phb *phb;
1241 struct pci_dn *pdn;
1242 unsigned int win;
1243 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001244 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001245 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001246 int total_vfs;
1247 resource_size_t size, start;
1248 int pe_num;
Wei Yangee8222f2015-10-22 09:22:16 +08001249 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001250
1251 bus = pdev->bus;
1252 hose = pci_bus_to_host(bus);
1253 phb = hose->private_data;
1254 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001255 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001256
Wei Yangee8222f2015-10-22 09:22:16 +08001257 if (pdn->m64_single_mode)
1258 m64_bars = num_vfs;
1259 else
1260 m64_bars = 1;
Wei Yang02639b02015-03-25 16:23:59 +08001261
Wei Yangee8222f2015-10-22 09:22:16 +08001262 pdn->m64_map = kmalloc(sizeof(*pdn->m64_map) * m64_bars, GFP_KERNEL);
1263 if (!pdn->m64_map)
1264 return -ENOMEM;
1265 /* Initialize the m64_map to IODA_INVALID_M64 */
1266 for (i = 0; i < m64_bars ; i++)
1267 for (j = 0; j < PCI_SRIOV_NUM_BARS; j++)
1268 pdn->m64_map[i][j] = IODA_INVALID_M64;
1269
Wei Yang781a8682015-03-25 16:23:57 +08001270
1271 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1272 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1273 if (!res->flags || !res->parent)
1274 continue;
1275
Wei Yangee8222f2015-10-22 09:22:16 +08001276 for (j = 0; j < m64_bars; j++) {
Wei Yang02639b02015-03-25 16:23:59 +08001277 do {
1278 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1279 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001280
Wei Yang02639b02015-03-25 16:23:59 +08001281 if (win >= phb->ioda.m64_bar_idx + 1)
1282 goto m64_failed;
1283 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001284
Wei Yangee8222f2015-10-22 09:22:16 +08001285 pdn->m64_map[j][i] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001286
Wei Yangee8222f2015-10-22 09:22:16 +08001287 if (pdn->m64_single_mode) {
Wei Yang02639b02015-03-25 16:23:59 +08001288 size = pci_iov_resource_size(pdev,
1289 PCI_IOV_RESOURCES + i);
Wei Yang02639b02015-03-25 16:23:59 +08001290 start = res->start + size * j;
1291 } else {
1292 size = resource_size(res);
1293 start = res->start;
1294 }
1295
1296 /* Map the M64 here */
Wei Yangee8222f2015-10-22 09:22:16 +08001297 if (pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001298 pe_num = pdn->pe_num_map[j];
Wei Yang02639b02015-03-25 16:23:59 +08001299 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1300 pe_num, OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001301 pdn->m64_map[j][i], 0);
Wei Yang02639b02015-03-25 16:23:59 +08001302 }
1303
1304 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001305 OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001306 pdn->m64_map[j][i],
Wei Yang02639b02015-03-25 16:23:59 +08001307 start,
Wei Yang781a8682015-03-25 16:23:57 +08001308 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001309 size);
Wei Yang781a8682015-03-25 16:23:57 +08001310
Wei Yang02639b02015-03-25 16:23:59 +08001311
1312 if (rc != OPAL_SUCCESS) {
1313 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1314 win, rc);
1315 goto m64_failed;
1316 }
1317
Wei Yangee8222f2015-10-22 09:22:16 +08001318 if (pdn->m64_single_mode)
Wei Yang02639b02015-03-25 16:23:59 +08001319 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001320 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2);
Wei Yang02639b02015-03-25 16:23:59 +08001321 else
1322 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001323 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1);
Wei Yang02639b02015-03-25 16:23:59 +08001324
1325 if (rc != OPAL_SUCCESS) {
1326 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1327 win, rc);
1328 goto m64_failed;
1329 }
Wei Yang781a8682015-03-25 16:23:57 +08001330 }
1331 }
1332 return 0;
1333
1334m64_failed:
Wei Yangee8222f2015-10-22 09:22:16 +08001335 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001336 return -EBUSY;
1337}
1338
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001339static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1340 int num);
1341static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1342
Wei Yang781a8682015-03-25 16:23:57 +08001343static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1344{
Wei Yang781a8682015-03-25 16:23:57 +08001345 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001346 int64_t rc;
1347
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001348 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001349 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001350 if (rc)
1351 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1352
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001353 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001354 if (pe->table_group.group) {
1355 iommu_group_put(pe->table_group.group);
1356 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001357 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001358 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001359 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001360}
1361
Wei Yangee8222f2015-10-22 09:22:16 +08001362static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
Wei Yang781a8682015-03-25 16:23:57 +08001363{
1364 struct pci_bus *bus;
1365 struct pci_controller *hose;
1366 struct pnv_phb *phb;
1367 struct pnv_ioda_pe *pe, *pe_n;
1368 struct pci_dn *pdn;
1369
1370 bus = pdev->bus;
1371 hose = pci_bus_to_host(bus);
1372 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001373 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001374
1375 if (!pdev->is_physfn)
1376 return;
1377
Wei Yang781a8682015-03-25 16:23:57 +08001378 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1379 if (pe->parent_dev != pdev)
1380 continue;
1381
1382 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1383
1384 /* Remove from list */
1385 mutex_lock(&phb->ioda.pe_list_mutex);
1386 list_del(&pe->list);
1387 mutex_unlock(&phb->ioda.pe_list_mutex);
1388
1389 pnv_ioda_deconfigure_pe(phb, pe);
1390
Gavin Shan1e916772016-05-03 15:41:36 +10001391 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001392 }
1393}
1394
1395void pnv_pci_sriov_disable(struct pci_dev *pdev)
1396{
1397 struct pci_bus *bus;
1398 struct pci_controller *hose;
1399 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001400 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001401 struct pci_dn *pdn;
1402 struct pci_sriov *iov;
Wei Yangbe283ee2015-10-22 09:22:19 +08001403 u16 num_vfs, i;
Wei Yang781a8682015-03-25 16:23:57 +08001404
1405 bus = pdev->bus;
1406 hose = pci_bus_to_host(bus);
1407 phb = hose->private_data;
1408 pdn = pci_get_pdn(pdev);
1409 iov = pdev->sriov;
1410 num_vfs = pdn->num_vfs;
1411
1412 /* Release VF PEs */
Wei Yangee8222f2015-10-22 09:22:16 +08001413 pnv_ioda_release_vf_PE(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001414
1415 if (phb->type == PNV_PHB_IODA2) {
Wei Yangee8222f2015-10-22 09:22:16 +08001416 if (!pdn->m64_single_mode)
Wei Yangbe283ee2015-10-22 09:22:19 +08001417 pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001418
1419 /* Release M64 windows */
Wei Yangee8222f2015-10-22 09:22:16 +08001420 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001421
1422 /* Release PE numbers */
Wei Yangbe283ee2015-10-22 09:22:19 +08001423 if (pdn->m64_single_mode) {
1424 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001425 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1426 continue;
1427
1428 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1429 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001430 }
1431 } else
1432 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1433 /* Releasing pe_num_map */
1434 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001435 }
1436}
1437
1438static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1439 struct pnv_ioda_pe *pe);
1440static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1441{
1442 struct pci_bus *bus;
1443 struct pci_controller *hose;
1444 struct pnv_phb *phb;
1445 struct pnv_ioda_pe *pe;
1446 int pe_num;
1447 u16 vf_index;
1448 struct pci_dn *pdn;
1449
1450 bus = pdev->bus;
1451 hose = pci_bus_to_host(bus);
1452 phb = hose->private_data;
1453 pdn = pci_get_pdn(pdev);
1454
1455 if (!pdev->is_physfn)
1456 return;
1457
1458 /* Reserve PE for each VF */
1459 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001460 if (pdn->m64_single_mode)
1461 pe_num = pdn->pe_num_map[vf_index];
1462 else
1463 pe_num = *pdn->pe_num_map + vf_index;
Wei Yang781a8682015-03-25 16:23:57 +08001464
1465 pe = &phb->ioda.pe_array[pe_num];
1466 pe->pe_number = pe_num;
1467 pe->phb = phb;
1468 pe->flags = PNV_IODA_PE_VF;
1469 pe->pbus = NULL;
1470 pe->parent_dev = pdev;
Wei Yang781a8682015-03-25 16:23:57 +08001471 pe->mve_number = -1;
1472 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1473 pci_iov_virtfn_devfn(pdev, vf_index);
1474
1475 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1476 hose->global_number, pdev->bus->number,
1477 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1478 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1479
1480 if (pnv_ioda_configure_pe(phb, pe)) {
1481 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001482 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001483 pe->pdev = NULL;
1484 continue;
1485 }
1486
Wei Yang781a8682015-03-25 16:23:57 +08001487 /* Put PE to the list */
1488 mutex_lock(&phb->ioda.pe_list_mutex);
1489 list_add_tail(&pe->list, &phb->ioda.pe_list);
1490 mutex_unlock(&phb->ioda.pe_list_mutex);
1491
1492 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1493 }
1494}
1495
1496int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1497{
1498 struct pci_bus *bus;
1499 struct pci_controller *hose;
1500 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001501 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001502 struct pci_dn *pdn;
1503 int ret;
Wei Yangbe283ee2015-10-22 09:22:19 +08001504 u16 i;
Wei Yang781a8682015-03-25 16:23:57 +08001505
1506 bus = pdev->bus;
1507 hose = pci_bus_to_host(bus);
1508 phb = hose->private_data;
1509 pdn = pci_get_pdn(pdev);
1510
1511 if (phb->type == PNV_PHB_IODA2) {
Wei Yangb0331852015-10-22 09:22:14 +08001512 if (!pdn->vfs_expanded) {
1513 dev_info(&pdev->dev, "don't support this SRIOV device"
1514 " with non 64bit-prefetchable IOV BAR\n");
1515 return -ENOSPC;
1516 }
1517
Wei Yangee8222f2015-10-22 09:22:16 +08001518 /*
1519 * When M64 BARs functions in Single PE mode, the number of VFs
1520 * could be enabled must be less than the number of M64 BARs.
1521 */
1522 if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) {
1523 dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n");
1524 return -EBUSY;
1525 }
1526
Wei Yangbe283ee2015-10-22 09:22:19 +08001527 /* Allocating pe_num_map */
1528 if (pdn->m64_single_mode)
1529 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map) * num_vfs,
1530 GFP_KERNEL);
1531 else
1532 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL);
1533
1534 if (!pdn->pe_num_map)
1535 return -ENOMEM;
1536
1537 if (pdn->m64_single_mode)
1538 for (i = 0; i < num_vfs; i++)
1539 pdn->pe_num_map[i] = IODA_INVALID_PE;
1540
Wei Yang781a8682015-03-25 16:23:57 +08001541 /* Calculate available PE for required VFs */
Wei Yangbe283ee2015-10-22 09:22:19 +08001542 if (pdn->m64_single_mode) {
1543 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001544 pe = pnv_ioda_alloc_pe(phb);
1545 if (!pe) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001546 ret = -EBUSY;
1547 goto m64_failed;
1548 }
Gavin Shan1e916772016-05-03 15:41:36 +10001549
1550 pdn->pe_num_map[i] = pe->pe_number;
Wei Yangbe283ee2015-10-22 09:22:19 +08001551 }
1552 } else {
1553 mutex_lock(&phb->ioda.pe_alloc_mutex);
1554 *pdn->pe_num_map = bitmap_find_next_zero_area(
Gavin Shan92b8f132016-05-03 15:41:24 +10001555 phb->ioda.pe_alloc, phb->ioda.total_pe_num,
Wei Yangbe283ee2015-10-22 09:22:19 +08001556 0, num_vfs, 0);
Gavin Shan92b8f132016-05-03 15:41:24 +10001557 if (*pdn->pe_num_map >= phb->ioda.total_pe_num) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001558 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1559 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1560 kfree(pdn->pe_num_map);
1561 return -EBUSY;
1562 }
1563 bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001564 mutex_unlock(&phb->ioda.pe_alloc_mutex);
Wei Yang781a8682015-03-25 16:23:57 +08001565 }
Wei Yang781a8682015-03-25 16:23:57 +08001566 pdn->num_vfs = num_vfs;
Wei Yang781a8682015-03-25 16:23:57 +08001567
1568 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001569 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001570 if (ret) {
1571 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1572 goto m64_failed;
1573 }
1574
1575 /*
1576 * When using one M64 BAR to map one IOV BAR, we need to shift
1577 * the IOV BAR according to the PE# allocated to the VFs.
1578 * Otherwise, the PE# for the VF will conflict with others.
1579 */
Wei Yangee8222f2015-10-22 09:22:16 +08001580 if (!pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001581 ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map);
Wei Yang02639b02015-03-25 16:23:59 +08001582 if (ret)
1583 goto m64_failed;
1584 }
Wei Yang781a8682015-03-25 16:23:57 +08001585 }
1586
1587 /* Setup VF PEs */
1588 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1589
1590 return 0;
1591
1592m64_failed:
Wei Yangbe283ee2015-10-22 09:22:19 +08001593 if (pdn->m64_single_mode) {
1594 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001595 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1596 continue;
1597
1598 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1599 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001600 }
1601 } else
1602 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1603
1604 /* Releasing pe_num_map */
1605 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001606
1607 return ret;
1608}
1609
Gavin Shana8b2f822015-03-25 16:23:52 +08001610int pcibios_sriov_disable(struct pci_dev *pdev)
1611{
Wei Yang781a8682015-03-25 16:23:57 +08001612 pnv_pci_sriov_disable(pdev);
1613
Gavin Shana8b2f822015-03-25 16:23:52 +08001614 /* Release PCI data */
1615 remove_dev_pci_data(pdev);
1616 return 0;
1617}
1618
1619int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1620{
1621 /* Allocate PCI data */
1622 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001623
Wei Yangee8222f2015-10-22 09:22:16 +08001624 return pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001625}
1626#endif /* CONFIG_PCI_IOV */
1627
Gavin Shan959c9bd2013-04-25 19:21:02 +00001628static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001629{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001630 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001631 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001632
Gavin Shan959c9bd2013-04-25 19:21:02 +00001633 /*
1634 * The function can be called while the PE#
1635 * hasn't been assigned. Do nothing for the
1636 * case.
1637 */
1638 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1639 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001640
Gavin Shan959c9bd2013-04-25 19:21:02 +00001641 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001642 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiy0e1ffef2015-08-27 16:01:16 +10001643 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001644 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001645 /*
1646 * Note: iommu_add_device() will fail here as
1647 * for physical PE: the device is already added by now;
1648 * for virtual PE: sysfs entries are not ready yet and
1649 * tce_iommu_bus_notifier will add the device to a group later.
1650 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001651}
1652
Daniel Axtens763d2d82015-04-28 15:12:07 +10001653static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001654{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001655 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1656 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001657 struct pci_dn *pdn = pci_get_pdn(pdev);
1658 struct pnv_ioda_pe *pe;
1659 uint64_t top;
1660 bool bypass = false;
1661
1662 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1663 return -ENODEV;;
1664
1665 pe = &phb->ioda.pe_array[pdn->pe_number];
1666 if (pe->tce_bypass_enabled) {
1667 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1668 bypass = (dma_mask >= top);
1669 }
1670
1671 if (bypass) {
1672 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1673 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001674 } else {
1675 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1676 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001677 }
Brian W Harta32305b2014-07-31 14:24:37 -05001678 *pdev->dev.dma_mask = dma_mask;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001679
1680 /* Update peer npu devices */
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10001681 pnv_npu_try_dma_set_bypass(pdev, bypass);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001682
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001683 return 0;
1684}
1685
Andrew Donnellan535229822015-08-07 13:45:54 +10001686static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001687{
Andrew Donnellan535229822015-08-07 13:45:54 +10001688 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1689 struct pnv_phb *phb = hose->private_data;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001690 struct pci_dn *pdn = pci_get_pdn(pdev);
1691 struct pnv_ioda_pe *pe;
1692 u64 end, mask;
1693
1694 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1695 return 0;
1696
1697 pe = &phb->ioda.pe_array[pdn->pe_number];
1698 if (!pe->tce_bypass_enabled)
1699 return __dma_get_required_mask(&pdev->dev);
1700
1701
1702 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1703 mask = 1ULL << (fls64(end) - 1);
1704 mask += mask - 1;
1705
1706 return mask;
1707}
1708
Gavin Shandff4a392014-07-15 17:00:55 +10001709static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001710 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001711{
1712 struct pci_dev *dev;
1713
1714 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001715 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001716 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001717 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001718
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001719 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001720 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001721 }
1722}
1723
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001724static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table *tbl,
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001725 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001726{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001727 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1728 &tbl->it_group_list, struct iommu_table_group_link,
1729 next);
1730 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001731 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001732 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001733 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1734 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001735 unsigned long start, end, inc;
1736
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001737 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1738 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1739 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001740
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001741 /* p7ioc-style invalidation, 2 TCEs per write */
1742 start |= (1ull << 63);
1743 end |= (1ull << 63);
1744 inc = 16;
Gavin Shan4cce9552013-04-25 19:21:00 +00001745 end |= inc - 1; /* round up end to be different than start */
1746
1747 mb(); /* Ensure above stores are visible */
1748 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001749 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001750 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001751 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001752 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001753 start += inc;
1754 }
1755
1756 /*
1757 * The iommu layer will do another mb() for us on build()
1758 * and we don't care on free()
1759 */
1760}
1761
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001762static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1763 long npages, unsigned long uaddr,
1764 enum dma_data_direction direction,
1765 struct dma_attrs *attrs)
1766{
1767 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1768 attrs);
1769
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001770 if (!ret)
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001771 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001772
1773 return ret;
1774}
1775
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001776#ifdef CONFIG_IOMMU_API
1777static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1778 unsigned long *hpa, enum dma_data_direction *direction)
1779{
1780 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1781
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001782 if (!ret)
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001783 pnv_pci_p7ioc_tce_invalidate(tbl, index, 1, false);
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001784
1785 return ret;
1786}
1787#endif
1788
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001789static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1790 long npages)
1791{
1792 pnv_tce_free(tbl, index, npages);
1793
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001794 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001795}
1796
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001797static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001798 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001799#ifdef CONFIG_IOMMU_API
1800 .exchange = pnv_ioda1_tce_xchg,
1801#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001802 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001803 .get = pnv_tce_get,
1804};
1805
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001806#define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)
1807#define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)
1808#define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)
Alexey Kardashevskiybef92532016-04-29 18:55:17 +10001809
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001810void pnv_pci_phb3_tce_invalidate_entire(struct pnv_phb *phb, bool rm)
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001811{
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001812 const unsigned long val = PHB3_TCE_KILL_INVAL_ALL;
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001813
1814 mb(); /* Ensure previous TCE table stores are visible */
1815 if (rm)
1816 __raw_rm_writeq(cpu_to_be64(val),
1817 (__be64 __iomem *)
1818 phb->ioda.tce_inval_reg_phys);
1819 else
1820 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1821}
1822
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001823static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001824{
1825 /* 01xb - invalidate TCEs that match the specified PE# */
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001826 unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001827 struct pnv_phb *phb = pe->phb;
1828
1829 if (!phb->ioda.tce_inval_reg)
1830 return;
1831
1832 mb(); /* Ensure above stores are visible */
1833 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1834}
1835
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001836static void pnv_pci_phb3_tce_invalidate(unsigned pe_number, bool rm,
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001837 __be64 __iomem *invalidate, unsigned shift,
1838 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001839{
1840 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001841
1842 /* We'll invalidate DMA address in PE scope */
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001843 start = PHB3_TCE_KILL_INVAL_ONE;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001844 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001845 end = start;
1846
1847 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001848 start |= (index << shift);
1849 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001850 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001851 mb();
1852
1853 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001854 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001855 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001856 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001857 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001858 start += inc;
1859 }
1860}
1861
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001862static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1863 unsigned long index, unsigned long npages, bool rm)
1864{
1865 struct iommu_table_group_link *tgl;
1866
1867 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
1868 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1869 struct pnv_ioda_pe, table_group);
1870 __be64 __iomem *invalidate = rm ?
1871 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1872 pe->phb->ioda.tce_inval_reg;
1873
Alexey Kardashevskiy85674862016-04-29 18:55:23 +10001874 if (pe->phb->type == PNV_PHB_NPU) {
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001875 /*
1876 * The NVLink hardware does not support TCE kill
1877 * per TCE entry so we have to invalidate
1878 * the entire cache for it.
1879 */
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001880 pnv_pci_phb3_tce_invalidate_entire(pe->phb, rm);
Alexey Kardashevskiy85674862016-04-29 18:55:23 +10001881 continue;
1882 }
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001883 pnv_pci_phb3_tce_invalidate(pe->pe_number, rm,
Alexey Kardashevskiy85674862016-04-29 18:55:23 +10001884 invalidate, tbl->it_page_shift,
1885 index, npages);
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001886 }
1887}
1888
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001889static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1890 long npages, unsigned long uaddr,
1891 enum dma_data_direction direction,
1892 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001893{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001894 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1895 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001896
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001897 if (!ret)
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001898 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1899
1900 return ret;
1901}
1902
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001903#ifdef CONFIG_IOMMU_API
1904static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1905 unsigned long *hpa, enum dma_data_direction *direction)
1906{
1907 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1908
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001909 if (!ret)
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001910 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1911
1912 return ret;
1913}
1914#endif
1915
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001916static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1917 long npages)
1918{
1919 pnv_tce_free(tbl, index, npages);
1920
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001921 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001922}
1923
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001924static void pnv_ioda2_table_free(struct iommu_table *tbl)
1925{
1926 pnv_pci_ioda2_table_free_pages(tbl);
1927 iommu_free_table(tbl, "pnv");
1928}
1929
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001930static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001931 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001932#ifdef CONFIG_IOMMU_API
1933 .exchange = pnv_ioda2_tce_xchg,
1934#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001935 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001936 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001937 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001938};
1939
Gavin Shan801846d2016-05-03 15:41:34 +10001940static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data)
1941{
1942 unsigned int *weight = (unsigned int *)data;
1943
1944 /* This is quite simplistic. The "base" weight of a device
1945 * is 10. 0 means no DMA is to be accounted for it.
1946 */
1947 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
1948 return 0;
1949
1950 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
1951 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
1952 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
1953 *weight += 3;
1954 else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
1955 *weight += 15;
1956 else
1957 *weight += 10;
1958
1959 return 0;
1960}
1961
1962static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe)
1963{
1964 unsigned int weight = 0;
1965
1966 /* SRIOV VF has same DMA32 weight as its PF */
1967#ifdef CONFIG_PCI_IOV
1968 if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) {
1969 pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight);
1970 return weight;
1971 }
1972#endif
1973
1974 if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) {
1975 pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight);
1976 } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) {
1977 struct pci_dev *pdev;
1978
1979 list_for_each_entry(pdev, &pe->pbus->devices, bus_list)
1980 pnv_pci_ioda_dev_dma_weight(pdev, &weight);
1981 } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) {
1982 pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight);
1983 }
1984
1985 return weight;
1986}
1987
Gavin Shanb30d9362016-05-03 15:41:32 +10001988static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
Gavin Shan2b923ed2016-05-05 12:04:16 +10001989 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001990{
1991
1992 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001993 struct iommu_table *tbl;
Gavin Shan2b923ed2016-05-05 12:04:16 +10001994 unsigned int weight, total_weight = 0;
1995 unsigned int tce32_segsz, base, segs, avail, i;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001996 int64_t rc;
1997 void *addr;
1998
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001999 /* XXX FIXME: Handle 64-bit only DMA devices */
2000 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
2001 /* XXX FIXME: Allocate multi-level tables on PHB3 */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002002 weight = pnv_pci_ioda_pe_dma_weight(pe);
2003 if (!weight)
2004 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002005
Gavin Shan2b923ed2016-05-05 12:04:16 +10002006 pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight,
2007 &total_weight);
2008 segs = (weight * phb->ioda.dma32_count) / total_weight;
2009 if (!segs)
2010 segs = 1;
2011
2012 /*
2013 * Allocate contiguous DMA32 segments. We begin with the expected
2014 * number of segments. With one more attempt, the number of DMA32
2015 * segments to be allocated is decreased by one until one segment
2016 * is allocated successfully.
2017 */
2018 do {
2019 for (base = 0; base <= phb->ioda.dma32_count - segs; base++) {
2020 for (avail = 0, i = base; i < base + segs; i++) {
2021 if (phb->ioda.dma32_segmap[i] ==
2022 IODA_INVALID_PE)
2023 avail++;
2024 }
2025
2026 if (avail == segs)
2027 goto found;
2028 }
2029 } while (--segs);
2030
2031 if (!segs) {
2032 pe_warn(pe, "No available DMA32 segments\n");
2033 return;
2034 }
2035
2036found:
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002037 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002038 iommu_register_group(&pe->table_group, phb->hose->global_number,
2039 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002040 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002041
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002042 /* Grab a 32-bit TCE table */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002043 pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
2044 weight, total_weight, base, segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002045 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
Gavin Shanacce9712016-05-03 15:41:33 +10002046 base * PNV_IODA1_DMA32_SEGSIZE,
2047 (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002048
2049 /* XXX Currently, we allocate one big contiguous table for the
2050 * TCEs. We only really need one chunk per 256M of TCE space
2051 * (ie per segment) but that's an optimization for later, it
2052 * requires some added smarts with our get/put_tce implementation
Gavin Shanacce9712016-05-03 15:41:33 +10002053 *
2054 * Each TCE page is 4KB in size and each TCE entry occupies 8
2055 * bytes
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002056 */
Gavin Shanacce9712016-05-03 15:41:33 +10002057 tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002058 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
Gavin Shanacce9712016-05-03 15:41:33 +10002059 get_order(tce32_segsz * segs));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002060 if (!tce_mem) {
2061 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
2062 goto fail;
2063 }
2064 addr = page_address(tce_mem);
Gavin Shanacce9712016-05-03 15:41:33 +10002065 memset(addr, 0, tce32_segsz * segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002066
2067 /* Configure HW */
2068 for (i = 0; i < segs; i++) {
2069 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2070 pe->pe_number,
2071 base + i, 1,
Gavin Shanacce9712016-05-03 15:41:33 +10002072 __pa(addr) + tce32_segsz * i,
2073 tce32_segsz, IOMMU_PAGE_SIZE_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002074 if (rc) {
2075 pe_err(pe, " Failed to configure 32-bit TCE table,"
2076 " err %ld\n", rc);
2077 goto fail;
2078 }
2079 }
2080
Gavin Shan2b923ed2016-05-05 12:04:16 +10002081 /* Setup DMA32 segment mapping */
2082 for (i = base; i < base + segs; i++)
2083 phb->ioda.dma32_segmap[i] = pe->pe_number;
2084
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002085 /* Setup linux iommu table */
Gavin Shanacce9712016-05-03 15:41:33 +10002086 pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs,
2087 base * PNV_IODA1_DMA32_SEGSIZE,
2088 IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002089
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002090 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002091 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
2092 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002093 iommu_init_table(tbl, phb->hose->node);
2094
Wei Yang781a8682015-03-25 16:23:57 +08002095 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002096 /*
2097 * Setting table base here only for carrying iommu_group
2098 * further down to let iommu_add_device() do the job.
2099 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2100 */
2101 set_iommu_table_base(&pe->pdev->dev, tbl);
2102 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002103 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002104 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10002105
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002106 return;
2107 fail:
2108 /* XXX Failure: Try to fallback to 64-bit only ? */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002109 if (tce_mem)
Gavin Shanacce9712016-05-03 15:41:33 +10002110 __free_pages(tce_mem, get_order(tce32_segsz * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002111 if (tbl) {
2112 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
2113 iommu_free_table(tbl, "pnv");
2114 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002115}
2116
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002117static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
2118 int num, struct iommu_table *tbl)
2119{
2120 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2121 table_group);
2122 struct pnv_phb *phb = pe->phb;
2123 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002124 const unsigned long size = tbl->it_indirect_levels ?
2125 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002126 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
2127 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
2128
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002129 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002130 start_addr, start_addr + win_size - 1,
2131 IOMMU_PAGE_SIZE(tbl));
2132
2133 /*
2134 * Map TCE table through TVT. The TVE index is the PE number
2135 * shifted by 1 bit for 32-bits DMA space.
2136 */
2137 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2138 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002139 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002140 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002141 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002142 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002143 IOMMU_PAGE_SIZE(tbl));
2144 if (rc) {
2145 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
2146 return rc;
2147 }
2148
2149 pnv_pci_link_table_and_group(phb->hose->node, num,
2150 tbl, &pe->table_group);
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002151 pnv_pci_phb3_tce_invalidate_pe(pe);
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002152
2153 return 0;
2154}
2155
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002156static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002157{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002158 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2159 int64_t rc;
2160
2161 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2162 if (enable) {
2163 phys_addr_t top = memblock_end_of_DRAM();
2164
2165 top = roundup_pow_of_two(top);
2166 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2167 pe->pe_number,
2168 window_id,
2169 pe->tce_bypass_base,
2170 top);
2171 } else {
2172 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2173 pe->pe_number,
2174 window_id,
2175 pe->tce_bypass_base,
2176 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002177 }
2178 if (rc)
2179 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2180 else
2181 pe->tce_bypass_enabled = enable;
2182}
2183
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002184static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2185 __u32 page_shift, __u64 window_size, __u32 levels,
2186 struct iommu_table *tbl);
2187
2188static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2189 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2190 struct iommu_table **ptbl)
2191{
2192 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2193 table_group);
2194 int nid = pe->phb->hose->node;
2195 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2196 long ret;
2197 struct iommu_table *tbl;
2198
2199 tbl = pnv_pci_table_alloc(nid);
2200 if (!tbl)
2201 return -ENOMEM;
2202
2203 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2204 bus_offset, page_shift, window_size,
2205 levels, tbl);
2206 if (ret) {
2207 iommu_free_table(tbl, "pnv");
2208 return ret;
2209 }
2210
2211 tbl->it_ops = &pnv_ioda2_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002212
2213 *ptbl = tbl;
2214
2215 return 0;
2216}
2217
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002218static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2219{
2220 struct iommu_table *tbl = NULL;
2221 long rc;
2222
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002223 /*
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002224 * crashkernel= specifies the kdump kernel's maximum memory at
2225 * some offset and there is no guaranteed the result is a power
2226 * of 2, which will cause errors later.
2227 */
2228 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
2229
2230 /*
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002231 * In memory constrained environments, e.g. kdump kernel, the
2232 * DMA window can be larger than available memory, which will
2233 * cause errors later.
2234 */
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002235 const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002236
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002237 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2238 IOMMU_PAGE_SHIFT_4K,
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002239 window_size,
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002240 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2241 if (rc) {
2242 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2243 rc);
2244 return rc;
2245 }
2246
2247 iommu_init_table(tbl, pe->phb->hose->node);
2248
2249 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2250 if (rc) {
2251 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2252 rc);
2253 pnv_ioda2_table_free(tbl);
2254 return rc;
2255 }
2256
2257 if (!pnv_iommu_bypass_disabled)
2258 pnv_pci_ioda2_set_bypass(pe, true);
2259
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002260 /*
2261 * Setting table base here only for carrying iommu_group
2262 * further down to let iommu_add_device() do the job.
2263 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2264 */
2265 if (pe->flags & PNV_IODA_PE_DEV)
2266 set_iommu_table_base(&pe->pdev->dev, tbl);
2267
2268 return 0;
2269}
2270
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002271#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2272static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2273 int num)
2274{
2275 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2276 table_group);
2277 struct pnv_phb *phb = pe->phb;
2278 long ret;
2279
2280 pe_info(pe, "Removing DMA window #%d\n", num);
2281
2282 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2283 (pe->pe_number << 1) + num,
2284 0/* levels */, 0/* table address */,
2285 0/* table size */, 0/* page size */);
2286 if (ret)
2287 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2288 else
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002289 pnv_pci_phb3_tce_invalidate_pe(pe);
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002290
2291 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2292
2293 return ret;
2294}
2295#endif
2296
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002297#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002298static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2299 __u64 window_size, __u32 levels)
2300{
2301 unsigned long bytes = 0;
2302 const unsigned window_shift = ilog2(window_size);
2303 unsigned entries_shift = window_shift - page_shift;
2304 unsigned table_shift = entries_shift + 3;
2305 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2306 unsigned long direct_table_size;
2307
2308 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2309 (window_size > memory_hotplug_max()) ||
2310 !is_power_of_2(window_size))
2311 return 0;
2312
2313 /* Calculate a direct table size from window_size and levels */
2314 entries_shift = (entries_shift + levels - 1) / levels;
2315 table_shift = entries_shift + 3;
2316 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2317 direct_table_size = 1UL << table_shift;
2318
2319 for ( ; levels; --levels) {
2320 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2321
2322 tce_table_size /= direct_table_size;
2323 tce_table_size <<= 3;
2324 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2325 }
2326
2327 return bytes;
2328}
2329
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002330static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002331{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002332 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2333 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002334 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2335 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002336
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002337 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002338 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2339 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002340}
2341
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002342static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2343{
2344 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2345 table_group);
2346
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002347 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002348}
2349
2350static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002351 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002352 .create_table = pnv_pci_ioda2_create_table,
2353 .set_window = pnv_pci_ioda2_set_window,
2354 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002355 .take_ownership = pnv_ioda2_take_ownership,
2356 .release_ownership = pnv_ioda2_release_ownership,
2357};
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002358
2359static int gpe_table_group_to_npe_cb(struct device *dev, void *opaque)
2360{
2361 struct pci_controller *hose;
2362 struct pnv_phb *phb;
2363 struct pnv_ioda_pe **ptmppe = opaque;
2364 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
2365 struct pci_dn *pdn = pci_get_pdn(pdev);
2366
2367 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2368 return 0;
2369
2370 hose = pci_bus_to_host(pdev->bus);
2371 phb = hose->private_data;
2372 if (phb->type != PNV_PHB_NPU)
2373 return 0;
2374
2375 *ptmppe = &phb->ioda.pe_array[pdn->pe_number];
2376
2377 return 1;
2378}
2379
2380/*
2381 * This returns PE of associated NPU.
2382 * This assumes that NPU is in the same IOMMU group with GPU and there is
2383 * no other PEs.
2384 */
2385static struct pnv_ioda_pe *gpe_table_group_to_npe(
2386 struct iommu_table_group *table_group)
2387{
2388 struct pnv_ioda_pe *npe = NULL;
2389 int ret = iommu_group_for_each_dev(table_group->group, &npe,
2390 gpe_table_group_to_npe_cb);
2391
2392 BUG_ON(!ret || !npe);
2393
2394 return npe;
2395}
2396
2397static long pnv_pci_ioda2_npu_set_window(struct iommu_table_group *table_group,
2398 int num, struct iommu_table *tbl)
2399{
2400 long ret = pnv_pci_ioda2_set_window(table_group, num, tbl);
2401
2402 if (ret)
2403 return ret;
2404
2405 ret = pnv_npu_set_window(gpe_table_group_to_npe(table_group), num, tbl);
2406 if (ret)
2407 pnv_pci_ioda2_unset_window(table_group, num);
2408
2409 return ret;
2410}
2411
2412static long pnv_pci_ioda2_npu_unset_window(
2413 struct iommu_table_group *table_group,
2414 int num)
2415{
2416 long ret = pnv_pci_ioda2_unset_window(table_group, num);
2417
2418 if (ret)
2419 return ret;
2420
2421 return pnv_npu_unset_window(gpe_table_group_to_npe(table_group), num);
2422}
2423
2424static void pnv_ioda2_npu_take_ownership(struct iommu_table_group *table_group)
2425{
2426 /*
2427 * Detach NPU first as pnv_ioda2_take_ownership() will destroy
2428 * the iommu_table if 32bit DMA is enabled.
2429 */
2430 pnv_npu_take_ownership(gpe_table_group_to_npe(table_group));
2431 pnv_ioda2_take_ownership(table_group);
2432}
2433
2434static struct iommu_table_group_ops pnv_pci_ioda2_npu_ops = {
2435 .get_table_size = pnv_pci_ioda2_get_table_size,
2436 .create_table = pnv_pci_ioda2_create_table,
2437 .set_window = pnv_pci_ioda2_npu_set_window,
2438 .unset_window = pnv_pci_ioda2_npu_unset_window,
2439 .take_ownership = pnv_ioda2_npu_take_ownership,
2440 .release_ownership = pnv_ioda2_release_ownership,
2441};
2442
2443static void pnv_pci_ioda_setup_iommu_api(void)
2444{
2445 struct pci_controller *hose, *tmp;
2446 struct pnv_phb *phb;
2447 struct pnv_ioda_pe *pe, *gpe;
2448
2449 /*
2450 * Now we have all PHBs discovered, time to add NPU devices to
2451 * the corresponding IOMMU groups.
2452 */
2453 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2454 phb = hose->private_data;
2455
2456 if (phb->type != PNV_PHB_NPU)
2457 continue;
2458
2459 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2460 gpe = pnv_pci_npu_setup_iommu(pe);
2461 if (gpe)
2462 gpe->table_group.ops = &pnv_pci_ioda2_npu_ops;
2463 }
2464 }
2465}
2466#else /* !CONFIG_IOMMU_API */
2467static void pnv_pci_ioda_setup_iommu_api(void) { };
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002468#endif
2469
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002470static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2471{
2472 const __be64 *swinvp;
2473
2474 /* OPAL variant of PHB3 invalidated TCEs */
2475 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2476 if (!swinvp)
2477 return;
2478
2479 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2480 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2481}
2482
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002483static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2484 unsigned levels, unsigned long limit,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002485 unsigned long *current_offset, unsigned long *total_allocated)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002486{
2487 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002488 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002489 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002490 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2491 unsigned entries = 1UL << (shift - 3);
2492 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002493
2494 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2495 if (!tce_mem) {
2496 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2497 return NULL;
2498 }
2499 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002500 memset(addr, 0, allocated);
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002501 *total_allocated += allocated;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002502
2503 --levels;
2504 if (!levels) {
2505 *current_offset += allocated;
2506 return addr;
2507 }
2508
2509 for (i = 0; i < entries; ++i) {
2510 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002511 levels, limit, current_offset, total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002512 if (!tmp)
2513 break;
2514
2515 addr[i] = cpu_to_be64(__pa(tmp) |
2516 TCE_PCI_READ | TCE_PCI_WRITE);
2517
2518 if (*current_offset >= limit)
2519 break;
2520 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002521
2522 return addr;
2523}
2524
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002525static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2526 unsigned long size, unsigned level);
2527
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002528static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002529 __u32 page_shift, __u64 window_size, __u32 levels,
2530 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002531{
2532 void *addr;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002533 unsigned long offset = 0, level_shift, total_allocated = 0;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002534 const unsigned window_shift = ilog2(window_size);
2535 unsigned entries_shift = window_shift - page_shift;
2536 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2537 const unsigned long tce_table_size = 1UL << table_shift;
2538
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002539 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2540 return -EINVAL;
2541
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002542 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2543 return -EINVAL;
2544
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002545 /* Adjust direct table size from window_size and levels */
2546 entries_shift = (entries_shift + levels - 1) / levels;
2547 level_shift = entries_shift + 3;
2548 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2549
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002550 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002551 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002552 levels, tce_table_size, &offset, &total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002553
2554 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002555 if (!addr)
2556 return -ENOMEM;
2557
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002558 /*
2559 * First level was allocated but some lower level failed as
2560 * we did not allocate as much as we wanted,
2561 * release partially allocated table.
2562 */
2563 if (offset < tce_table_size) {
2564 pnv_pci_ioda2_table_do_free_pages(addr,
2565 1ULL << (level_shift - 3), levels - 1);
2566 return -ENOMEM;
2567 }
2568
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002569 /* Setup linux iommu table */
2570 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2571 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002572 tbl->it_level_size = 1ULL << (level_shift - 3);
2573 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002574 tbl->it_allocated_size = total_allocated;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002575
2576 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2577 window_size, tce_table_size, bus_offset);
2578
2579 return 0;
2580}
2581
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002582static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2583 unsigned long size, unsigned level)
2584{
2585 const unsigned long addr_ul = (unsigned long) addr &
2586 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2587
2588 if (level) {
2589 long i;
2590 u64 *tmp = (u64 *) addr_ul;
2591
2592 for (i = 0; i < size; ++i) {
2593 unsigned long hpa = be64_to_cpu(tmp[i]);
2594
2595 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2596 continue;
2597
2598 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2599 level - 1);
2600 }
2601 }
2602
2603 free_pages(addr_ul, get_order(size << 3));
2604}
2605
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002606static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2607{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002608 const unsigned long size = tbl->it_indirect_levels ?
2609 tbl->it_level_size : tbl->it_size;
2610
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002611 if (!tbl->it_size)
2612 return;
2613
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002614 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2615 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002616}
2617
Gavin Shan373f5652013-04-25 19:21:01 +00002618static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2619 struct pnv_ioda_pe *pe)
2620{
Gavin Shan373f5652013-04-25 19:21:01 +00002621 int64_t rc;
2622
Gavin Shanccd1c192016-05-20 16:41:31 +10002623 if (!pnv_pci_ioda_pe_dma_weight(pe))
2624 return;
2625
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002626 /* TVE #1 is selected by PCI address bit 59 */
2627 pe->tce_bypass_base = 1ull << 59;
2628
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002629 iommu_register_group(&pe->table_group, phb->hose->global_number,
2630 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002631
Gavin Shan373f5652013-04-25 19:21:01 +00002632 /* The PE will reserve all possible 32-bits space */
Gavin Shan373f5652013-04-25 19:21:01 +00002633 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002634 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002635
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002636 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002637 pe->table_group.tce32_start = 0;
2638 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2639 pe->table_group.max_dynamic_windows_supported =
2640 IOMMU_TABLE_GROUP_MAX_TABLES;
2641 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2642 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002643#ifdef CONFIG_IOMMU_API
2644 pe->table_group.ops = &pnv_pci_ioda2_ops;
2645#endif
2646
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002647 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan801846d2016-05-03 15:41:34 +10002648 if (rc)
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002649 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002650
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002651 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002652 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002653 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002654 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002655}
2656
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002657#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002658static void pnv_ioda2_msi_eoi(struct irq_data *d)
2659{
2660 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2661 struct irq_chip *chip = irq_data_get_irq_chip(d);
2662 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2663 ioda.irq_chip);
2664 int64_t rc;
2665
2666 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2667 WARN_ON_ONCE(rc);
2668
2669 icp_native_eoi(d);
2670}
2671
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002672
Ian Munsief4568342016-07-14 07:17:00 +10002673void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002674{
2675 struct irq_data *idata;
2676 struct irq_chip *ichip;
2677
Benjamin Herrenschmidtfb111332016-07-08 16:37:09 +10002678 /* The MSI EOI OPAL call is only needed on PHB3 */
2679 if (phb->model != PNV_PHB_MODEL_PHB3)
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002680 return;
2681
2682 if (!phb->ioda.irq_chip_init) {
2683 /*
2684 * First time we setup an MSI IRQ, we need to setup the
2685 * corresponding IRQ chip to route correctly.
2686 */
2687 idata = irq_get_irq_data(virq);
2688 ichip = irq_data_get_irq_chip(idata);
2689 phb->ioda.irq_chip_init = 1;
2690 phb->ioda.irq_chip = *ichip;
2691 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2692 }
2693 irq_set_chip(virq, &phb->ioda.irq_chip);
2694}
2695
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002696static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002697 unsigned int hwirq, unsigned int virq,
2698 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002699{
2700 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2701 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002702 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002703 int rc;
2704
2705 /* No PE assigned ? bail out ... no MSI for you ! */
2706 if (pe == NULL)
2707 return -ENXIO;
2708
2709 /* Check if we have an MVE */
2710 if (pe->mve_number < 0)
2711 return -ENXIO;
2712
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002713 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002714 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002715 is_64 = 0;
2716
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002717 /* Assign XIVE to PE */
2718 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2719 if (rc) {
2720 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2721 pci_name(dev), rc, xive_num);
2722 return -EIO;
2723 }
2724
2725 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002726 __be64 addr64;
2727
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002728 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2729 &addr64, &data);
2730 if (rc) {
2731 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2732 pci_name(dev), rc);
2733 return -EIO;
2734 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002735 msg->address_hi = be64_to_cpu(addr64) >> 32;
2736 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002737 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002738 __be32 addr32;
2739
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002740 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2741 &addr32, &data);
2742 if (rc) {
2743 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2744 pci_name(dev), rc);
2745 return -EIO;
2746 }
2747 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002748 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002749 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002750 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002751
Ian Munsief4568342016-07-14 07:17:00 +10002752 pnv_set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002753
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002754 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2755 " address=%x_%08x data=%x PE# %d\n",
2756 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2757 msg->address_hi, msg->address_lo, data, pe->pe_number);
2758
2759 return 0;
2760}
2761
2762static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2763{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002764 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002765 const __be32 *prop = of_get_property(phb->hose->dn,
2766 "ibm,opal-msi-ranges", NULL);
2767 if (!prop) {
2768 /* BML Fallback */
2769 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2770 }
2771 if (!prop)
2772 return;
2773
2774 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002775 count = be32_to_cpup(prop + 1);
2776 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002777 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2778 phb->hose->global_number);
2779 return;
2780 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002781
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002782 phb->msi_setup = pnv_pci_ioda_msi_setup;
2783 phb->msi32_support = 1;
2784 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002785 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002786}
2787#else
2788static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2789#endif /* CONFIG_PCI_MSI */
2790
Wei Yang6e628c72015-03-25 16:23:55 +08002791#ifdef CONFIG_PCI_IOV
2792static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2793{
Wei Yangf2dd0af2015-10-22 09:22:17 +08002794 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
2795 struct pnv_phb *phb = hose->private_data;
2796 const resource_size_t gate = phb->ioda.m64_segsize >> 2;
Wei Yang6e628c72015-03-25 16:23:55 +08002797 struct resource *res;
2798 int i;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002799 resource_size_t size, total_vf_bar_sz;
Wei Yang6e628c72015-03-25 16:23:55 +08002800 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002801 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002802
2803 if (!pdev->is_physfn || pdev->is_added)
2804 return;
2805
Wei Yang6e628c72015-03-25 16:23:55 +08002806 pdn = pci_get_pdn(pdev);
2807 pdn->vfs_expanded = 0;
Wei Yangee8222f2015-10-22 09:22:16 +08002808 pdn->m64_single_mode = false;
Wei Yang6e628c72015-03-25 16:23:55 +08002809
Wei Yang5b88ec22015-03-25 16:23:58 +08002810 total_vfs = pci_sriov_get_totalvfs(pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10002811 mul = phb->ioda.total_pe_num;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002812 total_vf_bar_sz = 0;
Wei Yang5b88ec22015-03-25 16:23:58 +08002813
2814 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2815 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2816 if (!res->flags || res->parent)
2817 continue;
2818 if (!pnv_pci_is_mem_pref_64(res->flags)) {
Wei Yangb0331852015-10-22 09:22:14 +08002819 dev_warn(&pdev->dev, "Don't support SR-IOV with"
2820 " non M64 VF BAR%d: %pR. \n",
Wei Yang5b88ec22015-03-25 16:23:58 +08002821 i, res);
Wei Yangb0331852015-10-22 09:22:14 +08002822 goto truncate_iov;
Wei Yang5b88ec22015-03-25 16:23:58 +08002823 }
2824
Wei Yangdfcc8d42015-10-22 09:22:18 +08002825 total_vf_bar_sz += pci_iov_resource_size(pdev,
2826 i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002827
Wei Yangf2dd0af2015-10-22 09:22:17 +08002828 /*
2829 * If bigger than quarter of M64 segment size, just round up
2830 * power of two.
2831 *
2832 * Generally, one M64 BAR maps one IOV BAR. To avoid conflict
2833 * with other devices, IOV BAR size is expanded to be
2834 * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64
2835 * segment size , the expanded size would equal to half of the
2836 * whole M64 space size, which will exhaust the M64 Space and
2837 * limit the system flexibility. This is a design decision to
2838 * set the boundary to quarter of the M64 segment size.
2839 */
Wei Yangdfcc8d42015-10-22 09:22:18 +08002840 if (total_vf_bar_sz > gate) {
Wei Yang5b88ec22015-03-25 16:23:58 +08002841 mul = roundup_pow_of_two(total_vfs);
Wei Yangdfcc8d42015-10-22 09:22:18 +08002842 dev_info(&pdev->dev,
2843 "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n",
2844 total_vf_bar_sz, gate, mul);
Wei Yangee8222f2015-10-22 09:22:16 +08002845 pdn->m64_single_mode = true;
Wei Yang5b88ec22015-03-25 16:23:58 +08002846 break;
2847 }
2848 }
2849
Wei Yang6e628c72015-03-25 16:23:55 +08002850 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2851 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2852 if (!res->flags || res->parent)
2853 continue;
Wei Yang6e628c72015-03-25 16:23:55 +08002854
Wei Yang6e628c72015-03-25 16:23:55 +08002855 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yangee8222f2015-10-22 09:22:16 +08002856 /*
2857 * On PHB3, the minimum size alignment of M64 BAR in single
2858 * mode is 32MB.
2859 */
2860 if (pdn->m64_single_mode && (size < SZ_32M))
2861 goto truncate_iov;
2862 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
Wei Yang5b88ec22015-03-25 16:23:58 +08002863 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08002864 dev_dbg(&pdev->dev, " %pR\n", res);
2865 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08002866 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08002867 }
Wei Yang5b88ec22015-03-25 16:23:58 +08002868 pdn->vfs_expanded = mul;
Wei Yangb0331852015-10-22 09:22:14 +08002869
2870 return;
2871
2872truncate_iov:
2873 /* To save MMIO space, IOV BAR is truncated. */
2874 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2875 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2876 res->flags = 0;
2877 res->end = res->start - 1;
2878 }
Wei Yang6e628c72015-03-25 16:23:55 +08002879}
2880#endif /* CONFIG_PCI_IOV */
2881
Gavin Shan23e79422016-05-03 15:41:27 +10002882static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
2883 struct resource *res)
2884{
2885 struct pnv_phb *phb = pe->phb;
2886 struct pci_bus_region region;
2887 int index;
2888 int64_t rc;
2889
2890 if (!res || !res->flags || res->start > res->end)
2891 return;
2892
2893 if (res->flags & IORESOURCE_IO) {
2894 region.start = res->start - phb->ioda.io_pci_base;
2895 region.end = res->end - phb->ioda.io_pci_base;
2896 index = region.start / phb->ioda.io_segsize;
2897
2898 while (index < phb->ioda.total_pe_num &&
2899 region.start <= region.end) {
2900 phb->ioda.io_segmap[index] = pe->pe_number;
2901 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2902 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2903 if (rc != OPAL_SUCCESS) {
2904 pr_err("%s: Error %lld mapping IO segment#%d to PE#%d\n",
2905 __func__, rc, index, pe->pe_number);
2906 break;
2907 }
2908
2909 region.start += phb->ioda.io_segsize;
2910 index++;
2911 }
2912 } else if ((res->flags & IORESOURCE_MEM) &&
2913 !pnv_pci_is_mem_pref_64(res->flags)) {
2914 region.start = res->start -
2915 phb->hose->mem_offset[0] -
2916 phb->ioda.m32_pci_base;
2917 region.end = res->end -
2918 phb->hose->mem_offset[0] -
2919 phb->ioda.m32_pci_base;
2920 index = region.start / phb->ioda.m32_segsize;
2921
2922 while (index < phb->ioda.total_pe_num &&
2923 region.start <= region.end) {
2924 phb->ioda.m32_segmap[index] = pe->pe_number;
2925 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2926 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2927 if (rc != OPAL_SUCCESS) {
2928 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%d",
2929 __func__, rc, index, pe->pe_number);
2930 break;
2931 }
2932
2933 region.start += phb->ioda.m32_segsize;
2934 index++;
2935 }
2936 }
2937}
2938
Gavin Shan11685be2012-08-20 03:49:16 +00002939/*
2940 * This function is supposed to be called on basis of PE from top
2941 * to bottom style. So the the I/O or MMIO segment assigned to
2942 * parent PE could be overrided by its child PEs if necessary.
2943 */
Gavin Shan23e79422016-05-03 15:41:27 +10002944static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00002945{
Gavin Shan69d733e2016-05-03 15:41:28 +10002946 struct pci_dev *pdev;
Gavin Shan23e79422016-05-03 15:41:27 +10002947 int i;
Gavin Shan11685be2012-08-20 03:49:16 +00002948
2949 /*
2950 * NOTE: We only care PCI bus based PE for now. For PCI
2951 * device based PE, for example SRIOV sensitive VF should
2952 * be figured out later.
2953 */
2954 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2955
Gavin Shan69d733e2016-05-03 15:41:28 +10002956 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
2957 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
2958 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
2959
2960 /*
2961 * If the PE contains all subordinate PCI buses, the
2962 * windows of the child bridges should be mapped to
2963 * the PE as well.
2964 */
2965 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
2966 continue;
2967 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
2968 pnv_ioda_setup_pe_res(pe,
2969 &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
2970 }
Gavin Shan11685be2012-08-20 03:49:16 +00002971}
2972
Gavin Shan37c367f2013-06-20 18:13:25 +08002973static void pnv_pci_ioda_create_dbgfs(void)
2974{
2975#ifdef CONFIG_DEBUG_FS
2976 struct pci_controller *hose, *tmp;
2977 struct pnv_phb *phb;
2978 char name[16];
2979
2980 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2981 phb = hose->private_data;
2982
Gavin Shanccd1c192016-05-20 16:41:31 +10002983 /* Notify initialization of PHB done */
2984 phb->initialized = 1;
2985
Gavin Shan37c367f2013-06-20 18:13:25 +08002986 sprintf(name, "PCI%04x", hose->global_number);
2987 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
2988 if (!phb->dbgfs)
2989 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
2990 __func__, hose->global_number);
2991 }
2992#endif /* CONFIG_DEBUG_FS */
2993}
2994
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002995static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00002996{
2997 pnv_pci_ioda_setup_PEs();
Gavin Shanccd1c192016-05-20 16:41:31 +10002998 pnv_pci_ioda_setup_iommu_api();
Gavin Shan37c367f2013-06-20 18:13:25 +08002999 pnv_pci_ioda_create_dbgfs();
3000
Gavin Shane9cc17d2013-06-20 13:21:14 +08003001#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08003002 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04003003 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003004#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00003005}
3006
Gavin Shan271fd032012-09-11 16:59:47 -06003007/*
3008 * Returns the alignment for I/O or memory windows for P2P
3009 * bridges. That actually depends on how PEs are segmented.
3010 * For now, we return I/O or M32 segment size for PE sensitive
3011 * P2P bridges. Otherwise, the default values (4KiB for I/O,
3012 * 1MiB for memory) will be returned.
3013 *
3014 * The current PCI bus might be put into one PE, which was
3015 * create against the parent PCI bridge. For that case, we
3016 * needn't enlarge the alignment so that we can save some
3017 * resources.
3018 */
3019static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
3020 unsigned long type)
3021{
3022 struct pci_dev *bridge;
3023 struct pci_controller *hose = pci_bus_to_host(bus);
3024 struct pnv_phb *phb = hose->private_data;
3025 int num_pci_bridges = 0;
3026
3027 bridge = bus->self;
3028 while (bridge) {
3029 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
3030 num_pci_bridges++;
3031 if (num_pci_bridges >= 2)
3032 return 1;
3033 }
3034
3035 bridge = bridge->bus->self;
3036 }
3037
Guo Chao262af552014-07-21 14:42:30 +10003038 /* We fail back to M32 if M64 isn't supported */
3039 if (phb->ioda.m64_segsize &&
3040 pnv_pci_is_mem_pref_64(type))
3041 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06003042 if (type & IORESOURCE_MEM)
3043 return phb->ioda.m32_segsize;
3044
3045 return phb->ioda.io_segsize;
3046}
3047
Gavin Shan40e2a472016-05-20 16:41:33 +10003048/*
3049 * We are updating root port or the upstream port of the
3050 * bridge behind the root port with PHB's windows in order
3051 * to accommodate the changes on required resources during
3052 * PCI (slot) hotplug, which is connected to either root
3053 * port or the downstream ports of PCIe switch behind the
3054 * root port.
3055 */
3056static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
3057 unsigned long type)
3058{
3059 struct pci_controller *hose = pci_bus_to_host(bus);
3060 struct pnv_phb *phb = hose->private_data;
3061 struct pci_dev *bridge = bus->self;
3062 struct resource *r, *w;
3063 bool msi_region = false;
3064 int i;
3065
3066 /* Check if we need apply fixup to the bridge's windows */
3067 if (!pci_is_root_bus(bridge->bus) &&
3068 !pci_is_root_bus(bridge->bus->self->bus))
3069 return;
3070
3071 /* Fixup the resources */
3072 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
3073 r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
3074 if (!r->flags || !r->parent)
3075 continue;
3076
3077 w = NULL;
3078 if (r->flags & type & IORESOURCE_IO)
3079 w = &hose->io_resource;
3080 else if (pnv_pci_is_mem_pref_64(r->flags) &&
3081 (type & IORESOURCE_PREFETCH) &&
3082 phb->ioda.m64_segsize)
3083 w = &hose->mem_resources[1];
3084 else if (r->flags & type & IORESOURCE_MEM) {
3085 w = &hose->mem_resources[0];
3086 msi_region = true;
3087 }
3088
3089 r->start = w->start;
3090 r->end = w->end;
3091
3092 /* The 64KB 32-bits MSI region shouldn't be included in
3093 * the 32-bits bridge window. Otherwise, we can see strange
3094 * issues. One of them is EEH error observed on Garrison.
3095 *
3096 * Exclude top 1MB region which is the minimal alignment of
3097 * 32-bits bridge window.
3098 */
3099 if (msi_region) {
3100 r->end += 0x10000;
3101 r->end -= 0x100000;
3102 }
3103 }
3104}
3105
Gavin Shanccd1c192016-05-20 16:41:31 +10003106static void pnv_pci_setup_bridge(struct pci_bus *bus, unsigned long type)
3107{
3108 struct pci_controller *hose = pci_bus_to_host(bus);
3109 struct pnv_phb *phb = hose->private_data;
3110 struct pci_dev *bridge = bus->self;
3111 struct pnv_ioda_pe *pe;
3112 bool all = (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
3113
Gavin Shan40e2a472016-05-20 16:41:33 +10003114 /* Extend bridge's windows if necessary */
3115 pnv_pci_fixup_bridge_resources(bus, type);
3116
Gavin Shan63803c32016-05-20 16:41:32 +10003117 /* The PE for root bus should be realized before any one else */
3118 if (!phb->ioda.root_pe_populated) {
3119 pe = pnv_ioda_setup_bus_PE(phb->hose->bus, false);
3120 if (pe) {
3121 phb->ioda.root_pe_idx = pe->pe_number;
3122 phb->ioda.root_pe_populated = true;
3123 }
3124 }
3125
Gavin Shanccd1c192016-05-20 16:41:31 +10003126 /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
3127 if (list_empty(&bus->devices))
3128 return;
3129
3130 /* Reserve PEs according to used M64 resources */
3131 if (phb->reserve_m64_pe)
3132 phb->reserve_m64_pe(bus, NULL, all);
3133
3134 /*
3135 * Assign PE. We might run here because of partial hotplug.
3136 * For the case, we just pick up the existing PE and should
3137 * not allocate resources again.
3138 */
3139 pe = pnv_ioda_setup_bus_PE(bus, all);
3140 if (!pe)
3141 return;
3142
3143 pnv_ioda_setup_pe_seg(pe);
3144 switch (phb->type) {
3145 case PNV_PHB_IODA1:
3146 pnv_pci_ioda1_setup_dma_pe(phb, pe);
3147 break;
3148 case PNV_PHB_IODA2:
3149 pnv_pci_ioda2_setup_dma_pe(phb, pe);
3150 break;
3151 default:
3152 pr_warn("%s: No DMA for PHB#%d (type %d)\n",
3153 __func__, phb->hose->global_number, phb->type);
3154 }
3155}
3156
Wei Yang5350ab32015-03-25 16:23:56 +08003157#ifdef CONFIG_PCI_IOV
3158static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
3159 int resno)
3160{
Wei Yangee8222f2015-10-22 09:22:16 +08003161 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3162 struct pnv_phb *phb = hose->private_data;
Wei Yang5350ab32015-03-25 16:23:56 +08003163 struct pci_dn *pdn = pci_get_pdn(pdev);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003164 resource_size_t align;
Wei Yang5350ab32015-03-25 16:23:56 +08003165
Wei Yang7fbe7a92015-10-22 09:22:15 +08003166 /*
3167 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
3168 * SR-IOV. While from hardware perspective, the range mapped by M64
3169 * BAR should be size aligned.
3170 *
Wei Yangee8222f2015-10-22 09:22:16 +08003171 * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra
3172 * powernv-specific hardware restriction is gone. But if just use the
3173 * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with
3174 * in one segment of M64 #15, which introduces the PE conflict between
3175 * PF and VF. Based on this, the minimum alignment of an IOV BAR is
3176 * m64_segsize.
3177 *
Wei Yang7fbe7a92015-10-22 09:22:15 +08003178 * This function returns the total IOV BAR size if M64 BAR is in
3179 * Shared PE mode or just VF BAR size if not.
Wei Yangee8222f2015-10-22 09:22:16 +08003180 * If the M64 BAR is in Single PE mode, return the VF BAR size or
3181 * M64 segment size if IOV BAR size is less.
Wei Yang7fbe7a92015-10-22 09:22:15 +08003182 */
Wei Yang5350ab32015-03-25 16:23:56 +08003183 align = pci_iov_resource_size(pdev, resno);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003184 if (!pdn->vfs_expanded)
3185 return align;
Wei Yangee8222f2015-10-22 09:22:16 +08003186 if (pdn->m64_single_mode)
3187 return max(align, (resource_size_t)phb->ioda.m64_segsize);
Wei Yang5350ab32015-03-25 16:23:56 +08003188
Wei Yang7fbe7a92015-10-22 09:22:15 +08003189 return pdn->vfs_expanded * align;
Wei Yang5350ab32015-03-25 16:23:56 +08003190}
3191#endif /* CONFIG_PCI_IOV */
3192
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003193/* Prevent enabling devices for which we couldn't properly
3194 * assign a PE
3195 */
Ian Munsie4361b032016-07-14 07:17:06 +10003196bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003197{
Gavin Shandb1266c2012-08-20 03:49:18 +00003198 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3199 struct pnv_phb *phb = hose->private_data;
3200 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003201
Gavin Shandb1266c2012-08-20 03:49:18 +00003202 /* The function is probably called while the PEs have
3203 * not be created yet. For example, resource reassignment
3204 * during PCI probe period. We just skip the check if
3205 * PEs isn't ready.
3206 */
3207 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003208 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003209
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003210 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003211 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003212 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003213
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003214 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003215}
3216
Gavin Shanc5f77002016-05-20 16:41:35 +10003217static long pnv_pci_ioda1_unset_window(struct iommu_table_group *table_group,
3218 int num)
3219{
3220 struct pnv_ioda_pe *pe = container_of(table_group,
3221 struct pnv_ioda_pe, table_group);
3222 struct pnv_phb *phb = pe->phb;
3223 unsigned int idx;
3224 long rc;
3225
3226 pe_info(pe, "Removing DMA window #%d\n", num);
3227 for (idx = 0; idx < phb->ioda.dma32_count; idx++) {
3228 if (phb->ioda.dma32_segmap[idx] != pe->pe_number)
3229 continue;
3230
3231 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
3232 idx, 0, 0ul, 0ul, 0ul);
3233 if (rc != OPAL_SUCCESS) {
3234 pe_warn(pe, "Failure %ld unmapping DMA32 segment#%d\n",
3235 rc, idx);
3236 return rc;
3237 }
3238
3239 phb->ioda.dma32_segmap[idx] = IODA_INVALID_PE;
3240 }
3241
3242 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
3243 return OPAL_SUCCESS;
3244}
3245
3246static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe)
3247{
3248 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
3249 struct iommu_table *tbl = pe->table_group.tables[0];
3250 int64_t rc;
3251
3252 if (!weight)
3253 return;
3254
3255 rc = pnv_pci_ioda1_unset_window(&pe->table_group, 0);
3256 if (rc != OPAL_SUCCESS)
3257 return;
3258
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10003259 pnv_pci_p7ioc_tce_invalidate(tbl, tbl->it_offset, tbl->it_size, false);
Gavin Shanc5f77002016-05-20 16:41:35 +10003260 if (pe->table_group.group) {
3261 iommu_group_put(pe->table_group.group);
3262 WARN_ON(pe->table_group.group);
3263 }
3264
3265 free_pages(tbl->it_base, get_order(tbl->it_size << 3));
3266 iommu_free_table(tbl, "pnv");
3267}
3268
3269static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
3270{
3271 struct iommu_table *tbl = pe->table_group.tables[0];
3272 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
3273#ifdef CONFIG_IOMMU_API
3274 int64_t rc;
3275#endif
3276
3277 if (!weight)
3278 return;
3279
3280#ifdef CONFIG_IOMMU_API
3281 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
3282 if (rc)
3283 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
3284#endif
3285
3286 pnv_pci_ioda2_set_bypass(pe, false);
3287 if (pe->table_group.group) {
3288 iommu_group_put(pe->table_group.group);
3289 WARN_ON(pe->table_group.group);
3290 }
3291
3292 pnv_pci_ioda2_table_free_pages(tbl);
3293 iommu_free_table(tbl, "pnv");
3294}
3295
3296static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
3297 unsigned short win,
3298 unsigned int *map)
3299{
3300 struct pnv_phb *phb = pe->phb;
3301 int idx;
3302 int64_t rc;
3303
3304 for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
3305 if (map[idx] != pe->pe_number)
3306 continue;
3307
3308 if (win == OPAL_M64_WINDOW_TYPE)
3309 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3310 phb->ioda.reserved_pe_idx, win,
3311 idx / PNV_IODA1_M64_SEGS,
3312 idx % PNV_IODA1_M64_SEGS);
3313 else
3314 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3315 phb->ioda.reserved_pe_idx, win, 0, idx);
3316
3317 if (rc != OPAL_SUCCESS)
3318 pe_warn(pe, "Error %ld unmapping (%d) segment#%d\n",
3319 rc, win, idx);
3320
3321 map[idx] = IODA_INVALID_PE;
3322 }
3323}
3324
3325static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
3326{
3327 struct pnv_phb *phb = pe->phb;
3328
3329 if (phb->type == PNV_PHB_IODA1) {
3330 pnv_ioda_free_pe_seg(pe, OPAL_IO_WINDOW_TYPE,
3331 phb->ioda.io_segmap);
3332 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
3333 phb->ioda.m32_segmap);
3334 pnv_ioda_free_pe_seg(pe, OPAL_M64_WINDOW_TYPE,
3335 phb->ioda.m64_segmap);
3336 } else if (phb->type == PNV_PHB_IODA2) {
3337 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
3338 phb->ioda.m32_segmap);
3339 }
3340}
3341
3342static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
3343{
3344 struct pnv_phb *phb = pe->phb;
3345 struct pnv_ioda_pe *slave, *tmp;
3346
3347 /* Release slave PEs in compound PE */
3348 if (pe->flags & PNV_IODA_PE_MASTER) {
3349 list_for_each_entry_safe(slave, tmp, &pe->slaves, list)
3350 pnv_ioda_release_pe(slave);
3351 }
3352
3353 list_del(&pe->list);
3354 switch (phb->type) {
3355 case PNV_PHB_IODA1:
3356 pnv_pci_ioda1_release_pe_dma(pe);
3357 break;
3358 case PNV_PHB_IODA2:
3359 pnv_pci_ioda2_release_pe_dma(pe);
3360 break;
3361 default:
3362 WARN_ON(1);
3363 }
3364
3365 pnv_ioda_release_pe_seg(pe);
3366 pnv_ioda_deconfigure_pe(pe->phb, pe);
3367 pnv_ioda_free_pe(pe);
3368}
3369
3370static void pnv_pci_release_device(struct pci_dev *pdev)
3371{
3372 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3373 struct pnv_phb *phb = hose->private_data;
3374 struct pci_dn *pdn = pci_get_pdn(pdev);
3375 struct pnv_ioda_pe *pe;
3376
3377 if (pdev->is_virtfn)
3378 return;
3379
3380 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
3381 return;
3382
3383 pe = &phb->ioda.pe_array[pdn->pe_number];
3384 WARN_ON(--pe->device_count < 0);
3385 if (pe->device_count == 0)
3386 pnv_ioda_release_pe(pe);
3387}
3388
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003389static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003390{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003391 struct pnv_phb *phb = hose->private_data;
3392
Gavin Shand1a85ee2014-09-30 12:39:05 +10003393 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003394 OPAL_ASSERT_RESET);
3395}
3396
Daniel Axtens92ae0352015-04-28 15:12:05 +10003397static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003398 .dma_dev_setup = pnv_pci_dma_dev_setup,
3399 .dma_bus_setup = pnv_pci_dma_bus_setup,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003400#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003401 .setup_msi_irqs = pnv_setup_msi_irqs,
3402 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003403#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003404 .enable_device_hook = pnv_pci_enable_device_hook,
Gavin Shanc5f77002016-05-20 16:41:35 +10003405 .release_device = pnv_pci_release_device,
Gavin Shancb4224c2016-05-03 15:41:21 +10003406 .window_alignment = pnv_pci_window_alignment,
Gavin Shanccd1c192016-05-20 16:41:31 +10003407 .setup_bridge = pnv_pci_setup_bridge,
Gavin Shancb4224c2016-05-03 15:41:21 +10003408 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3409 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3410 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3411 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003412};
3413
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003414static int pnv_npu_dma_set_mask(struct pci_dev *npdev, u64 dma_mask)
3415{
3416 dev_err_once(&npdev->dev,
3417 "%s operation unsupported for NVLink devices\n",
3418 __func__);
3419 return -EPERM;
3420}
3421
Alistair Popple5d2aa712015-12-17 13:43:13 +11003422static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003423 .dma_dev_setup = pnv_pci_dma_dev_setup,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003424#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003425 .setup_msi_irqs = pnv_setup_msi_irqs,
3426 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003427#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003428 .enable_device_hook = pnv_pci_enable_device_hook,
3429 .window_alignment = pnv_pci_window_alignment,
3430 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3431 .dma_set_mask = pnv_npu_dma_set_mask,
3432 .shutdown = pnv_pci_ioda_shutdown,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003433};
3434
Ian Munsie4361b032016-07-14 07:17:06 +10003435#ifdef CONFIG_CXL_BASE
3436const struct pci_controller_ops pnv_cxl_cx4_ioda_controller_ops = {
3437 .dma_dev_setup = pnv_pci_dma_dev_setup,
3438 .dma_bus_setup = pnv_pci_dma_bus_setup,
Ian Munsiea2f67d52016-07-14 07:17:10 +10003439#ifdef CONFIG_PCI_MSI
3440 .setup_msi_irqs = pnv_cxl_cx4_setup_msi_irqs,
3441 .teardown_msi_irqs = pnv_cxl_cx4_teardown_msi_irqs,
3442#endif
Ian Munsie4361b032016-07-14 07:17:06 +10003443 .enable_device_hook = pnv_cxl_enable_device_hook,
3444 .disable_device = pnv_cxl_disable_device,
3445 .release_device = pnv_pci_release_device,
3446 .window_alignment = pnv_pci_window_alignment,
3447 .setup_bridge = pnv_pci_setup_bridge,
3448 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3449 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3450 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3451 .shutdown = pnv_pci_ioda_shutdown,
3452};
3453#endif
3454
Anton Blancharde51df2c2014-08-20 08:55:18 +10003455static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3456 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003457{
3458 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003459 struct pnv_phb *phb;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003460 unsigned long size, m64map_off, m32map_off, pemap_off;
3461 unsigned long iomap_off = 0, dma32map_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10003462 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003463 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003464 int len;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003465 unsigned int segno;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003466 u64 phb_id;
3467 void *aux;
3468 long rc;
3469
Gavin Shan9497a1c2016-06-21 12:35:56 +10003470 pr_info("Initializing %s PHB (%s)\n",
3471 pnv_phb_names[ioda_type], of_node_full_name(np));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003472
3473 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3474 if (!prop64) {
3475 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3476 return;
3477 }
3478 phb_id = be64_to_cpup(prop64);
3479 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3480
Michael Ellermane39f223f2014-11-18 16:47:35 +11003481 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003482
3483 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003484 phb->hose = hose = pcibios_alloc_controller(np);
3485 if (!phb->hose) {
3486 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003487 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003488 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003489 return;
3490 }
3491
3492 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003493 prop32 = of_get_property(np, "bus-range", &len);
3494 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003495 hose->first_busno = be32_to_cpu(prop32[0]);
3496 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003497 } else {
3498 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3499 hose->first_busno = 0;
3500 hose->last_busno = 0xff;
3501 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003502 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003503 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003504 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003505 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003506 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003507
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003508 /* Detect specific models for error handling */
3509 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3510 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003511 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003512 phb->model = PNV_PHB_MODEL_PHB3;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003513 else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
3514 phb->model = PNV_PHB_MODEL_NPU;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003515 else
3516 phb->model = PNV_PHB_MODEL_UNKNOWN;
3517
Gavin Shanaa0c0332013-04-25 19:20:57 +00003518 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003519 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003520
Gavin Shanaa0c0332013-04-25 19:20:57 +00003521 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003522 phb->regs = of_iomap(np, 0);
3523 if (phb->regs == NULL)
3524 pr_err(" Failed to map registers !\n");
3525
Gavin Shan577c8c82016-05-20 16:41:28 +10003526 /* Initialize TCE kill register */
3527 pnv_pci_ioda_setup_opal_tce_kill(phb);
3528
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003529 /* Initialize more IODA stuff */
Gavin Shan92b8f132016-05-03 15:41:24 +10003530 phb->ioda.total_pe_num = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003531 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003532 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003533 phb->ioda.total_pe_num = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003534 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3535 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003536 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003537
Gavin Shanc1275622016-05-20 16:41:29 +10003538 /* Invalidate RID to PE# mapping */
3539 for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
3540 phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
3541
Guo Chao262af552014-07-21 14:42:30 +10003542 /* Parse 64-bit MMIO range */
3543 pnv_ioda_parse_m64_window(phb);
3544
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003545 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003546 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003547 phb->ioda.m32_size += 0x10000;
3548
Gavin Shan92b8f132016-05-03 15:41:24 +10003549 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003550 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003551 phb->ioda.io_size = hose->pci_io_size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003552 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003553 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3554
Gavin Shan2b923ed2016-05-05 12:04:16 +10003555 /* Calculate how many 32-bit TCE segments we have */
3556 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
3557 PNV_IODA1_DMA32_SEGSIZE;
3558
Gavin Shanc35d2a82013-07-31 16:47:04 +08003559 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Alexey Kardashevskiy92a86752016-05-12 15:47:09 +10003560 size = _ALIGN_UP(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
3561 sizeof(unsigned long));
Gavin Shan93289d82016-05-03 15:41:29 +10003562 m64map_off = size;
3563 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003564 m32map_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003565 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003566 if (phb->type == PNV_PHB_IODA1) {
3567 iomap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003568 size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
Gavin Shan2b923ed2016-05-05 12:04:16 +10003569 dma32map_off = size;
3570 size += phb->ioda.dma32_count *
3571 sizeof(phb->ioda.dma32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003572 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003573 pemap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003574 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003575 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003576 phb->ioda.pe_alloc = aux;
Gavin Shan93289d82016-05-03 15:41:29 +10003577 phb->ioda.m64_segmap = aux + m64map_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003578 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shan93289d82016-05-03 15:41:29 +10003579 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
3580 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003581 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan93289d82016-05-03 15:41:29 +10003582 }
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003583 if (phb->type == PNV_PHB_IODA1) {
Gavin Shanc35d2a82013-07-31 16:47:04 +08003584 phb->ioda.io_segmap = aux + iomap_off;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003585 for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
3586 phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003587
3588 phb->ioda.dma32_segmap = aux + dma32map_off;
3589 for (segno = 0; segno < phb->ioda.dma32_count; segno++)
3590 phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003591 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003592 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan63803c32016-05-20 16:41:32 +10003593
3594 /*
3595 * Choose PE number for root bus, which shouldn't have
3596 * M64 resources consumed by its child devices. To pick
3597 * the PE number adjacent to the reserved one if possible.
3598 */
3599 pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
3600 if (phb->ioda.reserved_pe_idx == 0) {
3601 phb->ioda.root_pe_idx = 1;
3602 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3603 } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
3604 phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
3605 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3606 } else {
3607 phb->ioda.root_pe_idx = IODA_INVALID_PE;
3608 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003609
3610 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003611 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003612
3613 /* Calculate how many 32-bit TCE segments we have */
Gavin Shan2b923ed2016-05-05 12:04:16 +10003614 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
Gavin Shanacce9712016-05-03 15:41:33 +10003615 PNV_IODA1_DMA32_SEGSIZE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003616
Gavin Shanaa0c0332013-04-25 19:20:57 +00003617#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003618 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3619 window_type,
3620 window_num,
3621 starting_real_address,
3622 starting_pci_address,
3623 segment_size);
3624#endif
3625
Guo Chao262af552014-07-21 14:42:30 +10003626 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
Gavin Shan92b8f132016-05-03 15:41:24 +10003627 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
Guo Chao262af552014-07-21 14:42:30 +10003628 phb->ioda.m32_size, phb->ioda.m32_segsize);
3629 if (phb->ioda.m64_size)
3630 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3631 phb->ioda.m64_size, phb->ioda.m64_segsize);
3632 if (phb->ioda.io_size)
3633 pr_info(" IO: 0x%x [segment=0x%x]\n",
3634 phb->ioda.io_size, phb->ioda.io_segsize);
3635
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003636
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003637 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003638 phb->get_pe_state = pnv_ioda_get_pe_state;
3639 phb->freeze_pe = pnv_ioda_freeze_pe;
3640 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003641
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003642 /* Setup MSI support */
3643 pnv_pci_init_ioda_msis(phb);
3644
Gavin Shanc40a4212012-08-20 03:49:20 +00003645 /*
3646 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3647 * to let the PCI core do resource assignment. It's supposed
3648 * that the PCI core will do correct I/O and MMIO alignment
3649 * for the P2P bridge bars so that each PCI bus (excluding
3650 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003651 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003652 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003653
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003654 if (phb->type == PNV_PHB_NPU) {
Alistair Popple5d2aa712015-12-17 13:43:13 +11003655 hose->controller_ops = pnv_npu_ioda_controller_ops;
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003656 } else {
3657 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003658 hose->controller_ops = pnv_pci_ioda_controller_ops;
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003659 }
Michael Ellermanad30cb92015-04-14 09:29:23 +10003660
Wei Yang6e628c72015-03-25 16:23:55 +08003661#ifdef CONFIG_PCI_IOV
3662 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003663 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003664#endif
3665
Gavin Shanc40a4212012-08-20 03:49:20 +00003666 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003667
3668 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003669 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003670 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003671 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003672
3673 /* If we're running in kdump kerenl, the previous kerenl never
3674 * shutdown PCI devices correctly. We already got IODA table
3675 * cleaned out. So we have to issue PHB reset to stop all PCI
3676 * transactions from previous kerenl.
3677 */
3678 if (is_kdump_kernel()) {
3679 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003680 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3681 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003682 }
Guo Chao262af552014-07-21 14:42:30 +10003683
Gavin Shan9e9e8932014-11-12 13:36:05 +11003684 /* Remove M64 resource if we can't configure it successfully */
3685 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003686 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003687}
3688
Bjorn Helgaas67975002013-07-02 12:20:03 -06003689void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003690{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003691 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003692}
3693
Alistair Popple5d2aa712015-12-17 13:43:13 +11003694void __init pnv_pci_init_npu_phb(struct device_node *np)
3695{
3696 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU);
3697}
3698
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003699void __init pnv_pci_init_ioda_hub(struct device_node *np)
3700{
3701 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003702 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003703 u64 hub_id;
3704
3705 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3706
3707 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3708 if (!prop64) {
3709 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3710 return;
3711 }
3712 hub_id = be64_to_cpup(prop64);
3713 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3714
3715 /* Count child PHBs */
3716 for_each_child_of_node(np, phbn) {
3717 /* Look for IODA1 PHBs */
3718 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003719 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003720 }
3721}