blob: fc38f06ee41d22aa6c1a5d8fd5e1259e6cd49b3e [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>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000017#include <linux/delay.h>
18#include <linux/string.h>
19#include <linux/init.h>
20#include <linux/bootmem.h>
21#include <linux/irq.h>
22#include <linux/io.h>
23#include <linux/msi.h>
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +110024#include <linux/memblock.h>
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +100025#include <linux/iommu.h>
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +100026#include <linux/rculist.h>
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +100027#include <linux/sizes.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000028
29#include <asm/sections.h>
30#include <asm/io.h>
31#include <asm/prom.h>
32#include <asm/pci-bridge.h>
33#include <asm/machdep.h>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000034#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000035#include <asm/ppc-pci.h>
36#include <asm/opal.h>
37#include <asm/iommu.h>
38#include <asm/tce.h>
Gavin Shan137436c2013-04-25 19:20:59 +000039#include <asm/xics.h>
Michael Ellerman7644d582017-02-10 12:04:56 +110040#include <asm/debugfs.h>
Guo Chao262af552014-07-21 14:42:30 +100041#include <asm/firmware.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110042#include <asm/pnv-pci.h>
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100043#include <asm/mmzone.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110044
Michael Neulingec249dd2015-05-27 16:07:16 +100045#include <misc/cxl-base.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000046
47#include "powernv.h"
48#include "pci.h"
49
Gavin Shan99451552016-05-05 12:02:13 +100050#define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */
51#define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */
Gavin Shanacce9712016-05-03 15:41:33 +100052#define PNV_IODA1_DMA32_SEGSIZE 0x10000000
Wei Yang781a8682015-03-25 16:23:57 +080053
Frederic Barrat7f2c39e2018-01-23 12:31:36 +010054static const char * const pnv_phb_names[] = { "IODA1", "IODA2", "NPU_NVLINK",
55 "NPU_OCAPI" };
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100056
Alexey Kardashevskiy7d623e42016-04-29 18:55:21 +100057void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
Joe Perches6d31c2f2014-09-21 10:55:06 -070058 const char *fmt, ...)
59{
60 struct va_format vaf;
61 va_list args;
62 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000063
Joe Perches6d31c2f2014-09-21 10:55:06 -070064 va_start(args, fmt);
65
66 vaf.fmt = fmt;
67 vaf.va = &args;
68
Wei Yang781a8682015-03-25 16:23:57 +080069 if (pe->flags & PNV_IODA_PE_DEV)
Joe Perches6d31c2f2014-09-21 10:55:06 -070070 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
Wei Yang781a8682015-03-25 16:23:57 +080071 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Joe Perches6d31c2f2014-09-21 10:55:06 -070072 sprintf(pfix, "%04x:%02x ",
73 pci_domain_nr(pe->pbus), pe->pbus->number);
Wei Yang781a8682015-03-25 16:23:57 +080074#ifdef CONFIG_PCI_IOV
75 else if (pe->flags & PNV_IODA_PE_VF)
76 sprintf(pfix, "%04x:%02x:%2x.%d",
77 pci_domain_nr(pe->parent_dev->bus),
78 (pe->rid & 0xff00) >> 8,
79 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
80#endif /* CONFIG_PCI_IOV*/
Joe Perches6d31c2f2014-09-21 10:55:06 -070081
Russell Currey1f52f172016-11-16 14:02:15 +110082 printk("%spci %s: [PE# %.2x] %pV",
Joe Perches6d31c2f2014-09-21 10:55:06 -070083 level, pfix, pe->pe_number, &vaf);
84
85 va_end(args);
86}
87
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020088static bool pnv_iommu_bypass_disabled __read_mostly;
Guilherme G. Piccoli45baee12017-11-17 16:58:59 -020089static bool pci_reset_phbs __read_mostly;
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020090
91static int __init iommu_setup(char *str)
92{
93 if (!str)
94 return -EINVAL;
95
96 while (*str) {
97 if (!strncmp(str, "nobypass", 8)) {
98 pnv_iommu_bypass_disabled = true;
99 pr_info("PowerNV: IOMMU bypass window disabled.\n");
100 break;
101 }
102 str += strcspn(str, ",");
103 if (*str == ',')
104 str++;
105 }
106
107 return 0;
108}
109early_param("iommu", iommu_setup);
110
Guilherme G. Piccoli45baee12017-11-17 16:58:59 -0200111static int __init pci_reset_phbs_setup(char *str)
112{
113 pci_reset_phbs = true;
114 return 0;
115}
116
117early_param("ppc_pci_reset_phbs", pci_reset_phbs_setup);
118
Benjamin Herrenschmidt5958d192016-07-08 15:55:43 +1000119static inline bool pnv_pci_is_m64(struct pnv_phb *phb, struct resource *r)
Guo Chao262af552014-07-21 14:42:30 +1000120{
Benjamin Herrenschmidt5958d192016-07-08 15:55:43 +1000121 /*
122 * WARNING: We cannot rely on the resource flags. The Linux PCI
123 * allocation code sometimes decides to put a 64-bit prefetchable
124 * BAR in the 32-bit window, so we have to compare the addresses.
125 *
126 * For simplicity we only test resource start.
127 */
128 return (r->start >= phb->ioda.m64_base &&
129 r->start < (phb->ioda.m64_base + phb->ioda.m64_size));
Guo Chao262af552014-07-21 14:42:30 +1000130}
131
Russell Curreyb79331a2016-09-14 16:37:17 +1000132static inline bool pnv_pci_is_m64_flags(unsigned long resource_flags)
133{
134 unsigned long flags = (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
135
136 return (resource_flags & flags) == flags;
137}
138
Gavin Shan1e916772016-05-03 15:41:36 +1000139static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
140{
Gavin Shan313483d2016-09-28 14:34:56 +1000141 s64 rc;
142
Gavin Shan1e916772016-05-03 15:41:36 +1000143 phb->ioda.pe_array[pe_no].phb = phb;
144 phb->ioda.pe_array[pe_no].pe_number = pe_no;
145
Gavin Shan313483d2016-09-28 14:34:56 +1000146 /*
147 * Clear the PE frozen state as it might be put into frozen state
148 * in the last PCI remove path. It's not harmful to do so when the
149 * PE is already in unfrozen state.
150 */
151 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
152 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
Russell Curreyd4791db2016-11-16 12:12:26 +1100153 if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
Russell Currey1f52f172016-11-16 14:02:15 +1100154 pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n",
Gavin Shan313483d2016-09-28 14:34:56 +1000155 __func__, rc, phb->hose->global_number, pe_no);
156
Gavin Shan1e916772016-05-03 15:41:36 +1000157 return &phb->ioda.pe_array[pe_no];
158}
159
Gavin Shan4b82ab12014-11-12 13:36:07 +1100160static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
161{
Gavin Shan92b8f132016-05-03 15:41:24 +1000162 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
Russell Currey1f52f172016-11-16 14:02:15 +1100163 pr_warn("%s: Invalid PE %x on PHB#%x\n",
Gavin Shan4b82ab12014-11-12 13:36:07 +1100164 __func__, pe_no, phb->hose->global_number);
165 return;
166 }
167
Gavin Shane9dc4d72015-06-19 12:26:16 +1000168 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
Russell Currey1f52f172016-11-16 14:02:15 +1100169 pr_debug("%s: PE %x was reserved on PHB#%x\n",
Gavin Shane9dc4d72015-06-19 12:26:16 +1000170 __func__, pe_no, phb->hose->global_number);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100171
Gavin Shan1e916772016-05-03 15:41:36 +1000172 pnv_ioda_init_pe(phb, pe_no);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100173}
174
Gavin Shan1e916772016-05-03 15:41:36 +1000175static struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000176{
Andrzej Hajda60964812016-08-17 12:03:05 +0200177 long pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000178
Gavin Shan9fcd6f42016-05-20 16:41:30 +1000179 for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
180 if (!test_and_set_bit(pe, phb->ioda.pe_alloc))
181 return pnv_ioda_init_pe(phb, pe);
182 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000183
Gavin Shan9fcd6f42016-05-20 16:41:30 +1000184 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000185}
186
Gavin Shan1e916772016-05-03 15:41:36 +1000187static void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000188{
Gavin Shan1e916772016-05-03 15:41:36 +1000189 struct pnv_phb *phb = pe->phb;
Gavin Shancaa58f82016-09-06 14:17:18 +1000190 unsigned int pe_num = pe->pe_number;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000191
Gavin Shan1e916772016-05-03 15:41:36 +1000192 WARN_ON(pe->pdev);
193
194 memset(pe, 0, sizeof(struct pnv_ioda_pe));
Gavin Shancaa58f82016-09-06 14:17:18 +1000195 clear_bit(pe_num, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000196}
197
Guo Chao262af552014-07-21 14:42:30 +1000198/* The default M64 BAR is shared by all PEs */
199static int pnv_ioda2_init_m64(struct pnv_phb *phb)
200{
201 const char *desc;
202 struct resource *r;
203 s64 rc;
204
205 /* Configure the default M64 BAR */
206 rc = opal_pci_set_phb_mem_window(phb->opal_id,
207 OPAL_M64_WINDOW_TYPE,
208 phb->ioda.m64_bar_idx,
209 phb->ioda.m64_base,
210 0, /* unused */
211 phb->ioda.m64_size);
212 if (rc != OPAL_SUCCESS) {
213 desc = "configuring";
214 goto fail;
215 }
216
217 /* Enable the default M64 BAR */
218 rc = opal_pci_phb_mmio_enable(phb->opal_id,
219 OPAL_M64_WINDOW_TYPE,
220 phb->ioda.m64_bar_idx,
221 OPAL_ENABLE_M64_SPLIT);
222 if (rc != OPAL_SUCCESS) {
223 desc = "enabling";
224 goto fail;
225 }
226
Guo Chao262af552014-07-21 14:42:30 +1000227 /*
Gavin Shan63803c32016-05-20 16:41:32 +1000228 * Exclude the segments for reserved and root bus PE, which
229 * are first or last two PEs.
Guo Chao262af552014-07-21 14:42:30 +1000230 */
231 r = &phb->hose->mem_resources[1];
Gavin Shan92b8f132016-05-03 15:41:24 +1000232 if (phb->ioda.reserved_pe_idx == 0)
Gavin Shan63803c32016-05-20 16:41:32 +1000233 r->start += (2 * phb->ioda.m64_segsize);
Gavin Shan92b8f132016-05-03 15:41:24 +1000234 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Gavin Shan63803c32016-05-20 16:41:32 +1000235 r->end -= (2 * phb->ioda.m64_segsize);
Guo Chao262af552014-07-21 14:42:30 +1000236 else
Russell Currey1f52f172016-11-16 14:02:15 +1100237 pr_warn(" Cannot strip M64 segment for reserved PE#%x\n",
Gavin Shan92b8f132016-05-03 15:41:24 +1000238 phb->ioda.reserved_pe_idx);
Guo Chao262af552014-07-21 14:42:30 +1000239
240 return 0;
241
242fail:
243 pr_warn(" Failure %lld %s M64 BAR#%d\n",
244 rc, desc, phb->ioda.m64_bar_idx);
245 opal_pci_phb_mmio_enable(phb->opal_id,
246 OPAL_M64_WINDOW_TYPE,
247 phb->ioda.m64_bar_idx,
248 OPAL_DISABLE_M64);
249 return -EIO;
250}
251
Gavin Shanc4306702016-05-03 15:41:30 +1000252static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
Gavin Shan96a2f922015-06-19 12:26:17 +1000253 unsigned long *pe_bitmap)
Guo Chao262af552014-07-21 14:42:30 +1000254{
Gavin Shan96a2f922015-06-19 12:26:17 +1000255 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
256 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000257 struct resource *r;
Gavin Shan96a2f922015-06-19 12:26:17 +1000258 resource_size_t base, sgsz, start, end;
259 int segno, i;
Guo Chao262af552014-07-21 14:42:30 +1000260
Gavin Shan96a2f922015-06-19 12:26:17 +1000261 base = phb->ioda.m64_base;
262 sgsz = phb->ioda.m64_segsize;
263 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
264 r = &pdev->resource[i];
Benjamin Herrenschmidt5958d192016-07-08 15:55:43 +1000265 if (!r->parent || !pnv_pci_is_m64(phb, r))
Gavin Shan96a2f922015-06-19 12:26:17 +1000266 continue;
Guo Chao262af552014-07-21 14:42:30 +1000267
Gavin Shan96a2f922015-06-19 12:26:17 +1000268 start = _ALIGN_DOWN(r->start - base, sgsz);
269 end = _ALIGN_UP(r->end - base, sgsz);
270 for (segno = start / sgsz; segno < end / sgsz; segno++) {
271 if (pe_bitmap)
272 set_bit(segno, pe_bitmap);
273 else
274 pnv_ioda_reserve_pe(phb, segno);
Guo Chao262af552014-07-21 14:42:30 +1000275 }
276 }
277}
278
Gavin Shan99451552016-05-05 12:02:13 +1000279static int pnv_ioda1_init_m64(struct pnv_phb *phb)
280{
281 struct resource *r;
282 int index;
283
284 /*
285 * There are 16 M64 BARs, each of which has 8 segments. So
286 * there are as many M64 segments as the maximum number of
287 * PEs, which is 128.
288 */
289 for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
290 unsigned long base, segsz = phb->ioda.m64_segsize;
291 int64_t rc;
292
293 base = phb->ioda.m64_base +
294 index * PNV_IODA1_M64_SEGS * segsz;
295 rc = opal_pci_set_phb_mem_window(phb->opal_id,
296 OPAL_M64_WINDOW_TYPE, index, base, 0,
297 PNV_IODA1_M64_SEGS * segsz);
298 if (rc != OPAL_SUCCESS) {
Russell Currey1f52f172016-11-16 14:02:15 +1100299 pr_warn(" Error %lld setting M64 PHB#%x-BAR#%d\n",
Gavin Shan99451552016-05-05 12:02:13 +1000300 rc, phb->hose->global_number, index);
301 goto fail;
302 }
303
304 rc = opal_pci_phb_mmio_enable(phb->opal_id,
305 OPAL_M64_WINDOW_TYPE, index,
306 OPAL_ENABLE_M64_SPLIT);
307 if (rc != OPAL_SUCCESS) {
Russell Currey1f52f172016-11-16 14:02:15 +1100308 pr_warn(" Error %lld enabling M64 PHB#%x-BAR#%d\n",
Gavin Shan99451552016-05-05 12:02:13 +1000309 rc, phb->hose->global_number, index);
310 goto fail;
311 }
312 }
313
314 /*
Gavin Shan63803c32016-05-20 16:41:32 +1000315 * Exclude the segments for reserved and root bus PE, which
316 * are first or last two PEs.
Gavin Shan99451552016-05-05 12:02:13 +1000317 */
318 r = &phb->hose->mem_resources[1];
319 if (phb->ioda.reserved_pe_idx == 0)
Gavin Shan63803c32016-05-20 16:41:32 +1000320 r->start += (2 * phb->ioda.m64_segsize);
Gavin Shan99451552016-05-05 12:02:13 +1000321 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Gavin Shan63803c32016-05-20 16:41:32 +1000322 r->end -= (2 * phb->ioda.m64_segsize);
Gavin Shan99451552016-05-05 12:02:13 +1000323 else
Russell Currey1f52f172016-11-16 14:02:15 +1100324 WARN(1, "Wrong reserved PE#%x on PHB#%x\n",
Gavin Shan99451552016-05-05 12:02:13 +1000325 phb->ioda.reserved_pe_idx, phb->hose->global_number);
326
327 return 0;
328
329fail:
330 for ( ; index >= 0; index--)
331 opal_pci_phb_mmio_enable(phb->opal_id,
332 OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
333
334 return -EIO;
335}
336
Gavin Shanc4306702016-05-03 15:41:30 +1000337static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
338 unsigned long *pe_bitmap,
339 bool all)
Guo Chao262af552014-07-21 14:42:30 +1000340{
Guo Chao262af552014-07-21 14:42:30 +1000341 struct pci_dev *pdev;
Gavin Shan96a2f922015-06-19 12:26:17 +1000342
343 list_for_each_entry(pdev, &bus->devices, bus_list) {
Gavin Shanc4306702016-05-03 15:41:30 +1000344 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
Gavin Shan96a2f922015-06-19 12:26:17 +1000345
346 if (all && pdev->subordinate)
Gavin Shanc4306702016-05-03 15:41:30 +1000347 pnv_ioda_reserve_m64_pe(pdev->subordinate,
348 pe_bitmap, all);
Gavin Shan96a2f922015-06-19 12:26:17 +1000349 }
350}
351
Gavin Shan1e916772016-05-03 15:41:36 +1000352static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
Guo Chao262af552014-07-21 14:42:30 +1000353{
Gavin Shan26ba2482015-06-19 12:26:19 +1000354 struct pci_controller *hose = pci_bus_to_host(bus);
355 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000356 struct pnv_ioda_pe *master_pe, *pe;
357 unsigned long size, *pe_alloc;
Gavin Shan26ba2482015-06-19 12:26:19 +1000358 int i;
Guo Chao262af552014-07-21 14:42:30 +1000359
360 /* Root bus shouldn't use M64 */
361 if (pci_is_root_bus(bus))
Gavin Shan1e916772016-05-03 15:41:36 +1000362 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000363
Guo Chao262af552014-07-21 14:42:30 +1000364 /* Allocate bitmap */
Gavin Shan92b8f132016-05-03 15:41:24 +1000365 size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
Guo Chao262af552014-07-21 14:42:30 +1000366 pe_alloc = kzalloc(size, GFP_KERNEL);
367 if (!pe_alloc) {
368 pr_warn("%s: Out of memory !\n",
369 __func__);
Gavin Shan1e916772016-05-03 15:41:36 +1000370 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000371 }
372
Gavin Shan26ba2482015-06-19 12:26:19 +1000373 /* Figure out reserved PE numbers by the PE */
Gavin Shanc4306702016-05-03 15:41:30 +1000374 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
Guo Chao262af552014-07-21 14:42:30 +1000375
376 /*
377 * the current bus might not own M64 window and that's all
378 * contributed by its child buses. For the case, we needn't
379 * pick M64 dependent PE#.
380 */
Gavin Shan92b8f132016-05-03 15:41:24 +1000381 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
Guo Chao262af552014-07-21 14:42:30 +1000382 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000383 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000384 }
385
386 /*
387 * Figure out the master PE and put all slave PEs to master
388 * PE's list to form compound PE.
389 */
Guo Chao262af552014-07-21 14:42:30 +1000390 master_pe = NULL;
391 i = -1;
Gavin Shan92b8f132016-05-03 15:41:24 +1000392 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
393 phb->ioda.total_pe_num) {
Guo Chao262af552014-07-21 14:42:30 +1000394 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000395
Gavin Shan93289d82016-05-03 15:41:29 +1000396 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
Guo Chao262af552014-07-21 14:42:30 +1000397 if (!master_pe) {
398 pe->flags |= PNV_IODA_PE_MASTER;
399 INIT_LIST_HEAD(&pe->slaves);
400 master_pe = pe;
401 } else {
402 pe->flags |= PNV_IODA_PE_SLAVE;
403 pe->master = master_pe;
404 list_add_tail(&pe->list, &master_pe->slaves);
405 }
Gavin Shan99451552016-05-05 12:02:13 +1000406
407 /*
408 * P7IOC supports M64DT, which helps mapping M64 segment
409 * to one particular PE#. However, PHB3 has fixed mapping
410 * between M64 segment and PE#. In order to have same logic
411 * for P7IOC and PHB3, we enforce fixed mapping between M64
412 * segment and PE# on P7IOC.
413 */
414 if (phb->type == PNV_PHB_IODA1) {
415 int64_t rc;
416
417 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
418 pe->pe_number, OPAL_M64_WINDOW_TYPE,
419 pe->pe_number / PNV_IODA1_M64_SEGS,
420 pe->pe_number % PNV_IODA1_M64_SEGS);
421 if (rc != OPAL_SUCCESS)
Russell Currey1f52f172016-11-16 14:02:15 +1100422 pr_warn("%s: Error %lld mapping M64 for PHB#%x-PE#%x\n",
Gavin Shan99451552016-05-05 12:02:13 +1000423 __func__, rc, phb->hose->global_number,
424 pe->pe_number);
425 }
Guo Chao262af552014-07-21 14:42:30 +1000426 }
427
428 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000429 return master_pe;
Guo Chao262af552014-07-21 14:42:30 +1000430}
431
432static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
433{
434 struct pci_controller *hose = phb->hose;
435 struct device_node *dn = hose->dn;
436 struct resource *res;
Benjamin Herrenschmidta1339fa2016-07-08 16:37:16 +1000437 u32 m64_range[2], i;
Gavin Shan0e7736c2016-08-02 14:10:35 +1000438 const __be32 *r;
Guo Chao262af552014-07-21 14:42:30 +1000439 u64 pci_addr;
440
Gavin Shan99451552016-05-05 12:02:13 +1000441 if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
Gavin Shan1665c4a2014-11-12 13:36:04 +1100442 pr_info(" Not support M64 window\n");
443 return;
444 }
445
Stewart Smithe4d54f72015-12-09 17:18:20 +1100446 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
Guo Chao262af552014-07-21 14:42:30 +1000447 pr_info(" Firmware too old to support M64 window\n");
448 return;
449 }
450
451 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
452 if (!r) {
Rob Herringb7c670d2017-08-21 10:16:47 -0500453 pr_info(" No <ibm,opal-m64-window> on %pOF\n",
454 dn);
Guo Chao262af552014-07-21 14:42:30 +1000455 return;
456 }
457
Benjamin Herrenschmidta1339fa2016-07-08 16:37:16 +1000458 /*
459 * Find the available M64 BAR range and pickup the last one for
460 * covering the whole 64-bits space. We support only one range.
461 */
462 if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges",
463 m64_range, 2)) {
464 /* In absence of the property, assume 0..15 */
465 m64_range[0] = 0;
466 m64_range[1] = 16;
467 }
468 /* We only support 64 bits in our allocator */
469 if (m64_range[1] > 63) {
470 pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",
471 __func__, m64_range[1], phb->hose->global_number);
472 m64_range[1] = 63;
473 }
474 /* Empty range, no m64 */
475 if (m64_range[1] <= m64_range[0]) {
476 pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",
477 __func__, phb->hose->global_number);
478 return;
479 }
480
481 /* Configure M64 informations */
Guo Chao262af552014-07-21 14:42:30 +1000482 res = &hose->mem_resources[1];
Gavin Shane80c4e72015-10-22 12:03:08 +1100483 res->name = dn->full_name;
Guo Chao262af552014-07-21 14:42:30 +1000484 res->start = of_translate_address(dn, r + 2);
485 res->end = res->start + of_read_number(r + 4, 2) - 1;
486 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
487 pci_addr = of_read_number(r, 2);
488 hose->mem_offset[1] = res->start - pci_addr;
489
490 phb->ioda.m64_size = resource_size(res);
Gavin Shan92b8f132016-05-03 15:41:24 +1000491 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
Guo Chao262af552014-07-21 14:42:30 +1000492 phb->ioda.m64_base = pci_addr;
493
Benjamin Herrenschmidta1339fa2016-07-08 16:37:16 +1000494 /* This lines up nicely with the display from processing OF ranges */
495 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",
496 res->start, res->end, pci_addr, m64_range[0],
497 m64_range[0] + m64_range[1] - 1);
498
499 /* Mark all M64 used up by default */
500 phb->ioda.m64_bar_alloc = (unsigned long)-1;
Wei Yange9863e62014-12-12 12:39:37 +0800501
Guo Chao262af552014-07-21 14:42:30 +1000502 /* Use last M64 BAR to cover M64 window */
Benjamin Herrenschmidta1339fa2016-07-08 16:37:16 +1000503 m64_range[1]--;
504 phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1];
505
506 pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx);
507
508 /* Mark remaining ones free */
509 for (i = m64_range[0]; i < m64_range[1]; i++)
510 clear_bit(i, &phb->ioda.m64_bar_alloc);
511
512 /*
513 * Setup init functions for M64 based on IODA version, IODA3 uses
514 * the IODA2 code.
515 */
Gavin Shan99451552016-05-05 12:02:13 +1000516 if (phb->type == PNV_PHB_IODA1)
517 phb->init_m64 = pnv_ioda1_init_m64;
518 else
519 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shanc4306702016-05-03 15:41:30 +1000520 phb->reserve_m64_pe = pnv_ioda_reserve_m64_pe;
521 phb->pick_m64_pe = pnv_ioda_pick_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000522}
523
Gavin Shan49dec922014-07-21 14:42:33 +1000524static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
525{
526 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
527 struct pnv_ioda_pe *slave;
528 s64 rc;
529
530 /* Fetch master PE */
531 if (pe->flags & PNV_IODA_PE_SLAVE) {
532 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100533 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
534 return;
535
Gavin Shan49dec922014-07-21 14:42:33 +1000536 pe_no = pe->pe_number;
537 }
538
539 /* Freeze master PE */
540 rc = opal_pci_eeh_freeze_set(phb->opal_id,
541 pe_no,
542 OPAL_EEH_ACTION_SET_FREEZE_ALL);
543 if (rc != OPAL_SUCCESS) {
544 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
545 __func__, rc, phb->hose->global_number, pe_no);
546 return;
547 }
548
549 /* Freeze slave PEs */
550 if (!(pe->flags & PNV_IODA_PE_MASTER))
551 return;
552
553 list_for_each_entry(slave, &pe->slaves, list) {
554 rc = opal_pci_eeh_freeze_set(phb->opal_id,
555 slave->pe_number,
556 OPAL_EEH_ACTION_SET_FREEZE_ALL);
557 if (rc != OPAL_SUCCESS)
558 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
559 __func__, rc, phb->hose->global_number,
560 slave->pe_number);
561 }
562}
563
Anton Blancharde51df2c2014-08-20 08:55:18 +1000564static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000565{
566 struct pnv_ioda_pe *pe, *slave;
567 s64 rc;
568
569 /* Find master PE */
570 pe = &phb->ioda.pe_array[pe_no];
571 if (pe->flags & PNV_IODA_PE_SLAVE) {
572 pe = pe->master;
573 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
574 pe_no = pe->pe_number;
575 }
576
577 /* Clear frozen state for master PE */
578 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
579 if (rc != OPAL_SUCCESS) {
580 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
581 __func__, rc, opt, phb->hose->global_number, pe_no);
582 return -EIO;
583 }
584
585 if (!(pe->flags & PNV_IODA_PE_MASTER))
586 return 0;
587
588 /* Clear frozen state for slave PEs */
589 list_for_each_entry(slave, &pe->slaves, list) {
590 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
591 slave->pe_number,
592 opt);
593 if (rc != OPAL_SUCCESS) {
594 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
595 __func__, rc, opt, phb->hose->global_number,
596 slave->pe_number);
597 return -EIO;
598 }
599 }
600
601 return 0;
602}
603
604static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
605{
606 struct pnv_ioda_pe *slave, *pe;
607 u8 fstate, state;
608 __be16 pcierr;
609 s64 rc;
610
611 /* Sanity check on PE number */
Gavin Shan92b8f132016-05-03 15:41:24 +1000612 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
Gavin Shan49dec922014-07-21 14:42:33 +1000613 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
614
615 /*
616 * Fetch the master PE and the PE instance might be
617 * not initialized yet.
618 */
619 pe = &phb->ioda.pe_array[pe_no];
620 if (pe->flags & PNV_IODA_PE_SLAVE) {
621 pe = pe->master;
622 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
623 pe_no = pe->pe_number;
624 }
625
626 /* Check the master PE */
627 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
628 &state, &pcierr, NULL);
629 if (rc != OPAL_SUCCESS) {
630 pr_warn("%s: Failure %lld getting "
631 "PHB#%x-PE#%x state\n",
632 __func__, rc,
633 phb->hose->global_number, pe_no);
634 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
635 }
636
637 /* Check the slave PE */
638 if (!(pe->flags & PNV_IODA_PE_MASTER))
639 return state;
640
641 list_for_each_entry(slave, &pe->slaves, list) {
642 rc = opal_pci_eeh_freeze_status(phb->opal_id,
643 slave->pe_number,
644 &fstate,
645 &pcierr,
646 NULL);
647 if (rc != OPAL_SUCCESS) {
648 pr_warn("%s: Failure %lld getting "
649 "PHB#%x-PE#%x state\n",
650 __func__, rc,
651 phb->hose->global_number, slave->pe_number);
652 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
653 }
654
655 /*
656 * Override the result based on the ascending
657 * priority.
658 */
659 if (fstate > state)
660 state = fstate;
661 }
662
663 return state;
664}
665
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000666/* Currently those 2 are only used when MSIs are enabled, this will change
667 * but in the meantime, we need to protect them to avoid warnings
668 */
669#ifdef CONFIG_PCI_MSI
Ian Munsief4568342016-07-14 07:17:00 +1000670struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000671{
672 struct pci_controller *hose = pci_bus_to_host(dev->bus);
673 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000674 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000675
676 if (!pdn)
677 return NULL;
678 if (pdn->pe_number == IODA_INVALID_PE)
679 return NULL;
680 return &phb->ioda.pe_array[pdn->pe_number];
681}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000682#endif /* CONFIG_PCI_MSI */
683
Gavin Shanb131a842014-11-12 13:36:08 +1100684static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
685 struct pnv_ioda_pe *parent,
686 struct pnv_ioda_pe *child,
687 bool is_add)
688{
689 const char *desc = is_add ? "adding" : "removing";
690 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
691 OPAL_REMOVE_PE_FROM_DOMAIN;
692 struct pnv_ioda_pe *slave;
693 long rc;
694
695 /* Parent PE affects child PE */
696 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
697 child->pe_number, op);
698 if (rc != OPAL_SUCCESS) {
699 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
700 rc, desc);
701 return -ENXIO;
702 }
703
704 if (!(child->flags & PNV_IODA_PE_MASTER))
705 return 0;
706
707 /* Compound case: parent PE affects slave PEs */
708 list_for_each_entry(slave, &child->slaves, list) {
709 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
710 slave->pe_number, op);
711 if (rc != OPAL_SUCCESS) {
712 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
713 rc, desc);
714 return -ENXIO;
715 }
716 }
717
718 return 0;
719}
720
721static int pnv_ioda_set_peltv(struct pnv_phb *phb,
722 struct pnv_ioda_pe *pe,
723 bool is_add)
724{
725 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800726 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100727 int ret;
728
729 /*
730 * Clear PE frozen state. If it's master PE, we need
731 * clear slave PE frozen state as well.
732 */
733 if (is_add) {
734 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
735 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
736 if (pe->flags & PNV_IODA_PE_MASTER) {
737 list_for_each_entry(slave, &pe->slaves, list)
738 opal_pci_eeh_freeze_clear(phb->opal_id,
739 slave->pe_number,
740 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
741 }
742 }
743
744 /*
745 * Associate PE in PELT. We need add the PE into the
746 * corresponding PELT-V as well. Otherwise, the error
747 * originated from the PE might contribute to other
748 * PEs.
749 */
750 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
751 if (ret)
752 return ret;
753
754 /* For compound PEs, any one affects all of them */
755 if (pe->flags & PNV_IODA_PE_MASTER) {
756 list_for_each_entry(slave, &pe->slaves, list) {
757 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
758 if (ret)
759 return ret;
760 }
761 }
762
763 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
764 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800765 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100766 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800767#ifdef CONFIG_PCI_IOV
768 else if (pe->flags & PNV_IODA_PE_VF)
Gavin Shan283e2d82015-06-22 13:45:47 +1000769 pdev = pe->parent_dev;
Wei Yang781a8682015-03-25 16:23:57 +0800770#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100771 while (pdev) {
772 struct pci_dn *pdn = pci_get_pdn(pdev);
773 struct pnv_ioda_pe *parent;
774
775 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
776 parent = &phb->ioda.pe_array[pdn->pe_number];
777 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
778 if (ret)
779 return ret;
780 }
781
782 pdev = pdev->bus->self;
783 }
784
785 return 0;
786}
787
Wei Yang781a8682015-03-25 16:23:57 +0800788static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
789{
790 struct pci_dev *parent;
791 uint8_t bcomp, dcomp, fcomp;
792 int64_t rc;
793 long rid_end, rid;
794
795 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
796 if (pe->pbus) {
797 int count;
798
799 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
800 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
801 parent = pe->pbus->self;
802 if (pe->flags & PNV_IODA_PE_BUS_ALL)
803 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
804 else
805 count = 1;
806
807 switch(count) {
808 case 1: bcomp = OpalPciBusAll; break;
809 case 2: bcomp = OpalPciBus7Bits; break;
810 case 4: bcomp = OpalPciBus6Bits; break;
811 case 8: bcomp = OpalPciBus5Bits; break;
812 case 16: bcomp = OpalPciBus4Bits; break;
813 case 32: bcomp = OpalPciBus3Bits; break;
814 default:
815 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
816 count);
817 /* Do an exact match only */
818 bcomp = OpalPciBusAll;
819 }
820 rid_end = pe->rid + (count << 8);
821 } else {
Gavin Shan93e01a52016-05-20 16:41:34 +1000822#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +0800823 if (pe->flags & PNV_IODA_PE_VF)
824 parent = pe->parent_dev;
825 else
Gavin Shan93e01a52016-05-20 16:41:34 +1000826#endif
Wei Yang781a8682015-03-25 16:23:57 +0800827 parent = pe->pdev->bus->self;
828 bcomp = OpalPciBusAll;
829 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
830 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
831 rid_end = pe->rid + 1;
832 }
833
834 /* Clear the reverse map */
835 for (rid = pe->rid; rid < rid_end; rid++)
Gavin Shanc1275622016-05-20 16:41:29 +1000836 phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
Wei Yang781a8682015-03-25 16:23:57 +0800837
838 /* Release from all parents PELT-V */
839 while (parent) {
840 struct pci_dn *pdn = pci_get_pdn(parent);
841 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
842 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
843 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
844 /* XXX What to do in case of error ? */
845 }
846 parent = parent->bus->self;
847 }
848
Gavin Shanf951e512015-06-23 17:01:13 +1000849 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
Wei Yang781a8682015-03-25 16:23:57 +0800850 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
851
852 /* Disassociate PE in PELT */
853 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
854 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
855 if (rc)
856 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
857 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
858 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
859 if (rc)
860 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
861
862 pe->pbus = NULL;
863 pe->pdev = NULL;
Gavin Shan93e01a52016-05-20 16:41:34 +1000864#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +0800865 pe->parent_dev = NULL;
Gavin Shan93e01a52016-05-20 16:41:34 +1000866#endif
Wei Yang781a8682015-03-25 16:23:57 +0800867
868 return 0;
869}
Wei Yang781a8682015-03-25 16:23:57 +0800870
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800871static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000872{
873 struct pci_dev *parent;
874 uint8_t bcomp, dcomp, fcomp;
875 long rc, rid_end, rid;
876
877 /* Bus validation ? */
878 if (pe->pbus) {
879 int count;
880
881 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
882 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
883 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000884 if (pe->flags & PNV_IODA_PE_BUS_ALL)
885 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
886 else
887 count = 1;
888
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000889 switch(count) {
890 case 1: bcomp = OpalPciBusAll; break;
891 case 2: bcomp = OpalPciBus7Bits; break;
892 case 4: bcomp = OpalPciBus6Bits; break;
893 case 8: bcomp = OpalPciBus5Bits; break;
894 case 16: bcomp = OpalPciBus4Bits; break;
895 case 32: bcomp = OpalPciBus3Bits; break;
896 default:
Wei Yang781a8682015-03-25 16:23:57 +0800897 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
898 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000899 /* Do an exact match only */
900 bcomp = OpalPciBusAll;
901 }
902 rid_end = pe->rid + (count << 8);
903 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800904#ifdef CONFIG_PCI_IOV
905 if (pe->flags & PNV_IODA_PE_VF)
906 parent = pe->parent_dev;
907 else
908#endif /* CONFIG_PCI_IOV */
909 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000910 bcomp = OpalPciBusAll;
911 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
912 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
913 rid_end = pe->rid + 1;
914 }
915
Gavin Shan631ad692013-11-04 16:32:46 +0800916 /*
917 * Associate PE in PELT. We need add the PE into the
918 * corresponding PELT-V as well. Otherwise, the error
919 * originated from the PE might contribute to other
920 * PEs.
921 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000922 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
923 bcomp, dcomp, fcomp, OPAL_MAP_PE);
924 if (rc) {
925 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
926 return -ENXIO;
927 }
Gavin Shan631ad692013-11-04 16:32:46 +0800928
Alistair Popple5d2aa712015-12-17 13:43:13 +1100929 /*
930 * Configure PELTV. NPUs don't have a PELTV table so skip
931 * configuration on them.
932 */
Frederic Barrat7f2c39e2018-01-23 12:31:36 +0100933 if (phb->type != PNV_PHB_NPU_NVLINK && phb->type != PNV_PHB_NPU_OCAPI)
Alistair Popple5d2aa712015-12-17 13:43:13 +1100934 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000935
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000936 /* Setup reverse map */
937 for (rid = pe->rid; rid < rid_end; rid++)
938 phb->ioda.pe_rmap[rid] = pe->pe_number;
939
940 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100941 if (phb->type != PNV_PHB_IODA1) {
942 pe->mve_number = 0;
943 goto out;
944 }
945
946 pe->mve_number = pe->pe_number;
947 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
948 if (rc != OPAL_SUCCESS) {
Russell Currey1f52f172016-11-16 14:02:15 +1100949 pe_err(pe, "OPAL error %ld setting up MVE %x\n",
Gavin Shan4773f762014-11-12 13:36:09 +1100950 rc, pe->mve_number);
951 pe->mve_number = -1;
952 } else {
953 rc = opal_pci_set_mve_enable(phb->opal_id,
954 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000955 if (rc) {
Russell Currey1f52f172016-11-16 14:02:15 +1100956 pe_err(pe, "OPAL error %ld enabling MVE %x\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000957 rc, pe->mve_number);
958 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000959 }
Gavin Shan4773f762014-11-12 13:36:09 +1100960 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000961
Gavin Shan4773f762014-11-12 13:36:09 +1100962out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000963 return 0;
964}
965
Wei Yang781a8682015-03-25 16:23:57 +0800966#ifdef CONFIG_PCI_IOV
967static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
968{
969 struct pci_dn *pdn = pci_get_pdn(dev);
970 int i;
971 struct resource *res, res2;
972 resource_size_t size;
973 u16 num_vfs;
974
975 if (!dev->is_physfn)
976 return -EINVAL;
977
978 /*
979 * "offset" is in VFs. The M64 windows are sized so that when they
980 * are segmented, each segment is the same size as the IOV BAR.
981 * Each segment is in a separate PE, and the high order bits of the
982 * address are the PE number. Therefore, each VF's BAR is in a
983 * separate PE, and changing the IOV BAR start address changes the
984 * range of PEs the VFs are in.
985 */
986 num_vfs = pdn->num_vfs;
987 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
988 res = &dev->resource[i + PCI_IOV_RESOURCES];
989 if (!res->flags || !res->parent)
990 continue;
991
Wei Yang781a8682015-03-25 16:23:57 +0800992 /*
993 * The actual IOV BAR range is determined by the start address
994 * and the actual size for num_vfs VFs BAR. This check is to
995 * make sure that after shifting, the range will not overlap
996 * with another device.
997 */
998 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
999 res2.flags = res->flags;
1000 res2.start = res->start + (size * offset);
1001 res2.end = res2.start + (size * num_vfs) - 1;
1002
1003 if (res2.end > res->end) {
1004 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
1005 i, &res2, res, num_vfs, offset);
1006 return -EBUSY;
1007 }
1008 }
1009
1010 /*
Alexey Kardashevskiyd6f934f2017-09-27 16:52:31 +10001011 * Since M64 BAR shares segments among all possible 256 PEs,
1012 * we have to shift the beginning of PF IOV BAR to make it start from
1013 * the segment which belongs to the PE number assigned to the first VF.
1014 * This creates a "hole" in the /proc/iomem which could be used for
1015 * allocating other resources so we reserve this area below and
1016 * release when IOV is released.
Wei Yang781a8682015-03-25 16:23:57 +08001017 */
1018 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1019 res = &dev->resource[i + PCI_IOV_RESOURCES];
1020 if (!res->flags || !res->parent)
1021 continue;
1022
Wei Yang781a8682015-03-25 16:23:57 +08001023 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
1024 res2 = *res;
1025 res->start += size * offset;
1026
Wei Yang74703cc2015-07-20 18:14:58 +08001027 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
1028 i, &res2, res, (offset > 0) ? "En" : "Dis",
1029 num_vfs, offset);
Alexey Kardashevskiyd6f934f2017-09-27 16:52:31 +10001030
1031 if (offset < 0) {
1032 devm_release_resource(&dev->dev, &pdn->holes[i]);
1033 memset(&pdn->holes[i], 0, sizeof(pdn->holes[i]));
1034 }
1035
Wei Yang781a8682015-03-25 16:23:57 +08001036 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
Alexey Kardashevskiyd6f934f2017-09-27 16:52:31 +10001037
1038 if (offset > 0) {
1039 pdn->holes[i].start = res2.start;
1040 pdn->holes[i].end = res2.start + size * offset - 1;
1041 pdn->holes[i].flags = IORESOURCE_BUS;
1042 pdn->holes[i].name = "pnv_iov_reserved";
1043 devm_request_resource(&dev->dev, res->parent,
1044 &pdn->holes[i]);
1045 }
Wei Yang781a8682015-03-25 16:23:57 +08001046 }
1047 return 0;
1048}
1049#endif /* CONFIG_PCI_IOV */
1050
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001051static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001052{
1053 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1054 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001055 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001056 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001057
1058 if (!pdn) {
1059 pr_err("%s: Device tree node not associated properly\n",
1060 pci_name(dev));
1061 return NULL;
1062 }
1063 if (pdn->pe_number != IODA_INVALID_PE)
1064 return NULL;
1065
Gavin Shan1e916772016-05-03 15:41:36 +10001066 pe = pnv_ioda_alloc_pe(phb);
1067 if (!pe) {
Joe Perchesf2c2cbc2016-10-24 21:00:08 -07001068 pr_warn("%s: Not enough PE# available, disabling device\n",
1069 pci_name(dev));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001070 return NULL;
1071 }
1072
1073 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
1074 * pointer in the PE data structure, both should be destroyed at the
1075 * same time. However, this needs to be looked at more closely again
1076 * once we actually start removing things (Hotplug, SR-IOV, ...)
1077 *
1078 * At some point we want to remove the PDN completely anyways
1079 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001080 pci_dev_get(dev);
Gavin Shan1e916772016-05-03 15:41:36 +10001081 pdn->pe_number = pe->pe_number;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001082 pe->flags = PNV_IODA_PE_DEV;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001083 pe->pdev = dev;
1084 pe->pbus = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001085 pe->mve_number = -1;
1086 pe->rid = dev->bus->number << 8 | pdn->devfn;
1087
1088 pe_info(pe, "Associated device to PE\n");
1089
1090 if (pnv_ioda_configure_pe(phb, pe)) {
1091 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001092 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001093 pdn->pe_number = IODA_INVALID_PE;
1094 pe->pdev = NULL;
1095 pci_dev_put(dev);
1096 return NULL;
1097 }
1098
Alexey Kardashevskiy1d4e89c2016-05-12 15:47:10 +10001099 /* Put PE to the list */
1100 list_add_tail(&pe->list, &phb->ioda.pe_list);
1101
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001102 return pe;
1103}
1104
1105static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1106{
1107 struct pci_dev *dev;
1108
1109 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001110 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001111
1112 if (pdn == NULL) {
1113 pr_warn("%s: No device node associated with device !\n",
1114 pci_name(dev));
1115 continue;
1116 }
Gavin Shanccd1c192016-05-20 16:41:31 +10001117
1118 /*
1119 * In partial hotplug case, the PCI device might be still
1120 * associated with the PE and needn't attach it to the PE
1121 * again.
1122 */
1123 if (pdn->pe_number != IODA_INVALID_PE)
1124 continue;
1125
Gavin Shanc5f77002016-05-20 16:41:35 +10001126 pe->device_count++;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001127 pdn->pe_number = pe->pe_number;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001128 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001129 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1130 }
1131}
1132
Gavin Shanfb446ad2012-08-20 03:49:14 +00001133/*
1134 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1135 * single PCI bus. Another one that contains the primary PCI bus and its
1136 * subordinate PCI devices and buses. The second type of PE is normally
1137 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1138 */
Gavin Shan1e916772016-05-03 15:41:36 +10001139static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001140{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001141 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001142 struct pnv_phb *phb = hose->private_data;
Gavin Shan1e916772016-05-03 15:41:36 +10001143 struct pnv_ioda_pe *pe = NULL;
Gavin Shanccd1c192016-05-20 16:41:31 +10001144 unsigned int pe_num;
1145
1146 /*
1147 * In partial hotplug case, the PE instance might be still alive.
1148 * We should reuse it instead of allocating a new one.
1149 */
1150 pe_num = phb->ioda.pe_rmap[bus->number << 8];
1151 if (pe_num != IODA_INVALID_PE) {
1152 pe = &phb->ioda.pe_array[pe_num];
1153 pnv_ioda_setup_same_PE(bus, pe);
1154 return NULL;
1155 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001156
Gavin Shan63803c32016-05-20 16:41:32 +10001157 /* PE number for root bus should have been reserved */
1158 if (pci_is_root_bus(bus) &&
1159 phb->ioda.root_pe_idx != IODA_INVALID_PE)
1160 pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
1161
Guo Chao262af552014-07-21 14:42:30 +10001162 /* Check if PE is determined by M64 */
Gavin Shan63803c32016-05-20 16:41:32 +10001163 if (!pe && phb->pick_m64_pe)
Gavin Shan1e916772016-05-03 15:41:36 +10001164 pe = phb->pick_m64_pe(bus, all);
Guo Chao262af552014-07-21 14:42:30 +10001165
1166 /* The PE number isn't pinned by M64 */
Gavin Shan1e916772016-05-03 15:41:36 +10001167 if (!pe)
1168 pe = pnv_ioda_alloc_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +10001169
Gavin Shan1e916772016-05-03 15:41:36 +10001170 if (!pe) {
Joe Perchesf2c2cbc2016-10-24 21:00:08 -07001171 pr_warn("%s: Not enough PE# available for PCI bus %04x:%02x\n",
Gavin Shanfb446ad2012-08-20 03:49:14 +00001172 __func__, pci_domain_nr(bus), bus->number);
Gavin Shan1e916772016-05-03 15:41:36 +10001173 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001174 }
1175
Guo Chao262af552014-07-21 14:42:30 +10001176 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001177 pe->pbus = bus;
1178 pe->pdev = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001179 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001180 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001181
Gavin Shanfb446ad2012-08-20 03:49:14 +00001182 if (all)
Russell Currey1f52f172016-11-16 14:02:15 +11001183 pe_info(pe, "Secondary bus %d..%d associated with PE#%x\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001184 bus->busn_res.start, bus->busn_res.end, pe->pe_number);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001185 else
Russell Currey1f52f172016-11-16 14:02:15 +11001186 pe_info(pe, "Secondary bus %d associated with PE#%x\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001187 bus->busn_res.start, pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001188
1189 if (pnv_ioda_configure_pe(phb, pe)) {
1190 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001191 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001192 pe->pbus = NULL;
Gavin Shan1e916772016-05-03 15:41:36 +10001193 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001194 }
1195
1196 /* Associate it with all child devices */
1197 pnv_ioda_setup_same_PE(bus, pe);
1198
Gavin Shan7ebdf952012-08-20 03:49:15 +00001199 /* Put PE to the list */
1200 list_add_tail(&pe->list, &phb->ioda.pe_list);
Gavin Shan1e916772016-05-03 15:41:36 +10001201
1202 return pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001203}
1204
Alistair Poppleb5215492016-01-11 16:53:49 +11001205static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev)
Alistair Popple5d2aa712015-12-17 13:43:13 +11001206{
Alistair Poppleb5215492016-01-11 16:53:49 +11001207 int pe_num, found_pe = false, rc;
1208 long rid;
1209 struct pnv_ioda_pe *pe;
1210 struct pci_dev *gpu_pdev;
1211 struct pci_dn *npu_pdn;
1212 struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus);
1213 struct pnv_phb *phb = hose->private_data;
1214
1215 /*
1216 * Due to a hardware errata PE#0 on the NPU is reserved for
1217 * error handling. This means we only have three PEs remaining
1218 * which need to be assigned to four links, implying some
1219 * links must share PEs.
1220 *
1221 * To achieve this we assign PEs such that NPUs linking the
1222 * same GPU get assigned the same PE.
1223 */
1224 gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10001225 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
Alistair Poppleb5215492016-01-11 16:53:49 +11001226 pe = &phb->ioda.pe_array[pe_num];
1227 if (!pe->pdev)
1228 continue;
1229
1230 if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) {
1231 /*
1232 * This device has the same peer GPU so should
1233 * be assigned the same PE as the existing
1234 * peer NPU.
1235 */
1236 dev_info(&npu_pdev->dev,
Russell Currey1f52f172016-11-16 14:02:15 +11001237 "Associating to existing PE %x\n", pe_num);
Alistair Poppleb5215492016-01-11 16:53:49 +11001238 pci_dev_get(npu_pdev);
1239 npu_pdn = pci_get_pdn(npu_pdev);
1240 rid = npu_pdev->bus->number << 8 | npu_pdn->devfn;
Alistair Poppleb5215492016-01-11 16:53:49 +11001241 npu_pdn->pe_number = pe_num;
Alistair Poppleb5215492016-01-11 16:53:49 +11001242 phb->ioda.pe_rmap[rid] = pe->pe_number;
1243
1244 /* Map the PE to this link */
1245 rc = opal_pci_set_pe(phb->opal_id, pe_num, rid,
1246 OpalPciBusAll,
1247 OPAL_COMPARE_RID_DEVICE_NUMBER,
1248 OPAL_COMPARE_RID_FUNCTION_NUMBER,
1249 OPAL_MAP_PE);
1250 WARN_ON(rc != OPAL_SUCCESS);
1251 found_pe = true;
1252 break;
1253 }
1254 }
1255
1256 if (!found_pe)
1257 /*
1258 * Could not find an existing PE so allocate a new
1259 * one.
1260 */
1261 return pnv_ioda_setup_dev_PE(npu_pdev);
1262 else
1263 return pe;
1264}
1265
1266static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus)
1267{
Alistair Popple5d2aa712015-12-17 13:43:13 +11001268 struct pci_dev *pdev;
1269
1270 list_for_each_entry(pdev, &bus->devices, bus_list)
Alistair Poppleb5215492016-01-11 16:53:49 +11001271 pnv_ioda_setup_npu_PE(pdev);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001272}
1273
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001274static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001275{
1276 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001277 struct pnv_phb *phb;
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01001278 struct pci_bus *bus;
1279 struct pci_dev *pdev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001280
1281 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001282 phb = hose->private_data;
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01001283 if (phb->type == PNV_PHB_NPU_NVLINK) {
Alistair Popple08f48f32016-01-11 16:53:50 +11001284 /* PE#0 is needed for error reporting */
1285 pnv_ioda_reserve_pe(phb, 0);
Alistair Poppleb5215492016-01-11 16:53:49 +11001286 pnv_ioda_setup_npu_PEs(hose->bus);
Alistair Popple1ab66d12017-04-03 19:51:44 +10001287 if (phb->model == PNV_PHB_MODEL_NPU2)
1288 pnv_npu2_init(phb);
Gavin Shanccd1c192016-05-20 16:41:31 +10001289 }
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01001290 if (phb->type == PNV_PHB_NPU_OCAPI) {
1291 bus = hose->bus;
1292 list_for_each_entry(pdev, &bus->devices, bus_list)
1293 pnv_ioda_setup_dev_PE(pdev);
1294 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001295 }
1296}
1297
Gavin Shana8b2f822015-03-25 16:23:52 +08001298#ifdef CONFIG_PCI_IOV
Wei Yangee8222f2015-10-22 09:22:16 +08001299static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001300{
1301 struct pci_bus *bus;
1302 struct pci_controller *hose;
1303 struct pnv_phb *phb;
1304 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001305 int i, j;
Wei Yangee8222f2015-10-22 09:22:16 +08001306 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001307
1308 bus = pdev->bus;
1309 hose = pci_bus_to_host(bus);
1310 phb = hose->private_data;
1311 pdn = pci_get_pdn(pdev);
1312
Wei Yangee8222f2015-10-22 09:22:16 +08001313 if (pdn->m64_single_mode)
1314 m64_bars = num_vfs;
1315 else
1316 m64_bars = 1;
1317
Wei Yang02639b02015-03-25 16:23:59 +08001318 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yangee8222f2015-10-22 09:22:16 +08001319 for (j = 0; j < m64_bars; j++) {
1320 if (pdn->m64_map[j][i] == IODA_INVALID_M64)
Wei Yang02639b02015-03-25 16:23:59 +08001321 continue;
1322 opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001323 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0);
1324 clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc);
1325 pdn->m64_map[j][i] = IODA_INVALID_M64;
Wei Yang02639b02015-03-25 16:23:59 +08001326 }
Wei Yang781a8682015-03-25 16:23:57 +08001327
Wei Yangee8222f2015-10-22 09:22:16 +08001328 kfree(pdn->m64_map);
Wei Yang781a8682015-03-25 16:23:57 +08001329 return 0;
1330}
1331
Wei Yang02639b02015-03-25 16:23:59 +08001332static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001333{
1334 struct pci_bus *bus;
1335 struct pci_controller *hose;
1336 struct pnv_phb *phb;
1337 struct pci_dn *pdn;
1338 unsigned int win;
1339 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001340 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001341 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001342 int total_vfs;
1343 resource_size_t size, start;
1344 int pe_num;
Wei Yangee8222f2015-10-22 09:22:16 +08001345 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001346
1347 bus = pdev->bus;
1348 hose = pci_bus_to_host(bus);
1349 phb = hose->private_data;
1350 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001351 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001352
Wei Yangee8222f2015-10-22 09:22:16 +08001353 if (pdn->m64_single_mode)
1354 m64_bars = num_vfs;
1355 else
1356 m64_bars = 1;
Wei Yang02639b02015-03-25 16:23:59 +08001357
Markus Elfringfb37e122016-08-24 22:26:37 +02001358 pdn->m64_map = kmalloc_array(m64_bars,
1359 sizeof(*pdn->m64_map),
1360 GFP_KERNEL);
Wei Yangee8222f2015-10-22 09:22:16 +08001361 if (!pdn->m64_map)
1362 return -ENOMEM;
1363 /* Initialize the m64_map to IODA_INVALID_M64 */
1364 for (i = 0; i < m64_bars ; i++)
1365 for (j = 0; j < PCI_SRIOV_NUM_BARS; j++)
1366 pdn->m64_map[i][j] = IODA_INVALID_M64;
1367
Wei Yang781a8682015-03-25 16:23:57 +08001368
1369 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1370 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1371 if (!res->flags || !res->parent)
1372 continue;
1373
Wei Yangee8222f2015-10-22 09:22:16 +08001374 for (j = 0; j < m64_bars; j++) {
Wei Yang02639b02015-03-25 16:23:59 +08001375 do {
1376 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1377 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001378
Wei Yang02639b02015-03-25 16:23:59 +08001379 if (win >= phb->ioda.m64_bar_idx + 1)
1380 goto m64_failed;
1381 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001382
Wei Yangee8222f2015-10-22 09:22:16 +08001383 pdn->m64_map[j][i] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001384
Wei Yangee8222f2015-10-22 09:22:16 +08001385 if (pdn->m64_single_mode) {
Wei Yang02639b02015-03-25 16:23:59 +08001386 size = pci_iov_resource_size(pdev,
1387 PCI_IOV_RESOURCES + i);
Wei Yang02639b02015-03-25 16:23:59 +08001388 start = res->start + size * j;
1389 } else {
1390 size = resource_size(res);
1391 start = res->start;
1392 }
1393
1394 /* Map the M64 here */
Wei Yangee8222f2015-10-22 09:22:16 +08001395 if (pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001396 pe_num = pdn->pe_num_map[j];
Wei Yang02639b02015-03-25 16:23:59 +08001397 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1398 pe_num, OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001399 pdn->m64_map[j][i], 0);
Wei Yang02639b02015-03-25 16:23:59 +08001400 }
1401
1402 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001403 OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001404 pdn->m64_map[j][i],
Wei Yang02639b02015-03-25 16:23:59 +08001405 start,
Wei Yang781a8682015-03-25 16:23:57 +08001406 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001407 size);
Wei Yang781a8682015-03-25 16:23:57 +08001408
Wei Yang02639b02015-03-25 16:23:59 +08001409
1410 if (rc != OPAL_SUCCESS) {
1411 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1412 win, rc);
1413 goto m64_failed;
1414 }
1415
Wei Yangee8222f2015-10-22 09:22:16 +08001416 if (pdn->m64_single_mode)
Wei Yang02639b02015-03-25 16:23:59 +08001417 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001418 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2);
Wei Yang02639b02015-03-25 16:23:59 +08001419 else
1420 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001421 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1);
Wei Yang02639b02015-03-25 16:23:59 +08001422
1423 if (rc != OPAL_SUCCESS) {
1424 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1425 win, rc);
1426 goto m64_failed;
1427 }
Wei Yang781a8682015-03-25 16:23:57 +08001428 }
1429 }
1430 return 0;
1431
1432m64_failed:
Wei Yangee8222f2015-10-22 09:22:16 +08001433 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001434 return -EBUSY;
1435}
1436
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001437static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1438 int num);
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001439
Wei Yang781a8682015-03-25 16:23:57 +08001440static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1441{
Wei Yang781a8682015-03-25 16:23:57 +08001442 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001443 int64_t rc;
1444
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001445 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001446 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001447 if (rc)
1448 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1449
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001450 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001451 if (pe->table_group.group) {
1452 iommu_group_put(pe->table_group.group);
1453 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001454 }
Alexey Kardashevskiye5afdf92017-03-22 15:21:50 +11001455 iommu_tce_table_put(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001456}
1457
Wei Yangee8222f2015-10-22 09:22:16 +08001458static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
Wei Yang781a8682015-03-25 16:23:57 +08001459{
1460 struct pci_bus *bus;
1461 struct pci_controller *hose;
1462 struct pnv_phb *phb;
1463 struct pnv_ioda_pe *pe, *pe_n;
1464 struct pci_dn *pdn;
1465
1466 bus = pdev->bus;
1467 hose = pci_bus_to_host(bus);
1468 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001469 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001470
1471 if (!pdev->is_physfn)
1472 return;
1473
Wei Yang781a8682015-03-25 16:23:57 +08001474 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1475 if (pe->parent_dev != pdev)
1476 continue;
1477
1478 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1479
1480 /* Remove from list */
1481 mutex_lock(&phb->ioda.pe_list_mutex);
1482 list_del(&pe->list);
1483 mutex_unlock(&phb->ioda.pe_list_mutex);
1484
1485 pnv_ioda_deconfigure_pe(phb, pe);
1486
Gavin Shan1e916772016-05-03 15:41:36 +10001487 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001488 }
1489}
1490
1491void pnv_pci_sriov_disable(struct pci_dev *pdev)
1492{
1493 struct pci_bus *bus;
1494 struct pci_controller *hose;
1495 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001496 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001497 struct pci_dn *pdn;
Wei Yangbe283ee2015-10-22 09:22:19 +08001498 u16 num_vfs, i;
Wei Yang781a8682015-03-25 16:23:57 +08001499
1500 bus = pdev->bus;
1501 hose = pci_bus_to_host(bus);
1502 phb = hose->private_data;
1503 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001504 num_vfs = pdn->num_vfs;
1505
1506 /* Release VF PEs */
Wei Yangee8222f2015-10-22 09:22:16 +08001507 pnv_ioda_release_vf_PE(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001508
1509 if (phb->type == PNV_PHB_IODA2) {
Wei Yangee8222f2015-10-22 09:22:16 +08001510 if (!pdn->m64_single_mode)
Wei Yangbe283ee2015-10-22 09:22:19 +08001511 pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001512
1513 /* Release M64 windows */
Wei Yangee8222f2015-10-22 09:22:16 +08001514 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001515
1516 /* Release PE numbers */
Wei Yangbe283ee2015-10-22 09:22:19 +08001517 if (pdn->m64_single_mode) {
1518 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001519 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1520 continue;
1521
1522 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1523 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001524 }
1525 } else
1526 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1527 /* Releasing pe_num_map */
1528 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001529 }
1530}
1531
1532static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1533 struct pnv_ioda_pe *pe);
1534static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1535{
1536 struct pci_bus *bus;
1537 struct pci_controller *hose;
1538 struct pnv_phb *phb;
1539 struct pnv_ioda_pe *pe;
1540 int pe_num;
1541 u16 vf_index;
1542 struct pci_dn *pdn;
1543
1544 bus = pdev->bus;
1545 hose = pci_bus_to_host(bus);
1546 phb = hose->private_data;
1547 pdn = pci_get_pdn(pdev);
1548
1549 if (!pdev->is_physfn)
1550 return;
1551
1552 /* Reserve PE for each VF */
1553 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001554 if (pdn->m64_single_mode)
1555 pe_num = pdn->pe_num_map[vf_index];
1556 else
1557 pe_num = *pdn->pe_num_map + vf_index;
Wei Yang781a8682015-03-25 16:23:57 +08001558
1559 pe = &phb->ioda.pe_array[pe_num];
1560 pe->pe_number = pe_num;
1561 pe->phb = phb;
1562 pe->flags = PNV_IODA_PE_VF;
1563 pe->pbus = NULL;
1564 pe->parent_dev = pdev;
Wei Yang781a8682015-03-25 16:23:57 +08001565 pe->mve_number = -1;
1566 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1567 pci_iov_virtfn_devfn(pdev, vf_index);
1568
Russell Currey1f52f172016-11-16 14:02:15 +11001569 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%x\n",
Wei Yang781a8682015-03-25 16:23:57 +08001570 hose->global_number, pdev->bus->number,
1571 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1572 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1573
1574 if (pnv_ioda_configure_pe(phb, pe)) {
1575 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001576 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001577 pe->pdev = NULL;
1578 continue;
1579 }
1580
Wei Yang781a8682015-03-25 16:23:57 +08001581 /* Put PE to the list */
1582 mutex_lock(&phb->ioda.pe_list_mutex);
1583 list_add_tail(&pe->list, &phb->ioda.pe_list);
1584 mutex_unlock(&phb->ioda.pe_list_mutex);
1585
1586 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1587 }
1588}
1589
1590int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1591{
1592 struct pci_bus *bus;
1593 struct pci_controller *hose;
1594 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001595 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001596 struct pci_dn *pdn;
1597 int ret;
Wei Yangbe283ee2015-10-22 09:22:19 +08001598 u16 i;
Wei Yang781a8682015-03-25 16:23:57 +08001599
1600 bus = pdev->bus;
1601 hose = pci_bus_to_host(bus);
1602 phb = hose->private_data;
1603 pdn = pci_get_pdn(pdev);
1604
1605 if (phb->type == PNV_PHB_IODA2) {
Wei Yangb0331852015-10-22 09:22:14 +08001606 if (!pdn->vfs_expanded) {
1607 dev_info(&pdev->dev, "don't support this SRIOV device"
1608 " with non 64bit-prefetchable IOV BAR\n");
1609 return -ENOSPC;
1610 }
1611
Wei Yangee8222f2015-10-22 09:22:16 +08001612 /*
1613 * When M64 BARs functions in Single PE mode, the number of VFs
1614 * could be enabled must be less than the number of M64 BARs.
1615 */
1616 if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) {
1617 dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n");
1618 return -EBUSY;
1619 }
1620
Wei Yangbe283ee2015-10-22 09:22:19 +08001621 /* Allocating pe_num_map */
1622 if (pdn->m64_single_mode)
Markus Elfringfb37e122016-08-24 22:26:37 +02001623 pdn->pe_num_map = kmalloc_array(num_vfs,
1624 sizeof(*pdn->pe_num_map),
1625 GFP_KERNEL);
Wei Yangbe283ee2015-10-22 09:22:19 +08001626 else
1627 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL);
1628
1629 if (!pdn->pe_num_map)
1630 return -ENOMEM;
1631
1632 if (pdn->m64_single_mode)
1633 for (i = 0; i < num_vfs; i++)
1634 pdn->pe_num_map[i] = IODA_INVALID_PE;
1635
Wei Yang781a8682015-03-25 16:23:57 +08001636 /* Calculate available PE for required VFs */
Wei Yangbe283ee2015-10-22 09:22:19 +08001637 if (pdn->m64_single_mode) {
1638 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001639 pe = pnv_ioda_alloc_pe(phb);
1640 if (!pe) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001641 ret = -EBUSY;
1642 goto m64_failed;
1643 }
Gavin Shan1e916772016-05-03 15:41:36 +10001644
1645 pdn->pe_num_map[i] = pe->pe_number;
Wei Yangbe283ee2015-10-22 09:22:19 +08001646 }
1647 } else {
1648 mutex_lock(&phb->ioda.pe_alloc_mutex);
1649 *pdn->pe_num_map = bitmap_find_next_zero_area(
Gavin Shan92b8f132016-05-03 15:41:24 +10001650 phb->ioda.pe_alloc, phb->ioda.total_pe_num,
Wei Yangbe283ee2015-10-22 09:22:19 +08001651 0, num_vfs, 0);
Gavin Shan92b8f132016-05-03 15:41:24 +10001652 if (*pdn->pe_num_map >= phb->ioda.total_pe_num) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001653 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1654 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1655 kfree(pdn->pe_num_map);
1656 return -EBUSY;
1657 }
1658 bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001659 mutex_unlock(&phb->ioda.pe_alloc_mutex);
Wei Yang781a8682015-03-25 16:23:57 +08001660 }
Wei Yang781a8682015-03-25 16:23:57 +08001661 pdn->num_vfs = num_vfs;
Wei Yang781a8682015-03-25 16:23:57 +08001662
1663 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001664 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001665 if (ret) {
1666 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1667 goto m64_failed;
1668 }
1669
1670 /*
1671 * When using one M64 BAR to map one IOV BAR, we need to shift
1672 * the IOV BAR according to the PE# allocated to the VFs.
1673 * Otherwise, the PE# for the VF will conflict with others.
1674 */
Wei Yangee8222f2015-10-22 09:22:16 +08001675 if (!pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001676 ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map);
Wei Yang02639b02015-03-25 16:23:59 +08001677 if (ret)
1678 goto m64_failed;
1679 }
Wei Yang781a8682015-03-25 16:23:57 +08001680 }
1681
1682 /* Setup VF PEs */
1683 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1684
1685 return 0;
1686
1687m64_failed:
Wei Yangbe283ee2015-10-22 09:22:19 +08001688 if (pdn->m64_single_mode) {
1689 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001690 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1691 continue;
1692
1693 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1694 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001695 }
1696 } else
1697 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1698
1699 /* Releasing pe_num_map */
1700 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001701
1702 return ret;
1703}
1704
Bryant G. Ly988fc3b2017-11-09 08:00:33 -06001705int pnv_pcibios_sriov_disable(struct pci_dev *pdev)
Gavin Shana8b2f822015-03-25 16:23:52 +08001706{
Wei Yang781a8682015-03-25 16:23:57 +08001707 pnv_pci_sriov_disable(pdev);
1708
Gavin Shana8b2f822015-03-25 16:23:52 +08001709 /* Release PCI data */
1710 remove_dev_pci_data(pdev);
1711 return 0;
1712}
1713
Bryant G. Ly988fc3b2017-11-09 08:00:33 -06001714int pnv_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
Gavin Shana8b2f822015-03-25 16:23:52 +08001715{
1716 /* Allocate PCI data */
1717 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001718
Wei Yangee8222f2015-10-22 09:22:16 +08001719 return pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001720}
1721#endif /* CONFIG_PCI_IOV */
1722
Gavin Shan959c9bd2013-04-25 19:21:02 +00001723static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001724{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001725 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001726 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001727
Gavin Shan959c9bd2013-04-25 19:21:02 +00001728 /*
1729 * The function can be called while the PE#
1730 * hasn't been assigned. Do nothing for the
1731 * case.
1732 */
1733 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1734 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001735
Gavin Shan959c9bd2013-04-25 19:21:02 +00001736 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001737 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiy0e1ffef2015-08-27 16:01:16 +10001738 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001739 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001740 /*
1741 * Note: iommu_add_device() will fail here as
1742 * for physical PE: the device is already added by now;
1743 * for virtual PE: sysfs entries are not ready yet and
1744 * tce_iommu_bus_notifier will add the device to a group later.
1745 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001746}
1747
Russell Curreya0f98622017-06-21 17:18:03 +10001748static bool pnv_pci_ioda_pe_single_vendor(struct pnv_ioda_pe *pe)
1749{
1750 unsigned short vendor = 0;
1751 struct pci_dev *pdev;
1752
1753 if (pe->device_count == 1)
1754 return true;
1755
1756 /* pe->pdev should be set if it's a single device, pe->pbus if not */
1757 if (!pe->pbus)
1758 return true;
1759
1760 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
1761 if (!vendor) {
1762 vendor = pdev->vendor;
1763 continue;
1764 }
1765
1766 if (pdev->vendor != vendor)
1767 return false;
1768 }
1769
1770 return true;
1771}
1772
Russell Currey8e3f1b12017-06-21 17:18:04 +10001773/*
1774 * Reconfigure TVE#0 to be usable as 64-bit DMA space.
1775 *
1776 * The first 4GB of virtual memory for a PE is reserved for 32-bit accesses.
1777 * Devices can only access more than that if bit 59 of the PCI address is set
1778 * by hardware, which indicates TVE#1 should be used instead of TVE#0.
1779 * Many PCI devices are not capable of addressing that many bits, and as a
1780 * result are limited to the 4GB of virtual memory made available to 32-bit
1781 * devices in TVE#0.
1782 *
1783 * In order to work around this, reconfigure TVE#0 to be suitable for 64-bit
1784 * devices by configuring the virtual memory past the first 4GB inaccessible
1785 * by 64-bit DMAs. This should only be used by devices that want more than
1786 * 4GB, and only on PEs that have no 32-bit devices.
1787 *
1788 * Currently this will only work on PHB3 (POWER8).
1789 */
1790static int pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe *pe)
1791{
1792 u64 window_size, table_size, tce_count, addr;
1793 struct page *table_pages;
1794 u64 tce_order = 28; /* 256MB TCEs */
1795 __be64 *tces;
1796 s64 rc;
1797
1798 /*
1799 * Window size needs to be a power of two, but needs to account for
1800 * shifting memory by the 4GB offset required to skip 32bit space.
1801 */
1802 window_size = roundup_pow_of_two(memory_hotplug_max() + (1ULL << 32));
1803 tce_count = window_size >> tce_order;
1804 table_size = tce_count << 3;
1805
1806 if (table_size < PAGE_SIZE)
1807 table_size = PAGE_SIZE;
1808
1809 table_pages = alloc_pages_node(pe->phb->hose->node, GFP_KERNEL,
1810 get_order(table_size));
1811 if (!table_pages)
1812 goto err;
1813
1814 tces = page_address(table_pages);
1815 if (!tces)
1816 goto err;
1817
1818 memset(tces, 0, table_size);
1819
1820 for (addr = 0; addr < memory_hotplug_max(); addr += (1 << tce_order)) {
1821 tces[(addr + (1ULL << 32)) >> tce_order] =
1822 cpu_to_be64(addr | TCE_PCI_READ | TCE_PCI_WRITE);
1823 }
1824
1825 rc = opal_pci_map_pe_dma_window(pe->phb->opal_id,
1826 pe->pe_number,
1827 /* reconfigure window 0 */
1828 (pe->pe_number << 1) + 0,
1829 1,
1830 __pa(tces),
1831 table_size,
1832 1 << tce_order);
1833 if (rc == OPAL_SUCCESS) {
1834 pe_info(pe, "Using 64-bit DMA iommu bypass (through TVE#0)\n");
1835 return 0;
1836 }
1837err:
1838 pe_err(pe, "Error configuring 64-bit DMA bypass\n");
1839 return -EIO;
1840}
1841
Daniel Axtens763d2d82015-04-28 15:12:07 +10001842static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001843{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001844 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1845 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001846 struct pci_dn *pdn = pci_get_pdn(pdev);
1847 struct pnv_ioda_pe *pe;
1848 uint64_t top;
1849 bool bypass = false;
Russell Currey8e3f1b12017-06-21 17:18:04 +10001850 s64 rc;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001851
1852 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
Ingo Molnared7158b2018-02-22 10:54:55 +01001853 return -ENODEV;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001854
1855 pe = &phb->ioda.pe_array[pdn->pe_number];
1856 if (pe->tce_bypass_enabled) {
1857 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1858 bypass = (dma_mask >= top);
1859 }
1860
1861 if (bypass) {
1862 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
Christoph Hellwig2d9d6f62017-12-22 10:58:24 +01001863 set_dma_ops(&pdev->dev, &dma_nommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001864 } else {
Russell Currey8e3f1b12017-06-21 17:18:04 +10001865 /*
1866 * If the device can't set the TCE bypass bit but still wants
1867 * to access 4GB or more, on PHB3 we can reconfigure TVE#0 to
1868 * bypass the 32-bit region and be usable for 64-bit DMAs.
1869 * The device needs to be able to address all of this space.
1870 */
1871 if (dma_mask >> 32 &&
1872 dma_mask > (memory_hotplug_max() + (1ULL << 32)) &&
1873 pnv_pci_ioda_pe_single_vendor(pe) &&
1874 phb->model == PNV_PHB_MODEL_PHB3) {
1875 /* Configure the bypass mode */
1876 rc = pnv_pci_ioda_dma_64bit_bypass(pe);
1877 if (rc)
1878 return rc;
1879 /* 4GB offset bypasses 32-bit space */
1880 set_dma_offset(&pdev->dev, (1ULL << 32));
Christoph Hellwig2d9d6f62017-12-22 10:58:24 +01001881 set_dma_ops(&pdev->dev, &dma_nommu_ops);
Alistair Popple253fd512017-07-26 15:26:40 +10001882 } else if (dma_mask >> 32 && dma_mask != DMA_BIT_MASK(64)) {
1883 /*
1884 * Fail the request if a DMA mask between 32 and 64 bits
1885 * was requested but couldn't be fulfilled. Ideally we
1886 * would do this for 64-bits but historically we have
1887 * always fallen back to 32-bits.
1888 */
1889 return -ENOMEM;
Russell Currey8e3f1b12017-06-21 17:18:04 +10001890 } else {
1891 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1892 set_dma_ops(&pdev->dev, &dma_iommu_ops);
1893 }
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001894 }
Brian W Harta32305b2014-07-31 14:24:37 -05001895 *pdev->dev.dma_mask = dma_mask;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001896
1897 /* Update peer npu devices */
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10001898 pnv_npu_try_dma_set_bypass(pdev, bypass);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001899
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001900 return 0;
1901}
1902
Andrew Donnellan535229822015-08-07 13:45:54 +10001903static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001904{
Andrew Donnellan535229822015-08-07 13:45:54 +10001905 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1906 struct pnv_phb *phb = hose->private_data;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001907 struct pci_dn *pdn = pci_get_pdn(pdev);
1908 struct pnv_ioda_pe *pe;
1909 u64 end, mask;
1910
1911 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1912 return 0;
1913
1914 pe = &phb->ioda.pe_array[pdn->pe_number];
1915 if (!pe->tce_bypass_enabled)
1916 return __dma_get_required_mask(&pdev->dev);
1917
1918
1919 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1920 mask = 1ULL << (fls64(end) - 1);
1921 mask += mask - 1;
1922
1923 return mask;
1924}
1925
Gavin Shandff4a392014-07-15 17:00:55 +10001926static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiydb08e1d2017-02-21 13:41:31 +11001927 struct pci_bus *bus,
1928 bool add_to_group)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001929{
1930 struct pci_dev *dev;
1931
1932 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001933 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001934 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiydb08e1d2017-02-21 13:41:31 +11001935 if (add_to_group)
1936 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001937
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001938 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiydb08e1d2017-02-21 13:41:31 +11001939 pnv_ioda_setup_bus_dma(pe, dev->subordinate,
1940 add_to_group);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001941 }
1942}
1943
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001944static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb,
1945 bool real_mode)
1946{
1947 return real_mode ? (__be64 __iomem *)(phb->regs_phys + 0x210) :
1948 (phb->regs + 0x210);
1949}
1950
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001951static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table *tbl,
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001952 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001953{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001954 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1955 &tbl->it_group_list, struct iommu_table_group_link,
1956 next);
1957 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001958 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10001959 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00001960 unsigned long start, end, inc;
1961
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001962 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1963 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1964 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001965
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001966 /* p7ioc-style invalidation, 2 TCEs per write */
1967 start |= (1ull << 63);
1968 end |= (1ull << 63);
1969 inc = 16;
Gavin Shan4cce9552013-04-25 19:21:00 +00001970 end |= inc - 1; /* round up end to be different than start */
1971
1972 mb(); /* Ensure above stores are visible */
1973 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001974 if (rm)
Michael Ellerman001ff2e2018-05-14 22:50:32 +10001975 __raw_rm_writeq_be(start, invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001976 else
Michael Ellerman001ff2e2018-05-14 22:50:32 +10001977 __raw_writeq_be(start, invalidate);
1978
Gavin Shan4cce9552013-04-25 19:21:00 +00001979 start += inc;
1980 }
1981
1982 /*
1983 * The iommu layer will do another mb() for us on build()
1984 * and we don't care on free()
1985 */
1986}
1987
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001988static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1989 long npages, unsigned long uaddr,
1990 enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07001991 unsigned long attrs)
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001992{
1993 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1994 attrs);
1995
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10001996 if (!ret)
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10001997 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001998
1999 return ret;
2000}
2001
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002002#ifdef CONFIG_IOMMU_API
2003static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
2004 unsigned long *hpa, enum dma_data_direction *direction)
2005{
2006 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
2007
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10002008 if (!ret)
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002009 pnv_pci_p7ioc_tce_invalidate(tbl, index, 1, false);
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002010
2011 return ret;
2012}
Alexey Kardashevskiya540aa52017-03-22 15:21:48 +11002013
2014static int pnv_ioda1_tce_xchg_rm(struct iommu_table *tbl, long index,
2015 unsigned long *hpa, enum dma_data_direction *direction)
2016{
2017 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
2018
2019 if (!ret)
2020 pnv_pci_p7ioc_tce_invalidate(tbl, index, 1, true);
2021
2022 return ret;
2023}
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002024#endif
2025
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002026static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
2027 long npages)
2028{
2029 pnv_tce_free(tbl, index, npages);
2030
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10002031 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002032}
2033
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002034static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002035 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002036#ifdef CONFIG_IOMMU_API
2037 .exchange = pnv_ioda1_tce_xchg,
Alexey Kardashevskiya540aa52017-03-22 15:21:48 +11002038 .exchange_rm = pnv_ioda1_tce_xchg_rm,
Alexey Kardashevskiy090bad32018-07-04 16:13:47 +10002039 .useraddrptr = pnv_tce_useraddrptr,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002040#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002041 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002042 .get = pnv_tce_get,
2043};
2044
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002045#define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)
2046#define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)
2047#define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)
Alexey Kardashevskiybef92532016-04-29 18:55:17 +10002048
Alistair Popple6b3d12a2017-05-03 13:24:08 +10002049static void pnv_pci_phb3_tce_invalidate_entire(struct pnv_phb *phb, bool rm)
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10002050{
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10002051 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(phb, rm);
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002052 const unsigned long val = PHB3_TCE_KILL_INVAL_ALL;
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10002053
2054 mb(); /* Ensure previous TCE table stores are visible */
2055 if (rm)
Michael Ellerman001ff2e2018-05-14 22:50:32 +10002056 __raw_rm_writeq_be(val, invalidate);
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10002057 else
Michael Ellerman001ff2e2018-05-14 22:50:32 +10002058 __raw_writeq_be(val, invalidate);
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10002059}
2060
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002061static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002062{
2063 /* 01xb - invalidate TCEs that match the specified PE# */
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10002064 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, false);
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002065 unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002066
2067 mb(); /* Ensure above stores are visible */
Michael Ellerman001ff2e2018-05-14 22:50:32 +10002068 __raw_writeq_be(val, invalidate);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002069}
2070
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10002071static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe, bool rm,
2072 unsigned shift, unsigned long index,
2073 unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00002074{
Alexey Kardashevskiy4d902192016-08-03 18:40:45 +10002075 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
Gavin Shan4cce9552013-04-25 19:21:00 +00002076 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00002077
2078 /* We'll invalidate DMA address in PE scope */
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10002079 start = PHB3_TCE_KILL_INVAL_ONE;
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10002080 start |= (pe->pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00002081 end = start;
2082
2083 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002084 start |= (index << shift);
2085 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10002086 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00002087 mb();
2088
2089 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10002090 if (rm)
Michael Ellerman001ff2e2018-05-14 22:50:32 +10002091 __raw_rm_writeq_be(start, invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10002092 else
Michael Ellerman001ff2e2018-05-14 22:50:32 +10002093 __raw_writeq_be(start, invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00002094 start += inc;
2095 }
2096}
2097
Benjamin Herrenschmidtf0228c42016-07-08 16:37:15 +10002098static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
2099{
2100 struct pnv_phb *phb = pe->phb;
2101
2102 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
2103 pnv_pci_phb3_tce_invalidate_pe(pe);
2104 else
2105 opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE,
2106 pe->pe_number, 0, 0, 0);
2107}
2108
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10002109static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
2110 unsigned long index, unsigned long npages, bool rm)
2111{
2112 struct iommu_table_group_link *tgl;
2113
Alexey Kardashevskiya540aa52017-03-22 15:21:48 +11002114 list_for_each_entry_lockless(tgl, &tbl->it_group_list, next) {
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10002115 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
2116 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidtf0228c42016-07-08 16:37:15 +10002117 struct pnv_phb *phb = pe->phb;
2118 unsigned int shift = tbl->it_page_shift;
2119
Alistair Popple616badd2017-01-10 15:41:44 +11002120 /*
2121 * NVLink1 can use the TCE kill register directly as
2122 * it's the same as PHB3. NVLink2 is different and
2123 * should go via the OPAL call.
2124 */
2125 if (phb->model == PNV_PHB_MODEL_NPU) {
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10002126 /*
2127 * The NVLink hardware does not support TCE kill
2128 * per TCE entry so we have to invalidate
2129 * the entire cache for it.
2130 */
Benjamin Herrenschmidtf0228c42016-07-08 16:37:15 +10002131 pnv_pci_phb3_tce_invalidate_entire(phb, rm);
Alexey Kardashevskiy85674862016-04-29 18:55:23 +10002132 continue;
2133 }
Benjamin Herrenschmidtf0228c42016-07-08 16:37:15 +10002134 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
2135 pnv_pci_phb3_tce_invalidate(pe, rm, shift,
2136 index, npages);
Benjamin Herrenschmidtf0228c42016-07-08 16:37:15 +10002137 else
2138 opal_pci_tce_kill(phb->opal_id,
2139 OPAL_PCI_TCE_KILL_PAGES,
2140 pe->pe_number, 1u << shift,
2141 index << shift, npages);
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10002142 }
2143}
2144
Alistair Popple6b3d12a2017-05-03 13:24:08 +10002145void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_phb *phb, bool rm)
2146{
2147 if (phb->model == PNV_PHB_MODEL_NPU || phb->model == PNV_PHB_MODEL_PHB3)
2148 pnv_pci_phb3_tce_invalidate_entire(phb, rm);
2149 else
2150 opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL, 0, 0, 0, 0);
2151}
2152
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002153static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
2154 long npages, unsigned long uaddr,
2155 enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002156 unsigned long attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00002157{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002158 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
2159 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00002160
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10002161 if (!ret)
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002162 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
2163
2164 return ret;
2165}
2166
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002167#ifdef CONFIG_IOMMU_API
2168static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
2169 unsigned long *hpa, enum dma_data_direction *direction)
2170{
2171 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
2172
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10002173 if (!ret)
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002174 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
2175
2176 return ret;
2177}
Alexey Kardashevskiya540aa52017-03-22 15:21:48 +11002178
2179static int pnv_ioda2_tce_xchg_rm(struct iommu_table *tbl, long index,
2180 unsigned long *hpa, enum dma_data_direction *direction)
2181{
2182 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
2183
2184 if (!ret)
2185 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, true);
2186
2187 return ret;
2188}
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002189#endif
2190
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002191static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
2192 long npages)
2193{
2194 pnv_tce_free(tbl, index, npages);
2195
Benjamin Herrenschmidt08acce12016-07-08 16:37:13 +10002196 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00002197}
2198
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002199static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002200 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002201#ifdef CONFIG_IOMMU_API
2202 .exchange = pnv_ioda2_tce_xchg,
Alexey Kardashevskiya540aa52017-03-22 15:21:48 +11002203 .exchange_rm = pnv_ioda2_tce_xchg_rm,
Alexey Kardashevskiy090bad32018-07-04 16:13:47 +10002204 .useraddrptr = pnv_tce_useraddrptr,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002205#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002206 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002207 .get = pnv_tce_get,
Alexey Kardashevskiyda2bb0d2018-07-04 16:13:44 +10002208 .free = pnv_pci_ioda2_table_free_pages,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002209};
2210
Gavin Shan801846d2016-05-03 15:41:34 +10002211static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data)
2212{
2213 unsigned int *weight = (unsigned int *)data;
2214
2215 /* This is quite simplistic. The "base" weight of a device
2216 * is 10. 0 means no DMA is to be accounted for it.
2217 */
2218 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
2219 return 0;
2220
2221 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
2222 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
2223 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
2224 *weight += 3;
2225 else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
2226 *weight += 15;
2227 else
2228 *weight += 10;
2229
2230 return 0;
2231}
2232
2233static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe)
2234{
2235 unsigned int weight = 0;
2236
2237 /* SRIOV VF has same DMA32 weight as its PF */
2238#ifdef CONFIG_PCI_IOV
2239 if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) {
2240 pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight);
2241 return weight;
2242 }
2243#endif
2244
2245 if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) {
2246 pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight);
2247 } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) {
2248 struct pci_dev *pdev;
2249
2250 list_for_each_entry(pdev, &pe->pbus->devices, bus_list)
2251 pnv_pci_ioda_dev_dma_weight(pdev, &weight);
2252 } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) {
2253 pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight);
2254 }
2255
2256 return weight;
2257}
2258
Gavin Shanb30d9362016-05-03 15:41:32 +10002259static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
Gavin Shan2b923ed2016-05-05 12:04:16 +10002260 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002261{
2262
2263 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002264 struct iommu_table *tbl;
Gavin Shan2b923ed2016-05-05 12:04:16 +10002265 unsigned int weight, total_weight = 0;
2266 unsigned int tce32_segsz, base, segs, avail, i;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002267 int64_t rc;
2268 void *addr;
2269
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002270 /* XXX FIXME: Handle 64-bit only DMA devices */
2271 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
2272 /* XXX FIXME: Allocate multi-level tables on PHB3 */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002273 weight = pnv_pci_ioda_pe_dma_weight(pe);
2274 if (!weight)
2275 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002276
Gavin Shan2b923ed2016-05-05 12:04:16 +10002277 pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight,
2278 &total_weight);
2279 segs = (weight * phb->ioda.dma32_count) / total_weight;
2280 if (!segs)
2281 segs = 1;
2282
2283 /*
2284 * Allocate contiguous DMA32 segments. We begin with the expected
2285 * number of segments. With one more attempt, the number of DMA32
2286 * segments to be allocated is decreased by one until one segment
2287 * is allocated successfully.
2288 */
2289 do {
2290 for (base = 0; base <= phb->ioda.dma32_count - segs; base++) {
2291 for (avail = 0, i = base; i < base + segs; i++) {
2292 if (phb->ioda.dma32_segmap[i] ==
2293 IODA_INVALID_PE)
2294 avail++;
2295 }
2296
2297 if (avail == segs)
2298 goto found;
2299 }
2300 } while (--segs);
2301
2302 if (!segs) {
2303 pe_warn(pe, "No available DMA32 segments\n");
2304 return;
2305 }
2306
2307found:
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002308 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiy82eae1a2017-03-27 19:27:37 +11002309 if (WARN_ON(!tbl))
2310 return;
2311
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002312 iommu_register_group(&pe->table_group, phb->hose->global_number,
2313 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002314 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002315
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002316 /* Grab a 32-bit TCE table */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002317 pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
2318 weight, total_weight, base, segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002319 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
Gavin Shanacce9712016-05-03 15:41:33 +10002320 base * PNV_IODA1_DMA32_SEGSIZE,
2321 (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002322
2323 /* XXX Currently, we allocate one big contiguous table for the
2324 * TCEs. We only really need one chunk per 256M of TCE space
2325 * (ie per segment) but that's an optimization for later, it
2326 * requires some added smarts with our get/put_tce implementation
Gavin Shanacce9712016-05-03 15:41:33 +10002327 *
2328 * Each TCE page is 4KB in size and each TCE entry occupies 8
2329 * bytes
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002330 */
Gavin Shanacce9712016-05-03 15:41:33 +10002331 tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002332 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
Gavin Shanacce9712016-05-03 15:41:33 +10002333 get_order(tce32_segsz * segs));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002334 if (!tce_mem) {
2335 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
2336 goto fail;
2337 }
2338 addr = page_address(tce_mem);
Gavin Shanacce9712016-05-03 15:41:33 +10002339 memset(addr, 0, tce32_segsz * segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002340
2341 /* Configure HW */
2342 for (i = 0; i < segs; i++) {
2343 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2344 pe->pe_number,
2345 base + i, 1,
Gavin Shanacce9712016-05-03 15:41:33 +10002346 __pa(addr) + tce32_segsz * i,
2347 tce32_segsz, IOMMU_PAGE_SIZE_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002348 if (rc) {
2349 pe_err(pe, " Failed to configure 32-bit TCE table,"
2350 " err %ld\n", rc);
2351 goto fail;
2352 }
2353 }
2354
Gavin Shan2b923ed2016-05-05 12:04:16 +10002355 /* Setup DMA32 segment mapping */
2356 for (i = base; i < base + segs; i++)
2357 phb->ioda.dma32_segmap[i] = pe->pe_number;
2358
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002359 /* Setup linux iommu table */
Gavin Shanacce9712016-05-03 15:41:33 +10002360 pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs,
2361 base * PNV_IODA1_DMA32_SEGSIZE,
2362 IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002363
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002364 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002365 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
2366 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002367 iommu_init_table(tbl, phb->hose->node);
2368
Wei Yang781a8682015-03-25 16:23:57 +08002369 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002370 /*
2371 * Setting table base here only for carrying iommu_group
2372 * further down to let iommu_add_device() do the job.
2373 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2374 */
2375 set_iommu_table_base(&pe->pdev->dev, tbl);
2376 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002377 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiydb08e1d2017-02-21 13:41:31 +11002378 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10002379
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002380 return;
2381 fail:
2382 /* XXX Failure: Try to fallback to 64-bit only ? */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002383 if (tce_mem)
Gavin Shanacce9712016-05-03 15:41:33 +10002384 __free_pages(tce_mem, get_order(tce32_segsz * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002385 if (tbl) {
2386 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
Alexey Kardashevskiye5afdf92017-03-22 15:21:50 +11002387 iommu_tce_table_put(tbl);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002388 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002389}
2390
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002391static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
2392 int num, struct iommu_table *tbl)
2393{
2394 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2395 table_group);
2396 struct pnv_phb *phb = pe->phb;
2397 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002398 const unsigned long size = tbl->it_indirect_levels ?
2399 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002400 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
2401 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
2402
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002403 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002404 start_addr, start_addr + win_size - 1,
2405 IOMMU_PAGE_SIZE(tbl));
2406
2407 /*
2408 * Map TCE table through TVT. The TVE index is the PE number
2409 * shifted by 1 bit for 32-bits DMA space.
2410 */
2411 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2412 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002413 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002414 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002415 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002416 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002417 IOMMU_PAGE_SIZE(tbl));
2418 if (rc) {
2419 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
2420 return rc;
2421 }
2422
2423 pnv_pci_link_table_and_group(phb->hose->node, num,
2424 tbl, &pe->table_group);
Michael Ellermaned7d9a12016-09-15 17:03:06 +10002425 pnv_pci_ioda2_tce_invalidate_pe(pe);
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002426
2427 return 0;
2428}
2429
Frederic Barrat25529102017-08-04 11:55:14 +02002430void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002431{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002432 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2433 int64_t rc;
2434
2435 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2436 if (enable) {
2437 phys_addr_t top = memblock_end_of_DRAM();
2438
2439 top = roundup_pow_of_two(top);
2440 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2441 pe->pe_number,
2442 window_id,
2443 pe->tce_bypass_base,
2444 top);
2445 } else {
2446 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2447 pe->pe_number,
2448 window_id,
2449 pe->tce_bypass_base,
2450 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002451 }
2452 if (rc)
2453 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2454 else
2455 pe->tce_bypass_enabled = enable;
2456}
2457
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002458static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2459 int num, __u32 page_shift, __u64 window_size, __u32 levels,
Alexey Kardashevskiy090bad32018-07-04 16:13:47 +10002460 bool alloc_userspace_copy, struct iommu_table **ptbl)
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002461{
2462 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2463 table_group);
2464 int nid = pe->phb->hose->node;
2465 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2466 long ret;
2467 struct iommu_table *tbl;
2468
2469 tbl = pnv_pci_table_alloc(nid);
2470 if (!tbl)
2471 return -ENOMEM;
2472
Alexey Kardashevskiy11edf112017-03-22 15:21:49 +11002473 tbl->it_ops = &pnv_ioda2_iommu_ops;
2474
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002475 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2476 bus_offset, page_shift, window_size,
Alexey Kardashevskiy090bad32018-07-04 16:13:47 +10002477 levels, alloc_userspace_copy, tbl);
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002478 if (ret) {
Alexey Kardashevskiye5afdf92017-03-22 15:21:50 +11002479 iommu_tce_table_put(tbl);
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002480 return ret;
2481 }
2482
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002483 *ptbl = tbl;
2484
2485 return 0;
2486}
2487
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002488static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2489{
2490 struct iommu_table *tbl = NULL;
2491 long rc;
2492
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002493 /*
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002494 * crashkernel= specifies the kdump kernel's maximum memory at
2495 * some offset and there is no guaranteed the result is a power
2496 * of 2, which will cause errors later.
2497 */
2498 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
2499
2500 /*
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002501 * In memory constrained environments, e.g. kdump kernel, the
2502 * DMA window can be larger than available memory, which will
2503 * cause errors later.
2504 */
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002505 const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002506
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002507 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2508 IOMMU_PAGE_SHIFT_4K,
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002509 window_size,
Alexey Kardashevskiy090bad32018-07-04 16:13:47 +10002510 POWERNV_IOMMU_DEFAULT_LEVELS, false, &tbl);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002511 if (rc) {
2512 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2513 rc);
2514 return rc;
2515 }
2516
2517 iommu_init_table(tbl, pe->phb->hose->node);
2518
2519 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2520 if (rc) {
2521 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2522 rc);
Alexey Kardashevskiye5afdf92017-03-22 15:21:50 +11002523 iommu_tce_table_put(tbl);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002524 return rc;
2525 }
2526
2527 if (!pnv_iommu_bypass_disabled)
2528 pnv_pci_ioda2_set_bypass(pe, true);
2529
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002530 /*
2531 * Setting table base here only for carrying iommu_group
2532 * further down to let iommu_add_device() do the job.
2533 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2534 */
2535 if (pe->flags & PNV_IODA_PE_DEV)
2536 set_iommu_table_base(&pe->pdev->dev, tbl);
2537
2538 return 0;
2539}
2540
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002541#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2542static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2543 int num)
2544{
2545 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2546 table_group);
2547 struct pnv_phb *phb = pe->phb;
2548 long ret;
2549
2550 pe_info(pe, "Removing DMA window #%d\n", num);
2551
2552 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2553 (pe->pe_number << 1) + num,
2554 0/* levels */, 0/* table address */,
2555 0/* table size */, 0/* page size */);
2556 if (ret)
2557 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2558 else
Michael Ellermaned7d9a12016-09-15 17:03:06 +10002559 pnv_pci_ioda2_tce_invalidate_pe(pe);
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002560
2561 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2562
2563 return ret;
2564}
2565#endif
2566
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002567#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002568static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2569 __u64 window_size, __u32 levels)
2570{
2571 unsigned long bytes = 0;
2572 const unsigned window_shift = ilog2(window_size);
2573 unsigned entries_shift = window_shift - page_shift;
2574 unsigned table_shift = entries_shift + 3;
2575 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2576 unsigned long direct_table_size;
2577
2578 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002579 !is_power_of_2(window_size))
2580 return 0;
2581
2582 /* Calculate a direct table size from window_size and levels */
2583 entries_shift = (entries_shift + levels - 1) / levels;
2584 table_shift = entries_shift + 3;
2585 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2586 direct_table_size = 1UL << table_shift;
2587
2588 for ( ; levels; --levels) {
2589 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2590
2591 tce_table_size /= direct_table_size;
2592 tce_table_size <<= 3;
Alexey Kardashevskiye49a6a22017-04-13 17:05:27 +10002593 tce_table_size = max_t(unsigned long,
2594 tce_table_size, direct_table_size);
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002595 }
2596
Alexey Kardashevskiy090bad32018-07-04 16:13:47 +10002597 return bytes + bytes; /* one for HW table, one for userspace copy */
2598}
2599
2600static long pnv_pci_ioda2_create_table_userspace(
2601 struct iommu_table_group *table_group,
2602 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2603 struct iommu_table **ptbl)
2604{
2605 return pnv_pci_ioda2_create_table(table_group,
2606 num, page_shift, window_size, levels, true, ptbl);
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002607}
2608
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002609static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002610{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002611 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2612 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002613 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2614 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002615
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002616 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002617 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Alexey Kardashevskiydb08e1d2017-02-21 13:41:31 +11002618 if (pe->pbus)
2619 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
Alexey Kardashevskiye5afdf92017-03-22 15:21:50 +11002620 iommu_tce_table_put(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002621}
2622
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002623static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2624{
2625 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2626 table_group);
2627
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002628 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiydb08e1d2017-02-21 13:41:31 +11002629 if (pe->pbus)
2630 pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002631}
2632
2633static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002634 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy090bad32018-07-04 16:13:47 +10002635 .create_table = pnv_pci_ioda2_create_table_userspace,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002636 .set_window = pnv_pci_ioda2_set_window,
2637 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002638 .take_ownership = pnv_ioda2_take_ownership,
2639 .release_ownership = pnv_ioda2_release_ownership,
2640};
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002641
2642static int gpe_table_group_to_npe_cb(struct device *dev, void *opaque)
2643{
2644 struct pci_controller *hose;
2645 struct pnv_phb *phb;
2646 struct pnv_ioda_pe **ptmppe = opaque;
2647 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
2648 struct pci_dn *pdn = pci_get_pdn(pdev);
2649
2650 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2651 return 0;
2652
2653 hose = pci_bus_to_host(pdev->bus);
2654 phb = hose->private_data;
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01002655 if (phb->type != PNV_PHB_NPU_NVLINK)
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002656 return 0;
2657
2658 *ptmppe = &phb->ioda.pe_array[pdn->pe_number];
2659
2660 return 1;
2661}
2662
2663/*
2664 * This returns PE of associated NPU.
2665 * This assumes that NPU is in the same IOMMU group with GPU and there is
2666 * no other PEs.
2667 */
2668static struct pnv_ioda_pe *gpe_table_group_to_npe(
2669 struct iommu_table_group *table_group)
2670{
2671 struct pnv_ioda_pe *npe = NULL;
2672 int ret = iommu_group_for_each_dev(table_group->group, &npe,
2673 gpe_table_group_to_npe_cb);
2674
2675 BUG_ON(!ret || !npe);
2676
2677 return npe;
2678}
2679
2680static long pnv_pci_ioda2_npu_set_window(struct iommu_table_group *table_group,
2681 int num, struct iommu_table *tbl)
2682{
Alexey Kardashevskiyd41ce7b2018-02-13 16:51:35 +11002683 struct pnv_ioda_pe *npe = gpe_table_group_to_npe(table_group);
2684 int num2 = (num == 0) ? 1 : 0;
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002685 long ret = pnv_pci_ioda2_set_window(table_group, num, tbl);
2686
2687 if (ret)
2688 return ret;
2689
Alexey Kardashevskiyd41ce7b2018-02-13 16:51:35 +11002690 if (table_group->tables[num2])
2691 pnv_npu_unset_window(npe, num2);
2692
2693 ret = pnv_npu_set_window(npe, num, tbl);
2694 if (ret) {
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002695 pnv_pci_ioda2_unset_window(table_group, num);
Alexey Kardashevskiyd41ce7b2018-02-13 16:51:35 +11002696 if (table_group->tables[num2])
2697 pnv_npu_set_window(npe, num2,
2698 table_group->tables[num2]);
2699 }
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002700
2701 return ret;
2702}
2703
2704static long pnv_pci_ioda2_npu_unset_window(
2705 struct iommu_table_group *table_group,
2706 int num)
2707{
Alexey Kardashevskiyd41ce7b2018-02-13 16:51:35 +11002708 struct pnv_ioda_pe *npe = gpe_table_group_to_npe(table_group);
2709 int num2 = (num == 0) ? 1 : 0;
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002710 long ret = pnv_pci_ioda2_unset_window(table_group, num);
2711
2712 if (ret)
2713 return ret;
2714
Alexey Kardashevskiyd41ce7b2018-02-13 16:51:35 +11002715 if (!npe->table_group.tables[num])
2716 return 0;
2717
2718 ret = pnv_npu_unset_window(npe, num);
2719 if (ret)
2720 return ret;
2721
2722 if (table_group->tables[num2])
2723 ret = pnv_npu_set_window(npe, num2, table_group->tables[num2]);
2724
2725 return ret;
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002726}
2727
2728static void pnv_ioda2_npu_take_ownership(struct iommu_table_group *table_group)
2729{
2730 /*
2731 * Detach NPU first as pnv_ioda2_take_ownership() will destroy
2732 * the iommu_table if 32bit DMA is enabled.
2733 */
2734 pnv_npu_take_ownership(gpe_table_group_to_npe(table_group));
2735 pnv_ioda2_take_ownership(table_group);
2736}
2737
2738static struct iommu_table_group_ops pnv_pci_ioda2_npu_ops = {
2739 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy090bad32018-07-04 16:13:47 +10002740 .create_table = pnv_pci_ioda2_create_table_userspace,
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002741 .set_window = pnv_pci_ioda2_npu_set_window,
2742 .unset_window = pnv_pci_ioda2_npu_unset_window,
2743 .take_ownership = pnv_ioda2_npu_take_ownership,
2744 .release_ownership = pnv_ioda2_release_ownership,
2745};
2746
2747static void pnv_pci_ioda_setup_iommu_api(void)
2748{
2749 struct pci_controller *hose, *tmp;
2750 struct pnv_phb *phb;
2751 struct pnv_ioda_pe *pe, *gpe;
2752
2753 /*
2754 * Now we have all PHBs discovered, time to add NPU devices to
2755 * the corresponding IOMMU groups.
2756 */
2757 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2758 phb = hose->private_data;
2759
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01002760 if (phb->type != PNV_PHB_NPU_NVLINK)
Alexey Kardashevskiyb5cb9ab2016-04-29 18:55:24 +10002761 continue;
2762
2763 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2764 gpe = pnv_pci_npu_setup_iommu(pe);
2765 if (gpe)
2766 gpe->table_group.ops = &pnv_pci_ioda2_npu_ops;
2767 }
2768 }
2769}
2770#else /* !CONFIG_IOMMU_API */
2771static void pnv_pci_ioda_setup_iommu_api(void) { };
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002772#endif
2773
Alexey Kardashevskiy7ef73cd2018-05-14 19:39:22 +10002774static unsigned long pnv_ioda_parse_tce_sizes(struct pnv_phb *phb)
2775{
2776 struct pci_controller *hose = phb->hose;
2777 struct device_node *dn = hose->dn;
2778 unsigned long mask = 0;
2779 int i, rc, count;
2780 u32 val;
2781
2782 count = of_property_count_u32_elems(dn, "ibm,supported-tce-sizes");
2783 if (count <= 0) {
2784 mask = SZ_4K | SZ_64K;
2785 /* Add 16M for POWER8 by default */
2786 if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
2787 !cpu_has_feature(CPU_FTR_ARCH_300))
2788 mask |= SZ_16M;
2789 return mask;
2790 }
2791
2792 for (i = 0; i < count; i++) {
2793 rc = of_property_read_u32_index(dn, "ibm,supported-tce-sizes",
2794 i, &val);
2795 if (rc == 0)
2796 mask |= 1ULL << val;
2797 }
2798
2799 return mask;
2800}
2801
Gavin Shan373f5652013-04-25 19:21:01 +00002802static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2803 struct pnv_ioda_pe *pe)
2804{
Gavin Shan373f5652013-04-25 19:21:01 +00002805 int64_t rc;
2806
Gavin Shanccd1c192016-05-20 16:41:31 +10002807 if (!pnv_pci_ioda_pe_dma_weight(pe))
2808 return;
2809
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002810 /* TVE #1 is selected by PCI address bit 59 */
2811 pe->tce_bypass_base = 1ull << 59;
2812
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002813 iommu_register_group(&pe->table_group, phb->hose->global_number,
2814 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002815
Gavin Shan373f5652013-04-25 19:21:01 +00002816 /* The PE will reserve all possible 32-bits space */
Gavin Shan373f5652013-04-25 19:21:01 +00002817 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002818 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002819
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002820 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002821 pe->table_group.tce32_start = 0;
2822 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2823 pe->table_group.max_dynamic_windows_supported =
2824 IOMMU_TABLE_GROUP_MAX_TABLES;
2825 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
Alexey Kardashevskiy7ef73cd2018-05-14 19:39:22 +10002826 pe->table_group.pgsizes = pnv_ioda_parse_tce_sizes(phb);
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002827#ifdef CONFIG_IOMMU_API
2828 pe->table_group.ops = &pnv_pci_ioda2_ops;
2829#endif
2830
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002831 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan801846d2016-05-03 15:41:34 +10002832 if (rc)
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002833 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002834
Alexey Kardashevskiy20f13b92017-02-21 13:40:20 +11002835 if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiydb08e1d2017-02-21 13:41:31 +11002836 pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
Gavin Shan373f5652013-04-25 19:21:01 +00002837}
2838
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002839#ifdef CONFIG_PCI_MSI
Suresh Warrier4ee11c12016-08-19 15:35:49 +10002840int64_t pnv_opal_pci_msi_eoi(struct irq_chip *chip, unsigned int hw_irq)
Gavin Shan137436c2013-04-25 19:20:59 +00002841{
Gavin Shan137436c2013-04-25 19:20:59 +00002842 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2843 ioda.irq_chip);
Gavin Shan137436c2013-04-25 19:20:59 +00002844
Suresh Warrier4ee11c12016-08-19 15:35:49 +10002845 return opal_pci_msi_eoi(phb->opal_id, hw_irq);
2846}
2847
2848static void pnv_ioda2_msi_eoi(struct irq_data *d)
2849{
2850 int64_t rc;
2851 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2852 struct irq_chip *chip = irq_data_get_irq_chip(d);
2853
2854 rc = pnv_opal_pci_msi_eoi(chip, hw_irq);
Gavin Shan137436c2013-04-25 19:20:59 +00002855 WARN_ON_ONCE(rc);
2856
2857 icp_native_eoi(d);
2858}
2859
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002860
Ian Munsief4568342016-07-14 07:17:00 +10002861void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002862{
2863 struct irq_data *idata;
2864 struct irq_chip *ichip;
2865
Benjamin Herrenschmidtfb111332016-07-08 16:37:09 +10002866 /* The MSI EOI OPAL call is only needed on PHB3 */
2867 if (phb->model != PNV_PHB_MODEL_PHB3)
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002868 return;
2869
2870 if (!phb->ioda.irq_chip_init) {
2871 /*
2872 * First time we setup an MSI IRQ, we need to setup the
2873 * corresponding IRQ chip to route correctly.
2874 */
2875 idata = irq_get_irq_data(virq);
2876 ichip = irq_data_get_irq_chip(idata);
2877 phb->ioda.irq_chip_init = 1;
2878 phb->ioda.irq_chip = *ichip;
2879 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2880 }
2881 irq_set_chip(virq, &phb->ioda.irq_chip);
2882}
2883
Suresh Warrier4ee11c12016-08-19 15:35:49 +10002884/*
2885 * Returns true iff chip is something that we could call
2886 * pnv_opal_pci_msi_eoi for.
2887 */
2888bool is_pnv_opal_msi(struct irq_chip *chip)
2889{
2890 return chip->irq_eoi == pnv_ioda2_msi_eoi;
2891}
2892EXPORT_SYMBOL_GPL(is_pnv_opal_msi);
2893
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002894static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002895 unsigned int hwirq, unsigned int virq,
2896 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002897{
2898 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2899 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002900 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002901 int rc;
2902
2903 /* No PE assigned ? bail out ... no MSI for you ! */
2904 if (pe == NULL)
2905 return -ENXIO;
2906
2907 /* Check if we have an MVE */
2908 if (pe->mve_number < 0)
2909 return -ENXIO;
2910
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002911 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002912 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002913 is_64 = 0;
2914
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002915 /* Assign XIVE to PE */
2916 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2917 if (rc) {
2918 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2919 pci_name(dev), rc, xive_num);
2920 return -EIO;
2921 }
2922
2923 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002924 __be64 addr64;
2925
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002926 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2927 &addr64, &data);
2928 if (rc) {
2929 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2930 pci_name(dev), rc);
2931 return -EIO;
2932 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002933 msg->address_hi = be64_to_cpu(addr64) >> 32;
2934 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002935 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002936 __be32 addr32;
2937
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002938 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2939 &addr32, &data);
2940 if (rc) {
2941 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2942 pci_name(dev), rc);
2943 return -EIO;
2944 }
2945 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002946 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002947 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002948 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002949
Ian Munsief4568342016-07-14 07:17:00 +10002950 pnv_set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002951
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002952 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
Russell Currey1f52f172016-11-16 14:02:15 +11002953 " address=%x_%08x data=%x PE# %x\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002954 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2955 msg->address_hi, msg->address_lo, data, pe->pe_number);
2956
2957 return 0;
2958}
2959
2960static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2961{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002962 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002963 const __be32 *prop = of_get_property(phb->hose->dn,
2964 "ibm,opal-msi-ranges", NULL);
2965 if (!prop) {
2966 /* BML Fallback */
2967 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2968 }
2969 if (!prop)
2970 return;
2971
2972 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002973 count = be32_to_cpup(prop + 1);
2974 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002975 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2976 phb->hose->global_number);
2977 return;
2978 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002979
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002980 phb->msi_setup = pnv_pci_ioda_msi_setup;
2981 phb->msi32_support = 1;
2982 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002983 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002984}
2985#else
2986static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2987#endif /* CONFIG_PCI_MSI */
2988
Wei Yang6e628c72015-03-25 16:23:55 +08002989#ifdef CONFIG_PCI_IOV
2990static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2991{
Wei Yangf2dd0af2015-10-22 09:22:17 +08002992 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
2993 struct pnv_phb *phb = hose->private_data;
2994 const resource_size_t gate = phb->ioda.m64_segsize >> 2;
Wei Yang6e628c72015-03-25 16:23:55 +08002995 struct resource *res;
2996 int i;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002997 resource_size_t size, total_vf_bar_sz;
Wei Yang6e628c72015-03-25 16:23:55 +08002998 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002999 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08003000
3001 if (!pdev->is_physfn || pdev->is_added)
3002 return;
3003
Wei Yang6e628c72015-03-25 16:23:55 +08003004 pdn = pci_get_pdn(pdev);
3005 pdn->vfs_expanded = 0;
Wei Yangee8222f2015-10-22 09:22:16 +08003006 pdn->m64_single_mode = false;
Wei Yang6e628c72015-03-25 16:23:55 +08003007
Wei Yang5b88ec22015-03-25 16:23:58 +08003008 total_vfs = pci_sriov_get_totalvfs(pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10003009 mul = phb->ioda.total_pe_num;
Wei Yangdfcc8d42015-10-22 09:22:18 +08003010 total_vf_bar_sz = 0;
Wei Yang5b88ec22015-03-25 16:23:58 +08003011
3012 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
3013 res = &pdev->resource[i + PCI_IOV_RESOURCES];
3014 if (!res->flags || res->parent)
3015 continue;
Russell Curreyb79331a2016-09-14 16:37:17 +10003016 if (!pnv_pci_is_m64_flags(res->flags)) {
Wei Yangb0331852015-10-22 09:22:14 +08003017 dev_warn(&pdev->dev, "Don't support SR-IOV with"
3018 " non M64 VF BAR%d: %pR. \n",
Wei Yang5b88ec22015-03-25 16:23:58 +08003019 i, res);
Wei Yangb0331852015-10-22 09:22:14 +08003020 goto truncate_iov;
Wei Yang5b88ec22015-03-25 16:23:58 +08003021 }
3022
Wei Yangdfcc8d42015-10-22 09:22:18 +08003023 total_vf_bar_sz += pci_iov_resource_size(pdev,
3024 i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08003025
Wei Yangf2dd0af2015-10-22 09:22:17 +08003026 /*
3027 * If bigger than quarter of M64 segment size, just round up
3028 * power of two.
3029 *
3030 * Generally, one M64 BAR maps one IOV BAR. To avoid conflict
3031 * with other devices, IOV BAR size is expanded to be
3032 * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64
3033 * segment size , the expanded size would equal to half of the
3034 * whole M64 space size, which will exhaust the M64 Space and
3035 * limit the system flexibility. This is a design decision to
3036 * set the boundary to quarter of the M64 segment size.
3037 */
Wei Yangdfcc8d42015-10-22 09:22:18 +08003038 if (total_vf_bar_sz > gate) {
Wei Yang5b88ec22015-03-25 16:23:58 +08003039 mul = roundup_pow_of_two(total_vfs);
Wei Yangdfcc8d42015-10-22 09:22:18 +08003040 dev_info(&pdev->dev,
3041 "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n",
3042 total_vf_bar_sz, gate, mul);
Wei Yangee8222f2015-10-22 09:22:16 +08003043 pdn->m64_single_mode = true;
Wei Yang5b88ec22015-03-25 16:23:58 +08003044 break;
3045 }
3046 }
3047
Wei Yang6e628c72015-03-25 16:23:55 +08003048 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
3049 res = &pdev->resource[i + PCI_IOV_RESOURCES];
3050 if (!res->flags || res->parent)
3051 continue;
Wei Yang6e628c72015-03-25 16:23:55 +08003052
Wei Yang6e628c72015-03-25 16:23:55 +08003053 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yangee8222f2015-10-22 09:22:16 +08003054 /*
3055 * On PHB3, the minimum size alignment of M64 BAR in single
3056 * mode is 32MB.
3057 */
3058 if (pdn->m64_single_mode && (size < SZ_32M))
3059 goto truncate_iov;
3060 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
Wei Yang5b88ec22015-03-25 16:23:58 +08003061 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08003062 dev_dbg(&pdev->dev, " %pR\n", res);
3063 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08003064 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08003065 }
Wei Yang5b88ec22015-03-25 16:23:58 +08003066 pdn->vfs_expanded = mul;
Wei Yangb0331852015-10-22 09:22:14 +08003067
3068 return;
3069
3070truncate_iov:
3071 /* To save MMIO space, IOV BAR is truncated. */
3072 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
3073 res = &pdev->resource[i + PCI_IOV_RESOURCES];
3074 res->flags = 0;
3075 res->end = res->start - 1;
3076 }
Wei Yang6e628c72015-03-25 16:23:55 +08003077}
3078#endif /* CONFIG_PCI_IOV */
3079
Gavin Shan23e79422016-05-03 15:41:27 +10003080static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
3081 struct resource *res)
3082{
3083 struct pnv_phb *phb = pe->phb;
3084 struct pci_bus_region region;
3085 int index;
3086 int64_t rc;
3087
3088 if (!res || !res->flags || res->start > res->end)
3089 return;
3090
3091 if (res->flags & IORESOURCE_IO) {
3092 region.start = res->start - phb->ioda.io_pci_base;
3093 region.end = res->end - phb->ioda.io_pci_base;
3094 index = region.start / phb->ioda.io_segsize;
3095
3096 while (index < phb->ioda.total_pe_num &&
3097 region.start <= region.end) {
3098 phb->ioda.io_segmap[index] = pe->pe_number;
3099 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3100 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
3101 if (rc != OPAL_SUCCESS) {
Russell Currey1f52f172016-11-16 14:02:15 +11003102 pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",
Gavin Shan23e79422016-05-03 15:41:27 +10003103 __func__, rc, index, pe->pe_number);
3104 break;
3105 }
3106
3107 region.start += phb->ioda.io_segsize;
3108 index++;
3109 }
3110 } else if ((res->flags & IORESOURCE_MEM) &&
Benjamin Herrenschmidt5958d192016-07-08 15:55:43 +10003111 !pnv_pci_is_m64(phb, res)) {
Gavin Shan23e79422016-05-03 15:41:27 +10003112 region.start = res->start -
3113 phb->hose->mem_offset[0] -
3114 phb->ioda.m32_pci_base;
3115 region.end = res->end -
3116 phb->hose->mem_offset[0] -
3117 phb->ioda.m32_pci_base;
3118 index = region.start / phb->ioda.m32_segsize;
3119
3120 while (index < phb->ioda.total_pe_num &&
3121 region.start <= region.end) {
3122 phb->ioda.m32_segmap[index] = pe->pe_number;
3123 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3124 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
3125 if (rc != OPAL_SUCCESS) {
Russell Currey1f52f172016-11-16 14:02:15 +11003126 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",
Gavin Shan23e79422016-05-03 15:41:27 +10003127 __func__, rc, index, pe->pe_number);
3128 break;
3129 }
3130
3131 region.start += phb->ioda.m32_segsize;
3132 index++;
3133 }
3134 }
3135}
3136
Gavin Shan11685be2012-08-20 03:49:16 +00003137/*
3138 * This function is supposed to be called on basis of PE from top
3139 * to bottom style. So the the I/O or MMIO segment assigned to
Masahiro Yamada03671052017-02-27 14:29:28 -08003140 * parent PE could be overridden by its child PEs if necessary.
Gavin Shan11685be2012-08-20 03:49:16 +00003141 */
Gavin Shan23e79422016-05-03 15:41:27 +10003142static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00003143{
Gavin Shan69d733e2016-05-03 15:41:28 +10003144 struct pci_dev *pdev;
Gavin Shan23e79422016-05-03 15:41:27 +10003145 int i;
Gavin Shan11685be2012-08-20 03:49:16 +00003146
3147 /*
3148 * NOTE: We only care PCI bus based PE for now. For PCI
3149 * device based PE, for example SRIOV sensitive VF should
3150 * be figured out later.
3151 */
3152 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
3153
Gavin Shan69d733e2016-05-03 15:41:28 +10003154 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
3155 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
3156 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
3157
3158 /*
3159 * If the PE contains all subordinate PCI buses, the
3160 * windows of the child bridges should be mapped to
3161 * the PE as well.
3162 */
3163 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
3164 continue;
3165 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
3166 pnv_ioda_setup_pe_res(pe,
3167 &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
3168 }
Gavin Shan11685be2012-08-20 03:49:16 +00003169}
3170
Russell Currey98b665d2016-07-28 15:05:03 +10003171#ifdef CONFIG_DEBUG_FS
3172static int pnv_pci_diag_data_set(void *data, u64 val)
3173{
3174 struct pci_controller *hose;
3175 struct pnv_phb *phb;
3176 s64 ret;
3177
3178 if (val != 1ULL)
3179 return -EINVAL;
3180
3181 hose = (struct pci_controller *)data;
3182 if (!hose || !hose->private_data)
3183 return -ENODEV;
3184
3185 phb = hose->private_data;
3186
3187 /* Retrieve the diag data from firmware */
Russell Currey5cb1f8f2017-06-14 14:19:59 +10003188 ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
3189 phb->diag_data_size);
Russell Currey98b665d2016-07-28 15:05:03 +10003190 if (ret != OPAL_SUCCESS)
3191 return -EIO;
3192
3193 /* Print the diag data to the kernel log */
Russell Currey5cb1f8f2017-06-14 14:19:59 +10003194 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
Russell Currey98b665d2016-07-28 15:05:03 +10003195 return 0;
3196}
3197
3198DEFINE_SIMPLE_ATTRIBUTE(pnv_pci_diag_data_fops, NULL,
3199 pnv_pci_diag_data_set, "%llu\n");
3200
3201#endif /* CONFIG_DEBUG_FS */
3202
Gavin Shan37c367f2013-06-20 18:13:25 +08003203static void pnv_pci_ioda_create_dbgfs(void)
3204{
3205#ifdef CONFIG_DEBUG_FS
3206 struct pci_controller *hose, *tmp;
3207 struct pnv_phb *phb;
3208 char name[16];
3209
3210 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3211 phb = hose->private_data;
3212
Gavin Shanccd1c192016-05-20 16:41:31 +10003213 /* Notify initialization of PHB done */
3214 phb->initialized = 1;
3215
Gavin Shan37c367f2013-06-20 18:13:25 +08003216 sprintf(name, "PCI%04x", hose->global_number);
3217 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
Russell Currey98b665d2016-07-28 15:05:03 +10003218 if (!phb->dbgfs) {
Joe Perchesf2c2cbc2016-10-24 21:00:08 -07003219 pr_warn("%s: Error on creating debugfs on PHB#%x\n",
Gavin Shan37c367f2013-06-20 18:13:25 +08003220 __func__, hose->global_number);
Russell Currey98b665d2016-07-28 15:05:03 +10003221 continue;
3222 }
3223
3224 debugfs_create_file("dump_diag_regs", 0200, phb->dbgfs, hose,
3225 &pnv_pci_diag_data_fops);
Gavin Shan37c367f2013-06-20 18:13:25 +08003226 }
3227#endif /* CONFIG_DEBUG_FS */
3228}
3229
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003230static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00003231{
3232 pnv_pci_ioda_setup_PEs();
Gavin Shanccd1c192016-05-20 16:41:31 +10003233 pnv_pci_ioda_setup_iommu_api();
Gavin Shan37c367f2013-06-20 18:13:25 +08003234 pnv_pci_ioda_create_dbgfs();
3235
Gavin Shane9cc17d2013-06-20 13:21:14 +08003236#ifdef CONFIG_EEH
Benjamin Herrenschmidtb9fde582017-09-07 16:35:44 +10003237 pnv_eeh_post_init();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003238#endif
Gavin Shanfb446ad2012-08-20 03:49:14 +00003239}
3240
Gavin Shan271fd032012-09-11 16:59:47 -06003241/*
3242 * Returns the alignment for I/O or memory windows for P2P
3243 * bridges. That actually depends on how PEs are segmented.
3244 * For now, we return I/O or M32 segment size for PE sensitive
3245 * P2P bridges. Otherwise, the default values (4KiB for I/O,
3246 * 1MiB for memory) will be returned.
3247 *
3248 * The current PCI bus might be put into one PE, which was
3249 * create against the parent PCI bridge. For that case, we
3250 * needn't enlarge the alignment so that we can save some
3251 * resources.
3252 */
3253static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
3254 unsigned long type)
3255{
3256 struct pci_dev *bridge;
3257 struct pci_controller *hose = pci_bus_to_host(bus);
3258 struct pnv_phb *phb = hose->private_data;
3259 int num_pci_bridges = 0;
3260
3261 bridge = bus->self;
3262 while (bridge) {
3263 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
3264 num_pci_bridges++;
3265 if (num_pci_bridges >= 2)
3266 return 1;
3267 }
3268
3269 bridge = bridge->bus->self;
3270 }
3271
Benjamin Herrenschmidt5958d192016-07-08 15:55:43 +10003272 /*
3273 * We fall back to M32 if M64 isn't supported. We enforce the M64
3274 * alignment for any 64-bit resource, PCIe doesn't care and
3275 * bridges only do 64-bit prefetchable anyway.
3276 */
Russell Curreyb79331a2016-09-14 16:37:17 +10003277 if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type))
Guo Chao262af552014-07-21 14:42:30 +10003278 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06003279 if (type & IORESOURCE_MEM)
3280 return phb->ioda.m32_segsize;
3281
3282 return phb->ioda.io_segsize;
3283}
3284
Gavin Shan40e2a472016-05-20 16:41:33 +10003285/*
3286 * We are updating root port or the upstream port of the
3287 * bridge behind the root port with PHB's windows in order
3288 * to accommodate the changes on required resources during
3289 * PCI (slot) hotplug, which is connected to either root
3290 * port or the downstream ports of PCIe switch behind the
3291 * root port.
3292 */
3293static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
3294 unsigned long type)
3295{
3296 struct pci_controller *hose = pci_bus_to_host(bus);
3297 struct pnv_phb *phb = hose->private_data;
3298 struct pci_dev *bridge = bus->self;
3299 struct resource *r, *w;
3300 bool msi_region = false;
3301 int i;
3302
3303 /* Check if we need apply fixup to the bridge's windows */
3304 if (!pci_is_root_bus(bridge->bus) &&
3305 !pci_is_root_bus(bridge->bus->self->bus))
3306 return;
3307
3308 /* Fixup the resources */
3309 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
3310 r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
3311 if (!r->flags || !r->parent)
3312 continue;
3313
3314 w = NULL;
3315 if (r->flags & type & IORESOURCE_IO)
3316 w = &hose->io_resource;
Benjamin Herrenschmidt5958d192016-07-08 15:55:43 +10003317 else if (pnv_pci_is_m64(phb, r) &&
Gavin Shan40e2a472016-05-20 16:41:33 +10003318 (type & IORESOURCE_PREFETCH) &&
3319 phb->ioda.m64_segsize)
3320 w = &hose->mem_resources[1];
3321 else if (r->flags & type & IORESOURCE_MEM) {
3322 w = &hose->mem_resources[0];
3323 msi_region = true;
3324 }
3325
3326 r->start = w->start;
3327 r->end = w->end;
3328
3329 /* The 64KB 32-bits MSI region shouldn't be included in
3330 * the 32-bits bridge window. Otherwise, we can see strange
3331 * issues. One of them is EEH error observed on Garrison.
3332 *
3333 * Exclude top 1MB region which is the minimal alignment of
3334 * 32-bits bridge window.
3335 */
3336 if (msi_region) {
3337 r->end += 0x10000;
3338 r->end -= 0x100000;
3339 }
3340 }
3341}
3342
Gavin Shanccd1c192016-05-20 16:41:31 +10003343static void pnv_pci_setup_bridge(struct pci_bus *bus, unsigned long type)
3344{
3345 struct pci_controller *hose = pci_bus_to_host(bus);
3346 struct pnv_phb *phb = hose->private_data;
3347 struct pci_dev *bridge = bus->self;
3348 struct pnv_ioda_pe *pe;
3349 bool all = (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
3350
Gavin Shan40e2a472016-05-20 16:41:33 +10003351 /* Extend bridge's windows if necessary */
3352 pnv_pci_fixup_bridge_resources(bus, type);
3353
Gavin Shan63803c32016-05-20 16:41:32 +10003354 /* The PE for root bus should be realized before any one else */
3355 if (!phb->ioda.root_pe_populated) {
3356 pe = pnv_ioda_setup_bus_PE(phb->hose->bus, false);
3357 if (pe) {
3358 phb->ioda.root_pe_idx = pe->pe_number;
3359 phb->ioda.root_pe_populated = true;
3360 }
3361 }
3362
Gavin Shanccd1c192016-05-20 16:41:31 +10003363 /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
3364 if (list_empty(&bus->devices))
3365 return;
3366
3367 /* Reserve PEs according to used M64 resources */
3368 if (phb->reserve_m64_pe)
3369 phb->reserve_m64_pe(bus, NULL, all);
3370
3371 /*
3372 * Assign PE. We might run here because of partial hotplug.
3373 * For the case, we just pick up the existing PE and should
3374 * not allocate resources again.
3375 */
3376 pe = pnv_ioda_setup_bus_PE(bus, all);
3377 if (!pe)
3378 return;
3379
3380 pnv_ioda_setup_pe_seg(pe);
3381 switch (phb->type) {
3382 case PNV_PHB_IODA1:
3383 pnv_pci_ioda1_setup_dma_pe(phb, pe);
3384 break;
3385 case PNV_PHB_IODA2:
3386 pnv_pci_ioda2_setup_dma_pe(phb, pe);
3387 break;
3388 default:
Russell Currey1f52f172016-11-16 14:02:15 +11003389 pr_warn("%s: No DMA for PHB#%x (type %d)\n",
Gavin Shanccd1c192016-05-20 16:41:31 +10003390 __func__, phb->hose->global_number, phb->type);
3391 }
3392}
3393
Yongji Xie38274632017-04-10 19:58:13 +08003394static resource_size_t pnv_pci_default_alignment(void)
3395{
3396 return PAGE_SIZE;
3397}
3398
Wei Yang5350ab32015-03-25 16:23:56 +08003399#ifdef CONFIG_PCI_IOV
3400static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
3401 int resno)
3402{
Wei Yangee8222f2015-10-22 09:22:16 +08003403 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3404 struct pnv_phb *phb = hose->private_data;
Wei Yang5350ab32015-03-25 16:23:56 +08003405 struct pci_dn *pdn = pci_get_pdn(pdev);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003406 resource_size_t align;
Wei Yang5350ab32015-03-25 16:23:56 +08003407
Wei Yang7fbe7a92015-10-22 09:22:15 +08003408 /*
3409 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
3410 * SR-IOV. While from hardware perspective, the range mapped by M64
3411 * BAR should be size aligned.
3412 *
Wei Yangee8222f2015-10-22 09:22:16 +08003413 * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra
3414 * powernv-specific hardware restriction is gone. But if just use the
3415 * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with
3416 * in one segment of M64 #15, which introduces the PE conflict between
3417 * PF and VF. Based on this, the minimum alignment of an IOV BAR is
3418 * m64_segsize.
3419 *
Wei Yang7fbe7a92015-10-22 09:22:15 +08003420 * This function returns the total IOV BAR size if M64 BAR is in
3421 * Shared PE mode or just VF BAR size if not.
Wei Yangee8222f2015-10-22 09:22:16 +08003422 * If the M64 BAR is in Single PE mode, return the VF BAR size or
3423 * M64 segment size if IOV BAR size is less.
Wei Yang7fbe7a92015-10-22 09:22:15 +08003424 */
Wei Yang5350ab32015-03-25 16:23:56 +08003425 align = pci_iov_resource_size(pdev, resno);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003426 if (!pdn->vfs_expanded)
3427 return align;
Wei Yangee8222f2015-10-22 09:22:16 +08003428 if (pdn->m64_single_mode)
3429 return max(align, (resource_size_t)phb->ioda.m64_segsize);
Wei Yang5350ab32015-03-25 16:23:56 +08003430
Wei Yang7fbe7a92015-10-22 09:22:15 +08003431 return pdn->vfs_expanded * align;
Wei Yang5350ab32015-03-25 16:23:56 +08003432}
3433#endif /* CONFIG_PCI_IOV */
3434
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003435/* Prevent enabling devices for which we couldn't properly
3436 * assign a PE
3437 */
Ian Munsie4361b032016-07-14 07:17:06 +10003438bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003439{
Gavin Shandb1266c2012-08-20 03:49:18 +00003440 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3441 struct pnv_phb *phb = hose->private_data;
3442 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003443
Gavin Shandb1266c2012-08-20 03:49:18 +00003444 /* The function is probably called while the PEs have
3445 * not be created yet. For example, resource reassignment
3446 * during PCI probe period. We just skip the check if
3447 * PEs isn't ready.
3448 */
3449 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003450 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003451
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003452 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003453 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003454 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003455
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003456 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003457}
3458
Gavin Shanc5f77002016-05-20 16:41:35 +10003459static long pnv_pci_ioda1_unset_window(struct iommu_table_group *table_group,
3460 int num)
3461{
3462 struct pnv_ioda_pe *pe = container_of(table_group,
3463 struct pnv_ioda_pe, table_group);
3464 struct pnv_phb *phb = pe->phb;
3465 unsigned int idx;
3466 long rc;
3467
3468 pe_info(pe, "Removing DMA window #%d\n", num);
3469 for (idx = 0; idx < phb->ioda.dma32_count; idx++) {
3470 if (phb->ioda.dma32_segmap[idx] != pe->pe_number)
3471 continue;
3472
3473 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
3474 idx, 0, 0ul, 0ul, 0ul);
3475 if (rc != OPAL_SUCCESS) {
3476 pe_warn(pe, "Failure %ld unmapping DMA32 segment#%d\n",
3477 rc, idx);
3478 return rc;
3479 }
3480
3481 phb->ioda.dma32_segmap[idx] = IODA_INVALID_PE;
3482 }
3483
3484 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
3485 return OPAL_SUCCESS;
3486}
3487
3488static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe)
3489{
3490 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
3491 struct iommu_table *tbl = pe->table_group.tables[0];
3492 int64_t rc;
3493
3494 if (!weight)
3495 return;
3496
3497 rc = pnv_pci_ioda1_unset_window(&pe->table_group, 0);
3498 if (rc != OPAL_SUCCESS)
3499 return;
3500
Benjamin Herrenschmidta34ab7c2016-07-08 16:37:12 +10003501 pnv_pci_p7ioc_tce_invalidate(tbl, tbl->it_offset, tbl->it_size, false);
Gavin Shanc5f77002016-05-20 16:41:35 +10003502 if (pe->table_group.group) {
3503 iommu_group_put(pe->table_group.group);
3504 WARN_ON(pe->table_group.group);
3505 }
3506
3507 free_pages(tbl->it_base, get_order(tbl->it_size << 3));
Alexey Kardashevskiye5afdf92017-03-22 15:21:50 +11003508 iommu_tce_table_put(tbl);
Gavin Shanc5f77002016-05-20 16:41:35 +10003509}
3510
3511static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
3512{
3513 struct iommu_table *tbl = pe->table_group.tables[0];
3514 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
3515#ifdef CONFIG_IOMMU_API
3516 int64_t rc;
3517#endif
3518
3519 if (!weight)
3520 return;
3521
3522#ifdef CONFIG_IOMMU_API
3523 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
3524 if (rc)
3525 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
3526#endif
3527
3528 pnv_pci_ioda2_set_bypass(pe, false);
3529 if (pe->table_group.group) {
3530 iommu_group_put(pe->table_group.group);
3531 WARN_ON(pe->table_group.group);
3532 }
3533
Alexey Kardashevskiye5afdf92017-03-22 15:21:50 +11003534 iommu_tce_table_put(tbl);
Gavin Shanc5f77002016-05-20 16:41:35 +10003535}
3536
3537static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
3538 unsigned short win,
3539 unsigned int *map)
3540{
3541 struct pnv_phb *phb = pe->phb;
3542 int idx;
3543 int64_t rc;
3544
3545 for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
3546 if (map[idx] != pe->pe_number)
3547 continue;
3548
3549 if (win == OPAL_M64_WINDOW_TYPE)
3550 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3551 phb->ioda.reserved_pe_idx, win,
3552 idx / PNV_IODA1_M64_SEGS,
3553 idx % PNV_IODA1_M64_SEGS);
3554 else
3555 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3556 phb->ioda.reserved_pe_idx, win, 0, idx);
3557
3558 if (rc != OPAL_SUCCESS)
3559 pe_warn(pe, "Error %ld unmapping (%d) segment#%d\n",
3560 rc, win, idx);
3561
3562 map[idx] = IODA_INVALID_PE;
3563 }
3564}
3565
3566static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
3567{
3568 struct pnv_phb *phb = pe->phb;
3569
3570 if (phb->type == PNV_PHB_IODA1) {
3571 pnv_ioda_free_pe_seg(pe, OPAL_IO_WINDOW_TYPE,
3572 phb->ioda.io_segmap);
3573 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
3574 phb->ioda.m32_segmap);
3575 pnv_ioda_free_pe_seg(pe, OPAL_M64_WINDOW_TYPE,
3576 phb->ioda.m64_segmap);
3577 } else if (phb->type == PNV_PHB_IODA2) {
3578 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
3579 phb->ioda.m32_segmap);
3580 }
3581}
3582
3583static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
3584{
3585 struct pnv_phb *phb = pe->phb;
3586 struct pnv_ioda_pe *slave, *tmp;
3587
Gavin Shanc5f77002016-05-20 16:41:35 +10003588 list_del(&pe->list);
3589 switch (phb->type) {
3590 case PNV_PHB_IODA1:
3591 pnv_pci_ioda1_release_pe_dma(pe);
3592 break;
3593 case PNV_PHB_IODA2:
3594 pnv_pci_ioda2_release_pe_dma(pe);
3595 break;
3596 default:
3597 WARN_ON(1);
3598 }
3599
3600 pnv_ioda_release_pe_seg(pe);
3601 pnv_ioda_deconfigure_pe(pe->phb, pe);
Gavin Shanb3144272016-09-06 14:16:44 +10003602
3603 /* Release slave PEs in the compound PE */
3604 if (pe->flags & PNV_IODA_PE_MASTER) {
3605 list_for_each_entry_safe(slave, tmp, &pe->slaves, list) {
3606 list_del(&slave->list);
3607 pnv_ioda_free_pe(slave);
3608 }
3609 }
3610
Gavin Shan6eaed162016-09-13 16:40:24 +10003611 /*
3612 * The PE for root bus can be removed because of hotplug in EEH
3613 * recovery for fenced PHB error. We need to mark the PE dead so
3614 * that it can be populated again in PCI hot add path. The PE
3615 * shouldn't be destroyed as it's the global reserved resource.
3616 */
3617 if (phb->ioda.root_pe_populated &&
3618 phb->ioda.root_pe_idx == pe->pe_number)
3619 phb->ioda.root_pe_populated = false;
3620 else
3621 pnv_ioda_free_pe(pe);
Gavin Shanc5f77002016-05-20 16:41:35 +10003622}
3623
3624static void pnv_pci_release_device(struct pci_dev *pdev)
3625{
3626 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3627 struct pnv_phb *phb = hose->private_data;
3628 struct pci_dn *pdn = pci_get_pdn(pdev);
3629 struct pnv_ioda_pe *pe;
3630
3631 if (pdev->is_virtfn)
3632 return;
3633
3634 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
3635 return;
3636
Gavin Shan29bf2822016-09-06 16:34:01 +10003637 /*
3638 * PCI hotplug can happen as part of EEH error recovery. The @pdn
3639 * isn't removed and added afterwards in this scenario. We should
3640 * set the PE number in @pdn to an invalid one. Otherwise, the PE's
3641 * device count is decreased on removing devices while failing to
3642 * be increased on adding devices. It leads to unbalanced PE's device
3643 * count and eventually make normal PCI hotplug path broken.
3644 */
Gavin Shanc5f77002016-05-20 16:41:35 +10003645 pe = &phb->ioda.pe_array[pdn->pe_number];
Gavin Shan29bf2822016-09-06 16:34:01 +10003646 pdn->pe_number = IODA_INVALID_PE;
3647
Gavin Shanc5f77002016-05-20 16:41:35 +10003648 WARN_ON(--pe->device_count < 0);
3649 if (pe->device_count == 0)
3650 pnv_ioda_release_pe(pe);
3651}
3652
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003653static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003654{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003655 struct pnv_phb *phb = hose->private_data;
3656
Gavin Shand1a85ee2014-09-30 12:39:05 +10003657 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003658 OPAL_ASSERT_RESET);
3659}
3660
Daniel Axtens92ae0352015-04-28 15:12:05 +10003661static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003662 .dma_dev_setup = pnv_pci_dma_dev_setup,
3663 .dma_bus_setup = pnv_pci_dma_bus_setup,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003664#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003665 .setup_msi_irqs = pnv_setup_msi_irqs,
3666 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003667#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003668 .enable_device_hook = pnv_pci_enable_device_hook,
Gavin Shanc5f77002016-05-20 16:41:35 +10003669 .release_device = pnv_pci_release_device,
Gavin Shancb4224c2016-05-03 15:41:21 +10003670 .window_alignment = pnv_pci_window_alignment,
Gavin Shanccd1c192016-05-20 16:41:31 +10003671 .setup_bridge = pnv_pci_setup_bridge,
Gavin Shancb4224c2016-05-03 15:41:21 +10003672 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3673 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3674 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3675 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003676};
3677
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003678static int pnv_npu_dma_set_mask(struct pci_dev *npdev, u64 dma_mask)
3679{
3680 dev_err_once(&npdev->dev,
3681 "%s operation unsupported for NVLink devices\n",
3682 __func__);
3683 return -EPERM;
3684}
3685
Alistair Popple5d2aa712015-12-17 13:43:13 +11003686static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003687 .dma_dev_setup = pnv_pci_dma_dev_setup,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003688#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003689 .setup_msi_irqs = pnv_setup_msi_irqs,
3690 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003691#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003692 .enable_device_hook = pnv_pci_enable_device_hook,
3693 .window_alignment = pnv_pci_window_alignment,
3694 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3695 .dma_set_mask = pnv_npu_dma_set_mask,
3696 .shutdown = pnv_pci_ioda_shutdown,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003697};
3698
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01003699static const struct pci_controller_ops pnv_npu_ocapi_ioda_controller_ops = {
3700 .enable_device_hook = pnv_pci_enable_device_hook,
3701 .window_alignment = pnv_pci_window_alignment,
3702 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3703 .shutdown = pnv_pci_ioda_shutdown,
3704};
3705
Ian Munsie4361b032016-07-14 07:17:06 +10003706#ifdef CONFIG_CXL_BASE
3707const struct pci_controller_ops pnv_cxl_cx4_ioda_controller_ops = {
3708 .dma_dev_setup = pnv_pci_dma_dev_setup,
3709 .dma_bus_setup = pnv_pci_dma_bus_setup,
Ian Munsiea2f67d52016-07-14 07:17:10 +10003710#ifdef CONFIG_PCI_MSI
3711 .setup_msi_irqs = pnv_cxl_cx4_setup_msi_irqs,
3712 .teardown_msi_irqs = pnv_cxl_cx4_teardown_msi_irqs,
3713#endif
Ian Munsie4361b032016-07-14 07:17:06 +10003714 .enable_device_hook = pnv_cxl_enable_device_hook,
3715 .disable_device = pnv_cxl_disable_device,
3716 .release_device = pnv_pci_release_device,
3717 .window_alignment = pnv_pci_window_alignment,
3718 .setup_bridge = pnv_pci_setup_bridge,
3719 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3720 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3721 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3722 .shutdown = pnv_pci_ioda_shutdown,
3723};
3724#endif
3725
Anton Blancharde51df2c2014-08-20 08:55:18 +10003726static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3727 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003728{
3729 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003730 struct pnv_phb *phb;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003731 unsigned long size, m64map_off, m32map_off, pemap_off;
3732 unsigned long iomap_off = 0, dma32map_off = 0;
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10003733 struct resource r;
Alistair Popplec681b932013-09-23 12:04:57 +10003734 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003735 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003736 int len;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003737 unsigned int segno;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003738 u64 phb_id;
3739 void *aux;
3740 long rc;
3741
Benjamin Herrenschmidt08a45b32016-07-08 16:37:17 +10003742 if (!of_device_is_available(np))
3743 return;
3744
Rob Herringb7c670d2017-08-21 10:16:47 -05003745 pr_info("Initializing %s PHB (%pOF)\n", pnv_phb_names[ioda_type], np);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003746
3747 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3748 if (!prop64) {
3749 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3750 return;
3751 }
3752 phb_id = be64_to_cpup(prop64);
3753 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3754
Markus Elfringa0828cf2017-01-19 17:15:30 +01003755 phb = memblock_virt_alloc(sizeof(*phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003756
3757 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003758 phb->hose = hose = pcibios_alloc_controller(np);
3759 if (!phb->hose) {
Rob Herringb7c670d2017-08-21 10:16:47 -05003760 pr_err(" Can't allocate PCI controller for %pOF\n",
3761 np);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003762 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003763 return;
3764 }
3765
3766 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003767 prop32 = of_get_property(np, "bus-range", &len);
3768 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003769 hose->first_busno = be32_to_cpu(prop32[0]);
3770 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003771 } else {
Rob Herringb7c670d2017-08-21 10:16:47 -05003772 pr_warn(" Broken <bus-range> on %pOF\n", np);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003773 hose->first_busno = 0;
3774 hose->last_busno = 0xff;
3775 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003776 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003777 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003778 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003779 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003780 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003781
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003782 /* Detect specific models for error handling */
3783 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3784 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003785 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003786 phb->model = PNV_PHB_MODEL_PHB3;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003787 else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
3788 phb->model = PNV_PHB_MODEL_NPU;
Alistair Popple616badd2017-01-10 15:41:44 +11003789 else if (of_device_is_compatible(np, "ibm,power9-npu-pciex"))
3790 phb->model = PNV_PHB_MODEL_NPU2;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003791 else
3792 phb->model = PNV_PHB_MODEL_UNKNOWN;
3793
Russell Currey5cb1f8f2017-06-14 14:19:59 +10003794 /* Initialize diagnostic data buffer */
3795 prop32 = of_get_property(np, "ibm,phb-diag-data-size", NULL);
3796 if (prop32)
3797 phb->diag_data_size = be32_to_cpup(prop32);
3798 else
3799 phb->diag_data_size = PNV_PCI_DIAG_BUF_SIZE;
3800
3801 phb->diag_data = memblock_virt_alloc(phb->diag_data_size, 0);
3802
Gavin Shanaa0c0332013-04-25 19:20:57 +00003803 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003804 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003805
Gavin Shanaa0c0332013-04-25 19:20:57 +00003806 /* Get registers */
Benjamin Herrenschmidtfd141d1a2016-07-08 16:37:14 +10003807 if (!of_address_to_resource(np, 0, &r)) {
3808 phb->regs_phys = r.start;
3809 phb->regs = ioremap(r.start, resource_size(&r));
3810 if (phb->regs == NULL)
3811 pr_err(" Failed to map registers !\n");
3812 }
Gavin Shan577c8c82016-05-20 16:41:28 +10003813
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003814 /* Initialize more IODA stuff */
Gavin Shan92b8f132016-05-03 15:41:24 +10003815 phb->ioda.total_pe_num = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003816 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003817 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003818 phb->ioda.total_pe_num = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003819 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3820 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003821 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003822
Gavin Shanc1275622016-05-20 16:41:29 +10003823 /* Invalidate RID to PE# mapping */
3824 for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
3825 phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
3826
Guo Chao262af552014-07-21 14:42:30 +10003827 /* Parse 64-bit MMIO range */
3828 pnv_ioda_parse_m64_window(phb);
3829
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003830 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003831 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003832 phb->ioda.m32_size += 0x10000;
3833
Gavin Shan92b8f132016-05-03 15:41:24 +10003834 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003835 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003836 phb->ioda.io_size = hose->pci_io_size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003837 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003838 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3839
Gavin Shan2b923ed2016-05-05 12:04:16 +10003840 /* Calculate how many 32-bit TCE segments we have */
3841 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
3842 PNV_IODA1_DMA32_SEGSIZE;
3843
Gavin Shanc35d2a82013-07-31 16:47:04 +08003844 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Alexey Kardashevskiy92a86752016-05-12 15:47:09 +10003845 size = _ALIGN_UP(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
3846 sizeof(unsigned long));
Gavin Shan93289d82016-05-03 15:41:29 +10003847 m64map_off = size;
3848 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003849 m32map_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003850 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003851 if (phb->type == PNV_PHB_IODA1) {
3852 iomap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003853 size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
Gavin Shan2b923ed2016-05-05 12:04:16 +10003854 dma32map_off = size;
3855 size += phb->ioda.dma32_count *
3856 sizeof(phb->ioda.dma32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003857 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003858 pemap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003859 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003860 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003861 phb->ioda.pe_alloc = aux;
Gavin Shan93289d82016-05-03 15:41:29 +10003862 phb->ioda.m64_segmap = aux + m64map_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003863 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shan93289d82016-05-03 15:41:29 +10003864 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
3865 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003866 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan93289d82016-05-03 15:41:29 +10003867 }
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003868 if (phb->type == PNV_PHB_IODA1) {
Gavin Shanc35d2a82013-07-31 16:47:04 +08003869 phb->ioda.io_segmap = aux + iomap_off;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003870 for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
3871 phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003872
3873 phb->ioda.dma32_segmap = aux + dma32map_off;
3874 for (segno = 0; segno < phb->ioda.dma32_count; segno++)
3875 phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003876 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003877 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan63803c32016-05-20 16:41:32 +10003878
3879 /*
3880 * Choose PE number for root bus, which shouldn't have
3881 * M64 resources consumed by its child devices. To pick
3882 * the PE number adjacent to the reserved one if possible.
3883 */
3884 pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
3885 if (phb->ioda.reserved_pe_idx == 0) {
3886 phb->ioda.root_pe_idx = 1;
3887 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3888 } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
3889 phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
3890 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
3891 } else {
3892 phb->ioda.root_pe_idx = IODA_INVALID_PE;
3893 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003894
3895 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003896 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003897
3898 /* Calculate how many 32-bit TCE segments we have */
Gavin Shan2b923ed2016-05-05 12:04:16 +10003899 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
Gavin Shanacce9712016-05-03 15:41:33 +10003900 PNV_IODA1_DMA32_SEGSIZE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003901
Gavin Shanaa0c0332013-04-25 19:20:57 +00003902#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003903 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3904 window_type,
3905 window_num,
3906 starting_real_address,
3907 starting_pci_address,
3908 segment_size);
3909#endif
3910
Guo Chao262af552014-07-21 14:42:30 +10003911 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
Gavin Shan92b8f132016-05-03 15:41:24 +10003912 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
Guo Chao262af552014-07-21 14:42:30 +10003913 phb->ioda.m32_size, phb->ioda.m32_segsize);
3914 if (phb->ioda.m64_size)
3915 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3916 phb->ioda.m64_size, phb->ioda.m64_segsize);
3917 if (phb->ioda.io_size)
3918 pr_info(" IO: 0x%x [segment=0x%x]\n",
3919 phb->ioda.io_size, phb->ioda.io_segsize);
3920
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003921
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003922 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003923 phb->get_pe_state = pnv_ioda_get_pe_state;
3924 phb->freeze_pe = pnv_ioda_freeze_pe;
3925 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003926
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003927 /* Setup MSI support */
3928 pnv_pci_init_ioda_msis(phb);
3929
Gavin Shanc40a4212012-08-20 03:49:20 +00003930 /*
3931 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3932 * to let the PCI core do resource assignment. It's supposed
3933 * that the PCI core will do correct I/O and MMIO alignment
3934 * for the P2P bridge bars so that each PCI bus (excluding
3935 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003936 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003937 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003938
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01003939 switch (phb->type) {
3940 case PNV_PHB_NPU_NVLINK:
Alistair Popple5d2aa712015-12-17 13:43:13 +11003941 hose->controller_ops = pnv_npu_ioda_controller_ops;
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01003942 break;
3943 case PNV_PHB_NPU_OCAPI:
3944 hose->controller_ops = pnv_npu_ocapi_ioda_controller_ops;
3945 break;
3946 default:
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003947 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003948 hose->controller_ops = pnv_pci_ioda_controller_ops;
Alexey Kardashevskiyf9f83452016-04-29 18:55:20 +10003949 }
Michael Ellermanad30cb92015-04-14 09:29:23 +10003950
Yongji Xie38274632017-04-10 19:58:13 +08003951 ppc_md.pcibios_default_alignment = pnv_pci_default_alignment;
3952
Wei Yang6e628c72015-03-25 16:23:55 +08003953#ifdef CONFIG_PCI_IOV
3954 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003955 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Bryant G. Ly988fc3b2017-11-09 08:00:33 -06003956 ppc_md.pcibios_sriov_enable = pnv_pcibios_sriov_enable;
3957 ppc_md.pcibios_sriov_disable = pnv_pcibios_sriov_disable;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003958#endif
3959
Gavin Shanc40a4212012-08-20 03:49:20 +00003960 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003961
3962 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003963 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003964 if (rc)
Joe Perchesf2c2cbc2016-10-24 21:00:08 -07003965 pr_warn(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003966
Andrew Donnellan6060e9e2016-09-16 20:39:44 +10003967 /*
3968 * If we're running in kdump kernel, the previous kernel never
Gavin Shan361f2a22014-04-24 18:00:25 +10003969 * shutdown PCI devices correctly. We already got IODA table
3970 * cleaned out. So we have to issue PHB reset to stop all PCI
Guilherme G. Piccoli45baee12017-11-17 16:58:59 -02003971 * transactions from previous kernel. The ppc_pci_reset_phbs
3972 * kernel parameter will force this reset too.
Gavin Shan361f2a22014-04-24 18:00:25 +10003973 */
Guilherme G. Piccoli45baee12017-11-17 16:58:59 -02003974 if (is_kdump_kernel() || pci_reset_phbs) {
Gavin Shan361f2a22014-04-24 18:00:25 +10003975 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003976 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3977 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003978 }
Guo Chao262af552014-07-21 14:42:30 +10003979
Gavin Shan9e9e8932014-11-12 13:36:05 +11003980 /* Remove M64 resource if we can't configure it successfully */
3981 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003982 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003983}
3984
Bjorn Helgaas67975002013-07-02 12:20:03 -06003985void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003986{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003987 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003988}
3989
Alistair Popple5d2aa712015-12-17 13:43:13 +11003990void __init pnv_pci_init_npu_phb(struct device_node *np)
3991{
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01003992 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_NVLINK);
Alistair Popple5d2aa712015-12-17 13:43:13 +11003993}
3994
Frederic Barrat7f2c39e2018-01-23 12:31:36 +01003995void __init pnv_pci_init_npu2_opencapi_phb(struct device_node *np)
3996{
3997 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_OCAPI);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003998}
3999
Andrew Donnellan228c2f42018-01-23 12:31:37 +01004000static void pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev *dev)
4001{
4002 struct pci_controller *hose = pci_bus_to_host(dev->bus);
4003 struct pnv_phb *phb = hose->private_data;
4004
4005 if (!machine_is(powernv))
4006 return;
4007
4008 if (phb->type == PNV_PHB_NPU_OCAPI)
4009 dev->cfg_size = PCI_CFG_SPACE_EXP_SIZE;
4010}
4011DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pnv_npu2_opencapi_cfg_size_fixup);
4012
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00004013void __init pnv_pci_init_ioda_hub(struct device_node *np)
4014{
4015 struct device_node *phbn;
4016 const __be64 *prop64;
4017 u64 hub_id;
4018
Rob Herringb7c670d2017-08-21 10:16:47 -05004019 pr_info("Probing IODA IO-Hub %pOF\n", np);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00004020
4021 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
4022 if (!prop64) {
4023 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
4024 return;
4025 }
4026 hub_id = be64_to_cpup(prop64);
4027 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
4028
4029 /* Count child PHBs */
4030 for_each_child_of_node(np, phbn) {
4031 /* Look for IODA1 PHBs */
4032 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
4033 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
4034 }
4035}