blob: dedbbe9785ba1538b39378abd2f4ed24d8e8ff97 [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
Guo Chao262af552014-07-21 14:42:30 +1000194 /*
Gavin Shan63803c32016-05-20 16:41:32 +1000195 * Exclude the segments for reserved and root bus PE, which
196 * are first or last two PEs.
Guo Chao262af552014-07-21 14:42:30 +1000197 */
198 r = &phb->hose->mem_resources[1];
Gavin Shan92b8f132016-05-03 15:41:24 +1000199 if (phb->ioda.reserved_pe_idx == 0)
Gavin Shan63803c32016-05-20 16:41:32 +1000200 r->start += (2 * phb->ioda.m64_segsize);
Gavin Shan92b8f132016-05-03 15:41:24 +1000201 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Gavin Shan63803c32016-05-20 16:41:32 +1000202 r->end -= (2 * phb->ioda.m64_segsize);
Guo Chao262af552014-07-21 14:42:30 +1000203 else
204 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
Gavin Shan92b8f132016-05-03 15:41:24 +1000205 phb->ioda.reserved_pe_idx);
Guo Chao262af552014-07-21 14:42:30 +1000206
207 return 0;
208
209fail:
210 pr_warn(" Failure %lld %s M64 BAR#%d\n",
211 rc, desc, phb->ioda.m64_bar_idx);
212 opal_pci_phb_mmio_enable(phb->opal_id,
213 OPAL_M64_WINDOW_TYPE,
214 phb->ioda.m64_bar_idx,
215 OPAL_DISABLE_M64);
216 return -EIO;
217}
218
Gavin Shanc4306702016-05-03 15:41:30 +1000219static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
Gavin Shan96a2f922015-06-19 12:26:17 +1000220 unsigned long *pe_bitmap)
Guo Chao262af552014-07-21 14:42:30 +1000221{
Gavin Shan96a2f922015-06-19 12:26:17 +1000222 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
223 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000224 struct resource *r;
Gavin Shan96a2f922015-06-19 12:26:17 +1000225 resource_size_t base, sgsz, start, end;
226 int segno, i;
Guo Chao262af552014-07-21 14:42:30 +1000227
Gavin Shan96a2f922015-06-19 12:26:17 +1000228 base = phb->ioda.m64_base;
229 sgsz = phb->ioda.m64_segsize;
230 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
231 r = &pdev->resource[i];
232 if (!r->parent || !pnv_pci_is_mem_pref_64(r->flags))
233 continue;
Guo Chao262af552014-07-21 14:42:30 +1000234
Gavin Shan96a2f922015-06-19 12:26:17 +1000235 start = _ALIGN_DOWN(r->start - base, sgsz);
236 end = _ALIGN_UP(r->end - base, sgsz);
237 for (segno = start / sgsz; segno < end / sgsz; segno++) {
238 if (pe_bitmap)
239 set_bit(segno, pe_bitmap);
240 else
241 pnv_ioda_reserve_pe(phb, segno);
Guo Chao262af552014-07-21 14:42:30 +1000242 }
243 }
244}
245
Gavin Shan99451552016-05-05 12:02:13 +1000246static int pnv_ioda1_init_m64(struct pnv_phb *phb)
247{
248 struct resource *r;
249 int index;
250
251 /*
252 * There are 16 M64 BARs, each of which has 8 segments. So
253 * there are as many M64 segments as the maximum number of
254 * PEs, which is 128.
255 */
256 for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
257 unsigned long base, segsz = phb->ioda.m64_segsize;
258 int64_t rc;
259
260 base = phb->ioda.m64_base +
261 index * PNV_IODA1_M64_SEGS * segsz;
262 rc = opal_pci_set_phb_mem_window(phb->opal_id,
263 OPAL_M64_WINDOW_TYPE, index, base, 0,
264 PNV_IODA1_M64_SEGS * segsz);
265 if (rc != OPAL_SUCCESS) {
266 pr_warn(" Error %lld setting M64 PHB#%d-BAR#%d\n",
267 rc, phb->hose->global_number, index);
268 goto fail;
269 }
270
271 rc = opal_pci_phb_mmio_enable(phb->opal_id,
272 OPAL_M64_WINDOW_TYPE, index,
273 OPAL_ENABLE_M64_SPLIT);
274 if (rc != OPAL_SUCCESS) {
275 pr_warn(" Error %lld enabling M64 PHB#%d-BAR#%d\n",
276 rc, phb->hose->global_number, index);
277 goto fail;
278 }
279 }
280
281 /*
Gavin Shan63803c32016-05-20 16:41:32 +1000282 * Exclude the segments for reserved and root bus PE, which
283 * are first or last two PEs.
Gavin Shan99451552016-05-05 12:02:13 +1000284 */
285 r = &phb->hose->mem_resources[1];
286 if (phb->ioda.reserved_pe_idx == 0)
Gavin Shan63803c32016-05-20 16:41:32 +1000287 r->start += (2 * phb->ioda.m64_segsize);
Gavin Shan99451552016-05-05 12:02:13 +1000288 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Gavin Shan63803c32016-05-20 16:41:32 +1000289 r->end -= (2 * phb->ioda.m64_segsize);
Gavin Shan99451552016-05-05 12:02:13 +1000290 else
291 WARN(1, "Wrong reserved PE#%d on PHB#%d\n",
292 phb->ioda.reserved_pe_idx, phb->hose->global_number);
293
294 return 0;
295
296fail:
297 for ( ; index >= 0; index--)
298 opal_pci_phb_mmio_enable(phb->opal_id,
299 OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
300
301 return -EIO;
302}
303
Gavin Shanc4306702016-05-03 15:41:30 +1000304static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
305 unsigned long *pe_bitmap,
306 bool all)
Guo Chao262af552014-07-21 14:42:30 +1000307{
Guo Chao262af552014-07-21 14:42:30 +1000308 struct pci_dev *pdev;
Gavin Shan96a2f922015-06-19 12:26:17 +1000309
310 list_for_each_entry(pdev, &bus->devices, bus_list) {
Gavin Shanc4306702016-05-03 15:41:30 +1000311 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
Gavin Shan96a2f922015-06-19 12:26:17 +1000312
313 if (all && pdev->subordinate)
Gavin Shanc4306702016-05-03 15:41:30 +1000314 pnv_ioda_reserve_m64_pe(pdev->subordinate,
315 pe_bitmap, all);
Gavin Shan96a2f922015-06-19 12:26:17 +1000316 }
317}
318
Gavin Shan1e916772016-05-03 15:41:36 +1000319static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
Guo Chao262af552014-07-21 14:42:30 +1000320{
Gavin Shan26ba2482015-06-19 12:26:19 +1000321 struct pci_controller *hose = pci_bus_to_host(bus);
322 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000323 struct pnv_ioda_pe *master_pe, *pe;
324 unsigned long size, *pe_alloc;
Gavin Shan26ba2482015-06-19 12:26:19 +1000325 int i;
Guo Chao262af552014-07-21 14:42:30 +1000326
327 /* Root bus shouldn't use M64 */
328 if (pci_is_root_bus(bus))
Gavin Shan1e916772016-05-03 15:41:36 +1000329 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000330
Guo Chao262af552014-07-21 14:42:30 +1000331 /* Allocate bitmap */
Gavin Shan92b8f132016-05-03 15:41:24 +1000332 size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
Guo Chao262af552014-07-21 14:42:30 +1000333 pe_alloc = kzalloc(size, GFP_KERNEL);
334 if (!pe_alloc) {
335 pr_warn("%s: Out of memory !\n",
336 __func__);
Gavin Shan1e916772016-05-03 15:41:36 +1000337 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000338 }
339
Gavin Shan26ba2482015-06-19 12:26:19 +1000340 /* Figure out reserved PE numbers by the PE */
Gavin Shanc4306702016-05-03 15:41:30 +1000341 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
Guo Chao262af552014-07-21 14:42:30 +1000342
343 /*
344 * the current bus might not own M64 window and that's all
345 * contributed by its child buses. For the case, we needn't
346 * pick M64 dependent PE#.
347 */
Gavin Shan92b8f132016-05-03 15:41:24 +1000348 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
Guo Chao262af552014-07-21 14:42:30 +1000349 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000350 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000351 }
352
353 /*
354 * Figure out the master PE and put all slave PEs to master
355 * PE's list to form compound PE.
356 */
Guo Chao262af552014-07-21 14:42:30 +1000357 master_pe = NULL;
358 i = -1;
Gavin Shan92b8f132016-05-03 15:41:24 +1000359 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
360 phb->ioda.total_pe_num) {
Guo Chao262af552014-07-21 14:42:30 +1000361 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000362
Gavin Shan93289d82016-05-03 15:41:29 +1000363 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
Guo Chao262af552014-07-21 14:42:30 +1000364 if (!master_pe) {
365 pe->flags |= PNV_IODA_PE_MASTER;
366 INIT_LIST_HEAD(&pe->slaves);
367 master_pe = pe;
368 } else {
369 pe->flags |= PNV_IODA_PE_SLAVE;
370 pe->master = master_pe;
371 list_add_tail(&pe->list, &master_pe->slaves);
372 }
Gavin Shan99451552016-05-05 12:02:13 +1000373
374 /*
375 * P7IOC supports M64DT, which helps mapping M64 segment
376 * to one particular PE#. However, PHB3 has fixed mapping
377 * between M64 segment and PE#. In order to have same logic
378 * for P7IOC and PHB3, we enforce fixed mapping between M64
379 * segment and PE# on P7IOC.
380 */
381 if (phb->type == PNV_PHB_IODA1) {
382 int64_t rc;
383
384 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
385 pe->pe_number, OPAL_M64_WINDOW_TYPE,
386 pe->pe_number / PNV_IODA1_M64_SEGS,
387 pe->pe_number % PNV_IODA1_M64_SEGS);
388 if (rc != OPAL_SUCCESS)
389 pr_warn("%s: Error %lld mapping M64 for PHB#%d-PE#%d\n",
390 __func__, rc, phb->hose->global_number,
391 pe->pe_number);
392 }
Guo Chao262af552014-07-21 14:42:30 +1000393 }
394
395 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000396 return master_pe;
Guo Chao262af552014-07-21 14:42:30 +1000397}
398
399static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
400{
401 struct pci_controller *hose = phb->hose;
402 struct device_node *dn = hose->dn;
403 struct resource *res;
Benjamin Herrenschmidta1339fa2016-07-08 16:37:16 +1000404 u32 m64_range[2], i;
Guo Chao262af552014-07-21 14:42:30 +1000405 const u32 *r;
406 u64 pci_addr;
407
Gavin Shan99451552016-05-05 12:02:13 +1000408 if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
Gavin Shan1665c4a2014-11-12 13:36:04 +1100409 pr_info(" Not support M64 window\n");
410 return;
411 }
412
Stewart Smithe4d54f72015-12-09 17:18:20 +1100413 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
Guo Chao262af552014-07-21 14:42:30 +1000414 pr_info(" Firmware too old to support M64 window\n");
415 return;
416 }
417
418 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
419 if (!r) {
420 pr_info(" No <ibm,opal-m64-window> on %s\n",
421 dn->full_name);
422 return;
423 }
424
Benjamin Herrenschmidta1339fa2016-07-08 16:37:16 +1000425 /*
426 * Find the available M64 BAR range and pickup the last one for
427 * covering the whole 64-bits space. We support only one range.
428 */
429 if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges",
430 m64_range, 2)) {
431 /* In absence of the property, assume 0..15 */
432 m64_range[0] = 0;
433 m64_range[1] = 16;
434 }
435 /* We only support 64 bits in our allocator */
436 if (m64_range[1] > 63) {
437 pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",
438 __func__, m64_range[1], phb->hose->global_number);
439 m64_range[1] = 63;
440 }
441 /* Empty range, no m64 */
442 if (m64_range[1] <= m64_range[0]) {
443 pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",
444 __func__, phb->hose->global_number);
445 return;
446 }
447
448 /* Configure M64 informations */
Guo Chao262af552014-07-21 14:42:30 +1000449 res = &hose->mem_resources[1];
Gavin Shane80c4e72015-10-22 12:03:08 +1100450 res->name = dn->full_name;
Guo Chao262af552014-07-21 14:42:30 +1000451 res->start = of_translate_address(dn, r + 2);
452 res->end = res->start + of_read_number(r + 4, 2) - 1;
453 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
454 pci_addr = of_read_number(r, 2);
455 hose->mem_offset[1] = res->start - pci_addr;
456
457 phb->ioda.m64_size = resource_size(res);
Gavin Shan92b8f132016-05-03 15:41:24 +1000458 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
Guo Chao262af552014-07-21 14:42:30 +1000459 phb->ioda.m64_base = pci_addr;
460
Benjamin Herrenschmidta1339fa2016-07-08 16:37:16 +1000461 /* This lines up nicely with the display from processing OF ranges */
462 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",
463 res->start, res->end, pci_addr, m64_range[0],
464 m64_range[0] + m64_range[1] - 1);
465
466 /* Mark all M64 used up by default */
467 phb->ioda.m64_bar_alloc = (unsigned long)-1;
Wei Yange9863e62014-12-12 12:39:37 +0800468
Guo Chao262af552014-07-21 14:42:30 +1000469 /* Use last M64 BAR to cover M64 window */
Benjamin Herrenschmidta1339fa2016-07-08 16:37:16 +1000470 m64_range[1]--;
471 phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1];
472
473 pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx);
474
475 /* Mark remaining ones free */
476 for (i = m64_range[0]; i < m64_range[1]; i++)
477 clear_bit(i, &phb->ioda.m64_bar_alloc);
478
479 /*
480 * Setup init functions for M64 based on IODA version, IODA3 uses
481 * the IODA2 code.
482 */
Gavin Shan99451552016-05-05 12:02:13 +1000483 if (phb->type == PNV_PHB_IODA1)
484 phb->init_m64 = pnv_ioda1_init_m64;
485 else
486 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shanc4306702016-05-03 15:41:30 +1000487 phb->reserve_m64_pe = pnv_ioda_reserve_m64_pe;
488 phb->pick_m64_pe = pnv_ioda_pick_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000489}
490
Gavin Shan49dec922014-07-21 14:42:33 +1000491static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
492{
493 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
494 struct pnv_ioda_pe *slave;
495 s64 rc;
496
497 /* Fetch master PE */
498 if (pe->flags & PNV_IODA_PE_SLAVE) {
499 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100500 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
501 return;
502
Gavin Shan49dec922014-07-21 14:42:33 +1000503 pe_no = pe->pe_number;
504 }
505
506 /* Freeze master PE */
507 rc = opal_pci_eeh_freeze_set(phb->opal_id,
508 pe_no,
509 OPAL_EEH_ACTION_SET_FREEZE_ALL);
510 if (rc != OPAL_SUCCESS) {
511 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
512 __func__, rc, phb->hose->global_number, pe_no);
513 return;
514 }
515
516 /* Freeze slave PEs */
517 if (!(pe->flags & PNV_IODA_PE_MASTER))
518 return;
519
520 list_for_each_entry(slave, &pe->slaves, list) {
521 rc = opal_pci_eeh_freeze_set(phb->opal_id,
522 slave->pe_number,
523 OPAL_EEH_ACTION_SET_FREEZE_ALL);
524 if (rc != OPAL_SUCCESS)
525 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
526 __func__, rc, phb->hose->global_number,
527 slave->pe_number);
528 }
529}
530
Anton Blancharde51df2c2014-08-20 08:55:18 +1000531static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000532{
533 struct pnv_ioda_pe *pe, *slave;
534 s64 rc;
535
536 /* Find master PE */
537 pe = &phb->ioda.pe_array[pe_no];
538 if (pe->flags & PNV_IODA_PE_SLAVE) {
539 pe = pe->master;
540 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
541 pe_no = pe->pe_number;
542 }
543
544 /* Clear frozen state for master PE */
545 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
546 if (rc != OPAL_SUCCESS) {
547 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
548 __func__, rc, opt, phb->hose->global_number, pe_no);
549 return -EIO;
550 }
551
552 if (!(pe->flags & PNV_IODA_PE_MASTER))
553 return 0;
554
555 /* Clear frozen state for slave PEs */
556 list_for_each_entry(slave, &pe->slaves, list) {
557 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
558 slave->pe_number,
559 opt);
560 if (rc != OPAL_SUCCESS) {
561 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
562 __func__, rc, opt, phb->hose->global_number,
563 slave->pe_number);
564 return -EIO;
565 }
566 }
567
568 return 0;
569}
570
571static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
572{
573 struct pnv_ioda_pe *slave, *pe;
574 u8 fstate, state;
575 __be16 pcierr;
576 s64 rc;
577
578 /* Sanity check on PE number */
Gavin Shan92b8f132016-05-03 15:41:24 +1000579 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
Gavin Shan49dec922014-07-21 14:42:33 +1000580 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
581
582 /*
583 * Fetch the master PE and the PE instance might be
584 * not initialized yet.
585 */
586 pe = &phb->ioda.pe_array[pe_no];
587 if (pe->flags & PNV_IODA_PE_SLAVE) {
588 pe = pe->master;
589 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
590 pe_no = pe->pe_number;
591 }
592
593 /* Check the master PE */
594 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
595 &state, &pcierr, NULL);
596 if (rc != OPAL_SUCCESS) {
597 pr_warn("%s: Failure %lld getting "
598 "PHB#%x-PE#%x state\n",
599 __func__, rc,
600 phb->hose->global_number, pe_no);
601 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
602 }
603
604 /* Check the slave PE */
605 if (!(pe->flags & PNV_IODA_PE_MASTER))
606 return state;
607
608 list_for_each_entry(slave, &pe->slaves, list) {
609 rc = opal_pci_eeh_freeze_status(phb->opal_id,
610 slave->pe_number,
611 &fstate,
612 &pcierr,
613 NULL);
614 if (rc != OPAL_SUCCESS) {
615 pr_warn("%s: Failure %lld getting "
616 "PHB#%x-PE#%x state\n",
617 __func__, rc,
618 phb->hose->global_number, slave->pe_number);
619 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
620 }
621
622 /*
623 * Override the result based on the ascending
624 * priority.
625 */
626 if (fstate > state)
627 state = fstate;
628 }
629
630 return state;
631}
632
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000633/* Currently those 2 are only used when MSIs are enabled, this will change
634 * but in the meantime, we need to protect them to avoid warnings
635 */
636#ifdef CONFIG_PCI_MSI
Ian Munsief4568342016-07-14 07:17:00 +1000637struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000638{
639 struct pci_controller *hose = pci_bus_to_host(dev->bus);
640 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000641 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000642
643 if (!pdn)
644 return NULL;
645 if (pdn->pe_number == IODA_INVALID_PE)
646 return NULL;
647 return &phb->ioda.pe_array[pdn->pe_number];
648}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000649#endif /* CONFIG_PCI_MSI */
650
Gavin Shanb131a842014-11-12 13:36:08 +1100651static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
652 struct pnv_ioda_pe *parent,
653 struct pnv_ioda_pe *child,
654 bool is_add)
655{
656 const char *desc = is_add ? "adding" : "removing";
657 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
658 OPAL_REMOVE_PE_FROM_DOMAIN;
659 struct pnv_ioda_pe *slave;
660 long rc;
661
662 /* Parent PE affects child PE */
663 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
664 child->pe_number, op);
665 if (rc != OPAL_SUCCESS) {
666 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
667 rc, desc);
668 return -ENXIO;
669 }
670
671 if (!(child->flags & PNV_IODA_PE_MASTER))
672 return 0;
673
674 /* Compound case: parent PE affects slave PEs */
675 list_for_each_entry(slave, &child->slaves, list) {
676 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
677 slave->pe_number, op);
678 if (rc != OPAL_SUCCESS) {
679 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
680 rc, desc);
681 return -ENXIO;
682 }
683 }
684
685 return 0;
686}
687
688static int pnv_ioda_set_peltv(struct pnv_phb *phb,
689 struct pnv_ioda_pe *pe,
690 bool is_add)
691{
692 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800693 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100694 int ret;
695
696 /*
697 * Clear PE frozen state. If it's master PE, we need
698 * clear slave PE frozen state as well.
699 */
700 if (is_add) {
701 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
702 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
703 if (pe->flags & PNV_IODA_PE_MASTER) {
704 list_for_each_entry(slave, &pe->slaves, list)
705 opal_pci_eeh_freeze_clear(phb->opal_id,
706 slave->pe_number,
707 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
708 }
709 }
710
711 /*
712 * Associate PE in PELT. We need add the PE into the
713 * corresponding PELT-V as well. Otherwise, the error
714 * originated from the PE might contribute to other
715 * PEs.
716 */
717 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
718 if (ret)
719 return ret;
720
721 /* For compound PEs, any one affects all of them */
722 if (pe->flags & PNV_IODA_PE_MASTER) {
723 list_for_each_entry(slave, &pe->slaves, list) {
724 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
725 if (ret)
726 return ret;
727 }
728 }
729
730 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
731 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800732 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100733 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800734#ifdef CONFIG_PCI_IOV
735 else if (pe->flags & PNV_IODA_PE_VF)
Gavin Shan283e2d82015-06-22 13:45:47 +1000736 pdev = pe->parent_dev;
Wei Yang781a8682015-03-25 16:23:57 +0800737#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100738 while (pdev) {
739 struct pci_dn *pdn = pci_get_pdn(pdev);
740 struct pnv_ioda_pe *parent;
741
742 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
743 parent = &phb->ioda.pe_array[pdn->pe_number];
744 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
745 if (ret)
746 return ret;
747 }
748
749 pdev = pdev->bus->self;
750 }
751
752 return 0;
753}
754
Wei Yang781a8682015-03-25 16:23:57 +0800755static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
756{
757 struct pci_dev *parent;
758 uint8_t bcomp, dcomp, fcomp;
759 int64_t rc;
760 long rid_end, rid;
761
762 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
763 if (pe->pbus) {
764 int count;
765
766 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
767 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
768 parent = pe->pbus->self;
769 if (pe->flags & PNV_IODA_PE_BUS_ALL)
770 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
771 else
772 count = 1;
773
774 switch(count) {
775 case 1: bcomp = OpalPciBusAll; break;
776 case 2: bcomp = OpalPciBus7Bits; break;
777 case 4: bcomp = OpalPciBus6Bits; break;
778 case 8: bcomp = OpalPciBus5Bits; break;
779 case 16: bcomp = OpalPciBus4Bits; break;
780 case 32: bcomp = OpalPciBus3Bits; break;
781 default:
782 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
783 count);
784 /* Do an exact match only */
785 bcomp = OpalPciBusAll;
786 }
787 rid_end = pe->rid + (count << 8);
788 } else {
Gavin Shan93e01a52016-05-20 16:41:34 +1000789#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +0800790 if (pe->flags & PNV_IODA_PE_VF)
791 parent = pe->parent_dev;
792 else
Gavin Shan93e01a52016-05-20 16:41:34 +1000793#endif
Wei Yang781a8682015-03-25 16:23:57 +0800794 parent = pe->pdev->bus->self;
795 bcomp = OpalPciBusAll;
796 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
797 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
798 rid_end = pe->rid + 1;
799 }
800
801 /* Clear the reverse map */
802 for (rid = pe->rid; rid < rid_end; rid++)
Gavin Shanc1275622016-05-20 16:41:29 +1000803 phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
Wei Yang781a8682015-03-25 16:23:57 +0800804
805 /* Release from all parents PELT-V */
806 while (parent) {
807 struct pci_dn *pdn = pci_get_pdn(parent);
808 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
809 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
810 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
811 /* XXX What to do in case of error ? */
812 }
813 parent = parent->bus->self;
814 }
815
Gavin Shanf951e512015-06-23 17:01:13 +1000816 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
Wei Yang781a8682015-03-25 16:23:57 +0800817 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
818
819 /* Disassociate PE in PELT */
820 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
821 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
822 if (rc)
823 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
824 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
825 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
826 if (rc)
827 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
828
829 pe->pbus = NULL;
830 pe->pdev = NULL;
Gavin Shan93e01a52016-05-20 16:41:34 +1000831#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +0800832 pe->parent_dev = NULL;
Gavin Shan93e01a52016-05-20 16:41:34 +1000833#endif
Wei Yang781a8682015-03-25 16:23:57 +0800834
835 return 0;
836}
Wei Yang781a8682015-03-25 16:23:57 +0800837
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800838static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000839{
840 struct pci_dev *parent;
841 uint8_t bcomp, dcomp, fcomp;
842 long rc, rid_end, rid;
843
844 /* Bus validation ? */
845 if (pe->pbus) {
846 int count;
847
848 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
849 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
850 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000851 if (pe->flags & PNV_IODA_PE_BUS_ALL)
852 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
853 else
854 count = 1;
855
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000856 switch(count) {
857 case 1: bcomp = OpalPciBusAll; break;
858 case 2: bcomp = OpalPciBus7Bits; break;
859 case 4: bcomp = OpalPciBus6Bits; break;
860 case 8: bcomp = OpalPciBus5Bits; break;
861 case 16: bcomp = OpalPciBus4Bits; break;
862 case 32: bcomp = OpalPciBus3Bits; break;
863 default:
Wei Yang781a8682015-03-25 16:23:57 +0800864 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
865 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000866 /* Do an exact match only */
867 bcomp = OpalPciBusAll;
868 }
869 rid_end = pe->rid + (count << 8);
870 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800871#ifdef CONFIG_PCI_IOV
872 if (pe->flags & PNV_IODA_PE_VF)
873 parent = pe->parent_dev;
874 else
875#endif /* CONFIG_PCI_IOV */
876 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000877 bcomp = OpalPciBusAll;
878 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
879 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
880 rid_end = pe->rid + 1;
881 }
882
Gavin Shan631ad692013-11-04 16:32:46 +0800883 /*
884 * Associate PE in PELT. We need add the PE into the
885 * corresponding PELT-V as well. Otherwise, the error
886 * originated from the PE might contribute to other
887 * PEs.
888 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000889 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
890 bcomp, dcomp, fcomp, OPAL_MAP_PE);
891 if (rc) {
892 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
893 return -ENXIO;
894 }
Gavin Shan631ad692013-11-04 16:32:46 +0800895
Alistair Popple5d2aa712015-12-17 13:43:13 +1100896 /*
897 * Configure PELTV. NPUs don't have a PELTV table so skip
898 * configuration on them.
899 */
900 if (phb->type != PNV_PHB_NPU)
901 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000902
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000903 /* Setup reverse map */
904 for (rid = pe->rid; rid < rid_end; rid++)
905 phb->ioda.pe_rmap[rid] = pe->pe_number;
906
907 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100908 if (phb->type != PNV_PHB_IODA1) {
909 pe->mve_number = 0;
910 goto out;
911 }
912
913 pe->mve_number = pe->pe_number;
914 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
915 if (rc != OPAL_SUCCESS) {
916 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
917 rc, pe->mve_number);
918 pe->mve_number = -1;
919 } else {
920 rc = opal_pci_set_mve_enable(phb->opal_id,
921 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000922 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100923 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000924 rc, pe->mve_number);
925 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000926 }
Gavin Shan4773f762014-11-12 13:36:09 +1100927 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000928
Gavin Shan4773f762014-11-12 13:36:09 +1100929out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000930 return 0;
931}
932
Wei Yang781a8682015-03-25 16:23:57 +0800933#ifdef CONFIG_PCI_IOV
934static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
935{
936 struct pci_dn *pdn = pci_get_pdn(dev);
937 int i;
938 struct resource *res, res2;
939 resource_size_t size;
940 u16 num_vfs;
941
942 if (!dev->is_physfn)
943 return -EINVAL;
944
945 /*
946 * "offset" is in VFs. The M64 windows are sized so that when they
947 * are segmented, each segment is the same size as the IOV BAR.
948 * Each segment is in a separate PE, and the high order bits of the
949 * address are the PE number. Therefore, each VF's BAR is in a
950 * separate PE, and changing the IOV BAR start address changes the
951 * range of PEs the VFs are in.
952 */
953 num_vfs = pdn->num_vfs;
954 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
955 res = &dev->resource[i + PCI_IOV_RESOURCES];
956 if (!res->flags || !res->parent)
957 continue;
958
Wei Yang781a8682015-03-25 16:23:57 +0800959 /*
960 * The actual IOV BAR range is determined by the start address
961 * and the actual size for num_vfs VFs BAR. This check is to
962 * make sure that after shifting, the range will not overlap
963 * with another device.
964 */
965 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
966 res2.flags = res->flags;
967 res2.start = res->start + (size * offset);
968 res2.end = res2.start + (size * num_vfs) - 1;
969
970 if (res2.end > res->end) {
971 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
972 i, &res2, res, num_vfs, offset);
973 return -EBUSY;
974 }
975 }
976
977 /*
978 * After doing so, there would be a "hole" in the /proc/iomem when
979 * offset is a positive value. It looks like the device return some
980 * mmio back to the system, which actually no one could use it.
981 */
982 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
983 res = &dev->resource[i + PCI_IOV_RESOURCES];
984 if (!res->flags || !res->parent)
985 continue;
986
Wei Yang781a8682015-03-25 16:23:57 +0800987 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
988 res2 = *res;
989 res->start += size * offset;
990
Wei Yang74703cc2015-07-20 18:14:58 +0800991 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
992 i, &res2, res, (offset > 0) ? "En" : "Dis",
993 num_vfs, offset);
Wei Yang781a8682015-03-25 16:23:57 +0800994 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
995 }
996 return 0;
997}
998#endif /* CONFIG_PCI_IOV */
999
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001000static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001001{
1002 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1003 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001004 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001005 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001006
1007 if (!pdn) {
1008 pr_err("%s: Device tree node not associated properly\n",
1009 pci_name(dev));
1010 return NULL;
1011 }
1012 if (pdn->pe_number != IODA_INVALID_PE)
1013 return NULL;
1014
Gavin Shan1e916772016-05-03 15:41:36 +10001015 pe = pnv_ioda_alloc_pe(phb);
1016 if (!pe) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001017 pr_warning("%s: Not enough PE# available, disabling device\n",
1018 pci_name(dev));
1019 return NULL;
1020 }
1021
1022 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
1023 * pointer in the PE data structure, both should be destroyed at the
1024 * same time. However, this needs to be looked at more closely again
1025 * once we actually start removing things (Hotplug, SR-IOV, ...)
1026 *
1027 * At some point we want to remove the PDN completely anyways
1028 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001029 pci_dev_get(dev);
1030 pdn->pcidev = dev;
Gavin Shan1e916772016-05-03 15:41:36 +10001031 pdn->pe_number = pe->pe_number;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001032 pe->flags = PNV_IODA_PE_DEV;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001033 pe->pdev = dev;
1034 pe->pbus = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001035 pe->mve_number = -1;
1036 pe->rid = dev->bus->number << 8 | pdn->devfn;
1037
1038 pe_info(pe, "Associated device to PE\n");
1039
1040 if (pnv_ioda_configure_pe(phb, pe)) {
1041 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001042 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001043 pdn->pe_number = IODA_INVALID_PE;
1044 pe->pdev = NULL;
1045 pci_dev_put(dev);
1046 return NULL;
1047 }
1048
Alexey Kardashevskiy1d4e89c2016-05-12 15:47:10 +10001049 /* Put PE to the list */
1050 list_add_tail(&pe->list, &phb->ioda.pe_list);
1051
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001052 return pe;
1053}
1054
1055static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1056{
1057 struct pci_dev *dev;
1058
1059 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001060 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001061
1062 if (pdn == NULL) {
1063 pr_warn("%s: No device node associated with device !\n",
1064 pci_name(dev));
1065 continue;
1066 }
Gavin Shanccd1c192016-05-20 16:41:31 +10001067
1068 /*
1069 * In partial hotplug case, the PCI device might be still
1070 * associated with the PE and needn't attach it to the PE
1071 * again.
1072 */
1073 if (pdn->pe_number != IODA_INVALID_PE)
1074 continue;
1075
Gavin Shanc5f77002016-05-20 16:41:35 +10001076 pe->device_count++;
Alistair Popple94973b22015-12-17 13:43:11 +11001077 pdn->pcidev = dev;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001078 pdn->pe_number = pe->pe_number;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001079 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001080 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1081 }
1082}
1083
Gavin Shanfb446ad2012-08-20 03:49:14 +00001084/*
1085 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1086 * single PCI bus. Another one that contains the primary PCI bus and its
1087 * subordinate PCI devices and buses. The second type of PE is normally
1088 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1089 */
Gavin Shan1e916772016-05-03 15:41:36 +10001090static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001091{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001092 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001093 struct pnv_phb *phb = hose->private_data;
Gavin Shan1e916772016-05-03 15:41:36 +10001094 struct pnv_ioda_pe *pe = NULL;
Gavin Shanccd1c192016-05-20 16:41:31 +10001095 unsigned int pe_num;
1096
1097 /*
1098 * In partial hotplug case, the PE instance might be still alive.
1099 * We should reuse it instead of allocating a new one.
1100 */
1101 pe_num = phb->ioda.pe_rmap[bus->number << 8];
1102 if (pe_num != IODA_INVALID_PE) {
1103 pe = &phb->ioda.pe_array[pe_num];
1104 pnv_ioda_setup_same_PE(bus, pe);
1105 return NULL;
1106 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001107
Gavin Shan63803c32016-05-20 16:41:32 +10001108 /* PE number for root bus should have been reserved */
1109 if (pci_is_root_bus(bus) &&
1110 phb->ioda.root_pe_idx != IODA_INVALID_PE)
1111 pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
1112
Guo Chao262af552014-07-21 14:42:30 +10001113 /* Check if PE is determined by M64 */
Gavin Shan63803c32016-05-20 16:41:32 +10001114 if (!pe && phb->pick_m64_pe)
Gavin Shan1e916772016-05-03 15:41:36 +10001115 pe = phb->pick_m64_pe(bus, all);
Guo Chao262af552014-07-21 14:42:30 +10001116
1117 /* The PE number isn't pinned by M64 */
Gavin Shan1e916772016-05-03 15:41:36 +10001118 if (!pe)
1119 pe = pnv_ioda_alloc_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +10001120
Gavin Shan1e916772016-05-03 15:41:36 +10001121 if (!pe) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001122 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1123 __func__, pci_domain_nr(bus), bus->number);
Gavin Shan1e916772016-05-03 15:41:36 +10001124 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001125 }
1126
Guo Chao262af552014-07-21 14:42:30 +10001127 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001128 pe->pbus = bus;
1129 pe->pdev = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001130 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001131 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001132
Gavin Shanfb446ad2012-08-20 03:49:14 +00001133 if (all)
1134 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001135 bus->busn_res.start, bus->busn_res.end, pe->pe_number);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001136 else
1137 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001138 bus->busn_res.start, pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001139
1140 if (pnv_ioda_configure_pe(phb, pe)) {
1141 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001142 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001143 pe->pbus = NULL;
Gavin Shan1e916772016-05-03 15:41:36 +10001144 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001145 }
1146
1147 /* Associate it with all child devices */
1148 pnv_ioda_setup_same_PE(bus, pe);
1149
Gavin Shan7ebdf952012-08-20 03:49:15 +00001150 /* Put PE to the list */
1151 list_add_tail(&pe->list, &phb->ioda.pe_list);
Gavin Shan1e916772016-05-03 15:41:36 +10001152
1153 return pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001154}
1155
Alistair Poppleb5215492016-01-11 16:53:49 +11001156static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev)
Alistair Popple5d2aa712015-12-17 13:43:13 +11001157{
Alistair Poppleb5215492016-01-11 16:53:49 +11001158 int pe_num, found_pe = false, rc;
1159 long rid;
1160 struct pnv_ioda_pe *pe;
1161 struct pci_dev *gpu_pdev;
1162 struct pci_dn *npu_pdn;
1163 struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus);
1164 struct pnv_phb *phb = hose->private_data;
1165
1166 /*
1167 * Due to a hardware errata PE#0 on the NPU is reserved for
1168 * error handling. This means we only have three PEs remaining
1169 * which need to be assigned to four links, implying some
1170 * links must share PEs.
1171 *
1172 * To achieve this we assign PEs such that NPUs linking the
1173 * same GPU get assigned the same PE.
1174 */
1175 gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10001176 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
Alistair Poppleb5215492016-01-11 16:53:49 +11001177 pe = &phb->ioda.pe_array[pe_num];
1178 if (!pe->pdev)
1179 continue;
1180
1181 if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) {
1182 /*
1183 * This device has the same peer GPU so should
1184 * be assigned the same PE as the existing
1185 * peer NPU.
1186 */
1187 dev_info(&npu_pdev->dev,
1188 "Associating to existing PE %d\n", pe_num);
1189 pci_dev_get(npu_pdev);
1190 npu_pdn = pci_get_pdn(npu_pdev);
1191 rid = npu_pdev->bus->number << 8 | npu_pdn->devfn;
1192 npu_pdn->pcidev = npu_pdev;
1193 npu_pdn->pe_number = pe_num;
Alistair Poppleb5215492016-01-11 16:53:49 +11001194 phb->ioda.pe_rmap[rid] = pe->pe_number;
1195
1196 /* Map the PE to this link */
1197 rc = opal_pci_set_pe(phb->opal_id, pe_num, rid,
1198 OpalPciBusAll,
1199 OPAL_COMPARE_RID_DEVICE_NUMBER,
1200 OPAL_COMPARE_RID_FUNCTION_NUMBER,
1201 OPAL_MAP_PE);
1202 WARN_ON(rc != OPAL_SUCCESS);
1203 found_pe = true;
1204 break;
1205 }
1206 }
1207
1208 if (!found_pe)
1209 /*
1210 * Could not find an existing PE so allocate a new
1211 * one.
1212 */
1213 return pnv_ioda_setup_dev_PE(npu_pdev);
1214 else
1215 return pe;
1216}
1217
1218static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus)
1219{
Alistair Popple5d2aa712015-12-17 13:43:13 +11001220 struct pci_dev *pdev;
1221
1222 list_for_each_entry(pdev, &bus->devices, bus_list)
Alistair Poppleb5215492016-01-11 16:53:49 +11001223 pnv_ioda_setup_npu_PE(pdev);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001224}
1225
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001226static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001227{
1228 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001229 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001230
1231 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001232 phb = hose->private_data;
Alistair Popple08f48f32016-01-11 16:53:50 +11001233 if (phb->type == PNV_PHB_NPU) {
1234 /* PE#0 is needed for error reporting */
1235 pnv_ioda_reserve_pe(phb, 0);
Alistair Poppleb5215492016-01-11 16:53:49 +11001236 pnv_ioda_setup_npu_PEs(hose->bus);
Gavin Shanccd1c192016-05-20 16:41:31 +10001237 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001238 }
1239}
1240
Gavin Shana8b2f822015-03-25 16:23:52 +08001241#ifdef CONFIG_PCI_IOV
Wei Yangee8222f2015-10-22 09:22:16 +08001242static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001243{
1244 struct pci_bus *bus;
1245 struct pci_controller *hose;
1246 struct pnv_phb *phb;
1247 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001248 int i, j;
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);
1255
Wei Yangee8222f2015-10-22 09:22:16 +08001256 if (pdn->m64_single_mode)
1257 m64_bars = num_vfs;
1258 else
1259 m64_bars = 1;
1260
Wei Yang02639b02015-03-25 16:23:59 +08001261 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yangee8222f2015-10-22 09:22:16 +08001262 for (j = 0; j < m64_bars; j++) {
1263 if (pdn->m64_map[j][i] == IODA_INVALID_M64)
Wei Yang02639b02015-03-25 16:23:59 +08001264 continue;
1265 opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001266 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0);
1267 clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc);
1268 pdn->m64_map[j][i] = IODA_INVALID_M64;
Wei Yang02639b02015-03-25 16:23:59 +08001269 }
Wei Yang781a8682015-03-25 16:23:57 +08001270
Wei Yangee8222f2015-10-22 09:22:16 +08001271 kfree(pdn->m64_map);
Wei Yang781a8682015-03-25 16:23:57 +08001272 return 0;
1273}
1274
Wei Yang02639b02015-03-25 16:23:59 +08001275static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001276{
1277 struct pci_bus *bus;
1278 struct pci_controller *hose;
1279 struct pnv_phb *phb;
1280 struct pci_dn *pdn;
1281 unsigned int win;
1282 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001283 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001284 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001285 int total_vfs;
1286 resource_size_t size, start;
1287 int pe_num;
Wei Yangee8222f2015-10-22 09:22:16 +08001288 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001289
1290 bus = pdev->bus;
1291 hose = pci_bus_to_host(bus);
1292 phb = hose->private_data;
1293 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001294 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001295
Wei Yangee8222f2015-10-22 09:22:16 +08001296 if (pdn->m64_single_mode)
1297 m64_bars = num_vfs;
1298 else
1299 m64_bars = 1;
Wei Yang02639b02015-03-25 16:23:59 +08001300
Wei Yangee8222f2015-10-22 09:22:16 +08001301 pdn->m64_map = kmalloc(sizeof(*pdn->m64_map) * m64_bars, GFP_KERNEL);
1302 if (!pdn->m64_map)
1303 return -ENOMEM;
1304 /* Initialize the m64_map to IODA_INVALID_M64 */
1305 for (i = 0; i < m64_bars ; i++)
1306 for (j = 0; j < PCI_SRIOV_NUM_BARS; j++)
1307 pdn->m64_map[i][j] = IODA_INVALID_M64;
1308
Wei Yang781a8682015-03-25 16:23:57 +08001309
1310 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1311 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1312 if (!res->flags || !res->parent)
1313 continue;
1314
Wei Yangee8222f2015-10-22 09:22:16 +08001315 for (j = 0; j < m64_bars; j++) {
Wei Yang02639b02015-03-25 16:23:59 +08001316 do {
1317 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1318 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001319
Wei Yang02639b02015-03-25 16:23:59 +08001320 if (win >= phb->ioda.m64_bar_idx + 1)
1321 goto m64_failed;
1322 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001323
Wei Yangee8222f2015-10-22 09:22:16 +08001324 pdn->m64_map[j][i] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001325
Wei Yangee8222f2015-10-22 09:22:16 +08001326 if (pdn->m64_single_mode) {
Wei Yang02639b02015-03-25 16:23:59 +08001327 size = pci_iov_resource_size(pdev,
1328 PCI_IOV_RESOURCES + i);
Wei Yang02639b02015-03-25 16:23:59 +08001329 start = res->start + size * j;
1330 } else {
1331 size = resource_size(res);
1332 start = res->start;
1333 }
1334
1335 /* Map the M64 here */
Wei Yangee8222f2015-10-22 09:22:16 +08001336 if (pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001337 pe_num = pdn->pe_num_map[j];
Wei Yang02639b02015-03-25 16:23:59 +08001338 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1339 pe_num, OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001340 pdn->m64_map[j][i], 0);
Wei Yang02639b02015-03-25 16:23:59 +08001341 }
1342
1343 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001344 OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001345 pdn->m64_map[j][i],
Wei Yang02639b02015-03-25 16:23:59 +08001346 start,
Wei Yang781a8682015-03-25 16:23:57 +08001347 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001348 size);
Wei Yang781a8682015-03-25 16:23:57 +08001349
Wei Yang02639b02015-03-25 16:23:59 +08001350
1351 if (rc != OPAL_SUCCESS) {
1352 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1353 win, rc);
1354 goto m64_failed;
1355 }
1356
Wei Yangee8222f2015-10-22 09:22:16 +08001357 if (pdn->m64_single_mode)
Wei Yang02639b02015-03-25 16:23:59 +08001358 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001359 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2);
Wei Yang02639b02015-03-25 16:23:59 +08001360 else
1361 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001362 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1);
Wei Yang02639b02015-03-25 16:23:59 +08001363
1364 if (rc != OPAL_SUCCESS) {
1365 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1366 win, rc);
1367 goto m64_failed;
1368 }
Wei Yang781a8682015-03-25 16:23:57 +08001369 }
1370 }
1371 return 0;
1372
1373m64_failed:
Wei Yangee8222f2015-10-22 09:22:16 +08001374 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001375 return -EBUSY;
1376}
1377
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001378static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1379 int num);
1380static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1381
Wei Yang781a8682015-03-25 16:23:57 +08001382static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1383{
Wei Yang781a8682015-03-25 16:23:57 +08001384 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001385 int64_t rc;
1386
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001387 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001388 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001389 if (rc)
1390 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1391
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001392 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001393 if (pe->table_group.group) {
1394 iommu_group_put(pe->table_group.group);
1395 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001396 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001397 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001398 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001399}
1400
Wei Yangee8222f2015-10-22 09:22:16 +08001401static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
Wei Yang781a8682015-03-25 16:23:57 +08001402{
1403 struct pci_bus *bus;
1404 struct pci_controller *hose;
1405 struct pnv_phb *phb;
1406 struct pnv_ioda_pe *pe, *pe_n;
1407 struct pci_dn *pdn;
1408
1409 bus = pdev->bus;
1410 hose = pci_bus_to_host(bus);
1411 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001412 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001413
1414 if (!pdev->is_physfn)
1415 return;
1416
Wei Yang781a8682015-03-25 16:23:57 +08001417 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1418 if (pe->parent_dev != pdev)
1419 continue;
1420
1421 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1422
1423 /* Remove from list */
1424 mutex_lock(&phb->ioda.pe_list_mutex);
1425 list_del(&pe->list);
1426 mutex_unlock(&phb->ioda.pe_list_mutex);
1427
1428 pnv_ioda_deconfigure_pe(phb, pe);
1429
Gavin Shan1e916772016-05-03 15:41:36 +10001430 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001431 }
1432}
1433
1434void pnv_pci_sriov_disable(struct pci_dev *pdev)
1435{
1436 struct pci_bus *bus;
1437 struct pci_controller *hose;
1438 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001439 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001440 struct pci_dn *pdn;
1441 struct pci_sriov *iov;
Wei Yangbe283ee2015-10-22 09:22:19 +08001442 u16 num_vfs, i;
Wei Yang781a8682015-03-25 16:23:57 +08001443
1444 bus = pdev->bus;
1445 hose = pci_bus_to_host(bus);
1446 phb = hose->private_data;
1447 pdn = pci_get_pdn(pdev);
1448 iov = pdev->sriov;
1449 num_vfs = pdn->num_vfs;
1450
1451 /* Release VF PEs */
Wei Yangee8222f2015-10-22 09:22:16 +08001452 pnv_ioda_release_vf_PE(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001453
1454 if (phb->type == PNV_PHB_IODA2) {
Wei Yangee8222f2015-10-22 09:22:16 +08001455 if (!pdn->m64_single_mode)
Wei Yangbe283ee2015-10-22 09:22:19 +08001456 pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001457
1458 /* Release M64 windows */
Wei Yangee8222f2015-10-22 09:22:16 +08001459 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001460
1461 /* Release PE numbers */
Wei Yangbe283ee2015-10-22 09:22:19 +08001462 if (pdn->m64_single_mode) {
1463 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001464 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1465 continue;
1466
1467 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1468 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001469 }
1470 } else
1471 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1472 /* Releasing pe_num_map */
1473 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001474 }
1475}
1476
1477static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1478 struct pnv_ioda_pe *pe);
1479static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1480{
1481 struct pci_bus *bus;
1482 struct pci_controller *hose;
1483 struct pnv_phb *phb;
1484 struct pnv_ioda_pe *pe;
1485 int pe_num;
1486 u16 vf_index;
1487 struct pci_dn *pdn;
1488
1489 bus = pdev->bus;
1490 hose = pci_bus_to_host(bus);
1491 phb = hose->private_data;
1492 pdn = pci_get_pdn(pdev);
1493
1494 if (!pdev->is_physfn)
1495 return;
1496
1497 /* Reserve PE for each VF */
1498 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001499 if (pdn->m64_single_mode)
1500 pe_num = pdn->pe_num_map[vf_index];
1501 else
1502 pe_num = *pdn->pe_num_map + vf_index;
Wei Yang781a8682015-03-25 16:23:57 +08001503
1504 pe = &phb->ioda.pe_array[pe_num];
1505 pe->pe_number = pe_num;
1506 pe->phb = phb;
1507 pe->flags = PNV_IODA_PE_VF;
1508 pe->pbus = NULL;
1509 pe->parent_dev = pdev;
Wei Yang781a8682015-03-25 16:23:57 +08001510 pe->mve_number = -1;
1511 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1512 pci_iov_virtfn_devfn(pdev, vf_index);
1513
1514 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1515 hose->global_number, pdev->bus->number,
1516 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1517 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1518
1519 if (pnv_ioda_configure_pe(phb, pe)) {
1520 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001521 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001522 pe->pdev = NULL;
1523 continue;
1524 }
1525
Wei Yang781a8682015-03-25 16:23:57 +08001526 /* Put PE to the list */
1527 mutex_lock(&phb->ioda.pe_list_mutex);
1528 list_add_tail(&pe->list, &phb->ioda.pe_list);
1529 mutex_unlock(&phb->ioda.pe_list_mutex);
1530
1531 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1532 }
1533}
1534
1535int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1536{
1537 struct pci_bus *bus;
1538 struct pci_controller *hose;
1539 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001540 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001541 struct pci_dn *pdn;
1542 int ret;
Wei Yangbe283ee2015-10-22 09:22:19 +08001543 u16 i;
Wei Yang781a8682015-03-25 16:23:57 +08001544
1545 bus = pdev->bus;
1546 hose = pci_bus_to_host(bus);
1547 phb = hose->private_data;
1548 pdn = pci_get_pdn(pdev);
1549
1550 if (phb->type == PNV_PHB_IODA2) {
Wei Yangb0331852015-10-22 09:22:14 +08001551 if (!pdn->vfs_expanded) {
1552 dev_info(&pdev->dev, "don't support this SRIOV device"
1553 " with non 64bit-prefetchable IOV BAR\n");
1554 return -ENOSPC;
1555 }
1556
Wei Yangee8222f2015-10-22 09:22:16 +08001557 /*
1558 * When M64 BARs functions in Single PE mode, the number of VFs
1559 * could be enabled must be less than the number of M64 BARs.
1560 */
1561 if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) {
1562 dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n");
1563 return -EBUSY;
1564 }
1565
Wei Yangbe283ee2015-10-22 09:22:19 +08001566 /* Allocating pe_num_map */
1567 if (pdn->m64_single_mode)
1568 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map) * num_vfs,
1569 GFP_KERNEL);
1570 else
1571 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL);
1572
1573 if (!pdn->pe_num_map)
1574 return -ENOMEM;
1575
1576 if (pdn->m64_single_mode)
1577 for (i = 0; i < num_vfs; i++)
1578 pdn->pe_num_map[i] = IODA_INVALID_PE;
1579
Wei Yang781a8682015-03-25 16:23:57 +08001580 /* Calculate available PE for required VFs */
Wei Yangbe283ee2015-10-22 09:22:19 +08001581 if (pdn->m64_single_mode) {
1582 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001583 pe = pnv_ioda_alloc_pe(phb);
1584 if (!pe) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001585 ret = -EBUSY;
1586 goto m64_failed;
1587 }
Gavin Shan1e916772016-05-03 15:41:36 +10001588
1589 pdn->pe_num_map[i] = pe->pe_number;
Wei Yangbe283ee2015-10-22 09:22:19 +08001590 }
1591 } else {
1592 mutex_lock(&phb->ioda.pe_alloc_mutex);
1593 *pdn->pe_num_map = bitmap_find_next_zero_area(
Gavin Shan92b8f132016-05-03 15:41:24 +10001594 phb->ioda.pe_alloc, phb->ioda.total_pe_num,
Wei Yangbe283ee2015-10-22 09:22:19 +08001595 0, num_vfs, 0);
Gavin Shan92b8f132016-05-03 15:41:24 +10001596 if (*pdn->pe_num_map >= phb->ioda.total_pe_num) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001597 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1598 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1599 kfree(pdn->pe_num_map);
1600 return -EBUSY;
1601 }
1602 bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001603 mutex_unlock(&phb->ioda.pe_alloc_mutex);
Wei Yang781a8682015-03-25 16:23:57 +08001604 }
Wei Yang781a8682015-03-25 16:23:57 +08001605 pdn->num_vfs = num_vfs;
Wei Yang781a8682015-03-25 16:23:57 +08001606
1607 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001608 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001609 if (ret) {
1610 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1611 goto m64_failed;
1612 }
1613
1614 /*
1615 * When using one M64 BAR to map one IOV BAR, we need to shift
1616 * the IOV BAR according to the PE# allocated to the VFs.
1617 * Otherwise, the PE# for the VF will conflict with others.
1618 */
Wei Yangee8222f2015-10-22 09:22:16 +08001619 if (!pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001620 ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map);
Wei Yang02639b02015-03-25 16:23:59 +08001621 if (ret)
1622 goto m64_failed;
1623 }
Wei Yang781a8682015-03-25 16:23:57 +08001624 }
1625
1626 /* Setup VF PEs */
1627 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1628
1629 return 0;
1630
1631m64_failed:
Wei Yangbe283ee2015-10-22 09:22:19 +08001632 if (pdn->m64_single_mode) {
1633 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001634 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1635 continue;
1636
1637 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1638 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001639 }
1640 } else
1641 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1642
1643 /* Releasing pe_num_map */
1644 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001645
1646 return ret;
1647}
1648
Gavin Shana8b2f822015-03-25 16:23:52 +08001649int pcibios_sriov_disable(struct pci_dev *pdev)
1650{
Wei Yang781a8682015-03-25 16:23:57 +08001651 pnv_pci_sriov_disable(pdev);
1652
Gavin Shana8b2f822015-03-25 16:23:52 +08001653 /* Release PCI data */
1654 remove_dev_pci_data(pdev);
1655 return 0;
1656}
1657
1658int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1659{
1660 /* Allocate PCI data */
1661 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001662
Wei Yangee8222f2015-10-22 09:22:16 +08001663 return pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001664}
1665#endif /* CONFIG_PCI_IOV */
1666
Gavin Shan959c9bd2013-04-25 19:21:02 +00001667static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001668{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001669 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001670 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001671
Gavin Shan959c9bd2013-04-25 19:21:02 +00001672 /*
1673 * The function can be called while the PE#
1674 * hasn't been assigned. Do nothing for the
1675 * case.
1676 */
1677 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1678 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001679
Gavin Shan959c9bd2013-04-25 19:21:02 +00001680 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001681 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiy0e1ffef2015-08-27 16:01:16 +10001682 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001683 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001684 /*
1685 * Note: iommu_add_device() will fail here as
1686 * for physical PE: the device is already added by now;
1687 * for virtual PE: sysfs entries are not ready yet and
1688 * tce_iommu_bus_notifier will add the device to a group later.
1689 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001690}
1691
Daniel Axtens763d2d82015-04-28 15:12:07 +10001692static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001693{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001694 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1695 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001696 struct pci_dn *pdn = pci_get_pdn(pdev);
1697 struct pnv_ioda_pe *pe;
1698 uint64_t top;
1699 bool bypass = false;
1700
1701 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1702 return -ENODEV;;
1703
1704 pe = &phb->ioda.pe_array[pdn->pe_number];
1705 if (pe->tce_bypass_enabled) {
1706 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1707 bypass = (dma_mask >= top);
1708 }
1709
1710 if (bypass) {
1711 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1712 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001713 } else {
1714 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1715 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001716 }
Brian W Harta32305b2014-07-31 14:24:37 -05001717 *pdev->dev.dma_mask = dma_mask;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001718
1719 /* Update peer npu devices */
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10001720 pnv_npu_try_dma_set_bypass(pdev, bypass);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001721
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001722 return 0;
1723}
1724
Andrew Donnellan535229822015-08-07 13:45:54 +10001725static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001726{
Andrew Donnellan535229822015-08-07 13:45:54 +10001727 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1728 struct pnv_phb *phb = hose->private_data;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001729 struct pci_dn *pdn = pci_get_pdn(pdev);
1730 struct pnv_ioda_pe *pe;
1731 u64 end, mask;
1732
1733 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1734 return 0;
1735
1736 pe = &phb->ioda.pe_array[pdn->pe_number];
1737 if (!pe->tce_bypass_enabled)
1738 return __dma_get_required_mask(&pdev->dev);
1739
1740
1741 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1742 mask = 1ULL << (fls64(end) - 1);
1743 mask += mask - 1;
1744
1745 return mask;
1746}
1747
Gavin Shandff4a392014-07-15 17:00:55 +10001748static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001749 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001750{
1751 struct pci_dev *dev;
1752
1753 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001754 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001755 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001756 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001757
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001758 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001759 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001760 }
1761}
1762
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001763static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb,
1764 bool real_mode)
1765{
1766 return real_mode ? (__be64 __iomem *)(phb->regs_phys + 0x210) :
1767 (phb->regs + 0x210);
1768}
1769
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001770static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table *tbl,
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001771 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001772{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001773 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1774 &tbl->it_group_list, struct iommu_table_group_link,
1775 next);
1776 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001777 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001778 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00001779 unsigned long start, end, inc;
1780
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001781 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1782 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1783 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001784
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001785 /* p7ioc-style invalidation, 2 TCEs per write */
1786 start |= (1ull << 63);
1787 end |= (1ull << 63);
1788 inc = 16;
Gavin Shan4cce9552013-04-25 19:21:00 +00001789 end |= inc - 1; /* round up end to be different than start */
1790
1791 mb(); /* Ensure above stores are visible */
1792 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001793 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001794 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001795 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001796 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001797 start += inc;
1798 }
1799
1800 /*
1801 * The iommu layer will do another mb() for us on build()
1802 * and we don't care on free()
1803 */
1804}
1805
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001806static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1807 long npages, unsigned long uaddr,
1808 enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001809 unsigned long attrs)
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001810{
1811 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1812 attrs);
1813
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001814 if (!ret)
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001815 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001816
1817 return ret;
1818}
1819
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001820#ifdef CONFIG_IOMMU_API
1821static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1822 unsigned long *hpa, enum dma_data_direction *direction)
1823{
1824 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1825
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001826 if (!ret)
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001827 pnv_pci_p7ioc_tce_invalidate(tbl, index, 1, false);
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001828
1829 return ret;
1830}
1831#endif
1832
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001833static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1834 long npages)
1835{
1836 pnv_tce_free(tbl, index, npages);
1837
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001838 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001839}
1840
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001841static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001842 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001843#ifdef CONFIG_IOMMU_API
1844 .exchange = pnv_ioda1_tce_xchg,
1845#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001846 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001847 .get = pnv_tce_get,
1848};
1849
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001850#define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)
1851#define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)
1852#define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)
Alexey Kardashevskiybef92532016-04-29 18:55:17 +10001853
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001854void pnv_pci_phb3_tce_invalidate_entire(struct pnv_phb *phb, bool rm)
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001855{
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001856 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(phb, rm);
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001857 const unsigned long val = PHB3_TCE_KILL_INVAL_ALL;
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001858
1859 mb(); /* Ensure previous TCE table stores are visible */
1860 if (rm)
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001861 __raw_rm_writeq(cpu_to_be64(val), invalidate);
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001862 else
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001863 __raw_writeq(cpu_to_be64(val), invalidate);
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001864}
1865
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001866static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001867{
1868 /* 01xb - invalidate TCEs that match the specified PE# */
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001869 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, false);
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001870 unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001871
1872 mb(); /* Ensure above stores are visible */
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001873 __raw_writeq(cpu_to_be64(val), invalidate);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001874}
1875
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001876static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe, bool rm,
1877 unsigned shift, unsigned long index,
1878 unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001879{
Alexey Kardashevskiy4d902192016-08-03 18:40:45 +10001880 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00001881 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001882
1883 /* We'll invalidate DMA address in PE scope */
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001884 start = PHB3_TCE_KILL_INVAL_ONE;
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001885 start |= (pe->pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001886 end = start;
1887
1888 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001889 start |= (index << shift);
1890 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001891 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001892 mb();
1893
1894 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001895 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001896 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001897 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001898 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001899 start += inc;
1900 }
1901}
1902
Benjamin Herrenschmidtf0228c42016-07-08 16:37:15 +10001903static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
1904{
1905 struct pnv_phb *phb = pe->phb;
1906
1907 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1908 pnv_pci_phb3_tce_invalidate_pe(pe);
1909 else
1910 opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE,
1911 pe->pe_number, 0, 0, 0);
1912}
1913
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001914static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1915 unsigned long index, unsigned long npages, bool rm)
1916{
1917 struct iommu_table_group_link *tgl;
1918
1919 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
1920 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1921 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidtf0228c42016-07-08 16:37:15 +10001922 struct pnv_phb *phb = pe->phb;
1923 unsigned int shift = tbl->it_page_shift;
1924
1925 if (phb->type == PNV_PHB_NPU) {
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001926 /*
1927 * The NVLink hardware does not support TCE kill
1928 * per TCE entry so we have to invalidate
1929 * the entire cache for it.
1930 */
Benjamin Herrenschmidtf0228c42016-07-08 16:37:15 +10001931 pnv_pci_phb3_tce_invalidate_entire(phb, rm);
Alexey Kardashevskiy85674862016-04-29 18:55:23 +10001932 continue;
1933 }
Benjamin Herrenschmidtf0228c42016-07-08 16:37:15 +10001934 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1935 pnv_pci_phb3_tce_invalidate(pe, rm, shift,
1936 index, npages);
1937 else if (rm)
1938 opal_rm_pci_tce_kill(phb->opal_id,
1939 OPAL_PCI_TCE_KILL_PAGES,
1940 pe->pe_number, 1u << shift,
1941 index << shift, npages);
1942 else
1943 opal_pci_tce_kill(phb->opal_id,
1944 OPAL_PCI_TCE_KILL_PAGES,
1945 pe->pe_number, 1u << shift,
1946 index << shift, npages);
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001947 }
1948}
1949
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001950static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1951 long npages, unsigned long uaddr,
1952 enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001953 unsigned long attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001954{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001955 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1956 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001957
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001958 if (!ret)
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001959 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1960
1961 return ret;
1962}
1963
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001964#ifdef CONFIG_IOMMU_API
1965static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1966 unsigned long *hpa, enum dma_data_direction *direction)
1967{
1968 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1969
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001970 if (!ret)
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001971 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1972
1973 return ret;
1974}
1975#endif
1976
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001977static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1978 long npages)
1979{
1980 pnv_tce_free(tbl, index, npages);
1981
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001982 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001983}
1984
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001985static void pnv_ioda2_table_free(struct iommu_table *tbl)
1986{
1987 pnv_pci_ioda2_table_free_pages(tbl);
1988 iommu_free_table(tbl, "pnv");
1989}
1990
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001991static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001992 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001993#ifdef CONFIG_IOMMU_API
1994 .exchange = pnv_ioda2_tce_xchg,
1995#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001996 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001997 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001998 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001999};
2000
Gavin Shan801846d2016-05-03 15:41:34 +10002001static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data)
2002{
2003 unsigned int *weight = (unsigned int *)data;
2004
2005 /* This is quite simplistic. The "base" weight of a device
2006 * is 10. 0 means no DMA is to be accounted for it.
2007 */
2008 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
2009 return 0;
2010
2011 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
2012 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
2013 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
2014 *weight += 3;
2015 else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
2016 *weight += 15;
2017 else
2018 *weight += 10;
2019
2020 return 0;
2021}
2022
2023static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe)
2024{
2025 unsigned int weight = 0;
2026
2027 /* SRIOV VF has same DMA32 weight as its PF */
2028#ifdef CONFIG_PCI_IOV
2029 if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) {
2030 pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight);
2031 return weight;
2032 }
2033#endif
2034
2035 if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) {
2036 pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight);
2037 } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) {
2038 struct pci_dev *pdev;
2039
2040 list_for_each_entry(pdev, &pe->pbus->devices, bus_list)
2041 pnv_pci_ioda_dev_dma_weight(pdev, &weight);
2042 } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) {
2043 pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight);
2044 }
2045
2046 return weight;
2047}
2048
Gavin Shanb30d9362016-05-03 15:41:32 +10002049static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
Gavin Shan2b923ed2016-05-05 12:04:16 +10002050 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002051{
2052
2053 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002054 struct iommu_table *tbl;
Gavin Shan2b923ed2016-05-05 12:04:16 +10002055 unsigned int weight, total_weight = 0;
2056 unsigned int tce32_segsz, base, segs, avail, i;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002057 int64_t rc;
2058 void *addr;
2059
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002060 /* XXX FIXME: Handle 64-bit only DMA devices */
2061 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
2062 /* XXX FIXME: Allocate multi-level tables on PHB3 */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002063 weight = pnv_pci_ioda_pe_dma_weight(pe);
2064 if (!weight)
2065 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002066
Gavin Shan2b923ed2016-05-05 12:04:16 +10002067 pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight,
2068 &total_weight);
2069 segs = (weight * phb->ioda.dma32_count) / total_weight;
2070 if (!segs)
2071 segs = 1;
2072
2073 /*
2074 * Allocate contiguous DMA32 segments. We begin with the expected
2075 * number of segments. With one more attempt, the number of DMA32
2076 * segments to be allocated is decreased by one until one segment
2077 * is allocated successfully.
2078 */
2079 do {
2080 for (base = 0; base <= phb->ioda.dma32_count - segs; base++) {
2081 for (avail = 0, i = base; i < base + segs; i++) {
2082 if (phb->ioda.dma32_segmap[i] ==
2083 IODA_INVALID_PE)
2084 avail++;
2085 }
2086
2087 if (avail == segs)
2088 goto found;
2089 }
2090 } while (--segs);
2091
2092 if (!segs) {
2093 pe_warn(pe, "No available DMA32 segments\n");
2094 return;
2095 }
2096
2097found:
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002098 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002099 iommu_register_group(&pe->table_group, phb->hose->global_number,
2100 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002101 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002102
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002103 /* Grab a 32-bit TCE table */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002104 pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
2105 weight, total_weight, base, segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002106 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
Gavin Shanacce9712016-05-03 15:41:33 +10002107 base * PNV_IODA1_DMA32_SEGSIZE,
2108 (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002109
2110 /* XXX Currently, we allocate one big contiguous table for the
2111 * TCEs. We only really need one chunk per 256M of TCE space
2112 * (ie per segment) but that's an optimization for later, it
2113 * requires some added smarts with our get/put_tce implementation
Gavin Shanacce9712016-05-03 15:41:33 +10002114 *
2115 * Each TCE page is 4KB in size and each TCE entry occupies 8
2116 * bytes
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002117 */
Gavin Shanacce9712016-05-03 15:41:33 +10002118 tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002119 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
Gavin Shanacce9712016-05-03 15:41:33 +10002120 get_order(tce32_segsz * segs));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002121 if (!tce_mem) {
2122 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
2123 goto fail;
2124 }
2125 addr = page_address(tce_mem);
Gavin Shanacce9712016-05-03 15:41:33 +10002126 memset(addr, 0, tce32_segsz * segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002127
2128 /* Configure HW */
2129 for (i = 0; i < segs; i++) {
2130 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2131 pe->pe_number,
2132 base + i, 1,
Gavin Shanacce9712016-05-03 15:41:33 +10002133 __pa(addr) + tce32_segsz * i,
2134 tce32_segsz, IOMMU_PAGE_SIZE_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002135 if (rc) {
2136 pe_err(pe, " Failed to configure 32-bit TCE table,"
2137 " err %ld\n", rc);
2138 goto fail;
2139 }
2140 }
2141
Gavin Shan2b923ed2016-05-05 12:04:16 +10002142 /* Setup DMA32 segment mapping */
2143 for (i = base; i < base + segs; i++)
2144 phb->ioda.dma32_segmap[i] = pe->pe_number;
2145
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002146 /* Setup linux iommu table */
Gavin Shanacce9712016-05-03 15:41:33 +10002147 pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs,
2148 base * PNV_IODA1_DMA32_SEGSIZE,
2149 IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002150
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002151 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002152 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
2153 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002154 iommu_init_table(tbl, phb->hose->node);
2155
Wei Yang781a8682015-03-25 16:23:57 +08002156 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002157 /*
2158 * Setting table base here only for carrying iommu_group
2159 * further down to let iommu_add_device() do the job.
2160 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2161 */
2162 set_iommu_table_base(&pe->pdev->dev, tbl);
2163 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002164 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002165 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10002166
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002167 return;
2168 fail:
2169 /* XXX Failure: Try to fallback to 64-bit only ? */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002170 if (tce_mem)
Gavin Shanacce9712016-05-03 15:41:33 +10002171 __free_pages(tce_mem, get_order(tce32_segsz * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002172 if (tbl) {
2173 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
2174 iommu_free_table(tbl, "pnv");
2175 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002176}
2177
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002178static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
2179 int num, struct iommu_table *tbl)
2180{
2181 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2182 table_group);
2183 struct pnv_phb *phb = pe->phb;
2184 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002185 const unsigned long size = tbl->it_indirect_levels ?
2186 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002187 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
2188 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
2189
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002190 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002191 start_addr, start_addr + win_size - 1,
2192 IOMMU_PAGE_SIZE(tbl));
2193
2194 /*
2195 * Map TCE table through TVT. The TVE index is the PE number
2196 * shifted by 1 bit for 32-bits DMA space.
2197 */
2198 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2199 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002200 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002201 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002202 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002203 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002204 IOMMU_PAGE_SIZE(tbl));
2205 if (rc) {
2206 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
2207 return rc;
2208 }
2209
2210 pnv_pci_link_table_and_group(phb->hose->node, num,
2211 tbl, &pe->table_group);
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002212 pnv_pci_phb3_tce_invalidate_pe(pe);
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002213
2214 return 0;
2215}
2216
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002217static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002218{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002219 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2220 int64_t rc;
2221
2222 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2223 if (enable) {
2224 phys_addr_t top = memblock_end_of_DRAM();
2225
2226 top = roundup_pow_of_two(top);
2227 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2228 pe->pe_number,
2229 window_id,
2230 pe->tce_bypass_base,
2231 top);
2232 } else {
2233 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2234 pe->pe_number,
2235 window_id,
2236 pe->tce_bypass_base,
2237 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002238 }
2239 if (rc)
2240 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2241 else
2242 pe->tce_bypass_enabled = enable;
2243}
2244
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002245static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2246 __u32 page_shift, __u64 window_size, __u32 levels,
2247 struct iommu_table *tbl);
2248
2249static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2250 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2251 struct iommu_table **ptbl)
2252{
2253 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2254 table_group);
2255 int nid = pe->phb->hose->node;
2256 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2257 long ret;
2258 struct iommu_table *tbl;
2259
2260 tbl = pnv_pci_table_alloc(nid);
2261 if (!tbl)
2262 return -ENOMEM;
2263
2264 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2265 bus_offset, page_shift, window_size,
2266 levels, tbl);
2267 if (ret) {
2268 iommu_free_table(tbl, "pnv");
2269 return ret;
2270 }
2271
2272 tbl->it_ops = &pnv_ioda2_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002273
2274 *ptbl = tbl;
2275
2276 return 0;
2277}
2278
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002279static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2280{
2281 struct iommu_table *tbl = NULL;
2282 long rc;
2283
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002284 /*
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002285 * crashkernel= specifies the kdump kernel's maximum memory at
2286 * some offset and there is no guaranteed the result is a power
2287 * of 2, which will cause errors later.
2288 */
2289 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
2290
2291 /*
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002292 * In memory constrained environments, e.g. kdump kernel, the
2293 * DMA window can be larger than available memory, which will
2294 * cause errors later.
2295 */
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002296 const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002297
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002298 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2299 IOMMU_PAGE_SHIFT_4K,
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002300 window_size,
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002301 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2302 if (rc) {
2303 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2304 rc);
2305 return rc;
2306 }
2307
2308 iommu_init_table(tbl, pe->phb->hose->node);
2309
2310 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2311 if (rc) {
2312 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2313 rc);
2314 pnv_ioda2_table_free(tbl);
2315 return rc;
2316 }
2317
2318 if (!pnv_iommu_bypass_disabled)
2319 pnv_pci_ioda2_set_bypass(pe, true);
2320
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002321 /*
2322 * Setting table base here only for carrying iommu_group
2323 * further down to let iommu_add_device() do the job.
2324 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2325 */
2326 if (pe->flags & PNV_IODA_PE_DEV)
2327 set_iommu_table_base(&pe->pdev->dev, tbl);
2328
2329 return 0;
2330}
2331
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002332#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2333static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2334 int num)
2335{
2336 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2337 table_group);
2338 struct pnv_phb *phb = pe->phb;
2339 long ret;
2340
2341 pe_info(pe, "Removing DMA window #%d\n", num);
2342
2343 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2344 (pe->pe_number << 1) + num,
2345 0/* levels */, 0/* table address */,
2346 0/* table size */, 0/* page size */);
2347 if (ret)
2348 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2349 else
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002350 pnv_pci_phb3_tce_invalidate_pe(pe);
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002351
2352 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2353
2354 return ret;
2355}
2356#endif
2357
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002358#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002359static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2360 __u64 window_size, __u32 levels)
2361{
2362 unsigned long bytes = 0;
2363 const unsigned window_shift = ilog2(window_size);
2364 unsigned entries_shift = window_shift - page_shift;
2365 unsigned table_shift = entries_shift + 3;
2366 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2367 unsigned long direct_table_size;
2368
2369 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2370 (window_size > memory_hotplug_max()) ||
2371 !is_power_of_2(window_size))
2372 return 0;
2373
2374 /* Calculate a direct table size from window_size and levels */
2375 entries_shift = (entries_shift + levels - 1) / levels;
2376 table_shift = entries_shift + 3;
2377 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2378 direct_table_size = 1UL << table_shift;
2379
2380 for ( ; levels; --levels) {
2381 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2382
2383 tce_table_size /= direct_table_size;
2384 tce_table_size <<= 3;
2385 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2386 }
2387
2388 return bytes;
2389}
2390
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002391static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002392{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002393 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2394 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002395 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2396 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002397
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002398 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002399 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2400 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002401}
2402
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002403static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2404{
2405 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2406 table_group);
2407
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002408 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002409}
2410
2411static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002412 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002413 .create_table = pnv_pci_ioda2_create_table,
2414 .set_window = pnv_pci_ioda2_set_window,
2415 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002416 .take_ownership = pnv_ioda2_take_ownership,
2417 .release_ownership = pnv_ioda2_release_ownership,
2418};
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002419
2420static int gpe_table_group_to_npe_cb(struct device *dev, void *opaque)
2421{
2422 struct pci_controller *hose;
2423 struct pnv_phb *phb;
2424 struct pnv_ioda_pe **ptmppe = opaque;
2425 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
2426 struct pci_dn *pdn = pci_get_pdn(pdev);
2427
2428 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2429 return 0;
2430
2431 hose = pci_bus_to_host(pdev->bus);
2432 phb = hose->private_data;
2433 if (phb->type != PNV_PHB_NPU)
2434 return 0;
2435
2436 *ptmppe = &phb->ioda.pe_array[pdn->pe_number];
2437
2438 return 1;
2439}
2440
2441/*
2442 * This returns PE of associated NPU.
2443 * This assumes that NPU is in the same IOMMU group with GPU and there is
2444 * no other PEs.
2445 */
2446static struct pnv_ioda_pe *gpe_table_group_to_npe(
2447 struct iommu_table_group *table_group)
2448{
2449 struct pnv_ioda_pe *npe = NULL;
2450 int ret = iommu_group_for_each_dev(table_group->group, &npe,
2451 gpe_table_group_to_npe_cb);
2452
2453 BUG_ON(!ret || !npe);
2454
2455 return npe;
2456}
2457
2458static long pnv_pci_ioda2_npu_set_window(struct iommu_table_group *table_group,
2459 int num, struct iommu_table *tbl)
2460{
2461 long ret = pnv_pci_ioda2_set_window(table_group, num, tbl);
2462
2463 if (ret)
2464 return ret;
2465
2466 ret = pnv_npu_set_window(gpe_table_group_to_npe(table_group), num, tbl);
2467 if (ret)
2468 pnv_pci_ioda2_unset_window(table_group, num);
2469
2470 return ret;
2471}
2472
2473static long pnv_pci_ioda2_npu_unset_window(
2474 struct iommu_table_group *table_group,
2475 int num)
2476{
2477 long ret = pnv_pci_ioda2_unset_window(table_group, num);
2478
2479 if (ret)
2480 return ret;
2481
2482 return pnv_npu_unset_window(gpe_table_group_to_npe(table_group), num);
2483}
2484
2485static void pnv_ioda2_npu_take_ownership(struct iommu_table_group *table_group)
2486{
2487 /*
2488 * Detach NPU first as pnv_ioda2_take_ownership() will destroy
2489 * the iommu_table if 32bit DMA is enabled.
2490 */
2491 pnv_npu_take_ownership(gpe_table_group_to_npe(table_group));
2492 pnv_ioda2_take_ownership(table_group);
2493}
2494
2495static struct iommu_table_group_ops pnv_pci_ioda2_npu_ops = {
2496 .get_table_size = pnv_pci_ioda2_get_table_size,
2497 .create_table = pnv_pci_ioda2_create_table,
2498 .set_window = pnv_pci_ioda2_npu_set_window,
2499 .unset_window = pnv_pci_ioda2_npu_unset_window,
2500 .take_ownership = pnv_ioda2_npu_take_ownership,
2501 .release_ownership = pnv_ioda2_release_ownership,
2502};
2503
2504static void pnv_pci_ioda_setup_iommu_api(void)
2505{
2506 struct pci_controller *hose, *tmp;
2507 struct pnv_phb *phb;
2508 struct pnv_ioda_pe *pe, *gpe;
2509
2510 /*
2511 * Now we have all PHBs discovered, time to add NPU devices to
2512 * the corresponding IOMMU groups.
2513 */
2514 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2515 phb = hose->private_data;
2516
2517 if (phb->type != PNV_PHB_NPU)
2518 continue;
2519
2520 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2521 gpe = pnv_pci_npu_setup_iommu(pe);
2522 if (gpe)
2523 gpe->table_group.ops = &pnv_pci_ioda2_npu_ops;
2524 }
2525 }
2526}
2527#else /* !CONFIG_IOMMU_API */
2528static void pnv_pci_ioda_setup_iommu_api(void) { };
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002529#endif
2530
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002531static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2532 unsigned levels, unsigned long limit,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002533 unsigned long *current_offset, unsigned long *total_allocated)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002534{
2535 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002536 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002537 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002538 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2539 unsigned entries = 1UL << (shift - 3);
2540 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002541
2542 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2543 if (!tce_mem) {
2544 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2545 return NULL;
2546 }
2547 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002548 memset(addr, 0, allocated);
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002549 *total_allocated += allocated;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002550
2551 --levels;
2552 if (!levels) {
2553 *current_offset += allocated;
2554 return addr;
2555 }
2556
2557 for (i = 0; i < entries; ++i) {
2558 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002559 levels, limit, current_offset, total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002560 if (!tmp)
2561 break;
2562
2563 addr[i] = cpu_to_be64(__pa(tmp) |
2564 TCE_PCI_READ | TCE_PCI_WRITE);
2565
2566 if (*current_offset >= limit)
2567 break;
2568 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002569
2570 return addr;
2571}
2572
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002573static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2574 unsigned long size, unsigned level);
2575
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002576static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002577 __u32 page_shift, __u64 window_size, __u32 levels,
2578 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002579{
2580 void *addr;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002581 unsigned long offset = 0, level_shift, total_allocated = 0;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002582 const unsigned window_shift = ilog2(window_size);
2583 unsigned entries_shift = window_shift - page_shift;
2584 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2585 const unsigned long tce_table_size = 1UL << table_shift;
2586
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002587 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2588 return -EINVAL;
2589
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002590 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2591 return -EINVAL;
2592
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002593 /* Adjust direct table size from window_size and levels */
2594 entries_shift = (entries_shift + levels - 1) / levels;
2595 level_shift = entries_shift + 3;
2596 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2597
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002598 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002599 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002600 levels, tce_table_size, &offset, &total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002601
2602 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002603 if (!addr)
2604 return -ENOMEM;
2605
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002606 /*
2607 * First level was allocated but some lower level failed as
2608 * we did not allocate as much as we wanted,
2609 * release partially allocated table.
2610 */
2611 if (offset < tce_table_size) {
2612 pnv_pci_ioda2_table_do_free_pages(addr,
2613 1ULL << (level_shift - 3), levels - 1);
2614 return -ENOMEM;
2615 }
2616
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002617 /* Setup linux iommu table */
2618 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2619 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002620 tbl->it_level_size = 1ULL << (level_shift - 3);
2621 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002622 tbl->it_allocated_size = total_allocated;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002623
2624 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2625 window_size, tce_table_size, bus_offset);
2626
2627 return 0;
2628}
2629
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002630static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2631 unsigned long size, unsigned level)
2632{
2633 const unsigned long addr_ul = (unsigned long) addr &
2634 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2635
2636 if (level) {
2637 long i;
2638 u64 *tmp = (u64 *) addr_ul;
2639
2640 for (i = 0; i < size; ++i) {
2641 unsigned long hpa = be64_to_cpu(tmp[i]);
2642
2643 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2644 continue;
2645
2646 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2647 level - 1);
2648 }
2649 }
2650
2651 free_pages(addr_ul, get_order(size << 3));
2652}
2653
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002654static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2655{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002656 const unsigned long size = tbl->it_indirect_levels ?
2657 tbl->it_level_size : tbl->it_size;
2658
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002659 if (!tbl->it_size)
2660 return;
2661
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002662 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2663 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002664}
2665
Gavin Shan373f5652013-04-25 19:21:01 +00002666static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2667 struct pnv_ioda_pe *pe)
2668{
Gavin Shan373f5652013-04-25 19:21:01 +00002669 int64_t rc;
2670
Gavin Shanccd1c192016-05-20 16:41:31 +10002671 if (!pnv_pci_ioda_pe_dma_weight(pe))
2672 return;
2673
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002674 /* TVE #1 is selected by PCI address bit 59 */
2675 pe->tce_bypass_base = 1ull << 59;
2676
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002677 iommu_register_group(&pe->table_group, phb->hose->global_number,
2678 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002679
Gavin Shan373f5652013-04-25 19:21:01 +00002680 /* The PE will reserve all possible 32-bits space */
Gavin Shan373f5652013-04-25 19:21:01 +00002681 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002682 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002683
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002684 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002685 pe->table_group.tce32_start = 0;
2686 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2687 pe->table_group.max_dynamic_windows_supported =
2688 IOMMU_TABLE_GROUP_MAX_TABLES;
2689 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2690 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002691#ifdef CONFIG_IOMMU_API
2692 pe->table_group.ops = &pnv_pci_ioda2_ops;
2693#endif
2694
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002695 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan801846d2016-05-03 15:41:34 +10002696 if (rc)
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002697 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002698
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002699 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002700 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002701 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002702 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002703}
2704
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002705#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002706static void pnv_ioda2_msi_eoi(struct irq_data *d)
2707{
2708 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2709 struct irq_chip *chip = irq_data_get_irq_chip(d);
2710 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2711 ioda.irq_chip);
2712 int64_t rc;
2713
2714 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2715 WARN_ON_ONCE(rc);
2716
2717 icp_native_eoi(d);
2718}
2719
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002720
Ian Munsief4568342016-07-14 07:17:00 +10002721void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002722{
2723 struct irq_data *idata;
2724 struct irq_chip *ichip;
2725
Benjamin Herrenschmidtfb111332016-07-08 16:37:09 +10002726 /* The MSI EOI OPAL call is only needed on PHB3 */
2727 if (phb->model != PNV_PHB_MODEL_PHB3)
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002728 return;
2729
2730 if (!phb->ioda.irq_chip_init) {
2731 /*
2732 * First time we setup an MSI IRQ, we need to setup the
2733 * corresponding IRQ chip to route correctly.
2734 */
2735 idata = irq_get_irq_data(virq);
2736 ichip = irq_data_get_irq_chip(idata);
2737 phb->ioda.irq_chip_init = 1;
2738 phb->ioda.irq_chip = *ichip;
2739 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2740 }
2741 irq_set_chip(virq, &phb->ioda.irq_chip);
2742}
2743
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002744static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002745 unsigned int hwirq, unsigned int virq,
2746 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002747{
2748 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2749 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002750 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002751 int rc;
2752
2753 /* No PE assigned ? bail out ... no MSI for you ! */
2754 if (pe == NULL)
2755 return -ENXIO;
2756
2757 /* Check if we have an MVE */
2758 if (pe->mve_number < 0)
2759 return -ENXIO;
2760
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002761 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002762 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002763 is_64 = 0;
2764
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002765 /* Assign XIVE to PE */
2766 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2767 if (rc) {
2768 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2769 pci_name(dev), rc, xive_num);
2770 return -EIO;
2771 }
2772
2773 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002774 __be64 addr64;
2775
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002776 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2777 &addr64, &data);
2778 if (rc) {
2779 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2780 pci_name(dev), rc);
2781 return -EIO;
2782 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002783 msg->address_hi = be64_to_cpu(addr64) >> 32;
2784 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002785 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002786 __be32 addr32;
2787
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002788 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2789 &addr32, &data);
2790 if (rc) {
2791 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2792 pci_name(dev), rc);
2793 return -EIO;
2794 }
2795 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002796 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002797 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002798 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002799
Ian Munsief4568342016-07-14 07:17:00 +10002800 pnv_set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002801
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002802 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2803 " address=%x_%08x data=%x PE# %d\n",
2804 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2805 msg->address_hi, msg->address_lo, data, pe->pe_number);
2806
2807 return 0;
2808}
2809
2810static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2811{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002812 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002813 const __be32 *prop = of_get_property(phb->hose->dn,
2814 "ibm,opal-msi-ranges", NULL);
2815 if (!prop) {
2816 /* BML Fallback */
2817 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2818 }
2819 if (!prop)
2820 return;
2821
2822 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002823 count = be32_to_cpup(prop + 1);
2824 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002825 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2826 phb->hose->global_number);
2827 return;
2828 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002829
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002830 phb->msi_setup = pnv_pci_ioda_msi_setup;
2831 phb->msi32_support = 1;
2832 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002833 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002834}
2835#else
2836static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2837#endif /* CONFIG_PCI_MSI */
2838
Wei Yang6e628c72015-03-25 16:23:55 +08002839#ifdef CONFIG_PCI_IOV
2840static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2841{
Wei Yangf2dd0af2015-10-22 09:22:17 +08002842 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
2843 struct pnv_phb *phb = hose->private_data;
2844 const resource_size_t gate = phb->ioda.m64_segsize >> 2;
Wei Yang6e628c72015-03-25 16:23:55 +08002845 struct resource *res;
2846 int i;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002847 resource_size_t size, total_vf_bar_sz;
Wei Yang6e628c72015-03-25 16:23:55 +08002848 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002849 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002850
2851 if (!pdev->is_physfn || pdev->is_added)
2852 return;
2853
Wei Yang6e628c72015-03-25 16:23:55 +08002854 pdn = pci_get_pdn(pdev);
2855 pdn->vfs_expanded = 0;
Wei Yangee8222f2015-10-22 09:22:16 +08002856 pdn->m64_single_mode = false;
Wei Yang6e628c72015-03-25 16:23:55 +08002857
Wei Yang5b88ec22015-03-25 16:23:58 +08002858 total_vfs = pci_sriov_get_totalvfs(pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10002859 mul = phb->ioda.total_pe_num;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002860 total_vf_bar_sz = 0;
Wei Yang5b88ec22015-03-25 16:23:58 +08002861
2862 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2863 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2864 if (!res->flags || res->parent)
2865 continue;
2866 if (!pnv_pci_is_mem_pref_64(res->flags)) {
Wei Yangb0331852015-10-22 09:22:14 +08002867 dev_warn(&pdev->dev, "Don't support SR-IOV with"
2868 " non M64 VF BAR%d: %pR. \n",
Wei Yang5b88ec22015-03-25 16:23:58 +08002869 i, res);
Wei Yangb0331852015-10-22 09:22:14 +08002870 goto truncate_iov;
Wei Yang5b88ec22015-03-25 16:23:58 +08002871 }
2872
Wei Yangdfcc8d42015-10-22 09:22:18 +08002873 total_vf_bar_sz += pci_iov_resource_size(pdev,
2874 i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002875
Wei Yangf2dd0af2015-10-22 09:22:17 +08002876 /*
2877 * If bigger than quarter of M64 segment size, just round up
2878 * power of two.
2879 *
2880 * Generally, one M64 BAR maps one IOV BAR. To avoid conflict
2881 * with other devices, IOV BAR size is expanded to be
2882 * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64
2883 * segment size , the expanded size would equal to half of the
2884 * whole M64 space size, which will exhaust the M64 Space and
2885 * limit the system flexibility. This is a design decision to
2886 * set the boundary to quarter of the M64 segment size.
2887 */
Wei Yangdfcc8d42015-10-22 09:22:18 +08002888 if (total_vf_bar_sz > gate) {
Wei Yang5b88ec22015-03-25 16:23:58 +08002889 mul = roundup_pow_of_two(total_vfs);
Wei Yangdfcc8d42015-10-22 09:22:18 +08002890 dev_info(&pdev->dev,
2891 "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n",
2892 total_vf_bar_sz, gate, mul);
Wei Yangee8222f2015-10-22 09:22:16 +08002893 pdn->m64_single_mode = true;
Wei Yang5b88ec22015-03-25 16:23:58 +08002894 break;
2895 }
2896 }
2897
Wei Yang6e628c72015-03-25 16:23:55 +08002898 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2899 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2900 if (!res->flags || res->parent)
2901 continue;
Wei Yang6e628c72015-03-25 16:23:55 +08002902
Wei Yang6e628c72015-03-25 16:23:55 +08002903 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yangee8222f2015-10-22 09:22:16 +08002904 /*
2905 * On PHB3, the minimum size alignment of M64 BAR in single
2906 * mode is 32MB.
2907 */
2908 if (pdn->m64_single_mode && (size < SZ_32M))
2909 goto truncate_iov;
2910 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
Wei Yang5b88ec22015-03-25 16:23:58 +08002911 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08002912 dev_dbg(&pdev->dev, " %pR\n", res);
2913 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08002914 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08002915 }
Wei Yang5b88ec22015-03-25 16:23:58 +08002916 pdn->vfs_expanded = mul;
Wei Yangb0331852015-10-22 09:22:14 +08002917
2918 return;
2919
2920truncate_iov:
2921 /* To save MMIO space, IOV BAR is truncated. */
2922 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2923 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2924 res->flags = 0;
2925 res->end = res->start - 1;
2926 }
Wei Yang6e628c72015-03-25 16:23:55 +08002927}
2928#endif /* CONFIG_PCI_IOV */
2929
Gavin Shan23e79422016-05-03 15:41:27 +10002930static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
2931 struct resource *res)
2932{
2933 struct pnv_phb *phb = pe->phb;
2934 struct pci_bus_region region;
2935 int index;
2936 int64_t rc;
2937
2938 if (!res || !res->flags || res->start > res->end)
2939 return;
2940
2941 if (res->flags & IORESOURCE_IO) {
2942 region.start = res->start - phb->ioda.io_pci_base;
2943 region.end = res->end - phb->ioda.io_pci_base;
2944 index = region.start / phb->ioda.io_segsize;
2945
2946 while (index < phb->ioda.total_pe_num &&
2947 region.start <= region.end) {
2948 phb->ioda.io_segmap[index] = pe->pe_number;
2949 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2950 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2951 if (rc != OPAL_SUCCESS) {
2952 pr_err("%s: Error %lld mapping IO segment#%d to PE#%d\n",
2953 __func__, rc, index, pe->pe_number);
2954 break;
2955 }
2956
2957 region.start += phb->ioda.io_segsize;
2958 index++;
2959 }
2960 } else if ((res->flags & IORESOURCE_MEM) &&
2961 !pnv_pci_is_mem_pref_64(res->flags)) {
2962 region.start = res->start -
2963 phb->hose->mem_offset[0] -
2964 phb->ioda.m32_pci_base;
2965 region.end = res->end -
2966 phb->hose->mem_offset[0] -
2967 phb->ioda.m32_pci_base;
2968 index = region.start / phb->ioda.m32_segsize;
2969
2970 while (index < phb->ioda.total_pe_num &&
2971 region.start <= region.end) {
2972 phb->ioda.m32_segmap[index] = pe->pe_number;
2973 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2974 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2975 if (rc != OPAL_SUCCESS) {
2976 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%d",
2977 __func__, rc, index, pe->pe_number);
2978 break;
2979 }
2980
2981 region.start += phb->ioda.m32_segsize;
2982 index++;
2983 }
2984 }
2985}
2986
Gavin Shan11685be2012-08-20 03:49:16 +00002987/*
2988 * This function is supposed to be called on basis of PE from top
2989 * to bottom style. So the the I/O or MMIO segment assigned to
2990 * parent PE could be overrided by its child PEs if necessary.
2991 */
Gavin Shan23e79422016-05-03 15:41:27 +10002992static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00002993{
Gavin Shan69d733e2016-05-03 15:41:28 +10002994 struct pci_dev *pdev;
Gavin Shan23e79422016-05-03 15:41:27 +10002995 int i;
Gavin Shan11685be2012-08-20 03:49:16 +00002996
2997 /*
2998 * NOTE: We only care PCI bus based PE for now. For PCI
2999 * device based PE, for example SRIOV sensitive VF should
3000 * be figured out later.
3001 */
3002 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
3003
Gavin Shan69d733e2016-05-03 15:41:28 +10003004 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
3005 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
3006 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
3007
3008 /*
3009 * If the PE contains all subordinate PCI buses, the
3010 * windows of the child bridges should be mapped to
3011 * the PE as well.
3012 */
3013 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
3014 continue;
3015 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
3016 pnv_ioda_setup_pe_res(pe,
3017 &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
3018 }
Gavin Shan11685be2012-08-20 03:49:16 +00003019}
3020
Gavin Shan37c367f2013-06-20 18:13:25 +08003021static void pnv_pci_ioda_create_dbgfs(void)
3022{
3023#ifdef CONFIG_DEBUG_FS
3024 struct pci_controller *hose, *tmp;
3025 struct pnv_phb *phb;
3026 char name[16];
3027
3028 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3029 phb = hose->private_data;
3030
Gavin Shanccd1c192016-05-20 16:41:31 +10003031 /* Notify initialization of PHB done */
3032 phb->initialized = 1;
3033
Gavin Shan37c367f2013-06-20 18:13:25 +08003034 sprintf(name, "PCI%04x", hose->global_number);
3035 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
3036 if (!phb->dbgfs)
3037 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
3038 __func__, hose->global_number);
3039 }
3040#endif /* CONFIG_DEBUG_FS */
3041}
3042
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003043static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00003044{
3045 pnv_pci_ioda_setup_PEs();
Gavin Shanccd1c192016-05-20 16:41:31 +10003046 pnv_pci_ioda_setup_iommu_api();
Gavin Shan37c367f2013-06-20 18:13:25 +08003047 pnv_pci_ioda_create_dbgfs();
3048
Gavin Shane9cc17d2013-06-20 13:21:14 +08003049#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08003050 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04003051 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003052#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00003053}
3054
Gavin Shan271fd032012-09-11 16:59:47 -06003055/*
3056 * Returns the alignment for I/O or memory windows for P2P
3057 * bridges. That actually depends on how PEs are segmented.
3058 * For now, we return I/O or M32 segment size for PE sensitive
3059 * P2P bridges. Otherwise, the default values (4KiB for I/O,
3060 * 1MiB for memory) will be returned.
3061 *
3062 * The current PCI bus might be put into one PE, which was
3063 * create against the parent PCI bridge. For that case, we
3064 * needn't enlarge the alignment so that we can save some
3065 * resources.
3066 */
3067static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
3068 unsigned long type)
3069{
3070 struct pci_dev *bridge;
3071 struct pci_controller *hose = pci_bus_to_host(bus);
3072 struct pnv_phb *phb = hose->private_data;
3073 int num_pci_bridges = 0;
3074
3075 bridge = bus->self;
3076 while (bridge) {
3077 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
3078 num_pci_bridges++;
3079 if (num_pci_bridges >= 2)
3080 return 1;
3081 }
3082
3083 bridge = bridge->bus->self;
3084 }
3085
Guo Chao262af552014-07-21 14:42:30 +10003086 /* We fail back to M32 if M64 isn't supported */
3087 if (phb->ioda.m64_segsize &&
3088 pnv_pci_is_mem_pref_64(type))
3089 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06003090 if (type & IORESOURCE_MEM)
3091 return phb->ioda.m32_segsize;
3092
3093 return phb->ioda.io_segsize;
3094}
3095
Gavin Shan40e2a472016-05-20 16:41:33 +10003096/*
3097 * We are updating root port or the upstream port of the
3098 * bridge behind the root port with PHB's windows in order
3099 * to accommodate the changes on required resources during
3100 * PCI (slot) hotplug, which is connected to either root
3101 * port or the downstream ports of PCIe switch behind the
3102 * root port.
3103 */
3104static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
3105 unsigned long type)
3106{
3107 struct pci_controller *hose = pci_bus_to_host(bus);
3108 struct pnv_phb *phb = hose->private_data;
3109 struct pci_dev *bridge = bus->self;
3110 struct resource *r, *w;
3111 bool msi_region = false;
3112 int i;
3113
3114 /* Check if we need apply fixup to the bridge's windows */
3115 if (!pci_is_root_bus(bridge->bus) &&
3116 !pci_is_root_bus(bridge->bus->self->bus))
3117 return;
3118
3119 /* Fixup the resources */
3120 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
3121 r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
3122 if (!r->flags || !r->parent)
3123 continue;
3124
3125 w = NULL;
3126 if (r->flags & type & IORESOURCE_IO)
3127 w = &hose->io_resource;
3128 else if (pnv_pci_is_mem_pref_64(r->flags) &&
3129 (type & IORESOURCE_PREFETCH) &&
3130 phb->ioda.m64_segsize)
3131 w = &hose->mem_resources[1];
3132 else if (r->flags & type & IORESOURCE_MEM) {
3133 w = &hose->mem_resources[0];
3134 msi_region = true;
3135 }
3136
3137 r->start = w->start;
3138 r->end = w->end;
3139
3140 /* The 64KB 32-bits MSI region shouldn't be included in
3141 * the 32-bits bridge window. Otherwise, we can see strange
3142 * issues. One of them is EEH error observed on Garrison.
3143 *
3144 * Exclude top 1MB region which is the minimal alignment of
3145 * 32-bits bridge window.
3146 */
3147 if (msi_region) {
3148 r->end += 0x10000;
3149 r->end -= 0x100000;
3150 }
3151 }
3152}
3153
Gavin Shanccd1c192016-05-20 16:41:31 +10003154static void pnv_pci_setup_bridge(struct pci_bus *bus, unsigned long type)
3155{
3156 struct pci_controller *hose = pci_bus_to_host(bus);
3157 struct pnv_phb *phb = hose->private_data;
3158 struct pci_dev *bridge = bus->self;
3159 struct pnv_ioda_pe *pe;
3160 bool all = (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
3161
Gavin Shan40e2a472016-05-20 16:41:33 +10003162 /* Extend bridge's windows if necessary */
3163 pnv_pci_fixup_bridge_resources(bus, type);
3164
Gavin Shan63803c32016-05-20 16:41:32 +10003165 /* The PE for root bus should be realized before any one else */
3166 if (!phb->ioda.root_pe_populated) {
3167 pe = pnv_ioda_setup_bus_PE(phb->hose->bus, false);
3168 if (pe) {
3169 phb->ioda.root_pe_idx = pe->pe_number;
3170 phb->ioda.root_pe_populated = true;
3171 }
3172 }
3173
Gavin Shanccd1c192016-05-20 16:41:31 +10003174 /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
3175 if (list_empty(&bus->devices))
3176 return;
3177
3178 /* Reserve PEs according to used M64 resources */
3179 if (phb->reserve_m64_pe)
3180 phb->reserve_m64_pe(bus, NULL, all);
3181
3182 /*
3183 * Assign PE. We might run here because of partial hotplug.
3184 * For the case, we just pick up the existing PE and should
3185 * not allocate resources again.
3186 */
3187 pe = pnv_ioda_setup_bus_PE(bus, all);
3188 if (!pe)
3189 return;
3190
3191 pnv_ioda_setup_pe_seg(pe);
3192 switch (phb->type) {
3193 case PNV_PHB_IODA1:
3194 pnv_pci_ioda1_setup_dma_pe(phb, pe);
3195 break;
3196 case PNV_PHB_IODA2:
3197 pnv_pci_ioda2_setup_dma_pe(phb, pe);
3198 break;
3199 default:
3200 pr_warn("%s: No DMA for PHB#%d (type %d)\n",
3201 __func__, phb->hose->global_number, phb->type);
3202 }
3203}
3204
Wei Yang5350ab32015-03-25 16:23:56 +08003205#ifdef CONFIG_PCI_IOV
3206static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
3207 int resno)
3208{
Wei Yangee8222f2015-10-22 09:22:16 +08003209 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3210 struct pnv_phb *phb = hose->private_data;
Wei Yang5350ab32015-03-25 16:23:56 +08003211 struct pci_dn *pdn = pci_get_pdn(pdev);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003212 resource_size_t align;
Wei Yang5350ab32015-03-25 16:23:56 +08003213
Wei Yang7fbe7a92015-10-22 09:22:15 +08003214 /*
3215 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
3216 * SR-IOV. While from hardware perspective, the range mapped by M64
3217 * BAR should be size aligned.
3218 *
Wei Yangee8222f2015-10-22 09:22:16 +08003219 * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra
3220 * powernv-specific hardware restriction is gone. But if just use the
3221 * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with
3222 * in one segment of M64 #15, which introduces the PE conflict between
3223 * PF and VF. Based on this, the minimum alignment of an IOV BAR is
3224 * m64_segsize.
3225 *
Wei Yang7fbe7a92015-10-22 09:22:15 +08003226 * This function returns the total IOV BAR size if M64 BAR is in
3227 * Shared PE mode or just VF BAR size if not.
Wei Yangee8222f2015-10-22 09:22:16 +08003228 * If the M64 BAR is in Single PE mode, return the VF BAR size or
3229 * M64 segment size if IOV BAR size is less.
Wei Yang7fbe7a92015-10-22 09:22:15 +08003230 */
Wei Yang5350ab32015-03-25 16:23:56 +08003231 align = pci_iov_resource_size(pdev, resno);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003232 if (!pdn->vfs_expanded)
3233 return align;
Wei Yangee8222f2015-10-22 09:22:16 +08003234 if (pdn->m64_single_mode)
3235 return max(align, (resource_size_t)phb->ioda.m64_segsize);
Wei Yang5350ab32015-03-25 16:23:56 +08003236
Wei Yang7fbe7a92015-10-22 09:22:15 +08003237 return pdn->vfs_expanded * align;
Wei Yang5350ab32015-03-25 16:23:56 +08003238}
3239#endif /* CONFIG_PCI_IOV */
3240
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003241/* Prevent enabling devices for which we couldn't properly
3242 * assign a PE
3243 */
Ian Munsie4361b032016-07-14 07:17:06 +10003244bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003245{
Gavin Shandb1266c2012-08-20 03:49:18 +00003246 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3247 struct pnv_phb *phb = hose->private_data;
3248 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003249
Gavin Shandb1266c2012-08-20 03:49:18 +00003250 /* The function is probably called while the PEs have
3251 * not be created yet. For example, resource reassignment
3252 * during PCI probe period. We just skip the check if
3253 * PEs isn't ready.
3254 */
3255 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003256 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003257
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003258 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003259 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003260 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003261
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003262 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003263}
3264
Gavin Shanc5f77002016-05-20 16:41:35 +10003265static long pnv_pci_ioda1_unset_window(struct iommu_table_group *table_group,
3266 int num)
3267{
3268 struct pnv_ioda_pe *pe = container_of(table_group,
3269 struct pnv_ioda_pe, table_group);
3270 struct pnv_phb *phb = pe->phb;
3271 unsigned int idx;
3272 long rc;
3273
3274 pe_info(pe, "Removing DMA window #%d\n", num);
3275 for (idx = 0; idx < phb->ioda.dma32_count; idx++) {
3276 if (phb->ioda.dma32_segmap[idx] != pe->pe_number)
3277 continue;
3278
3279 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
3280 idx, 0, 0ul, 0ul, 0ul);
3281 if (rc != OPAL_SUCCESS) {
3282 pe_warn(pe, "Failure %ld unmapping DMA32 segment#%d\n",
3283 rc, idx);
3284 return rc;
3285 }
3286
3287 phb->ioda.dma32_segmap[idx] = IODA_INVALID_PE;
3288 }
3289
3290 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
3291 return OPAL_SUCCESS;
3292}
3293
3294static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe)
3295{
3296 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
3297 struct iommu_table *tbl = pe->table_group.tables[0];
3298 int64_t rc;
3299
3300 if (!weight)
3301 return;
3302
3303 rc = pnv_pci_ioda1_unset_window(&pe->table_group, 0);
3304 if (rc != OPAL_SUCCESS)
3305 return;
3306
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10003307 pnv_pci_p7ioc_tce_invalidate(tbl, tbl->it_offset, tbl->it_size, false);
Gavin Shanc5f77002016-05-20 16:41:35 +10003308 if (pe->table_group.group) {
3309 iommu_group_put(pe->table_group.group);
3310 WARN_ON(pe->table_group.group);
3311 }
3312
3313 free_pages(tbl->it_base, get_order(tbl->it_size << 3));
3314 iommu_free_table(tbl, "pnv");
3315}
3316
3317static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
3318{
3319 struct iommu_table *tbl = pe->table_group.tables[0];
3320 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
3321#ifdef CONFIG_IOMMU_API
3322 int64_t rc;
3323#endif
3324
3325 if (!weight)
3326 return;
3327
3328#ifdef CONFIG_IOMMU_API
3329 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
3330 if (rc)
3331 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
3332#endif
3333
3334 pnv_pci_ioda2_set_bypass(pe, false);
3335 if (pe->table_group.group) {
3336 iommu_group_put(pe->table_group.group);
3337 WARN_ON(pe->table_group.group);
3338 }
3339
3340 pnv_pci_ioda2_table_free_pages(tbl);
3341 iommu_free_table(tbl, "pnv");
3342}
3343
3344static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
3345 unsigned short win,
3346 unsigned int *map)
3347{
3348 struct pnv_phb *phb = pe->phb;
3349 int idx;
3350 int64_t rc;
3351
3352 for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
3353 if (map[idx] != pe->pe_number)
3354 continue;
3355
3356 if (win == OPAL_M64_WINDOW_TYPE)
3357 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3358 phb->ioda.reserved_pe_idx, win,
3359 idx / PNV_IODA1_M64_SEGS,
3360 idx % PNV_IODA1_M64_SEGS);
3361 else
3362 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3363 phb->ioda.reserved_pe_idx, win, 0, idx);
3364
3365 if (rc != OPAL_SUCCESS)
3366 pe_warn(pe, "Error %ld unmapping (%d) segment#%d\n",
3367 rc, win, idx);
3368
3369 map[idx] = IODA_INVALID_PE;
3370 }
3371}
3372
3373static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
3374{
3375 struct pnv_phb *phb = pe->phb;
3376
3377 if (phb->type == PNV_PHB_IODA1) {
3378 pnv_ioda_free_pe_seg(pe, OPAL_IO_WINDOW_TYPE,
3379 phb->ioda.io_segmap);
3380 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
3381 phb->ioda.m32_segmap);
3382 pnv_ioda_free_pe_seg(pe, OPAL_M64_WINDOW_TYPE,
3383 phb->ioda.m64_segmap);
3384 } else if (phb->type == PNV_PHB_IODA2) {
3385 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
3386 phb->ioda.m32_segmap);
3387 }
3388}
3389
3390static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
3391{
3392 struct pnv_phb *phb = pe->phb;
3393 struct pnv_ioda_pe *slave, *tmp;
3394
3395 /* Release slave PEs in compound PE */
3396 if (pe->flags & PNV_IODA_PE_MASTER) {
3397 list_for_each_entry_safe(slave, tmp, &pe->slaves, list)
3398 pnv_ioda_release_pe(slave);
3399 }
3400
3401 list_del(&pe->list);
3402 switch (phb->type) {
3403 case PNV_PHB_IODA1:
3404 pnv_pci_ioda1_release_pe_dma(pe);
3405 break;
3406 case PNV_PHB_IODA2:
3407 pnv_pci_ioda2_release_pe_dma(pe);
3408 break;
3409 default:
3410 WARN_ON(1);
3411 }
3412
3413 pnv_ioda_release_pe_seg(pe);
3414 pnv_ioda_deconfigure_pe(pe->phb, pe);
3415 pnv_ioda_free_pe(pe);
3416}
3417
3418static void pnv_pci_release_device(struct pci_dev *pdev)
3419{
3420 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3421 struct pnv_phb *phb = hose->private_data;
3422 struct pci_dn *pdn = pci_get_pdn(pdev);
3423 struct pnv_ioda_pe *pe;
3424
3425 if (pdev->is_virtfn)
3426 return;
3427
3428 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
3429 return;
3430
3431 pe = &phb->ioda.pe_array[pdn->pe_number];
3432 WARN_ON(--pe->device_count < 0);
3433 if (pe->device_count == 0)
3434 pnv_ioda_release_pe(pe);
3435}
3436
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003437static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003438{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003439 struct pnv_phb *phb = hose->private_data;
3440
Gavin Shand1a85ee2014-09-30 12:39:05 +10003441 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003442 OPAL_ASSERT_RESET);
3443}
3444
Daniel Axtens92ae0352015-04-28 15:12:05 +10003445static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003446 .dma_dev_setup = pnv_pci_dma_dev_setup,
3447 .dma_bus_setup = pnv_pci_dma_bus_setup,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003448#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003449 .setup_msi_irqs = pnv_setup_msi_irqs,
3450 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003451#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003452 .enable_device_hook = pnv_pci_enable_device_hook,
Gavin Shanc5f77002016-05-20 16:41:35 +10003453 .release_device = pnv_pci_release_device,
Gavin Shancb4224c2016-05-03 15:41:21 +10003454 .window_alignment = pnv_pci_window_alignment,
Gavin Shanccd1c192016-05-20 16:41:31 +10003455 .setup_bridge = pnv_pci_setup_bridge,
Gavin Shancb4224c2016-05-03 15:41:21 +10003456 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3457 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3458 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3459 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003460};
3461
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003462static int pnv_npu_dma_set_mask(struct pci_dev *npdev, u64 dma_mask)
3463{
3464 dev_err_once(&npdev->dev,
3465 "%s operation unsupported for NVLink devices\n",
3466 __func__);
3467 return -EPERM;
3468}
3469
Alistair Popple5d2aa712015-12-17 13:43:13 +11003470static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003471 .dma_dev_setup = pnv_pci_dma_dev_setup,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003472#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003473 .setup_msi_irqs = pnv_setup_msi_irqs,
3474 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003475#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003476 .enable_device_hook = pnv_pci_enable_device_hook,
3477 .window_alignment = pnv_pci_window_alignment,
3478 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3479 .dma_set_mask = pnv_npu_dma_set_mask,
3480 .shutdown = pnv_pci_ioda_shutdown,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003481};
3482
Ian Munsie4361b032016-07-14 07:17:06 +10003483#ifdef CONFIG_CXL_BASE
3484const struct pci_controller_ops pnv_cxl_cx4_ioda_controller_ops = {
3485 .dma_dev_setup = pnv_pci_dma_dev_setup,
3486 .dma_bus_setup = pnv_pci_dma_bus_setup,
Ian Munsiea2f67d52016-07-14 07:17:10 +10003487#ifdef CONFIG_PCI_MSI
3488 .setup_msi_irqs = pnv_cxl_cx4_setup_msi_irqs,
3489 .teardown_msi_irqs = pnv_cxl_cx4_teardown_msi_irqs,
3490#endif
Ian Munsie4361b032016-07-14 07:17:06 +10003491 .enable_device_hook = pnv_cxl_enable_device_hook,
3492 .disable_device = pnv_cxl_disable_device,
3493 .release_device = pnv_pci_release_device,
3494 .window_alignment = pnv_pci_window_alignment,
3495 .setup_bridge = pnv_pci_setup_bridge,
3496 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3497 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3498 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3499 .shutdown = pnv_pci_ioda_shutdown,
3500};
3501#endif
3502
Anton Blancharde51df2c2014-08-20 08:55:18 +10003503static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3504 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003505{
3506 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003507 struct pnv_phb *phb;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003508 unsigned long size, m64map_off, m32map_off, pemap_off;
3509 unsigned long iomap_off = 0, dma32map_off = 0;
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10003510 struct resource r;
Alistair Popplec681b932013-09-23 12:04:57 +10003511 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003512 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003513 int len;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003514 unsigned int segno;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003515 u64 phb_id;
3516 void *aux;
3517 long rc;
3518
Benjamin Herrenschmidt08a45b32016-07-08 16:37:17 +10003519 if (!of_device_is_available(np))
3520 return;
3521
Gavin Shan9497a1c2016-06-21 12:35:56 +10003522 pr_info("Initializing %s PHB (%s)\n",
3523 pnv_phb_names[ioda_type], of_node_full_name(np));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003524
3525 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3526 if (!prop64) {
3527 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3528 return;
3529 }
3530 phb_id = be64_to_cpup(prop64);
3531 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3532
Michael Ellermane39f223f2014-11-18 16:47:35 +11003533 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003534
3535 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003536 phb->hose = hose = pcibios_alloc_controller(np);
3537 if (!phb->hose) {
3538 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003539 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003540 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003541 return;
3542 }
3543
3544 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003545 prop32 = of_get_property(np, "bus-range", &len);
3546 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003547 hose->first_busno = be32_to_cpu(prop32[0]);
3548 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003549 } else {
3550 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3551 hose->first_busno = 0;
3552 hose->last_busno = 0xff;
3553 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003554 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003555 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003556 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003557 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003558 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003559
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003560 /* Detect specific models for error handling */
3561 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3562 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003563 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003564 phb->model = PNV_PHB_MODEL_PHB3;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003565 else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
3566 phb->model = PNV_PHB_MODEL_NPU;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003567 else
3568 phb->model = PNV_PHB_MODEL_UNKNOWN;
3569
Gavin Shanaa0c0332013-04-25 19:20:57 +00003570 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003571 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003572
Gavin Shanaa0c0332013-04-25 19:20:57 +00003573 /* Get registers */
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10003574 if (!of_address_to_resource(np, 0, &r)) {
3575 phb->regs_phys = r.start;
3576 phb->regs = ioremap(r.start, resource_size(&r));
3577 if (phb->regs == NULL)
3578 pr_err(" Failed to map registers !\n");
3579 }
Gavin Shan577c8c82016-05-20 16:41:28 +10003580
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003581 /* Initialize more IODA stuff */
Gavin Shan92b8f132016-05-03 15:41:24 +10003582 phb->ioda.total_pe_num = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003583 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003584 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003585 phb->ioda.total_pe_num = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003586 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3587 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003588 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003589
Gavin Shanc1275622016-05-20 16:41:29 +10003590 /* Invalidate RID to PE# mapping */
3591 for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
3592 phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
3593
Guo Chao262af552014-07-21 14:42:30 +10003594 /* Parse 64-bit MMIO range */
3595 pnv_ioda_parse_m64_window(phb);
3596
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003597 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003598 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003599 phb->ioda.m32_size += 0x10000;
3600
Gavin Shan92b8f132016-05-03 15:41:24 +10003601 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003602 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003603 phb->ioda.io_size = hose->pci_io_size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003604 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003605 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3606
Gavin Shan2b923ed2016-05-05 12:04:16 +10003607 /* Calculate how many 32-bit TCE segments we have */
3608 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
3609 PNV_IODA1_DMA32_SEGSIZE;
3610
Gavin Shanc35d2a82013-07-31 16:47:04 +08003611 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Alexey Kardashevskiy92a86752016-05-12 15:47:09 +10003612 size = _ALIGN_UP(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
3613 sizeof(unsigned long));
Gavin Shan93289d82016-05-03 15:41:29 +10003614 m64map_off = size;
3615 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003616 m32map_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003617 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003618 if (phb->type == PNV_PHB_IODA1) {
3619 iomap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003620 size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
Gavin Shan2b923ed2016-05-05 12:04:16 +10003621 dma32map_off = size;
3622 size += phb->ioda.dma32_count *
3623 sizeof(phb->ioda.dma32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003624 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003625 pemap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003626 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003627 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003628 phb->ioda.pe_alloc = aux;
Gavin Shan93289d82016-05-03 15:41:29 +10003629 phb->ioda.m64_segmap = aux + m64map_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003630 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shan93289d82016-05-03 15:41:29 +10003631 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
3632 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003633 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan93289d82016-05-03 15:41:29 +10003634 }
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003635 if (phb->type == PNV_PHB_IODA1) {
Gavin Shanc35d2a82013-07-31 16:47:04 +08003636 phb->ioda.io_segmap = aux + iomap_off;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003637 for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
3638 phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003639
3640 phb->ioda.dma32_segmap = aux + dma32map_off;
3641 for (segno = 0; segno < phb->ioda.dma32_count; segno++)
3642 phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003643 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003644 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan63803c32016-05-20 16:41:32 +10003645
3646 /*
3647 * Choose PE number for root bus, which shouldn't have
3648 * M64 resources consumed by its child devices. To pick
3649 * the PE number adjacent to the reserved one if possible.
3650 */
3651 pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
3652 if (phb->ioda.reserved_pe_idx == 0) {
3653 phb->ioda.root_pe_idx = 1;
3654 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3655 } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
3656 phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
3657 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3658 } else {
3659 phb->ioda.root_pe_idx = IODA_INVALID_PE;
3660 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003661
3662 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003663 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003664
3665 /* Calculate how many 32-bit TCE segments we have */
Gavin Shan2b923ed2016-05-05 12:04:16 +10003666 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
Gavin Shanacce9712016-05-03 15:41:33 +10003667 PNV_IODA1_DMA32_SEGSIZE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003668
Gavin Shanaa0c0332013-04-25 19:20:57 +00003669#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003670 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3671 window_type,
3672 window_num,
3673 starting_real_address,
3674 starting_pci_address,
3675 segment_size);
3676#endif
3677
Guo Chao262af552014-07-21 14:42:30 +10003678 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
Gavin Shan92b8f132016-05-03 15:41:24 +10003679 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
Guo Chao262af552014-07-21 14:42:30 +10003680 phb->ioda.m32_size, phb->ioda.m32_segsize);
3681 if (phb->ioda.m64_size)
3682 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3683 phb->ioda.m64_size, phb->ioda.m64_segsize);
3684 if (phb->ioda.io_size)
3685 pr_info(" IO: 0x%x [segment=0x%x]\n",
3686 phb->ioda.io_size, phb->ioda.io_segsize);
3687
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003688
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003689 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003690 phb->get_pe_state = pnv_ioda_get_pe_state;
3691 phb->freeze_pe = pnv_ioda_freeze_pe;
3692 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003693
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003694 /* Setup MSI support */
3695 pnv_pci_init_ioda_msis(phb);
3696
Gavin Shanc40a4212012-08-20 03:49:20 +00003697 /*
3698 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3699 * to let the PCI core do resource assignment. It's supposed
3700 * that the PCI core will do correct I/O and MMIO alignment
3701 * for the P2P bridge bars so that each PCI bus (excluding
3702 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003703 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003704 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003705
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003706 if (phb->type == PNV_PHB_NPU) {
Alistair Popple5d2aa712015-12-17 13:43:13 +11003707 hose->controller_ops = pnv_npu_ioda_controller_ops;
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003708 } else {
3709 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003710 hose->controller_ops = pnv_pci_ioda_controller_ops;
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003711 }
Michael Ellermanad30cb92015-04-14 09:29:23 +10003712
Wei Yang6e628c72015-03-25 16:23:55 +08003713#ifdef CONFIG_PCI_IOV
3714 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003715 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003716#endif
3717
Gavin Shanc40a4212012-08-20 03:49:20 +00003718 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003719
3720 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003721 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003722 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003723 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003724
3725 /* If we're running in kdump kerenl, the previous kerenl never
3726 * shutdown PCI devices correctly. We already got IODA table
3727 * cleaned out. So we have to issue PHB reset to stop all PCI
3728 * transactions from previous kerenl.
3729 */
3730 if (is_kdump_kernel()) {
3731 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003732 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3733 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003734 }
Guo Chao262af552014-07-21 14:42:30 +10003735
Gavin Shan9e9e8932014-11-12 13:36:05 +11003736 /* Remove M64 resource if we can't configure it successfully */
3737 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003738 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003739}
3740
Bjorn Helgaas67975002013-07-02 12:20:03 -06003741void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003742{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003743 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003744}
3745
Alistair Popple5d2aa712015-12-17 13:43:13 +11003746void __init pnv_pci_init_npu_phb(struct device_node *np)
3747{
3748 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU);
3749}
3750
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003751void __init pnv_pci_init_ioda_hub(struct device_node *np)
3752{
3753 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003754 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003755 u64 hub_id;
3756
3757 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3758
3759 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3760 if (!prop64) {
3761 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3762 return;
3763 }
3764 hub_id = be64_to_cpup(prop64);
3765 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3766
3767 /* Count child PHBs */
3768 for_each_child_of_node(np, phbn) {
3769 /* Look for IODA1 PHBs */
3770 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003771 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003772 }
3773}