blob: 323e1e58da93c37f86c82d7d7065ce01535c7f99 [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
Wei Yang781a8682015-03-25 16:23:57 +080051/* 256M DMA window, 4K TCE pages, 8 bytes TCE */
52#define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
53
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +100054#define POWERNV_IOMMU_DEFAULT_LEVELS 1
55#define POWERNV_IOMMU_MAX_LEVELS 5
56
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +100057static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
58
Joe Perches6d31c2f2014-09-21 10:55:06 -070059static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
60 const char *fmt, ...)
61{
62 struct va_format vaf;
63 va_list args;
64 char pfix[32];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000065
Joe Perches6d31c2f2014-09-21 10:55:06 -070066 va_start(args, fmt);
67
68 vaf.fmt = fmt;
69 vaf.va = &args;
70
Wei Yang781a8682015-03-25 16:23:57 +080071 if (pe->flags & PNV_IODA_PE_DEV)
Joe Perches6d31c2f2014-09-21 10:55:06 -070072 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
Wei Yang781a8682015-03-25 16:23:57 +080073 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Joe Perches6d31c2f2014-09-21 10:55:06 -070074 sprintf(pfix, "%04x:%02x ",
75 pci_domain_nr(pe->pbus), pe->pbus->number);
Wei Yang781a8682015-03-25 16:23:57 +080076#ifdef CONFIG_PCI_IOV
77 else if (pe->flags & PNV_IODA_PE_VF)
78 sprintf(pfix, "%04x:%02x:%2x.%d",
79 pci_domain_nr(pe->parent_dev->bus),
80 (pe->rid & 0xff00) >> 8,
81 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
82#endif /* CONFIG_PCI_IOV*/
Joe Perches6d31c2f2014-09-21 10:55:06 -070083
84 printk("%spci %s: [PE# %.3d] %pV",
85 level, pfix, pe->pe_number, &vaf);
86
87 va_end(args);
88}
89
90#define pe_err(pe, fmt, ...) \
91 pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
92#define pe_warn(pe, fmt, ...) \
93 pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
94#define pe_info(pe, fmt, ...) \
95 pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +000096
Thadeu Lima de Souza Cascardo4e287842014-10-23 19:19:35 -020097static bool pnv_iommu_bypass_disabled __read_mostly;
98
99static int __init iommu_setup(char *str)
100{
101 if (!str)
102 return -EINVAL;
103
104 while (*str) {
105 if (!strncmp(str, "nobypass", 8)) {
106 pnv_iommu_bypass_disabled = true;
107 pr_info("PowerNV: IOMMU bypass window disabled.\n");
108 break;
109 }
110 str += strcspn(str, ",");
111 if (*str == ',')
112 str++;
113 }
114
115 return 0;
116}
117early_param("iommu", iommu_setup);
118
Guo Chao262af552014-07-21 14:42:30 +1000119static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
120{
121 return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
122 (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
123}
124
Gavin Shan4b82ab12014-11-12 13:36:07 +1100125static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
126{
127 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
128 pr_warn("%s: Invalid PE %d on PHB#%x\n",
129 __func__, pe_no, phb->hose->global_number);
130 return;
131 }
132
Gavin Shane9dc4d72015-06-19 12:26:16 +1000133 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
134 pr_debug("%s: PE %d was reserved on PHB#%x\n",
135 __func__, pe_no, phb->hose->global_number);
Gavin Shan4b82ab12014-11-12 13:36:07 +1100136
137 phb->ioda.pe_array[pe_no].phb = phb;
138 phb->ioda.pe_array[pe_no].pe_number = pe_no;
139}
140
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800141static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000142{
143 unsigned long pe;
144
145 do {
146 pe = find_next_zero_bit(phb->ioda.pe_alloc,
147 phb->ioda.total_pe, 0);
148 if (pe >= phb->ioda.total_pe)
149 return IODA_INVALID_PE;
150 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
151
Gavin Shan4cce9552013-04-25 19:21:00 +0000152 phb->ioda.pe_array[pe].phb = phb;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000153 phb->ioda.pe_array[pe].pe_number = pe;
154 return pe;
155}
156
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800157static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000158{
159 WARN_ON(phb->ioda.pe_array[pe].pdev);
160
161 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
162 clear_bit(pe, phb->ioda.pe_alloc);
163}
164
Guo Chao262af552014-07-21 14:42:30 +1000165/* The default M64 BAR is shared by all PEs */
166static int pnv_ioda2_init_m64(struct pnv_phb *phb)
167{
168 const char *desc;
169 struct resource *r;
170 s64 rc;
171
172 /* Configure the default M64 BAR */
173 rc = opal_pci_set_phb_mem_window(phb->opal_id,
174 OPAL_M64_WINDOW_TYPE,
175 phb->ioda.m64_bar_idx,
176 phb->ioda.m64_base,
177 0, /* unused */
178 phb->ioda.m64_size);
179 if (rc != OPAL_SUCCESS) {
180 desc = "configuring";
181 goto fail;
182 }
183
184 /* Enable the default M64 BAR */
185 rc = opal_pci_phb_mmio_enable(phb->opal_id,
186 OPAL_M64_WINDOW_TYPE,
187 phb->ioda.m64_bar_idx,
188 OPAL_ENABLE_M64_SPLIT);
189 if (rc != OPAL_SUCCESS) {
190 desc = "enabling";
191 goto fail;
192 }
193
194 /* Mark the M64 BAR assigned */
195 set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
196
197 /*
198 * Strip off the segment used by the reserved PE, which is
199 * expected to be 0 or last one of PE capabicity.
200 */
201 r = &phb->hose->mem_resources[1];
202 if (phb->ioda.reserved_pe == 0)
203 r->start += phb->ioda.m64_segsize;
204 else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
205 r->end -= phb->ioda.m64_segsize;
206 else
207 pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
208 phb->ioda.reserved_pe);
209
210 return 0;
211
212fail:
213 pr_warn(" Failure %lld %s M64 BAR#%d\n",
214 rc, desc, phb->ioda.m64_bar_idx);
215 opal_pci_phb_mmio_enable(phb->opal_id,
216 OPAL_M64_WINDOW_TYPE,
217 phb->ioda.m64_bar_idx,
218 OPAL_DISABLE_M64);
219 return -EIO;
220}
221
Gavin Shan96a2f922015-06-19 12:26:17 +1000222static void pnv_ioda2_reserve_dev_m64_pe(struct pci_dev *pdev,
223 unsigned long *pe_bitmap)
Guo Chao262af552014-07-21 14:42:30 +1000224{
Gavin Shan96a2f922015-06-19 12:26:17 +1000225 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
226 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000227 struct resource *r;
Gavin Shan96a2f922015-06-19 12:26:17 +1000228 resource_size_t base, sgsz, start, end;
229 int segno, i;
Guo Chao262af552014-07-21 14:42:30 +1000230
Gavin Shan96a2f922015-06-19 12:26:17 +1000231 base = phb->ioda.m64_base;
232 sgsz = phb->ioda.m64_segsize;
233 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
234 r = &pdev->resource[i];
235 if (!r->parent || !pnv_pci_is_mem_pref_64(r->flags))
236 continue;
Guo Chao262af552014-07-21 14:42:30 +1000237
Gavin Shan96a2f922015-06-19 12:26:17 +1000238 start = _ALIGN_DOWN(r->start - base, sgsz);
239 end = _ALIGN_UP(r->end - base, sgsz);
240 for (segno = start / sgsz; segno < end / sgsz; segno++) {
241 if (pe_bitmap)
242 set_bit(segno, pe_bitmap);
243 else
244 pnv_ioda_reserve_pe(phb, segno);
Guo Chao262af552014-07-21 14:42:30 +1000245 }
246 }
247}
248
Gavin Shan96a2f922015-06-19 12:26:17 +1000249static void pnv_ioda2_reserve_m64_pe(struct pci_bus *bus,
250 unsigned long *pe_bitmap,
251 bool all)
Guo Chao262af552014-07-21 14:42:30 +1000252{
Guo Chao262af552014-07-21 14:42:30 +1000253 struct pci_dev *pdev;
Gavin Shan96a2f922015-06-19 12:26:17 +1000254
255 list_for_each_entry(pdev, &bus->devices, bus_list) {
256 pnv_ioda2_reserve_dev_m64_pe(pdev, pe_bitmap);
257
258 if (all && pdev->subordinate)
259 pnv_ioda2_reserve_m64_pe(pdev->subordinate,
260 pe_bitmap, all);
261 }
262}
263
Gavin Shan26ba2482015-06-19 12:26:19 +1000264static int pnv_ioda2_pick_m64_pe(struct pci_bus *bus, bool all)
Guo Chao262af552014-07-21 14:42:30 +1000265{
Gavin Shan26ba2482015-06-19 12:26:19 +1000266 struct pci_controller *hose = pci_bus_to_host(bus);
267 struct pnv_phb *phb = hose->private_data;
Guo Chao262af552014-07-21 14:42:30 +1000268 struct pnv_ioda_pe *master_pe, *pe;
269 unsigned long size, *pe_alloc;
Gavin Shan26ba2482015-06-19 12:26:19 +1000270 int i;
Guo Chao262af552014-07-21 14:42:30 +1000271
272 /* Root bus shouldn't use M64 */
273 if (pci_is_root_bus(bus))
274 return IODA_INVALID_PE;
275
Guo Chao262af552014-07-21 14:42:30 +1000276 /* Allocate bitmap */
277 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
278 pe_alloc = kzalloc(size, GFP_KERNEL);
279 if (!pe_alloc) {
280 pr_warn("%s: Out of memory !\n",
281 __func__);
282 return IODA_INVALID_PE;
283 }
284
Gavin Shan26ba2482015-06-19 12:26:19 +1000285 /* Figure out reserved PE numbers by the PE */
286 pnv_ioda2_reserve_m64_pe(bus, pe_alloc, all);
Guo Chao262af552014-07-21 14:42:30 +1000287
288 /*
289 * the current bus might not own M64 window and that's all
290 * contributed by its child buses. For the case, we needn't
291 * pick M64 dependent PE#.
292 */
293 if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
294 kfree(pe_alloc);
295 return IODA_INVALID_PE;
296 }
297
298 /*
299 * Figure out the master PE and put all slave PEs to master
300 * PE's list to form compound PE.
301 */
Guo Chao262af552014-07-21 14:42:30 +1000302 master_pe = NULL;
303 i = -1;
304 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
305 phb->ioda.total_pe) {
306 pe = &phb->ioda.pe_array[i];
Guo Chao262af552014-07-21 14:42:30 +1000307
308 if (!master_pe) {
309 pe->flags |= PNV_IODA_PE_MASTER;
310 INIT_LIST_HEAD(&pe->slaves);
311 master_pe = pe;
312 } else {
313 pe->flags |= PNV_IODA_PE_SLAVE;
314 pe->master = master_pe;
315 list_add_tail(&pe->list, &master_pe->slaves);
316 }
317 }
318
319 kfree(pe_alloc);
320 return master_pe->pe_number;
321}
322
323static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
324{
325 struct pci_controller *hose = phb->hose;
326 struct device_node *dn = hose->dn;
327 struct resource *res;
328 const u32 *r;
329 u64 pci_addr;
330
Gavin Shan1665c4a2014-11-12 13:36:04 +1100331 /* FIXME: Support M64 for P7IOC */
332 if (phb->type != PNV_PHB_IODA2) {
333 pr_info(" Not support M64 window\n");
334 return;
335 }
336
Stewart Smithe4d54f72015-12-09 17:18:20 +1100337 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
Guo Chao262af552014-07-21 14:42:30 +1000338 pr_info(" Firmware too old to support M64 window\n");
339 return;
340 }
341
342 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
343 if (!r) {
344 pr_info(" No <ibm,opal-m64-window> on %s\n",
345 dn->full_name);
346 return;
347 }
348
Guo Chao262af552014-07-21 14:42:30 +1000349 res = &hose->mem_resources[1];
Gavin Shane80c4e72015-10-22 12:03:08 +1100350 res->name = dn->full_name;
Guo Chao262af552014-07-21 14:42:30 +1000351 res->start = of_translate_address(dn, r + 2);
352 res->end = res->start + of_read_number(r + 4, 2) - 1;
353 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
354 pci_addr = of_read_number(r, 2);
355 hose->mem_offset[1] = res->start - pci_addr;
356
357 phb->ioda.m64_size = resource_size(res);
358 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
359 phb->ioda.m64_base = pci_addr;
360
Wei Yange9863e62014-12-12 12:39:37 +0800361 pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
362 res->start, res->end, pci_addr);
363
Guo Chao262af552014-07-21 14:42:30 +1000364 /* Use last M64 BAR to cover M64 window */
365 phb->ioda.m64_bar_idx = 15;
366 phb->init_m64 = pnv_ioda2_init_m64;
Gavin Shan5ef73562014-11-12 13:36:06 +1100367 phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
Guo Chao262af552014-07-21 14:42:30 +1000368 phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
369}
370
Gavin Shan49dec922014-07-21 14:42:33 +1000371static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
372{
373 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
374 struct pnv_ioda_pe *slave;
375 s64 rc;
376
377 /* Fetch master PE */
378 if (pe->flags & PNV_IODA_PE_SLAVE) {
379 pe = pe->master;
Gavin Shanec8e4e92014-11-12 13:36:10 +1100380 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
381 return;
382
Gavin Shan49dec922014-07-21 14:42:33 +1000383 pe_no = pe->pe_number;
384 }
385
386 /* Freeze master PE */
387 rc = opal_pci_eeh_freeze_set(phb->opal_id,
388 pe_no,
389 OPAL_EEH_ACTION_SET_FREEZE_ALL);
390 if (rc != OPAL_SUCCESS) {
391 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
392 __func__, rc, phb->hose->global_number, pe_no);
393 return;
394 }
395
396 /* Freeze slave PEs */
397 if (!(pe->flags & PNV_IODA_PE_MASTER))
398 return;
399
400 list_for_each_entry(slave, &pe->slaves, list) {
401 rc = opal_pci_eeh_freeze_set(phb->opal_id,
402 slave->pe_number,
403 OPAL_EEH_ACTION_SET_FREEZE_ALL);
404 if (rc != OPAL_SUCCESS)
405 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
406 __func__, rc, phb->hose->global_number,
407 slave->pe_number);
408 }
409}
410
Anton Blancharde51df2c2014-08-20 08:55:18 +1000411static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
Gavin Shan49dec922014-07-21 14:42:33 +1000412{
413 struct pnv_ioda_pe *pe, *slave;
414 s64 rc;
415
416 /* Find master PE */
417 pe = &phb->ioda.pe_array[pe_no];
418 if (pe->flags & PNV_IODA_PE_SLAVE) {
419 pe = pe->master;
420 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
421 pe_no = pe->pe_number;
422 }
423
424 /* Clear frozen state for master PE */
425 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
426 if (rc != OPAL_SUCCESS) {
427 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
428 __func__, rc, opt, phb->hose->global_number, pe_no);
429 return -EIO;
430 }
431
432 if (!(pe->flags & PNV_IODA_PE_MASTER))
433 return 0;
434
435 /* Clear frozen state for slave PEs */
436 list_for_each_entry(slave, &pe->slaves, list) {
437 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
438 slave->pe_number,
439 opt);
440 if (rc != OPAL_SUCCESS) {
441 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
442 __func__, rc, opt, phb->hose->global_number,
443 slave->pe_number);
444 return -EIO;
445 }
446 }
447
448 return 0;
449}
450
451static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
452{
453 struct pnv_ioda_pe *slave, *pe;
454 u8 fstate, state;
455 __be16 pcierr;
456 s64 rc;
457
458 /* Sanity check on PE number */
459 if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
460 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
461
462 /*
463 * Fetch the master PE and the PE instance might be
464 * not initialized yet.
465 */
466 pe = &phb->ioda.pe_array[pe_no];
467 if (pe->flags & PNV_IODA_PE_SLAVE) {
468 pe = pe->master;
469 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
470 pe_no = pe->pe_number;
471 }
472
473 /* Check the master PE */
474 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
475 &state, &pcierr, NULL);
476 if (rc != OPAL_SUCCESS) {
477 pr_warn("%s: Failure %lld getting "
478 "PHB#%x-PE#%x state\n",
479 __func__, rc,
480 phb->hose->global_number, pe_no);
481 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
482 }
483
484 /* Check the slave PE */
485 if (!(pe->flags & PNV_IODA_PE_MASTER))
486 return state;
487
488 list_for_each_entry(slave, &pe->slaves, list) {
489 rc = opal_pci_eeh_freeze_status(phb->opal_id,
490 slave->pe_number,
491 &fstate,
492 &pcierr,
493 NULL);
494 if (rc != OPAL_SUCCESS) {
495 pr_warn("%s: Failure %lld getting "
496 "PHB#%x-PE#%x state\n",
497 __func__, rc,
498 phb->hose->global_number, slave->pe_number);
499 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
500 }
501
502 /*
503 * Override the result based on the ascending
504 * priority.
505 */
506 if (fstate > state)
507 state = fstate;
508 }
509
510 return state;
511}
512
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000513/* Currently those 2 are only used when MSIs are enabled, this will change
514 * but in the meantime, we need to protect them to avoid warnings
515 */
516#ifdef CONFIG_PCI_MSI
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800517static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000518{
519 struct pci_controller *hose = pci_bus_to_host(dev->bus);
520 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000521 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000522
523 if (!pdn)
524 return NULL;
525 if (pdn->pe_number == IODA_INVALID_PE)
526 return NULL;
527 return &phb->ioda.pe_array[pdn->pe_number];
528}
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000529#endif /* CONFIG_PCI_MSI */
530
Gavin Shanb131a842014-11-12 13:36:08 +1100531static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
532 struct pnv_ioda_pe *parent,
533 struct pnv_ioda_pe *child,
534 bool is_add)
535{
536 const char *desc = is_add ? "adding" : "removing";
537 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
538 OPAL_REMOVE_PE_FROM_DOMAIN;
539 struct pnv_ioda_pe *slave;
540 long rc;
541
542 /* Parent PE affects child PE */
543 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
544 child->pe_number, op);
545 if (rc != OPAL_SUCCESS) {
546 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
547 rc, desc);
548 return -ENXIO;
549 }
550
551 if (!(child->flags & PNV_IODA_PE_MASTER))
552 return 0;
553
554 /* Compound case: parent PE affects slave PEs */
555 list_for_each_entry(slave, &child->slaves, list) {
556 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
557 slave->pe_number, op);
558 if (rc != OPAL_SUCCESS) {
559 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
560 rc, desc);
561 return -ENXIO;
562 }
563 }
564
565 return 0;
566}
567
568static int pnv_ioda_set_peltv(struct pnv_phb *phb,
569 struct pnv_ioda_pe *pe,
570 bool is_add)
571{
572 struct pnv_ioda_pe *slave;
Wei Yang781a8682015-03-25 16:23:57 +0800573 struct pci_dev *pdev = NULL;
Gavin Shanb131a842014-11-12 13:36:08 +1100574 int ret;
575
576 /*
577 * Clear PE frozen state. If it's master PE, we need
578 * clear slave PE frozen state as well.
579 */
580 if (is_add) {
581 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
582 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
583 if (pe->flags & PNV_IODA_PE_MASTER) {
584 list_for_each_entry(slave, &pe->slaves, list)
585 opal_pci_eeh_freeze_clear(phb->opal_id,
586 slave->pe_number,
587 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
588 }
589 }
590
591 /*
592 * Associate PE in PELT. We need add the PE into the
593 * corresponding PELT-V as well. Otherwise, the error
594 * originated from the PE might contribute to other
595 * PEs.
596 */
597 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
598 if (ret)
599 return ret;
600
601 /* For compound PEs, any one affects all of them */
602 if (pe->flags & PNV_IODA_PE_MASTER) {
603 list_for_each_entry(slave, &pe->slaves, list) {
604 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
605 if (ret)
606 return ret;
607 }
608 }
609
610 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
611 pdev = pe->pbus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800612 else if (pe->flags & PNV_IODA_PE_DEV)
Gavin Shanb131a842014-11-12 13:36:08 +1100613 pdev = pe->pdev->bus->self;
Wei Yang781a8682015-03-25 16:23:57 +0800614#ifdef CONFIG_PCI_IOV
615 else if (pe->flags & PNV_IODA_PE_VF)
Gavin Shan283e2d82015-06-22 13:45:47 +1000616 pdev = pe->parent_dev;
Wei Yang781a8682015-03-25 16:23:57 +0800617#endif /* CONFIG_PCI_IOV */
Gavin Shanb131a842014-11-12 13:36:08 +1100618 while (pdev) {
619 struct pci_dn *pdn = pci_get_pdn(pdev);
620 struct pnv_ioda_pe *parent;
621
622 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
623 parent = &phb->ioda.pe_array[pdn->pe_number];
624 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
625 if (ret)
626 return ret;
627 }
628
629 pdev = pdev->bus->self;
630 }
631
632 return 0;
633}
634
Wei Yang781a8682015-03-25 16:23:57 +0800635#ifdef CONFIG_PCI_IOV
636static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
637{
638 struct pci_dev *parent;
639 uint8_t bcomp, dcomp, fcomp;
640 int64_t rc;
641 long rid_end, rid;
642
643 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
644 if (pe->pbus) {
645 int count;
646
647 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
648 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
649 parent = pe->pbus->self;
650 if (pe->flags & PNV_IODA_PE_BUS_ALL)
651 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
652 else
653 count = 1;
654
655 switch(count) {
656 case 1: bcomp = OpalPciBusAll; break;
657 case 2: bcomp = OpalPciBus7Bits; break;
658 case 4: bcomp = OpalPciBus6Bits; break;
659 case 8: bcomp = OpalPciBus5Bits; break;
660 case 16: bcomp = OpalPciBus4Bits; break;
661 case 32: bcomp = OpalPciBus3Bits; break;
662 default:
663 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
664 count);
665 /* Do an exact match only */
666 bcomp = OpalPciBusAll;
667 }
668 rid_end = pe->rid + (count << 8);
669 } else {
670 if (pe->flags & PNV_IODA_PE_VF)
671 parent = pe->parent_dev;
672 else
673 parent = pe->pdev->bus->self;
674 bcomp = OpalPciBusAll;
675 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
676 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
677 rid_end = pe->rid + 1;
678 }
679
680 /* Clear the reverse map */
681 for (rid = pe->rid; rid < rid_end; rid++)
682 phb->ioda.pe_rmap[rid] = 0;
683
684 /* Release from all parents PELT-V */
685 while (parent) {
686 struct pci_dn *pdn = pci_get_pdn(parent);
687 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
688 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
689 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
690 /* XXX What to do in case of error ? */
691 }
692 parent = parent->bus->self;
693 }
694
Gavin Shanf951e512015-06-23 17:01:13 +1000695 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
Wei Yang781a8682015-03-25 16:23:57 +0800696 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
697
698 /* Disassociate PE in PELT */
699 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
700 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
701 if (rc)
702 pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
703 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
704 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
705 if (rc)
706 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
707
708 pe->pbus = NULL;
709 pe->pdev = NULL;
710 pe->parent_dev = NULL;
711
712 return 0;
713}
714#endif /* CONFIG_PCI_IOV */
715
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800716static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000717{
718 struct pci_dev *parent;
719 uint8_t bcomp, dcomp, fcomp;
720 long rc, rid_end, rid;
721
722 /* Bus validation ? */
723 if (pe->pbus) {
724 int count;
725
726 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
727 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
728 parent = pe->pbus->self;
Gavin Shanfb446ad2012-08-20 03:49:14 +0000729 if (pe->flags & PNV_IODA_PE_BUS_ALL)
730 count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
731 else
732 count = 1;
733
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000734 switch(count) {
735 case 1: bcomp = OpalPciBusAll; break;
736 case 2: bcomp = OpalPciBus7Bits; break;
737 case 4: bcomp = OpalPciBus6Bits; break;
738 case 8: bcomp = OpalPciBus5Bits; break;
739 case 16: bcomp = OpalPciBus4Bits; break;
740 case 32: bcomp = OpalPciBus3Bits; break;
741 default:
Wei Yang781a8682015-03-25 16:23:57 +0800742 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
743 count);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000744 /* Do an exact match only */
745 bcomp = OpalPciBusAll;
746 }
747 rid_end = pe->rid + (count << 8);
748 } else {
Wei Yang781a8682015-03-25 16:23:57 +0800749#ifdef CONFIG_PCI_IOV
750 if (pe->flags & PNV_IODA_PE_VF)
751 parent = pe->parent_dev;
752 else
753#endif /* CONFIG_PCI_IOV */
754 parent = pe->pdev->bus->self;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000755 bcomp = OpalPciBusAll;
756 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
757 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
758 rid_end = pe->rid + 1;
759 }
760
Gavin Shan631ad692013-11-04 16:32:46 +0800761 /*
762 * Associate PE in PELT. We need add the PE into the
763 * corresponding PELT-V as well. Otherwise, the error
764 * originated from the PE might contribute to other
765 * PEs.
766 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000767 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
768 bcomp, dcomp, fcomp, OPAL_MAP_PE);
769 if (rc) {
770 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
771 return -ENXIO;
772 }
Gavin Shan631ad692013-11-04 16:32:46 +0800773
Alistair Popple5d2aa712015-12-17 13:43:13 +1100774 /*
775 * Configure PELTV. NPUs don't have a PELTV table so skip
776 * configuration on them.
777 */
778 if (phb->type != PNV_PHB_NPU)
779 pnv_ioda_set_peltv(phb, pe, true);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000780
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000781 /* Setup reverse map */
782 for (rid = pe->rid; rid < rid_end; rid++)
783 phb->ioda.pe_rmap[rid] = pe->pe_number;
784
785 /* Setup one MVTs on IODA1 */
Gavin Shan4773f762014-11-12 13:36:09 +1100786 if (phb->type != PNV_PHB_IODA1) {
787 pe->mve_number = 0;
788 goto out;
789 }
790
791 pe->mve_number = pe->pe_number;
792 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
793 if (rc != OPAL_SUCCESS) {
794 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
795 rc, pe->mve_number);
796 pe->mve_number = -1;
797 } else {
798 rc = opal_pci_set_mve_enable(phb->opal_id,
799 pe->mve_number, OPAL_ENABLE_MVE);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000800 if (rc) {
Gavin Shan4773f762014-11-12 13:36:09 +1100801 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000802 rc, pe->mve_number);
803 pe->mve_number = -1;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000804 }
Gavin Shan4773f762014-11-12 13:36:09 +1100805 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000806
Gavin Shan4773f762014-11-12 13:36:09 +1100807out:
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000808 return 0;
809}
810
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800811static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
812 struct pnv_ioda_pe *pe)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000813{
814 struct pnv_ioda_pe *lpe;
815
Gavin Shan7ebdf952012-08-20 03:49:15 +0000816 list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000817 if (lpe->dma_weight < pe->dma_weight) {
Gavin Shan7ebdf952012-08-20 03:49:15 +0000818 list_add_tail(&pe->dma_link, &lpe->dma_link);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000819 return;
820 }
821 }
Gavin Shan7ebdf952012-08-20 03:49:15 +0000822 list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000823}
824
825static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
826{
827 /* This is quite simplistic. The "base" weight of a device
828 * is 10. 0 means no DMA is to be accounted for it.
829 */
830
831 /* If it's a bridge, no DMA */
832 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
833 return 0;
834
835 /* Reduce the weight of slow USB controllers */
836 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
837 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
838 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
839 return 3;
840
841 /* Increase the weight of RAID (includes Obsidian) */
842 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
843 return 15;
844
845 /* Default */
846 return 10;
847}
848
Wei Yang781a8682015-03-25 16:23:57 +0800849#ifdef CONFIG_PCI_IOV
850static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
851{
852 struct pci_dn *pdn = pci_get_pdn(dev);
853 int i;
854 struct resource *res, res2;
855 resource_size_t size;
856 u16 num_vfs;
857
858 if (!dev->is_physfn)
859 return -EINVAL;
860
861 /*
862 * "offset" is in VFs. The M64 windows are sized so that when they
863 * are segmented, each segment is the same size as the IOV BAR.
864 * Each segment is in a separate PE, and the high order bits of the
865 * address are the PE number. Therefore, each VF's BAR is in a
866 * separate PE, and changing the IOV BAR start address changes the
867 * range of PEs the VFs are in.
868 */
869 num_vfs = pdn->num_vfs;
870 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
871 res = &dev->resource[i + PCI_IOV_RESOURCES];
872 if (!res->flags || !res->parent)
873 continue;
874
875 if (!pnv_pci_is_mem_pref_64(res->flags))
876 continue;
877
878 /*
879 * The actual IOV BAR range is determined by the start address
880 * and the actual size for num_vfs VFs BAR. This check is to
881 * make sure that after shifting, the range will not overlap
882 * with another device.
883 */
884 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
885 res2.flags = res->flags;
886 res2.start = res->start + (size * offset);
887 res2.end = res2.start + (size * num_vfs) - 1;
888
889 if (res2.end > res->end) {
890 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
891 i, &res2, res, num_vfs, offset);
892 return -EBUSY;
893 }
894 }
895
896 /*
897 * After doing so, there would be a "hole" in the /proc/iomem when
898 * offset is a positive value. It looks like the device return some
899 * mmio back to the system, which actually no one could use it.
900 */
901 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
902 res = &dev->resource[i + PCI_IOV_RESOURCES];
903 if (!res->flags || !res->parent)
904 continue;
905
906 if (!pnv_pci_is_mem_pref_64(res->flags))
907 continue;
908
909 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
910 res2 = *res;
911 res->start += size * offset;
912
Wei Yang74703cc2015-07-20 18:14:58 +0800913 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
914 i, &res2, res, (offset > 0) ? "En" : "Dis",
915 num_vfs, offset);
Wei Yang781a8682015-03-25 16:23:57 +0800916 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
917 }
918 return 0;
919}
920#endif /* CONFIG_PCI_IOV */
921
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800922static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000923{
924 struct pci_controller *hose = pci_bus_to_host(dev->bus);
925 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000926 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000927 struct pnv_ioda_pe *pe;
928 int pe_num;
929
930 if (!pdn) {
931 pr_err("%s: Device tree node not associated properly\n",
932 pci_name(dev));
933 return NULL;
934 }
935 if (pdn->pe_number != IODA_INVALID_PE)
936 return NULL;
937
Alistair Popple5d2aa712015-12-17 13:43:13 +1100938 pe_num = pnv_ioda_alloc_pe(phb);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000939 if (pe_num == IODA_INVALID_PE) {
940 pr_warning("%s: Not enough PE# available, disabling device\n",
941 pci_name(dev));
942 return NULL;
943 }
944
945 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
946 * pointer in the PE data structure, both should be destroyed at the
947 * same time. However, this needs to be looked at more closely again
948 * once we actually start removing things (Hotplug, SR-IOV, ...)
949 *
950 * At some point we want to remove the PDN completely anyways
951 */
952 pe = &phb->ioda.pe_array[pe_num];
953 pci_dev_get(dev);
954 pdn->pcidev = dev;
955 pdn->pe_number = pe_num;
Alistair Popple5d2aa712015-12-17 13:43:13 +1100956 pe->flags = PNV_IODA_PE_DEV;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000957 pe->pdev = dev;
958 pe->pbus = NULL;
959 pe->tce32_seg = -1;
960 pe->mve_number = -1;
961 pe->rid = dev->bus->number << 8 | pdn->devfn;
962
963 pe_info(pe, "Associated device to PE\n");
964
965 if (pnv_ioda_configure_pe(phb, pe)) {
966 /* XXX What do we do here ? */
967 if (pe_num)
968 pnv_ioda_free_pe(phb, pe_num);
969 pdn->pe_number = IODA_INVALID_PE;
970 pe->pdev = NULL;
971 pci_dev_put(dev);
972 return NULL;
973 }
974
975 /* Assign a DMA weight to the device */
976 pe->dma_weight = pnv_ioda_dma_weight(dev);
977 if (pe->dma_weight != 0) {
978 phb->ioda.dma_weight += pe->dma_weight;
979 phb->ioda.dma_pe_count++;
980 }
981
982 /* Link the PE */
983 pnv_ioda_link_pe_by_weight(phb, pe);
984
985 return pe;
986}
987
988static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
989{
990 struct pci_dev *dev;
991
992 list_for_each_entry(dev, &bus->devices, bus_list) {
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +0000993 struct pci_dn *pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000994
995 if (pdn == NULL) {
996 pr_warn("%s: No device node associated with device !\n",
997 pci_name(dev));
998 continue;
999 }
Alistair Popple94973b22015-12-17 13:43:11 +11001000 pdn->pcidev = dev;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001001 pdn->pe_number = pe->pe_number;
1002 pe->dma_weight += pnv_ioda_dma_weight(dev);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001003 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001004 pnv_ioda_setup_same_PE(dev->subordinate, pe);
1005 }
1006}
1007
Gavin Shanfb446ad2012-08-20 03:49:14 +00001008/*
1009 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
1010 * single PCI bus. Another one that contains the primary PCI bus and its
1011 * subordinate PCI devices and buses. The second type of PE is normally
1012 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
1013 */
Gavin Shand1203852015-06-19 12:26:18 +10001014static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001015{
Gavin Shanfb446ad2012-08-20 03:49:14 +00001016 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001017 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001018 struct pnv_ioda_pe *pe;
Guo Chao262af552014-07-21 14:42:30 +10001019 int pe_num = IODA_INVALID_PE;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001020
Guo Chao262af552014-07-21 14:42:30 +10001021 /* Check if PE is determined by M64 */
1022 if (phb->pick_m64_pe)
Gavin Shan26ba2482015-06-19 12:26:19 +10001023 pe_num = phb->pick_m64_pe(bus, all);
Guo Chao262af552014-07-21 14:42:30 +10001024
1025 /* The PE number isn't pinned by M64 */
1026 if (pe_num == IODA_INVALID_PE)
1027 pe_num = pnv_ioda_alloc_pe(phb);
1028
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001029 if (pe_num == IODA_INVALID_PE) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001030 pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
1031 __func__, pci_domain_nr(bus), bus->number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001032 return;
1033 }
1034
1035 pe = &phb->ioda.pe_array[pe_num];
Guo Chao262af552014-07-21 14:42:30 +10001036 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001037 pe->pbus = bus;
1038 pe->pdev = NULL;
1039 pe->tce32_seg = -1;
1040 pe->mve_number = -1;
Yinghai Lub918c622012-05-17 18:51:11 -07001041 pe->rid = bus->busn_res.start << 8;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001042 pe->dma_weight = 0;
1043
Gavin Shanfb446ad2012-08-20 03:49:14 +00001044 if (all)
1045 pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
1046 bus->busn_res.start, bus->busn_res.end, pe_num);
1047 else
1048 pe_info(pe, "Secondary bus %d associated with PE#%d\n",
1049 bus->busn_res.start, pe_num);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001050
1051 if (pnv_ioda_configure_pe(phb, pe)) {
1052 /* XXX What do we do here ? */
1053 if (pe_num)
1054 pnv_ioda_free_pe(phb, pe_num);
1055 pe->pbus = NULL;
1056 return;
1057 }
1058
1059 /* Associate it with all child devices */
1060 pnv_ioda_setup_same_PE(bus, pe);
1061
Gavin Shan7ebdf952012-08-20 03:49:15 +00001062 /* Put PE to the list */
1063 list_add_tail(&pe->list, &phb->ioda.pe_list);
1064
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001065 /* Account for one DMA PE if at least one DMA capable device exist
1066 * below the bridge
1067 */
1068 if (pe->dma_weight != 0) {
1069 phb->ioda.dma_weight += pe->dma_weight;
1070 phb->ioda.dma_pe_count++;
1071 }
1072
1073 /* Link the PE */
1074 pnv_ioda_link_pe_by_weight(phb, pe);
1075}
1076
Alistair Popple5d2aa712015-12-17 13:43:13 +11001077static void pnv_ioda_setup_dev_PEs(struct pci_bus *bus)
1078{
1079 struct pci_bus *child;
1080 struct pci_dev *pdev;
1081
1082 list_for_each_entry(pdev, &bus->devices, bus_list)
1083 pnv_ioda_setup_dev_PE(pdev);
1084
1085 list_for_each_entry(child, &bus->children, node)
1086 pnv_ioda_setup_dev_PEs(child);
1087}
1088
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001089static void pnv_ioda_setup_PEs(struct pci_bus *bus)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001090{
1091 struct pci_dev *dev;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001092
Gavin Shand1203852015-06-19 12:26:18 +10001093 pnv_ioda_setup_bus_PE(bus, false);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001094
1095 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shanfb446ad2012-08-20 03:49:14 +00001096 if (dev->subordinate) {
1097 if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
Gavin Shand1203852015-06-19 12:26:18 +10001098 pnv_ioda_setup_bus_PE(dev->subordinate, true);
Gavin Shanfb446ad2012-08-20 03:49:14 +00001099 else
1100 pnv_ioda_setup_PEs(dev->subordinate);
1101 }
1102 }
1103}
1104
1105/*
1106 * Configure PEs so that the downstream PCI buses and devices
1107 * could have their associated PE#. Unfortunately, we didn't
1108 * figure out the way to identify the PLX bridge yet. So we
1109 * simply put the PCI bus and the subordinate behind the root
1110 * port to PE# here. The game rule here is expected to be changed
1111 * as soon as we can detected PLX bridge correctly.
1112 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001113static void pnv_pci_ioda_setup_PEs(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00001114{
1115 struct pci_controller *hose, *tmp;
Guo Chao262af552014-07-21 14:42:30 +10001116 struct pnv_phb *phb;
Gavin Shanfb446ad2012-08-20 03:49:14 +00001117
1118 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Guo Chao262af552014-07-21 14:42:30 +10001119 phb = hose->private_data;
1120
1121 /* M64 layout might affect PE allocation */
Gavin Shan5ef73562014-11-12 13:36:06 +11001122 if (phb->reserve_m64_pe)
Gavin Shan96a2f922015-06-19 12:26:17 +10001123 phb->reserve_m64_pe(hose->bus, NULL, true);
Guo Chao262af552014-07-21 14:42:30 +10001124
Alistair Popple5d2aa712015-12-17 13:43:13 +11001125 /*
1126 * On NPU PHB, we expect separate PEs for individual PCI
1127 * functions. PCI bus dependent PEs are required for the
1128 * remaining types of PHBs.
1129 */
1130 if (phb->type == PNV_PHB_NPU)
1131 pnv_ioda_setup_dev_PEs(hose->bus);
1132 else
1133 pnv_ioda_setup_PEs(hose->bus);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001134 }
1135}
1136
Gavin Shana8b2f822015-03-25 16:23:52 +08001137#ifdef CONFIG_PCI_IOV
Wei Yang781a8682015-03-25 16:23:57 +08001138static int pnv_pci_vf_release_m64(struct pci_dev *pdev)
1139{
1140 struct pci_bus *bus;
1141 struct pci_controller *hose;
1142 struct pnv_phb *phb;
1143 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001144 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001145
1146 bus = pdev->bus;
1147 hose = pci_bus_to_host(bus);
1148 phb = hose->private_data;
1149 pdn = pci_get_pdn(pdev);
1150
Wei Yang02639b02015-03-25 16:23:59 +08001151 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
1152 for (j = 0; j < M64_PER_IOV; j++) {
1153 if (pdn->m64_wins[i][j] == IODA_INVALID_M64)
1154 continue;
1155 opal_pci_phb_mmio_enable(phb->opal_id,
1156 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 0);
1157 clear_bit(pdn->m64_wins[i][j], &phb->ioda.m64_bar_alloc);
1158 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1159 }
Wei Yang781a8682015-03-25 16:23:57 +08001160
1161 return 0;
1162}
1163
Wei Yang02639b02015-03-25 16:23:59 +08001164static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001165{
1166 struct pci_bus *bus;
1167 struct pci_controller *hose;
1168 struct pnv_phb *phb;
1169 struct pci_dn *pdn;
1170 unsigned int win;
1171 struct resource *res;
Wei Yang02639b02015-03-25 16:23:59 +08001172 int i, j;
Wei Yang781a8682015-03-25 16:23:57 +08001173 int64_t rc;
Wei Yang02639b02015-03-25 16:23:59 +08001174 int total_vfs;
1175 resource_size_t size, start;
1176 int pe_num;
1177 int vf_groups;
1178 int vf_per_group;
Wei Yang781a8682015-03-25 16:23:57 +08001179
1180 bus = pdev->bus;
1181 hose = pci_bus_to_host(bus);
1182 phb = hose->private_data;
1183 pdn = pci_get_pdn(pdev);
Wei Yang02639b02015-03-25 16:23:59 +08001184 total_vfs = pci_sriov_get_totalvfs(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001185
1186 /* Initialize the m64_wins to IODA_INVALID_M64 */
1187 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
Wei Yang02639b02015-03-25 16:23:59 +08001188 for (j = 0; j < M64_PER_IOV; j++)
1189 pdn->m64_wins[i][j] = IODA_INVALID_M64;
1190
1191 if (pdn->m64_per_iov == M64_PER_IOV) {
1192 vf_groups = (num_vfs <= M64_PER_IOV) ? num_vfs: M64_PER_IOV;
1193 vf_per_group = (num_vfs <= M64_PER_IOV)? 1:
1194 roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1195 } else {
1196 vf_groups = 1;
1197 vf_per_group = 1;
1198 }
Wei Yang781a8682015-03-25 16:23:57 +08001199
1200 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1201 res = &pdev->resource[i + PCI_IOV_RESOURCES];
1202 if (!res->flags || !res->parent)
1203 continue;
1204
1205 if (!pnv_pci_is_mem_pref_64(res->flags))
1206 continue;
1207
Wei Yang02639b02015-03-25 16:23:59 +08001208 for (j = 0; j < vf_groups; j++) {
1209 do {
1210 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
1211 phb->ioda.m64_bar_idx + 1, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001212
Wei Yang02639b02015-03-25 16:23:59 +08001213 if (win >= phb->ioda.m64_bar_idx + 1)
1214 goto m64_failed;
1215 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
Wei Yang781a8682015-03-25 16:23:57 +08001216
Wei Yang02639b02015-03-25 16:23:59 +08001217 pdn->m64_wins[i][j] = win;
Wei Yang781a8682015-03-25 16:23:57 +08001218
Wei Yang02639b02015-03-25 16:23:59 +08001219 if (pdn->m64_per_iov == M64_PER_IOV) {
1220 size = pci_iov_resource_size(pdev,
1221 PCI_IOV_RESOURCES + i);
1222 size = size * vf_per_group;
1223 start = res->start + size * j;
1224 } else {
1225 size = resource_size(res);
1226 start = res->start;
1227 }
1228
1229 /* Map the M64 here */
1230 if (pdn->m64_per_iov == M64_PER_IOV) {
1231 pe_num = pdn->offset + j;
1232 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1233 pe_num, OPAL_M64_WINDOW_TYPE,
1234 pdn->m64_wins[i][j], 0);
1235 }
1236
1237 rc = opal_pci_set_phb_mem_window(phb->opal_id,
Wei Yang781a8682015-03-25 16:23:57 +08001238 OPAL_M64_WINDOW_TYPE,
Wei Yang02639b02015-03-25 16:23:59 +08001239 pdn->m64_wins[i][j],
1240 start,
Wei Yang781a8682015-03-25 16:23:57 +08001241 0, /* unused */
Wei Yang02639b02015-03-25 16:23:59 +08001242 size);
Wei Yang781a8682015-03-25 16:23:57 +08001243
Wei Yang02639b02015-03-25 16:23:59 +08001244
1245 if (rc != OPAL_SUCCESS) {
1246 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
1247 win, rc);
1248 goto m64_failed;
1249 }
1250
1251 if (pdn->m64_per_iov == M64_PER_IOV)
1252 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1253 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 2);
1254 else
1255 rc = opal_pci_phb_mmio_enable(phb->opal_id,
1256 OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 1);
1257
1258 if (rc != OPAL_SUCCESS) {
1259 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
1260 win, rc);
1261 goto m64_failed;
1262 }
Wei Yang781a8682015-03-25 16:23:57 +08001263 }
1264 }
1265 return 0;
1266
1267m64_failed:
1268 pnv_pci_vf_release_m64(pdev);
1269 return -EBUSY;
1270}
1271
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001272static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1273 int num);
1274static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
1275
Wei Yang781a8682015-03-25 16:23:57 +08001276static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
1277{
Wei Yang781a8682015-03-25 16:23:57 +08001278 struct iommu_table *tbl;
Wei Yang781a8682015-03-25 16:23:57 +08001279 int64_t rc;
1280
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001281 tbl = pe->table_group.tables[0];
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001282 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
Wei Yang781a8682015-03-25 16:23:57 +08001283 if (rc)
1284 pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
1285
Alexey Kardashevskiyc035e372015-06-05 16:35:21 +10001286 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001287 if (pe->table_group.group) {
1288 iommu_group_put(pe->table_group.group);
1289 BUG_ON(pe->table_group.group);
Alexey Kardashevskiyac9a5882015-06-05 16:34:56 +10001290 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10001291 pnv_pci_ioda2_table_free_pages(tbl);
Wei Yang781a8682015-03-25 16:23:57 +08001292 iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
Wei Yang781a8682015-03-25 16:23:57 +08001293}
1294
Wei Yang02639b02015-03-25 16:23:59 +08001295static void pnv_ioda_release_vf_PE(struct pci_dev *pdev, u16 num_vfs)
Wei Yang781a8682015-03-25 16:23:57 +08001296{
1297 struct pci_bus *bus;
1298 struct pci_controller *hose;
1299 struct pnv_phb *phb;
1300 struct pnv_ioda_pe *pe, *pe_n;
1301 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001302 u16 vf_index;
1303 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001304
1305 bus = pdev->bus;
1306 hose = pci_bus_to_host(bus);
1307 phb = hose->private_data;
Wei Yang02639b02015-03-25 16:23:59 +08001308 pdn = pci_get_pdn(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001309
1310 if (!pdev->is_physfn)
1311 return;
1312
Wei Yang02639b02015-03-25 16:23:59 +08001313 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1314 int vf_group;
1315 int vf_per_group;
1316 int vf_index1;
1317
1318 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1319
1320 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++)
1321 for (vf_index = vf_group * vf_per_group;
1322 vf_index < (vf_group + 1) * vf_per_group &&
1323 vf_index < num_vfs;
1324 vf_index++)
1325 for (vf_index1 = vf_group * vf_per_group;
1326 vf_index1 < (vf_group + 1) * vf_per_group &&
1327 vf_index1 < num_vfs;
1328 vf_index1++){
1329
1330 rc = opal_pci_set_peltv(phb->opal_id,
1331 pdn->offset + vf_index,
1332 pdn->offset + vf_index1,
1333 OPAL_REMOVE_PE_FROM_DOMAIN);
1334
1335 if (rc)
1336 dev_warn(&pdev->dev, "%s: Failed to unlink same group PE#%d(%lld)\n",
1337 __func__,
1338 pdn->offset + vf_index1, rc);
1339 }
1340 }
1341
Wei Yang781a8682015-03-25 16:23:57 +08001342 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
1343 if (pe->parent_dev != pdev)
1344 continue;
1345
1346 pnv_pci_ioda2_release_dma_pe(pdev, pe);
1347
1348 /* Remove from list */
1349 mutex_lock(&phb->ioda.pe_list_mutex);
1350 list_del(&pe->list);
1351 mutex_unlock(&phb->ioda.pe_list_mutex);
1352
1353 pnv_ioda_deconfigure_pe(phb, pe);
1354
1355 pnv_ioda_free_pe(phb, pe->pe_number);
1356 }
1357}
1358
1359void pnv_pci_sriov_disable(struct pci_dev *pdev)
1360{
1361 struct pci_bus *bus;
1362 struct pci_controller *hose;
1363 struct pnv_phb *phb;
1364 struct pci_dn *pdn;
1365 struct pci_sriov *iov;
1366 u16 num_vfs;
1367
1368 bus = pdev->bus;
1369 hose = pci_bus_to_host(bus);
1370 phb = hose->private_data;
1371 pdn = pci_get_pdn(pdev);
1372 iov = pdev->sriov;
1373 num_vfs = pdn->num_vfs;
1374
1375 /* Release VF PEs */
Wei Yang02639b02015-03-25 16:23:59 +08001376 pnv_ioda_release_vf_PE(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001377
1378 if (phb->type == PNV_PHB_IODA2) {
Wei Yang02639b02015-03-25 16:23:59 +08001379 if (pdn->m64_per_iov == 1)
1380 pnv_pci_vf_resource_shift(pdev, -pdn->offset);
Wei Yang781a8682015-03-25 16:23:57 +08001381
1382 /* Release M64 windows */
1383 pnv_pci_vf_release_m64(pdev);
1384
1385 /* Release PE numbers */
1386 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1387 pdn->offset = 0;
1388 }
1389}
1390
1391static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1392 struct pnv_ioda_pe *pe);
1393static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
1394{
1395 struct pci_bus *bus;
1396 struct pci_controller *hose;
1397 struct pnv_phb *phb;
1398 struct pnv_ioda_pe *pe;
1399 int pe_num;
1400 u16 vf_index;
1401 struct pci_dn *pdn;
Wei Yang02639b02015-03-25 16:23:59 +08001402 int64_t rc;
Wei Yang781a8682015-03-25 16:23:57 +08001403
1404 bus = pdev->bus;
1405 hose = pci_bus_to_host(bus);
1406 phb = hose->private_data;
1407 pdn = pci_get_pdn(pdev);
1408
1409 if (!pdev->is_physfn)
1410 return;
1411
1412 /* Reserve PE for each VF */
1413 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
1414 pe_num = pdn->offset + vf_index;
1415
1416 pe = &phb->ioda.pe_array[pe_num];
1417 pe->pe_number = pe_num;
1418 pe->phb = phb;
1419 pe->flags = PNV_IODA_PE_VF;
1420 pe->pbus = NULL;
1421 pe->parent_dev = pdev;
1422 pe->tce32_seg = -1;
1423 pe->mve_number = -1;
1424 pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
1425 pci_iov_virtfn_devfn(pdev, vf_index);
1426
1427 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
1428 hose->global_number, pdev->bus->number,
1429 PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
1430 PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
1431
1432 if (pnv_ioda_configure_pe(phb, pe)) {
1433 /* XXX What do we do here ? */
1434 if (pe_num)
1435 pnv_ioda_free_pe(phb, pe_num);
1436 pe->pdev = NULL;
1437 continue;
1438 }
1439
Wei Yang781a8682015-03-25 16:23:57 +08001440 /* Put PE to the list */
1441 mutex_lock(&phb->ioda.pe_list_mutex);
1442 list_add_tail(&pe->list, &phb->ioda.pe_list);
1443 mutex_unlock(&phb->ioda.pe_list_mutex);
1444
1445 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1446 }
Wei Yang02639b02015-03-25 16:23:59 +08001447
1448 if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
1449 int vf_group;
1450 int vf_per_group;
1451 int vf_index1;
1452
1453 vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
1454
1455 for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++) {
1456 for (vf_index = vf_group * vf_per_group;
1457 vf_index < (vf_group + 1) * vf_per_group &&
1458 vf_index < num_vfs;
1459 vf_index++) {
1460 for (vf_index1 = vf_group * vf_per_group;
1461 vf_index1 < (vf_group + 1) * vf_per_group &&
1462 vf_index1 < num_vfs;
1463 vf_index1++) {
1464
1465 rc = opal_pci_set_peltv(phb->opal_id,
1466 pdn->offset + vf_index,
1467 pdn->offset + vf_index1,
1468 OPAL_ADD_PE_TO_DOMAIN);
1469
1470 if (rc)
1471 dev_warn(&pdev->dev, "%s: Failed to link same group PE#%d(%lld)\n",
1472 __func__,
1473 pdn->offset + vf_index1, rc);
1474 }
1475 }
1476 }
1477 }
Wei Yang781a8682015-03-25 16:23:57 +08001478}
1479
1480int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1481{
1482 struct pci_bus *bus;
1483 struct pci_controller *hose;
1484 struct pnv_phb *phb;
1485 struct pci_dn *pdn;
1486 int ret;
1487
1488 bus = pdev->bus;
1489 hose = pci_bus_to_host(bus);
1490 phb = hose->private_data;
1491 pdn = pci_get_pdn(pdev);
1492
1493 if (phb->type == PNV_PHB_IODA2) {
1494 /* Calculate available PE for required VFs */
1495 mutex_lock(&phb->ioda.pe_alloc_mutex);
1496 pdn->offset = bitmap_find_next_zero_area(
1497 phb->ioda.pe_alloc, phb->ioda.total_pe,
1498 0, num_vfs, 0);
1499 if (pdn->offset >= phb->ioda.total_pe) {
1500 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1501 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
1502 pdn->offset = 0;
1503 return -EBUSY;
1504 }
1505 bitmap_set(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1506 pdn->num_vfs = num_vfs;
1507 mutex_unlock(&phb->ioda.pe_alloc_mutex);
1508
1509 /* Assign M64 window accordingly */
Wei Yang02639b02015-03-25 16:23:59 +08001510 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
Wei Yang781a8682015-03-25 16:23:57 +08001511 if (ret) {
1512 dev_info(&pdev->dev, "Not enough M64 window resources\n");
1513 goto m64_failed;
1514 }
1515
1516 /*
1517 * When using one M64 BAR to map one IOV BAR, we need to shift
1518 * the IOV BAR according to the PE# allocated to the VFs.
1519 * Otherwise, the PE# for the VF will conflict with others.
1520 */
Wei Yang02639b02015-03-25 16:23:59 +08001521 if (pdn->m64_per_iov == 1) {
1522 ret = pnv_pci_vf_resource_shift(pdev, pdn->offset);
1523 if (ret)
1524 goto m64_failed;
1525 }
Wei Yang781a8682015-03-25 16:23:57 +08001526 }
1527
1528 /* Setup VF PEs */
1529 pnv_ioda_setup_vf_PE(pdev, num_vfs);
1530
1531 return 0;
1532
1533m64_failed:
1534 bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
1535 pdn->offset = 0;
1536
1537 return ret;
1538}
1539
Gavin Shana8b2f822015-03-25 16:23:52 +08001540int pcibios_sriov_disable(struct pci_dev *pdev)
1541{
Wei Yang781a8682015-03-25 16:23:57 +08001542 pnv_pci_sriov_disable(pdev);
1543
Gavin Shana8b2f822015-03-25 16:23:52 +08001544 /* Release PCI data */
1545 remove_dev_pci_data(pdev);
1546 return 0;
1547}
1548
1549int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
1550{
1551 /* Allocate PCI data */
1552 add_dev_pci_data(pdev);
Wei Yang781a8682015-03-25 16:23:57 +08001553
1554 pnv_pci_sriov_enable(pdev, num_vfs);
Gavin Shana8b2f822015-03-25 16:23:52 +08001555 return 0;
1556}
1557#endif /* CONFIG_PCI_IOV */
1558
Gavin Shan959c9bd2013-04-25 19:21:02 +00001559static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001560{
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00001561 struct pci_dn *pdn = pci_get_pdn(pdev);
Gavin Shan959c9bd2013-04-25 19:21:02 +00001562 struct pnv_ioda_pe *pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001563
Gavin Shan959c9bd2013-04-25 19:21:02 +00001564 /*
1565 * The function can be called while the PE#
1566 * hasn't been assigned. Do nothing for the
1567 * case.
1568 */
1569 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1570 return;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001571
Gavin Shan959c9bd2013-04-25 19:21:02 +00001572 pe = &phb->ioda.pe_array[pdn->pe_number];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001573 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
Alexey Kardashevskiy0e1ffef2015-08-27 16:01:16 +10001574 set_dma_offset(&pdev->dev, pe->tce_bypass_base);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001575 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001576 /*
1577 * Note: iommu_add_device() will fail here as
1578 * for physical PE: the device is already added by now;
1579 * for virtual PE: sysfs entries are not ready yet and
1580 * tce_iommu_bus_notifier will add the device to a group later.
1581 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001582}
1583
Daniel Axtens763d2d82015-04-28 15:12:07 +10001584static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001585{
Daniel Axtens763d2d82015-04-28 15:12:07 +10001586 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1587 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001588 struct pci_dn *pdn = pci_get_pdn(pdev);
1589 struct pnv_ioda_pe *pe;
1590 uint64_t top;
1591 bool bypass = false;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001592 struct pci_dev *linked_npu_dev;
1593 int i;
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001594
1595 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1596 return -ENODEV;;
1597
1598 pe = &phb->ioda.pe_array[pdn->pe_number];
1599 if (pe->tce_bypass_enabled) {
1600 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1601 bypass = (dma_mask >= top);
1602 }
1603
1604 if (bypass) {
1605 dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
1606 set_dma_ops(&pdev->dev, &dma_direct_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001607 } else {
1608 dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
1609 set_dma_ops(&pdev->dev, &dma_iommu_ops);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001610 }
Brian W Harta32305b2014-07-31 14:24:37 -05001611 *pdev->dev.dma_mask = dma_mask;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001612
1613 /* Update peer npu devices */
1614 if (pe->flags & PNV_IODA_PE_PEER)
1615 for (i = 0; pe->peers[i]; i++) {
1616 linked_npu_dev = pe->peers[i]->pdev;
1617 if (dma_get_mask(&linked_npu_dev->dev) != dma_mask)
1618 dma_set_mask(&linked_npu_dev->dev, dma_mask);
1619 }
1620
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11001621 return 0;
1622}
1623
Andrew Donnellan535229822015-08-07 13:45:54 +10001624static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001625{
Andrew Donnellan535229822015-08-07 13:45:54 +10001626 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
1627 struct pnv_phb *phb = hose->private_data;
Gavin Shanfe7e85c2014-09-30 12:39:10 +10001628 struct pci_dn *pdn = pci_get_pdn(pdev);
1629 struct pnv_ioda_pe *pe;
1630 u64 end, mask;
1631
1632 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1633 return 0;
1634
1635 pe = &phb->ioda.pe_array[pdn->pe_number];
1636 if (!pe->tce_bypass_enabled)
1637 return __dma_get_required_mask(&pdev->dev);
1638
1639
1640 end = pe->tce_bypass_base + memblock_end_of_DRAM();
1641 mask = 1ULL << (fls64(end) - 1);
1642 mask += mask - 1;
1643
1644 return mask;
1645}
1646
Gavin Shandff4a392014-07-15 17:00:55 +10001647static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001648 struct pci_bus *bus)
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001649{
1650 struct pci_dev *dev;
1651
1652 list_for_each_entry(dev, &bus->devices, bus_list) {
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001653 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
Benjamin Herrenschmidte91c25112015-06-24 15:25:27 +10001654 set_dma_offset(&dev->dev, pe->tce_bypass_base);
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001655 iommu_add_device(&dev->dev);
Gavin Shandff4a392014-07-15 17:00:55 +10001656
Alexey Kardashevskiy5c89a872015-06-18 11:41:36 +10001657 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001658 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001659 }
1660}
1661
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001662static void pnv_pci_ioda1_tce_invalidate(struct iommu_table *tbl,
1663 unsigned long index, unsigned long npages, bool rm)
Gavin Shan4cce9552013-04-25 19:21:00 +00001664{
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001665 struct iommu_table_group_link *tgl = list_first_entry_or_null(
1666 &tbl->it_group_list, struct iommu_table_group_link,
1667 next);
1668 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001669 struct pnv_ioda_pe, table_group);
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001670 __be64 __iomem *invalidate = rm ?
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001671 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1672 pe->phb->ioda.tce_inval_reg;
Gavin Shan4cce9552013-04-25 19:21:00 +00001673 unsigned long start, end, inc;
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001674 const unsigned shift = tbl->it_page_shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001675
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001676 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
1677 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
1678 npages - 1);
Gavin Shan4cce9552013-04-25 19:21:00 +00001679
1680 /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
1681 if (tbl->it_busno) {
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001682 start <<= shift;
1683 end <<= shift;
1684 inc = 128ull << shift;
Gavin Shan4cce9552013-04-25 19:21:00 +00001685 start |= tbl->it_busno;
1686 end |= tbl->it_busno;
1687 } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
1688 /* p7ioc-style invalidation, 2 TCEs per write */
1689 start |= (1ull << 63);
1690 end |= (1ull << 63);
1691 inc = 16;
1692 } else {
1693 /* Default (older HW) */
1694 inc = 128;
1695 }
1696
1697 end |= inc - 1; /* round up end to be different than start */
1698
1699 mb(); /* Ensure above stores are visible */
1700 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001701 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001702 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001703 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001704 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001705 start += inc;
1706 }
1707
1708 /*
1709 * The iommu layer will do another mb() for us on build()
1710 * and we don't care on free()
1711 */
1712}
1713
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001714static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
1715 long npages, unsigned long uaddr,
1716 enum dma_data_direction direction,
1717 struct dma_attrs *attrs)
1718{
1719 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1720 attrs);
1721
1722 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1723 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1724
1725 return ret;
1726}
1727
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001728#ifdef CONFIG_IOMMU_API
1729static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
1730 unsigned long *hpa, enum dma_data_direction *direction)
1731{
1732 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1733
1734 if (!ret && (tbl->it_type &
1735 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1736 pnv_pci_ioda1_tce_invalidate(tbl, index, 1, false);
1737
1738 return ret;
1739}
1740#endif
1741
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001742static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
1743 long npages)
1744{
1745 pnv_tce_free(tbl, index, npages);
1746
1747 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1748 pnv_pci_ioda1_tce_invalidate(tbl, index, npages, false);
1749}
1750
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001751static struct iommu_table_ops pnv_ioda1_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001752 .set = pnv_ioda1_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001753#ifdef CONFIG_IOMMU_API
1754 .exchange = pnv_ioda1_tce_xchg,
1755#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001756 .clear = pnv_ioda1_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001757 .get = pnv_tce_get,
1758};
1759
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001760static inline void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_ioda_pe *pe)
1761{
1762 /* 01xb - invalidate TCEs that match the specified PE# */
1763 unsigned long val = (0x4ull << 60) | (pe->pe_number & 0xFF);
1764 struct pnv_phb *phb = pe->phb;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001765 struct pnv_ioda_pe *npe;
1766 int i;
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001767
1768 if (!phb->ioda.tce_inval_reg)
1769 return;
1770
1771 mb(); /* Ensure above stores are visible */
1772 __raw_writeq(cpu_to_be64(val), phb->ioda.tce_inval_reg);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001773
1774 if (pe->flags & PNV_IODA_PE_PEER)
1775 for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
1776 npe = pe->peers[i];
1777 if (!npe || npe->phb->type != PNV_PHB_NPU)
1778 continue;
1779
1780 pnv_npu_tce_invalidate_entire(npe);
1781 }
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001782}
1783
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001784static void pnv_pci_ioda2_do_tce_invalidate(unsigned pe_number, bool rm,
1785 __be64 __iomem *invalidate, unsigned shift,
1786 unsigned long index, unsigned long npages)
Gavin Shan4cce9552013-04-25 19:21:00 +00001787{
1788 unsigned long start, end, inc;
Gavin Shan4cce9552013-04-25 19:21:00 +00001789
1790 /* We'll invalidate DMA address in PE scope */
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001791 start = 0x2ull << 60;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001792 start |= (pe_number & 0xFF);
Gavin Shan4cce9552013-04-25 19:21:00 +00001793 end = start;
1794
1795 /* Figure out the start, end and step */
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001796 start |= (index << shift);
1797 end |= ((index + npages - 1) << shift);
Alexey Kardashevskiyb0376c92014-06-06 18:44:01 +10001798 inc = (0x1ull << shift);
Gavin Shan4cce9552013-04-25 19:21:00 +00001799 mb();
1800
1801 while (start <= end) {
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001802 if (rm)
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001803 __raw_rm_writeq(cpu_to_be64(start), invalidate);
Alexey Kardashevskiy8e0a1612013-08-28 18:37:43 +10001804 else
Benjamin Herrenschmidt3ad26e52013-10-11 18:23:53 +11001805 __raw_writeq(cpu_to_be64(start), invalidate);
Gavin Shan4cce9552013-04-25 19:21:00 +00001806 start += inc;
1807 }
1808}
1809
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001810static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1811 unsigned long index, unsigned long npages, bool rm)
1812{
1813 struct iommu_table_group_link *tgl;
1814
1815 list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
Alistair Popple5d2aa712015-12-17 13:43:13 +11001816 struct pnv_ioda_pe *npe;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001817 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1818 struct pnv_ioda_pe, table_group);
1819 __be64 __iomem *invalidate = rm ?
1820 (__be64 __iomem *)pe->phb->ioda.tce_inval_reg_phys :
1821 pe->phb->ioda.tce_inval_reg;
Alistair Popple5d2aa712015-12-17 13:43:13 +11001822 int i;
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001823
1824 pnv_pci_ioda2_do_tce_invalidate(pe->pe_number, rm,
1825 invalidate, tbl->it_page_shift,
1826 index, npages);
Alistair Popple5d2aa712015-12-17 13:43:13 +11001827
1828 if (pe->flags & PNV_IODA_PE_PEER)
1829 /* Invalidate PEs using the same TCE table */
1830 for (i = 0; i < PNV_IODA_MAX_PEER_PES; i++) {
1831 npe = pe->peers[i];
1832 if (!npe || npe->phb->type != PNV_PHB_NPU)
1833 continue;
1834
1835 pnv_npu_tce_invalidate(npe, tbl, index,
1836 npages, rm);
1837 }
Alexey Kardashevskiye57080f2015-06-05 16:35:13 +10001838 }
1839}
1840
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001841static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1842 long npages, unsigned long uaddr,
1843 enum dma_data_direction direction,
1844 struct dma_attrs *attrs)
Gavin Shan4cce9552013-04-25 19:21:00 +00001845{
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001846 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1847 attrs);
Gavin Shan4cce9552013-04-25 19:21:00 +00001848
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001849 if (!ret && (tbl->it_type & TCE_PCI_SWINV_CREATE))
1850 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
1851
1852 return ret;
1853}
1854
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001855#ifdef CONFIG_IOMMU_API
1856static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
1857 unsigned long *hpa, enum dma_data_direction *direction)
1858{
1859 long ret = pnv_tce_xchg(tbl, index, hpa, direction);
1860
1861 if (!ret && (tbl->it_type &
1862 (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE)))
1863 pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
1864
1865 return ret;
1866}
1867#endif
1868
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001869static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1870 long npages)
1871{
1872 pnv_tce_free(tbl, index, npages);
1873
1874 if (tbl->it_type & TCE_PCI_SWINV_FREE)
1875 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
Gavin Shan4cce9552013-04-25 19:21:00 +00001876}
1877
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001878static void pnv_ioda2_table_free(struct iommu_table *tbl)
1879{
1880 pnv_pci_ioda2_table_free_pages(tbl);
1881 iommu_free_table(tbl, "pnv");
1882}
1883
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001884static struct iommu_table_ops pnv_ioda2_iommu_ops = {
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001885 .set = pnv_ioda2_tce_build,
Alexey Kardashevskiy05c6cfb2015-06-05 16:35:15 +10001886#ifdef CONFIG_IOMMU_API
1887 .exchange = pnv_ioda2_tce_xchg,
1888#endif
Alexey Kardashevskiydecbda22015-06-05 16:35:07 +10001889 .clear = pnv_ioda2_tce_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001890 .get = pnv_tce_get,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001891 .free = pnv_ioda2_table_free,
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001892};
1893
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08001894static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
1895 struct pnv_ioda_pe *pe, unsigned int base,
1896 unsigned int segs)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001897{
1898
1899 struct page *tce_mem = NULL;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001900 struct iommu_table *tbl;
1901 unsigned int i;
1902 int64_t rc;
1903 void *addr;
1904
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001905 /* XXX FIXME: Handle 64-bit only DMA devices */
1906 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
1907 /* XXX FIXME: Allocate multi-level tables on PHB3 */
1908
1909 /* We shouldn't already have a 32-bit DMA associated */
1910 if (WARN_ON(pe->tce32_seg >= 0))
1911 return;
1912
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001913 tbl = pnv_pci_table_alloc(phb->hose->node);
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10001914 iommu_register_group(&pe->table_group, phb->hose->global_number,
1915 pe->pe_number);
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001916 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001917
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001918 /* Grab a 32-bit TCE table */
1919 pe->tce32_seg = base;
1920 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
1921 (base << 28), ((base + segs) << 28) - 1);
1922
1923 /* XXX Currently, we allocate one big contiguous table for the
1924 * TCEs. We only really need one chunk per 256M of TCE space
1925 * (ie per segment) but that's an optimization for later, it
1926 * requires some added smarts with our get/put_tce implementation
1927 */
1928 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
1929 get_order(TCE32_TABLE_SIZE * segs));
1930 if (!tce_mem) {
1931 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
1932 goto fail;
1933 }
1934 addr = page_address(tce_mem);
1935 memset(addr, 0, TCE32_TABLE_SIZE * segs);
1936
1937 /* Configure HW */
1938 for (i = 0; i < segs; i++) {
1939 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1940 pe->pe_number,
1941 base + i, 1,
1942 __pa(addr) + TCE32_TABLE_SIZE * i,
1943 TCE32_TABLE_SIZE, 0x1000);
1944 if (rc) {
1945 pe_err(pe, " Failed to configure 32-bit TCE table,"
1946 " err %ld\n", rc);
1947 goto fail;
1948 }
1949 }
1950
1951 /* Setup linux iommu table */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001952 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
Alexey Kardashevskiy8fa5d452014-06-06 18:44:03 +10001953 base << 28, IOMMU_PAGE_SHIFT_4K);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001954
1955 /* OPAL variant of P7IOC SW invalidated TCEs */
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001956 if (phb->ioda.tce_inval_reg)
Gavin Shan65fd7662014-04-24 18:00:28 +10001957 tbl->it_type |= (TCE_PCI_SWINV_CREATE |
1958 TCE_PCI_SWINV_FREE |
1959 TCE_PCI_SWINV_PAIR);
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10001960
Alexey Kardashevskiyda004c32015-06-05 16:35:06 +10001961 tbl->it_ops = &pnv_ioda1_iommu_ops;
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10001962 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
1963 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001964 iommu_init_table(tbl, phb->hose->node);
1965
Wei Yang781a8682015-03-25 16:23:57 +08001966 if (pe->flags & PNV_IODA_PE_DEV) {
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10001967 /*
1968 * Setting table base here only for carrying iommu_group
1969 * further down to let iommu_add_device() do the job.
1970 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
1971 */
1972 set_iommu_table_base(&pe->pdev->dev, tbl);
1973 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10001974 } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001975 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Benjamin Herrenschmidt74251fe2013-07-01 17:54:09 +10001976
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001977 return;
1978 fail:
1979 /* XXX Failure: Try to fallback to 64-bit only ? */
1980 if (pe->tce32_seg >= 0)
1981 pe->tce32_seg = -1;
1982 if (tce_mem)
1983 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
Alexey Kardashevskiy0eaf4de2015-06-05 16:35:09 +10001984 if (tbl) {
1985 pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
1986 iommu_free_table(tbl, "pnv");
1987 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00001988}
1989
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001990static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
1991 int num, struct iommu_table *tbl)
1992{
1993 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1994 table_group);
1995 struct pnv_phb *phb = pe->phb;
1996 int64_t rc;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10001997 const unsigned long size = tbl->it_indirect_levels ?
1998 tbl->it_level_size : tbl->it_size;
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10001999 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
2000 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
2001
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002002 pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002003 start_addr, start_addr + win_size - 1,
2004 IOMMU_PAGE_SIZE(tbl));
2005
2006 /*
2007 * Map TCE table through TVT. The TVE index is the PE number
2008 * shifted by 1 bit for 32-bits DMA space.
2009 */
2010 rc = opal_pci_map_pe_dma_window(phb->opal_id,
2011 pe->pe_number,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002012 (pe->pe_number << 1) + num,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002013 tbl->it_indirect_levels + 1,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002014 __pa(tbl->it_base),
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002015 size << 3,
Alexey Kardashevskiy43cb60a2015-06-05 16:35:18 +10002016 IOMMU_PAGE_SIZE(tbl));
2017 if (rc) {
2018 pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
2019 return rc;
2020 }
2021
2022 pnv_pci_link_table_and_group(phb->hose->node, num,
2023 tbl, &pe->table_group);
2024 pnv_pci_ioda2_tce_invalidate_entire(pe);
2025
2026 return 0;
2027}
2028
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002029static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002030{
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002031 uint16_t window_id = (pe->pe_number << 1 ) + 1;
2032 int64_t rc;
2033
2034 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
2035 if (enable) {
2036 phys_addr_t top = memblock_end_of_DRAM();
2037
2038 top = roundup_pow_of_two(top);
2039 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2040 pe->pe_number,
2041 window_id,
2042 pe->tce_bypass_base,
2043 top);
2044 } else {
2045 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
2046 pe->pe_number,
2047 window_id,
2048 pe->tce_bypass_base,
2049 0);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002050 }
2051 if (rc)
2052 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
2053 else
2054 pe->tce_bypass_enabled = enable;
2055}
2056
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002057static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
2058 __u32 page_shift, __u64 window_size, __u32 levels,
2059 struct iommu_table *tbl);
2060
2061static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
2062 int num, __u32 page_shift, __u64 window_size, __u32 levels,
2063 struct iommu_table **ptbl)
2064{
2065 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2066 table_group);
2067 int nid = pe->phb->hose->node;
2068 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
2069 long ret;
2070 struct iommu_table *tbl;
2071
2072 tbl = pnv_pci_table_alloc(nid);
2073 if (!tbl)
2074 return -ENOMEM;
2075
2076 ret = pnv_pci_ioda2_table_alloc_pages(nid,
2077 bus_offset, page_shift, window_size,
2078 levels, tbl);
2079 if (ret) {
2080 iommu_free_table(tbl, "pnv");
2081 return ret;
2082 }
2083
2084 tbl->it_ops = &pnv_ioda2_iommu_ops;
2085 if (pe->phb->ioda.tce_inval_reg)
2086 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2087
2088 *ptbl = tbl;
2089
2090 return 0;
2091}
2092
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002093static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
2094{
2095 struct iommu_table *tbl = NULL;
2096 long rc;
2097
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002098 /*
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002099 * crashkernel= specifies the kdump kernel's maximum memory at
2100 * some offset and there is no guaranteed the result is a power
2101 * of 2, which will cause errors later.
2102 */
2103 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
2104
2105 /*
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002106 * In memory constrained environments, e.g. kdump kernel, the
2107 * DMA window can be larger than available memory, which will
2108 * cause errors later.
2109 */
Nishanth Aravamudanfa144862015-09-04 11:22:52 -07002110 const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002111
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002112 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
2113 IOMMU_PAGE_SHIFT_4K,
Nishanth Aravamudanbb005452015-09-02 08:39:28 -07002114 window_size,
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002115 POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
2116 if (rc) {
2117 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
2118 rc);
2119 return rc;
2120 }
2121
2122 iommu_init_table(tbl, pe->phb->hose->node);
2123
2124 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
2125 if (rc) {
2126 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
2127 rc);
2128 pnv_ioda2_table_free(tbl);
2129 return rc;
2130 }
2131
2132 if (!pnv_iommu_bypass_disabled)
2133 pnv_pci_ioda2_set_bypass(pe, true);
2134
2135 /* OPAL variant of PHB3 invalidated TCEs */
2136 if (pe->phb->ioda.tce_inval_reg)
2137 tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
2138
2139 /*
2140 * Setting table base here only for carrying iommu_group
2141 * further down to let iommu_add_device() do the job.
2142 * pnv_pci_ioda_dma_dev_setup will override it later anyway.
2143 */
2144 if (pe->flags & PNV_IODA_PE_DEV)
2145 set_iommu_table_base(&pe->pdev->dev, tbl);
2146
2147 return 0;
2148}
2149
Alexey Kardashevskiyb5926432015-06-15 17:49:59 +10002150#if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
2151static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
2152 int num)
2153{
2154 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2155 table_group);
2156 struct pnv_phb *phb = pe->phb;
2157 long ret;
2158
2159 pe_info(pe, "Removing DMA window #%d\n", num);
2160
2161 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
2162 (pe->pe_number << 1) + num,
2163 0/* levels */, 0/* table address */,
2164 0/* table size */, 0/* page size */);
2165 if (ret)
2166 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
2167 else
2168 pnv_pci_ioda2_tce_invalidate_entire(pe);
2169
2170 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
2171
2172 return ret;
2173}
2174#endif
2175
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002176#ifdef CONFIG_IOMMU_API
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002177static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2178 __u64 window_size, __u32 levels)
2179{
2180 unsigned long bytes = 0;
2181 const unsigned window_shift = ilog2(window_size);
2182 unsigned entries_shift = window_shift - page_shift;
2183 unsigned table_shift = entries_shift + 3;
2184 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
2185 unsigned long direct_table_size;
2186
2187 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
2188 (window_size > memory_hotplug_max()) ||
2189 !is_power_of_2(window_size))
2190 return 0;
2191
2192 /* Calculate a direct table size from window_size and levels */
2193 entries_shift = (entries_shift + levels - 1) / levels;
2194 table_shift = entries_shift + 3;
2195 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
2196 direct_table_size = 1UL << table_shift;
2197
2198 for ( ; levels; --levels) {
2199 bytes += _ALIGN_UP(tce_table_size, direct_table_size);
2200
2201 tce_table_size /= direct_table_size;
2202 tce_table_size <<= 3;
2203 tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
2204 }
2205
2206 return bytes;
2207}
2208
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002209static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002210{
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002211 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2212 table_group);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002213 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
2214 struct iommu_table *tbl = pe->table_group.tables[0];
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002215
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002216 pnv_pci_ioda2_set_bypass(pe, false);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002217 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2218 pnv_ioda2_table_free(tbl);
Benjamin Herrenschmidtcd15b042014-02-11 11:32:38 +11002219}
2220
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002221static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
2222{
2223 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
2224 table_group);
2225
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002226 pnv_pci_ioda2_setup_default_config(pe);
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002227}
2228
2229static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
Alexey Kardashevskiy00547192015-06-05 16:35:22 +10002230 .get_table_size = pnv_pci_ioda2_get_table_size,
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002231 .create_table = pnv_pci_ioda2_create_table,
2232 .set_window = pnv_pci_ioda2_set_window,
2233 .unset_window = pnv_pci_ioda2_unset_window,
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002234 .take_ownership = pnv_ioda2_take_ownership,
2235 .release_ownership = pnv_ioda2_release_ownership,
2236};
2237#endif
2238
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002239static void pnv_pci_ioda_setup_opal_tce_kill(struct pnv_phb *phb)
2240{
2241 const __be64 *swinvp;
2242
2243 /* OPAL variant of PHB3 invalidated TCEs */
2244 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
2245 if (!swinvp)
2246 return;
2247
2248 phb->ioda.tce_inval_reg_phys = be64_to_cpup(swinvp);
2249 phb->ioda.tce_inval_reg = ioremap(phb->ioda.tce_inval_reg_phys, 8);
2250}
2251
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002252static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
2253 unsigned levels, unsigned long limit,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002254 unsigned long *current_offset, unsigned long *total_allocated)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002255{
2256 struct page *tce_mem = NULL;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002257 __be64 *addr, *tmp;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002258 unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002259 unsigned long allocated = 1UL << (order + PAGE_SHIFT);
2260 unsigned entries = 1UL << (shift - 3);
2261 long i;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002262
2263 tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
2264 if (!tce_mem) {
2265 pr_err("Failed to allocate a TCE memory, order=%d\n", order);
2266 return NULL;
2267 }
2268 addr = page_address(tce_mem);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002269 memset(addr, 0, allocated);
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002270 *total_allocated += allocated;
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002271
2272 --levels;
2273 if (!levels) {
2274 *current_offset += allocated;
2275 return addr;
2276 }
2277
2278 for (i = 0; i < entries; ++i) {
2279 tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002280 levels, limit, current_offset, total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002281 if (!tmp)
2282 break;
2283
2284 addr[i] = cpu_to_be64(__pa(tmp) |
2285 TCE_PCI_READ | TCE_PCI_WRITE);
2286
2287 if (*current_offset >= limit)
2288 break;
2289 }
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002290
2291 return addr;
2292}
2293
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002294static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2295 unsigned long size, unsigned level);
2296
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002297static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002298 __u32 page_shift, __u64 window_size, __u32 levels,
2299 struct iommu_table *tbl)
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002300{
2301 void *addr;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002302 unsigned long offset = 0, level_shift, total_allocated = 0;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002303 const unsigned window_shift = ilog2(window_size);
2304 unsigned entries_shift = window_shift - page_shift;
2305 unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
2306 const unsigned long tce_table_size = 1UL << table_shift;
2307
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002308 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
2309 return -EINVAL;
2310
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002311 if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
2312 return -EINVAL;
2313
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002314 /* Adjust direct table size from window_size and levels */
2315 entries_shift = (entries_shift + levels - 1) / levels;
2316 level_shift = entries_shift + 3;
2317 level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
2318
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002319 /* Allocate TCE table */
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002320 addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002321 levels, tce_table_size, &offset, &total_allocated);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002322
2323 /* addr==NULL means that the first level allocation failed */
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002324 if (!addr)
2325 return -ENOMEM;
2326
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002327 /*
2328 * First level was allocated but some lower level failed as
2329 * we did not allocate as much as we wanted,
2330 * release partially allocated table.
2331 */
2332 if (offset < tce_table_size) {
2333 pnv_pci_ioda2_table_do_free_pages(addr,
2334 1ULL << (level_shift - 3), levels - 1);
2335 return -ENOMEM;
2336 }
2337
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002338 /* Setup linux iommu table */
2339 pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
2340 page_shift);
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002341 tbl->it_level_size = 1ULL << (level_shift - 3);
2342 tbl->it_indirect_levels = levels - 1;
Alexey Kardashevskiy3ba3a732015-07-20 20:45:51 +10002343 tbl->it_allocated_size = total_allocated;
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002344
2345 pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
2346 window_size, tce_table_size, bus_offset);
2347
2348 return 0;
2349}
2350
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002351static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
2352 unsigned long size, unsigned level)
2353{
2354 const unsigned long addr_ul = (unsigned long) addr &
2355 ~(TCE_PCI_READ | TCE_PCI_WRITE);
2356
2357 if (level) {
2358 long i;
2359 u64 *tmp = (u64 *) addr_ul;
2360
2361 for (i = 0; i < size; ++i) {
2362 unsigned long hpa = be64_to_cpu(tmp[i]);
2363
2364 if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
2365 continue;
2366
2367 pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
2368 level - 1);
2369 }
2370 }
2371
2372 free_pages(addr_ul, get_order(size << 3));
2373}
2374
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002375static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
2376{
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002377 const unsigned long size = tbl->it_indirect_levels ?
2378 tbl->it_level_size : tbl->it_size;
2379
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002380 if (!tbl->it_size)
2381 return;
2382
Alexey Kardashevskiybbb845c2015-06-05 16:35:19 +10002383 pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
2384 tbl->it_indirect_levels);
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002385}
2386
Gavin Shan373f5652013-04-25 19:21:01 +00002387static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
2388 struct pnv_ioda_pe *pe)
2389{
Gavin Shan373f5652013-04-25 19:21:01 +00002390 int64_t rc;
2391
2392 /* We shouldn't already have a 32-bit DMA associated */
2393 if (WARN_ON(pe->tce32_seg >= 0))
2394 return;
2395
Alexey Kardashevskiyf87a8862015-06-05 16:35:10 +10002396 /* TVE #1 is selected by PCI address bit 59 */
2397 pe->tce_bypass_base = 1ull << 59;
2398
Alexey Kardashevskiyb348aa62015-06-05 16:35:08 +10002399 iommu_register_group(&pe->table_group, phb->hose->global_number,
2400 pe->pe_number);
Alexey Kardashevskiyc5773822015-06-05 16:34:55 +10002401
Gavin Shan373f5652013-04-25 19:21:01 +00002402 /* The PE will reserve all possible 32-bits space */
2403 pe->tce32_seg = 0;
Gavin Shan373f5652013-04-25 19:21:01 +00002404 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
Alexey Kardashevskiyaca69132015-06-05 16:35:17 +10002405 phb->ioda.m32_pci_base);
Gavin Shan373f5652013-04-25 19:21:01 +00002406
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002407 /* Setup linux iommu table */
Alexey Kardashevskiy4793d652015-06-05 16:35:20 +10002408 pe->table_group.tce32_start = 0;
2409 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
2410 pe->table_group.max_dynamic_windows_supported =
2411 IOMMU_TABLE_GROUP_MAX_TABLES;
2412 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
2413 pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
Alexey Kardashevskiye5aad1e2015-06-05 16:35:16 +10002414#ifdef CONFIG_IOMMU_API
2415 pe->table_group.ops = &pnv_pci_ioda2_ops;
2416#endif
2417
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002418 rc = pnv_pci_ioda2_setup_default_config(pe);
Gavin Shan373f5652013-04-25 19:21:01 +00002419 if (rc) {
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002420 if (pe->tce32_seg >= 0)
2421 pe->tce32_seg = -1;
2422 return;
Gavin Shan373f5652013-04-25 19:21:01 +00002423 }
2424
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002425 if (pe->flags & PNV_IODA_PE_DEV)
Alexey Kardashevskiy46170822015-06-05 16:34:54 +10002426 iommu_add_device(&pe->pdev->dev);
Alexey Kardashevskiy46d3e1e2015-06-05 16:35:23 +10002427 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10002428 pnv_ioda_setup_bus_dma(pe, pe->pbus);
Gavin Shan373f5652013-04-25 19:21:01 +00002429}
2430
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002431static void pnv_ioda_setup_dma(struct pnv_phb *phb)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002432{
2433 struct pci_controller *hose = phb->hose;
2434 unsigned int residual, remaining, segs, tw, base;
2435 struct pnv_ioda_pe *pe;
2436
2437 /* If we have more PE# than segments available, hand out one
2438 * per PE until we run out and let the rest fail. If not,
2439 * then we assign at least one segment per PE, plus more based
2440 * on the amount of devices under that PE
2441 */
2442 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
2443 residual = 0;
2444 else
2445 residual = phb->ioda.tce32_count -
2446 phb->ioda.dma_pe_count;
2447
2448 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
2449 hose->global_number, phb->ioda.tce32_count);
2450 pr_info("PCI: %d PE# for a total weight of %d\n",
2451 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
2452
Alexey Kardashevskiy5780fb02015-06-05 16:35:12 +10002453 pnv_pci_ioda_setup_opal_tce_kill(phb);
2454
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002455 /* Walk our PE list and configure their DMA segments, hand them
2456 * out one base segment plus any residual segments based on
2457 * weight
2458 */
2459 remaining = phb->ioda.tce32_count;
2460 tw = phb->ioda.dma_weight;
2461 base = 0;
Gavin Shan7ebdf952012-08-20 03:49:15 +00002462 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002463 if (!pe->dma_weight)
2464 continue;
2465 if (!remaining) {
2466 pe_warn(pe, "No DMA32 resources available\n");
2467 continue;
2468 }
2469 segs = 1;
2470 if (residual) {
2471 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
2472 if (segs > remaining)
2473 segs = remaining;
2474 }
Gavin Shan373f5652013-04-25 19:21:01 +00002475
2476 /*
2477 * For IODA2 compliant PHB3, we needn't care about the weight.
2478 * The all available 32-bits DMA space will be assigned to
2479 * the specific PE.
2480 */
2481 if (phb->type == PNV_PHB_IODA1) {
2482 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
2483 pe->dma_weight, segs);
2484 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
Alistair Popple5d2aa712015-12-17 13:43:13 +11002485 } else if (phb->type == PNV_PHB_IODA2) {
Gavin Shan373f5652013-04-25 19:21:01 +00002486 pe_info(pe, "Assign DMA32 space\n");
2487 segs = 0;
2488 pnv_pci_ioda2_setup_dma_pe(phb, pe);
Alistair Popple5d2aa712015-12-17 13:43:13 +11002489 } else if (phb->type == PNV_PHB_NPU) {
2490 /*
2491 * We initialise the DMA space for an NPU PHB
2492 * after setup of the PHB is complete as we
2493 * point the NPU TVT to the the same location
2494 * as the PHB3 TVT.
2495 */
Gavin Shan373f5652013-04-25 19:21:01 +00002496 }
2497
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002498 remaining -= segs;
2499 base += segs;
2500 }
2501}
2502
2503#ifdef CONFIG_PCI_MSI
Gavin Shan137436c2013-04-25 19:20:59 +00002504static void pnv_ioda2_msi_eoi(struct irq_data *d)
2505{
2506 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
2507 struct irq_chip *chip = irq_data_get_irq_chip(d);
2508 struct pnv_phb *phb = container_of(chip, struct pnv_phb,
2509 ioda.irq_chip);
2510 int64_t rc;
2511
2512 rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
2513 WARN_ON_ONCE(rc);
2514
2515 icp_native_eoi(d);
2516}
2517
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002518
2519static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
2520{
2521 struct irq_data *idata;
2522 struct irq_chip *ichip;
2523
2524 if (phb->type != PNV_PHB_IODA2)
2525 return;
2526
2527 if (!phb->ioda.irq_chip_init) {
2528 /*
2529 * First time we setup an MSI IRQ, we need to setup the
2530 * corresponding IRQ chip to route correctly.
2531 */
2532 idata = irq_get_irq_data(virq);
2533 ichip = irq_data_get_irq_chip(idata);
2534 phb->ioda.irq_chip_init = 1;
2535 phb->ioda.irq_chip = *ichip;
2536 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
2537 }
2538 irq_set_chip(virq, &phb->ioda.irq_chip);
2539}
2540
Ian Munsie80c49c72014-10-08 19:54:57 +11002541#ifdef CONFIG_CXL_BASE
2542
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002543struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
Ian Munsie80c49c72014-10-08 19:54:57 +11002544{
2545 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2546
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002547 return of_node_get(hose->dn);
Ian Munsie80c49c72014-10-08 19:54:57 +11002548}
Ryan Grimm6f963ec2015-01-28 20:16:04 -06002549EXPORT_SYMBOL(pnv_pci_get_phb_node);
Ian Munsie80c49c72014-10-08 19:54:57 +11002550
Ryan Grimm1212aa12015-01-19 11:52:50 -06002551int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
Ian Munsie80c49c72014-10-08 19:54:57 +11002552{
2553 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2554 struct pnv_phb *phb = hose->private_data;
2555 struct pnv_ioda_pe *pe;
2556 int rc;
2557
2558 pe = pnv_ioda_get_pe(dev);
2559 if (!pe)
2560 return -ENODEV;
2561
2562 pe_info(pe, "Switching PHB to CXL\n");
2563
Ryan Grimm1212aa12015-01-19 11:52:50 -06002564 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
Ian Munsie80c49c72014-10-08 19:54:57 +11002565 if (rc)
2566 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
2567
2568 return rc;
2569}
Ryan Grimm1212aa12015-01-19 11:52:50 -06002570EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
Ian Munsie80c49c72014-10-08 19:54:57 +11002571
2572/* Find PHB for cxl dev and allocate MSI hwirqs?
2573 * Returns the absolute hardware IRQ number
2574 */
2575int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
2576{
2577 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2578 struct pnv_phb *phb = hose->private_data;
2579 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
2580
2581 if (hwirq < 0) {
2582 dev_warn(&dev->dev, "Failed to find a free MSI\n");
2583 return -ENOSPC;
2584 }
2585
2586 return phb->msi_base + hwirq;
2587}
2588EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
2589
2590void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
2591{
2592 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2593 struct pnv_phb *phb = hose->private_data;
2594
2595 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
2596}
2597EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
2598
2599void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
2600 struct pci_dev *dev)
2601{
2602 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2603 struct pnv_phb *phb = hose->private_data;
2604 int i, hwirq;
2605
2606 for (i = 1; i < CXL_IRQ_RANGES; i++) {
2607 if (!irqs->range[i])
2608 continue;
2609 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
2610 i, irqs->offset[i],
2611 irqs->range[i]);
2612 hwirq = irqs->offset[i] - phb->msi_base;
2613 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
2614 irqs->range[i]);
2615 }
2616}
2617EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
2618
2619int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
2620 struct pci_dev *dev, int num)
2621{
2622 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2623 struct pnv_phb *phb = hose->private_data;
2624 int i, hwirq, try;
2625
2626 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
2627
2628 /* 0 is reserved for the multiplexed PSL DSI interrupt */
2629 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
2630 try = num;
2631 while (try) {
2632 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
2633 if (hwirq >= 0)
2634 break;
2635 try /= 2;
2636 }
2637 if (!try)
2638 goto fail;
2639
2640 irqs->offset[i] = phb->msi_base + hwirq;
2641 irqs->range[i] = try;
2642 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
2643 i, irqs->offset[i], irqs->range[i]);
2644 num -= try;
2645 }
2646 if (num)
2647 goto fail;
2648
2649 return 0;
2650fail:
2651 pnv_cxl_release_hwirq_ranges(irqs, dev);
2652 return -ENOSPC;
2653}
2654EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
2655
2656int pnv_cxl_get_irq_count(struct pci_dev *dev)
2657{
2658 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2659 struct pnv_phb *phb = hose->private_data;
2660
2661 return phb->msi_bmp.irq_count;
2662}
2663EXPORT_SYMBOL(pnv_cxl_get_irq_count);
2664
2665int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
2666 unsigned int virq)
2667{
2668 struct pci_controller *hose = pci_bus_to_host(dev->bus);
2669 struct pnv_phb *phb = hose->private_data;
2670 unsigned int xive_num = hwirq - phb->msi_base;
2671 struct pnv_ioda_pe *pe;
2672 int rc;
2673
2674 if (!(pe = pnv_ioda_get_pe(dev)))
2675 return -ENODEV;
2676
2677 /* Assign XIVE to PE */
2678 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2679 if (rc) {
2680 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
2681 "hwirq 0x%x XIVE 0x%x PE\n",
2682 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
2683 return -EIO;
2684 }
2685 set_msi_irq_chip(phb, virq);
2686
2687 return 0;
2688}
2689EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
2690#endif
2691
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002692static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
Gavin Shan137436c2013-04-25 19:20:59 +00002693 unsigned int hwirq, unsigned int virq,
2694 unsigned int is_64, struct msi_msg *msg)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002695{
2696 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
2697 unsigned int xive_num = hwirq - phb->msi_base;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002698 __be32 data;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002699 int rc;
2700
2701 /* No PE assigned ? bail out ... no MSI for you ! */
2702 if (pe == NULL)
2703 return -ENXIO;
2704
2705 /* Check if we have an MVE */
2706 if (pe->mve_number < 0)
2707 return -ENXIO;
2708
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002709 /* Force 32-bit MSI on some broken devices */
Benjamin Herrenschmidt36074382014-10-07 16:12:36 +11002710 if (dev->no_64bit_msi)
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00002711 is_64 = 0;
2712
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002713 /* Assign XIVE to PE */
2714 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
2715 if (rc) {
2716 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
2717 pci_name(dev), rc, xive_num);
2718 return -EIO;
2719 }
2720
2721 if (is_64) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002722 __be64 addr64;
2723
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002724 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
2725 &addr64, &data);
2726 if (rc) {
2727 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
2728 pci_name(dev), rc);
2729 return -EIO;
2730 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002731 msg->address_hi = be64_to_cpu(addr64) >> 32;
2732 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002733 } else {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002734 __be32 addr32;
2735
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002736 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
2737 &addr32, &data);
2738 if (rc) {
2739 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
2740 pci_name(dev), rc);
2741 return -EIO;
2742 }
2743 msg->address_hi = 0;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002744 msg->address_lo = be32_to_cpu(addr32);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002745 }
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10002746 msg->data = be32_to_cpu(data);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002747
Ian Munsiefd9a1c22014-10-08 19:54:55 +11002748 set_msi_irq_chip(phb, virq);
Gavin Shan137436c2013-04-25 19:20:59 +00002749
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002750 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
2751 " address=%x_%08x data=%x PE# %d\n",
2752 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
2753 msg->address_hi, msg->address_lo, data, pe->pe_number);
2754
2755 return 0;
2756}
2757
2758static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
2759{
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002760 unsigned int count;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002761 const __be32 *prop = of_get_property(phb->hose->dn,
2762 "ibm,opal-msi-ranges", NULL);
2763 if (!prop) {
2764 /* BML Fallback */
2765 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
2766 }
2767 if (!prop)
2768 return;
2769
2770 phb->msi_base = be32_to_cpup(prop);
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002771 count = be32_to_cpup(prop + 1);
2772 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002773 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
2774 phb->hose->global_number);
2775 return;
2776 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002777
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002778 phb->msi_setup = pnv_pci_ioda_msi_setup;
2779 phb->msi32_support = 1;
2780 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
Gavin Shanfb1b55d2013-03-05 21:12:37 +00002781 count, phb->msi_base);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00002782}
2783#else
2784static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
2785#endif /* CONFIG_PCI_MSI */
2786
Wei Yang6e628c72015-03-25 16:23:55 +08002787#ifdef CONFIG_PCI_IOV
2788static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
2789{
2790 struct pci_controller *hose;
2791 struct pnv_phb *phb;
2792 struct resource *res;
2793 int i;
2794 resource_size_t size;
2795 struct pci_dn *pdn;
Wei Yang5b88ec22015-03-25 16:23:58 +08002796 int mul, total_vfs;
Wei Yang6e628c72015-03-25 16:23:55 +08002797
2798 if (!pdev->is_physfn || pdev->is_added)
2799 return;
2800
2801 hose = pci_bus_to_host(pdev->bus);
2802 phb = hose->private_data;
2803
2804 pdn = pci_get_pdn(pdev);
2805 pdn->vfs_expanded = 0;
2806
Wei Yang5b88ec22015-03-25 16:23:58 +08002807 total_vfs = pci_sriov_get_totalvfs(pdev);
2808 pdn->m64_per_iov = 1;
2809 mul = phb->ioda.total_pe;
2810
2811 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2812 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2813 if (!res->flags || res->parent)
2814 continue;
2815 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2816 dev_warn(&pdev->dev, " non M64 VF BAR%d: %pR\n",
2817 i, res);
2818 continue;
2819 }
2820
2821 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
2822
2823 /* bigger than 64M */
2824 if (size > (1 << 26)) {
2825 dev_info(&pdev->dev, "PowerNV: VF BAR%d: %pR IOV size is bigger than 64M, roundup power2\n",
2826 i, res);
2827 pdn->m64_per_iov = M64_PER_IOV;
2828 mul = roundup_pow_of_two(total_vfs);
2829 break;
2830 }
2831 }
2832
Wei Yang6e628c72015-03-25 16:23:55 +08002833 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
2834 res = &pdev->resource[i + PCI_IOV_RESOURCES];
2835 if (!res->flags || res->parent)
2836 continue;
2837 if (!pnv_pci_is_mem_pref_64(res->flags)) {
2838 dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
2839 i, res);
2840 continue;
2841 }
2842
2843 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
2844 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
Wei Yang5b88ec22015-03-25 16:23:58 +08002845 res->end = res->start + size * mul - 1;
Wei Yang6e628c72015-03-25 16:23:55 +08002846 dev_dbg(&pdev->dev, " %pR\n", res);
2847 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
Wei Yang5b88ec22015-03-25 16:23:58 +08002848 i, res, mul);
Wei Yang6e628c72015-03-25 16:23:55 +08002849 }
Wei Yang5b88ec22015-03-25 16:23:58 +08002850 pdn->vfs_expanded = mul;
Wei Yang6e628c72015-03-25 16:23:55 +08002851}
2852#endif /* CONFIG_PCI_IOV */
2853
Gavin Shan11685be2012-08-20 03:49:16 +00002854/*
2855 * This function is supposed to be called on basis of PE from top
2856 * to bottom style. So the the I/O or MMIO segment assigned to
2857 * parent PE could be overrided by its child PEs if necessary.
2858 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002859static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
2860 struct pnv_ioda_pe *pe)
Gavin Shan11685be2012-08-20 03:49:16 +00002861{
2862 struct pnv_phb *phb = hose->private_data;
2863 struct pci_bus_region region;
2864 struct resource *res;
2865 int i, index;
2866 int rc;
2867
2868 /*
2869 * NOTE: We only care PCI bus based PE for now. For PCI
2870 * device based PE, for example SRIOV sensitive VF should
2871 * be figured out later.
2872 */
2873 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2874
2875 pci_bus_for_each_resource(pe->pbus, res, i) {
2876 if (!res || !res->flags ||
2877 res->start > res->end)
2878 continue;
2879
2880 if (res->flags & IORESOURCE_IO) {
2881 region.start = res->start - phb->ioda.io_pci_base;
2882 region.end = res->end - phb->ioda.io_pci_base;
2883 index = region.start / phb->ioda.io_segsize;
2884
2885 while (index < phb->ioda.total_pe &&
2886 region.start <= region.end) {
2887 phb->ioda.io_segmap[index] = pe->pe_number;
2888 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2889 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
2890 if (rc != OPAL_SUCCESS) {
2891 pr_err("%s: OPAL error %d when mapping IO "
2892 "segment #%d to PE#%d\n",
2893 __func__, rc, index, pe->pe_number);
2894 break;
2895 }
2896
2897 region.start += phb->ioda.io_segsize;
2898 index++;
2899 }
Gavin Shan027fa022015-03-27 11:29:00 +11002900 } else if ((res->flags & IORESOURCE_MEM) &&
2901 !pnv_pci_is_mem_pref_64(res->flags)) {
Gavin Shan11685be2012-08-20 03:49:16 +00002902 region.start = res->start -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002903 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002904 phb->ioda.m32_pci_base;
2905 region.end = res->end -
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10002906 hose->mem_offset[0] -
Gavin Shan11685be2012-08-20 03:49:16 +00002907 phb->ioda.m32_pci_base;
2908 index = region.start / phb->ioda.m32_segsize;
2909
2910 while (index < phb->ioda.total_pe &&
2911 region.start <= region.end) {
2912 phb->ioda.m32_segmap[index] = pe->pe_number;
2913 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2914 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
2915 if (rc != OPAL_SUCCESS) {
2916 pr_err("%s: OPAL error %d when mapping M32 "
2917 "segment#%d to PE#%d",
2918 __func__, rc, index, pe->pe_number);
2919 break;
2920 }
2921
2922 region.start += phb->ioda.m32_segsize;
2923 index++;
2924 }
2925 }
2926 }
2927}
2928
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002929static void pnv_pci_ioda_setup_seg(void)
Gavin Shan11685be2012-08-20 03:49:16 +00002930{
2931 struct pci_controller *tmp, *hose;
2932 struct pnv_phb *phb;
2933 struct pnv_ioda_pe *pe;
2934
2935 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2936 phb = hose->private_data;
Alistair Popple5d2aa712015-12-17 13:43:13 +11002937
2938 /* NPU PHB does not support IO or MMIO segmentation */
2939 if (phb->type == PNV_PHB_NPU)
2940 continue;
2941
Gavin Shan11685be2012-08-20 03:49:16 +00002942 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2943 pnv_ioda_setup_pe_seg(hose, pe);
2944 }
2945 }
2946}
2947
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08002948static void pnv_pci_ioda_setup_DMA(void)
Gavin Shan13395c42012-08-20 03:49:17 +00002949{
2950 struct pci_controller *hose, *tmp;
Gavin Shandb1266c2012-08-20 03:49:18 +00002951 struct pnv_phb *phb;
Gavin Shan13395c42012-08-20 03:49:17 +00002952
2953 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2954 pnv_ioda_setup_dma(hose->private_data);
Gavin Shandb1266c2012-08-20 03:49:18 +00002955
2956 /* Mark the PHB initialization done */
2957 phb = hose->private_data;
2958 phb->initialized = 1;
Gavin Shan13395c42012-08-20 03:49:17 +00002959 }
2960}
2961
Gavin Shan37c367f2013-06-20 18:13:25 +08002962static void pnv_pci_ioda_create_dbgfs(void)
2963{
2964#ifdef CONFIG_DEBUG_FS
2965 struct pci_controller *hose, *tmp;
2966 struct pnv_phb *phb;
2967 char name[16];
2968
2969 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2970 phb = hose->private_data;
2971
2972 sprintf(name, "PCI%04x", hose->global_number);
2973 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
2974 if (!phb->dbgfs)
2975 pr_warning("%s: Error on creating debugfs on PHB#%x\n",
2976 __func__, hose->global_number);
2977 }
2978#endif /* CONFIG_DEBUG_FS */
2979}
2980
Alistair Popple5d2aa712015-12-17 13:43:13 +11002981static void pnv_npu_ioda_fixup(void)
2982{
2983 bool enable_bypass;
2984 struct pci_controller *hose, *tmp;
2985 struct pnv_phb *phb;
2986 struct pnv_ioda_pe *pe;
2987
2988 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2989 phb = hose->private_data;
2990 if (phb->type != PNV_PHB_NPU)
2991 continue;
2992
2993 list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
2994 enable_bypass = dma_get_mask(&pe->pdev->dev) ==
2995 DMA_BIT_MASK(64);
2996 pnv_npu_init_dma_pe(pe);
2997 pnv_npu_dma_set_bypass(pe, enable_bypass);
2998 }
2999 }
3000}
3001
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -08003002static void pnv_pci_ioda_fixup(void)
Gavin Shanfb446ad2012-08-20 03:49:14 +00003003{
3004 pnv_pci_ioda_setup_PEs();
Gavin Shan11685be2012-08-20 03:49:16 +00003005 pnv_pci_ioda_setup_seg();
Gavin Shan13395c42012-08-20 03:49:17 +00003006 pnv_pci_ioda_setup_DMA();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003007
Gavin Shan37c367f2013-06-20 18:13:25 +08003008 pnv_pci_ioda_create_dbgfs();
3009
Gavin Shane9cc17d2013-06-20 13:21:14 +08003010#ifdef CONFIG_EEH
Gavin Shane9cc17d2013-06-20 13:21:14 +08003011 eeh_init();
Mike Qiudadcd6d2014-06-26 02:58:47 -04003012 eeh_addr_cache_build();
Gavin Shane9cc17d2013-06-20 13:21:14 +08003013#endif
Alistair Popple5d2aa712015-12-17 13:43:13 +11003014
3015 /* Link NPU IODA tables to their PCI devices. */
3016 pnv_npu_ioda_fixup();
Gavin Shanfb446ad2012-08-20 03:49:14 +00003017}
3018
Gavin Shan271fd032012-09-11 16:59:47 -06003019/*
3020 * Returns the alignment for I/O or memory windows for P2P
3021 * bridges. That actually depends on how PEs are segmented.
3022 * For now, we return I/O or M32 segment size for PE sensitive
3023 * P2P bridges. Otherwise, the default values (4KiB for I/O,
3024 * 1MiB for memory) will be returned.
3025 *
3026 * The current PCI bus might be put into one PE, which was
3027 * create against the parent PCI bridge. For that case, we
3028 * needn't enlarge the alignment so that we can save some
3029 * resources.
3030 */
3031static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
3032 unsigned long type)
3033{
3034 struct pci_dev *bridge;
3035 struct pci_controller *hose = pci_bus_to_host(bus);
3036 struct pnv_phb *phb = hose->private_data;
3037 int num_pci_bridges = 0;
3038
3039 bridge = bus->self;
3040 while (bridge) {
3041 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
3042 num_pci_bridges++;
3043 if (num_pci_bridges >= 2)
3044 return 1;
3045 }
3046
3047 bridge = bridge->bus->self;
3048 }
3049
Guo Chao262af552014-07-21 14:42:30 +10003050 /* We fail back to M32 if M64 isn't supported */
3051 if (phb->ioda.m64_segsize &&
3052 pnv_pci_is_mem_pref_64(type))
3053 return phb->ioda.m64_segsize;
Gavin Shan271fd032012-09-11 16:59:47 -06003054 if (type & IORESOURCE_MEM)
3055 return phb->ioda.m32_segsize;
3056
3057 return phb->ioda.io_segsize;
3058}
3059
Wei Yang5350ab32015-03-25 16:23:56 +08003060#ifdef CONFIG_PCI_IOV
3061static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
3062 int resno)
3063{
3064 struct pci_dn *pdn = pci_get_pdn(pdev);
3065 resource_size_t align, iov_align;
3066
3067 iov_align = resource_size(&pdev->resource[resno]);
3068 if (iov_align)
3069 return iov_align;
3070
3071 align = pci_iov_resource_size(pdev, resno);
3072 if (pdn->vfs_expanded)
3073 return pdn->vfs_expanded * align;
3074
3075 return align;
3076}
3077#endif /* CONFIG_PCI_IOV */
3078
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003079/* Prevent enabling devices for which we couldn't properly
3080 * assign a PE
3081 */
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003082static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003083{
Gavin Shandb1266c2012-08-20 03:49:18 +00003084 struct pci_controller *hose = pci_bus_to_host(dev->bus);
3085 struct pnv_phb *phb = hose->private_data;
3086 struct pci_dn *pdn;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003087
Gavin Shandb1266c2012-08-20 03:49:18 +00003088 /* The function is probably called while the PEs have
3089 * not be created yet. For example, resource reassignment
3090 * during PCI probe period. We just skip the check if
3091 * PEs isn't ready.
3092 */
3093 if (!phb->initialized)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003094 return true;
Gavin Shandb1266c2012-08-20 03:49:18 +00003095
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +00003096 pdn = pci_get_pdn(dev);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003097 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003098 return false;
Gavin Shandb1266c2012-08-20 03:49:18 +00003099
Daniel Axtensc88c2a12015-03-31 16:00:41 +11003100 return true;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003101}
3102
3103static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
3104 u32 devfn)
3105{
3106 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
3107}
3108
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003109static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003110{
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003111 struct pnv_phb *phb = hose->private_data;
3112
Gavin Shand1a85ee2014-09-30 12:39:05 +10003113 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +10003114 OPAL_ASSERT_RESET);
3115}
3116
Daniel Axtens92ae0352015-04-28 15:12:05 +10003117static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
3118 .dma_dev_setup = pnv_pci_dma_dev_setup,
3119#ifdef CONFIG_PCI_MSI
3120 .setup_msi_irqs = pnv_setup_msi_irqs,
3121 .teardown_msi_irqs = pnv_teardown_msi_irqs,
3122#endif
3123 .enable_device_hook = pnv_pci_enable_device_hook,
3124 .window_alignment = pnv_pci_window_alignment,
3125 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
Daniel Axtens763d2d82015-04-28 15:12:07 +10003126 .dma_set_mask = pnv_pci_ioda_dma_set_mask,
Andrew Donnellan535229822015-08-07 13:45:54 +10003127 .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
Michael Neuling7a8e6bb2015-05-27 16:06:59 +10003128 .shutdown = pnv_pci_ioda_shutdown,
Daniel Axtens92ae0352015-04-28 15:12:05 +10003129};
3130
Alistair Popple5d2aa712015-12-17 13:43:13 +11003131static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
3132 .dma_dev_setup = pnv_pci_dma_dev_setup,
3133#ifdef CONFIG_PCI_MSI
3134 .setup_msi_irqs = pnv_setup_msi_irqs,
3135 .teardown_msi_irqs = pnv_teardown_msi_irqs,
3136#endif
3137 .enable_device_hook = pnv_pci_enable_device_hook,
3138 .window_alignment = pnv_pci_window_alignment,
3139 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
3140 .dma_set_mask = pnv_npu_dma_set_mask,
3141 .shutdown = pnv_pci_ioda_shutdown,
3142};
3143
Anton Blancharde51df2c2014-08-20 08:55:18 +10003144static void __init pnv_pci_init_ioda_phb(struct device_node *np,
3145 u64 hub_id, int ioda_type)
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003146{
3147 struct pci_controller *hose;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003148 struct pnv_phb *phb;
Gavin Shan81846162013-12-26 09:29:40 +08003149 unsigned long size, m32map_off, pemap_off, iomap_off = 0;
Alistair Popplec681b932013-09-23 12:04:57 +10003150 const __be64 *prop64;
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003151 const __be32 *prop32;
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003152 int len;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003153 u64 phb_id;
3154 void *aux;
3155 long rc;
3156
Gavin Shan58d714e2013-07-31 16:47:00 +08003157 pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003158
3159 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
3160 if (!prop64) {
3161 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
3162 return;
3163 }
3164 phb_id = be64_to_cpup(prop64);
3165 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
3166
Michael Ellermane39f223f2014-11-18 16:47:35 +11003167 phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
Gavin Shan58d714e2013-07-31 16:47:00 +08003168
3169 /* Allocate PCI controller */
Gavin Shan58d714e2013-07-31 16:47:00 +08003170 phb->hose = hose = pcibios_alloc_controller(np);
3171 if (!phb->hose) {
3172 pr_err(" Can't allocate PCI controller for %s\n",
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003173 np->full_name);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003174 memblock_free(__pa(phb), sizeof(struct pnv_phb));
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003175 return;
3176 }
3177
3178 spin_lock_init(&phb->lock);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003179 prop32 = of_get_property(np, "bus-range", &len);
3180 if (prop32 && len == 8) {
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003181 hose->first_busno = be32_to_cpu(prop32[0]);
3182 hose->last_busno = be32_to_cpu(prop32[1]);
Gavin Shanf1b7cc32013-07-31 16:47:01 +08003183 } else {
3184 pr_warn(" Broken <bus-range> on %s\n", np->full_name);
3185 hose->first_busno = 0;
3186 hose->last_busno = 0xff;
3187 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003188 hose->private_data = phb;
Gavin Shane9cc17d2013-06-20 13:21:14 +08003189 phb->hub_id = hub_id;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003190 phb->opal_id = phb_id;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003191 phb->type = ioda_type;
Wei Yang781a8682015-03-25 16:23:57 +08003192 mutex_init(&phb->ioda.pe_alloc_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003193
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003194 /* Detect specific models for error handling */
3195 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
3196 phb->model = PNV_PHB_MODEL_P7IOC;
Benjamin Herrenschmidtf3d40c22013-05-04 14:24:32 +00003197 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
Gavin Shanaa0c0332013-04-25 19:20:57 +00003198 phb->model = PNV_PHB_MODEL_PHB3;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003199 else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
3200 phb->model = PNV_PHB_MODEL_NPU;
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +00003201 else
3202 phb->model = PNV_PHB_MODEL_UNKNOWN;
3203
Gavin Shanaa0c0332013-04-25 19:20:57 +00003204 /* Parse 32-bit and IO ranges (if any) */
Gavin Shan2f1ec022013-07-31 16:47:02 +08003205 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003206
Gavin Shanaa0c0332013-04-25 19:20:57 +00003207 /* Get registers */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003208 phb->regs = of_iomap(np, 0);
3209 if (phb->regs == NULL)
3210 pr_err(" Failed to map registers !\n");
3211
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003212 /* Initialize more IODA stuff */
Gavin Shan36954dc2013-11-04 16:32:47 +08003213 phb->ioda.total_pe = 1;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003214 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
Gavin Shan36954dc2013-11-04 16:32:47 +08003215 if (prop32)
Benjamin Herrenschmidt3a1a4662013-09-23 12:05:01 +10003216 phb->ioda.total_pe = be32_to_cpup(prop32);
Gavin Shan36954dc2013-11-04 16:32:47 +08003217 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
3218 if (prop32)
3219 phb->ioda.reserved_pe = be32_to_cpup(prop32);
Guo Chao262af552014-07-21 14:42:30 +10003220
3221 /* Parse 64-bit MMIO range */
3222 pnv_ioda_parse_m64_window(phb);
3223
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003224 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
Gavin Shanaa0c0332013-04-25 19:20:57 +00003225 /* FW Has already off top 64k of M32 space (MSI space) */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003226 phb->ioda.m32_size += 0x10000;
3227
3228 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
Benjamin Herrenschmidt3fd47f02013-05-06 13:40:40 +10003229 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003230 phb->ioda.io_size = hose->pci_io_size;
3231 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
3232 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
3233
Gavin Shanc35d2a82013-07-31 16:47:04 +08003234 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003235 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
3236 m32map_off = size;
Gavin Shane47747f2012-08-20 03:49:19 +00003237 size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
Gavin Shanc35d2a82013-07-31 16:47:04 +08003238 if (phb->type == PNV_PHB_IODA1) {
3239 iomap_off = size;
3240 size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
3241 }
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003242 pemap_off = size;
3243 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
Michael Ellermane39f223f2014-11-18 16:47:35 +11003244 aux = memblock_virt_alloc(size, 0);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003245 phb->ioda.pe_alloc = aux;
3246 phb->ioda.m32_segmap = aux + m32map_off;
Gavin Shanc35d2a82013-07-31 16:47:04 +08003247 if (phb->type == PNV_PHB_IODA1)
3248 phb->ioda.io_segmap = aux + iomap_off;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003249 phb->ioda.pe_array = aux + pemap_off;
Gavin Shan36954dc2013-11-04 16:32:47 +08003250 set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003251
Gavin Shan7ebdf952012-08-20 03:49:15 +00003252 INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003253 INIT_LIST_HEAD(&phb->ioda.pe_list);
Wei Yang781a8682015-03-25 16:23:57 +08003254 mutex_init(&phb->ioda.pe_list_mutex);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003255
3256 /* Calculate how many 32-bit TCE segments we have */
3257 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
3258
Gavin Shanaa0c0332013-04-25 19:20:57 +00003259#if 0 /* We should really do that ... */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003260 rc = opal_pci_set_phb_mem_window(opal->phb_id,
3261 window_type,
3262 window_num,
3263 starting_real_address,
3264 starting_pci_address,
3265 segment_size);
3266#endif
3267
Guo Chao262af552014-07-21 14:42:30 +10003268 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
3269 phb->ioda.total_pe, phb->ioda.reserved_pe,
3270 phb->ioda.m32_size, phb->ioda.m32_segsize);
3271 if (phb->ioda.m64_size)
3272 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
3273 phb->ioda.m64_size, phb->ioda.m64_segsize);
3274 if (phb->ioda.io_size)
3275 pr_info(" IO: 0x%x [segment=0x%x]\n",
3276 phb->ioda.io_size, phb->ioda.io_segsize);
3277
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003278
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003279 phb->hose->ops = &pnv_pci_ops;
Gavin Shan49dec922014-07-21 14:42:33 +10003280 phb->get_pe_state = pnv_ioda_get_pe_state;
3281 phb->freeze_pe = pnv_ioda_freeze_pe;
3282 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003283
3284 /* Setup RID -> PE mapping function */
3285 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
3286
3287 /* Setup TCEs */
3288 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
3289
3290 /* Setup MSI support */
3291 pnv_pci_init_ioda_msis(phb);
3292
Gavin Shanc40a4212012-08-20 03:49:20 +00003293 /*
3294 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
3295 * to let the PCI core do resource assignment. It's supposed
3296 * that the PCI core will do correct I/O and MMIO alignment
3297 * for the P2P bridge bars so that each PCI bus (excluding
3298 * the child P2P bridges) can form individual PE.
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003299 */
Gavin Shanfb446ad2012-08-20 03:49:14 +00003300 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
Alistair Popple5d2aa712015-12-17 13:43:13 +11003301
3302 if (phb->type == PNV_PHB_NPU)
3303 hose->controller_ops = pnv_npu_ioda_controller_ops;
3304 else
3305 hose->controller_ops = pnv_pci_ioda_controller_ops;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003306
Wei Yang6e628c72015-03-25 16:23:55 +08003307#ifdef CONFIG_PCI_IOV
3308 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
Wei Yang5350ab32015-03-25 16:23:56 +08003309 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
Michael Ellermanad30cb92015-04-14 09:29:23 +10003310#endif
3311
Gavin Shanc40a4212012-08-20 03:49:20 +00003312 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003313
3314 /* Reset IODA tables to a clean state */
Gavin Shand1a85ee2014-09-30 12:39:05 +10003315 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003316 if (rc)
Benjamin Herrenschmidtf11fe552011-11-29 18:22:50 +00003317 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
Gavin Shan361f2a22014-04-24 18:00:25 +10003318
3319 /* If we're running in kdump kerenl, the previous kerenl never
3320 * shutdown PCI devices correctly. We already got IODA table
3321 * cleaned out. So we have to issue PHB reset to stop all PCI
3322 * transactions from previous kerenl.
3323 */
3324 if (is_kdump_kernel()) {
3325 pr_info(" Issue PHB reset ...\n");
Gavin Shancadf3642015-02-16 14:45:47 +11003326 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
3327 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
Gavin Shan361f2a22014-04-24 18:00:25 +10003328 }
Guo Chao262af552014-07-21 14:42:30 +10003329
Gavin Shan9e9e8932014-11-12 13:36:05 +11003330 /* Remove M64 resource if we can't configure it successfully */
3331 if (!phb->init_m64 || phb->init_m64(phb))
Guo Chao262af552014-07-21 14:42:30 +10003332 hose->mem_resources[1].flags = 0;
Gavin Shanaa0c0332013-04-25 19:20:57 +00003333}
3334
Bjorn Helgaas67975002013-07-02 12:20:03 -06003335void __init pnv_pci_init_ioda2_phb(struct device_node *np)
Gavin Shanaa0c0332013-04-25 19:20:57 +00003336{
Gavin Shane9cc17d2013-06-20 13:21:14 +08003337 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003338}
3339
Alistair Popple5d2aa712015-12-17 13:43:13 +11003340void __init pnv_pci_init_npu_phb(struct device_node *np)
3341{
3342 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU);
3343}
3344
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003345void __init pnv_pci_init_ioda_hub(struct device_node *np)
3346{
3347 struct device_node *phbn;
Alistair Popplec681b932013-09-23 12:04:57 +10003348 const __be64 *prop64;
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003349 u64 hub_id;
3350
3351 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
3352
3353 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
3354 if (!prop64) {
3355 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
3356 return;
3357 }
3358 hub_id = be64_to_cpup(prop64);
3359 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
3360
3361 /* Count child PHBs */
3362 for_each_child_of_node(np, phbn) {
3363 /* Look for IODA1 PHBs */
3364 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
Gavin Shane9cc17d2013-06-20 13:21:14 +08003365 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +00003366 }
3367}