blob: 9b99aa11f6e9e2e45543f5706313e785f4175be2 [file] [log] [blame]
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001/*
2 * Support PCI/PCIe on PowerNV platforms
3 *
4 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +000012#undef DEBUG
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000013
14#include <linux/kernel.h>
15#include <linux/pci.h>
Gavin Shan361f2a22014-04-24 18:00:25 +100016#include <linux/crash_dump.h>
Gavin Shan37c367f2013-06-20 18:13:25 +080017#include <linux/debugfs.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000018#include <linux/delay.h>
19#include <linux/string.h>
20#include <linux/init.h>
21#include <linux/bootmem.h>
22#include <linux/irq.h>
23#include <linux/io.h>
24#include <linux/msi.h>
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +110025#include <linux/memblock.h>
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +100026#include <linux/iommu.h>
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +100027#include <linux/rculist.h>
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +100028#include <linux/sizes.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000029
30#include <asm/sections.h>
31#include <asm/io.h>
32#include <asm/prom.h>
33#include <asm/pci-bridge.h>
34#include <asm/machdep.h>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000035#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000036#include <asm/ppc-pci.h>
37#include <asm/opal.h>
38#include <asm/iommu.h>
39#include <asm/tce.h>
Gavin Shan137436c2013-04-25 19:20:59 +000040#include <asm/xics.h>
Gavin Shan37c367f2013-06-20 18:13:25 +080041#include <asm/debug.h>
Guo Chao262af552014-07-21 14:42:30 +100042#include <asm/firmware.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110043#include <asm/pnv-pci.h>
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100044#include <asm/mmzone.h>
Ian Munsie80c49c72014-10-08 19:54:57 +110045
Michael Neulingec249dd2015-05-27 16:07:16 +100046#include <misc/cxl-base.h>
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000047
48#include "powernv.h"
49#include "pci.h"
50
Gavin Shan99451552016-05-05 12:02:13 +100051#define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */
52#define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */
Gavin Shanacce9712016-05-03 15:41:33 +100053#define PNV_IODA1_DMA32_SEGSIZE 0x10000000
Wei Yang781a8682015-03-25 16:23:57 +080054
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +100055#define POWERNV_IOMMU_DEFAULT_LEVELS 1
56#define POWERNV_IOMMU_MAX_LEVELS 5
57
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100058static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
59
Joe Perches6d31c2f2014-09-21 10:55:06 -070060static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
61 const char *fmt, ...)
62{
63 struct va_format vaf;
64 va_list args;
65 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000066
Joe Perches6d31c2f2014-09-21 10:55:06 -070067 va_start(args, fmt);
68
69 vaf.fmt = fmt;
70 vaf.va = &args;
71
Wei Yang781a8682015-03-25 16:23:57 +080072 if (pe->flags & PNV_IODA_PE_DEV)
Joe Perches6d31c2f2014-09-21 10:55:06 -070073 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
Wei Yang781a8682015-03-25 16:23:57 +080074 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Joe Perches6d31c2f2014-09-21 10:55:06 -070075 sprintf(pfix, "%04x:%02x ",
76 pci_domain_nr(pe->pbus), pe->pbus->number);
Wei Yang781a8682015-03-25 16:23:57 +080077#ifdef CONFIG_PCI_IOV
78 else if (pe->flags & PNV_IODA_PE_VF)
79 sprintf(pfix, "%04x:%02x:%2x.%d",
80 pci_domain_nr(pe->parent_dev->bus),
81 (pe->rid & 0xff00) >> 8,
82 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
83#endif /* CONFIG_PCI_IOV*/
Joe Perches6d31c2f2014-09-21 10:55:06 -070084
85 printk("%spci %s: [PE# %.3d] %pV",
86 level, pfix, pe->pe_number, &vaf);
87
88 va_end(args);
89}
90
91#define pe_err(pe, fmt, ...) \
92 pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
93#define pe_warn(pe, fmt, ...) \
94 pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
95#define pe_info(pe, fmt, ...) \
96 pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000097
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020098static bool pnv_iommu_bypass_disabled __read_mostly;
99
100static int __init iommu_setup(char *str)
101{
102 if (!str)
103 return -EINVAL;
104
105 while (*str) {
106 if (!strncmp(str, "nobypass", 8)) {
107 pnv_iommu_bypass_disabled = true;
108 pr_info("PowerNV: IOMMU bypass window disabled.\n");
109 break;
110 }
111 str += strcspn(str, ",");
112 if (*str == ',')
113 str++;
114 }
115
116 return 0;
117}
118early_param("iommu", iommu_setup);
119
Guo Chao262af552014-07-21 14:42:30 +1000120static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
121{
122 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
123 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
124}
125
Gavin Shan1e916772016-05-03 15:41:36 +1000126static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
127{
128 phb->ioda.pe_array[pe_no].phb = phb;
129 phb->ioda.pe_array[pe_no].pe_number = pe_no;
130
131 return &phb->ioda.pe_array[pe_no];
132}
133
Gavin Shan4b82ab12014-11-12 13:36:07 +1100134static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
135{
Gavin Shan92b8f132016-05-03 15:41:24 +1000136 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100137 pr_warn("%s: Invalid PE %d on PHB#%x\n",
138 __func__, pe_no, phb->hose->global_number);
139 return;
140 }
141
Gavin Shane9dc4d72015-06-19 12:26:16 +1000142 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
143 pr_debug("%s: PE %d was reserved on PHB#%x\n",
144 __func__, pe_no, phb->hose->global_number);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100145
Gavin Shan1e916772016-05-03 15:41:36 +1000146 pnv_ioda_init_pe(phb, pe_no);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100147}
148
Gavin Shan1e916772016-05-03 15:41:36 +1000149static struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000150{
151 unsigned long pe;
152
153 do {
154 pe = find_next_zero_bit(phb->ioda.pe_alloc,
Gavin Shan92b8f132016-05-03 15:41:24 +1000155 phb->ioda.total_pe_num, 0);
156 if (pe >= phb->ioda.total_pe_num)
Gavin Shan1e916772016-05-03 15:41:36 +1000157 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000158 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
159
Gavin Shan1e916772016-05-03 15:41:36 +1000160 return pnv_ioda_init_pe(phb, pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000161}
162
Gavin Shan1e916772016-05-03 15:41:36 +1000163static void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000164{
Gavin Shan1e916772016-05-03 15:41:36 +1000165 struct pnv_phb *phb = pe->phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000166
Gavin Shan1e916772016-05-03 15:41:36 +1000167 WARN_ON(pe->pdev);
168
169 memset(pe, 0, sizeof(struct pnv_ioda_pe));
170 clear_bit(pe->pe_number, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000171}
172
Guo Chao262af552014-07-21 14:42:30 +1000173/* The default M64 BAR is shared by all PEs */
174static int pnv_ioda2_init_m64(struct pnv_phb *phb)
175{
176 const char *desc;
177 struct resource *r;
178 s64 rc;
179
180 /* Configure the default M64 BAR */
181 rc = opal_pci_set_phb_mem_window(phb->opal_id,
182 OPAL_M64_WINDOW_TYPE,
183 phb->ioda.m64_bar_idx,
184 phb->ioda.m64_base,
185 0, /* unused */
186 phb->ioda.m64_size);
187 if (rc != OPAL_SUCCESS) {
188 desc = "configuring";
189 goto fail;
190 }
191
192 /* Enable the default M64 BAR */
193 rc = opal_pci_phb_mmio_enable(phb->opal_id,
194 OPAL_M64_WINDOW_TYPE,
195 phb->ioda.m64_bar_idx,
196 OPAL_ENABLE_M64_SPLIT);
197 if (rc != OPAL_SUCCESS) {
198 desc = "enabling";
199 goto fail;
200 }
201
202 /* Mark the M64 BAR assigned */
203 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
204
205 /*
206 * Strip off the segment used by the reserved PE, which is
207 * expected to be 0 or last one of PE capabicity.
208 */
209 r = &phb->hose->mem_resources[1];
Gavin Shan92b8f132016-05-03 15:41:24 +1000210 if (phb->ioda.reserved_pe_idx == 0)
Guo Chao262af552014-07-21 14:42:30 +1000211 r->start += phb->ioda.m64_segsize;
Gavin Shan92b8f132016-05-03 15:41:24 +1000212 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Guo Chao262af552014-07-21 14:42:30 +1000213 r->end -= phb->ioda.m64_segsize;
214 else
215 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
Gavin Shan92b8f132016-05-03 15:41:24 +1000216 phb->ioda.reserved_pe_idx);
Guo Chao262af552014-07-21 14:42:30 +1000217
218 return 0;
219
220fail:
221 pr_warn(" Failure %lld %s M64 BAR#%d\n",
222 rc, desc, phb->ioda.m64_bar_idx);
223 opal_pci_phb_mmio_enable(phb->opal_id,
224 OPAL_M64_WINDOW_TYPE,
225 phb->ioda.m64_bar_idx,
226 OPAL_DISABLE_M64);
227 return -EIO;
228}
229
Gavin Shanc4306702016-05-03 15:41:30 +1000230static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
Gavin Shan96a2f922015-06-19 12:26:17 +1000231 unsigned long *pe_bitmap)
Guo Chao262af552014-07-21 14:42:30 +1000232{
Gavin Shan96a2f922015-06-19 12:26:17 +1000233 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
234 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000235 struct resource *r;
Gavin Shan96a2f922015-06-19 12:26:17 +1000236 resource_size_t base, sgsz, start, end;
237 int segno, i;
Guo Chao262af552014-07-21 14:42:30 +1000238
Gavin Shan96a2f922015-06-19 12:26:17 +1000239 base = phb->ioda.m64_base;
240 sgsz = phb->ioda.m64_segsize;
241 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
242 r = &pdev->resource[i];
243 if (!r->parent || !pnv_pci_is_mem_pref_64(r->flags))
244 continue;
Guo Chao262af552014-07-21 14:42:30 +1000245
Gavin Shan96a2f922015-06-19 12:26:17 +1000246 start = _ALIGN_DOWN(r->start - base, sgsz);
247 end = _ALIGN_UP(r->end - base, sgsz);
248 for (segno = start / sgsz; segno < end / sgsz; segno++) {
249 if (pe_bitmap)
250 set_bit(segno, pe_bitmap);
251 else
252 pnv_ioda_reserve_pe(phb, segno);
Guo Chao262af552014-07-21 14:42:30 +1000253 }
254 }
255}
256
Gavin Shan99451552016-05-05 12:02:13 +1000257static int pnv_ioda1_init_m64(struct pnv_phb *phb)
258{
259 struct resource *r;
260 int index;
261
262 /*
263 * There are 16 M64 BARs, each of which has 8 segments. So
264 * there are as many M64 segments as the maximum number of
265 * PEs, which is 128.
266 */
267 for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
268 unsigned long base, segsz = phb->ioda.m64_segsize;
269 int64_t rc;
270
271 base = phb->ioda.m64_base +
272 index * PNV_IODA1_M64_SEGS * segsz;
273 rc = opal_pci_set_phb_mem_window(phb->opal_id,
274 OPAL_M64_WINDOW_TYPE, index, base, 0,
275 PNV_IODA1_M64_SEGS * segsz);
276 if (rc != OPAL_SUCCESS) {
277 pr_warn(" Error %lld setting M64 PHB#%d-BAR#%d\n",
278 rc, phb->hose->global_number, index);
279 goto fail;
280 }
281
282 rc = opal_pci_phb_mmio_enable(phb->opal_id,
283 OPAL_M64_WINDOW_TYPE, index,
284 OPAL_ENABLE_M64_SPLIT);
285 if (rc != OPAL_SUCCESS) {
286 pr_warn(" Error %lld enabling M64 PHB#%d-BAR#%d\n",
287 rc, phb->hose->global_number, index);
288 goto fail;
289 }
290 }
291
292 /*
293 * Exclude the segment used by the reserved PE, which
294 * is expected to be 0 or last supported PE#.
295 */
296 r = &phb->hose->mem_resources[1];
297 if (phb->ioda.reserved_pe_idx == 0)
298 r->start += phb->ioda.m64_segsize;
299 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
300 r->end -= phb->ioda.m64_segsize;
301 else
302 WARN(1, "Wrong reserved PE#%d on PHB#%d\n",
303 phb->ioda.reserved_pe_idx, phb->hose->global_number);
304
305 return 0;
306
307fail:
308 for ( ; index >= 0; index--)
309 opal_pci_phb_mmio_enable(phb->opal_id,
310 OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
311
312 return -EIO;
313}
314
Gavin Shanc4306702016-05-03 15:41:30 +1000315static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
316 unsigned long *pe_bitmap,
317 bool all)
Guo Chao262af552014-07-21 14:42:30 +1000318{
Guo Chao262af552014-07-21 14:42:30 +1000319 struct pci_dev *pdev;
Gavin Shan96a2f922015-06-19 12:26:17 +1000320
321 list_for_each_entry(pdev, &bus->devices, bus_list) {
Gavin Shanc4306702016-05-03 15:41:30 +1000322 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
Gavin Shan96a2f922015-06-19 12:26:17 +1000323
324 if (all && pdev->subordinate)
Gavin Shanc4306702016-05-03 15:41:30 +1000325 pnv_ioda_reserve_m64_pe(pdev->subordinate,
326 pe_bitmap, all);
Gavin Shan96a2f922015-06-19 12:26:17 +1000327 }
328}
329
Gavin Shan1e916772016-05-03 15:41:36 +1000330static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
Guo Chao262af552014-07-21 14:42:30 +1000331{
Gavin Shan26ba2482015-06-19 12:26:19 +1000332 struct pci_controller *hose = pci_bus_to_host(bus);
333 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000334 struct pnv_ioda_pe *master_pe, *pe;
335 unsigned long size, *pe_alloc;
Gavin Shan26ba2482015-06-19 12:26:19 +1000336 int i;
Guo Chao262af552014-07-21 14:42:30 +1000337
338 /* Root bus shouldn't use M64 */
339 if (pci_is_root_bus(bus))
Gavin Shan1e916772016-05-03 15:41:36 +1000340 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000341
Guo Chao262af552014-07-21 14:42:30 +1000342 /* Allocate bitmap */
Gavin Shan92b8f132016-05-03 15:41:24 +1000343 size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
Guo Chao262af552014-07-21 14:42:30 +1000344 pe_alloc = kzalloc(size, GFP_KERNEL);
345 if (!pe_alloc) {
346 pr_warn("%s: Out of memory !\n",
347 __func__);
Gavin Shan1e916772016-05-03 15:41:36 +1000348 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000349 }
350
Gavin Shan26ba2482015-06-19 12:26:19 +1000351 /* Figure out reserved PE numbers by the PE */
Gavin Shanc4306702016-05-03 15:41:30 +1000352 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
Guo Chao262af552014-07-21 14:42:30 +1000353
354 /*
355 * the current bus might not own M64 window and that's all
356 * contributed by its child buses. For the case, we needn't
357 * pick M64 dependent PE#.
358 */
Gavin Shan92b8f132016-05-03 15:41:24 +1000359 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
Guo Chao262af552014-07-21 14:42:30 +1000360 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000361 return NULL;
Guo Chao262af552014-07-21 14:42:30 +1000362 }
363
364 /*
365 * Figure out the master PE and put all slave PEs to master
366 * PE's list to form compound PE.
367 */
Guo Chao262af552014-07-21 14:42:30 +1000368 master_pe = NULL;
369 i = -1;
Gavin Shan92b8f132016-05-03 15:41:24 +1000370 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
371 phb->ioda.total_pe_num) {
Guo Chao262af552014-07-21 14:42:30 +1000372 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000373
Gavin Shan93289d82016-05-03 15:41:29 +1000374 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
Guo Chao262af552014-07-21 14:42:30 +1000375 if (!master_pe) {
376 pe->flags |= PNV_IODA_PE_MASTER;
377 INIT_LIST_HEAD(&pe->slaves);
378 master_pe = pe;
379 } else {
380 pe->flags |= PNV_IODA_PE_SLAVE;
381 pe->master = master_pe;
382 list_add_tail(&pe->list, &master_pe->slaves);
383 }
Gavin Shan99451552016-05-05 12:02:13 +1000384
385 /*
386 * P7IOC supports M64DT, which helps mapping M64 segment
387 * to one particular PE#. However, PHB3 has fixed mapping
388 * between M64 segment and PE#. In order to have same logic
389 * for P7IOC and PHB3, we enforce fixed mapping between M64
390 * segment and PE# on P7IOC.
391 */
392 if (phb->type == PNV_PHB_IODA1) {
393 int64_t rc;
394
395 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
396 pe->pe_number, OPAL_M64_WINDOW_TYPE,
397 pe->pe_number / PNV_IODA1_M64_SEGS,
398 pe->pe_number % PNV_IODA1_M64_SEGS);
399 if (rc != OPAL_SUCCESS)
400 pr_warn("%s: Error %lld mapping M64 for PHB#%d-PE#%d\n",
401 __func__, rc, phb->hose->global_number,
402 pe->pe_number);
403 }
Guo Chao262af552014-07-21 14:42:30 +1000404 }
405
406 kfree(pe_alloc);
Gavin Shan1e916772016-05-03 15:41:36 +1000407 return master_pe;
Guo Chao262af552014-07-21 14:42:30 +1000408}
409
410static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
411{
412 struct pci_controller *hose = phb->hose;
413 struct device_node *dn = hose->dn;
414 struct resource *res;
415 const u32 *r;
416 u64 pci_addr;
417
Gavin Shan99451552016-05-05 12:02:13 +1000418 if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
Gavin Shan1665c4a2014-11-12 13:36:04 +1100419 pr_info(" Not support M64 window\n");
420 return;
421 }
422
Stewart Smithe4d54f72015-12-09 17:18:20 +1100423 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
Guo Chao262af552014-07-21 14:42:30 +1000424 pr_info(" Firmware too old to support M64 window\n");
425 return;
426 }
427
428 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
429 if (!r) {
430 pr_info(" No <ibm,opal-m64-window> on %s\n",
431 dn->full_name);
432 return;
433 }
434
Guo Chao262af552014-07-21 14:42:30 +1000435 res = &hose->mem_resources[1];
Gavin Shane80c4e72015-10-22 12:03:08 +1100436 res->name = dn->full_name;
Guo Chao262af552014-07-21 14:42:30 +1000437 res->start = of_translate_address(dn, r + 2);
438 res->end = res->start + of_read_number(r + 4, 2) - 1;
439 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
440 pci_addr = of_read_number(r, 2);
441 hose->mem_offset[1] = res->start - pci_addr;
442
443 phb->ioda.m64_size = resource_size(res);
Gavin Shan92b8f132016-05-03 15:41:24 +1000444 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
Guo Chao262af552014-07-21 14:42:30 +1000445 phb->ioda.m64_base = pci_addr;
446
Wei Yange9863e62014-12-12 12:39:37 +0800447 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
448 res->start, res->end, pci_addr);
449
Guo Chao262af552014-07-21 14:42:30 +1000450 /* Use last M64 BAR to cover M64 window */
451 phb->ioda.m64_bar_idx = 15;
Gavin Shan99451552016-05-05 12:02:13 +1000452 if (phb->type == PNV_PHB_IODA1)
453 phb->init_m64 = pnv_ioda1_init_m64;
454 else
455 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shanc4306702016-05-03 15:41:30 +1000456 phb->reserve_m64_pe = pnv_ioda_reserve_m64_pe;
457 phb->pick_m64_pe = pnv_ioda_pick_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000458}
459
Gavin Shan49dec922014-07-21 14:42:33 +1000460static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
461{
462 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
463 struct pnv_ioda_pe *slave;
464 s64 rc;
465
466 /* Fetch master PE */
467 if (pe->flags & PNV_IODA_PE_SLAVE) {
468 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100469 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
470 return;
471
Gavin Shan49dec922014-07-21 14:42:33 +1000472 pe_no = pe->pe_number;
473 }
474
475 /* Freeze master PE */
476 rc = opal_pci_eeh_freeze_set(phb->opal_id,
477 pe_no,
478 OPAL_EEH_ACTION_SET_FREEZE_ALL);
479 if (rc != OPAL_SUCCESS) {
480 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
481 __func__, rc, phb->hose->global_number, pe_no);
482 return;
483 }
484
485 /* Freeze slave PEs */
486 if (!(pe->flags & PNV_IODA_PE_MASTER))
487 return;
488
489 list_for_each_entry(slave, &pe->slaves, list) {
490 rc = opal_pci_eeh_freeze_set(phb->opal_id,
491 slave->pe_number,
492 OPAL_EEH_ACTION_SET_FREEZE_ALL);
493 if (rc != OPAL_SUCCESS)
494 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
495 __func__, rc, phb->hose->global_number,
496 slave->pe_number);
497 }
498}
499
Anton Blancharde51df2c2014-08-20 08:55:18 +1000500static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000501{
502 struct pnv_ioda_pe *pe, *slave;
503 s64 rc;
504
505 /* Find master PE */
506 pe = &phb->ioda.pe_array[pe_no];
507 if (pe->flags & PNV_IODA_PE_SLAVE) {
508 pe = pe->master;
509 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
510 pe_no = pe->pe_number;
511 }
512
513 /* Clear frozen state for master PE */
514 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
515 if (rc != OPAL_SUCCESS) {
516 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
517 __func__, rc, opt, phb->hose->global_number, pe_no);
518 return -EIO;
519 }
520
521 if (!(pe->flags & PNV_IODA_PE_MASTER))
522 return 0;
523
524 /* Clear frozen state for slave PEs */
525 list_for_each_entry(slave, &pe->slaves, list) {
526 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
527 slave->pe_number,
528 opt);
529 if (rc != OPAL_SUCCESS) {
530 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
531 __func__, rc, opt, phb->hose->global_number,
532 slave->pe_number);
533 return -EIO;
534 }
535 }
536
537 return 0;
538}
539
540static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
541{
542 struct pnv_ioda_pe *slave, *pe;
543 u8 fstate, state;
544 __be16 pcierr;
545 s64 rc;
546
547 /* Sanity check on PE number */
Gavin Shan92b8f132016-05-03 15:41:24 +1000548 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
Gavin Shan49dec922014-07-21 14:42:33 +1000549 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
550
551 /*
552 * Fetch the master PE and the PE instance might be
553 * not initialized yet.
554 */
555 pe = &phb->ioda.pe_array[pe_no];
556 if (pe->flags & PNV_IODA_PE_SLAVE) {
557 pe = pe->master;
558 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
559 pe_no = pe->pe_number;
560 }
561
562 /* Check the master PE */
563 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
564 &state, &pcierr, NULL);
565 if (rc != OPAL_SUCCESS) {
566 pr_warn("%s: Failure %lld getting "
567 "PHB#%x-PE#%x state\n",
568 __func__, rc,
569 phb->hose->global_number, pe_no);
570 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
571 }
572
573 /* Check the slave PE */
574 if (!(pe->flags & PNV_IODA_PE_MASTER))
575 return state;
576
577 list_for_each_entry(slave, &pe->slaves, list) {
578 rc = opal_pci_eeh_freeze_status(phb->opal_id,
579 slave->pe_number,
580 &fstate,
581 &pcierr,
582 NULL);
583 if (rc != OPAL_SUCCESS) {
584 pr_warn("%s: Failure %lld getting "
585 "PHB#%x-PE#%x state\n",
586 __func__, rc,
587 phb->hose->global_number, slave->pe_number);
588 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
589 }
590
591 /*
592 * Override the result based on the ascending
593 * priority.
594 */
595 if (fstate > state)
596 state = fstate;
597 }
598
599 return state;
600}
601
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000602/* Currently those 2 are only used when MSIs are enabled, this will change
603 * but in the meantime, we need to protect them to avoid warnings
604 */
605#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800606static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000607{
608 struct pci_controller *hose = pci_bus_to_host(dev->bus);
609 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000610 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000611
612 if (!pdn)
613 return NULL;
614 if (pdn->pe_number == IODA_INVALID_PE)
615 return NULL;
616 return &phb->ioda.pe_array[pdn->pe_number];
617}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000618#endif /* CONFIG_PCI_MSI */
619
Gavin Shanb131a842014-11-12 13:36:08 +1100620static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
621 struct pnv_ioda_pe *parent,
622 struct pnv_ioda_pe *child,
623 bool is_add)
624{
625 const char *desc = is_add ? "adding" : "removing";
626 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
627 OPAL_REMOVE_PE_FROM_DOMAIN;
628 struct pnv_ioda_pe *slave;
629 long rc;
630
631 /* Parent PE affects child PE */
632 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
633 child->pe_number, op);
634 if (rc != OPAL_SUCCESS) {
635 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
636 rc, desc);
637 return -ENXIO;
638 }
639
640 if (!(child->flags & PNV_IODA_PE_MASTER))
641 return 0;
642
643 /* Compound case: parent PE affects slave PEs */
644 list_for_each_entry(slave, &child->slaves, list) {
645 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
646 slave->pe_number, op);
647 if (rc != OPAL_SUCCESS) {
648 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
649 rc, desc);
650 return -ENXIO;
651 }
652 }
653
654 return 0;
655}
656
657static int pnv_ioda_set_peltv(struct pnv_phb *phb,
658 struct pnv_ioda_pe *pe,
659 bool is_add)
660{
661 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800662 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100663 int ret;
664
665 /*
666 * Clear PE frozen state. If it's master PE, we need
667 * clear slave PE frozen state as well.
668 */
669 if (is_add) {
670 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
671 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
672 if (pe->flags & PNV_IODA_PE_MASTER) {
673 list_for_each_entry(slave, &pe->slaves, list)
674 opal_pci_eeh_freeze_clear(phb->opal_id,
675 slave->pe_number,
676 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
677 }
678 }
679
680 /*
681 * Associate PE in PELT. We need add the PE into the
682 * corresponding PELT-V as well. Otherwise, the error
683 * originated from the PE might contribute to other
684 * PEs.
685 */
686 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
687 if (ret)
688 return ret;
689
690 /* For compound PEs, any one affects all of them */
691 if (pe->flags & PNV_IODA_PE_MASTER) {
692 list_for_each_entry(slave, &pe->slaves, list) {
693 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
694 if (ret)
695 return ret;
696 }
697 }
698
699 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
700 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800701 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100702 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800703#ifdef CONFIG_PCI_IOV
704 else if (pe->flags & PNV_IODA_PE_VF)
Gavin Shan283e2d82015-06-22 13:45:47 +1000705 pdev = pe->parent_dev;
Wei Yang781a8682015-03-25 16:23:57 +0800706#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100707 while (pdev) {
708 struct pci_dn *pdn = pci_get_pdn(pdev);
709 struct pnv_ioda_pe *parent;
710
711 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
712 parent = &phb->ioda.pe_array[pdn->pe_number];
713 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
714 if (ret)
715 return ret;
716 }
717
718 pdev = pdev->bus->self;
719 }
720
721 return 0;
722}
723
Wei Yang781a8682015-03-25 16:23:57 +0800724#ifdef CONFIG_PCI_IOV
725static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
726{
727 struct pci_dev *parent;
728 uint8_t bcomp, dcomp, fcomp;
729 int64_t rc;
730 long rid_end, rid;
731
732 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
733 if (pe->pbus) {
734 int count;
735
736 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
737 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
738 parent = pe->pbus->self;
739 if (pe->flags & PNV_IODA_PE_BUS_ALL)
740 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
741 else
742 count = 1;
743
744 switch(count) {
745 case 1: bcomp = OpalPciBusAll; break;
746 case 2: bcomp = OpalPciBus7Bits; break;
747 case 4: bcomp = OpalPciBus6Bits; break;
748 case 8: bcomp = OpalPciBus5Bits; break;
749 case 16: bcomp = OpalPciBus4Bits; break;
750 case 32: bcomp = OpalPciBus3Bits; break;
751 default:
752 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
753 count);
754 /* Do an exact match only */
755 bcomp = OpalPciBusAll;
756 }
757 rid_end = pe->rid + (count << 8);
758 } else {
759 if (pe->flags & PNV_IODA_PE_VF)
760 parent = pe->parent_dev;
761 else
762 parent = pe->pdev->bus->self;
763 bcomp = OpalPciBusAll;
764 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
765 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
766 rid_end = pe->rid + 1;
767 }
768
769 /* Clear the reverse map */
770 for (rid = pe->rid; rid < rid_end; rid++)
771 phb->ioda.pe_rmap[rid] = 0;
772
773 /* Release from all parents PELT-V */
774 while (parent) {
775 struct pci_dn *pdn = pci_get_pdn(parent);
776 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
777 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
778 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
779 /* XXX What to do in case of error ? */
780 }
781 parent = parent->bus->self;
782 }
783
Gavin Shanf951e512015-06-23 17:01:13 +1000784 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
Wei Yang781a8682015-03-25 16:23:57 +0800785 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
786
787 /* Disassociate PE in PELT */
788 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
789 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
790 if (rc)
791 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
792 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
793 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
794 if (rc)
795 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
796
797 pe->pbus = NULL;
798 pe->pdev = NULL;
799 pe->parent_dev = NULL;
800
801 return 0;
802}
803#endif /* CONFIG_PCI_IOV */
804
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800805static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000806{
807 struct pci_dev *parent;
808 uint8_t bcomp, dcomp, fcomp;
809 long rc, rid_end, rid;
810
811 /* Bus validation ? */
812 if (pe->pbus) {
813 int count;
814
815 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
816 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
817 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000818 if (pe->flags & PNV_IODA_PE_BUS_ALL)
819 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
820 else
821 count = 1;
822
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000823 switch(count) {
824 case 1: bcomp = OpalPciBusAll; break;
825 case 2: bcomp = OpalPciBus7Bits; break;
826 case 4: bcomp = OpalPciBus6Bits; break;
827 case 8: bcomp = OpalPciBus5Bits; break;
828 case 16: bcomp = OpalPciBus4Bits; break;
829 case 32: bcomp = OpalPciBus3Bits; break;
830 default:
Wei Yang781a8682015-03-25 16:23:57 +0800831 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
832 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000833 /* Do an exact match only */
834 bcomp = OpalPciBusAll;
835 }
836 rid_end = pe->rid + (count << 8);
837 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800838#ifdef CONFIG_PCI_IOV
839 if (pe->flags & PNV_IODA_PE_VF)
840 parent = pe->parent_dev;
841 else
842#endif /* CONFIG_PCI_IOV */
843 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000844 bcomp = OpalPciBusAll;
845 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
846 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
847 rid_end = pe->rid + 1;
848 }
849
Gavin Shan631ad692013-11-04 16:32:46 +0800850 /*
851 * Associate PE in PELT. We need add the PE into the
852 * corresponding PELT-V as well. Otherwise, the error
853 * originated from the PE might contribute to other
854 * PEs.
855 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000856 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
857 bcomp, dcomp, fcomp, OPAL_MAP_PE);
858 if (rc) {
859 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
860 return -ENXIO;
861 }
Gavin Shan631ad692013-11-04 16:32:46 +0800862
Alistair Popple5d2aa712015-12-17 13:43:13 +1100863 /*
864 * Configure PELTV. NPUs don't have a PELTV table so skip
865 * configuration on them.
866 */
867 if (phb->type != PNV_PHB_NPU)
868 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000869
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000870 /* Setup reverse map */
871 for (rid = pe->rid; rid < rid_end; rid++)
872 phb->ioda.pe_rmap[rid] = pe->pe_number;
873
874 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100875 if (phb->type != PNV_PHB_IODA1) {
876 pe->mve_number = 0;
877 goto out;
878 }
879
880 pe->mve_number = pe->pe_number;
881 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
882 if (rc != OPAL_SUCCESS) {
883 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
884 rc, pe->mve_number);
885 pe->mve_number = -1;
886 } else {
887 rc = opal_pci_set_mve_enable(phb->opal_id,
888 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000889 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100890 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000891 rc, pe->mve_number);
892 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000893 }
Gavin Shan4773f762014-11-12 13:36:09 +1100894 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000895
Gavin Shan4773f762014-11-12 13:36:09 +1100896out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000897 return 0;
898}
899
Wei Yang781a8682015-03-25 16:23:57 +0800900#ifdef CONFIG_PCI_IOV
901static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
902{
903 struct pci_dn *pdn = pci_get_pdn(dev);
904 int i;
905 struct resource *res, res2;
906 resource_size_t size;
907 u16 num_vfs;
908
909 if (!dev->is_physfn)
910 return -EINVAL;
911
912 /*
913 * "offset" is in VFs. The M64 windows are sized so that when they
914 * are segmented, each segment is the same size as the IOV BAR.
915 * Each segment is in a separate PE, and the high order bits of the
916 * address are the PE number. Therefore, each VF's BAR is in a
917 * separate PE, and changing the IOV BAR start address changes the
918 * range of PEs the VFs are in.
919 */
920 num_vfs = pdn->num_vfs;
921 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
922 res = &dev->resource[i + PCI_IOV_RESOURCES];
923 if (!res->flags || !res->parent)
924 continue;
925
Wei Yang781a8682015-03-25 16:23:57 +0800926 /*
927 * The actual IOV BAR range is determined by the start address
928 * and the actual size for num_vfs VFs BAR. This check is to
929 * make sure that after shifting, the range will not overlap
930 * with another device.
931 */
932 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
933 res2.flags = res->flags;
934 res2.start = res->start + (size * offset);
935 res2.end = res2.start + (size * num_vfs) - 1;
936
937 if (res2.end > res->end) {
938 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
939 i, &res2, res, num_vfs, offset);
940 return -EBUSY;
941 }
942 }
943
944 /*
945 * After doing so, there would be a "hole" in the /proc/iomem when
946 * offset is a positive value. It looks like the device return some
947 * mmio back to the system, which actually no one could use it.
948 */
949 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
950 res = &dev->resource[i + PCI_IOV_RESOURCES];
951 if (!res->flags || !res->parent)
952 continue;
953
Wei Yang781a8682015-03-25 16:23:57 +0800954 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
955 res2 = *res;
956 res->start += size * offset;
957
Wei Yang74703cc2015-07-20 18:14:58 +0800958 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
959 i, &res2, res, (offset > 0) ? "En" : "Dis",
960 num_vfs, offset);
Wei Yang781a8682015-03-25 16:23:57 +0800961 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
962 }
963 return 0;
964}
965#endif /* CONFIG_PCI_IOV */
966
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800967static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000968{
969 struct pci_controller *hose = pci_bus_to_host(dev->bus);
970 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000971 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000972 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000973
974 if (!pdn) {
975 pr_err("%s: Device tree node not associated properly\n",
976 pci_name(dev));
977 return NULL;
978 }
979 if (pdn->pe_number != IODA_INVALID_PE)
980 return NULL;
981
Gavin Shan1e916772016-05-03 15:41:36 +1000982 pe = pnv_ioda_alloc_pe(phb);
983 if (!pe) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000984 pr_warning("%s: Not enough PE# available, disabling device\n",
985 pci_name(dev));
986 return NULL;
987 }
988
989 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
990 * pointer in the PE data structure, both should be destroyed at the
991 * same time. However, this needs to be looked at more closely again
992 * once we actually start removing things (Hotplug, SR-IOV, ...)
993 *
994 * At some point we want to remove the PDN completely anyways
995 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000996 pci_dev_get(dev);
997 pdn->pcidev = dev;
Gavin Shan1e916772016-05-03 15:41:36 +1000998 pdn->pe_number = pe->pe_number;
Alistair Popple5d2aa712015-12-17 13:43:13 +1100999 pe->flags = PNV_IODA_PE_DEV;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001000 pe->pdev = dev;
1001 pe->pbus = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001002 pe->mve_number = -1;
1003 pe->rid = dev->bus->number << 8 | pdn->devfn;
1004
1005 pe_info(pe, "Associated device to PE\n");
1006
1007 if (pnv_ioda_configure_pe(phb, pe)) {
1008 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001009 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001010 pdn->pe_number = IODA_INVALID_PE;
1011 pe->pdev = NULL;
1012 pci_dev_put(dev);
1013 return NULL;
1014 }
1015
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001016 return pe;
1017}
1018
1019static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1020{
1021 struct pci_dev *dev;
1022
1023 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001024 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001025
1026 if (pdn == NULL) {
1027 pr_warn("%s: No device node associated with device !\n",
1028 pci_name(dev));
1029 continue;
1030 }
Alistair Popple94973b22015-12-17 13:43:11 +11001031 pdn->pcidev = dev;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001032 pdn->pe_number = pe->pe_number;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001033 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001034 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1035 }
1036}
1037
Gavin Shanfb446ad2012-08-20 03:49:14 +00001038/*
1039 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1040 * single PCI bus. Another one that contains the primary PCI bus and its
1041 * subordinate PCI devices and buses. The second type of PE is normally
1042 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1043 */
Gavin Shan1e916772016-05-03 15:41:36 +10001044static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001045{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001046 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001047 struct pnv_phb *phb = hose->private_data;
Gavin Shan1e916772016-05-03 15:41:36 +10001048 struct pnv_ioda_pe *pe = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001049
Guo Chao262af552014-07-21 14:42:30 +10001050 /* Check if PE is determined by M64 */
1051 if (phb->pick_m64_pe)
Gavin Shan1e916772016-05-03 15:41:36 +10001052 pe = phb->pick_m64_pe(bus, all);
Guo Chao262af552014-07-21 14:42:30 +10001053
1054 /* The PE number isn't pinned by M64 */
Gavin Shan1e916772016-05-03 15:41:36 +10001055 if (!pe)
1056 pe = pnv_ioda_alloc_pe(phb);
Guo Chao262af552014-07-21 14:42:30 +10001057
Gavin Shan1e916772016-05-03 15:41:36 +10001058 if (!pe) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001059 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1060 __func__, pci_domain_nr(bus), bus->number);
Gavin Shan1e916772016-05-03 15:41:36 +10001061 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001062 }
1063
Guo Chao262af552014-07-21 14:42:30 +10001064 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001065 pe->pbus = bus;
1066 pe->pdev = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001067 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001068 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001069
Gavin Shanfb446ad2012-08-20 03:49:14 +00001070 if (all)
1071 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001072 bus->busn_res.start, bus->busn_res.end, pe->pe_number);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001073 else
1074 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
Gavin Shan1e916772016-05-03 15:41:36 +10001075 bus->busn_res.start, pe->pe_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001076
1077 if (pnv_ioda_configure_pe(phb, pe)) {
1078 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001079 pnv_ioda_free_pe(pe);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001080 pe->pbus = NULL;
Gavin Shan1e916772016-05-03 15:41:36 +10001081 return NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001082 }
1083
1084 /* Associate it with all child devices */
1085 pnv_ioda_setup_same_PE(bus, pe);
1086
Gavin Shan7ebdf952012-08-20 03:49:15 +00001087 /* Put PE to the list */
1088 list_add_tail(&pe->list, &phb->ioda.pe_list);
Gavin Shan1e916772016-05-03 15:41:36 +10001089
1090 return pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001091}
1092
Alistair Poppleb5215492016-01-11 16:53:49 +11001093static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev)
Alistair Popple5d2aa712015-12-17 13:43:13 +11001094{
Alistair Poppleb5215492016-01-11 16:53:49 +11001095 int pe_num, found_pe = false, rc;
1096 long rid;
1097 struct pnv_ioda_pe *pe;
1098 struct pci_dev *gpu_pdev;
1099 struct pci_dn *npu_pdn;
1100 struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus);
1101 struct pnv_phb *phb = hose->private_data;
1102
1103 /*
1104 * Due to a hardware errata PE#0 on the NPU is reserved for
1105 * error handling. This means we only have three PEs remaining
1106 * which need to be assigned to four links, implying some
1107 * links must share PEs.
1108 *
1109 * To achieve this we assign PEs such that NPUs linking the
1110 * same GPU get assigned the same PE.
1111 */
1112 gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10001113 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
Alistair Poppleb5215492016-01-11 16:53:49 +11001114 pe = &phb->ioda.pe_array[pe_num];
1115 if (!pe->pdev)
1116 continue;
1117
1118 if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) {
1119 /*
1120 * This device has the same peer GPU so should
1121 * be assigned the same PE as the existing
1122 * peer NPU.
1123 */
1124 dev_info(&npu_pdev->dev,
1125 "Associating to existing PE %d\n", pe_num);
1126 pci_dev_get(npu_pdev);
1127 npu_pdn = pci_get_pdn(npu_pdev);
1128 rid = npu_pdev->bus->number << 8 | npu_pdn->devfn;
1129 npu_pdn->pcidev = npu_pdev;
1130 npu_pdn->pe_number = pe_num;
Alistair Poppleb5215492016-01-11 16:53:49 +11001131 phb->ioda.pe_rmap[rid] = pe->pe_number;
1132
1133 /* Map the PE to this link */
1134 rc = opal_pci_set_pe(phb->opal_id, pe_num, rid,
1135 OpalPciBusAll,
1136 OPAL_COMPARE_RID_DEVICE_NUMBER,
1137 OPAL_COMPARE_RID_FUNCTION_NUMBER,
1138 OPAL_MAP_PE);
1139 WARN_ON(rc != OPAL_SUCCESS);
1140 found_pe = true;
1141 break;
1142 }
1143 }
1144
1145 if (!found_pe)
1146 /*
1147 * Could not find an existing PE so allocate a new
1148 * one.
1149 */
1150 return pnv_ioda_setup_dev_PE(npu_pdev);
1151 else
1152 return pe;
1153}
1154
1155static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus)
1156{
Alistair Popple5d2aa712015-12-17 13:43:13 +11001157 struct pci_dev *pdev;
1158
1159 list_for_each_entry(pdev, &bus->devices, bus_list)
Alistair Poppleb5215492016-01-11 16:53:49 +11001160 pnv_ioda_setup_npu_PE(pdev);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001161}
1162
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001163static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001164{
1165 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001166
Gavin Shand1203852015-06-19 12:26:18 +10001167 pnv_ioda_setup_bus_PE(bus, false);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001168
1169 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001170 if (dev->subordinate) {
1171 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
Gavin Shand1203852015-06-19 12:26:18 +10001172 pnv_ioda_setup_bus_PE(dev->subordinate, true);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001173 else
1174 pnv_ioda_setup_PEs(dev->subordinate);
1175 }
1176 }
1177}
1178
1179/*
1180 * Configure PEs so that the downstream PCI buses and devices
1181 * could have their associated PE#. Unfortunately, we didn't
1182 * figure out the way to identify the PLX bridge yet. So we
1183 * simply put the PCI bus and the subordinate behind the root
1184 * port to PE# here. The game rule here is expected to be changed
1185 * as soon as we can detected PLX bridge correctly.
1186 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001187static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001188{
1189 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001190 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001191
1192 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001193 phb = hose->private_data;
1194
1195 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +11001196 if (phb->reserve_m64_pe)
Gavin Shan96a2f922015-06-19 12:26:17 +10001197 phb->reserve_m64_pe(hose->bus, NULL, true);
Guo Chao262af552014-07-21 14:42:30 +10001198
Alistair Popple5d2aa712015-12-17 13:43:13 +11001199 /*
1200 * On NPU PHB, we expect separate PEs for individual PCI
1201 * functions. PCI bus dependent PEs are required for the
1202 * remaining types of PHBs.
1203 */
Alistair Popple08f48f32016-01-11 16:53:50 +11001204 if (phb->type == PNV_PHB_NPU) {
1205 /* PE#0 is needed for error reporting */
1206 pnv_ioda_reserve_pe(phb, 0);
Alistair Poppleb5215492016-01-11 16:53:49 +11001207 pnv_ioda_setup_npu_PEs(hose->bus);
Alistair Popple08f48f32016-01-11 16:53:50 +11001208 } else
Alistair Popple5d2aa712015-12-17 13:43:13 +11001209 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001210 }
1211}
1212
Gavin Shana8b2f822015-03-25 16:23:52 +08001213#ifdef CONFIG_PCI_IOV
Wei Yangee8222f2015-10-22 09:22:16 +08001214static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001215{
1216 struct pci_bus *bus;
1217 struct pci_controller *hose;
1218 struct pnv_phb *phb;
1219 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001220 int i, j;
Wei Yangee8222f2015-10-22 09:22:16 +08001221 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001222
1223 bus = pdev->bus;
1224 hose = pci_bus_to_host(bus);
1225 phb = hose->private_data;
1226 pdn = pci_get_pdn(pdev);
1227
Wei Yangee8222f2015-10-22 09:22:16 +08001228 if (pdn->m64_single_mode)
1229 m64_bars = num_vfs;
1230 else
1231 m64_bars = 1;
1232
Wei Yang02639b02015-03-25 16:23:59 +08001233 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yangee8222f2015-10-22 09:22:16 +08001234 for (j = 0; j < m64_bars; j++) {
1235 if (pdn->m64_map[j][i] == IODA_INVALID_M64)
Wei Yang02639b02015-03-25 16:23:59 +08001236 continue;
1237 opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001238 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0);
1239 clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc);
1240 pdn->m64_map[j][i] = IODA_INVALID_M64;
Wei Yang02639b02015-03-25 16:23:59 +08001241 }
Wei Yang781a8682015-03-25 16:23:57 +08001242
Wei Yangee8222f2015-10-22 09:22:16 +08001243 kfree(pdn->m64_map);
Wei Yang781a8682015-03-25 16:23:57 +08001244 return 0;
1245}
1246
Wei Yang02639b02015-03-25 16:23:59 +08001247static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001248{
1249 struct pci_bus *bus;
1250 struct pci_controller *hose;
1251 struct pnv_phb *phb;
1252 struct pci_dn *pdn;
1253 unsigned int win;
1254 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001255 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001256 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001257 int total_vfs;
1258 resource_size_t size, start;
1259 int pe_num;
Wei Yangee8222f2015-10-22 09:22:16 +08001260 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001261
1262 bus = pdev->bus;
1263 hose = pci_bus_to_host(bus);
1264 phb = hose->private_data;
1265 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001266 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001267
Wei Yangee8222f2015-10-22 09:22:16 +08001268 if (pdn->m64_single_mode)
1269 m64_bars = num_vfs;
1270 else
1271 m64_bars = 1;
Wei Yang02639b02015-03-25 16:23:59 +08001272
Wei Yangee8222f2015-10-22 09:22:16 +08001273 pdn->m64_map = kmalloc(sizeof(*pdn->m64_map) * m64_bars, GFP_KERNEL);
1274 if (!pdn->m64_map)
1275 return -ENOMEM;
1276 /* Initialize the m64_map to IODA_INVALID_M64 */
1277 for (i = 0; i < m64_bars ; i++)
1278 for (j = 0; j < PCI_SRIOV_NUM_BARS; j++)
1279 pdn->m64_map[i][j] = IODA_INVALID_M64;
1280
Wei Yang781a8682015-03-25 16:23:57 +08001281
1282 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1283 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1284 if (!res->flags || !res->parent)
1285 continue;
1286
Wei Yangee8222f2015-10-22 09:22:16 +08001287 for (j = 0; j < m64_bars; j++) {
Wei Yang02639b02015-03-25 16:23:59 +08001288 do {
1289 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1290 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001291
Wei Yang02639b02015-03-25 16:23:59 +08001292 if (win >= phb->ioda.m64_bar_idx + 1)
1293 goto m64_failed;
1294 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001295
Wei Yangee8222f2015-10-22 09:22:16 +08001296 pdn->m64_map[j][i] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001297
Wei Yangee8222f2015-10-22 09:22:16 +08001298 if (pdn->m64_single_mode) {
Wei Yang02639b02015-03-25 16:23:59 +08001299 size = pci_iov_resource_size(pdev,
1300 PCI_IOV_RESOURCES + i);
Wei Yang02639b02015-03-25 16:23:59 +08001301 start = res->start + size * j;
1302 } else {
1303 size = resource_size(res);
1304 start = res->start;
1305 }
1306
1307 /* Map the M64 here */
Wei Yangee8222f2015-10-22 09:22:16 +08001308 if (pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001309 pe_num = pdn->pe_num_map[j];
Wei Yang02639b02015-03-25 16:23:59 +08001310 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1311 pe_num, OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001312 pdn->m64_map[j][i], 0);
Wei Yang02639b02015-03-25 16:23:59 +08001313 }
1314
1315 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001316 OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001317 pdn->m64_map[j][i],
Wei Yang02639b02015-03-25 16:23:59 +08001318 start,
Wei Yang781a8682015-03-25 16:23:57 +08001319 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001320 size);
Wei Yang781a8682015-03-25 16:23:57 +08001321
Wei Yang02639b02015-03-25 16:23:59 +08001322
1323 if (rc != OPAL_SUCCESS) {
1324 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1325 win, rc);
1326 goto m64_failed;
1327 }
1328
Wei Yangee8222f2015-10-22 09:22:16 +08001329 if (pdn->m64_single_mode)
Wei Yang02639b02015-03-25 16:23:59 +08001330 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001331 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2);
Wei Yang02639b02015-03-25 16:23:59 +08001332 else
1333 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001334 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1);
Wei Yang02639b02015-03-25 16:23:59 +08001335
1336 if (rc != OPAL_SUCCESS) {
1337 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1338 win, rc);
1339 goto m64_failed;
1340 }
Wei Yang781a8682015-03-25 16:23:57 +08001341 }
1342 }
1343 return 0;
1344
1345m64_failed:
Wei Yangee8222f2015-10-22 09:22:16 +08001346 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001347 return -EBUSY;
1348}
1349
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001350static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1351 int num);
1352static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1353
Wei Yang781a8682015-03-25 16:23:57 +08001354static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1355{
Wei Yang781a8682015-03-25 16:23:57 +08001356 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001357 int64_t rc;
1358
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001359 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001360 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001361 if (rc)
1362 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1363
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001364 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001365 if (pe->table_group.group) {
1366 iommu_group_put(pe->table_group.group);
1367 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001368 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001369 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001370 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001371}
1372
Wei Yangee8222f2015-10-22 09:22:16 +08001373static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
Wei Yang781a8682015-03-25 16:23:57 +08001374{
1375 struct pci_bus *bus;
1376 struct pci_controller *hose;
1377 struct pnv_phb *phb;
1378 struct pnv_ioda_pe *pe, *pe_n;
1379 struct pci_dn *pdn;
1380
1381 bus = pdev->bus;
1382 hose = pci_bus_to_host(bus);
1383 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001384 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001385
1386 if (!pdev->is_physfn)
1387 return;
1388
Wei Yang781a8682015-03-25 16:23:57 +08001389 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1390 if (pe->parent_dev != pdev)
1391 continue;
1392
1393 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1394
1395 /* Remove from list */
1396 mutex_lock(&phb->ioda.pe_list_mutex);
1397 list_del(&pe->list);
1398 mutex_unlock(&phb->ioda.pe_list_mutex);
1399
1400 pnv_ioda_deconfigure_pe(phb, pe);
1401
Gavin Shan1e916772016-05-03 15:41:36 +10001402 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001403 }
1404}
1405
1406void pnv_pci_sriov_disable(struct pci_dev *pdev)
1407{
1408 struct pci_bus *bus;
1409 struct pci_controller *hose;
1410 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001411 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001412 struct pci_dn *pdn;
1413 struct pci_sriov *iov;
Wei Yangbe283ee2015-10-22 09:22:19 +08001414 u16 num_vfs, i;
Wei Yang781a8682015-03-25 16:23:57 +08001415
1416 bus = pdev->bus;
1417 hose = pci_bus_to_host(bus);
1418 phb = hose->private_data;
1419 pdn = pci_get_pdn(pdev);
1420 iov = pdev->sriov;
1421 num_vfs = pdn->num_vfs;
1422
1423 /* Release VF PEs */
Wei Yangee8222f2015-10-22 09:22:16 +08001424 pnv_ioda_release_vf_PE(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001425
1426 if (phb->type == PNV_PHB_IODA2) {
Wei Yangee8222f2015-10-22 09:22:16 +08001427 if (!pdn->m64_single_mode)
Wei Yangbe283ee2015-10-22 09:22:19 +08001428 pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001429
1430 /* Release M64 windows */
Wei Yangee8222f2015-10-22 09:22:16 +08001431 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001432
1433 /* Release PE numbers */
Wei Yangbe283ee2015-10-22 09:22:19 +08001434 if (pdn->m64_single_mode) {
1435 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001436 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1437 continue;
1438
1439 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1440 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001441 }
1442 } else
1443 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1444 /* Releasing pe_num_map */
1445 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001446 }
1447}
1448
1449static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1450 struct pnv_ioda_pe *pe);
1451static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1452{
1453 struct pci_bus *bus;
1454 struct pci_controller *hose;
1455 struct pnv_phb *phb;
1456 struct pnv_ioda_pe *pe;
1457 int pe_num;
1458 u16 vf_index;
1459 struct pci_dn *pdn;
1460
1461 bus = pdev->bus;
1462 hose = pci_bus_to_host(bus);
1463 phb = hose->private_data;
1464 pdn = pci_get_pdn(pdev);
1465
1466 if (!pdev->is_physfn)
1467 return;
1468
1469 /* Reserve PE for each VF */
1470 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001471 if (pdn->m64_single_mode)
1472 pe_num = pdn->pe_num_map[vf_index];
1473 else
1474 pe_num = *pdn->pe_num_map + vf_index;
Wei Yang781a8682015-03-25 16:23:57 +08001475
1476 pe = &phb->ioda.pe_array[pe_num];
1477 pe->pe_number = pe_num;
1478 pe->phb = phb;
1479 pe->flags = PNV_IODA_PE_VF;
1480 pe->pbus = NULL;
1481 pe->parent_dev = pdev;
Wei Yang781a8682015-03-25 16:23:57 +08001482 pe->mve_number = -1;
1483 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1484 pci_iov_virtfn_devfn(pdev, vf_index);
1485
1486 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1487 hose->global_number, pdev->bus->number,
1488 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1489 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1490
1491 if (pnv_ioda_configure_pe(phb, pe)) {
1492 /* XXX What do we do here ? */
Gavin Shan1e916772016-05-03 15:41:36 +10001493 pnv_ioda_free_pe(pe);
Wei Yang781a8682015-03-25 16:23:57 +08001494 pe->pdev = NULL;
1495 continue;
1496 }
1497
Wei Yang781a8682015-03-25 16:23:57 +08001498 /* Put PE to the list */
1499 mutex_lock(&phb->ioda.pe_list_mutex);
1500 list_add_tail(&pe->list, &phb->ioda.pe_list);
1501 mutex_unlock(&phb->ioda.pe_list_mutex);
1502
1503 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1504 }
1505}
1506
1507int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1508{
1509 struct pci_bus *bus;
1510 struct pci_controller *hose;
1511 struct pnv_phb *phb;
Gavin Shan1e916772016-05-03 15:41:36 +10001512 struct pnv_ioda_pe *pe;
Wei Yang781a8682015-03-25 16:23:57 +08001513 struct pci_dn *pdn;
1514 int ret;
Wei Yangbe283ee2015-10-22 09:22:19 +08001515 u16 i;
Wei Yang781a8682015-03-25 16:23:57 +08001516
1517 bus = pdev->bus;
1518 hose = pci_bus_to_host(bus);
1519 phb = hose->private_data;
1520 pdn = pci_get_pdn(pdev);
1521
1522 if (phb->type == PNV_PHB_IODA2) {
Wei Yangb0331852015-10-22 09:22:14 +08001523 if (!pdn->vfs_expanded) {
1524 dev_info(&pdev->dev, "don't support this SRIOV device"
1525 " with non 64bit-prefetchable IOV BAR\n");
1526 return -ENOSPC;
1527 }
1528
Wei Yangee8222f2015-10-22 09:22:16 +08001529 /*
1530 * When M64 BARs functions in Single PE mode, the number of VFs
1531 * could be enabled must be less than the number of M64 BARs.
1532 */
1533 if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) {
1534 dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n");
1535 return -EBUSY;
1536 }
1537
Wei Yangbe283ee2015-10-22 09:22:19 +08001538 /* Allocating pe_num_map */
1539 if (pdn->m64_single_mode)
1540 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map) * num_vfs,
1541 GFP_KERNEL);
1542 else
1543 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL);
1544
1545 if (!pdn->pe_num_map)
1546 return -ENOMEM;
1547
1548 if (pdn->m64_single_mode)
1549 for (i = 0; i < num_vfs; i++)
1550 pdn->pe_num_map[i] = IODA_INVALID_PE;
1551
Wei Yang781a8682015-03-25 16:23:57 +08001552 /* Calculate available PE for required VFs */
Wei Yangbe283ee2015-10-22 09:22:19 +08001553 if (pdn->m64_single_mode) {
1554 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001555 pe = pnv_ioda_alloc_pe(phb);
1556 if (!pe) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001557 ret = -EBUSY;
1558 goto m64_failed;
1559 }
Gavin Shan1e916772016-05-03 15:41:36 +10001560
1561 pdn->pe_num_map[i] = pe->pe_number;
Wei Yangbe283ee2015-10-22 09:22:19 +08001562 }
1563 } else {
1564 mutex_lock(&phb->ioda.pe_alloc_mutex);
1565 *pdn->pe_num_map = bitmap_find_next_zero_area(
Gavin Shan92b8f132016-05-03 15:41:24 +10001566 phb->ioda.pe_alloc, phb->ioda.total_pe_num,
Wei Yangbe283ee2015-10-22 09:22:19 +08001567 0, num_vfs, 0);
Gavin Shan92b8f132016-05-03 15:41:24 +10001568 if (*pdn->pe_num_map >= phb->ioda.total_pe_num) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001569 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1570 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1571 kfree(pdn->pe_num_map);
1572 return -EBUSY;
1573 }
1574 bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001575 mutex_unlock(&phb->ioda.pe_alloc_mutex);
Wei Yang781a8682015-03-25 16:23:57 +08001576 }
Wei Yang781a8682015-03-25 16:23:57 +08001577 pdn->num_vfs = num_vfs;
Wei Yang781a8682015-03-25 16:23:57 +08001578
1579 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001580 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001581 if (ret) {
1582 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1583 goto m64_failed;
1584 }
1585
1586 /*
1587 * When using one M64 BAR to map one IOV BAR, we need to shift
1588 * the IOV BAR according to the PE# allocated to the VFs.
1589 * Otherwise, the PE# for the VF will conflict with others.
1590 */
Wei Yangee8222f2015-10-22 09:22:16 +08001591 if (!pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001592 ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map);
Wei Yang02639b02015-03-25 16:23:59 +08001593 if (ret)
1594 goto m64_failed;
1595 }
Wei Yang781a8682015-03-25 16:23:57 +08001596 }
1597
1598 /* Setup VF PEs */
1599 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1600
1601 return 0;
1602
1603m64_failed:
Wei Yangbe283ee2015-10-22 09:22:19 +08001604 if (pdn->m64_single_mode) {
1605 for (i = 0; i < num_vfs; i++) {
Gavin Shan1e916772016-05-03 15:41:36 +10001606 if (pdn->pe_num_map[i] == IODA_INVALID_PE)
1607 continue;
1608
1609 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
1610 pnv_ioda_free_pe(pe);
Wei Yangbe283ee2015-10-22 09:22:19 +08001611 }
1612 } else
1613 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1614
1615 /* Releasing pe_num_map */
1616 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001617
1618 return ret;
1619}
1620
Gavin Shana8b2f822015-03-25 16:23:52 +08001621int pcibios_sriov_disable(struct pci_dev *pdev)
1622{
Wei Yang781a8682015-03-25 16:23:57 +08001623 pnv_pci_sriov_disable(pdev);
1624
Gavin Shana8b2f822015-03-25 16:23:52 +08001625 /* Release PCI data */
1626 remove_dev_pci_data(pdev);
1627 return 0;
1628}
1629
1630int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1631{
1632 /* Allocate PCI data */
1633 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001634
Wei Yangee8222f2015-10-22 09:22:16 +08001635 return pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001636}
1637#endif /* CONFIG_PCI_IOV */
1638
Gavin Shan959c9bd2013-04-25 19:21:02 +00001639static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001640{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001641 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001642 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001643
Gavin Shan959c9bd2013-04-25 19:21:02 +00001644 /*
1645 * The function can be called while the PE#
1646 * hasn't been assigned. Do nothing for the
1647 * case.
1648 */
1649 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1650 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001651
Gavin Shan959c9bd2013-04-25 19:21:02 +00001652 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001653 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiy0e1ffef2015-08-27 16:01:16 +10001654 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001655 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001656 /*
1657 * Note: iommu_add_device() will fail here as
1658 * for physical PE: the device is already added by now;
1659 * for virtual PE: sysfs entries are not ready yet and
1660 * tce_iommu_bus_notifier will add the device to a group later.
1661 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001662}
1663
Daniel Axtens763d2d82015-04-28 15:12:07 +10001664static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001665{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001666 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1667 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001668 struct pci_dn *pdn = pci_get_pdn(pdev);
1669 struct pnv_ioda_pe *pe;
1670 uint64_t top;
1671 bool bypass = false;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001672 struct pci_dev *linked_npu_dev;
1673 int i;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001674
1675 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1676 return -ENODEV;;
1677
1678 pe = &phb->ioda.pe_array[pdn->pe_number];
1679 if (pe->tce_bypass_enabled) {
1680 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1681 bypass = (dma_mask >= top);
1682 }
1683
1684 if (bypass) {
1685 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1686 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001687 } else {
1688 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1689 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001690 }
Brian W Harta32305b2014-07-31 14:24:37 -05001691 *pdev->dev.dma_mask = dma_mask;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001692
1693 /* Update peer npu devices */
1694 if (pe->flags & PNV_IODA_PE_PEER)
Alistair Popple419dbd52016-01-08 11:35:09 +11001695 for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
1696 if (!pe->peers[i])
1697 continue;
1698
Alistair Popple5d2aa712015-12-17 13:43:13 +11001699 linked_npu_dev = pe->peers[i]->pdev;
1700 if (dma_get_mask(&linked_npu_dev->dev) != dma_mask)
1701 dma_set_mask(&linked_npu_dev->dev, dma_mask);
1702 }
1703
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001704 return 0;
1705}
1706
Andrew Donnellan535229822015-08-07 13:45:54 +10001707static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001708{
Andrew Donnellan535229822015-08-07 13:45:54 +10001709 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1710 struct pnv_phb *phb = hose->private_data;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001711 struct pci_dn *pdn = pci_get_pdn(pdev);
1712 struct pnv_ioda_pe *pe;
1713 u64 end, mask;
1714
1715 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1716 return 0;
1717
1718 pe = &phb->ioda.pe_array[pdn->pe_number];
1719 if (!pe->tce_bypass_enabled)
1720 return __dma_get_required_mask(&pdev->dev);
1721
1722
1723 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1724 mask = 1ULL << (fls64(end) - 1);
1725 mask += mask - 1;
1726
1727 return mask;
1728}
1729
Gavin Shandff4a392014-07-15 17:00:55 +10001730static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001731 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001732{
1733 struct pci_dev *dev;
1734
1735 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001736 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001737 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001738 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001739
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001740 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001741 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001742 }
1743}
1744
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001745static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
1746 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001747{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001748 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1749 &tbl->it_group_list, struct iommu_table_group_link,
1750 next);
1751 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001752 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001753 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001754 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1755 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001756 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001757 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001758
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001759 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1760 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1761 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001762
1763 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1764 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001765 start <<= shift;
1766 end <<= shift;
1767 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001768 start |= tbl->it_busno;
1769 end |= tbl->it_busno;
1770 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1771 /* p7ioc-style invalidation, 2 TCEs per write */
1772 start |= (1ull << 63);
1773 end |= (1ull << 63);
1774 inc = 16;
1775 } else {
1776 /* Default (older HW) */
1777 inc = 128;
1778 }
1779
1780 end |= inc - 1; /* round up end to be different than start */
1781
1782 mb(); /* Ensure above stores are visible */
1783 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001784 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001785 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001786 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001787 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001788 start += inc;
1789 }
1790
1791 /*
1792 * The iommu layer will do another mb() for us on build()
1793 * and we don't care on free()
1794 */
1795}
1796
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001797static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1798 long npages, unsigned long uaddr,
1799 enum dma_data_direction direction,
1800 struct dma_attrs *attrs)
1801{
1802 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1803 attrs);
1804
1805 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1806 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1807
1808 return ret;
1809}
1810
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001811#ifdef CONFIG_IOMMU_API
1812static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1813 unsigned long *hpa, enum dma_data_direction *direction)
1814{
1815 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1816
1817 if (!ret && (tbl->it_type &
1818 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1819 pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);
1820
1821 return ret;
1822}
1823#endif
1824
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001825static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1826 long npages)
1827{
1828 pnv_tce_free(tbl, index, npages);
1829
1830 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1831 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1832}
1833
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001834static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001835 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001836#ifdef CONFIG_IOMMU_API
1837 .exchange = pnv_ioda1_tce_xchg,
1838#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001839 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001840 .get = pnv_tce_get,
1841};
1842
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001843#define TCE_KILL_INVAL_ALL PPC_BIT(0)
Alexey Kardashevskiybef92532016-04-29 18:55:17 +10001844#define TCE_KILL_INVAL_PE PPC_BIT(1)
1845#define TCE_KILL_INVAL_TCE PPC_BIT(2)
1846
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001847void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_phb *phb, bool rm)
1848{
1849 const unsigned long val = TCE_KILL_INVAL_ALL;
1850
1851 mb(); /* Ensure previous TCE table stores are visible */
1852 if (rm)
1853 __raw_rm_writeq(cpu_to_be64(val),
1854 (__be64 __iomem *)
1855 phb->ioda.tce_inval_reg_phys);
1856 else
1857 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
1858}
1859
Alexey Kardashevskiya7cf13c2016-04-29 18:55:16 +10001860static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001861{
1862 /* 01xb - invalidate TCEs that match the specified PE# */
Alexey Kardashevskiybef92532016-04-29 18:55:17 +10001863 unsigned long val = TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001864 struct pnv_phb *phb = pe->phb;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001865 struct pnv_ioda_pe *npe;
1866 int i;
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001867
1868 if (!phb->ioda.tce_inval_reg)
1869 return;
1870
1871 mb(); /* Ensure above stores are visible */
1872 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001873
1874 if (pe->flags & PNV_IODA_PE_PEER)
1875 for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
1876 npe = pe->peers[i];
1877 if (!npe || npe->phb->type != PNV_PHB_NPU)
1878 continue;
1879
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001880 pnv_pci_ioda2_tce_invalidate_entire(npe->phb, false);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001881 }
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001882}
1883
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001884static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
1885 __be64 __iomem *invalidate, unsigned shift,
1886 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001887{
1888 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001889
1890 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiybef92532016-04-29 18:55:17 +10001891 start = TCE_KILL_INVAL_TCE;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001892 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001893 end = start;
1894
1895 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001896 start |= (index << shift);
1897 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001898 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001899 mb();
1900
1901 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001902 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001903 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001904 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001905 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001906 start += inc;
1907 }
1908}
1909
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001910static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1911 unsigned long index, unsigned long npages, bool rm)
1912{
1913 struct iommu_table_group_link *tgl;
1914
1915 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
Alistair Popple5d2aa712015-12-17 13:43:13 +11001916 struct pnv_ioda_pe *npe;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001917 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1918 struct pnv_ioda_pe, table_group);
1919 __be64 __iomem *invalidate = rm ?
1920 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1921 pe->phb->ioda.tce_inval_reg;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001922 int i;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001923
1924 pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
1925 invalidate, tbl->it_page_shift,
1926 index, npages);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001927
1928 if (pe->flags & PNV_IODA_PE_PEER)
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001929 /*
1930 * The NVLink hardware does not support TCE kill
1931 * per TCE entry so we have to invalidate
1932 * the entire cache for it.
1933 */
Alistair Popple5d2aa712015-12-17 13:43:13 +11001934 for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
1935 npe = pe->peers[i];
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001936 if (!npe || npe->phb->type != PNV_PHB_NPU ||
1937 !npe->phb->ioda.tce_inval_reg)
Alistair Popple5d2aa712015-12-17 13:43:13 +11001938 continue;
1939
Alexey Kardashevskiy0bbcdb42016-04-29 18:55:18 +10001940 pnv_pci_ioda2_tce_invalidate_entire(npe->phb,
1941 rm);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001942 }
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001943 }
1944}
1945
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001946static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1947 long npages, unsigned long uaddr,
1948 enum dma_data_direction direction,
1949 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001950{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001951 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1952 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001953
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001954 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1955 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1956
1957 return ret;
1958}
1959
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001960#ifdef CONFIG_IOMMU_API
1961static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1962 unsigned long *hpa, enum dma_data_direction *direction)
1963{
1964 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1965
1966 if (!ret && (tbl->it_type &
1967 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1968 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1969
1970 return ret;
1971}
1972#endif
1973
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001974static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1975 long npages)
1976{
1977 pnv_tce_free(tbl, index, npages);
1978
1979 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1980 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001981}
1982
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001983static void pnv_ioda2_table_free(struct iommu_table *tbl)
1984{
1985 pnv_pci_ioda2_table_free_pages(tbl);
1986 iommu_free_table(tbl, "pnv");
1987}
1988
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001989static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001990 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001991#ifdef CONFIG_IOMMU_API
1992 .exchange = pnv_ioda2_tce_xchg,
1993#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001994 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001995 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001996 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001997};
1998
Gavin Shan801846d2016-05-03 15:41:34 +10001999static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data)
2000{
2001 unsigned int *weight = (unsigned int *)data;
2002
2003 /* This is quite simplistic. The "base" weight of a device
2004 * is 10. 0 means no DMA is to be accounted for it.
2005 */
2006 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
2007 return 0;
2008
2009 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
2010 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
2011 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
2012 *weight += 3;
2013 else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
2014 *weight += 15;
2015 else
2016 *weight += 10;
2017
2018 return 0;
2019}
2020
2021static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe)
2022{
2023 unsigned int weight = 0;
2024
2025 /* SRIOV VF has same DMA32 weight as its PF */
2026#ifdef CONFIG_PCI_IOV
2027 if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) {
2028 pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight);
2029 return weight;
2030 }
2031#endif
2032
2033 if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) {
2034 pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight);
2035 } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) {
2036 struct pci_dev *pdev;
2037
2038 list_for_each_entry(pdev, &pe->pbus->devices, bus_list)
2039 pnv_pci_ioda_dev_dma_weight(pdev, &weight);
2040 } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) {
2041 pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight);
2042 }
2043
2044 return weight;
2045}
2046
Gavin Shanb30d9362016-05-03 15:41:32 +10002047static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
Gavin Shan2b923ed2016-05-05 12:04:16 +10002048 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002049{
2050
2051 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002052 struct iommu_table *tbl;
Gavin Shan2b923ed2016-05-05 12:04:16 +10002053 unsigned int weight, total_weight = 0;
2054 unsigned int tce32_segsz, base, segs, avail, i;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002055 int64_t rc;
2056 void *addr;
2057
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002058 /* XXX FIXME: Handle 64-bit only DMA devices */
2059 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
2060 /* XXX FIXME: Allocate multi-level tables on PHB3 */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002061 weight = pnv_pci_ioda_pe_dma_weight(pe);
2062 if (!weight)
2063 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002064
Gavin Shan2b923ed2016-05-05 12:04:16 +10002065 pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight,
2066 &total_weight);
2067 segs = (weight * phb->ioda.dma32_count) / total_weight;
2068 if (!segs)
2069 segs = 1;
2070
2071 /*
2072 * Allocate contiguous DMA32 segments. We begin with the expected
2073 * number of segments. With one more attempt, the number of DMA32
2074 * segments to be allocated is decreased by one until one segment
2075 * is allocated successfully.
2076 */
2077 do {
2078 for (base = 0; base <= phb->ioda.dma32_count - segs; base++) {
2079 for (avail = 0, i = base; i < base + segs; i++) {
2080 if (phb->ioda.dma32_segmap[i] ==
2081 IODA_INVALID_PE)
2082 avail++;
2083 }
2084
2085 if (avail == segs)
2086 goto found;
2087 }
2088 } while (--segs);
2089
2090 if (!segs) {
2091 pe_warn(pe, "No available DMA32 segments\n");
2092 return;
2093 }
2094
2095found:
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002096 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002097 iommu_register_group(&pe->table_group, phb->hose->global_number,
2098 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002099 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002100
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002101 /* Grab a 32-bit TCE table */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002102 pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
2103 weight, total_weight, base, segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002104 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
Gavin Shanacce9712016-05-03 15:41:33 +10002105 base * PNV_IODA1_DMA32_SEGSIZE,
2106 (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002107
2108 /* XXX Currently, we allocate one big contiguous table for the
2109 * TCEs. We only really need one chunk per 256M of TCE space
2110 * (ie per segment) but that's an optimization for later, it
2111 * requires some added smarts with our get/put_tce implementation
Gavin Shanacce9712016-05-03 15:41:33 +10002112 *
2113 * Each TCE page is 4KB in size and each TCE entry occupies 8
2114 * bytes
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002115 */
Gavin Shanacce9712016-05-03 15:41:33 +10002116 tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002117 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
Gavin Shanacce9712016-05-03 15:41:33 +10002118 get_order(tce32_segsz * segs));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002119 if (!tce_mem) {
2120 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
2121 goto fail;
2122 }
2123 addr = page_address(tce_mem);
Gavin Shanacce9712016-05-03 15:41:33 +10002124 memset(addr, 0, tce32_segsz * segs);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002125
2126 /* Configure HW */
2127 for (i = 0; i < segs; i++) {
2128 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2129 pe->pe_number,
2130 base + i, 1,
Gavin Shanacce9712016-05-03 15:41:33 +10002131 __pa(addr) + tce32_segsz * i,
2132 tce32_segsz, IOMMU_PAGE_SIZE_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002133 if (rc) {
2134 pe_err(pe, " Failed to configure 32-bit TCE table,"
2135 " err %ld\n", rc);
2136 goto fail;
2137 }
2138 }
2139
Gavin Shan2b923ed2016-05-05 12:04:16 +10002140 /* Setup DMA32 segment mapping */
2141 for (i = base; i < base + segs; i++)
2142 phb->ioda.dma32_segmap[i] = pe->pe_number;
2143
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002144 /* Setup linux iommu table */
Gavin Shanacce9712016-05-03 15:41:33 +10002145 pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs,
2146 base * PNV_IODA1_DMA32_SEGSIZE,
2147 IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002148
2149 /* OPAL variant of P7IOC SW invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002150 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10002151 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
2152 TCE_PCI_SWINV_FREE |
2153 TCE_PCI_SWINV_PAIR);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002154
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002155 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002156 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
2157 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002158 iommu_init_table(tbl, phb->hose->node);
2159
Wei Yang781a8682015-03-25 16:23:57 +08002160 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002161 /*
2162 * Setting table base here only for carrying iommu_group
2163 * further down to let iommu_add_device() do the job.
2164 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2165 */
2166 set_iommu_table_base(&pe->pdev->dev, tbl);
2167 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002168 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002169 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10002170
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002171 return;
2172 fail:
2173 /* XXX Failure: Try to fallback to 64-bit only ? */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002174 if (tce_mem)
Gavin Shanacce9712016-05-03 15:41:33 +10002175 __free_pages(tce_mem, get_order(tce32_segsz * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002176 if (tbl) {
2177 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
2178 iommu_free_table(tbl, "pnv");
2179 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002180}
2181
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002182static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
2183 int num, struct iommu_table *tbl)
2184{
2185 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2186 table_group);
2187 struct pnv_phb *phb = pe->phb;
2188 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002189 const unsigned long size = tbl->it_indirect_levels ?
2190 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002191 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
2192 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
2193
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002194 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002195 start_addr, start_addr + win_size - 1,
2196 IOMMU_PAGE_SIZE(tbl));
2197
2198 /*
2199 * Map TCE table through TVT. The TVE index is the PE number
2200 * shifted by 1 bit for 32-bits DMA space.
2201 */
2202 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2203 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002204 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002205 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002206 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002207 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002208 IOMMU_PAGE_SIZE(tbl));
2209 if (rc) {
2210 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
2211 return rc;
2212 }
2213
2214 pnv_pci_link_table_and_group(phb->hose->node, num,
2215 tbl, &pe->table_group);
Alexey Kardashevskiya7cf13c2016-04-29 18:55:16 +10002216 pnv_pci_ioda2_tce_invalidate_pe(pe);
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002217
2218 return 0;
2219}
2220
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002221static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002222{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002223 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2224 int64_t rc;
2225
2226 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2227 if (enable) {
2228 phys_addr_t top = memblock_end_of_DRAM();
2229
2230 top = roundup_pow_of_two(top);
2231 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2232 pe->pe_number,
2233 window_id,
2234 pe->tce_bypass_base,
2235 top);
2236 } else {
2237 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2238 pe->pe_number,
2239 window_id,
2240 pe->tce_bypass_base,
2241 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002242 }
2243 if (rc)
2244 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2245 else
2246 pe->tce_bypass_enabled = enable;
2247}
2248
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002249static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2250 __u32 page_shift, __u64 window_size, __u32 levels,
2251 struct iommu_table *tbl);
2252
2253static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2254 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2255 struct iommu_table **ptbl)
2256{
2257 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2258 table_group);
2259 int nid = pe->phb->hose->node;
2260 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2261 long ret;
2262 struct iommu_table *tbl;
2263
2264 tbl = pnv_pci_table_alloc(nid);
2265 if (!tbl)
2266 return -ENOMEM;
2267
2268 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2269 bus_offset, page_shift, window_size,
2270 levels, tbl);
2271 if (ret) {
2272 iommu_free_table(tbl, "pnv");
2273 return ret;
2274 }
2275
2276 tbl->it_ops = &pnv_ioda2_iommu_ops;
2277 if (pe->phb->ioda.tce_inval_reg)
2278 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2279
2280 *ptbl = tbl;
2281
2282 return 0;
2283}
2284
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002285static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2286{
2287 struct iommu_table *tbl = NULL;
2288 long rc;
2289
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002290 /*
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002291 * crashkernel= specifies the kdump kernel's maximum memory at
2292 * some offset and there is no guaranteed the result is a power
2293 * of 2, which will cause errors later.
2294 */
2295 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
2296
2297 /*
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002298 * In memory constrained environments, e.g. kdump kernel, the
2299 * DMA window can be larger than available memory, which will
2300 * cause errors later.
2301 */
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002302 const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002303
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002304 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2305 IOMMU_PAGE_SHIFT_4K,
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002306 window_size,
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002307 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2308 if (rc) {
2309 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2310 rc);
2311 return rc;
2312 }
2313
2314 iommu_init_table(tbl, pe->phb->hose->node);
2315
2316 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2317 if (rc) {
2318 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2319 rc);
2320 pnv_ioda2_table_free(tbl);
2321 return rc;
2322 }
2323
2324 if (!pnv_iommu_bypass_disabled)
2325 pnv_pci_ioda2_set_bypass(pe, true);
2326
2327 /* OPAL variant of PHB3 invalidated TCEs */
2328 if (pe->phb->ioda.tce_inval_reg)
2329 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2330
2331 /*
2332 * Setting table base here only for carrying iommu_group
2333 * further down to let iommu_add_device() do the job.
2334 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2335 */
2336 if (pe->flags & PNV_IODA_PE_DEV)
2337 set_iommu_table_base(&pe->pdev->dev, tbl);
2338
2339 return 0;
2340}
2341
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002342#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2343static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2344 int num)
2345{
2346 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2347 table_group);
2348 struct pnv_phb *phb = pe->phb;
2349 long ret;
2350
2351 pe_info(pe, "Removing DMA window #%d\n", num);
2352
2353 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2354 (pe->pe_number << 1) + num,
2355 0/* levels */, 0/* table address */,
2356 0/* table size */, 0/* page size */);
2357 if (ret)
2358 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2359 else
Alexey Kardashevskiya7cf13c2016-04-29 18:55:16 +10002360 pnv_pci_ioda2_tce_invalidate_pe(pe);
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002361
2362 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2363
2364 return ret;
2365}
2366#endif
2367
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002368#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002369static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2370 __u64 window_size, __u32 levels)
2371{
2372 unsigned long bytes = 0;
2373 const unsigned window_shift = ilog2(window_size);
2374 unsigned entries_shift = window_shift - page_shift;
2375 unsigned table_shift = entries_shift + 3;
2376 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2377 unsigned long direct_table_size;
2378
2379 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2380 (window_size > memory_hotplug_max()) ||
2381 !is_power_of_2(window_size))
2382 return 0;
2383
2384 /* Calculate a direct table size from window_size and levels */
2385 entries_shift = (entries_shift + levels - 1) / levels;
2386 table_shift = entries_shift + 3;
2387 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2388 direct_table_size = 1UL << table_shift;
2389
2390 for ( ; levels; --levels) {
2391 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2392
2393 tce_table_size /= direct_table_size;
2394 tce_table_size <<= 3;
2395 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2396 }
2397
2398 return bytes;
2399}
2400
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002401static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002402{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002403 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2404 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002405 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2406 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002407
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002408 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002409 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2410 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002411}
2412
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002413static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2414{
2415 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2416 table_group);
2417
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002418 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002419}
2420
2421static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002422 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002423 .create_table = pnv_pci_ioda2_create_table,
2424 .set_window = pnv_pci_ioda2_set_window,
2425 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002426 .take_ownership = pnv_ioda2_take_ownership,
2427 .release_ownership = pnv_ioda2_release_ownership,
2428};
2429#endif
2430
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002431static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2432{
2433 const __be64 *swinvp;
2434
2435 /* OPAL variant of PHB3 invalidated TCEs */
2436 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2437 if (!swinvp)
2438 return;
2439
2440 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2441 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2442}
2443
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002444static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2445 unsigned levels, unsigned long limit,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002446 unsigned long *current_offset, unsigned long *total_allocated)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002447{
2448 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002449 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002450 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002451 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2452 unsigned entries = 1UL << (shift - 3);
2453 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002454
2455 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2456 if (!tce_mem) {
2457 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2458 return NULL;
2459 }
2460 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002461 memset(addr, 0, allocated);
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002462 *total_allocated += allocated;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002463
2464 --levels;
2465 if (!levels) {
2466 *current_offset += allocated;
2467 return addr;
2468 }
2469
2470 for (i = 0; i < entries; ++i) {
2471 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002472 levels, limit, current_offset, total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002473 if (!tmp)
2474 break;
2475
2476 addr[i] = cpu_to_be64(__pa(tmp) |
2477 TCE_PCI_READ | TCE_PCI_WRITE);
2478
2479 if (*current_offset >= limit)
2480 break;
2481 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002482
2483 return addr;
2484}
2485
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002486static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2487 unsigned long size, unsigned level);
2488
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002489static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002490 __u32 page_shift, __u64 window_size, __u32 levels,
2491 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002492{
2493 void *addr;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002494 unsigned long offset = 0, level_shift, total_allocated = 0;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002495 const unsigned window_shift = ilog2(window_size);
2496 unsigned entries_shift = window_shift - page_shift;
2497 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2498 const unsigned long tce_table_size = 1UL << table_shift;
2499
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002500 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2501 return -EINVAL;
2502
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002503 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2504 return -EINVAL;
2505
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002506 /* Adjust direct table size from window_size and levels */
2507 entries_shift = (entries_shift + levels - 1) / levels;
2508 level_shift = entries_shift + 3;
2509 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2510
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002511 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002512 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002513 levels, tce_table_size, &offset, &total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002514
2515 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002516 if (!addr)
2517 return -ENOMEM;
2518
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002519 /*
2520 * First level was allocated but some lower level failed as
2521 * we did not allocate as much as we wanted,
2522 * release partially allocated table.
2523 */
2524 if (offset < tce_table_size) {
2525 pnv_pci_ioda2_table_do_free_pages(addr,
2526 1ULL << (level_shift - 3), levels - 1);
2527 return -ENOMEM;
2528 }
2529
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002530 /* Setup linux iommu table */
2531 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2532 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002533 tbl->it_level_size = 1ULL << (level_shift - 3);
2534 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002535 tbl->it_allocated_size = total_allocated;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002536
2537 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2538 window_size, tce_table_size, bus_offset);
2539
2540 return 0;
2541}
2542
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002543static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2544 unsigned long size, unsigned level)
2545{
2546 const unsigned long addr_ul = (unsigned long) addr &
2547 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2548
2549 if (level) {
2550 long i;
2551 u64 *tmp = (u64 *) addr_ul;
2552
2553 for (i = 0; i < size; ++i) {
2554 unsigned long hpa = be64_to_cpu(tmp[i]);
2555
2556 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2557 continue;
2558
2559 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2560 level - 1);
2561 }
2562 }
2563
2564 free_pages(addr_ul, get_order(size << 3));
2565}
2566
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002567static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2568{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002569 const unsigned long size = tbl->it_indirect_levels ?
2570 tbl->it_level_size : tbl->it_size;
2571
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002572 if (!tbl->it_size)
2573 return;
2574
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002575 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2576 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002577}
2578
Gavin Shan373f5652013-04-25 19:21:01 +00002579static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2580 struct pnv_ioda_pe *pe)
2581{
Gavin Shan373f5652013-04-25 19:21:01 +00002582 int64_t rc;
2583
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002584 /* TVE #1 is selected by PCI address bit 59 */
2585 pe->tce_bypass_base = 1ull << 59;
2586
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002587 iommu_register_group(&pe->table_group, phb->hose->global_number,
2588 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002589
Gavin Shan373f5652013-04-25 19:21:01 +00002590 /* The PE will reserve all possible 32-bits space */
Gavin Shan373f5652013-04-25 19:21:01 +00002591 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002592 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002593
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002594 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002595 pe->table_group.tce32_start = 0;
2596 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2597 pe->table_group.max_dynamic_windows_supported =
2598 IOMMU_TABLE_GROUP_MAX_TABLES;
2599 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2600 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002601#ifdef CONFIG_IOMMU_API
2602 pe->table_group.ops = &pnv_pci_ioda2_ops;
2603#endif
2604
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002605 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan801846d2016-05-03 15:41:34 +10002606 if (rc)
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002607 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002608
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002609 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002610 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002611 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002612 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002613}
2614
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002615static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002616{
2617 struct pci_controller *hose = phb->hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002618 struct pnv_ioda_pe *pe;
Gavin Shan2b923ed2016-05-05 12:04:16 +10002619 unsigned int weight;
Gavin Shan801846d2016-05-03 15:41:34 +10002620
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002621 /* If we have more PE# than segments available, hand out one
2622 * per PE until we run out and let the rest fail. If not,
2623 * then we assign at least one segment per PE, plus more based
2624 * on the amount of devices under that PE
2625 */
Gavin Shan2b923ed2016-05-05 12:04:16 +10002626 pr_info("PCI: Domain %04x has %d available 32-bit DMA segments\n",
2627 hose->global_number, phb->ioda.dma32_count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002628
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002629 pnv_pci_ioda_setup_opal_tce_kill(phb);
2630
Gavin Shan2b923ed2016-05-05 12:04:16 +10002631 /* Walk our PE list and configure their DMA segments */
Gavin Shan801846d2016-05-03 15:41:34 +10002632 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2633 weight = pnv_pci_ioda_pe_dma_weight(pe);
2634 if (!weight)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002635 continue;
Gavin Shan801846d2016-05-03 15:41:34 +10002636
Gavin Shan373f5652013-04-25 19:21:01 +00002637 /*
2638 * For IODA2 compliant PHB3, we needn't care about the weight.
2639 * The all available 32-bits DMA space will be assigned to
2640 * the specific PE.
2641 */
2642 if (phb->type == PNV_PHB_IODA1) {
Gavin Shan2b923ed2016-05-05 12:04:16 +10002643 pnv_pci_ioda1_setup_dma_pe(phb, pe);
Alistair Popple5d2aa712015-12-17 13:43:13 +11002644 } else if (phb->type == PNV_PHB_IODA2) {
Gavin Shan373f5652013-04-25 19:21:01 +00002645 pe_info(pe, "Assign DMA32 space\n");
Gavin Shan373f5652013-04-25 19:21:01 +00002646 pnv_pci_ioda2_setup_dma_pe(phb, pe);
Alistair Popple5d2aa712015-12-17 13:43:13 +11002647 } else if (phb->type == PNV_PHB_NPU) {
2648 /*
2649 * We initialise the DMA space for an NPU PHB
2650 * after setup of the PHB is complete as we
2651 * point the NPU TVT to the the same location
2652 * as the PHB3 TVT.
2653 */
Gavin Shan373f5652013-04-25 19:21:01 +00002654 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002655 }
2656}
2657
2658#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002659static void pnv_ioda2_msi_eoi(struct irq_data *d)
2660{
2661 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2662 struct irq_chip *chip = irq_data_get_irq_chip(d);
2663 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2664 ioda.irq_chip);
2665 int64_t rc;
2666
2667 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2668 WARN_ON_ONCE(rc);
2669
2670 icp_native_eoi(d);
2671}
2672
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002673
2674static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2675{
2676 struct irq_data *idata;
2677 struct irq_chip *ichip;
2678
2679 if (phb->type != PNV_PHB_IODA2)
2680 return;
2681
2682 if (!phb->ioda.irq_chip_init) {
2683 /*
2684 * First time we setup an MSI IRQ, we need to setup the
2685 * corresponding IRQ chip to route correctly.
2686 */
2687 idata = irq_get_irq_data(virq);
2688 ichip = irq_data_get_irq_chip(idata);
2689 phb->ioda.irq_chip_init = 1;
2690 phb->ioda.irq_chip = *ichip;
2691 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2692 }
2693 irq_set_chip(virq, &phb->ioda.irq_chip);
2694}
2695
Ian Munsie80c49c72014-10-08 19:54:57 +11002696#ifdef CONFIG_CXL_BASE
2697
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002698struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002699{
2700 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2701
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002702 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002703}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002704EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002705
Ryan Grimm1212aa12015-01-19 11:52:50 -06002706int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002707{
2708 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2709 struct pnv_phb *phb = hose->private_data;
2710 struct pnv_ioda_pe *pe;
2711 int rc;
2712
2713 pe = pnv_ioda_get_pe(dev);
2714 if (!pe)
2715 return -ENODEV;
2716
2717 pe_info(pe, "Switching PHB to CXL\n");
2718
Ryan Grimm1212aa12015-01-19 11:52:50 -06002719 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11002720 if (rc)
2721 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2722
2723 return rc;
2724}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002725EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002726
2727/* Find PHB for cxl dev and allocate MSI hwirqs?
2728 * Returns the absolute hardware IRQ number
2729 */
2730int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2731{
2732 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2733 struct pnv_phb *phb = hose->private_data;
2734 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2735
2736 if (hwirq < 0) {
2737 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2738 return -ENOSPC;
2739 }
2740
2741 return phb->msi_base + hwirq;
2742}
2743EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2744
2745void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2746{
2747 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2748 struct pnv_phb *phb = hose->private_data;
2749
2750 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2751}
2752EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2753
2754void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2755 struct pci_dev *dev)
2756{
2757 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2758 struct pnv_phb *phb = hose->private_data;
2759 int i, hwirq;
2760
2761 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2762 if (!irqs->range[i])
2763 continue;
2764 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2765 i, irqs->offset[i],
2766 irqs->range[i]);
2767 hwirq = irqs->offset[i] - phb->msi_base;
2768 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2769 irqs->range[i]);
2770 }
2771}
2772EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2773
2774int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2775 struct pci_dev *dev, int num)
2776{
2777 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2778 struct pnv_phb *phb = hose->private_data;
2779 int i, hwirq, try;
2780
2781 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2782
2783 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2784 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2785 try = num;
2786 while (try) {
2787 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2788 if (hwirq >= 0)
2789 break;
2790 try /= 2;
2791 }
2792 if (!try)
2793 goto fail;
2794
2795 irqs->offset[i] = phb->msi_base + hwirq;
2796 irqs->range[i] = try;
2797 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2798 i, irqs->offset[i], irqs->range[i]);
2799 num -= try;
2800 }
2801 if (num)
2802 goto fail;
2803
2804 return 0;
2805fail:
2806 pnv_cxl_release_hwirq_ranges(irqs, dev);
2807 return -ENOSPC;
2808}
2809EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2810
2811int pnv_cxl_get_irq_count(struct pci_dev *dev)
2812{
2813 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2814 struct pnv_phb *phb = hose->private_data;
2815
2816 return phb->msi_bmp.irq_count;
2817}
2818EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2819
2820int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2821 unsigned int virq)
2822{
2823 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2824 struct pnv_phb *phb = hose->private_data;
2825 unsigned int xive_num = hwirq - phb->msi_base;
2826 struct pnv_ioda_pe *pe;
2827 int rc;
2828
2829 if (!(pe = pnv_ioda_get_pe(dev)))
2830 return -ENODEV;
2831
2832 /* Assign XIVE to PE */
2833 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2834 if (rc) {
2835 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2836 "hwirq 0x%x XIVE 0x%x PE\n",
2837 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2838 return -EIO;
2839 }
2840 set_msi_irq_chip(phb, virq);
2841
2842 return 0;
2843}
2844EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2845#endif
2846
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002847static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002848 unsigned int hwirq, unsigned int virq,
2849 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002850{
2851 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2852 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002853 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002854 int rc;
2855
2856 /* No PE assigned ? bail out ... no MSI for you ! */
2857 if (pe == NULL)
2858 return -ENXIO;
2859
2860 /* Check if we have an MVE */
2861 if (pe->mve_number < 0)
2862 return -ENXIO;
2863
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002864 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002865 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002866 is_64 = 0;
2867
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002868 /* Assign XIVE to PE */
2869 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2870 if (rc) {
2871 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2872 pci_name(dev), rc, xive_num);
2873 return -EIO;
2874 }
2875
2876 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002877 __be64 addr64;
2878
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002879 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2880 &addr64, &data);
2881 if (rc) {
2882 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2883 pci_name(dev), rc);
2884 return -EIO;
2885 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002886 msg->address_hi = be64_to_cpu(addr64) >> 32;
2887 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002888 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002889 __be32 addr32;
2890
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002891 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2892 &addr32, &data);
2893 if (rc) {
2894 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2895 pci_name(dev), rc);
2896 return -EIO;
2897 }
2898 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002899 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002900 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002901 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002902
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002903 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002904
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002905 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2906 " address=%x_%08x data=%x PE# %d\n",
2907 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2908 msg->address_hi, msg->address_lo, data, pe->pe_number);
2909
2910 return 0;
2911}
2912
2913static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2914{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002915 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002916 const __be32 *prop = of_get_property(phb->hose->dn,
2917 "ibm,opal-msi-ranges", NULL);
2918 if (!prop) {
2919 /* BML Fallback */
2920 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2921 }
2922 if (!prop)
2923 return;
2924
2925 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002926 count = be32_to_cpup(prop + 1);
2927 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002928 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2929 phb->hose->global_number);
2930 return;
2931 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002932
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002933 phb->msi_setup = pnv_pci_ioda_msi_setup;
2934 phb->msi32_support = 1;
2935 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002936 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002937}
2938#else
2939static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2940#endif /* CONFIG_PCI_MSI */
2941
Wei Yang6e628c72015-03-25 16:23:55 +08002942#ifdef CONFIG_PCI_IOV
2943static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2944{
Wei Yangf2dd0af2015-10-22 09:22:17 +08002945 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
2946 struct pnv_phb *phb = hose->private_data;
2947 const resource_size_t gate = phb->ioda.m64_segsize >> 2;
Wei Yang6e628c72015-03-25 16:23:55 +08002948 struct resource *res;
2949 int i;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002950 resource_size_t size, total_vf_bar_sz;
Wei Yang6e628c72015-03-25 16:23:55 +08002951 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002952 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002953
2954 if (!pdev->is_physfn || pdev->is_added)
2955 return;
2956
Wei Yang6e628c72015-03-25 16:23:55 +08002957 pdn = pci_get_pdn(pdev);
2958 pdn->vfs_expanded = 0;
Wei Yangee8222f2015-10-22 09:22:16 +08002959 pdn->m64_single_mode = false;
Wei Yang6e628c72015-03-25 16:23:55 +08002960
Wei Yang5b88ec22015-03-25 16:23:58 +08002961 total_vfs = pci_sriov_get_totalvfs(pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10002962 mul = phb->ioda.total_pe_num;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002963 total_vf_bar_sz = 0;
Wei Yang5b88ec22015-03-25 16:23:58 +08002964
2965 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2966 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2967 if (!res->flags || res->parent)
2968 continue;
2969 if (!pnv_pci_is_mem_pref_64(res->flags)) {
Wei Yangb0331852015-10-22 09:22:14 +08002970 dev_warn(&pdev->dev, "Don't support SR-IOV with"
2971 " non M64 VF BAR%d: %pR. \n",
Wei Yang5b88ec22015-03-25 16:23:58 +08002972 i, res);
Wei Yangb0331852015-10-22 09:22:14 +08002973 goto truncate_iov;
Wei Yang5b88ec22015-03-25 16:23:58 +08002974 }
2975
Wei Yangdfcc8d42015-10-22 09:22:18 +08002976 total_vf_bar_sz += pci_iov_resource_size(pdev,
2977 i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002978
Wei Yangf2dd0af2015-10-22 09:22:17 +08002979 /*
2980 * If bigger than quarter of M64 segment size, just round up
2981 * power of two.
2982 *
2983 * Generally, one M64 BAR maps one IOV BAR. To avoid conflict
2984 * with other devices, IOV BAR size is expanded to be
2985 * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64
2986 * segment size , the expanded size would equal to half of the
2987 * whole M64 space size, which will exhaust the M64 Space and
2988 * limit the system flexibility. This is a design decision to
2989 * set the boundary to quarter of the M64 segment size.
2990 */
Wei Yangdfcc8d42015-10-22 09:22:18 +08002991 if (total_vf_bar_sz > gate) {
Wei Yang5b88ec22015-03-25 16:23:58 +08002992 mul = roundup_pow_of_two(total_vfs);
Wei Yangdfcc8d42015-10-22 09:22:18 +08002993 dev_info(&pdev->dev,
2994 "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n",
2995 total_vf_bar_sz, gate, mul);
Wei Yangee8222f2015-10-22 09:22:16 +08002996 pdn->m64_single_mode = true;
Wei Yang5b88ec22015-03-25 16:23:58 +08002997 break;
2998 }
2999 }
3000
Wei Yang6e628c72015-03-25 16:23:55 +08003001 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
3002 res = &pdev->resource[i + PCI_IOV_RESOURCES];
3003 if (!res->flags || res->parent)
3004 continue;
Wei Yang6e628c72015-03-25 16:23:55 +08003005
Wei Yang6e628c72015-03-25 16:23:55 +08003006 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yangee8222f2015-10-22 09:22:16 +08003007 /*
3008 * On PHB3, the minimum size alignment of M64 BAR in single
3009 * mode is 32MB.
3010 */
3011 if (pdn->m64_single_mode && (size < SZ_32M))
3012 goto truncate_iov;
3013 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
Wei Yang5b88ec22015-03-25 16:23:58 +08003014 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08003015 dev_dbg(&pdev->dev, " %pR\n", res);
3016 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08003017 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08003018 }
Wei Yang5b88ec22015-03-25 16:23:58 +08003019 pdn->vfs_expanded = mul;
Wei Yangb0331852015-10-22 09:22:14 +08003020
3021 return;
3022
3023truncate_iov:
3024 /* To save MMIO space, IOV BAR is truncated. */
3025 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
3026 res = &pdev->resource[i + PCI_IOV_RESOURCES];
3027 res->flags = 0;
3028 res->end = res->start - 1;
3029 }
Wei Yang6e628c72015-03-25 16:23:55 +08003030}
3031#endif /* CONFIG_PCI_IOV */
3032
Gavin Shan23e79422016-05-03 15:41:27 +10003033static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
3034 struct resource *res)
3035{
3036 struct pnv_phb *phb = pe->phb;
3037 struct pci_bus_region region;
3038 int index;
3039 int64_t rc;
3040
3041 if (!res || !res->flags || res->start > res->end)
3042 return;
3043
3044 if (res->flags & IORESOURCE_IO) {
3045 region.start = res->start - phb->ioda.io_pci_base;
3046 region.end = res->end - phb->ioda.io_pci_base;
3047 index = region.start / phb->ioda.io_segsize;
3048
3049 while (index < phb->ioda.total_pe_num &&
3050 region.start <= region.end) {
3051 phb->ioda.io_segmap[index] = pe->pe_number;
3052 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3053 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
3054 if (rc != OPAL_SUCCESS) {
3055 pr_err("%s: Error %lld mapping IO segment#%d to PE#%d\n",
3056 __func__, rc, index, pe->pe_number);
3057 break;
3058 }
3059
3060 region.start += phb->ioda.io_segsize;
3061 index++;
3062 }
3063 } else if ((res->flags & IORESOURCE_MEM) &&
3064 !pnv_pci_is_mem_pref_64(res->flags)) {
3065 region.start = res->start -
3066 phb->hose->mem_offset[0] -
3067 phb->ioda.m32_pci_base;
3068 region.end = res->end -
3069 phb->hose->mem_offset[0] -
3070 phb->ioda.m32_pci_base;
3071 index = region.start / phb->ioda.m32_segsize;
3072
3073 while (index < phb->ioda.total_pe_num &&
3074 region.start <= region.end) {
3075 phb->ioda.m32_segmap[index] = pe->pe_number;
3076 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3077 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
3078 if (rc != OPAL_SUCCESS) {
3079 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%d",
3080 __func__, rc, index, pe->pe_number);
3081 break;
3082 }
3083
3084 region.start += phb->ioda.m32_segsize;
3085 index++;
3086 }
3087 }
3088}
3089
Gavin Shan11685be2012-08-20 03:49:16 +00003090/*
3091 * This function is supposed to be called on basis of PE from top
3092 * to bottom style. So the the I/O or MMIO segment assigned to
3093 * parent PE could be overrided by its child PEs if necessary.
3094 */
Gavin Shan23e79422016-05-03 15:41:27 +10003095static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00003096{
Gavin Shan69d733e2016-05-03 15:41:28 +10003097 struct pci_dev *pdev;
Gavin Shan23e79422016-05-03 15:41:27 +10003098 int i;
Gavin Shan11685be2012-08-20 03:49:16 +00003099
3100 /*
3101 * NOTE: We only care PCI bus based PE for now. For PCI
3102 * device based PE, for example SRIOV sensitive VF should
3103 * be figured out later.
3104 */
3105 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
3106
Gavin Shan69d733e2016-05-03 15:41:28 +10003107 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
3108 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
3109 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
3110
3111 /*
3112 * If the PE contains all subordinate PCI buses, the
3113 * windows of the child bridges should be mapped to
3114 * the PE as well.
3115 */
3116 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
3117 continue;
3118 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
3119 pnv_ioda_setup_pe_res(pe,
3120 &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
3121 }
Gavin Shan11685be2012-08-20 03:49:16 +00003122}
3123
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003124static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00003125{
3126 struct pci_controller *tmp, *hose;
3127 struct pnv_phb *phb;
3128 struct pnv_ioda_pe *pe;
3129
3130 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3131 phb = hose->private_data;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003132
3133 /* NPU PHB does not support IO or MMIO segmentation */
3134 if (phb->type == PNV_PHB_NPU)
3135 continue;
3136
Gavin Shan11685be2012-08-20 03:49:16 +00003137 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
Gavin Shan23e79422016-05-03 15:41:27 +10003138 pnv_ioda_setup_pe_seg(pe);
Gavin Shan11685be2012-08-20 03:49:16 +00003139 }
3140 }
3141}
3142
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003143static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00003144{
3145 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00003146 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00003147
3148 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3149 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00003150
3151 /* Mark the PHB initialization done */
3152 phb = hose->private_data;
3153 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00003154 }
3155}
3156
Gavin Shan37c367f2013-06-20 18:13:25 +08003157static void pnv_pci_ioda_create_dbgfs(void)
3158{
3159#ifdef CONFIG_DEBUG_FS
3160 struct pci_controller *hose, *tmp;
3161 struct pnv_phb *phb;
3162 char name[16];
3163
3164 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3165 phb = hose->private_data;
3166
3167 sprintf(name, "PCI%04x", hose->global_number);
3168 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
3169 if (!phb->dbgfs)
3170 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
3171 __func__, hose->global_number);
3172 }
3173#endif /* CONFIG_DEBUG_FS */
3174}
3175
Alistair Popple5d2aa712015-12-17 13:43:13 +11003176static void pnv_npu_ioda_fixup(void)
3177{
3178 bool enable_bypass;
3179 struct pci_controller *hose, *tmp;
3180 struct pnv_phb *phb;
3181 struct pnv_ioda_pe *pe;
Gavin Shan801846d2016-05-03 15:41:34 +10003182 unsigned int weight;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003183
3184 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3185 phb = hose->private_data;
3186 if (phb->type != PNV_PHB_NPU)
3187 continue;
3188
Gavin Shan801846d2016-05-03 15:41:34 +10003189 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
3190 weight = pnv_pci_ioda_pe_dma_weight(pe);
3191 if (WARN_ON(!weight))
3192 continue;
3193
Alistair Popple5d2aa712015-12-17 13:43:13 +11003194 enable_bypass = dma_get_mask(&pe->pdev->dev) ==
3195 DMA_BIT_MASK(64);
3196 pnv_npu_init_dma_pe(pe);
3197 pnv_npu_dma_set_bypass(pe, enable_bypass);
3198 }
3199 }
3200}
3201
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003202static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00003203{
3204 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00003205 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00003206 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003207
Gavin Shan37c367f2013-06-20 18:13:25 +08003208 pnv_pci_ioda_create_dbgfs();
3209
Gavin Shane9cc17d2013-06-20 13:21:14 +08003210#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08003211 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04003212 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003213#endif
Alistair Popple5d2aa712015-12-17 13:43:13 +11003214
3215 /* Link NPU IODA tables to their PCI devices. */
3216 pnv_npu_ioda_fixup();
Gavin Shanfb446ad2012-08-20 03:49:14 +00003217}
3218
Gavin Shan271fd032012-09-11 16:59:47 -06003219/*
3220 * Returns the alignment for I/O or memory windows for P2P
3221 * bridges. That actually depends on how PEs are segmented.
3222 * For now, we return I/O or M32 segment size for PE sensitive
3223 * P2P bridges. Otherwise, the default values (4KiB for I/O,
3224 * 1MiB for memory) will be returned.
3225 *
3226 * The current PCI bus might be put into one PE, which was
3227 * create against the parent PCI bridge. For that case, we
3228 * needn't enlarge the alignment so that we can save some
3229 * resources.
3230 */
3231static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
3232 unsigned long type)
3233{
3234 struct pci_dev *bridge;
3235 struct pci_controller *hose = pci_bus_to_host(bus);
3236 struct pnv_phb *phb = hose->private_data;
3237 int num_pci_bridges = 0;
3238
3239 bridge = bus->self;
3240 while (bridge) {
3241 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
3242 num_pci_bridges++;
3243 if (num_pci_bridges >= 2)
3244 return 1;
3245 }
3246
3247 bridge = bridge->bus->self;
3248 }
3249
Guo Chao262af552014-07-21 14:42:30 +10003250 /* We fail back to M32 if M64 isn't supported */
3251 if (phb->ioda.m64_segsize &&
3252 pnv_pci_is_mem_pref_64(type))
3253 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06003254 if (type & IORESOURCE_MEM)
3255 return phb->ioda.m32_segsize;
3256
3257 return phb->ioda.io_segsize;
3258}
3259
Wei Yang5350ab32015-03-25 16:23:56 +08003260#ifdef CONFIG_PCI_IOV
3261static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
3262 int resno)
3263{
Wei Yangee8222f2015-10-22 09:22:16 +08003264 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3265 struct pnv_phb *phb = hose->private_data;
Wei Yang5350ab32015-03-25 16:23:56 +08003266 struct pci_dn *pdn = pci_get_pdn(pdev);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003267 resource_size_t align;
Wei Yang5350ab32015-03-25 16:23:56 +08003268
Wei Yang7fbe7a92015-10-22 09:22:15 +08003269 /*
3270 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
3271 * SR-IOV. While from hardware perspective, the range mapped by M64
3272 * BAR should be size aligned.
3273 *
Wei Yangee8222f2015-10-22 09:22:16 +08003274 * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra
3275 * powernv-specific hardware restriction is gone. But if just use the
3276 * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with
3277 * in one segment of M64 #15, which introduces the PE conflict between
3278 * PF and VF. Based on this, the minimum alignment of an IOV BAR is
3279 * m64_segsize.
3280 *
Wei Yang7fbe7a92015-10-22 09:22:15 +08003281 * This function returns the total IOV BAR size if M64 BAR is in
3282 * Shared PE mode or just VF BAR size if not.
Wei Yangee8222f2015-10-22 09:22:16 +08003283 * If the M64 BAR is in Single PE mode, return the VF BAR size or
3284 * M64 segment size if IOV BAR size is less.
Wei Yang7fbe7a92015-10-22 09:22:15 +08003285 */
Wei Yang5350ab32015-03-25 16:23:56 +08003286 align = pci_iov_resource_size(pdev, resno);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003287 if (!pdn->vfs_expanded)
3288 return align;
Wei Yangee8222f2015-10-22 09:22:16 +08003289 if (pdn->m64_single_mode)
3290 return max(align, (resource_size_t)phb->ioda.m64_segsize);
Wei Yang5350ab32015-03-25 16:23:56 +08003291
Wei Yang7fbe7a92015-10-22 09:22:15 +08003292 return pdn->vfs_expanded * align;
Wei Yang5350ab32015-03-25 16:23:56 +08003293}
3294#endif /* CONFIG_PCI_IOV */
3295
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003296/* Prevent enabling devices for which we couldn't properly
3297 * assign a PE
3298 */
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003299static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003300{
Gavin Shandb1266c2012-08-20 03:49:18 +00003301 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3302 struct pnv_phb *phb = hose->private_data;
3303 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003304
Gavin Shandb1266c2012-08-20 03:49:18 +00003305 /* The function is probably called while the PEs have
3306 * not be created yet. For example, resource reassignment
3307 * during PCI probe period. We just skip the check if
3308 * PEs isn't ready.
3309 */
3310 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003311 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003312
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003313 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003314 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003315 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003316
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003317 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003318}
3319
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003320static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003321{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003322 struct pnv_phb *phb = hose->private_data;
3323
Gavin Shand1a85ee2014-09-30 12:39:05 +10003324 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003325 OPAL_ASSERT_RESET);
3326}
3327
Daniel Axtens92ae0352015-04-28 15:12:05 +10003328static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003329 .dma_dev_setup = pnv_pci_dma_dev_setup,
3330 .dma_bus_setup = pnv_pci_dma_bus_setup,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003331#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003332 .setup_msi_irqs = pnv_setup_msi_irqs,
3333 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003334#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003335 .enable_device_hook = pnv_pci_enable_device_hook,
3336 .window_alignment = pnv_pci_window_alignment,
3337 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3338 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3339 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3340 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003341};
3342
Alistair Popple5d2aa712015-12-17 13:43:13 +11003343static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003344 .dma_dev_setup = pnv_pci_dma_dev_setup,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003345#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003346 .setup_msi_irqs = pnv_setup_msi_irqs,
3347 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003348#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003349 .enable_device_hook = pnv_pci_enable_device_hook,
3350 .window_alignment = pnv_pci_window_alignment,
3351 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3352 .dma_set_mask = pnv_npu_dma_set_mask,
3353 .shutdown = pnv_pci_ioda_shutdown,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003354};
3355
Anton Blancharde51df2c2014-08-20 08:55:18 +10003356static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3357 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003358{
3359 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003360 struct pnv_phb *phb;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003361 unsigned long size, m64map_off, m32map_off, pemap_off;
3362 unsigned long iomap_off = 0, dma32map_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10003363 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003364 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003365 int len;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003366 unsigned int segno;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003367 u64 phb_id;
3368 void *aux;
3369 long rc;
3370
Gavin Shan58d714e2013-07-31 16:47:00 +08003371 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003372
3373 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3374 if (!prop64) {
3375 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3376 return;
3377 }
3378 phb_id = be64_to_cpup(prop64);
3379 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3380
Michael Ellermane39f223f2014-11-18 16:47:35 +11003381 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003382
3383 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003384 phb->hose = hose = pcibios_alloc_controller(np);
3385 if (!phb->hose) {
3386 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003387 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003388 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003389 return;
3390 }
3391
3392 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003393 prop32 = of_get_property(np, "bus-range", &len);
3394 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003395 hose->first_busno = be32_to_cpu(prop32[0]);
3396 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003397 } else {
3398 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3399 hose->first_busno = 0;
3400 hose->last_busno = 0xff;
3401 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003402 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003403 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003404 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003405 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003406 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003407
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003408 /* Detect specific models for error handling */
3409 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3410 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003411 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003412 phb->model = PNV_PHB_MODEL_PHB3;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003413 else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
3414 phb->model = PNV_PHB_MODEL_NPU;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003415 else
3416 phb->model = PNV_PHB_MODEL_UNKNOWN;
3417
Gavin Shanaa0c0332013-04-25 19:20:57 +00003418 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003419 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003420
Gavin Shanaa0c0332013-04-25 19:20:57 +00003421 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003422 phb->regs = of_iomap(np, 0);
3423 if (phb->regs == NULL)
3424 pr_err(" Failed to map registers !\n");
3425
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003426 /* Initialize more IODA stuff */
Gavin Shan92b8f132016-05-03 15:41:24 +10003427 phb->ioda.total_pe_num = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003428 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003429 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003430 phb->ioda.total_pe_num = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003431 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3432 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003433 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003434
3435 /* Parse 64-bit MMIO range */
3436 pnv_ioda_parse_m64_window(phb);
3437
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003438 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003439 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003440 phb->ioda.m32_size += 0x10000;
3441
Gavin Shan92b8f132016-05-03 15:41:24 +10003442 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003443 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003444 phb->ioda.io_size = hose->pci_io_size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003445 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003446 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3447
Gavin Shan2b923ed2016-05-05 12:04:16 +10003448 /* Calculate how many 32-bit TCE segments we have */
3449 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
3450 PNV_IODA1_DMA32_SEGSIZE;
3451
Gavin Shanc35d2a82013-07-31 16:47:04 +08003452 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Gavin Shan92b8f132016-05-03 15:41:24 +10003453 size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
Gavin Shan93289d82016-05-03 15:41:29 +10003454 m64map_off = size;
3455 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003456 m32map_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003457 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003458 if (phb->type == PNV_PHB_IODA1) {
3459 iomap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003460 size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
Gavin Shan2b923ed2016-05-05 12:04:16 +10003461 dma32map_off = size;
3462 size += phb->ioda.dma32_count *
3463 sizeof(phb->ioda.dma32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003464 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003465 pemap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003466 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003467 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003468 phb->ioda.pe_alloc = aux;
Gavin Shan93289d82016-05-03 15:41:29 +10003469 phb->ioda.m64_segmap = aux + m64map_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003470 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shan93289d82016-05-03 15:41:29 +10003471 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
3472 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003473 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan93289d82016-05-03 15:41:29 +10003474 }
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003475 if (phb->type == PNV_PHB_IODA1) {
Gavin Shanc35d2a82013-07-31 16:47:04 +08003476 phb->ioda.io_segmap = aux + iomap_off;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003477 for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
3478 phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
Gavin Shan2b923ed2016-05-05 12:04:16 +10003479
3480 phb->ioda.dma32_segmap = aux + dma32map_off;
3481 for (segno = 0; segno < phb->ioda.dma32_count; segno++)
3482 phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003483 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003484 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan92b8f132016-05-03 15:41:24 +10003485 set_bit(phb->ioda.reserved_pe_idx, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003486
3487 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003488 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003489
3490 /* Calculate how many 32-bit TCE segments we have */
Gavin Shan2b923ed2016-05-05 12:04:16 +10003491 phb->ioda.dma32_count = phb->ioda.m32_pci_base /
Gavin Shanacce9712016-05-03 15:41:33 +10003492 PNV_IODA1_DMA32_SEGSIZE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003493
Gavin Shanaa0c0332013-04-25 19:20:57 +00003494#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003495 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3496 window_type,
3497 window_num,
3498 starting_real_address,
3499 starting_pci_address,
3500 segment_size);
3501#endif
3502
Guo Chao262af552014-07-21 14:42:30 +10003503 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
Gavin Shan92b8f132016-05-03 15:41:24 +10003504 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
Guo Chao262af552014-07-21 14:42:30 +10003505 phb->ioda.m32_size, phb->ioda.m32_segsize);
3506 if (phb->ioda.m64_size)
3507 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3508 phb->ioda.m64_size, phb->ioda.m64_segsize);
3509 if (phb->ioda.io_size)
3510 pr_info(" IO: 0x%x [segment=0x%x]\n",
3511 phb->ioda.io_size, phb->ioda.io_segsize);
3512
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003513
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003514 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003515 phb->get_pe_state = pnv_ioda_get_pe_state;
3516 phb->freeze_pe = pnv_ioda_freeze_pe;
3517 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003518
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003519 /* Setup TCEs */
3520 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
3521
3522 /* Setup MSI support */
3523 pnv_pci_init_ioda_msis(phb);
3524
Gavin Shanc40a4212012-08-20 03:49:20 +00003525 /*
3526 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3527 * to let the PCI core do resource assignment. It's supposed
3528 * that the PCI core will do correct I/O and MMIO alignment
3529 * for the P2P bridge bars so that each PCI bus (excluding
3530 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003531 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003532 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003533
3534 if (phb->type == PNV_PHB_NPU)
3535 hose->controller_ops = pnv_npu_ioda_controller_ops;
3536 else
3537 hose->controller_ops = pnv_pci_ioda_controller_ops;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003538
Wei Yang6e628c72015-03-25 16:23:55 +08003539#ifdef CONFIG_PCI_IOV
3540 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003541 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003542#endif
3543
Gavin Shanc40a4212012-08-20 03:49:20 +00003544 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003545
3546 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003547 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003548 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003549 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003550
3551 /* If we're running in kdump kerenl, the previous kerenl never
3552 * shutdown PCI devices correctly. We already got IODA table
3553 * cleaned out. So we have to issue PHB reset to stop all PCI
3554 * transactions from previous kerenl.
3555 */
3556 if (is_kdump_kernel()) {
3557 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003558 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3559 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003560 }
Guo Chao262af552014-07-21 14:42:30 +10003561
Gavin Shan9e9e8932014-11-12 13:36:05 +11003562 /* Remove M64 resource if we can't configure it successfully */
3563 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003564 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003565}
3566
Bjorn Helgaas67975002013-07-02 12:20:03 -06003567void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003568{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003569 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003570}
3571
Alistair Popple5d2aa712015-12-17 13:43:13 +11003572void __init pnv_pci_init_npu_phb(struct device_node *np)
3573{
3574 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU);
3575}
3576
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003577void __init pnv_pci_init_ioda_hub(struct device_node *np)
3578{
3579 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003580 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003581 u64 hub_id;
3582
3583 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3584
3585 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3586 if (!prop64) {
3587 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3588 return;
3589 }
3590 hub_id = be64_to_cpup(prop64);
3591 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3592
3593 /* Count child PHBs */
3594 for_each_child_of_node(np, phbn) {
3595 /* Look for IODA1 PHBs */
3596 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003597 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003598 }
3599}