blob: 30a6d79f75022d2e2a9e4635bfbeca0cc6dbd85a [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 */
53
Wei Yang781a8682015-03-25 16:23:57 +080054/* 256M DMA window, 4K TCE pages, 8 bytes TCE */
55#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
56
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +100057#define POWERNV_IOMMU_DEFAULT_LEVELS 1
58#define POWERNV_IOMMU_MAX_LEVELS 5
59
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100060static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
61
Joe Perches6d31c2f2014-09-21 10:55:06 -070062static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
63 const char *fmt, ...)
64{
65 struct va_format vaf;
66 va_list args;
67 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000068
Joe Perches6d31c2f2014-09-21 10:55:06 -070069 va_start(args, fmt);
70
71 vaf.fmt = fmt;
72 vaf.va = &args;
73
Wei Yang781a8682015-03-25 16:23:57 +080074 if (pe->flags & PNV_IODA_PE_DEV)
Joe Perches6d31c2f2014-09-21 10:55:06 -070075 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
Wei Yang781a8682015-03-25 16:23:57 +080076 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Joe Perches6d31c2f2014-09-21 10:55:06 -070077 sprintf(pfix, "%04x:%02x ",
78 pci_domain_nr(pe->pbus), pe->pbus->number);
Wei Yang781a8682015-03-25 16:23:57 +080079#ifdef CONFIG_PCI_IOV
80 else if (pe->flags & PNV_IODA_PE_VF)
81 sprintf(pfix, "%04x:%02x:%2x.%d",
82 pci_domain_nr(pe->parent_dev->bus),
83 (pe->rid & 0xff00) >> 8,
84 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
85#endif /* CONFIG_PCI_IOV*/
Joe Perches6d31c2f2014-09-21 10:55:06 -070086
87 printk("%spci %s: [PE# %.3d] %pV",
88 level, pfix, pe->pe_number, &vaf);
89
90 va_end(args);
91}
92
93#define pe_err(pe, fmt, ...) \
94 pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
95#define pe_warn(pe, fmt, ...) \
96 pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
97#define pe_info(pe, fmt, ...) \
98 pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000099
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -0200100static bool pnv_iommu_bypass_disabled __read_mostly;
101
102static int __init iommu_setup(char *str)
103{
104 if (!str)
105 return -EINVAL;
106
107 while (*str) {
108 if (!strncmp(str, "nobypass", 8)) {
109 pnv_iommu_bypass_disabled = true;
110 pr_info("PowerNV: IOMMU bypass window disabled.\n");
111 break;
112 }
113 str += strcspn(str, ",");
114 if (*str == ',')
115 str++;
116 }
117
118 return 0;
119}
120early_param("iommu", iommu_setup);
121
Guo Chao262af552014-07-21 14:42:30 +1000122static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
123{
124 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
125 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
126}
127
Gavin Shan4b82ab12014-11-12 13:36:07 +1100128static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
129{
Gavin Shan92b8f132016-05-03 15:41:24 +1000130 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
Gavin Shan4b82ab12014-11-12 13:36:07 +1100131 pr_warn("%s: Invalid PE %d on PHB#%x\n",
132 __func__, pe_no, phb->hose->global_number);
133 return;
134 }
135
Gavin Shane9dc4d72015-06-19 12:26:16 +1000136 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
137 pr_debug("%s: PE %d was reserved on PHB#%x\n",
138 __func__, pe_no, phb->hose->global_number);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100139
140 phb->ioda.pe_array[pe_no].phb = phb;
141 phb->ioda.pe_array[pe_no].pe_number = pe_no;
142}
143
Gavin Shan689ee8c2016-05-03 15:41:25 +1000144static unsigned int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000145{
146 unsigned long pe;
147
148 do {
149 pe = find_next_zero_bit(phb->ioda.pe_alloc,
Gavin Shan92b8f132016-05-03 15:41:24 +1000150 phb->ioda.total_pe_num, 0);
151 if (pe >= phb->ioda.total_pe_num)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000152 return IODA_INVALID_PE;
153 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
154
Gavin Shan4cce9552013-04-25 19:21:00 +0000155 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000156 phb->ioda.pe_array[pe].pe_number = pe;
157 return pe;
158}
159
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800160static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000161{
162 WARN_ON(phb->ioda.pe_array[pe].pdev);
163
164 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
165 clear_bit(pe, phb->ioda.pe_alloc);
166}
167
Guo Chao262af552014-07-21 14:42:30 +1000168/* The default M64 BAR is shared by all PEs */
169static int pnv_ioda2_init_m64(struct pnv_phb *phb)
170{
171 const char *desc;
172 struct resource *r;
173 s64 rc;
174
175 /* Configure the default M64 BAR */
176 rc = opal_pci_set_phb_mem_window(phb->opal_id,
177 OPAL_M64_WINDOW_TYPE,
178 phb->ioda.m64_bar_idx,
179 phb->ioda.m64_base,
180 0, /* unused */
181 phb->ioda.m64_size);
182 if (rc != OPAL_SUCCESS) {
183 desc = "configuring";
184 goto fail;
185 }
186
187 /* Enable the default M64 BAR */
188 rc = opal_pci_phb_mmio_enable(phb->opal_id,
189 OPAL_M64_WINDOW_TYPE,
190 phb->ioda.m64_bar_idx,
191 OPAL_ENABLE_M64_SPLIT);
192 if (rc != OPAL_SUCCESS) {
193 desc = "enabling";
194 goto fail;
195 }
196
197 /* Mark the M64 BAR assigned */
198 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
199
200 /*
201 * Strip off the segment used by the reserved PE, which is
202 * expected to be 0 or last one of PE capabicity.
203 */
204 r = &phb->hose->mem_resources[1];
Gavin Shan92b8f132016-05-03 15:41:24 +1000205 if (phb->ioda.reserved_pe_idx == 0)
Guo Chao262af552014-07-21 14:42:30 +1000206 r->start += phb->ioda.m64_segsize;
Gavin Shan92b8f132016-05-03 15:41:24 +1000207 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
Guo Chao262af552014-07-21 14:42:30 +1000208 r->end -= phb->ioda.m64_segsize;
209 else
210 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
Gavin Shan92b8f132016-05-03 15:41:24 +1000211 phb->ioda.reserved_pe_idx);
Guo Chao262af552014-07-21 14:42:30 +1000212
213 return 0;
214
215fail:
216 pr_warn(" Failure %lld %s M64 BAR#%d\n",
217 rc, desc, phb->ioda.m64_bar_idx);
218 opal_pci_phb_mmio_enable(phb->opal_id,
219 OPAL_M64_WINDOW_TYPE,
220 phb->ioda.m64_bar_idx,
221 OPAL_DISABLE_M64);
222 return -EIO;
223}
224
Gavin Shanc4306702016-05-03 15:41:30 +1000225static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
Gavin Shan96a2f922015-06-19 12:26:17 +1000226 unsigned long *pe_bitmap)
Guo Chao262af552014-07-21 14:42:30 +1000227{
Gavin Shan96a2f922015-06-19 12:26:17 +1000228 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
229 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000230 struct resource *r;
Gavin Shan96a2f922015-06-19 12:26:17 +1000231 resource_size_t base, sgsz, start, end;
232 int segno, i;
Guo Chao262af552014-07-21 14:42:30 +1000233
Gavin Shan96a2f922015-06-19 12:26:17 +1000234 base = phb->ioda.m64_base;
235 sgsz = phb->ioda.m64_segsize;
236 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
237 r = &pdev->resource[i];
238 if (!r->parent || !pnv_pci_is_mem_pref_64(r->flags))
239 continue;
Guo Chao262af552014-07-21 14:42:30 +1000240
Gavin Shan96a2f922015-06-19 12:26:17 +1000241 start = _ALIGN_DOWN(r->start - base, sgsz);
242 end = _ALIGN_UP(r->end - base, sgsz);
243 for (segno = start / sgsz; segno < end / sgsz; segno++) {
244 if (pe_bitmap)
245 set_bit(segno, pe_bitmap);
246 else
247 pnv_ioda_reserve_pe(phb, segno);
Guo Chao262af552014-07-21 14:42:30 +1000248 }
249 }
250}
251
Gavin Shan99451552016-05-05 12:02:13 +1000252static int pnv_ioda1_init_m64(struct pnv_phb *phb)
253{
254 struct resource *r;
255 int index;
256
257 /*
258 * There are 16 M64 BARs, each of which has 8 segments. So
259 * there are as many M64 segments as the maximum number of
260 * PEs, which is 128.
261 */
262 for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
263 unsigned long base, segsz = phb->ioda.m64_segsize;
264 int64_t rc;
265
266 base = phb->ioda.m64_base +
267 index * PNV_IODA1_M64_SEGS * segsz;
268 rc = opal_pci_set_phb_mem_window(phb->opal_id,
269 OPAL_M64_WINDOW_TYPE, index, base, 0,
270 PNV_IODA1_M64_SEGS * segsz);
271 if (rc != OPAL_SUCCESS) {
272 pr_warn(" Error %lld setting M64 PHB#%d-BAR#%d\n",
273 rc, phb->hose->global_number, index);
274 goto fail;
275 }
276
277 rc = opal_pci_phb_mmio_enable(phb->opal_id,
278 OPAL_M64_WINDOW_TYPE, index,
279 OPAL_ENABLE_M64_SPLIT);
280 if (rc != OPAL_SUCCESS) {
281 pr_warn(" Error %lld enabling M64 PHB#%d-BAR#%d\n",
282 rc, phb->hose->global_number, index);
283 goto fail;
284 }
285 }
286
287 /*
288 * Exclude the segment used by the reserved PE, which
289 * is expected to be 0 or last supported PE#.
290 */
291 r = &phb->hose->mem_resources[1];
292 if (phb->ioda.reserved_pe_idx == 0)
293 r->start += phb->ioda.m64_segsize;
294 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
295 r->end -= phb->ioda.m64_segsize;
296 else
297 WARN(1, "Wrong reserved PE#%d on PHB#%d\n",
298 phb->ioda.reserved_pe_idx, phb->hose->global_number);
299
300 return 0;
301
302fail:
303 for ( ; index >= 0; index--)
304 opal_pci_phb_mmio_enable(phb->opal_id,
305 OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
306
307 return -EIO;
308}
309
Gavin Shanc4306702016-05-03 15:41:30 +1000310static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
311 unsigned long *pe_bitmap,
312 bool all)
Guo Chao262af552014-07-21 14:42:30 +1000313{
Guo Chao262af552014-07-21 14:42:30 +1000314 struct pci_dev *pdev;
Gavin Shan96a2f922015-06-19 12:26:17 +1000315
316 list_for_each_entry(pdev, &bus->devices, bus_list) {
Gavin Shanc4306702016-05-03 15:41:30 +1000317 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
Gavin Shan96a2f922015-06-19 12:26:17 +1000318
319 if (all && pdev->subordinate)
Gavin Shanc4306702016-05-03 15:41:30 +1000320 pnv_ioda_reserve_m64_pe(pdev->subordinate,
321 pe_bitmap, all);
Gavin Shan96a2f922015-06-19 12:26:17 +1000322 }
323}
324
Gavin Shanc4306702016-05-03 15:41:30 +1000325static unsigned int pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
Guo Chao262af552014-07-21 14:42:30 +1000326{
Gavin Shan26ba2482015-06-19 12:26:19 +1000327 struct pci_controller *hose = pci_bus_to_host(bus);
328 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000329 struct pnv_ioda_pe *master_pe, *pe;
330 unsigned long size, *pe_alloc;
Gavin Shan26ba2482015-06-19 12:26:19 +1000331 int i;
Guo Chao262af552014-07-21 14:42:30 +1000332
333 /* Root bus shouldn't use M64 */
334 if (pci_is_root_bus(bus))
335 return IODA_INVALID_PE;
336
Guo Chao262af552014-07-21 14:42:30 +1000337 /* Allocate bitmap */
Gavin Shan92b8f132016-05-03 15:41:24 +1000338 size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
Guo Chao262af552014-07-21 14:42:30 +1000339 pe_alloc = kzalloc(size, GFP_KERNEL);
340 if (!pe_alloc) {
341 pr_warn("%s: Out of memory !\n",
342 __func__);
343 return IODA_INVALID_PE;
344 }
345
Gavin Shan26ba2482015-06-19 12:26:19 +1000346 /* Figure out reserved PE numbers by the PE */
Gavin Shanc4306702016-05-03 15:41:30 +1000347 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
Guo Chao262af552014-07-21 14:42:30 +1000348
349 /*
350 * the current bus might not own M64 window and that's all
351 * contributed by its child buses. For the case, we needn't
352 * pick M64 dependent PE#.
353 */
Gavin Shan92b8f132016-05-03 15:41:24 +1000354 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
Guo Chao262af552014-07-21 14:42:30 +1000355 kfree(pe_alloc);
356 return IODA_INVALID_PE;
357 }
358
359 /*
360 * Figure out the master PE and put all slave PEs to master
361 * PE's list to form compound PE.
362 */
Guo Chao262af552014-07-21 14:42:30 +1000363 master_pe = NULL;
364 i = -1;
Gavin Shan92b8f132016-05-03 15:41:24 +1000365 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
366 phb->ioda.total_pe_num) {
Guo Chao262af552014-07-21 14:42:30 +1000367 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000368
Gavin Shan93289d82016-05-03 15:41:29 +1000369 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
Guo Chao262af552014-07-21 14:42:30 +1000370 if (!master_pe) {
371 pe->flags |= PNV_IODA_PE_MASTER;
372 INIT_LIST_HEAD(&pe->slaves);
373 master_pe = pe;
374 } else {
375 pe->flags |= PNV_IODA_PE_SLAVE;
376 pe->master = master_pe;
377 list_add_tail(&pe->list, &master_pe->slaves);
378 }
Gavin Shan99451552016-05-05 12:02:13 +1000379
380 /*
381 * P7IOC supports M64DT, which helps mapping M64 segment
382 * to one particular PE#. However, PHB3 has fixed mapping
383 * between M64 segment and PE#. In order to have same logic
384 * for P7IOC and PHB3, we enforce fixed mapping between M64
385 * segment and PE# on P7IOC.
386 */
387 if (phb->type == PNV_PHB_IODA1) {
388 int64_t rc;
389
390 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
391 pe->pe_number, OPAL_M64_WINDOW_TYPE,
392 pe->pe_number / PNV_IODA1_M64_SEGS,
393 pe->pe_number % PNV_IODA1_M64_SEGS);
394 if (rc != OPAL_SUCCESS)
395 pr_warn("%s: Error %lld mapping M64 for PHB#%d-PE#%d\n",
396 __func__, rc, phb->hose->global_number,
397 pe->pe_number);
398 }
Guo Chao262af552014-07-21 14:42:30 +1000399 }
400
401 kfree(pe_alloc);
402 return master_pe->pe_number;
403}
404
405static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
406{
407 struct pci_controller *hose = phb->hose;
408 struct device_node *dn = hose->dn;
409 struct resource *res;
410 const u32 *r;
411 u64 pci_addr;
412
Gavin Shan99451552016-05-05 12:02:13 +1000413 if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
Gavin Shan1665c4a2014-11-12 13:36:04 +1100414 pr_info(" Not support M64 window\n");
415 return;
416 }
417
Stewart Smithe4d54f72015-12-09 17:18:20 +1100418 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
Guo Chao262af552014-07-21 14:42:30 +1000419 pr_info(" Firmware too old to support M64 window\n");
420 return;
421 }
422
423 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
424 if (!r) {
425 pr_info(" No <ibm,opal-m64-window> on %s\n",
426 dn->full_name);
427 return;
428 }
429
Guo Chao262af552014-07-21 14:42:30 +1000430 res = &hose->mem_resources[1];
Gavin Shane80c4e72015-10-22 12:03:08 +1100431 res->name = dn->full_name;
Guo Chao262af552014-07-21 14:42:30 +1000432 res->start = of_translate_address(dn, r + 2);
433 res->end = res->start + of_read_number(r + 4, 2) - 1;
434 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
435 pci_addr = of_read_number(r, 2);
436 hose->mem_offset[1] = res->start - pci_addr;
437
438 phb->ioda.m64_size = resource_size(res);
Gavin Shan92b8f132016-05-03 15:41:24 +1000439 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
Guo Chao262af552014-07-21 14:42:30 +1000440 phb->ioda.m64_base = pci_addr;
441
Wei Yange9863e62014-12-12 12:39:37 +0800442 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
443 res->start, res->end, pci_addr);
444
Guo Chao262af552014-07-21 14:42:30 +1000445 /* Use last M64 BAR to cover M64 window */
446 phb->ioda.m64_bar_idx = 15;
Gavin Shan99451552016-05-05 12:02:13 +1000447 if (phb->type == PNV_PHB_IODA1)
448 phb->init_m64 = pnv_ioda1_init_m64;
449 else
450 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shanc4306702016-05-03 15:41:30 +1000451 phb->reserve_m64_pe = pnv_ioda_reserve_m64_pe;
452 phb->pick_m64_pe = pnv_ioda_pick_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000453}
454
Gavin Shan49dec922014-07-21 14:42:33 +1000455static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
456{
457 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
458 struct pnv_ioda_pe *slave;
459 s64 rc;
460
461 /* Fetch master PE */
462 if (pe->flags & PNV_IODA_PE_SLAVE) {
463 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100464 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
465 return;
466
Gavin Shan49dec922014-07-21 14:42:33 +1000467 pe_no = pe->pe_number;
468 }
469
470 /* Freeze master PE */
471 rc = opal_pci_eeh_freeze_set(phb->opal_id,
472 pe_no,
473 OPAL_EEH_ACTION_SET_FREEZE_ALL);
474 if (rc != OPAL_SUCCESS) {
475 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
476 __func__, rc, phb->hose->global_number, pe_no);
477 return;
478 }
479
480 /* Freeze slave PEs */
481 if (!(pe->flags & PNV_IODA_PE_MASTER))
482 return;
483
484 list_for_each_entry(slave, &pe->slaves, list) {
485 rc = opal_pci_eeh_freeze_set(phb->opal_id,
486 slave->pe_number,
487 OPAL_EEH_ACTION_SET_FREEZE_ALL);
488 if (rc != OPAL_SUCCESS)
489 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
490 __func__, rc, phb->hose->global_number,
491 slave->pe_number);
492 }
493}
494
Anton Blancharde51df2c2014-08-20 08:55:18 +1000495static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000496{
497 struct pnv_ioda_pe *pe, *slave;
498 s64 rc;
499
500 /* Find master PE */
501 pe = &phb->ioda.pe_array[pe_no];
502 if (pe->flags & PNV_IODA_PE_SLAVE) {
503 pe = pe->master;
504 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
505 pe_no = pe->pe_number;
506 }
507
508 /* Clear frozen state for master PE */
509 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
510 if (rc != OPAL_SUCCESS) {
511 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
512 __func__, rc, opt, phb->hose->global_number, pe_no);
513 return -EIO;
514 }
515
516 if (!(pe->flags & PNV_IODA_PE_MASTER))
517 return 0;
518
519 /* Clear frozen state for slave PEs */
520 list_for_each_entry(slave, &pe->slaves, list) {
521 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
522 slave->pe_number,
523 opt);
524 if (rc != OPAL_SUCCESS) {
525 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
526 __func__, rc, opt, phb->hose->global_number,
527 slave->pe_number);
528 return -EIO;
529 }
530 }
531
532 return 0;
533}
534
535static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
536{
537 struct pnv_ioda_pe *slave, *pe;
538 u8 fstate, state;
539 __be16 pcierr;
540 s64 rc;
541
542 /* Sanity check on PE number */
Gavin Shan92b8f132016-05-03 15:41:24 +1000543 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
Gavin Shan49dec922014-07-21 14:42:33 +1000544 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
545
546 /*
547 * Fetch the master PE and the PE instance might be
548 * not initialized yet.
549 */
550 pe = &phb->ioda.pe_array[pe_no];
551 if (pe->flags & PNV_IODA_PE_SLAVE) {
552 pe = pe->master;
553 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
554 pe_no = pe->pe_number;
555 }
556
557 /* Check the master PE */
558 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
559 &state, &pcierr, NULL);
560 if (rc != OPAL_SUCCESS) {
561 pr_warn("%s: Failure %lld getting "
562 "PHB#%x-PE#%x state\n",
563 __func__, rc,
564 phb->hose->global_number, pe_no);
565 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
566 }
567
568 /* Check the slave PE */
569 if (!(pe->flags & PNV_IODA_PE_MASTER))
570 return state;
571
572 list_for_each_entry(slave, &pe->slaves, list) {
573 rc = opal_pci_eeh_freeze_status(phb->opal_id,
574 slave->pe_number,
575 &fstate,
576 &pcierr,
577 NULL);
578 if (rc != OPAL_SUCCESS) {
579 pr_warn("%s: Failure %lld getting "
580 "PHB#%x-PE#%x state\n",
581 __func__, rc,
582 phb->hose->global_number, slave->pe_number);
583 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
584 }
585
586 /*
587 * Override the result based on the ascending
588 * priority.
589 */
590 if (fstate > state)
591 state = fstate;
592 }
593
594 return state;
595}
596
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000597/* Currently those 2 are only used when MSIs are enabled, this will change
598 * but in the meantime, we need to protect them to avoid warnings
599 */
600#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800601static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000602{
603 struct pci_controller *hose = pci_bus_to_host(dev->bus);
604 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000605 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000606
607 if (!pdn)
608 return NULL;
609 if (pdn->pe_number == IODA_INVALID_PE)
610 return NULL;
611 return &phb->ioda.pe_array[pdn->pe_number];
612}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000613#endif /* CONFIG_PCI_MSI */
614
Gavin Shanb131a842014-11-12 13:36:08 +1100615static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
616 struct pnv_ioda_pe *parent,
617 struct pnv_ioda_pe *child,
618 bool is_add)
619{
620 const char *desc = is_add ? "adding" : "removing";
621 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
622 OPAL_REMOVE_PE_FROM_DOMAIN;
623 struct pnv_ioda_pe *slave;
624 long rc;
625
626 /* Parent PE affects child PE */
627 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
628 child->pe_number, op);
629 if (rc != OPAL_SUCCESS) {
630 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
631 rc, desc);
632 return -ENXIO;
633 }
634
635 if (!(child->flags & PNV_IODA_PE_MASTER))
636 return 0;
637
638 /* Compound case: parent PE affects slave PEs */
639 list_for_each_entry(slave, &child->slaves, list) {
640 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
641 slave->pe_number, op);
642 if (rc != OPAL_SUCCESS) {
643 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
644 rc, desc);
645 return -ENXIO;
646 }
647 }
648
649 return 0;
650}
651
652static int pnv_ioda_set_peltv(struct pnv_phb *phb,
653 struct pnv_ioda_pe *pe,
654 bool is_add)
655{
656 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800657 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100658 int ret;
659
660 /*
661 * Clear PE frozen state. If it's master PE, we need
662 * clear slave PE frozen state as well.
663 */
664 if (is_add) {
665 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
666 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
667 if (pe->flags & PNV_IODA_PE_MASTER) {
668 list_for_each_entry(slave, &pe->slaves, list)
669 opal_pci_eeh_freeze_clear(phb->opal_id,
670 slave->pe_number,
671 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
672 }
673 }
674
675 /*
676 * Associate PE in PELT. We need add the PE into the
677 * corresponding PELT-V as well. Otherwise, the error
678 * originated from the PE might contribute to other
679 * PEs.
680 */
681 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
682 if (ret)
683 return ret;
684
685 /* For compound PEs, any one affects all of them */
686 if (pe->flags & PNV_IODA_PE_MASTER) {
687 list_for_each_entry(slave, &pe->slaves, list) {
688 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
689 if (ret)
690 return ret;
691 }
692 }
693
694 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
695 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800696 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100697 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800698#ifdef CONFIG_PCI_IOV
699 else if (pe->flags & PNV_IODA_PE_VF)
Gavin Shan283e2d82015-06-22 13:45:47 +1000700 pdev = pe->parent_dev;
Wei Yang781a8682015-03-25 16:23:57 +0800701#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100702 while (pdev) {
703 struct pci_dn *pdn = pci_get_pdn(pdev);
704 struct pnv_ioda_pe *parent;
705
706 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
707 parent = &phb->ioda.pe_array[pdn->pe_number];
708 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
709 if (ret)
710 return ret;
711 }
712
713 pdev = pdev->bus->self;
714 }
715
716 return 0;
717}
718
Wei Yang781a8682015-03-25 16:23:57 +0800719#ifdef CONFIG_PCI_IOV
720static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
721{
722 struct pci_dev *parent;
723 uint8_t bcomp, dcomp, fcomp;
724 int64_t rc;
725 long rid_end, rid;
726
727 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
728 if (pe->pbus) {
729 int count;
730
731 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
732 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
733 parent = pe->pbus->self;
734 if (pe->flags & PNV_IODA_PE_BUS_ALL)
735 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
736 else
737 count = 1;
738
739 switch(count) {
740 case 1: bcomp = OpalPciBusAll; break;
741 case 2: bcomp = OpalPciBus7Bits; break;
742 case 4: bcomp = OpalPciBus6Bits; break;
743 case 8: bcomp = OpalPciBus5Bits; break;
744 case 16: bcomp = OpalPciBus4Bits; break;
745 case 32: bcomp = OpalPciBus3Bits; break;
746 default:
747 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
748 count);
749 /* Do an exact match only */
750 bcomp = OpalPciBusAll;
751 }
752 rid_end = pe->rid + (count << 8);
753 } else {
754 if (pe->flags & PNV_IODA_PE_VF)
755 parent = pe->parent_dev;
756 else
757 parent = pe->pdev->bus->self;
758 bcomp = OpalPciBusAll;
759 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
760 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
761 rid_end = pe->rid + 1;
762 }
763
764 /* Clear the reverse map */
765 for (rid = pe->rid; rid < rid_end; rid++)
766 phb->ioda.pe_rmap[rid] = 0;
767
768 /* Release from all parents PELT-V */
769 while (parent) {
770 struct pci_dn *pdn = pci_get_pdn(parent);
771 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
772 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
773 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
774 /* XXX What to do in case of error ? */
775 }
776 parent = parent->bus->self;
777 }
778
Gavin Shanf951e512015-06-23 17:01:13 +1000779 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
Wei Yang781a8682015-03-25 16:23:57 +0800780 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
781
782 /* Disassociate PE in PELT */
783 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
784 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
785 if (rc)
786 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
787 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
788 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
789 if (rc)
790 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
791
792 pe->pbus = NULL;
793 pe->pdev = NULL;
794 pe->parent_dev = NULL;
795
796 return 0;
797}
798#endif /* CONFIG_PCI_IOV */
799
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800800static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000801{
802 struct pci_dev *parent;
803 uint8_t bcomp, dcomp, fcomp;
804 long rc, rid_end, rid;
805
806 /* Bus validation ? */
807 if (pe->pbus) {
808 int count;
809
810 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
811 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
812 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000813 if (pe->flags & PNV_IODA_PE_BUS_ALL)
814 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
815 else
816 count = 1;
817
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000818 switch(count) {
819 case 1: bcomp = OpalPciBusAll; break;
820 case 2: bcomp = OpalPciBus7Bits; break;
821 case 4: bcomp = OpalPciBus6Bits; break;
822 case 8: bcomp = OpalPciBus5Bits; break;
823 case 16: bcomp = OpalPciBus4Bits; break;
824 case 32: bcomp = OpalPciBus3Bits; break;
825 default:
Wei Yang781a8682015-03-25 16:23:57 +0800826 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
827 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000828 /* Do an exact match only */
829 bcomp = OpalPciBusAll;
830 }
831 rid_end = pe->rid + (count << 8);
832 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800833#ifdef CONFIG_PCI_IOV
834 if (pe->flags & PNV_IODA_PE_VF)
835 parent = pe->parent_dev;
836 else
837#endif /* CONFIG_PCI_IOV */
838 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000839 bcomp = OpalPciBusAll;
840 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
841 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
842 rid_end = pe->rid + 1;
843 }
844
Gavin Shan631ad692013-11-04 16:32:46 +0800845 /*
846 * Associate PE in PELT. We need add the PE into the
847 * corresponding PELT-V as well. Otherwise, the error
848 * originated from the PE might contribute to other
849 * PEs.
850 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000851 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
852 bcomp, dcomp, fcomp, OPAL_MAP_PE);
853 if (rc) {
854 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
855 return -ENXIO;
856 }
Gavin Shan631ad692013-11-04 16:32:46 +0800857
Alistair Popple5d2aa712015-12-17 13:43:13 +1100858 /*
859 * Configure PELTV. NPUs don't have a PELTV table so skip
860 * configuration on them.
861 */
862 if (phb->type != PNV_PHB_NPU)
863 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000864
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000865 /* Setup reverse map */
866 for (rid = pe->rid; rid < rid_end; rid++)
867 phb->ioda.pe_rmap[rid] = pe->pe_number;
868
869 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100870 if (phb->type != PNV_PHB_IODA1) {
871 pe->mve_number = 0;
872 goto out;
873 }
874
875 pe->mve_number = pe->pe_number;
876 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
877 if (rc != OPAL_SUCCESS) {
878 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
879 rc, pe->mve_number);
880 pe->mve_number = -1;
881 } else {
882 rc = opal_pci_set_mve_enable(phb->opal_id,
883 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000884 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100885 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000886 rc, pe->mve_number);
887 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000888 }
Gavin Shan4773f762014-11-12 13:36:09 +1100889 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000890
Gavin Shan4773f762014-11-12 13:36:09 +1100891out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000892 return 0;
893}
894
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800895static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
896 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000897{
898 struct pnv_ioda_pe *lpe;
899
Gavin Shan7ebdf952012-08-20 03:49:15 +0000900 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000901 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000902 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000903 return;
904 }
905 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000906 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000907}
908
909static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
910{
911 /* This is quite simplistic. The "base" weight of a device
912 * is 10. 0 means no DMA is to be accounted for it.
913 */
914
915 /* If it's a bridge, no DMA */
916 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
917 return 0;
918
919 /* Reduce the weight of slow USB controllers */
920 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
921 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
922 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
923 return 3;
924
925 /* Increase the weight of RAID (includes Obsidian) */
926 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
927 return 15;
928
929 /* Default */
930 return 10;
931}
932
Wei Yang781a8682015-03-25 16:23:57 +0800933#ifdef CONFIG_PCI_IOV
934static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
935{
936 struct pci_dn *pdn = pci_get_pdn(dev);
937 int i;
938 struct resource *res, res2;
939 resource_size_t size;
940 u16 num_vfs;
941
942 if (!dev->is_physfn)
943 return -EINVAL;
944
945 /*
946 * "offset" is in VFs. The M64 windows are sized so that when they
947 * are segmented, each segment is the same size as the IOV BAR.
948 * Each segment is in a separate PE, and the high order bits of the
949 * address are the PE number. Therefore, each VF's BAR is in a
950 * separate PE, and changing the IOV BAR start address changes the
951 * range of PEs the VFs are in.
952 */
953 num_vfs = pdn->num_vfs;
954 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
955 res = &dev->resource[i + PCI_IOV_RESOURCES];
956 if (!res->flags || !res->parent)
957 continue;
958
Wei Yang781a8682015-03-25 16:23:57 +0800959 /*
960 * The actual IOV BAR range is determined by the start address
961 * and the actual size for num_vfs VFs BAR. This check is to
962 * make sure that after shifting, the range will not overlap
963 * with another device.
964 */
965 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
966 res2.flags = res->flags;
967 res2.start = res->start + (size * offset);
968 res2.end = res2.start + (size * num_vfs) - 1;
969
970 if (res2.end > res->end) {
971 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
972 i, &res2, res, num_vfs, offset);
973 return -EBUSY;
974 }
975 }
976
977 /*
978 * After doing so, there would be a "hole" in the /proc/iomem when
979 * offset is a positive value. It looks like the device return some
980 * mmio back to the system, which actually no one could use it.
981 */
982 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
983 res = &dev->resource[i + PCI_IOV_RESOURCES];
984 if (!res->flags || !res->parent)
985 continue;
986
Wei Yang781a8682015-03-25 16:23:57 +0800987 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
988 res2 = *res;
989 res->start += size * offset;
990
Wei Yang74703cc2015-07-20 18:14:58 +0800991 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
992 i, &res2, res, (offset > 0) ? "En" : "Dis",
993 num_vfs, offset);
Wei Yang781a8682015-03-25 16:23:57 +0800994 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
995 }
996 return 0;
997}
998#endif /* CONFIG_PCI_IOV */
999
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001000static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001001{
1002 struct pci_controller *hose = pci_bus_to_host(dev->bus);
1003 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001004 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001005 struct pnv_ioda_pe *pe;
Gavin Shan689ee8c2016-05-03 15:41:25 +10001006 unsigned int pe_num;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001007
1008 if (!pdn) {
1009 pr_err("%s: Device tree node not associated properly\n",
1010 pci_name(dev));
1011 return NULL;
1012 }
1013 if (pdn->pe_number != IODA_INVALID_PE)
1014 return NULL;
1015
Alistair Popple5d2aa712015-12-17 13:43:13 +11001016 pe_num = pnv_ioda_alloc_pe(phb);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001017 if (pe_num == IODA_INVALID_PE) {
1018 pr_warning("%s: Not enough PE# available, disabling device\n",
1019 pci_name(dev));
1020 return NULL;
1021 }
1022
1023 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
1024 * pointer in the PE data structure, both should be destroyed at the
1025 * same time. However, this needs to be looked at more closely again
1026 * once we actually start removing things (Hotplug, SR-IOV, ...)
1027 *
1028 * At some point we want to remove the PDN completely anyways
1029 */
1030 pe = &phb->ioda.pe_array[pe_num];
1031 pci_dev_get(dev);
1032 pdn->pcidev = dev;
1033 pdn->pe_number = pe_num;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001034 pe->flags = PNV_IODA_PE_DEV;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001035 pe->pdev = dev;
1036 pe->pbus = NULL;
1037 pe->tce32_seg = -1;
1038 pe->mve_number = -1;
1039 pe->rid = dev->bus->number << 8 | pdn->devfn;
1040
1041 pe_info(pe, "Associated device to PE\n");
1042
1043 if (pnv_ioda_configure_pe(phb, pe)) {
1044 /* XXX What do we do here ? */
1045 if (pe_num)
1046 pnv_ioda_free_pe(phb, pe_num);
1047 pdn->pe_number = IODA_INVALID_PE;
1048 pe->pdev = NULL;
1049 pci_dev_put(dev);
1050 return NULL;
1051 }
1052
1053 /* Assign a DMA weight to the device */
1054 pe->dma_weight = pnv_ioda_dma_weight(dev);
1055 if (pe->dma_weight != 0) {
1056 phb->ioda.dma_weight += pe->dma_weight;
1057 phb->ioda.dma_pe_count++;
1058 }
1059
1060 /* Link the PE */
1061 pnv_ioda_link_pe_by_weight(phb, pe);
1062
1063 return pe;
1064}
1065
1066static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
1067{
1068 struct pci_dev *dev;
1069
1070 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001071 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001072
1073 if (pdn == NULL) {
1074 pr_warn("%s: No device node associated with device !\n",
1075 pci_name(dev));
1076 continue;
1077 }
Alistair Popple94973b22015-12-17 13:43:11 +11001078 pdn->pcidev = dev;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001079 pdn->pe_number = pe->pe_number;
1080 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001081 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001082 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1083 }
1084}
1085
Gavin Shanfb446ad2012-08-20 03:49:14 +00001086/*
1087 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1088 * single PCI bus. Another one that contains the primary PCI bus and its
1089 * subordinate PCI devices and buses. The second type of PE is normally
1090 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1091 */
Gavin Shand1203852015-06-19 12:26:18 +10001092static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001093{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001094 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001095 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001096 struct pnv_ioda_pe *pe;
Gavin Shan689ee8c2016-05-03 15:41:25 +10001097 unsigned int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001098
Guo Chao262af552014-07-21 14:42:30 +10001099 /* Check if PE is determined by M64 */
1100 if (phb->pick_m64_pe)
Gavin Shan26ba2482015-06-19 12:26:19 +10001101 pe_num = phb->pick_m64_pe(bus, all);
Guo Chao262af552014-07-21 14:42:30 +10001102
1103 /* The PE number isn't pinned by M64 */
1104 if (pe_num == IODA_INVALID_PE)
1105 pe_num = pnv_ioda_alloc_pe(phb);
1106
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001107 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001108 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1109 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001110 return;
1111 }
1112
1113 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +10001114 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001115 pe->pbus = bus;
1116 pe->pdev = NULL;
1117 pe->tce32_seg = -1;
1118 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001119 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001120 pe->dma_weight = 0;
1121
Gavin Shanfb446ad2012-08-20 03:49:14 +00001122 if (all)
1123 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
1124 bus->busn_res.start, bus->busn_res.end, pe_num);
1125 else
1126 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
1127 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001128
1129 if (pnv_ioda_configure_pe(phb, pe)) {
1130 /* XXX What do we do here ? */
1131 if (pe_num)
1132 pnv_ioda_free_pe(phb, pe_num);
1133 pe->pbus = NULL;
1134 return;
1135 }
1136
1137 /* Associate it with all child devices */
1138 pnv_ioda_setup_same_PE(bus, pe);
1139
Gavin Shan7ebdf952012-08-20 03:49:15 +00001140 /* Put PE to the list */
1141 list_add_tail(&pe->list, &phb->ioda.pe_list);
1142
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001143 /* Account for one DMA PE if at least one DMA capable device exist
1144 * below the bridge
1145 */
1146 if (pe->dma_weight != 0) {
1147 phb->ioda.dma_weight += pe->dma_weight;
1148 phb->ioda.dma_pe_count++;
1149 }
1150
1151 /* Link the PE */
1152 pnv_ioda_link_pe_by_weight(phb, pe);
1153}
1154
Alistair Poppleb5215492016-01-11 16:53:49 +11001155static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev)
Alistair Popple5d2aa712015-12-17 13:43:13 +11001156{
Alistair Poppleb5215492016-01-11 16:53:49 +11001157 int pe_num, found_pe = false, rc;
1158 long rid;
1159 struct pnv_ioda_pe *pe;
1160 struct pci_dev *gpu_pdev;
1161 struct pci_dn *npu_pdn;
1162 struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus);
1163 struct pnv_phb *phb = hose->private_data;
1164
1165 /*
1166 * Due to a hardware errata PE#0 on the NPU is reserved for
1167 * error handling. This means we only have three PEs remaining
1168 * which need to be assigned to four links, implying some
1169 * links must share PEs.
1170 *
1171 * To achieve this we assign PEs such that NPUs linking the
1172 * same GPU get assigned the same PE.
1173 */
1174 gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10001175 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
Alistair Poppleb5215492016-01-11 16:53:49 +11001176 pe = &phb->ioda.pe_array[pe_num];
1177 if (!pe->pdev)
1178 continue;
1179
1180 if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) {
1181 /*
1182 * This device has the same peer GPU so should
1183 * be assigned the same PE as the existing
1184 * peer NPU.
1185 */
1186 dev_info(&npu_pdev->dev,
1187 "Associating to existing PE %d\n", pe_num);
1188 pci_dev_get(npu_pdev);
1189 npu_pdn = pci_get_pdn(npu_pdev);
1190 rid = npu_pdev->bus->number << 8 | npu_pdn->devfn;
1191 npu_pdn->pcidev = npu_pdev;
1192 npu_pdn->pe_number = pe_num;
1193 pe->dma_weight += pnv_ioda_dma_weight(npu_pdev);
1194 phb->ioda.pe_rmap[rid] = pe->pe_number;
1195
1196 /* Map the PE to this link */
1197 rc = opal_pci_set_pe(phb->opal_id, pe_num, rid,
1198 OpalPciBusAll,
1199 OPAL_COMPARE_RID_DEVICE_NUMBER,
1200 OPAL_COMPARE_RID_FUNCTION_NUMBER,
1201 OPAL_MAP_PE);
1202 WARN_ON(rc != OPAL_SUCCESS);
1203 found_pe = true;
1204 break;
1205 }
1206 }
1207
1208 if (!found_pe)
1209 /*
1210 * Could not find an existing PE so allocate a new
1211 * one.
1212 */
1213 return pnv_ioda_setup_dev_PE(npu_pdev);
1214 else
1215 return pe;
1216}
1217
1218static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus)
1219{
Alistair Popple5d2aa712015-12-17 13:43:13 +11001220 struct pci_dev *pdev;
1221
1222 list_for_each_entry(pdev, &bus->devices, bus_list)
Alistair Poppleb5215492016-01-11 16:53:49 +11001223 pnv_ioda_setup_npu_PE(pdev);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001224}
1225
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001226static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001227{
1228 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001229
Gavin Shand1203852015-06-19 12:26:18 +10001230 pnv_ioda_setup_bus_PE(bus, false);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001231
1232 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001233 if (dev->subordinate) {
1234 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
Gavin Shand1203852015-06-19 12:26:18 +10001235 pnv_ioda_setup_bus_PE(dev->subordinate, true);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001236 else
1237 pnv_ioda_setup_PEs(dev->subordinate);
1238 }
1239 }
1240}
1241
1242/*
1243 * Configure PEs so that the downstream PCI buses and devices
1244 * could have their associated PE#. Unfortunately, we didn't
1245 * figure out the way to identify the PLX bridge yet. So we
1246 * simply put the PCI bus and the subordinate behind the root
1247 * port to PE# here. The game rule here is expected to be changed
1248 * as soon as we can detected PLX bridge correctly.
1249 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001250static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001251{
1252 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001253 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001254
1255 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001256 phb = hose->private_data;
1257
1258 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +11001259 if (phb->reserve_m64_pe)
Gavin Shan96a2f922015-06-19 12:26:17 +10001260 phb->reserve_m64_pe(hose->bus, NULL, true);
Guo Chao262af552014-07-21 14:42:30 +10001261
Alistair Popple5d2aa712015-12-17 13:43:13 +11001262 /*
1263 * On NPU PHB, we expect separate PEs for individual PCI
1264 * functions. PCI bus dependent PEs are required for the
1265 * remaining types of PHBs.
1266 */
Alistair Popple08f48f32016-01-11 16:53:50 +11001267 if (phb->type == PNV_PHB_NPU) {
1268 /* PE#0 is needed for error reporting */
1269 pnv_ioda_reserve_pe(phb, 0);
Alistair Poppleb5215492016-01-11 16:53:49 +11001270 pnv_ioda_setup_npu_PEs(hose->bus);
Alistair Popple08f48f32016-01-11 16:53:50 +11001271 } else
Alistair Popple5d2aa712015-12-17 13:43:13 +11001272 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001273 }
1274}
1275
Gavin Shana8b2f822015-03-25 16:23:52 +08001276#ifdef CONFIG_PCI_IOV
Wei Yangee8222f2015-10-22 09:22:16 +08001277static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001278{
1279 struct pci_bus *bus;
1280 struct pci_controller *hose;
1281 struct pnv_phb *phb;
1282 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001283 int i, j;
Wei Yangee8222f2015-10-22 09:22:16 +08001284 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001285
1286 bus = pdev->bus;
1287 hose = pci_bus_to_host(bus);
1288 phb = hose->private_data;
1289 pdn = pci_get_pdn(pdev);
1290
Wei Yangee8222f2015-10-22 09:22:16 +08001291 if (pdn->m64_single_mode)
1292 m64_bars = num_vfs;
1293 else
1294 m64_bars = 1;
1295
Wei Yang02639b02015-03-25 16:23:59 +08001296 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yangee8222f2015-10-22 09:22:16 +08001297 for (j = 0; j < m64_bars; j++) {
1298 if (pdn->m64_map[j][i] == IODA_INVALID_M64)
Wei Yang02639b02015-03-25 16:23:59 +08001299 continue;
1300 opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001301 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0);
1302 clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc);
1303 pdn->m64_map[j][i] = IODA_INVALID_M64;
Wei Yang02639b02015-03-25 16:23:59 +08001304 }
Wei Yang781a8682015-03-25 16:23:57 +08001305
Wei Yangee8222f2015-10-22 09:22:16 +08001306 kfree(pdn->m64_map);
Wei Yang781a8682015-03-25 16:23:57 +08001307 return 0;
1308}
1309
Wei Yang02639b02015-03-25 16:23:59 +08001310static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001311{
1312 struct pci_bus *bus;
1313 struct pci_controller *hose;
1314 struct pnv_phb *phb;
1315 struct pci_dn *pdn;
1316 unsigned int win;
1317 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001318 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001319 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001320 int total_vfs;
1321 resource_size_t size, start;
1322 int pe_num;
Wei Yangee8222f2015-10-22 09:22:16 +08001323 int m64_bars;
Wei Yang781a8682015-03-25 16:23:57 +08001324
1325 bus = pdev->bus;
1326 hose = pci_bus_to_host(bus);
1327 phb = hose->private_data;
1328 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001329 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001330
Wei Yangee8222f2015-10-22 09:22:16 +08001331 if (pdn->m64_single_mode)
1332 m64_bars = num_vfs;
1333 else
1334 m64_bars = 1;
Wei Yang02639b02015-03-25 16:23:59 +08001335
Wei Yangee8222f2015-10-22 09:22:16 +08001336 pdn->m64_map = kmalloc(sizeof(*pdn->m64_map) * m64_bars, GFP_KERNEL);
1337 if (!pdn->m64_map)
1338 return -ENOMEM;
1339 /* Initialize the m64_map to IODA_INVALID_M64 */
1340 for (i = 0; i < m64_bars ; i++)
1341 for (j = 0; j < PCI_SRIOV_NUM_BARS; j++)
1342 pdn->m64_map[i][j] = IODA_INVALID_M64;
1343
Wei Yang781a8682015-03-25 16:23:57 +08001344
1345 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1346 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1347 if (!res->flags || !res->parent)
1348 continue;
1349
Wei Yangee8222f2015-10-22 09:22:16 +08001350 for (j = 0; j < m64_bars; j++) {
Wei Yang02639b02015-03-25 16:23:59 +08001351 do {
1352 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1353 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001354
Wei Yang02639b02015-03-25 16:23:59 +08001355 if (win >= phb->ioda.m64_bar_idx + 1)
1356 goto m64_failed;
1357 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001358
Wei Yangee8222f2015-10-22 09:22:16 +08001359 pdn->m64_map[j][i] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001360
Wei Yangee8222f2015-10-22 09:22:16 +08001361 if (pdn->m64_single_mode) {
Wei Yang02639b02015-03-25 16:23:59 +08001362 size = pci_iov_resource_size(pdev,
1363 PCI_IOV_RESOURCES + i);
Wei Yang02639b02015-03-25 16:23:59 +08001364 start = res->start + size * j;
1365 } else {
1366 size = resource_size(res);
1367 start = res->start;
1368 }
1369
1370 /* Map the M64 here */
Wei Yangee8222f2015-10-22 09:22:16 +08001371 if (pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001372 pe_num = pdn->pe_num_map[j];
Wei Yang02639b02015-03-25 16:23:59 +08001373 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1374 pe_num, OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001375 pdn->m64_map[j][i], 0);
Wei Yang02639b02015-03-25 16:23:59 +08001376 }
1377
1378 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001379 OPAL_M64_WINDOW_TYPE,
Wei Yangee8222f2015-10-22 09:22:16 +08001380 pdn->m64_map[j][i],
Wei Yang02639b02015-03-25 16:23:59 +08001381 start,
Wei Yang781a8682015-03-25 16:23:57 +08001382 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001383 size);
Wei Yang781a8682015-03-25 16:23:57 +08001384
Wei Yang02639b02015-03-25 16:23:59 +08001385
1386 if (rc != OPAL_SUCCESS) {
1387 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1388 win, rc);
1389 goto m64_failed;
1390 }
1391
Wei Yangee8222f2015-10-22 09:22:16 +08001392 if (pdn->m64_single_mode)
Wei Yang02639b02015-03-25 16:23:59 +08001393 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001394 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2);
Wei Yang02639b02015-03-25 16:23:59 +08001395 else
1396 rc = opal_pci_phb_mmio_enable(phb->opal_id,
Wei Yangee8222f2015-10-22 09:22:16 +08001397 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1);
Wei Yang02639b02015-03-25 16:23:59 +08001398
1399 if (rc != OPAL_SUCCESS) {
1400 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1401 win, rc);
1402 goto m64_failed;
1403 }
Wei Yang781a8682015-03-25 16:23:57 +08001404 }
1405 }
1406 return 0;
1407
1408m64_failed:
Wei Yangee8222f2015-10-22 09:22:16 +08001409 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001410 return -EBUSY;
1411}
1412
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001413static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1414 int num);
1415static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1416
Wei Yang781a8682015-03-25 16:23:57 +08001417static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1418{
Wei Yang781a8682015-03-25 16:23:57 +08001419 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001420 int64_t rc;
1421
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001422 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001423 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001424 if (rc)
1425 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1426
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001427 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001428 if (pe->table_group.group) {
1429 iommu_group_put(pe->table_group.group);
1430 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001431 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001432 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001433 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001434}
1435
Wei Yangee8222f2015-10-22 09:22:16 +08001436static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
Wei Yang781a8682015-03-25 16:23:57 +08001437{
1438 struct pci_bus *bus;
1439 struct pci_controller *hose;
1440 struct pnv_phb *phb;
1441 struct pnv_ioda_pe *pe, *pe_n;
1442 struct pci_dn *pdn;
1443
1444 bus = pdev->bus;
1445 hose = pci_bus_to_host(bus);
1446 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001447 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001448
1449 if (!pdev->is_physfn)
1450 return;
1451
Wei Yang781a8682015-03-25 16:23:57 +08001452 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1453 if (pe->parent_dev != pdev)
1454 continue;
1455
1456 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1457
1458 /* Remove from list */
1459 mutex_lock(&phb->ioda.pe_list_mutex);
1460 list_del(&pe->list);
1461 mutex_unlock(&phb->ioda.pe_list_mutex);
1462
1463 pnv_ioda_deconfigure_pe(phb, pe);
1464
1465 pnv_ioda_free_pe(phb, pe->pe_number);
1466 }
1467}
1468
1469void pnv_pci_sriov_disable(struct pci_dev *pdev)
1470{
1471 struct pci_bus *bus;
1472 struct pci_controller *hose;
1473 struct pnv_phb *phb;
1474 struct pci_dn *pdn;
1475 struct pci_sriov *iov;
Wei Yangbe283ee2015-10-22 09:22:19 +08001476 u16 num_vfs, i;
Wei Yang781a8682015-03-25 16:23:57 +08001477
1478 bus = pdev->bus;
1479 hose = pci_bus_to_host(bus);
1480 phb = hose->private_data;
1481 pdn = pci_get_pdn(pdev);
1482 iov = pdev->sriov;
1483 num_vfs = pdn->num_vfs;
1484
1485 /* Release VF PEs */
Wei Yangee8222f2015-10-22 09:22:16 +08001486 pnv_ioda_release_vf_PE(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001487
1488 if (phb->type == PNV_PHB_IODA2) {
Wei Yangee8222f2015-10-22 09:22:16 +08001489 if (!pdn->m64_single_mode)
Wei Yangbe283ee2015-10-22 09:22:19 +08001490 pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001491
1492 /* Release M64 windows */
Wei Yangee8222f2015-10-22 09:22:16 +08001493 pnv_pci_vf_release_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001494
1495 /* Release PE numbers */
Wei Yangbe283ee2015-10-22 09:22:19 +08001496 if (pdn->m64_single_mode) {
1497 for (i = 0; i < num_vfs; i++) {
1498 if (pdn->pe_num_map[i] != IODA_INVALID_PE)
1499 pnv_ioda_free_pe(phb, pdn->pe_num_map[i]);
1500 }
1501 } else
1502 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1503 /* Releasing pe_num_map */
1504 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001505 }
1506}
1507
1508static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1509 struct pnv_ioda_pe *pe);
1510static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1511{
1512 struct pci_bus *bus;
1513 struct pci_controller *hose;
1514 struct pnv_phb *phb;
1515 struct pnv_ioda_pe *pe;
1516 int pe_num;
1517 u16 vf_index;
1518 struct pci_dn *pdn;
1519
1520 bus = pdev->bus;
1521 hose = pci_bus_to_host(bus);
1522 phb = hose->private_data;
1523 pdn = pci_get_pdn(pdev);
1524
1525 if (!pdev->is_physfn)
1526 return;
1527
1528 /* Reserve PE for each VF */
1529 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001530 if (pdn->m64_single_mode)
1531 pe_num = pdn->pe_num_map[vf_index];
1532 else
1533 pe_num = *pdn->pe_num_map + vf_index;
Wei Yang781a8682015-03-25 16:23:57 +08001534
1535 pe = &phb->ioda.pe_array[pe_num];
1536 pe->pe_number = pe_num;
1537 pe->phb = phb;
1538 pe->flags = PNV_IODA_PE_VF;
1539 pe->pbus = NULL;
1540 pe->parent_dev = pdev;
1541 pe->tce32_seg = -1;
1542 pe->mve_number = -1;
1543 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1544 pci_iov_virtfn_devfn(pdev, vf_index);
1545
1546 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1547 hose->global_number, pdev->bus->number,
1548 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1549 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1550
1551 if (pnv_ioda_configure_pe(phb, pe)) {
1552 /* XXX What do we do here ? */
1553 if (pe_num)
1554 pnv_ioda_free_pe(phb, pe_num);
1555 pe->pdev = NULL;
1556 continue;
1557 }
1558
Wei Yang781a8682015-03-25 16:23:57 +08001559 /* Put PE to the list */
1560 mutex_lock(&phb->ioda.pe_list_mutex);
1561 list_add_tail(&pe->list, &phb->ioda.pe_list);
1562 mutex_unlock(&phb->ioda.pe_list_mutex);
1563
1564 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1565 }
1566}
1567
1568int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1569{
1570 struct pci_bus *bus;
1571 struct pci_controller *hose;
1572 struct pnv_phb *phb;
1573 struct pci_dn *pdn;
1574 int ret;
Wei Yangbe283ee2015-10-22 09:22:19 +08001575 u16 i;
Wei Yang781a8682015-03-25 16:23:57 +08001576
1577 bus = pdev->bus;
1578 hose = pci_bus_to_host(bus);
1579 phb = hose->private_data;
1580 pdn = pci_get_pdn(pdev);
1581
1582 if (phb->type == PNV_PHB_IODA2) {
Wei Yangb0331852015-10-22 09:22:14 +08001583 if (!pdn->vfs_expanded) {
1584 dev_info(&pdev->dev, "don't support this SRIOV device"
1585 " with non 64bit-prefetchable IOV BAR\n");
1586 return -ENOSPC;
1587 }
1588
Wei Yangee8222f2015-10-22 09:22:16 +08001589 /*
1590 * When M64 BARs functions in Single PE mode, the number of VFs
1591 * could be enabled must be less than the number of M64 BARs.
1592 */
1593 if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) {
1594 dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n");
1595 return -EBUSY;
1596 }
1597
Wei Yangbe283ee2015-10-22 09:22:19 +08001598 /* Allocating pe_num_map */
1599 if (pdn->m64_single_mode)
1600 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map) * num_vfs,
1601 GFP_KERNEL);
1602 else
1603 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL);
1604
1605 if (!pdn->pe_num_map)
1606 return -ENOMEM;
1607
1608 if (pdn->m64_single_mode)
1609 for (i = 0; i < num_vfs; i++)
1610 pdn->pe_num_map[i] = IODA_INVALID_PE;
1611
Wei Yang781a8682015-03-25 16:23:57 +08001612 /* Calculate available PE for required VFs */
Wei Yangbe283ee2015-10-22 09:22:19 +08001613 if (pdn->m64_single_mode) {
1614 for (i = 0; i < num_vfs; i++) {
1615 pdn->pe_num_map[i] = pnv_ioda_alloc_pe(phb);
1616 if (pdn->pe_num_map[i] == IODA_INVALID_PE) {
1617 ret = -EBUSY;
1618 goto m64_failed;
1619 }
1620 }
1621 } else {
1622 mutex_lock(&phb->ioda.pe_alloc_mutex);
1623 *pdn->pe_num_map = bitmap_find_next_zero_area(
Gavin Shan92b8f132016-05-03 15:41:24 +10001624 phb->ioda.pe_alloc, phb->ioda.total_pe_num,
Wei Yangbe283ee2015-10-22 09:22:19 +08001625 0, num_vfs, 0);
Gavin Shan92b8f132016-05-03 15:41:24 +10001626 if (*pdn->pe_num_map >= phb->ioda.total_pe_num) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001627 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1628 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1629 kfree(pdn->pe_num_map);
1630 return -EBUSY;
1631 }
1632 bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001633 mutex_unlock(&phb->ioda.pe_alloc_mutex);
Wei Yang781a8682015-03-25 16:23:57 +08001634 }
Wei Yang781a8682015-03-25 16:23:57 +08001635 pdn->num_vfs = num_vfs;
Wei Yang781a8682015-03-25 16:23:57 +08001636
1637 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001638 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001639 if (ret) {
1640 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1641 goto m64_failed;
1642 }
1643
1644 /*
1645 * When using one M64 BAR to map one IOV BAR, we need to shift
1646 * the IOV BAR according to the PE# allocated to the VFs.
1647 * Otherwise, the PE# for the VF will conflict with others.
1648 */
Wei Yangee8222f2015-10-22 09:22:16 +08001649 if (!pdn->m64_single_mode) {
Wei Yangbe283ee2015-10-22 09:22:19 +08001650 ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map);
Wei Yang02639b02015-03-25 16:23:59 +08001651 if (ret)
1652 goto m64_failed;
1653 }
Wei Yang781a8682015-03-25 16:23:57 +08001654 }
1655
1656 /* Setup VF PEs */
1657 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1658
1659 return 0;
1660
1661m64_failed:
Wei Yangbe283ee2015-10-22 09:22:19 +08001662 if (pdn->m64_single_mode) {
1663 for (i = 0; i < num_vfs; i++) {
1664 if (pdn->pe_num_map[i] != IODA_INVALID_PE)
1665 pnv_ioda_free_pe(phb, pdn->pe_num_map[i]);
1666 }
1667 } else
1668 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
1669
1670 /* Releasing pe_num_map */
1671 kfree(pdn->pe_num_map);
Wei Yang781a8682015-03-25 16:23:57 +08001672
1673 return ret;
1674}
1675
Gavin Shana8b2f822015-03-25 16:23:52 +08001676int pcibios_sriov_disable(struct pci_dev *pdev)
1677{
Wei Yang781a8682015-03-25 16:23:57 +08001678 pnv_pci_sriov_disable(pdev);
1679
Gavin Shana8b2f822015-03-25 16:23:52 +08001680 /* Release PCI data */
1681 remove_dev_pci_data(pdev);
1682 return 0;
1683}
1684
1685int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1686{
1687 /* Allocate PCI data */
1688 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001689
Wei Yangee8222f2015-10-22 09:22:16 +08001690 return pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001691}
1692#endif /* CONFIG_PCI_IOV */
1693
Gavin Shan959c9bd2013-04-25 19:21:02 +00001694static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001695{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001696 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001697 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001698
Gavin Shan959c9bd2013-04-25 19:21:02 +00001699 /*
1700 * The function can be called while the PE#
1701 * hasn't been assigned. Do nothing for the
1702 * case.
1703 */
1704 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1705 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001706
Gavin Shan959c9bd2013-04-25 19:21:02 +00001707 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001708 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiy0e1ffef2015-08-27 16:01:16 +10001709 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001710 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001711 /*
1712 * Note: iommu_add_device() will fail here as
1713 * for physical PE: the device is already added by now;
1714 * for virtual PE: sysfs entries are not ready yet and
1715 * tce_iommu_bus_notifier will add the device to a group later.
1716 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001717}
1718
Daniel Axtens763d2d82015-04-28 15:12:07 +10001719static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001720{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001721 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1722 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001723 struct pci_dn *pdn = pci_get_pdn(pdev);
1724 struct pnv_ioda_pe *pe;
1725 uint64_t top;
1726 bool bypass = false;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001727 struct pci_dev *linked_npu_dev;
1728 int i;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001729
1730 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1731 return -ENODEV;;
1732
1733 pe = &phb->ioda.pe_array[pdn->pe_number];
1734 if (pe->tce_bypass_enabled) {
1735 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1736 bypass = (dma_mask >= top);
1737 }
1738
1739 if (bypass) {
1740 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1741 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001742 } else {
1743 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1744 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001745 }
Brian W Harta32305b2014-07-31 14:24:37 -05001746 *pdev->dev.dma_mask = dma_mask;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001747
1748 /* Update peer npu devices */
1749 if (pe->flags & PNV_IODA_PE_PEER)
Alistair Popple419dbd52016-01-08 11:35:09 +11001750 for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
1751 if (!pe->peers[i])
1752 continue;
1753
Alistair Popple5d2aa712015-12-17 13:43:13 +11001754 linked_npu_dev = pe->peers[i]->pdev;
1755 if (dma_get_mask(&linked_npu_dev->dev) != dma_mask)
1756 dma_set_mask(&linked_npu_dev->dev, dma_mask);
1757 }
1758
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001759 return 0;
1760}
1761
Andrew Donnellan535229822015-08-07 13:45:54 +10001762static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001763{
Andrew Donnellan535229822015-08-07 13:45:54 +10001764 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1765 struct pnv_phb *phb = hose->private_data;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001766 struct pci_dn *pdn = pci_get_pdn(pdev);
1767 struct pnv_ioda_pe *pe;
1768 u64 end, mask;
1769
1770 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1771 return 0;
1772
1773 pe = &phb->ioda.pe_array[pdn->pe_number];
1774 if (!pe->tce_bypass_enabled)
1775 return __dma_get_required_mask(&pdev->dev);
1776
1777
1778 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1779 mask = 1ULL << (fls64(end) - 1);
1780 mask += mask - 1;
1781
1782 return mask;
1783}
1784
Gavin Shandff4a392014-07-15 17:00:55 +10001785static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001786 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001787{
1788 struct pci_dev *dev;
1789
1790 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001791 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001792 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001793 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001794
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001795 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001796 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001797 }
1798}
1799
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001800static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
1801 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001802{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001803 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1804 &tbl->it_group_list, struct iommu_table_group_link,
1805 next);
1806 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001807 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001808 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001809 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1810 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001811 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001812 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001813
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001814 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1815 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1816 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001817
1818 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1819 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001820 start <<= shift;
1821 end <<= shift;
1822 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001823 start |= tbl->it_busno;
1824 end |= tbl->it_busno;
1825 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1826 /* p7ioc-style invalidation, 2 TCEs per write */
1827 start |= (1ull << 63);
1828 end |= (1ull << 63);
1829 inc = 16;
1830 } else {
1831 /* Default (older HW) */
1832 inc = 128;
1833 }
1834
1835 end |= inc - 1; /* round up end to be different than start */
1836
1837 mb(); /* Ensure above stores are visible */
1838 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001839 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001840 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001841 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001842 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001843 start += inc;
1844 }
1845
1846 /*
1847 * The iommu layer will do another mb() for us on build()
1848 * and we don't care on free()
1849 */
1850}
1851
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001852static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1853 long npages, unsigned long uaddr,
1854 enum dma_data_direction direction,
1855 struct dma_attrs *attrs)
1856{
1857 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1858 attrs);
1859
1860 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1861 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1862
1863 return ret;
1864}
1865
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001866#ifdef CONFIG_IOMMU_API
1867static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1868 unsigned long *hpa, enum dma_data_direction *direction)
1869{
1870 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1871
1872 if (!ret && (tbl->it_type &
1873 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1874 pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);
1875
1876 return ret;
1877}
1878#endif
1879
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001880static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1881 long npages)
1882{
1883 pnv_tce_free(tbl, index, npages);
1884
1885 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1886 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1887}
1888
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001889static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001890 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001891#ifdef CONFIG_IOMMU_API
1892 .exchange = pnv_ioda1_tce_xchg,
1893#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001894 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001895 .get = pnv_tce_get,
1896};
1897
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001898static inline void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_ioda_pe *pe)
1899{
1900 /* 01xb - invalidate TCEs that match the specified PE# */
1901 unsigned long val = (0x4ull << 60) | (pe->pe_number & 0xFF);
1902 struct pnv_phb *phb = pe->phb;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001903 struct pnv_ioda_pe *npe;
1904 int i;
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001905
1906 if (!phb->ioda.tce_inval_reg)
1907 return;
1908
1909 mb(); /* Ensure above stores are visible */
1910 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001911
1912 if (pe->flags & PNV_IODA_PE_PEER)
1913 for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
1914 npe = pe->peers[i];
1915 if (!npe || npe->phb->type != PNV_PHB_NPU)
1916 continue;
1917
1918 pnv_npu_tce_invalidate_entire(npe);
1919 }
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001920}
1921
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001922static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
1923 __be64 __iomem *invalidate, unsigned shift,
1924 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001925{
1926 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001927
1928 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001929 start = 0x2ull << 60;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001930 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001931 end = start;
1932
1933 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001934 start |= (index << shift);
1935 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001936 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001937 mb();
1938
1939 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001940 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001941 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001942 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001943 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001944 start += inc;
1945 }
1946}
1947
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001948static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1949 unsigned long index, unsigned long npages, bool rm)
1950{
1951 struct iommu_table_group_link *tgl;
1952
1953 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
Alistair Popple5d2aa712015-12-17 13:43:13 +11001954 struct pnv_ioda_pe *npe;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001955 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1956 struct pnv_ioda_pe, table_group);
1957 __be64 __iomem *invalidate = rm ?
1958 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1959 pe->phb->ioda.tce_inval_reg;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001960 int i;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001961
1962 pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
1963 invalidate, tbl->it_page_shift,
1964 index, npages);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001965
1966 if (pe->flags & PNV_IODA_PE_PEER)
1967 /* Invalidate PEs using the same TCE table */
1968 for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
1969 npe = pe->peers[i];
1970 if (!npe || npe->phb->type != PNV_PHB_NPU)
1971 continue;
1972
1973 pnv_npu_tce_invalidate(npe, tbl, index,
1974 npages, rm);
1975 }
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001976 }
1977}
1978
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001979static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1980 long npages, unsigned long uaddr,
1981 enum dma_data_direction direction,
1982 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001983{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001984 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1985 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001986
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001987 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1988 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1989
1990 return ret;
1991}
1992
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001993#ifdef CONFIG_IOMMU_API
1994static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1995 unsigned long *hpa, enum dma_data_direction *direction)
1996{
1997 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1998
1999 if (!ret && (tbl->it_type &
2000 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
2001 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
2002
2003 return ret;
2004}
2005#endif
2006
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002007static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
2008 long npages)
2009{
2010 pnv_tce_free(tbl, index, npages);
2011
2012 if (tbl->it_type & TCE_PCI_SWINV_FREE)
2013 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00002014}
2015
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002016static void pnv_ioda2_table_free(struct iommu_table *tbl)
2017{
2018 pnv_pci_ioda2_table_free_pages(tbl);
2019 iommu_free_table(tbl, "pnv");
2020}
2021
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002022static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002023 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10002024#ifdef CONFIG_IOMMU_API
2025 .exchange = pnv_ioda2_tce_xchg,
2026#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10002027 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002028 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002029 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002030};
2031
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002032static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
2033 struct pnv_ioda_pe *pe, unsigned int base,
2034 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002035{
2036
2037 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002038 struct iommu_table *tbl;
2039 unsigned int i;
2040 int64_t rc;
2041 void *addr;
2042
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002043 /* XXX FIXME: Handle 64-bit only DMA devices */
2044 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
2045 /* XXX FIXME: Allocate multi-level tables on PHB3 */
2046
2047 /* We shouldn't already have a 32-bit DMA associated */
2048 if (WARN_ON(pe->tce32_seg >= 0))
2049 return;
2050
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002051 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002052 iommu_register_group(&pe->table_group, phb->hose->global_number,
2053 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002054 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002055
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002056 /* Grab a 32-bit TCE table */
2057 pe->tce32_seg = base;
2058 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
2059 (base << 28), ((base + segs) << 28) - 1);
2060
2061 /* XXX Currently, we allocate one big contiguous table for the
2062 * TCEs. We only really need one chunk per 256M of TCE space
2063 * (ie per segment) but that's an optimization for later, it
2064 * requires some added smarts with our get/put_tce implementation
2065 */
2066 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
2067 get_order(TCE32_TABLE_SIZE * segs));
2068 if (!tce_mem) {
2069 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
2070 goto fail;
2071 }
2072 addr = page_address(tce_mem);
2073 memset(addr, 0, TCE32_TABLE_SIZE * segs);
2074
2075 /* Configure HW */
2076 for (i = 0; i < segs; i++) {
2077 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2078 pe->pe_number,
2079 base + i, 1,
2080 __pa(addr) + TCE32_TABLE_SIZE * i,
2081 TCE32_TABLE_SIZE, 0x1000);
2082 if (rc) {
2083 pe_err(pe, " Failed to configure 32-bit TCE table,"
2084 " err %ld\n", rc);
2085 goto fail;
2086 }
2087 }
2088
2089 /* Setup linux iommu table */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002090 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10002091 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002092
2093 /* OPAL variant of P7IOC SW invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002094 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10002095 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
2096 TCE_PCI_SWINV_FREE |
2097 TCE_PCI_SWINV_PAIR);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002098
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10002099 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002100 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
2101 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002102 iommu_init_table(tbl, phb->hose->node);
2103
Wei Yang781a8682015-03-25 16:23:57 +08002104 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002105 /*
2106 * Setting table base here only for carrying iommu_group
2107 * further down to let iommu_add_device() do the job.
2108 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2109 */
2110 set_iommu_table_base(&pe->pdev->dev, tbl);
2111 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002112 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002113 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10002114
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002115 return;
2116 fail:
2117 /* XXX Failure: Try to fallback to 64-bit only ? */
2118 if (pe->tce32_seg >= 0)
2119 pe->tce32_seg = -1;
2120 if (tce_mem)
2121 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10002122 if (tbl) {
2123 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
2124 iommu_free_table(tbl, "pnv");
2125 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002126}
2127
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002128static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
2129 int num, struct iommu_table *tbl)
2130{
2131 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2132 table_group);
2133 struct pnv_phb *phb = pe->phb;
2134 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002135 const unsigned long size = tbl->it_indirect_levels ?
2136 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002137 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
2138 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
2139
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002140 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002141 start_addr, start_addr + win_size - 1,
2142 IOMMU_PAGE_SIZE(tbl));
2143
2144 /*
2145 * Map TCE table through TVT. The TVE index is the PE number
2146 * shifted by 1 bit for 32-bits DMA space.
2147 */
2148 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2149 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002150 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002151 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002152 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002153 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002154 IOMMU_PAGE_SIZE(tbl));
2155 if (rc) {
2156 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
2157 return rc;
2158 }
2159
2160 pnv_pci_link_table_and_group(phb->hose->node, num,
2161 tbl, &pe->table_group);
2162 pnv_pci_ioda2_tce_invalidate_entire(pe);
2163
2164 return 0;
2165}
2166
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002167static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002168{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002169 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2170 int64_t rc;
2171
2172 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2173 if (enable) {
2174 phys_addr_t top = memblock_end_of_DRAM();
2175
2176 top = roundup_pow_of_two(top);
2177 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2178 pe->pe_number,
2179 window_id,
2180 pe->tce_bypass_base,
2181 top);
2182 } else {
2183 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2184 pe->pe_number,
2185 window_id,
2186 pe->tce_bypass_base,
2187 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002188 }
2189 if (rc)
2190 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2191 else
2192 pe->tce_bypass_enabled = enable;
2193}
2194
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002195static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2196 __u32 page_shift, __u64 window_size, __u32 levels,
2197 struct iommu_table *tbl);
2198
2199static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2200 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2201 struct iommu_table **ptbl)
2202{
2203 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2204 table_group);
2205 int nid = pe->phb->hose->node;
2206 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2207 long ret;
2208 struct iommu_table *tbl;
2209
2210 tbl = pnv_pci_table_alloc(nid);
2211 if (!tbl)
2212 return -ENOMEM;
2213
2214 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2215 bus_offset, page_shift, window_size,
2216 levels, tbl);
2217 if (ret) {
2218 iommu_free_table(tbl, "pnv");
2219 return ret;
2220 }
2221
2222 tbl->it_ops = &pnv_ioda2_iommu_ops;
2223 if (pe->phb->ioda.tce_inval_reg)
2224 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2225
2226 *ptbl = tbl;
2227
2228 return 0;
2229}
2230
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002231static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2232{
2233 struct iommu_table *tbl = NULL;
2234 long rc;
2235
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002236 /*
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002237 * crashkernel= specifies the kdump kernel's maximum memory at
2238 * some offset and there is no guaranteed the result is a power
2239 * of 2, which will cause errors later.
2240 */
2241 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
2242
2243 /*
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002244 * In memory constrained environments, e.g. kdump kernel, the
2245 * DMA window can be larger than available memory, which will
2246 * cause errors later.
2247 */
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002248 const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002249
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002250 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2251 IOMMU_PAGE_SHIFT_4K,
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002252 window_size,
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002253 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2254 if (rc) {
2255 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2256 rc);
2257 return rc;
2258 }
2259
2260 iommu_init_table(tbl, pe->phb->hose->node);
2261
2262 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2263 if (rc) {
2264 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2265 rc);
2266 pnv_ioda2_table_free(tbl);
2267 return rc;
2268 }
2269
2270 if (!pnv_iommu_bypass_disabled)
2271 pnv_pci_ioda2_set_bypass(pe, true);
2272
2273 /* OPAL variant of PHB3 invalidated TCEs */
2274 if (pe->phb->ioda.tce_inval_reg)
2275 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2276
2277 /*
2278 * Setting table base here only for carrying iommu_group
2279 * further down to let iommu_add_device() do the job.
2280 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2281 */
2282 if (pe->flags & PNV_IODA_PE_DEV)
2283 set_iommu_table_base(&pe->pdev->dev, tbl);
2284
2285 return 0;
2286}
2287
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002288#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2289static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2290 int num)
2291{
2292 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2293 table_group);
2294 struct pnv_phb *phb = pe->phb;
2295 long ret;
2296
2297 pe_info(pe, "Removing DMA window #%d\n", num);
2298
2299 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2300 (pe->pe_number << 1) + num,
2301 0/* levels */, 0/* table address */,
2302 0/* table size */, 0/* page size */);
2303 if (ret)
2304 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2305 else
2306 pnv_pci_ioda2_tce_invalidate_entire(pe);
2307
2308 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2309
2310 return ret;
2311}
2312#endif
2313
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002314#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002315static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2316 __u64 window_size, __u32 levels)
2317{
2318 unsigned long bytes = 0;
2319 const unsigned window_shift = ilog2(window_size);
2320 unsigned entries_shift = window_shift - page_shift;
2321 unsigned table_shift = entries_shift + 3;
2322 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2323 unsigned long direct_table_size;
2324
2325 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2326 (window_size > memory_hotplug_max()) ||
2327 !is_power_of_2(window_size))
2328 return 0;
2329
2330 /* Calculate a direct table size from window_size and levels */
2331 entries_shift = (entries_shift + levels - 1) / levels;
2332 table_shift = entries_shift + 3;
2333 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2334 direct_table_size = 1UL << table_shift;
2335
2336 for ( ; levels; --levels) {
2337 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2338
2339 tce_table_size /= direct_table_size;
2340 tce_table_size <<= 3;
2341 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2342 }
2343
2344 return bytes;
2345}
2346
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002347static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002348{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002349 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2350 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002351 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2352 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002353
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002354 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002355 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2356 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002357}
2358
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002359static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2360{
2361 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2362 table_group);
2363
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002364 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002365}
2366
2367static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002368 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002369 .create_table = pnv_pci_ioda2_create_table,
2370 .set_window = pnv_pci_ioda2_set_window,
2371 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002372 .take_ownership = pnv_ioda2_take_ownership,
2373 .release_ownership = pnv_ioda2_release_ownership,
2374};
2375#endif
2376
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002377static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2378{
2379 const __be64 *swinvp;
2380
2381 /* OPAL variant of PHB3 invalidated TCEs */
2382 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2383 if (!swinvp)
2384 return;
2385
2386 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2387 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2388}
2389
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002390static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2391 unsigned levels, unsigned long limit,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002392 unsigned long *current_offset, unsigned long *total_allocated)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002393{
2394 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002395 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002396 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002397 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2398 unsigned entries = 1UL << (shift - 3);
2399 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002400
2401 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2402 if (!tce_mem) {
2403 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2404 return NULL;
2405 }
2406 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002407 memset(addr, 0, allocated);
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002408 *total_allocated += allocated;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002409
2410 --levels;
2411 if (!levels) {
2412 *current_offset += allocated;
2413 return addr;
2414 }
2415
2416 for (i = 0; i < entries; ++i) {
2417 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002418 levels, limit, current_offset, total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002419 if (!tmp)
2420 break;
2421
2422 addr[i] = cpu_to_be64(__pa(tmp) |
2423 TCE_PCI_READ | TCE_PCI_WRITE);
2424
2425 if (*current_offset >= limit)
2426 break;
2427 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002428
2429 return addr;
2430}
2431
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002432static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2433 unsigned long size, unsigned level);
2434
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002435static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002436 __u32 page_shift, __u64 window_size, __u32 levels,
2437 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002438{
2439 void *addr;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002440 unsigned long offset = 0, level_shift, total_allocated = 0;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002441 const unsigned window_shift = ilog2(window_size);
2442 unsigned entries_shift = window_shift - page_shift;
2443 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2444 const unsigned long tce_table_size = 1UL << table_shift;
2445
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002446 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2447 return -EINVAL;
2448
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002449 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2450 return -EINVAL;
2451
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002452 /* Adjust direct table size from window_size and levels */
2453 entries_shift = (entries_shift + levels - 1) / levels;
2454 level_shift = entries_shift + 3;
2455 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2456
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002457 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002458 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002459 levels, tce_table_size, &offset, &total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002460
2461 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002462 if (!addr)
2463 return -ENOMEM;
2464
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002465 /*
2466 * First level was allocated but some lower level failed as
2467 * we did not allocate as much as we wanted,
2468 * release partially allocated table.
2469 */
2470 if (offset < tce_table_size) {
2471 pnv_pci_ioda2_table_do_free_pages(addr,
2472 1ULL << (level_shift - 3), levels - 1);
2473 return -ENOMEM;
2474 }
2475
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002476 /* Setup linux iommu table */
2477 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2478 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002479 tbl->it_level_size = 1ULL << (level_shift - 3);
2480 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002481 tbl->it_allocated_size = total_allocated;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002482
2483 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2484 window_size, tce_table_size, bus_offset);
2485
2486 return 0;
2487}
2488
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002489static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2490 unsigned long size, unsigned level)
2491{
2492 const unsigned long addr_ul = (unsigned long) addr &
2493 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2494
2495 if (level) {
2496 long i;
2497 u64 *tmp = (u64 *) addr_ul;
2498
2499 for (i = 0; i < size; ++i) {
2500 unsigned long hpa = be64_to_cpu(tmp[i]);
2501
2502 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2503 continue;
2504
2505 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2506 level - 1);
2507 }
2508 }
2509
2510 free_pages(addr_ul, get_order(size << 3));
2511}
2512
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002513static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2514{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002515 const unsigned long size = tbl->it_indirect_levels ?
2516 tbl->it_level_size : tbl->it_size;
2517
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002518 if (!tbl->it_size)
2519 return;
2520
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002521 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2522 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002523}
2524
Gavin Shan373f5652013-04-25 19:21:01 +00002525static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2526 struct pnv_ioda_pe *pe)
2527{
Gavin Shan373f5652013-04-25 19:21:01 +00002528 int64_t rc;
2529
2530 /* We shouldn't already have a 32-bit DMA associated */
2531 if (WARN_ON(pe->tce32_seg >= 0))
2532 return;
2533
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002534 /* TVE #1 is selected by PCI address bit 59 */
2535 pe->tce_bypass_base = 1ull << 59;
2536
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002537 iommu_register_group(&pe->table_group, phb->hose->global_number,
2538 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002539
Gavin Shan373f5652013-04-25 19:21:01 +00002540 /* The PE will reserve all possible 32-bits space */
2541 pe->tce32_seg = 0;
Gavin Shan373f5652013-04-25 19:21:01 +00002542 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002543 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002544
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002545 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002546 pe->table_group.tce32_start = 0;
2547 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2548 pe->table_group.max_dynamic_windows_supported =
2549 IOMMU_TABLE_GROUP_MAX_TABLES;
2550 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2551 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002552#ifdef CONFIG_IOMMU_API
2553 pe->table_group.ops = &pnv_pci_ioda2_ops;
2554#endif
2555
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002556 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan373f5652013-04-25 19:21:01 +00002557 if (rc) {
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002558 if (pe->tce32_seg >= 0)
2559 pe->tce32_seg = -1;
2560 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002561 }
2562
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002563 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002564 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002565 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002566 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002567}
2568
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002569static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002570{
2571 struct pci_controller *hose = phb->hose;
2572 unsigned int residual, remaining, segs, tw, base;
2573 struct pnv_ioda_pe *pe;
2574
2575 /* If we have more PE# than segments available, hand out one
2576 * per PE until we run out and let the rest fail. If not,
2577 * then we assign at least one segment per PE, plus more based
2578 * on the amount of devices under that PE
2579 */
2580 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
2581 residual = 0;
2582 else
2583 residual = phb->ioda.tce32_count -
2584 phb->ioda.dma_pe_count;
2585
2586 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
2587 hose->global_number, phb->ioda.tce32_count);
2588 pr_info("PCI: %d PE# for a total weight of %d\n",
2589 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
2590
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002591 pnv_pci_ioda_setup_opal_tce_kill(phb);
2592
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002593 /* Walk our PE list and configure their DMA segments, hand them
2594 * out one base segment plus any residual segments based on
2595 * weight
2596 */
2597 remaining = phb->ioda.tce32_count;
2598 tw = phb->ioda.dma_weight;
2599 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00002600 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002601 if (!pe->dma_weight)
2602 continue;
2603 if (!remaining) {
2604 pe_warn(pe, "No DMA32 resources available\n");
2605 continue;
2606 }
2607 segs = 1;
2608 if (residual) {
2609 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
2610 if (segs > remaining)
2611 segs = remaining;
2612 }
Gavin Shan373f5652013-04-25 19:21:01 +00002613
2614 /*
2615 * For IODA2 compliant PHB3, we needn't care about the weight.
2616 * The all available 32-bits DMA space will be assigned to
2617 * the specific PE.
2618 */
2619 if (phb->type == PNV_PHB_IODA1) {
2620 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
2621 pe->dma_weight, segs);
2622 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
Alistair Popple5d2aa712015-12-17 13:43:13 +11002623 } else if (phb->type == PNV_PHB_IODA2) {
Gavin Shan373f5652013-04-25 19:21:01 +00002624 pe_info(pe, "Assign DMA32 space\n");
2625 segs = 0;
2626 pnv_pci_ioda2_setup_dma_pe(phb, pe);
Alistair Popple5d2aa712015-12-17 13:43:13 +11002627 } else if (phb->type == PNV_PHB_NPU) {
2628 /*
2629 * We initialise the DMA space for an NPU PHB
2630 * after setup of the PHB is complete as we
2631 * point the NPU TVT to the the same location
2632 * as the PHB3 TVT.
2633 */
Gavin Shan373f5652013-04-25 19:21:01 +00002634 }
2635
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002636 remaining -= segs;
2637 base += segs;
2638 }
2639}
2640
2641#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002642static void pnv_ioda2_msi_eoi(struct irq_data *d)
2643{
2644 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2645 struct irq_chip *chip = irq_data_get_irq_chip(d);
2646 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2647 ioda.irq_chip);
2648 int64_t rc;
2649
2650 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2651 WARN_ON_ONCE(rc);
2652
2653 icp_native_eoi(d);
2654}
2655
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002656
2657static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2658{
2659 struct irq_data *idata;
2660 struct irq_chip *ichip;
2661
2662 if (phb->type != PNV_PHB_IODA2)
2663 return;
2664
2665 if (!phb->ioda.irq_chip_init) {
2666 /*
2667 * First time we setup an MSI IRQ, we need to setup the
2668 * corresponding IRQ chip to route correctly.
2669 */
2670 idata = irq_get_irq_data(virq);
2671 ichip = irq_data_get_irq_chip(idata);
2672 phb->ioda.irq_chip_init = 1;
2673 phb->ioda.irq_chip = *ichip;
2674 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2675 }
2676 irq_set_chip(virq, &phb->ioda.irq_chip);
2677}
2678
Ian Munsie80c49c72014-10-08 19:54:57 +11002679#ifdef CONFIG_CXL_BASE
2680
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002681struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002682{
2683 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2684
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002685 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002686}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002687EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002688
Ryan Grimm1212aa12015-01-19 11:52:50 -06002689int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002690{
2691 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2692 struct pnv_phb *phb = hose->private_data;
2693 struct pnv_ioda_pe *pe;
2694 int rc;
2695
2696 pe = pnv_ioda_get_pe(dev);
2697 if (!pe)
2698 return -ENODEV;
2699
2700 pe_info(pe, "Switching PHB to CXL\n");
2701
Ryan Grimm1212aa12015-01-19 11:52:50 -06002702 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11002703 if (rc)
2704 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2705
2706 return rc;
2707}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002708EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002709
2710/* Find PHB for cxl dev and allocate MSI hwirqs?
2711 * Returns the absolute hardware IRQ number
2712 */
2713int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2714{
2715 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2716 struct pnv_phb *phb = hose->private_data;
2717 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2718
2719 if (hwirq < 0) {
2720 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2721 return -ENOSPC;
2722 }
2723
2724 return phb->msi_base + hwirq;
2725}
2726EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2727
2728void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2729{
2730 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2731 struct pnv_phb *phb = hose->private_data;
2732
2733 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2734}
2735EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2736
2737void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2738 struct pci_dev *dev)
2739{
2740 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2741 struct pnv_phb *phb = hose->private_data;
2742 int i, hwirq;
2743
2744 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2745 if (!irqs->range[i])
2746 continue;
2747 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2748 i, irqs->offset[i],
2749 irqs->range[i]);
2750 hwirq = irqs->offset[i] - phb->msi_base;
2751 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2752 irqs->range[i]);
2753 }
2754}
2755EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2756
2757int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2758 struct pci_dev *dev, int num)
2759{
2760 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2761 struct pnv_phb *phb = hose->private_data;
2762 int i, hwirq, try;
2763
2764 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2765
2766 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2767 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2768 try = num;
2769 while (try) {
2770 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2771 if (hwirq >= 0)
2772 break;
2773 try /= 2;
2774 }
2775 if (!try)
2776 goto fail;
2777
2778 irqs->offset[i] = phb->msi_base + hwirq;
2779 irqs->range[i] = try;
2780 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2781 i, irqs->offset[i], irqs->range[i]);
2782 num -= try;
2783 }
2784 if (num)
2785 goto fail;
2786
2787 return 0;
2788fail:
2789 pnv_cxl_release_hwirq_ranges(irqs, dev);
2790 return -ENOSPC;
2791}
2792EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2793
2794int pnv_cxl_get_irq_count(struct pci_dev *dev)
2795{
2796 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2797 struct pnv_phb *phb = hose->private_data;
2798
2799 return phb->msi_bmp.irq_count;
2800}
2801EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2802
2803int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2804 unsigned int virq)
2805{
2806 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2807 struct pnv_phb *phb = hose->private_data;
2808 unsigned int xive_num = hwirq - phb->msi_base;
2809 struct pnv_ioda_pe *pe;
2810 int rc;
2811
2812 if (!(pe = pnv_ioda_get_pe(dev)))
2813 return -ENODEV;
2814
2815 /* Assign XIVE to PE */
2816 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2817 if (rc) {
2818 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2819 "hwirq 0x%x XIVE 0x%x PE\n",
2820 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2821 return -EIO;
2822 }
2823 set_msi_irq_chip(phb, virq);
2824
2825 return 0;
2826}
2827EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2828#endif
2829
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002830static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002831 unsigned int hwirq, unsigned int virq,
2832 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002833{
2834 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2835 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002836 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002837 int rc;
2838
2839 /* No PE assigned ? bail out ... no MSI for you ! */
2840 if (pe == NULL)
2841 return -ENXIO;
2842
2843 /* Check if we have an MVE */
2844 if (pe->mve_number < 0)
2845 return -ENXIO;
2846
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002847 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002848 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002849 is_64 = 0;
2850
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002851 /* Assign XIVE to PE */
2852 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2853 if (rc) {
2854 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2855 pci_name(dev), rc, xive_num);
2856 return -EIO;
2857 }
2858
2859 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002860 __be64 addr64;
2861
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002862 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2863 &addr64, &data);
2864 if (rc) {
2865 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2866 pci_name(dev), rc);
2867 return -EIO;
2868 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002869 msg->address_hi = be64_to_cpu(addr64) >> 32;
2870 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002871 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002872 __be32 addr32;
2873
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002874 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2875 &addr32, &data);
2876 if (rc) {
2877 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2878 pci_name(dev), rc);
2879 return -EIO;
2880 }
2881 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002882 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002883 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002884 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002885
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002886 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002887
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002888 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2889 " address=%x_%08x data=%x PE# %d\n",
2890 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2891 msg->address_hi, msg->address_lo, data, pe->pe_number);
2892
2893 return 0;
2894}
2895
2896static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2897{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002898 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002899 const __be32 *prop = of_get_property(phb->hose->dn,
2900 "ibm,opal-msi-ranges", NULL);
2901 if (!prop) {
2902 /* BML Fallback */
2903 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2904 }
2905 if (!prop)
2906 return;
2907
2908 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002909 count = be32_to_cpup(prop + 1);
2910 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002911 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2912 phb->hose->global_number);
2913 return;
2914 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002915
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002916 phb->msi_setup = pnv_pci_ioda_msi_setup;
2917 phb->msi32_support = 1;
2918 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002919 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002920}
2921#else
2922static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2923#endif /* CONFIG_PCI_MSI */
2924
Wei Yang6e628c72015-03-25 16:23:55 +08002925#ifdef CONFIG_PCI_IOV
2926static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2927{
Wei Yangf2dd0af2015-10-22 09:22:17 +08002928 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
2929 struct pnv_phb *phb = hose->private_data;
2930 const resource_size_t gate = phb->ioda.m64_segsize >> 2;
Wei Yang6e628c72015-03-25 16:23:55 +08002931 struct resource *res;
2932 int i;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002933 resource_size_t size, total_vf_bar_sz;
Wei Yang6e628c72015-03-25 16:23:55 +08002934 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002935 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002936
2937 if (!pdev->is_physfn || pdev->is_added)
2938 return;
2939
Wei Yang6e628c72015-03-25 16:23:55 +08002940 pdn = pci_get_pdn(pdev);
2941 pdn->vfs_expanded = 0;
Wei Yangee8222f2015-10-22 09:22:16 +08002942 pdn->m64_single_mode = false;
Wei Yang6e628c72015-03-25 16:23:55 +08002943
Wei Yang5b88ec22015-03-25 16:23:58 +08002944 total_vfs = pci_sriov_get_totalvfs(pdev);
Gavin Shan92b8f132016-05-03 15:41:24 +10002945 mul = phb->ioda.total_pe_num;
Wei Yangdfcc8d42015-10-22 09:22:18 +08002946 total_vf_bar_sz = 0;
Wei Yang5b88ec22015-03-25 16:23:58 +08002947
2948 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2949 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2950 if (!res->flags || res->parent)
2951 continue;
2952 if (!pnv_pci_is_mem_pref_64(res->flags)) {
Wei Yangb0331852015-10-22 09:22:14 +08002953 dev_warn(&pdev->dev, "Don't support SR-IOV with"
2954 " non M64 VF BAR%d: %pR. \n",
Wei Yang5b88ec22015-03-25 16:23:58 +08002955 i, res);
Wei Yangb0331852015-10-22 09:22:14 +08002956 goto truncate_iov;
Wei Yang5b88ec22015-03-25 16:23:58 +08002957 }
2958
Wei Yangdfcc8d42015-10-22 09:22:18 +08002959 total_vf_bar_sz += pci_iov_resource_size(pdev,
2960 i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002961
Wei Yangf2dd0af2015-10-22 09:22:17 +08002962 /*
2963 * If bigger than quarter of M64 segment size, just round up
2964 * power of two.
2965 *
2966 * Generally, one M64 BAR maps one IOV BAR. To avoid conflict
2967 * with other devices, IOV BAR size is expanded to be
2968 * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64
2969 * segment size , the expanded size would equal to half of the
2970 * whole M64 space size, which will exhaust the M64 Space and
2971 * limit the system flexibility. This is a design decision to
2972 * set the boundary to quarter of the M64 segment size.
2973 */
Wei Yangdfcc8d42015-10-22 09:22:18 +08002974 if (total_vf_bar_sz > gate) {
Wei Yang5b88ec22015-03-25 16:23:58 +08002975 mul = roundup_pow_of_two(total_vfs);
Wei Yangdfcc8d42015-10-22 09:22:18 +08002976 dev_info(&pdev->dev,
2977 "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n",
2978 total_vf_bar_sz, gate, mul);
Wei Yangee8222f2015-10-22 09:22:16 +08002979 pdn->m64_single_mode = true;
Wei Yang5b88ec22015-03-25 16:23:58 +08002980 break;
2981 }
2982 }
2983
Wei Yang6e628c72015-03-25 16:23:55 +08002984 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2985 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2986 if (!res->flags || res->parent)
2987 continue;
Wei Yang6e628c72015-03-25 16:23:55 +08002988
Wei Yang6e628c72015-03-25 16:23:55 +08002989 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yangee8222f2015-10-22 09:22:16 +08002990 /*
2991 * On PHB3, the minimum size alignment of M64 BAR in single
2992 * mode is 32MB.
2993 */
2994 if (pdn->m64_single_mode && (size < SZ_32M))
2995 goto truncate_iov;
2996 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
Wei Yang5b88ec22015-03-25 16:23:58 +08002997 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08002998 dev_dbg(&pdev->dev, " %pR\n", res);
2999 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08003000 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08003001 }
Wei Yang5b88ec22015-03-25 16:23:58 +08003002 pdn->vfs_expanded = mul;
Wei Yangb0331852015-10-22 09:22:14 +08003003
3004 return;
3005
3006truncate_iov:
3007 /* To save MMIO space, IOV BAR is truncated. */
3008 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
3009 res = &pdev->resource[i + PCI_IOV_RESOURCES];
3010 res->flags = 0;
3011 res->end = res->start - 1;
3012 }
Wei Yang6e628c72015-03-25 16:23:55 +08003013}
3014#endif /* CONFIG_PCI_IOV */
3015
Gavin Shan23e79422016-05-03 15:41:27 +10003016static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
3017 struct resource *res)
3018{
3019 struct pnv_phb *phb = pe->phb;
3020 struct pci_bus_region region;
3021 int index;
3022 int64_t rc;
3023
3024 if (!res || !res->flags || res->start > res->end)
3025 return;
3026
3027 if (res->flags & IORESOURCE_IO) {
3028 region.start = res->start - phb->ioda.io_pci_base;
3029 region.end = res->end - phb->ioda.io_pci_base;
3030 index = region.start / phb->ioda.io_segsize;
3031
3032 while (index < phb->ioda.total_pe_num &&
3033 region.start <= region.end) {
3034 phb->ioda.io_segmap[index] = pe->pe_number;
3035 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3036 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
3037 if (rc != OPAL_SUCCESS) {
3038 pr_err("%s: Error %lld mapping IO segment#%d to PE#%d\n",
3039 __func__, rc, index, pe->pe_number);
3040 break;
3041 }
3042
3043 region.start += phb->ioda.io_segsize;
3044 index++;
3045 }
3046 } else if ((res->flags & IORESOURCE_MEM) &&
3047 !pnv_pci_is_mem_pref_64(res->flags)) {
3048 region.start = res->start -
3049 phb->hose->mem_offset[0] -
3050 phb->ioda.m32_pci_base;
3051 region.end = res->end -
3052 phb->hose->mem_offset[0] -
3053 phb->ioda.m32_pci_base;
3054 index = region.start / phb->ioda.m32_segsize;
3055
3056 while (index < phb->ioda.total_pe_num &&
3057 region.start <= region.end) {
3058 phb->ioda.m32_segmap[index] = pe->pe_number;
3059 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
3060 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
3061 if (rc != OPAL_SUCCESS) {
3062 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%d",
3063 __func__, rc, index, pe->pe_number);
3064 break;
3065 }
3066
3067 region.start += phb->ioda.m32_segsize;
3068 index++;
3069 }
3070 }
3071}
3072
Gavin Shan11685be2012-08-20 03:49:16 +00003073/*
3074 * This function is supposed to be called on basis of PE from top
3075 * to bottom style. So the the I/O or MMIO segment assigned to
3076 * parent PE could be overrided by its child PEs if necessary.
3077 */
Gavin Shan23e79422016-05-03 15:41:27 +10003078static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00003079{
Gavin Shan69d733e2016-05-03 15:41:28 +10003080 struct pci_dev *pdev;
Gavin Shan23e79422016-05-03 15:41:27 +10003081 int i;
Gavin Shan11685be2012-08-20 03:49:16 +00003082
3083 /*
3084 * NOTE: We only care PCI bus based PE for now. For PCI
3085 * device based PE, for example SRIOV sensitive VF should
3086 * be figured out later.
3087 */
3088 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
3089
Gavin Shan69d733e2016-05-03 15:41:28 +10003090 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
3091 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
3092 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
3093
3094 /*
3095 * If the PE contains all subordinate PCI buses, the
3096 * windows of the child bridges should be mapped to
3097 * the PE as well.
3098 */
3099 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
3100 continue;
3101 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
3102 pnv_ioda_setup_pe_res(pe,
3103 &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
3104 }
Gavin Shan11685be2012-08-20 03:49:16 +00003105}
3106
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003107static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00003108{
3109 struct pci_controller *tmp, *hose;
3110 struct pnv_phb *phb;
3111 struct pnv_ioda_pe *pe;
3112
3113 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3114 phb = hose->private_data;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003115
3116 /* NPU PHB does not support IO or MMIO segmentation */
3117 if (phb->type == PNV_PHB_NPU)
3118 continue;
3119
Gavin Shan11685be2012-08-20 03:49:16 +00003120 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
Gavin Shan23e79422016-05-03 15:41:27 +10003121 pnv_ioda_setup_pe_seg(pe);
Gavin Shan11685be2012-08-20 03:49:16 +00003122 }
3123 }
3124}
3125
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003126static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00003127{
3128 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00003129 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00003130
3131 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3132 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00003133
3134 /* Mark the PHB initialization done */
3135 phb = hose->private_data;
3136 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00003137 }
3138}
3139
Gavin Shan37c367f2013-06-20 18:13:25 +08003140static void pnv_pci_ioda_create_dbgfs(void)
3141{
3142#ifdef CONFIG_DEBUG_FS
3143 struct pci_controller *hose, *tmp;
3144 struct pnv_phb *phb;
3145 char name[16];
3146
3147 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3148 phb = hose->private_data;
3149
3150 sprintf(name, "PCI%04x", hose->global_number);
3151 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
3152 if (!phb->dbgfs)
3153 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
3154 __func__, hose->global_number);
3155 }
3156#endif /* CONFIG_DEBUG_FS */
3157}
3158
Alistair Popple5d2aa712015-12-17 13:43:13 +11003159static void pnv_npu_ioda_fixup(void)
3160{
3161 bool enable_bypass;
3162 struct pci_controller *hose, *tmp;
3163 struct pnv_phb *phb;
3164 struct pnv_ioda_pe *pe;
3165
3166 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
3167 phb = hose->private_data;
3168 if (phb->type != PNV_PHB_NPU)
3169 continue;
3170
3171 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
3172 enable_bypass = dma_get_mask(&pe->pdev->dev) ==
3173 DMA_BIT_MASK(64);
3174 pnv_npu_init_dma_pe(pe);
3175 pnv_npu_dma_set_bypass(pe, enable_bypass);
3176 }
3177 }
3178}
3179
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003180static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00003181{
3182 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00003183 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00003184 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003185
Gavin Shan37c367f2013-06-20 18:13:25 +08003186 pnv_pci_ioda_create_dbgfs();
3187
Gavin Shane9cc17d2013-06-20 13:21:14 +08003188#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08003189 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04003190 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003191#endif
Alistair Popple5d2aa712015-12-17 13:43:13 +11003192
3193 /* Link NPU IODA tables to their PCI devices. */
3194 pnv_npu_ioda_fixup();
Gavin Shanfb446ad2012-08-20 03:49:14 +00003195}
3196
Gavin Shan271fd032012-09-11 16:59:47 -06003197/*
3198 * Returns the alignment for I/O or memory windows for P2P
3199 * bridges. That actually depends on how PEs are segmented.
3200 * For now, we return I/O or M32 segment size for PE sensitive
3201 * P2P bridges. Otherwise, the default values (4KiB for I/O,
3202 * 1MiB for memory) will be returned.
3203 *
3204 * The current PCI bus might be put into one PE, which was
3205 * create against the parent PCI bridge. For that case, we
3206 * needn't enlarge the alignment so that we can save some
3207 * resources.
3208 */
3209static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
3210 unsigned long type)
3211{
3212 struct pci_dev *bridge;
3213 struct pci_controller *hose = pci_bus_to_host(bus);
3214 struct pnv_phb *phb = hose->private_data;
3215 int num_pci_bridges = 0;
3216
3217 bridge = bus->self;
3218 while (bridge) {
3219 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
3220 num_pci_bridges++;
3221 if (num_pci_bridges >= 2)
3222 return 1;
3223 }
3224
3225 bridge = bridge->bus->self;
3226 }
3227
Guo Chao262af552014-07-21 14:42:30 +10003228 /* We fail back to M32 if M64 isn't supported */
3229 if (phb->ioda.m64_segsize &&
3230 pnv_pci_is_mem_pref_64(type))
3231 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06003232 if (type & IORESOURCE_MEM)
3233 return phb->ioda.m32_segsize;
3234
3235 return phb->ioda.io_segsize;
3236}
3237
Wei Yang5350ab32015-03-25 16:23:56 +08003238#ifdef CONFIG_PCI_IOV
3239static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
3240 int resno)
3241{
Wei Yangee8222f2015-10-22 09:22:16 +08003242 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
3243 struct pnv_phb *phb = hose->private_data;
Wei Yang5350ab32015-03-25 16:23:56 +08003244 struct pci_dn *pdn = pci_get_pdn(pdev);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003245 resource_size_t align;
Wei Yang5350ab32015-03-25 16:23:56 +08003246
Wei Yang7fbe7a92015-10-22 09:22:15 +08003247 /*
3248 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
3249 * SR-IOV. While from hardware perspective, the range mapped by M64
3250 * BAR should be size aligned.
3251 *
Wei Yangee8222f2015-10-22 09:22:16 +08003252 * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra
3253 * powernv-specific hardware restriction is gone. But if just use the
3254 * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with
3255 * in one segment of M64 #15, which introduces the PE conflict between
3256 * PF and VF. Based on this, the minimum alignment of an IOV BAR is
3257 * m64_segsize.
3258 *
Wei Yang7fbe7a92015-10-22 09:22:15 +08003259 * This function returns the total IOV BAR size if M64 BAR is in
3260 * Shared PE mode or just VF BAR size if not.
Wei Yangee8222f2015-10-22 09:22:16 +08003261 * If the M64 BAR is in Single PE mode, return the VF BAR size or
3262 * M64 segment size if IOV BAR size is less.
Wei Yang7fbe7a92015-10-22 09:22:15 +08003263 */
Wei Yang5350ab32015-03-25 16:23:56 +08003264 align = pci_iov_resource_size(pdev, resno);
Wei Yang7fbe7a92015-10-22 09:22:15 +08003265 if (!pdn->vfs_expanded)
3266 return align;
Wei Yangee8222f2015-10-22 09:22:16 +08003267 if (pdn->m64_single_mode)
3268 return max(align, (resource_size_t)phb->ioda.m64_segsize);
Wei Yang5350ab32015-03-25 16:23:56 +08003269
Wei Yang7fbe7a92015-10-22 09:22:15 +08003270 return pdn->vfs_expanded * align;
Wei Yang5350ab32015-03-25 16:23:56 +08003271}
3272#endif /* CONFIG_PCI_IOV */
3273
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003274/* Prevent enabling devices for which we couldn't properly
3275 * assign a PE
3276 */
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003277static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003278{
Gavin Shandb1266c2012-08-20 03:49:18 +00003279 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3280 struct pnv_phb *phb = hose->private_data;
3281 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003282
Gavin Shandb1266c2012-08-20 03:49:18 +00003283 /* The function is probably called while the PEs have
3284 * not be created yet. For example, resource reassignment
3285 * during PCI probe period. We just skip the check if
3286 * PEs isn't ready.
3287 */
3288 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003289 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003290
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003291 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003292 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003293 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003294
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003295 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003296}
3297
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003298static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003299{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003300 struct pnv_phb *phb = hose->private_data;
3301
Gavin Shand1a85ee2014-09-30 12:39:05 +10003302 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003303 OPAL_ASSERT_RESET);
3304}
3305
Daniel Axtens92ae0352015-04-28 15:12:05 +10003306static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003307 .dma_dev_setup = pnv_pci_dma_dev_setup,
3308 .dma_bus_setup = pnv_pci_dma_bus_setup,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003309#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003310 .setup_msi_irqs = pnv_setup_msi_irqs,
3311 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003312#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003313 .enable_device_hook = pnv_pci_enable_device_hook,
3314 .window_alignment = pnv_pci_window_alignment,
3315 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3316 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
3317 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3318 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003319};
3320
Alistair Popple5d2aa712015-12-17 13:43:13 +11003321static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
Gavin Shancb4224c2016-05-03 15:41:21 +10003322 .dma_dev_setup = pnv_pci_dma_dev_setup,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003323#ifdef CONFIG_PCI_MSI
Gavin Shancb4224c2016-05-03 15:41:21 +10003324 .setup_msi_irqs = pnv_setup_msi_irqs,
3325 .teardown_msi_irqs = pnv_teardown_msi_irqs,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003326#endif
Gavin Shancb4224c2016-05-03 15:41:21 +10003327 .enable_device_hook = pnv_pci_enable_device_hook,
3328 .window_alignment = pnv_pci_window_alignment,
3329 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3330 .dma_set_mask = pnv_npu_dma_set_mask,
3331 .shutdown = pnv_pci_ioda_shutdown,
Alistair Popple5d2aa712015-12-17 13:43:13 +11003332};
3333
Anton Blancharde51df2c2014-08-20 08:55:18 +10003334static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3335 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003336{
3337 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003338 struct pnv_phb *phb;
Gavin Shan93289d82016-05-03 15:41:29 +10003339 unsigned long size, m64map_off, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10003340 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003341 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003342 int len;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003343 unsigned int segno;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003344 u64 phb_id;
3345 void *aux;
3346 long rc;
3347
Gavin Shan58d714e2013-07-31 16:47:00 +08003348 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003349
3350 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3351 if (!prop64) {
3352 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3353 return;
3354 }
3355 phb_id = be64_to_cpup(prop64);
3356 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3357
Michael Ellermane39f223f2014-11-18 16:47:35 +11003358 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003359
3360 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003361 phb->hose = hose = pcibios_alloc_controller(np);
3362 if (!phb->hose) {
3363 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003364 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003365 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003366 return;
3367 }
3368
3369 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003370 prop32 = of_get_property(np, "bus-range", &len);
3371 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003372 hose->first_busno = be32_to_cpu(prop32[0]);
3373 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003374 } else {
3375 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3376 hose->first_busno = 0;
3377 hose->last_busno = 0xff;
3378 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003379 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003380 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003381 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003382 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003383 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003384
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003385 /* Detect specific models for error handling */
3386 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3387 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003388 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003389 phb->model = PNV_PHB_MODEL_PHB3;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003390 else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
3391 phb->model = PNV_PHB_MODEL_NPU;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003392 else
3393 phb->model = PNV_PHB_MODEL_UNKNOWN;
3394
Gavin Shanaa0c0332013-04-25 19:20:57 +00003395 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003396 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003397
Gavin Shanaa0c0332013-04-25 19:20:57 +00003398 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003399 phb->regs = of_iomap(np, 0);
3400 if (phb->regs == NULL)
3401 pr_err(" Failed to map registers !\n");
3402
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003403 /* Initialize more IODA stuff */
Gavin Shan92b8f132016-05-03 15:41:24 +10003404 phb->ioda.total_pe_num = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003405 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003406 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003407 phb->ioda.total_pe_num = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003408 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3409 if (prop32)
Gavin Shan92b8f132016-05-03 15:41:24 +10003410 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003411
3412 /* Parse 64-bit MMIO range */
3413 pnv_ioda_parse_m64_window(phb);
3414
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003415 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003416 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003417 phb->ioda.m32_size += 0x10000;
3418
Gavin Shan92b8f132016-05-03 15:41:24 +10003419 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003420 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003421 phb->ioda.io_size = hose->pci_io_size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003422 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003423 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3424
Gavin Shanc35d2a82013-07-31 16:47:04 +08003425 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Gavin Shan92b8f132016-05-03 15:41:24 +10003426 size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
Gavin Shan93289d82016-05-03 15:41:29 +10003427 m64map_off = size;
3428 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003429 m32map_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003430 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003431 if (phb->type == PNV_PHB_IODA1) {
3432 iomap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003433 size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003434 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003435 pemap_off = size;
Gavin Shan92b8f132016-05-03 15:41:24 +10003436 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003437 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003438 phb->ioda.pe_alloc = aux;
Gavin Shan93289d82016-05-03 15:41:29 +10003439 phb->ioda.m64_segmap = aux + m64map_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003440 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shan93289d82016-05-03 15:41:29 +10003441 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
3442 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003443 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
Gavin Shan93289d82016-05-03 15:41:29 +10003444 }
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003445 if (phb->type == PNV_PHB_IODA1) {
Gavin Shanc35d2a82013-07-31 16:47:04 +08003446 phb->ioda.io_segmap = aux + iomap_off;
Gavin Shan3fa23ff2016-05-03 15:41:26 +10003447 for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
3448 phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
3449 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003450 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan92b8f132016-05-03 15:41:24 +10003451 set_bit(phb->ioda.reserved_pe_idx, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003452
Gavin Shan7ebdf952012-08-20 03:49:15 +00003453 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003454 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003455 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003456
3457 /* Calculate how many 32-bit TCE segments we have */
3458 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
3459
Gavin Shanaa0c0332013-04-25 19:20:57 +00003460#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003461 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3462 window_type,
3463 window_num,
3464 starting_real_address,
3465 starting_pci_address,
3466 segment_size);
3467#endif
3468
Guo Chao262af552014-07-21 14:42:30 +10003469 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
Gavin Shan92b8f132016-05-03 15:41:24 +10003470 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
Guo Chao262af552014-07-21 14:42:30 +10003471 phb->ioda.m32_size, phb->ioda.m32_segsize);
3472 if (phb->ioda.m64_size)
3473 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3474 phb->ioda.m64_size, phb->ioda.m64_segsize);
3475 if (phb->ioda.io_size)
3476 pr_info(" IO: 0x%x [segment=0x%x]\n",
3477 phb->ioda.io_size, phb->ioda.io_segsize);
3478
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003479
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003480 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003481 phb->get_pe_state = pnv_ioda_get_pe_state;
3482 phb->freeze_pe = pnv_ioda_freeze_pe;
3483 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003484
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003485 /* Setup TCEs */
3486 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
3487
3488 /* Setup MSI support */
3489 pnv_pci_init_ioda_msis(phb);
3490
Gavin Shanc40a4212012-08-20 03:49:20 +00003491 /*
3492 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3493 * to let the PCI core do resource assignment. It's supposed
3494 * that the PCI core will do correct I/O and MMIO alignment
3495 * for the P2P bridge bars so that each PCI bus (excluding
3496 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003497 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003498 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003499
3500 if (phb->type == PNV_PHB_NPU)
3501 hose->controller_ops = pnv_npu_ioda_controller_ops;
3502 else
3503 hose->controller_ops = pnv_pci_ioda_controller_ops;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003504
Wei Yang6e628c72015-03-25 16:23:55 +08003505#ifdef CONFIG_PCI_IOV
3506 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003507 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003508#endif
3509
Gavin Shanc40a4212012-08-20 03:49:20 +00003510 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003511
3512 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003513 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003514 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003515 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003516
3517 /* If we're running in kdump kerenl, the previous kerenl never
3518 * shutdown PCI devices correctly. We already got IODA table
3519 * cleaned out. So we have to issue PHB reset to stop all PCI
3520 * transactions from previous kerenl.
3521 */
3522 if (is_kdump_kernel()) {
3523 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003524 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3525 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003526 }
Guo Chao262af552014-07-21 14:42:30 +10003527
Gavin Shan9e9e8932014-11-12 13:36:05 +11003528 /* Remove M64 resource if we can't configure it successfully */
3529 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003530 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003531}
3532
Bjorn Helgaas67975002013-07-02 12:20:03 -06003533void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003534{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003535 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003536}
3537
Alistair Popple5d2aa712015-12-17 13:43:13 +11003538void __init pnv_pci_init_npu_phb(struct device_node *np)
3539{
3540 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU);
3541}
3542
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003543void __init pnv_pci_init_ioda_hub(struct device_node *np)
3544{
3545 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003546 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003547 u64 hub_id;
3548
3549 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3550
3551 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3552 if (!prop64) {
3553 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3554 return;
3555 }
3556 hub_id = be64_to_cpup(prop64);
3557 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3558
3559 /* Count child PHBs */
3560 for_each_child_of_node(np, phbn) {
3561 /* Look for IODA1 PHBs */
3562 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003563 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003564 }
3565}