Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1 | /* |
| 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 Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 12 | #undef DEBUG |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/pci.h> |
Gavin Shan | 361f2a2 | 2014-04-24 18:00:25 +1000 | [diff] [blame] | 16 | #include <linux/crash_dump.h> |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 17 | #include <linux/debugfs.h> |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 18 | #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 Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 25 | #include <linux/memblock.h> |
Alexey Kardashevskiy | ac9a588 | 2015-06-05 16:34:56 +1000 | [diff] [blame] | 26 | #include <linux/iommu.h> |
Alexey Kardashevskiy | e57080f | 2015-06-05 16:35:13 +1000 | [diff] [blame] | 27 | #include <linux/rculist.h> |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 28 | #include <linux/sizes.h> |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 29 | |
| 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 Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 35 | #include <asm/msi_bitmap.h> |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 36 | #include <asm/ppc-pci.h> |
| 37 | #include <asm/opal.h> |
| 38 | #include <asm/iommu.h> |
| 39 | #include <asm/tce.h> |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame] | 40 | #include <asm/xics.h> |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 41 | #include <asm/debug.h> |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 42 | #include <asm/firmware.h> |
Ian Munsie | 80c49c7 | 2014-10-08 19:54:57 +1100 | [diff] [blame] | 43 | #include <asm/pnv-pci.h> |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 44 | #include <asm/mmzone.h> |
Ian Munsie | 80c49c7 | 2014-10-08 19:54:57 +1100 | [diff] [blame] | 45 | |
Michael Neuling | ec249dd | 2015-05-27 16:07:16 +1000 | [diff] [blame] | 46 | #include <misc/cxl-base.h> |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 47 | |
| 48 | #include "powernv.h" |
| 49 | #include "pci.h" |
| 50 | |
Gavin Shan | 9945155 | 2016-05-05 12:02:13 +1000 | [diff] [blame] | 51 | #define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */ |
| 52 | #define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */ |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 53 | #define PNV_IODA1_DMA32_SEGSIZE 0x10000000 |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 54 | |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 55 | #define POWERNV_IOMMU_DEFAULT_LEVELS 1 |
| 56 | #define POWERNV_IOMMU_MAX_LEVELS 5 |
| 57 | |
Gavin Shan | 9497a1c | 2016-06-21 12:35:56 +1000 | [diff] [blame] | 58 | static const char * const pnv_phb_names[] = { "IODA1", "IODA2", "NPU" }; |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 59 | static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl); |
| 60 | |
Alexey Kardashevskiy | 7d623e4 | 2016-04-29 18:55:21 +1000 | [diff] [blame] | 61 | void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level, |
Joe Perches | 6d31c2fa | 2014-09-21 10:55:06 -0700 | [diff] [blame] | 62 | const char *fmt, ...) |
| 63 | { |
| 64 | struct va_format vaf; |
| 65 | va_list args; |
| 66 | char pfix[32]; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 67 | |
Joe Perches | 6d31c2fa | 2014-09-21 10:55:06 -0700 | [diff] [blame] | 68 | va_start(args, fmt); |
| 69 | |
| 70 | vaf.fmt = fmt; |
| 71 | vaf.va = &args; |
| 72 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 73 | if (pe->flags & PNV_IODA_PE_DEV) |
Joe Perches | 6d31c2fa | 2014-09-21 10:55:06 -0700 | [diff] [blame] | 74 | strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix)); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 75 | else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)) |
Joe Perches | 6d31c2fa | 2014-09-21 10:55:06 -0700 | [diff] [blame] | 76 | sprintf(pfix, "%04x:%02x ", |
| 77 | pci_domain_nr(pe->pbus), pe->pbus->number); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 78 | #ifdef CONFIG_PCI_IOV |
| 79 | else if (pe->flags & PNV_IODA_PE_VF) |
| 80 | sprintf(pfix, "%04x:%02x:%2x.%d", |
| 81 | pci_domain_nr(pe->parent_dev->bus), |
| 82 | (pe->rid & 0xff00) >> 8, |
| 83 | PCI_SLOT(pe->rid), PCI_FUNC(pe->rid)); |
| 84 | #endif /* CONFIG_PCI_IOV*/ |
Joe Perches | 6d31c2fa | 2014-09-21 10:55:06 -0700 | [diff] [blame] | 85 | |
| 86 | printk("%spci %s: [PE# %.3d] %pV", |
| 87 | level, pfix, pe->pe_number, &vaf); |
| 88 | |
| 89 | va_end(args); |
| 90 | } |
| 91 | |
Thadeu Lima de Souza Cascardo | 4e28784 | 2014-10-23 19:19:35 -0200 | [diff] [blame] | 92 | static bool pnv_iommu_bypass_disabled __read_mostly; |
| 93 | |
| 94 | static int __init iommu_setup(char *str) |
| 95 | { |
| 96 | if (!str) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | while (*str) { |
| 100 | if (!strncmp(str, "nobypass", 8)) { |
| 101 | pnv_iommu_bypass_disabled = true; |
| 102 | pr_info("PowerNV: IOMMU bypass window disabled.\n"); |
| 103 | break; |
| 104 | } |
| 105 | str += strcspn(str, ","); |
| 106 | if (*str == ',') |
| 107 | str++; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | early_param("iommu", iommu_setup); |
| 113 | |
Benjamin Herrenschmidt | 5958d19 | 2016-07-08 15:55:43 +1000 | [diff] [blame] | 114 | static inline bool pnv_pci_is_m64(struct pnv_phb *phb, struct resource *r) |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 115 | { |
Benjamin Herrenschmidt | 5958d19 | 2016-07-08 15:55:43 +1000 | [diff] [blame] | 116 | /* |
| 117 | * WARNING: We cannot rely on the resource flags. The Linux PCI |
| 118 | * allocation code sometimes decides to put a 64-bit prefetchable |
| 119 | * BAR in the 32-bit window, so we have to compare the addresses. |
| 120 | * |
| 121 | * For simplicity we only test resource start. |
| 122 | */ |
| 123 | return (r->start >= phb->ioda.m64_base && |
| 124 | r->start < (phb->ioda.m64_base + phb->ioda.m64_size)); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 125 | } |
| 126 | |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 127 | static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no) |
| 128 | { |
| 129 | phb->ioda.pe_array[pe_no].phb = phb; |
| 130 | phb->ioda.pe_array[pe_no].pe_number = pe_no; |
| 131 | |
| 132 | return &phb->ioda.pe_array[pe_no]; |
| 133 | } |
| 134 | |
Gavin Shan | 4b82ab1 | 2014-11-12 13:36:07 +1100 | [diff] [blame] | 135 | static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no) |
| 136 | { |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 137 | if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) { |
Gavin Shan | 4b82ab1 | 2014-11-12 13:36:07 +1100 | [diff] [blame] | 138 | pr_warn("%s: Invalid PE %d on PHB#%x\n", |
| 139 | __func__, pe_no, phb->hose->global_number); |
| 140 | return; |
| 141 | } |
| 142 | |
Gavin Shan | e9dc4d7 | 2015-06-19 12:26:16 +1000 | [diff] [blame] | 143 | if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) |
| 144 | pr_debug("%s: PE %d was reserved on PHB#%x\n", |
| 145 | __func__, pe_no, phb->hose->global_number); |
Gavin Shan | 4b82ab1 | 2014-11-12 13:36:07 +1100 | [diff] [blame] | 146 | |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 147 | pnv_ioda_init_pe(phb, pe_no); |
Gavin Shan | 4b82ab1 | 2014-11-12 13:36:07 +1100 | [diff] [blame] | 148 | } |
| 149 | |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 150 | static struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 151 | { |
Andrzej Hajda | 6096481 | 2016-08-17 12:03:05 +0200 | [diff] [blame] | 152 | long pe; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 153 | |
Gavin Shan | 9fcd6f4 | 2016-05-20 16:41:30 +1000 | [diff] [blame] | 154 | for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) { |
| 155 | if (!test_and_set_bit(pe, phb->ioda.pe_alloc)) |
| 156 | return pnv_ioda_init_pe(phb, pe); |
| 157 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 158 | |
Gavin Shan | 9fcd6f4 | 2016-05-20 16:41:30 +1000 | [diff] [blame] | 159 | return NULL; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 162 | static void pnv_ioda_free_pe(struct pnv_ioda_pe *pe) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 163 | { |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 164 | struct pnv_phb *phb = pe->phb; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 165 | |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 166 | WARN_ON(pe->pdev); |
| 167 | |
| 168 | memset(pe, 0, sizeof(struct pnv_ioda_pe)); |
| 169 | clear_bit(pe->pe_number, phb->ioda.pe_alloc); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 172 | /* The default M64 BAR is shared by all PEs */ |
| 173 | static int pnv_ioda2_init_m64(struct pnv_phb *phb) |
| 174 | { |
| 175 | const char *desc; |
| 176 | struct resource *r; |
| 177 | s64 rc; |
| 178 | |
| 179 | /* Configure the default M64 BAR */ |
| 180 | rc = opal_pci_set_phb_mem_window(phb->opal_id, |
| 181 | OPAL_M64_WINDOW_TYPE, |
| 182 | phb->ioda.m64_bar_idx, |
| 183 | phb->ioda.m64_base, |
| 184 | 0, /* unused */ |
| 185 | phb->ioda.m64_size); |
| 186 | if (rc != OPAL_SUCCESS) { |
| 187 | desc = "configuring"; |
| 188 | goto fail; |
| 189 | } |
| 190 | |
| 191 | /* Enable the default M64 BAR */ |
| 192 | rc = opal_pci_phb_mmio_enable(phb->opal_id, |
| 193 | OPAL_M64_WINDOW_TYPE, |
| 194 | phb->ioda.m64_bar_idx, |
| 195 | OPAL_ENABLE_M64_SPLIT); |
| 196 | if (rc != OPAL_SUCCESS) { |
| 197 | desc = "enabling"; |
| 198 | goto fail; |
| 199 | } |
| 200 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 201 | /* |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 202 | * Exclude the segments for reserved and root bus PE, which |
| 203 | * are first or last two PEs. |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 204 | */ |
| 205 | r = &phb->hose->mem_resources[1]; |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 206 | if (phb->ioda.reserved_pe_idx == 0) |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 207 | r->start += (2 * phb->ioda.m64_segsize); |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 208 | else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 209 | r->end -= (2 * phb->ioda.m64_segsize); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 210 | else |
| 211 | pr_warn(" Cannot strip M64 segment for reserved PE#%d\n", |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 212 | phb->ioda.reserved_pe_idx); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 213 | |
| 214 | return 0; |
| 215 | |
| 216 | fail: |
| 217 | pr_warn(" Failure %lld %s M64 BAR#%d\n", |
| 218 | rc, desc, phb->ioda.m64_bar_idx); |
| 219 | opal_pci_phb_mmio_enable(phb->opal_id, |
| 220 | OPAL_M64_WINDOW_TYPE, |
| 221 | phb->ioda.m64_bar_idx, |
| 222 | OPAL_DISABLE_M64); |
| 223 | return -EIO; |
| 224 | } |
| 225 | |
Gavin Shan | c430670 | 2016-05-03 15:41:30 +1000 | [diff] [blame] | 226 | static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev, |
Gavin Shan | 96a2f92 | 2015-06-19 12:26:17 +1000 | [diff] [blame] | 227 | unsigned long *pe_bitmap) |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 228 | { |
Gavin Shan | 96a2f92 | 2015-06-19 12:26:17 +1000 | [diff] [blame] | 229 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 230 | struct pnv_phb *phb = hose->private_data; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 231 | struct resource *r; |
Gavin Shan | 96a2f92 | 2015-06-19 12:26:17 +1000 | [diff] [blame] | 232 | resource_size_t base, sgsz, start, end; |
| 233 | int segno, i; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 234 | |
Gavin Shan | 96a2f92 | 2015-06-19 12:26:17 +1000 | [diff] [blame] | 235 | base = phb->ioda.m64_base; |
| 236 | sgsz = phb->ioda.m64_segsize; |
| 237 | for (i = 0; i <= PCI_ROM_RESOURCE; i++) { |
| 238 | r = &pdev->resource[i]; |
Benjamin Herrenschmidt | 5958d19 | 2016-07-08 15:55:43 +1000 | [diff] [blame] | 239 | if (!r->parent || !pnv_pci_is_m64(phb, r)) |
Gavin Shan | 96a2f92 | 2015-06-19 12:26:17 +1000 | [diff] [blame] | 240 | continue; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 241 | |
Gavin Shan | 96a2f92 | 2015-06-19 12:26:17 +1000 | [diff] [blame] | 242 | start = _ALIGN_DOWN(r->start - base, sgsz); |
| 243 | end = _ALIGN_UP(r->end - base, sgsz); |
| 244 | for (segno = start / sgsz; segno < end / sgsz; segno++) { |
| 245 | if (pe_bitmap) |
| 246 | set_bit(segno, pe_bitmap); |
| 247 | else |
| 248 | pnv_ioda_reserve_pe(phb, segno); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Gavin Shan | 9945155 | 2016-05-05 12:02:13 +1000 | [diff] [blame] | 253 | static int pnv_ioda1_init_m64(struct pnv_phb *phb) |
| 254 | { |
| 255 | struct resource *r; |
| 256 | int index; |
| 257 | |
| 258 | /* |
| 259 | * There are 16 M64 BARs, each of which has 8 segments. So |
| 260 | * there are as many M64 segments as the maximum number of |
| 261 | * PEs, which is 128. |
| 262 | */ |
| 263 | for (index = 0; index < PNV_IODA1_M64_NUM; index++) { |
| 264 | unsigned long base, segsz = phb->ioda.m64_segsize; |
| 265 | int64_t rc; |
| 266 | |
| 267 | base = phb->ioda.m64_base + |
| 268 | index * PNV_IODA1_M64_SEGS * segsz; |
| 269 | rc = opal_pci_set_phb_mem_window(phb->opal_id, |
| 270 | OPAL_M64_WINDOW_TYPE, index, base, 0, |
| 271 | PNV_IODA1_M64_SEGS * segsz); |
| 272 | if (rc != OPAL_SUCCESS) { |
| 273 | pr_warn(" Error %lld setting M64 PHB#%d-BAR#%d\n", |
| 274 | rc, phb->hose->global_number, index); |
| 275 | goto fail; |
| 276 | } |
| 277 | |
| 278 | rc = opal_pci_phb_mmio_enable(phb->opal_id, |
| 279 | OPAL_M64_WINDOW_TYPE, index, |
| 280 | OPAL_ENABLE_M64_SPLIT); |
| 281 | if (rc != OPAL_SUCCESS) { |
| 282 | pr_warn(" Error %lld enabling M64 PHB#%d-BAR#%d\n", |
| 283 | rc, phb->hose->global_number, index); |
| 284 | goto fail; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 289 | * Exclude the segments for reserved and root bus PE, which |
| 290 | * are first or last two PEs. |
Gavin Shan | 9945155 | 2016-05-05 12:02:13 +1000 | [diff] [blame] | 291 | */ |
| 292 | r = &phb->hose->mem_resources[1]; |
| 293 | if (phb->ioda.reserved_pe_idx == 0) |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 294 | r->start += (2 * phb->ioda.m64_segsize); |
Gavin Shan | 9945155 | 2016-05-05 12:02:13 +1000 | [diff] [blame] | 295 | else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 296 | r->end -= (2 * phb->ioda.m64_segsize); |
Gavin Shan | 9945155 | 2016-05-05 12:02:13 +1000 | [diff] [blame] | 297 | else |
| 298 | WARN(1, "Wrong reserved PE#%d on PHB#%d\n", |
| 299 | phb->ioda.reserved_pe_idx, phb->hose->global_number); |
| 300 | |
| 301 | return 0; |
| 302 | |
| 303 | fail: |
| 304 | for ( ; index >= 0; index--) |
| 305 | opal_pci_phb_mmio_enable(phb->opal_id, |
| 306 | OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64); |
| 307 | |
| 308 | return -EIO; |
| 309 | } |
| 310 | |
Gavin Shan | c430670 | 2016-05-03 15:41:30 +1000 | [diff] [blame] | 311 | static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus, |
| 312 | unsigned long *pe_bitmap, |
| 313 | bool all) |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 314 | { |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 315 | struct pci_dev *pdev; |
Gavin Shan | 96a2f92 | 2015-06-19 12:26:17 +1000 | [diff] [blame] | 316 | |
| 317 | list_for_each_entry(pdev, &bus->devices, bus_list) { |
Gavin Shan | c430670 | 2016-05-03 15:41:30 +1000 | [diff] [blame] | 318 | pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap); |
Gavin Shan | 96a2f92 | 2015-06-19 12:26:17 +1000 | [diff] [blame] | 319 | |
| 320 | if (all && pdev->subordinate) |
Gavin Shan | c430670 | 2016-05-03 15:41:30 +1000 | [diff] [blame] | 321 | pnv_ioda_reserve_m64_pe(pdev->subordinate, |
| 322 | pe_bitmap, all); |
Gavin Shan | 96a2f92 | 2015-06-19 12:26:17 +1000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 326 | static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all) |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 327 | { |
Gavin Shan | 26ba248 | 2015-06-19 12:26:19 +1000 | [diff] [blame] | 328 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 329 | struct pnv_phb *phb = hose->private_data; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 330 | struct pnv_ioda_pe *master_pe, *pe; |
| 331 | unsigned long size, *pe_alloc; |
Gavin Shan | 26ba248 | 2015-06-19 12:26:19 +1000 | [diff] [blame] | 332 | int i; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 333 | |
| 334 | /* Root bus shouldn't use M64 */ |
| 335 | if (pci_is_root_bus(bus)) |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 336 | return NULL; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 337 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 338 | /* Allocate bitmap */ |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 339 | size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long)); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 340 | pe_alloc = kzalloc(size, GFP_KERNEL); |
| 341 | if (!pe_alloc) { |
| 342 | pr_warn("%s: Out of memory !\n", |
| 343 | __func__); |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 344 | return NULL; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 345 | } |
| 346 | |
Gavin Shan | 26ba248 | 2015-06-19 12:26:19 +1000 | [diff] [blame] | 347 | /* Figure out reserved PE numbers by the PE */ |
Gavin Shan | c430670 | 2016-05-03 15:41:30 +1000 | [diff] [blame] | 348 | pnv_ioda_reserve_m64_pe(bus, pe_alloc, all); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 349 | |
| 350 | /* |
| 351 | * the current bus might not own M64 window and that's all |
| 352 | * contributed by its child buses. For the case, we needn't |
| 353 | * pick M64 dependent PE#. |
| 354 | */ |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 355 | if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) { |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 356 | kfree(pe_alloc); |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 357 | return NULL; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Figure out the master PE and put all slave PEs to master |
| 362 | * PE's list to form compound PE. |
| 363 | */ |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 364 | master_pe = NULL; |
| 365 | i = -1; |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 366 | while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) < |
| 367 | phb->ioda.total_pe_num) { |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 368 | pe = &phb->ioda.pe_array[i]; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 369 | |
Gavin Shan | 93289d8 | 2016-05-03 15:41:29 +1000 | [diff] [blame] | 370 | phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 371 | if (!master_pe) { |
| 372 | pe->flags |= PNV_IODA_PE_MASTER; |
| 373 | INIT_LIST_HEAD(&pe->slaves); |
| 374 | master_pe = pe; |
| 375 | } else { |
| 376 | pe->flags |= PNV_IODA_PE_SLAVE; |
| 377 | pe->master = master_pe; |
| 378 | list_add_tail(&pe->list, &master_pe->slaves); |
| 379 | } |
Gavin Shan | 9945155 | 2016-05-05 12:02:13 +1000 | [diff] [blame] | 380 | |
| 381 | /* |
| 382 | * P7IOC supports M64DT, which helps mapping M64 segment |
| 383 | * to one particular PE#. However, PHB3 has fixed mapping |
| 384 | * between M64 segment and PE#. In order to have same logic |
| 385 | * for P7IOC and PHB3, we enforce fixed mapping between M64 |
| 386 | * segment and PE# on P7IOC. |
| 387 | */ |
| 388 | if (phb->type == PNV_PHB_IODA1) { |
| 389 | int64_t rc; |
| 390 | |
| 391 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 392 | pe->pe_number, OPAL_M64_WINDOW_TYPE, |
| 393 | pe->pe_number / PNV_IODA1_M64_SEGS, |
| 394 | pe->pe_number % PNV_IODA1_M64_SEGS); |
| 395 | if (rc != OPAL_SUCCESS) |
| 396 | pr_warn("%s: Error %lld mapping M64 for PHB#%d-PE#%d\n", |
| 397 | __func__, rc, phb->hose->global_number, |
| 398 | pe->pe_number); |
| 399 | } |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | kfree(pe_alloc); |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 403 | return master_pe; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb) |
| 407 | { |
| 408 | struct pci_controller *hose = phb->hose; |
| 409 | struct device_node *dn = hose->dn; |
| 410 | struct resource *res; |
Benjamin Herrenschmidt | a1339fa | 2016-07-08 16:37:16 +1000 | [diff] [blame] | 411 | u32 m64_range[2], i; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 412 | const u32 *r; |
| 413 | u64 pci_addr; |
| 414 | |
Gavin Shan | 9945155 | 2016-05-05 12:02:13 +1000 | [diff] [blame] | 415 | if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) { |
Gavin Shan | 1665c4a | 2014-11-12 13:36:04 +1100 | [diff] [blame] | 416 | pr_info(" Not support M64 window\n"); |
| 417 | return; |
| 418 | } |
| 419 | |
Stewart Smith | e4d54f7 | 2015-12-09 17:18:20 +1100 | [diff] [blame] | 420 | if (!firmware_has_feature(FW_FEATURE_OPAL)) { |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 421 | pr_info(" Firmware too old to support M64 window\n"); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | r = of_get_property(dn, "ibm,opal-m64-window", NULL); |
| 426 | if (!r) { |
| 427 | pr_info(" No <ibm,opal-m64-window> on %s\n", |
| 428 | dn->full_name); |
| 429 | return; |
| 430 | } |
| 431 | |
Benjamin Herrenschmidt | a1339fa | 2016-07-08 16:37:16 +1000 | [diff] [blame] | 432 | /* |
| 433 | * Find the available M64 BAR range and pickup the last one for |
| 434 | * covering the whole 64-bits space. We support only one range. |
| 435 | */ |
| 436 | if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges", |
| 437 | m64_range, 2)) { |
| 438 | /* In absence of the property, assume 0..15 */ |
| 439 | m64_range[0] = 0; |
| 440 | m64_range[1] = 16; |
| 441 | } |
| 442 | /* We only support 64 bits in our allocator */ |
| 443 | if (m64_range[1] > 63) { |
| 444 | pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n", |
| 445 | __func__, m64_range[1], phb->hose->global_number); |
| 446 | m64_range[1] = 63; |
| 447 | } |
| 448 | /* Empty range, no m64 */ |
| 449 | if (m64_range[1] <= m64_range[0]) { |
| 450 | pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n", |
| 451 | __func__, phb->hose->global_number); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | /* Configure M64 informations */ |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 456 | res = &hose->mem_resources[1]; |
Gavin Shan | e80c4e7 | 2015-10-22 12:03:08 +1100 | [diff] [blame] | 457 | res->name = dn->full_name; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 458 | res->start = of_translate_address(dn, r + 2); |
| 459 | res->end = res->start + of_read_number(r + 4, 2) - 1; |
| 460 | res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH); |
| 461 | pci_addr = of_read_number(r, 2); |
| 462 | hose->mem_offset[1] = res->start - pci_addr; |
| 463 | |
| 464 | phb->ioda.m64_size = resource_size(res); |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 465 | phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 466 | phb->ioda.m64_base = pci_addr; |
| 467 | |
Benjamin Herrenschmidt | a1339fa | 2016-07-08 16:37:16 +1000 | [diff] [blame] | 468 | /* This lines up nicely with the display from processing OF ranges */ |
| 469 | pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n", |
| 470 | res->start, res->end, pci_addr, m64_range[0], |
| 471 | m64_range[0] + m64_range[1] - 1); |
| 472 | |
| 473 | /* Mark all M64 used up by default */ |
| 474 | phb->ioda.m64_bar_alloc = (unsigned long)-1; |
Wei Yang | e9863e6 | 2014-12-12 12:39:37 +0800 | [diff] [blame] | 475 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 476 | /* Use last M64 BAR to cover M64 window */ |
Benjamin Herrenschmidt | a1339fa | 2016-07-08 16:37:16 +1000 | [diff] [blame] | 477 | m64_range[1]--; |
| 478 | phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1]; |
| 479 | |
| 480 | pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx); |
| 481 | |
| 482 | /* Mark remaining ones free */ |
| 483 | for (i = m64_range[0]; i < m64_range[1]; i++) |
| 484 | clear_bit(i, &phb->ioda.m64_bar_alloc); |
| 485 | |
| 486 | /* |
| 487 | * Setup init functions for M64 based on IODA version, IODA3 uses |
| 488 | * the IODA2 code. |
| 489 | */ |
Gavin Shan | 9945155 | 2016-05-05 12:02:13 +1000 | [diff] [blame] | 490 | if (phb->type == PNV_PHB_IODA1) |
| 491 | phb->init_m64 = pnv_ioda1_init_m64; |
| 492 | else |
| 493 | phb->init_m64 = pnv_ioda2_init_m64; |
Gavin Shan | c430670 | 2016-05-03 15:41:30 +1000 | [diff] [blame] | 494 | phb->reserve_m64_pe = pnv_ioda_reserve_m64_pe; |
| 495 | phb->pick_m64_pe = pnv_ioda_pick_m64_pe; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 496 | } |
| 497 | |
Gavin Shan | 49dec92 | 2014-07-21 14:42:33 +1000 | [diff] [blame] | 498 | static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no) |
| 499 | { |
| 500 | struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no]; |
| 501 | struct pnv_ioda_pe *slave; |
| 502 | s64 rc; |
| 503 | |
| 504 | /* Fetch master PE */ |
| 505 | if (pe->flags & PNV_IODA_PE_SLAVE) { |
| 506 | pe = pe->master; |
Gavin Shan | ec8e4e9 | 2014-11-12 13:36:10 +1100 | [diff] [blame] | 507 | if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER))) |
| 508 | return; |
| 509 | |
Gavin Shan | 49dec92 | 2014-07-21 14:42:33 +1000 | [diff] [blame] | 510 | pe_no = pe->pe_number; |
| 511 | } |
| 512 | |
| 513 | /* Freeze master PE */ |
| 514 | rc = opal_pci_eeh_freeze_set(phb->opal_id, |
| 515 | pe_no, |
| 516 | OPAL_EEH_ACTION_SET_FREEZE_ALL); |
| 517 | if (rc != OPAL_SUCCESS) { |
| 518 | pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n", |
| 519 | __func__, rc, phb->hose->global_number, pe_no); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | /* Freeze slave PEs */ |
| 524 | if (!(pe->flags & PNV_IODA_PE_MASTER)) |
| 525 | return; |
| 526 | |
| 527 | list_for_each_entry(slave, &pe->slaves, list) { |
| 528 | rc = opal_pci_eeh_freeze_set(phb->opal_id, |
| 529 | slave->pe_number, |
| 530 | OPAL_EEH_ACTION_SET_FREEZE_ALL); |
| 531 | if (rc != OPAL_SUCCESS) |
| 532 | pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n", |
| 533 | __func__, rc, phb->hose->global_number, |
| 534 | slave->pe_number); |
| 535 | } |
| 536 | } |
| 537 | |
Anton Blanchard | e51df2c | 2014-08-20 08:55:18 +1000 | [diff] [blame] | 538 | static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt) |
Gavin Shan | 49dec92 | 2014-07-21 14:42:33 +1000 | [diff] [blame] | 539 | { |
| 540 | struct pnv_ioda_pe *pe, *slave; |
| 541 | s64 rc; |
| 542 | |
| 543 | /* Find master PE */ |
| 544 | pe = &phb->ioda.pe_array[pe_no]; |
| 545 | if (pe->flags & PNV_IODA_PE_SLAVE) { |
| 546 | pe = pe->master; |
| 547 | WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)); |
| 548 | pe_no = pe->pe_number; |
| 549 | } |
| 550 | |
| 551 | /* Clear frozen state for master PE */ |
| 552 | rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt); |
| 553 | if (rc != OPAL_SUCCESS) { |
| 554 | pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n", |
| 555 | __func__, rc, opt, phb->hose->global_number, pe_no); |
| 556 | return -EIO; |
| 557 | } |
| 558 | |
| 559 | if (!(pe->flags & PNV_IODA_PE_MASTER)) |
| 560 | return 0; |
| 561 | |
| 562 | /* Clear frozen state for slave PEs */ |
| 563 | list_for_each_entry(slave, &pe->slaves, list) { |
| 564 | rc = opal_pci_eeh_freeze_clear(phb->opal_id, |
| 565 | slave->pe_number, |
| 566 | opt); |
| 567 | if (rc != OPAL_SUCCESS) { |
| 568 | pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n", |
| 569 | __func__, rc, opt, phb->hose->global_number, |
| 570 | slave->pe_number); |
| 571 | return -EIO; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no) |
| 579 | { |
| 580 | struct pnv_ioda_pe *slave, *pe; |
| 581 | u8 fstate, state; |
| 582 | __be16 pcierr; |
| 583 | s64 rc; |
| 584 | |
| 585 | /* Sanity check on PE number */ |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 586 | if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num) |
Gavin Shan | 49dec92 | 2014-07-21 14:42:33 +1000 | [diff] [blame] | 587 | return OPAL_EEH_STOPPED_PERM_UNAVAIL; |
| 588 | |
| 589 | /* |
| 590 | * Fetch the master PE and the PE instance might be |
| 591 | * not initialized yet. |
| 592 | */ |
| 593 | pe = &phb->ioda.pe_array[pe_no]; |
| 594 | if (pe->flags & PNV_IODA_PE_SLAVE) { |
| 595 | pe = pe->master; |
| 596 | WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)); |
| 597 | pe_no = pe->pe_number; |
| 598 | } |
| 599 | |
| 600 | /* Check the master PE */ |
| 601 | rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, |
| 602 | &state, &pcierr, NULL); |
| 603 | if (rc != OPAL_SUCCESS) { |
| 604 | pr_warn("%s: Failure %lld getting " |
| 605 | "PHB#%x-PE#%x state\n", |
| 606 | __func__, rc, |
| 607 | phb->hose->global_number, pe_no); |
| 608 | return OPAL_EEH_STOPPED_TEMP_UNAVAIL; |
| 609 | } |
| 610 | |
| 611 | /* Check the slave PE */ |
| 612 | if (!(pe->flags & PNV_IODA_PE_MASTER)) |
| 613 | return state; |
| 614 | |
| 615 | list_for_each_entry(slave, &pe->slaves, list) { |
| 616 | rc = opal_pci_eeh_freeze_status(phb->opal_id, |
| 617 | slave->pe_number, |
| 618 | &fstate, |
| 619 | &pcierr, |
| 620 | NULL); |
| 621 | if (rc != OPAL_SUCCESS) { |
| 622 | pr_warn("%s: Failure %lld getting " |
| 623 | "PHB#%x-PE#%x state\n", |
| 624 | __func__, rc, |
| 625 | phb->hose->global_number, slave->pe_number); |
| 626 | return OPAL_EEH_STOPPED_TEMP_UNAVAIL; |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Override the result based on the ascending |
| 631 | * priority. |
| 632 | */ |
| 633 | if (fstate > state) |
| 634 | state = fstate; |
| 635 | } |
| 636 | |
| 637 | return state; |
| 638 | } |
| 639 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 640 | /* Currently those 2 are only used when MSIs are enabled, this will change |
| 641 | * but in the meantime, we need to protect them to avoid warnings |
| 642 | */ |
| 643 | #ifdef CONFIG_PCI_MSI |
Ian Munsie | f456834 | 2016-07-14 07:17:00 +1000 | [diff] [blame] | 644 | struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 645 | { |
| 646 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 647 | struct pnv_phb *phb = hose->private_data; |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 648 | struct pci_dn *pdn = pci_get_pdn(dev); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 649 | |
| 650 | if (!pdn) |
| 651 | return NULL; |
| 652 | if (pdn->pe_number == IODA_INVALID_PE) |
| 653 | return NULL; |
| 654 | return &phb->ioda.pe_array[pdn->pe_number]; |
| 655 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 656 | #endif /* CONFIG_PCI_MSI */ |
| 657 | |
Gavin Shan | b131a84 | 2014-11-12 13:36:08 +1100 | [diff] [blame] | 658 | static int pnv_ioda_set_one_peltv(struct pnv_phb *phb, |
| 659 | struct pnv_ioda_pe *parent, |
| 660 | struct pnv_ioda_pe *child, |
| 661 | bool is_add) |
| 662 | { |
| 663 | const char *desc = is_add ? "adding" : "removing"; |
| 664 | uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN : |
| 665 | OPAL_REMOVE_PE_FROM_DOMAIN; |
| 666 | struct pnv_ioda_pe *slave; |
| 667 | long rc; |
| 668 | |
| 669 | /* Parent PE affects child PE */ |
| 670 | rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number, |
| 671 | child->pe_number, op); |
| 672 | if (rc != OPAL_SUCCESS) { |
| 673 | pe_warn(child, "OPAL error %ld %s to parent PELTV\n", |
| 674 | rc, desc); |
| 675 | return -ENXIO; |
| 676 | } |
| 677 | |
| 678 | if (!(child->flags & PNV_IODA_PE_MASTER)) |
| 679 | return 0; |
| 680 | |
| 681 | /* Compound case: parent PE affects slave PEs */ |
| 682 | list_for_each_entry(slave, &child->slaves, list) { |
| 683 | rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number, |
| 684 | slave->pe_number, op); |
| 685 | if (rc != OPAL_SUCCESS) { |
| 686 | pe_warn(slave, "OPAL error %ld %s to parent PELTV\n", |
| 687 | rc, desc); |
| 688 | return -ENXIO; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | static int pnv_ioda_set_peltv(struct pnv_phb *phb, |
| 696 | struct pnv_ioda_pe *pe, |
| 697 | bool is_add) |
| 698 | { |
| 699 | struct pnv_ioda_pe *slave; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 700 | struct pci_dev *pdev = NULL; |
Gavin Shan | b131a84 | 2014-11-12 13:36:08 +1100 | [diff] [blame] | 701 | int ret; |
| 702 | |
| 703 | /* |
| 704 | * Clear PE frozen state. If it's master PE, we need |
| 705 | * clear slave PE frozen state as well. |
| 706 | */ |
| 707 | if (is_add) { |
| 708 | opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number, |
| 709 | OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); |
| 710 | if (pe->flags & PNV_IODA_PE_MASTER) { |
| 711 | list_for_each_entry(slave, &pe->slaves, list) |
| 712 | opal_pci_eeh_freeze_clear(phb->opal_id, |
| 713 | slave->pe_number, |
| 714 | OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * Associate PE in PELT. We need add the PE into the |
| 720 | * corresponding PELT-V as well. Otherwise, the error |
| 721 | * originated from the PE might contribute to other |
| 722 | * PEs. |
| 723 | */ |
| 724 | ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add); |
| 725 | if (ret) |
| 726 | return ret; |
| 727 | |
| 728 | /* For compound PEs, any one affects all of them */ |
| 729 | if (pe->flags & PNV_IODA_PE_MASTER) { |
| 730 | list_for_each_entry(slave, &pe->slaves, list) { |
| 731 | ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add); |
| 732 | if (ret) |
| 733 | return ret; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS)) |
| 738 | pdev = pe->pbus->self; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 739 | else if (pe->flags & PNV_IODA_PE_DEV) |
Gavin Shan | b131a84 | 2014-11-12 13:36:08 +1100 | [diff] [blame] | 740 | pdev = pe->pdev->bus->self; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 741 | #ifdef CONFIG_PCI_IOV |
| 742 | else if (pe->flags & PNV_IODA_PE_VF) |
Gavin Shan | 283e2d8 | 2015-06-22 13:45:47 +1000 | [diff] [blame] | 743 | pdev = pe->parent_dev; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 744 | #endif /* CONFIG_PCI_IOV */ |
Gavin Shan | b131a84 | 2014-11-12 13:36:08 +1100 | [diff] [blame] | 745 | while (pdev) { |
| 746 | struct pci_dn *pdn = pci_get_pdn(pdev); |
| 747 | struct pnv_ioda_pe *parent; |
| 748 | |
| 749 | if (pdn && pdn->pe_number != IODA_INVALID_PE) { |
| 750 | parent = &phb->ioda.pe_array[pdn->pe_number]; |
| 751 | ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add); |
| 752 | if (ret) |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | pdev = pdev->bus->self; |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 762 | static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe) |
| 763 | { |
| 764 | struct pci_dev *parent; |
| 765 | uint8_t bcomp, dcomp, fcomp; |
| 766 | int64_t rc; |
| 767 | long rid_end, rid; |
| 768 | |
| 769 | /* Currently, we just deconfigure VF PE. Bus PE will always there.*/ |
| 770 | if (pe->pbus) { |
| 771 | int count; |
| 772 | |
| 773 | dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER; |
| 774 | fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER; |
| 775 | parent = pe->pbus->self; |
| 776 | if (pe->flags & PNV_IODA_PE_BUS_ALL) |
| 777 | count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1; |
| 778 | else |
| 779 | count = 1; |
| 780 | |
| 781 | switch(count) { |
| 782 | case 1: bcomp = OpalPciBusAll; break; |
| 783 | case 2: bcomp = OpalPciBus7Bits; break; |
| 784 | case 4: bcomp = OpalPciBus6Bits; break; |
| 785 | case 8: bcomp = OpalPciBus5Bits; break; |
| 786 | case 16: bcomp = OpalPciBus4Bits; break; |
| 787 | case 32: bcomp = OpalPciBus3Bits; break; |
| 788 | default: |
| 789 | dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n", |
| 790 | count); |
| 791 | /* Do an exact match only */ |
| 792 | bcomp = OpalPciBusAll; |
| 793 | } |
| 794 | rid_end = pe->rid + (count << 8); |
| 795 | } else { |
Gavin Shan | 93e01a5 | 2016-05-20 16:41:34 +1000 | [diff] [blame] | 796 | #ifdef CONFIG_PCI_IOV |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 797 | if (pe->flags & PNV_IODA_PE_VF) |
| 798 | parent = pe->parent_dev; |
| 799 | else |
Gavin Shan | 93e01a5 | 2016-05-20 16:41:34 +1000 | [diff] [blame] | 800 | #endif |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 801 | parent = pe->pdev->bus->self; |
| 802 | bcomp = OpalPciBusAll; |
| 803 | dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER; |
| 804 | fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER; |
| 805 | rid_end = pe->rid + 1; |
| 806 | } |
| 807 | |
| 808 | /* Clear the reverse map */ |
| 809 | for (rid = pe->rid; rid < rid_end; rid++) |
Gavin Shan | c127562 | 2016-05-20 16:41:29 +1000 | [diff] [blame] | 810 | phb->ioda.pe_rmap[rid] = IODA_INVALID_PE; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 811 | |
| 812 | /* Release from all parents PELT-V */ |
| 813 | while (parent) { |
| 814 | struct pci_dn *pdn = pci_get_pdn(parent); |
| 815 | if (pdn && pdn->pe_number != IODA_INVALID_PE) { |
| 816 | rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number, |
| 817 | pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN); |
| 818 | /* XXX What to do in case of error ? */ |
| 819 | } |
| 820 | parent = parent->bus->self; |
| 821 | } |
| 822 | |
Gavin Shan | f951e51 | 2015-06-23 17:01:13 +1000 | [diff] [blame] | 823 | opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number, |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 824 | OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); |
| 825 | |
| 826 | /* Disassociate PE in PELT */ |
| 827 | rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number, |
| 828 | pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN); |
| 829 | if (rc) |
| 830 | pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc); |
| 831 | rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid, |
| 832 | bcomp, dcomp, fcomp, OPAL_UNMAP_PE); |
| 833 | if (rc) |
| 834 | pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc); |
| 835 | |
| 836 | pe->pbus = NULL; |
| 837 | pe->pdev = NULL; |
Gavin Shan | 93e01a5 | 2016-05-20 16:41:34 +1000 | [diff] [blame] | 838 | #ifdef CONFIG_PCI_IOV |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 839 | pe->parent_dev = NULL; |
Gavin Shan | 93e01a5 | 2016-05-20 16:41:34 +1000 | [diff] [blame] | 840 | #endif |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 844 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 845 | static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 846 | { |
| 847 | struct pci_dev *parent; |
| 848 | uint8_t bcomp, dcomp, fcomp; |
| 849 | long rc, rid_end, rid; |
| 850 | |
| 851 | /* Bus validation ? */ |
| 852 | if (pe->pbus) { |
| 853 | int count; |
| 854 | |
| 855 | dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER; |
| 856 | fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER; |
| 857 | parent = pe->pbus->self; |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 858 | if (pe->flags & PNV_IODA_PE_BUS_ALL) |
| 859 | count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1; |
| 860 | else |
| 861 | count = 1; |
| 862 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 863 | switch(count) { |
| 864 | case 1: bcomp = OpalPciBusAll; break; |
| 865 | case 2: bcomp = OpalPciBus7Bits; break; |
| 866 | case 4: bcomp = OpalPciBus6Bits; break; |
| 867 | case 8: bcomp = OpalPciBus5Bits; break; |
| 868 | case 16: bcomp = OpalPciBus4Bits; break; |
| 869 | case 32: bcomp = OpalPciBus3Bits; break; |
| 870 | default: |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 871 | dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n", |
| 872 | count); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 873 | /* Do an exact match only */ |
| 874 | bcomp = OpalPciBusAll; |
| 875 | } |
| 876 | rid_end = pe->rid + (count << 8); |
| 877 | } else { |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 878 | #ifdef CONFIG_PCI_IOV |
| 879 | if (pe->flags & PNV_IODA_PE_VF) |
| 880 | parent = pe->parent_dev; |
| 881 | else |
| 882 | #endif /* CONFIG_PCI_IOV */ |
| 883 | parent = pe->pdev->bus->self; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 884 | bcomp = OpalPciBusAll; |
| 885 | dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER; |
| 886 | fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER; |
| 887 | rid_end = pe->rid + 1; |
| 888 | } |
| 889 | |
Gavin Shan | 631ad69 | 2013-11-04 16:32:46 +0800 | [diff] [blame] | 890 | /* |
| 891 | * Associate PE in PELT. We need add the PE into the |
| 892 | * corresponding PELT-V as well. Otherwise, the error |
| 893 | * originated from the PE might contribute to other |
| 894 | * PEs. |
| 895 | */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 896 | rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid, |
| 897 | bcomp, dcomp, fcomp, OPAL_MAP_PE); |
| 898 | if (rc) { |
| 899 | pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc); |
| 900 | return -ENXIO; |
| 901 | } |
Gavin Shan | 631ad69 | 2013-11-04 16:32:46 +0800 | [diff] [blame] | 902 | |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 903 | /* |
| 904 | * Configure PELTV. NPUs don't have a PELTV table so skip |
| 905 | * configuration on them. |
| 906 | */ |
| 907 | if (phb->type != PNV_PHB_NPU) |
| 908 | pnv_ioda_set_peltv(phb, pe, true); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 909 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 910 | /* Setup reverse map */ |
| 911 | for (rid = pe->rid; rid < rid_end; rid++) |
| 912 | phb->ioda.pe_rmap[rid] = pe->pe_number; |
| 913 | |
| 914 | /* Setup one MVTs on IODA1 */ |
Gavin Shan | 4773f76 | 2014-11-12 13:36:09 +1100 | [diff] [blame] | 915 | if (phb->type != PNV_PHB_IODA1) { |
| 916 | pe->mve_number = 0; |
| 917 | goto out; |
| 918 | } |
| 919 | |
| 920 | pe->mve_number = pe->pe_number; |
| 921 | rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number); |
| 922 | if (rc != OPAL_SUCCESS) { |
| 923 | pe_err(pe, "OPAL error %ld setting up MVE %d\n", |
| 924 | rc, pe->mve_number); |
| 925 | pe->mve_number = -1; |
| 926 | } else { |
| 927 | rc = opal_pci_set_mve_enable(phb->opal_id, |
| 928 | pe->mve_number, OPAL_ENABLE_MVE); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 929 | if (rc) { |
Gavin Shan | 4773f76 | 2014-11-12 13:36:09 +1100 | [diff] [blame] | 930 | pe_err(pe, "OPAL error %ld enabling MVE %d\n", |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 931 | rc, pe->mve_number); |
| 932 | pe->mve_number = -1; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 933 | } |
Gavin Shan | 4773f76 | 2014-11-12 13:36:09 +1100 | [diff] [blame] | 934 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 935 | |
Gavin Shan | 4773f76 | 2014-11-12 13:36:09 +1100 | [diff] [blame] | 936 | out: |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 937 | return 0; |
| 938 | } |
| 939 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 940 | #ifdef CONFIG_PCI_IOV |
| 941 | static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset) |
| 942 | { |
| 943 | struct pci_dn *pdn = pci_get_pdn(dev); |
| 944 | int i; |
| 945 | struct resource *res, res2; |
| 946 | resource_size_t size; |
| 947 | u16 num_vfs; |
| 948 | |
| 949 | if (!dev->is_physfn) |
| 950 | return -EINVAL; |
| 951 | |
| 952 | /* |
| 953 | * "offset" is in VFs. The M64 windows are sized so that when they |
| 954 | * are segmented, each segment is the same size as the IOV BAR. |
| 955 | * Each segment is in a separate PE, and the high order bits of the |
| 956 | * address are the PE number. Therefore, each VF's BAR is in a |
| 957 | * separate PE, and changing the IOV BAR start address changes the |
| 958 | * range of PEs the VFs are in. |
| 959 | */ |
| 960 | num_vfs = pdn->num_vfs; |
| 961 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 962 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
| 963 | if (!res->flags || !res->parent) |
| 964 | continue; |
| 965 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 966 | /* |
| 967 | * The actual IOV BAR range is determined by the start address |
| 968 | * and the actual size for num_vfs VFs BAR. This check is to |
| 969 | * make sure that after shifting, the range will not overlap |
| 970 | * with another device. |
| 971 | */ |
| 972 | size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES); |
| 973 | res2.flags = res->flags; |
| 974 | res2.start = res->start + (size * offset); |
| 975 | res2.end = res2.start + (size * num_vfs) - 1; |
| 976 | |
| 977 | if (res2.end > res->end) { |
| 978 | dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n", |
| 979 | i, &res2, res, num_vfs, offset); |
| 980 | return -EBUSY; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * After doing so, there would be a "hole" in the /proc/iomem when |
| 986 | * offset is a positive value. It looks like the device return some |
| 987 | * mmio back to the system, which actually no one could use it. |
| 988 | */ |
| 989 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 990 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
| 991 | if (!res->flags || !res->parent) |
| 992 | continue; |
| 993 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 994 | size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES); |
| 995 | res2 = *res; |
| 996 | res->start += size * offset; |
| 997 | |
Wei Yang | 74703cc | 2015-07-20 18:14:58 +0800 | [diff] [blame] | 998 | dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n", |
| 999 | i, &res2, res, (offset > 0) ? "En" : "Dis", |
| 1000 | num_vfs, offset); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1001 | pci_update_resource(dev, i + PCI_IOV_RESOURCES); |
| 1002 | } |
| 1003 | return 0; |
| 1004 | } |
| 1005 | #endif /* CONFIG_PCI_IOV */ |
| 1006 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 1007 | static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1008 | { |
| 1009 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 1010 | struct pnv_phb *phb = hose->private_data; |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 1011 | struct pci_dn *pdn = pci_get_pdn(dev); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1012 | struct pnv_ioda_pe *pe; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1013 | |
| 1014 | if (!pdn) { |
| 1015 | pr_err("%s: Device tree node not associated properly\n", |
| 1016 | pci_name(dev)); |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | if (pdn->pe_number != IODA_INVALID_PE) |
| 1020 | return NULL; |
| 1021 | |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1022 | pe = pnv_ioda_alloc_pe(phb); |
| 1023 | if (!pe) { |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1024 | pr_warning("%s: Not enough PE# available, disabling device\n", |
| 1025 | pci_name(dev)); |
| 1026 | return NULL; |
| 1027 | } |
| 1028 | |
| 1029 | /* NOTE: We get only one ref to the pci_dev for the pdn, not for the |
| 1030 | * pointer in the PE data structure, both should be destroyed at the |
| 1031 | * same time. However, this needs to be looked at more closely again |
| 1032 | * once we actually start removing things (Hotplug, SR-IOV, ...) |
| 1033 | * |
| 1034 | * At some point we want to remove the PDN completely anyways |
| 1035 | */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1036 | pci_dev_get(dev); |
| 1037 | pdn->pcidev = dev; |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1038 | pdn->pe_number = pe->pe_number; |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 1039 | pe->flags = PNV_IODA_PE_DEV; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1040 | pe->pdev = dev; |
| 1041 | pe->pbus = NULL; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1042 | pe->mve_number = -1; |
| 1043 | pe->rid = dev->bus->number << 8 | pdn->devfn; |
| 1044 | |
| 1045 | pe_info(pe, "Associated device to PE\n"); |
| 1046 | |
| 1047 | if (pnv_ioda_configure_pe(phb, pe)) { |
| 1048 | /* XXX What do we do here ? */ |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1049 | pnv_ioda_free_pe(pe); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1050 | pdn->pe_number = IODA_INVALID_PE; |
| 1051 | pe->pdev = NULL; |
| 1052 | pci_dev_put(dev); |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | |
Alexey Kardashevskiy | 1d4e89c | 2016-05-12 15:47:10 +1000 | [diff] [blame] | 1056 | /* Put PE to the list */ |
| 1057 | list_add_tail(&pe->list, &phb->ioda.pe_list); |
| 1058 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1059 | return pe; |
| 1060 | } |
| 1061 | |
| 1062 | static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe) |
| 1063 | { |
| 1064 | struct pci_dev *dev; |
| 1065 | |
| 1066 | list_for_each_entry(dev, &bus->devices, bus_list) { |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 1067 | struct pci_dn *pdn = pci_get_pdn(dev); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1068 | |
| 1069 | if (pdn == NULL) { |
| 1070 | pr_warn("%s: No device node associated with device !\n", |
| 1071 | pci_name(dev)); |
| 1072 | continue; |
| 1073 | } |
Gavin Shan | ccd1c19 | 2016-05-20 16:41:31 +1000 | [diff] [blame] | 1074 | |
| 1075 | /* |
| 1076 | * In partial hotplug case, the PCI device might be still |
| 1077 | * associated with the PE and needn't attach it to the PE |
| 1078 | * again. |
| 1079 | */ |
| 1080 | if (pdn->pe_number != IODA_INVALID_PE) |
| 1081 | continue; |
| 1082 | |
Gavin Shan | c5f7700 | 2016-05-20 16:41:35 +1000 | [diff] [blame] | 1083 | pe->device_count++; |
Alistair Popple | 94973b2 | 2015-12-17 13:43:11 +1100 | [diff] [blame] | 1084 | pdn->pcidev = dev; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1085 | pdn->pe_number = pe->pe_number; |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1086 | if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1087 | pnv_ioda_setup_same_PE(dev->subordinate, pe); |
| 1088 | } |
| 1089 | } |
| 1090 | |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1091 | /* |
| 1092 | * There're 2 types of PCI bus sensitive PEs: One that is compromised of |
| 1093 | * single PCI bus. Another one that contains the primary PCI bus and its |
| 1094 | * subordinate PCI devices and buses. The second type of PE is normally |
| 1095 | * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports. |
| 1096 | */ |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1097 | static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1098 | { |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1099 | struct pci_controller *hose = pci_bus_to_host(bus); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1100 | struct pnv_phb *phb = hose->private_data; |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1101 | struct pnv_ioda_pe *pe = NULL; |
Gavin Shan | ccd1c19 | 2016-05-20 16:41:31 +1000 | [diff] [blame] | 1102 | unsigned int pe_num; |
| 1103 | |
| 1104 | /* |
| 1105 | * In partial hotplug case, the PE instance might be still alive. |
| 1106 | * We should reuse it instead of allocating a new one. |
| 1107 | */ |
| 1108 | pe_num = phb->ioda.pe_rmap[bus->number << 8]; |
| 1109 | if (pe_num != IODA_INVALID_PE) { |
| 1110 | pe = &phb->ioda.pe_array[pe_num]; |
| 1111 | pnv_ioda_setup_same_PE(bus, pe); |
| 1112 | return NULL; |
| 1113 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1114 | |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 1115 | /* PE number for root bus should have been reserved */ |
| 1116 | if (pci_is_root_bus(bus) && |
| 1117 | phb->ioda.root_pe_idx != IODA_INVALID_PE) |
| 1118 | pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx]; |
| 1119 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1120 | /* Check if PE is determined by M64 */ |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 1121 | if (!pe && phb->pick_m64_pe) |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1122 | pe = phb->pick_m64_pe(bus, all); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1123 | |
| 1124 | /* The PE number isn't pinned by M64 */ |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1125 | if (!pe) |
| 1126 | pe = pnv_ioda_alloc_pe(phb); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1127 | |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1128 | if (!pe) { |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1129 | pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n", |
| 1130 | __func__, pci_domain_nr(bus), bus->number); |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1131 | return NULL; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1134 | pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1135 | pe->pbus = bus; |
| 1136 | pe->pdev = NULL; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1137 | pe->mve_number = -1; |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 1138 | pe->rid = bus->busn_res.start << 8; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1139 | |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1140 | if (all) |
| 1141 | pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n", |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1142 | bus->busn_res.start, bus->busn_res.end, pe->pe_number); |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1143 | else |
| 1144 | pe_info(pe, "Secondary bus %d associated with PE#%d\n", |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1145 | bus->busn_res.start, pe->pe_number); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1146 | |
| 1147 | if (pnv_ioda_configure_pe(phb, pe)) { |
| 1148 | /* XXX What do we do here ? */ |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1149 | pnv_ioda_free_pe(pe); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1150 | pe->pbus = NULL; |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1151 | return NULL; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | /* Associate it with all child devices */ |
| 1155 | pnv_ioda_setup_same_PE(bus, pe); |
| 1156 | |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 1157 | /* Put PE to the list */ |
| 1158 | list_add_tail(&pe->list, &phb->ioda.pe_list); |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1159 | |
| 1160 | return pe; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Alistair Popple | b521549 | 2016-01-11 16:53:49 +1100 | [diff] [blame] | 1163 | static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev) |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 1164 | { |
Alistair Popple | b521549 | 2016-01-11 16:53:49 +1100 | [diff] [blame] | 1165 | int pe_num, found_pe = false, rc; |
| 1166 | long rid; |
| 1167 | struct pnv_ioda_pe *pe; |
| 1168 | struct pci_dev *gpu_pdev; |
| 1169 | struct pci_dn *npu_pdn; |
| 1170 | struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus); |
| 1171 | struct pnv_phb *phb = hose->private_data; |
| 1172 | |
| 1173 | /* |
| 1174 | * Due to a hardware errata PE#0 on the NPU is reserved for |
| 1175 | * error handling. This means we only have three PEs remaining |
| 1176 | * which need to be assigned to four links, implying some |
| 1177 | * links must share PEs. |
| 1178 | * |
| 1179 | * To achieve this we assign PEs such that NPUs linking the |
| 1180 | * same GPU get assigned the same PE. |
| 1181 | */ |
| 1182 | gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev); |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 1183 | for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) { |
Alistair Popple | b521549 | 2016-01-11 16:53:49 +1100 | [diff] [blame] | 1184 | pe = &phb->ioda.pe_array[pe_num]; |
| 1185 | if (!pe->pdev) |
| 1186 | continue; |
| 1187 | |
| 1188 | if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) { |
| 1189 | /* |
| 1190 | * This device has the same peer GPU so should |
| 1191 | * be assigned the same PE as the existing |
| 1192 | * peer NPU. |
| 1193 | */ |
| 1194 | dev_info(&npu_pdev->dev, |
| 1195 | "Associating to existing PE %d\n", pe_num); |
| 1196 | pci_dev_get(npu_pdev); |
| 1197 | npu_pdn = pci_get_pdn(npu_pdev); |
| 1198 | rid = npu_pdev->bus->number << 8 | npu_pdn->devfn; |
| 1199 | npu_pdn->pcidev = npu_pdev; |
| 1200 | npu_pdn->pe_number = pe_num; |
Alistair Popple | b521549 | 2016-01-11 16:53:49 +1100 | [diff] [blame] | 1201 | phb->ioda.pe_rmap[rid] = pe->pe_number; |
| 1202 | |
| 1203 | /* Map the PE to this link */ |
| 1204 | rc = opal_pci_set_pe(phb->opal_id, pe_num, rid, |
| 1205 | OpalPciBusAll, |
| 1206 | OPAL_COMPARE_RID_DEVICE_NUMBER, |
| 1207 | OPAL_COMPARE_RID_FUNCTION_NUMBER, |
| 1208 | OPAL_MAP_PE); |
| 1209 | WARN_ON(rc != OPAL_SUCCESS); |
| 1210 | found_pe = true; |
| 1211 | break; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | if (!found_pe) |
| 1216 | /* |
| 1217 | * Could not find an existing PE so allocate a new |
| 1218 | * one. |
| 1219 | */ |
| 1220 | return pnv_ioda_setup_dev_PE(npu_pdev); |
| 1221 | else |
| 1222 | return pe; |
| 1223 | } |
| 1224 | |
| 1225 | static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus) |
| 1226 | { |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 1227 | struct pci_dev *pdev; |
| 1228 | |
| 1229 | list_for_each_entry(pdev, &bus->devices, bus_list) |
Alistair Popple | b521549 | 2016-01-11 16:53:49 +1100 | [diff] [blame] | 1230 | pnv_ioda_setup_npu_PE(pdev); |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 1231 | } |
| 1232 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 1233 | static void pnv_pci_ioda_setup_PEs(void) |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1234 | { |
| 1235 | struct pci_controller *hose, *tmp; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1236 | struct pnv_phb *phb; |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1237 | |
| 1238 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1239 | phb = hose->private_data; |
Alistair Popple | 08f48f3 | 2016-01-11 16:53:50 +1100 | [diff] [blame] | 1240 | if (phb->type == PNV_PHB_NPU) { |
| 1241 | /* PE#0 is needed for error reporting */ |
| 1242 | pnv_ioda_reserve_pe(phb, 0); |
Alistair Popple | b521549 | 2016-01-11 16:53:49 +1100 | [diff] [blame] | 1243 | pnv_ioda_setup_npu_PEs(hose->bus); |
Gavin Shan | ccd1c19 | 2016-05-20 16:41:31 +1000 | [diff] [blame] | 1244 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | |
Gavin Shan | a8b2f82 | 2015-03-25 16:23:52 +0800 | [diff] [blame] | 1248 | #ifdef CONFIG_PCI_IOV |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1249 | static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs) |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1250 | { |
| 1251 | struct pci_bus *bus; |
| 1252 | struct pci_controller *hose; |
| 1253 | struct pnv_phb *phb; |
| 1254 | struct pci_dn *pdn; |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1255 | int i, j; |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1256 | int m64_bars; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1257 | |
| 1258 | bus = pdev->bus; |
| 1259 | hose = pci_bus_to_host(bus); |
| 1260 | phb = hose->private_data; |
| 1261 | pdn = pci_get_pdn(pdev); |
| 1262 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1263 | if (pdn->m64_single_mode) |
| 1264 | m64_bars = num_vfs; |
| 1265 | else |
| 1266 | m64_bars = 1; |
| 1267 | |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1268 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1269 | for (j = 0; j < m64_bars; j++) { |
| 1270 | if (pdn->m64_map[j][i] == IODA_INVALID_M64) |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1271 | continue; |
| 1272 | opal_pci_phb_mmio_enable(phb->opal_id, |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1273 | OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0); |
| 1274 | clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc); |
| 1275 | pdn->m64_map[j][i] = IODA_INVALID_M64; |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1276 | } |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1277 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1278 | kfree(pdn->m64_map); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1279 | return 0; |
| 1280 | } |
| 1281 | |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1282 | static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs) |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1283 | { |
| 1284 | struct pci_bus *bus; |
| 1285 | struct pci_controller *hose; |
| 1286 | struct pnv_phb *phb; |
| 1287 | struct pci_dn *pdn; |
| 1288 | unsigned int win; |
| 1289 | struct resource *res; |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1290 | int i, j; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1291 | int64_t rc; |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1292 | int total_vfs; |
| 1293 | resource_size_t size, start; |
| 1294 | int pe_num; |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1295 | int m64_bars; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1296 | |
| 1297 | bus = pdev->bus; |
| 1298 | hose = pci_bus_to_host(bus); |
| 1299 | phb = hose->private_data; |
| 1300 | pdn = pci_get_pdn(pdev); |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1301 | total_vfs = pci_sriov_get_totalvfs(pdev); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1302 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1303 | if (pdn->m64_single_mode) |
| 1304 | m64_bars = num_vfs; |
| 1305 | else |
| 1306 | m64_bars = 1; |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1307 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1308 | pdn->m64_map = kmalloc(sizeof(*pdn->m64_map) * m64_bars, GFP_KERNEL); |
| 1309 | if (!pdn->m64_map) |
| 1310 | return -ENOMEM; |
| 1311 | /* Initialize the m64_map to IODA_INVALID_M64 */ |
| 1312 | for (i = 0; i < m64_bars ; i++) |
| 1313 | for (j = 0; j < PCI_SRIOV_NUM_BARS; j++) |
| 1314 | pdn->m64_map[i][j] = IODA_INVALID_M64; |
| 1315 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1316 | |
| 1317 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 1318 | res = &pdev->resource[i + PCI_IOV_RESOURCES]; |
| 1319 | if (!res->flags || !res->parent) |
| 1320 | continue; |
| 1321 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1322 | for (j = 0; j < m64_bars; j++) { |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1323 | do { |
| 1324 | win = find_next_zero_bit(&phb->ioda.m64_bar_alloc, |
| 1325 | phb->ioda.m64_bar_idx + 1, 0); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1326 | |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1327 | if (win >= phb->ioda.m64_bar_idx + 1) |
| 1328 | goto m64_failed; |
| 1329 | } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc)); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1330 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1331 | pdn->m64_map[j][i] = win; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1332 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1333 | if (pdn->m64_single_mode) { |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1334 | size = pci_iov_resource_size(pdev, |
| 1335 | PCI_IOV_RESOURCES + i); |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1336 | start = res->start + size * j; |
| 1337 | } else { |
| 1338 | size = resource_size(res); |
| 1339 | start = res->start; |
| 1340 | } |
| 1341 | |
| 1342 | /* Map the M64 here */ |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1343 | if (pdn->m64_single_mode) { |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1344 | pe_num = pdn->pe_num_map[j]; |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1345 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 1346 | pe_num, OPAL_M64_WINDOW_TYPE, |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1347 | pdn->m64_map[j][i], 0); |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | rc = opal_pci_set_phb_mem_window(phb->opal_id, |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1351 | OPAL_M64_WINDOW_TYPE, |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1352 | pdn->m64_map[j][i], |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1353 | start, |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1354 | 0, /* unused */ |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1355 | size); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1356 | |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1357 | |
| 1358 | if (rc != OPAL_SUCCESS) { |
| 1359 | dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n", |
| 1360 | win, rc); |
| 1361 | goto m64_failed; |
| 1362 | } |
| 1363 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1364 | if (pdn->m64_single_mode) |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1365 | rc = opal_pci_phb_mmio_enable(phb->opal_id, |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1366 | OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2); |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1367 | else |
| 1368 | rc = opal_pci_phb_mmio_enable(phb->opal_id, |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1369 | OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1); |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1370 | |
| 1371 | if (rc != OPAL_SUCCESS) { |
| 1372 | dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n", |
| 1373 | win, rc); |
| 1374 | goto m64_failed; |
| 1375 | } |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | return 0; |
| 1379 | |
| 1380 | m64_failed: |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1381 | pnv_pci_vf_release_m64(pdev, num_vfs); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1382 | return -EBUSY; |
| 1383 | } |
| 1384 | |
Alexey Kardashevskiy | c035e37 | 2015-06-05 16:35:21 +1000 | [diff] [blame] | 1385 | static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group, |
| 1386 | int num); |
| 1387 | static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable); |
| 1388 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1389 | static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe) |
| 1390 | { |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1391 | struct iommu_table *tbl; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1392 | int64_t rc; |
| 1393 | |
Alexey Kardashevskiy | b348aa6 | 2015-06-05 16:35:08 +1000 | [diff] [blame] | 1394 | tbl = pe->table_group.tables[0]; |
Alexey Kardashevskiy | c035e37 | 2015-06-05 16:35:21 +1000 | [diff] [blame] | 1395 | rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1396 | if (rc) |
| 1397 | pe_warn(pe, "OPAL error %ld release DMA window\n", rc); |
| 1398 | |
Alexey Kardashevskiy | c035e37 | 2015-06-05 16:35:21 +1000 | [diff] [blame] | 1399 | pnv_pci_ioda2_set_bypass(pe, false); |
Alexey Kardashevskiy | 0eaf4de | 2015-06-05 16:35:09 +1000 | [diff] [blame] | 1400 | if (pe->table_group.group) { |
| 1401 | iommu_group_put(pe->table_group.group); |
| 1402 | BUG_ON(pe->table_group.group); |
Alexey Kardashevskiy | ac9a588 | 2015-06-05 16:34:56 +1000 | [diff] [blame] | 1403 | } |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 1404 | pnv_pci_ioda2_table_free_pages(tbl); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1405 | iommu_free_table(tbl, of_node_full_name(dev->dev.of_node)); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1406 | } |
| 1407 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1408 | static void pnv_ioda_release_vf_PE(struct pci_dev *pdev) |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1409 | { |
| 1410 | struct pci_bus *bus; |
| 1411 | struct pci_controller *hose; |
| 1412 | struct pnv_phb *phb; |
| 1413 | struct pnv_ioda_pe *pe, *pe_n; |
| 1414 | struct pci_dn *pdn; |
| 1415 | |
| 1416 | bus = pdev->bus; |
| 1417 | hose = pci_bus_to_host(bus); |
| 1418 | phb = hose->private_data; |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1419 | pdn = pci_get_pdn(pdev); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1420 | |
| 1421 | if (!pdev->is_physfn) |
| 1422 | return; |
| 1423 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1424 | list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) { |
| 1425 | if (pe->parent_dev != pdev) |
| 1426 | continue; |
| 1427 | |
| 1428 | pnv_pci_ioda2_release_dma_pe(pdev, pe); |
| 1429 | |
| 1430 | /* Remove from list */ |
| 1431 | mutex_lock(&phb->ioda.pe_list_mutex); |
| 1432 | list_del(&pe->list); |
| 1433 | mutex_unlock(&phb->ioda.pe_list_mutex); |
| 1434 | |
| 1435 | pnv_ioda_deconfigure_pe(phb, pe); |
| 1436 | |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1437 | pnv_ioda_free_pe(pe); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | void pnv_pci_sriov_disable(struct pci_dev *pdev) |
| 1442 | { |
| 1443 | struct pci_bus *bus; |
| 1444 | struct pci_controller *hose; |
| 1445 | struct pnv_phb *phb; |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1446 | struct pnv_ioda_pe *pe; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1447 | struct pci_dn *pdn; |
| 1448 | struct pci_sriov *iov; |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1449 | u16 num_vfs, i; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1450 | |
| 1451 | bus = pdev->bus; |
| 1452 | hose = pci_bus_to_host(bus); |
| 1453 | phb = hose->private_data; |
| 1454 | pdn = pci_get_pdn(pdev); |
| 1455 | iov = pdev->sriov; |
| 1456 | num_vfs = pdn->num_vfs; |
| 1457 | |
| 1458 | /* Release VF PEs */ |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1459 | pnv_ioda_release_vf_PE(pdev); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1460 | |
| 1461 | if (phb->type == PNV_PHB_IODA2) { |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1462 | if (!pdn->m64_single_mode) |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1463 | pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1464 | |
| 1465 | /* Release M64 windows */ |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1466 | pnv_pci_vf_release_m64(pdev, num_vfs); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1467 | |
| 1468 | /* Release PE numbers */ |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1469 | if (pdn->m64_single_mode) { |
| 1470 | for (i = 0; i < num_vfs; i++) { |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1471 | if (pdn->pe_num_map[i] == IODA_INVALID_PE) |
| 1472 | continue; |
| 1473 | |
| 1474 | pe = &phb->ioda.pe_array[pdn->pe_num_map[i]]; |
| 1475 | pnv_ioda_free_pe(pe); |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1476 | } |
| 1477 | } else |
| 1478 | bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs); |
| 1479 | /* Releasing pe_num_map */ |
| 1480 | kfree(pdn->pe_num_map); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb, |
| 1485 | struct pnv_ioda_pe *pe); |
| 1486 | static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs) |
| 1487 | { |
| 1488 | struct pci_bus *bus; |
| 1489 | struct pci_controller *hose; |
| 1490 | struct pnv_phb *phb; |
| 1491 | struct pnv_ioda_pe *pe; |
| 1492 | int pe_num; |
| 1493 | u16 vf_index; |
| 1494 | struct pci_dn *pdn; |
| 1495 | |
| 1496 | bus = pdev->bus; |
| 1497 | hose = pci_bus_to_host(bus); |
| 1498 | phb = hose->private_data; |
| 1499 | pdn = pci_get_pdn(pdev); |
| 1500 | |
| 1501 | if (!pdev->is_physfn) |
| 1502 | return; |
| 1503 | |
| 1504 | /* Reserve PE for each VF */ |
| 1505 | for (vf_index = 0; vf_index < num_vfs; vf_index++) { |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1506 | if (pdn->m64_single_mode) |
| 1507 | pe_num = pdn->pe_num_map[vf_index]; |
| 1508 | else |
| 1509 | pe_num = *pdn->pe_num_map + vf_index; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1510 | |
| 1511 | pe = &phb->ioda.pe_array[pe_num]; |
| 1512 | pe->pe_number = pe_num; |
| 1513 | pe->phb = phb; |
| 1514 | pe->flags = PNV_IODA_PE_VF; |
| 1515 | pe->pbus = NULL; |
| 1516 | pe->parent_dev = pdev; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1517 | pe->mve_number = -1; |
| 1518 | pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) | |
| 1519 | pci_iov_virtfn_devfn(pdev, vf_index); |
| 1520 | |
| 1521 | pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n", |
| 1522 | hose->global_number, pdev->bus->number, |
| 1523 | PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)), |
| 1524 | PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num); |
| 1525 | |
| 1526 | if (pnv_ioda_configure_pe(phb, pe)) { |
| 1527 | /* XXX What do we do here ? */ |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1528 | pnv_ioda_free_pe(pe); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1529 | pe->pdev = NULL; |
| 1530 | continue; |
| 1531 | } |
| 1532 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1533 | /* Put PE to the list */ |
| 1534 | mutex_lock(&phb->ioda.pe_list_mutex); |
| 1535 | list_add_tail(&pe->list, &phb->ioda.pe_list); |
| 1536 | mutex_unlock(&phb->ioda.pe_list_mutex); |
| 1537 | |
| 1538 | pnv_pci_ioda2_setup_dma_pe(phb, pe); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs) |
| 1543 | { |
| 1544 | struct pci_bus *bus; |
| 1545 | struct pci_controller *hose; |
| 1546 | struct pnv_phb *phb; |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1547 | struct pnv_ioda_pe *pe; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1548 | struct pci_dn *pdn; |
| 1549 | int ret; |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1550 | u16 i; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1551 | |
| 1552 | bus = pdev->bus; |
| 1553 | hose = pci_bus_to_host(bus); |
| 1554 | phb = hose->private_data; |
| 1555 | pdn = pci_get_pdn(pdev); |
| 1556 | |
| 1557 | if (phb->type == PNV_PHB_IODA2) { |
Wei Yang | b033185 | 2015-10-22 09:22:14 +0800 | [diff] [blame] | 1558 | if (!pdn->vfs_expanded) { |
| 1559 | dev_info(&pdev->dev, "don't support this SRIOV device" |
| 1560 | " with non 64bit-prefetchable IOV BAR\n"); |
| 1561 | return -ENOSPC; |
| 1562 | } |
| 1563 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1564 | /* |
| 1565 | * When M64 BARs functions in Single PE mode, the number of VFs |
| 1566 | * could be enabled must be less than the number of M64 BARs. |
| 1567 | */ |
| 1568 | if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) { |
| 1569 | dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n"); |
| 1570 | return -EBUSY; |
| 1571 | } |
| 1572 | |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1573 | /* Allocating pe_num_map */ |
| 1574 | if (pdn->m64_single_mode) |
| 1575 | pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map) * num_vfs, |
| 1576 | GFP_KERNEL); |
| 1577 | else |
| 1578 | pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL); |
| 1579 | |
| 1580 | if (!pdn->pe_num_map) |
| 1581 | return -ENOMEM; |
| 1582 | |
| 1583 | if (pdn->m64_single_mode) |
| 1584 | for (i = 0; i < num_vfs; i++) |
| 1585 | pdn->pe_num_map[i] = IODA_INVALID_PE; |
| 1586 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1587 | /* Calculate available PE for required VFs */ |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1588 | if (pdn->m64_single_mode) { |
| 1589 | for (i = 0; i < num_vfs; i++) { |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1590 | pe = pnv_ioda_alloc_pe(phb); |
| 1591 | if (!pe) { |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1592 | ret = -EBUSY; |
| 1593 | goto m64_failed; |
| 1594 | } |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1595 | |
| 1596 | pdn->pe_num_map[i] = pe->pe_number; |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1597 | } |
| 1598 | } else { |
| 1599 | mutex_lock(&phb->ioda.pe_alloc_mutex); |
| 1600 | *pdn->pe_num_map = bitmap_find_next_zero_area( |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 1601 | phb->ioda.pe_alloc, phb->ioda.total_pe_num, |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1602 | 0, num_vfs, 0); |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 1603 | if (*pdn->pe_num_map >= phb->ioda.total_pe_num) { |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1604 | mutex_unlock(&phb->ioda.pe_alloc_mutex); |
| 1605 | dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs); |
| 1606 | kfree(pdn->pe_num_map); |
| 1607 | return -EBUSY; |
| 1608 | } |
| 1609 | bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1610 | mutex_unlock(&phb->ioda.pe_alloc_mutex); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1611 | } |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1612 | pdn->num_vfs = num_vfs; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1613 | |
| 1614 | /* Assign M64 window accordingly */ |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1615 | ret = pnv_pci_vf_assign_m64(pdev, num_vfs); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1616 | if (ret) { |
| 1617 | dev_info(&pdev->dev, "Not enough M64 window resources\n"); |
| 1618 | goto m64_failed; |
| 1619 | } |
| 1620 | |
| 1621 | /* |
| 1622 | * When using one M64 BAR to map one IOV BAR, we need to shift |
| 1623 | * the IOV BAR according to the PE# allocated to the VFs. |
| 1624 | * Otherwise, the PE# for the VF will conflict with others. |
| 1625 | */ |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1626 | if (!pdn->m64_single_mode) { |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1627 | ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map); |
Wei Yang | 02639b0 | 2015-03-25 16:23:59 +0800 | [diff] [blame] | 1628 | if (ret) |
| 1629 | goto m64_failed; |
| 1630 | } |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | /* Setup VF PEs */ |
| 1634 | pnv_ioda_setup_vf_PE(pdev, num_vfs); |
| 1635 | |
| 1636 | return 0; |
| 1637 | |
| 1638 | m64_failed: |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1639 | if (pdn->m64_single_mode) { |
| 1640 | for (i = 0; i < num_vfs; i++) { |
Gavin Shan | 1e91677 | 2016-05-03 15:41:36 +1000 | [diff] [blame] | 1641 | if (pdn->pe_num_map[i] == IODA_INVALID_PE) |
| 1642 | continue; |
| 1643 | |
| 1644 | pe = &phb->ioda.pe_array[pdn->pe_num_map[i]]; |
| 1645 | pnv_ioda_free_pe(pe); |
Wei Yang | be283ee | 2015-10-22 09:22:19 +0800 | [diff] [blame] | 1646 | } |
| 1647 | } else |
| 1648 | bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs); |
| 1649 | |
| 1650 | /* Releasing pe_num_map */ |
| 1651 | kfree(pdn->pe_num_map); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1652 | |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
Gavin Shan | a8b2f82 | 2015-03-25 16:23:52 +0800 | [diff] [blame] | 1656 | int pcibios_sriov_disable(struct pci_dev *pdev) |
| 1657 | { |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1658 | pnv_pci_sriov_disable(pdev); |
| 1659 | |
Gavin Shan | a8b2f82 | 2015-03-25 16:23:52 +0800 | [diff] [blame] | 1660 | /* Release PCI data */ |
| 1661 | remove_dev_pci_data(pdev); |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs) |
| 1666 | { |
| 1667 | /* Allocate PCI data */ |
| 1668 | add_dev_pci_data(pdev); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 1669 | |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 1670 | return pnv_pci_sriov_enable(pdev, num_vfs); |
Gavin Shan | a8b2f82 | 2015-03-25 16:23:52 +0800 | [diff] [blame] | 1671 | } |
| 1672 | #endif /* CONFIG_PCI_IOV */ |
| 1673 | |
Gavin Shan | 959c9bd | 2013-04-25 19:21:02 +0000 | [diff] [blame] | 1674 | static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1675 | { |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 1676 | struct pci_dn *pdn = pci_get_pdn(pdev); |
Gavin Shan | 959c9bd | 2013-04-25 19:21:02 +0000 | [diff] [blame] | 1677 | struct pnv_ioda_pe *pe; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1678 | |
Gavin Shan | 959c9bd | 2013-04-25 19:21:02 +0000 | [diff] [blame] | 1679 | /* |
| 1680 | * The function can be called while the PE# |
| 1681 | * hasn't been assigned. Do nothing for the |
| 1682 | * case. |
| 1683 | */ |
| 1684 | if (!pdn || pdn->pe_number == IODA_INVALID_PE) |
| 1685 | return; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1686 | |
Gavin Shan | 959c9bd | 2013-04-25 19:21:02 +0000 | [diff] [blame] | 1687 | pe = &phb->ioda.pe_array[pdn->pe_number]; |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1688 | WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops); |
Alexey Kardashevskiy | 0e1ffef | 2015-08-27 16:01:16 +1000 | [diff] [blame] | 1689 | set_dma_offset(&pdev->dev, pe->tce_bypass_base); |
Alexey Kardashevskiy | b348aa6 | 2015-06-05 16:35:08 +1000 | [diff] [blame] | 1690 | set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]); |
Alexey Kardashevskiy | 4617082 | 2015-06-05 16:34:54 +1000 | [diff] [blame] | 1691 | /* |
| 1692 | * Note: iommu_add_device() will fail here as |
| 1693 | * for physical PE: the device is already added by now; |
| 1694 | * for virtual PE: sysfs entries are not ready yet and |
| 1695 | * tce_iommu_bus_notifier will add the device to a group later. |
| 1696 | */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Daniel Axtens | 763d2d8 | 2015-04-28 15:12:07 +1000 | [diff] [blame] | 1699 | static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask) |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1700 | { |
Daniel Axtens | 763d2d8 | 2015-04-28 15:12:07 +1000 | [diff] [blame] | 1701 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 1702 | struct pnv_phb *phb = hose->private_data; |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1703 | struct pci_dn *pdn = pci_get_pdn(pdev); |
| 1704 | struct pnv_ioda_pe *pe; |
| 1705 | uint64_t top; |
| 1706 | bool bypass = false; |
| 1707 | |
| 1708 | if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE)) |
| 1709 | return -ENODEV;; |
| 1710 | |
| 1711 | pe = &phb->ioda.pe_array[pdn->pe_number]; |
| 1712 | if (pe->tce_bypass_enabled) { |
| 1713 | top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1; |
| 1714 | bypass = (dma_mask >= top); |
| 1715 | } |
| 1716 | |
| 1717 | if (bypass) { |
| 1718 | dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n"); |
| 1719 | set_dma_ops(&pdev->dev, &dma_direct_ops); |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1720 | } else { |
| 1721 | dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n"); |
| 1722 | set_dma_ops(&pdev->dev, &dma_iommu_ops); |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1723 | } |
Brian W Hart | a32305b | 2014-07-31 14:24:37 -0500 | [diff] [blame] | 1724 | *pdev->dev.dma_mask = dma_mask; |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 1725 | |
| 1726 | /* Update peer npu devices */ |
Alexey Kardashevskiy | f9f8345 | 2016-04-29 18:55:20 +1000 | [diff] [blame] | 1727 | pnv_npu_try_dma_set_bypass(pdev, bypass); |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 1728 | |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1729 | return 0; |
| 1730 | } |
| 1731 | |
Andrew Donnellan | 53522982 | 2015-08-07 13:45:54 +1000 | [diff] [blame] | 1732 | static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev) |
Gavin Shan | fe7e85c | 2014-09-30 12:39:10 +1000 | [diff] [blame] | 1733 | { |
Andrew Donnellan | 53522982 | 2015-08-07 13:45:54 +1000 | [diff] [blame] | 1734 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 1735 | struct pnv_phb *phb = hose->private_data; |
Gavin Shan | fe7e85c | 2014-09-30 12:39:10 +1000 | [diff] [blame] | 1736 | struct pci_dn *pdn = pci_get_pdn(pdev); |
| 1737 | struct pnv_ioda_pe *pe; |
| 1738 | u64 end, mask; |
| 1739 | |
| 1740 | if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE)) |
| 1741 | return 0; |
| 1742 | |
| 1743 | pe = &phb->ioda.pe_array[pdn->pe_number]; |
| 1744 | if (!pe->tce_bypass_enabled) |
| 1745 | return __dma_get_required_mask(&pdev->dev); |
| 1746 | |
| 1747 | |
| 1748 | end = pe->tce_bypass_base + memblock_end_of_DRAM(); |
| 1749 | mask = 1ULL << (fls64(end) - 1); |
| 1750 | mask += mask - 1; |
| 1751 | |
| 1752 | return mask; |
| 1753 | } |
| 1754 | |
Gavin Shan | dff4a39 | 2014-07-15 17:00:55 +1000 | [diff] [blame] | 1755 | static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, |
Alexey Kardashevskiy | ea30e99 | 2015-06-05 16:34:53 +1000 | [diff] [blame] | 1756 | struct pci_bus *bus) |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 1757 | { |
| 1758 | struct pci_dev *dev; |
| 1759 | |
| 1760 | list_for_each_entry(dev, &bus->devices, bus_list) { |
Alexey Kardashevskiy | b348aa6 | 2015-06-05 16:35:08 +1000 | [diff] [blame] | 1761 | set_iommu_table_base(&dev->dev, pe->table_group.tables[0]); |
Benjamin Herrenschmidt | e91c2511 | 2015-06-24 15:25:27 +1000 | [diff] [blame] | 1762 | set_dma_offset(&dev->dev, pe->tce_bypass_base); |
Alexey Kardashevskiy | 4617082 | 2015-06-05 16:34:54 +1000 | [diff] [blame] | 1763 | iommu_add_device(&dev->dev); |
Gavin Shan | dff4a39 | 2014-07-15 17:00:55 +1000 | [diff] [blame] | 1764 | |
Alexey Kardashevskiy | 5c89a87 | 2015-06-18 11:41:36 +1000 | [diff] [blame] | 1765 | if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate) |
Alexey Kardashevskiy | ea30e99 | 2015-06-05 16:34:53 +1000 | [diff] [blame] | 1766 | pnv_ioda_setup_bus_dma(pe, dev->subordinate); |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 1770 | static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb, |
| 1771 | bool real_mode) |
| 1772 | { |
| 1773 | return real_mode ? (__be64 __iomem *)(phb->regs_phys + 0x210) : |
| 1774 | (phb->regs + 0x210); |
| 1775 | } |
| 1776 | |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 1777 | static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table *tbl, |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1778 | unsigned long index, unsigned long npages, bool rm) |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1779 | { |
Alexey Kardashevskiy | 0eaf4de | 2015-06-05 16:35:09 +1000 | [diff] [blame] | 1780 | struct iommu_table_group_link *tgl = list_first_entry_or_null( |
| 1781 | &tbl->it_group_list, struct iommu_table_group_link, |
| 1782 | next); |
| 1783 | struct pnv_ioda_pe *pe = container_of(tgl->table_group, |
Alexey Kardashevskiy | b348aa6 | 2015-06-05 16:35:08 +1000 | [diff] [blame] | 1784 | struct pnv_ioda_pe, table_group); |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 1785 | __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1786 | unsigned long start, end, inc; |
| 1787 | |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1788 | start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset); |
| 1789 | end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset + |
| 1790 | npages - 1); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1791 | |
Benjamin Herrenschmidt | 08acce1 | 2016-07-08 16:37:13 +1000 | [diff] [blame] | 1792 | /* p7ioc-style invalidation, 2 TCEs per write */ |
| 1793 | start |= (1ull << 63); |
| 1794 | end |= (1ull << 63); |
| 1795 | inc = 16; |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1796 | end |= inc - 1; /* round up end to be different than start */ |
| 1797 | |
| 1798 | mb(); /* Ensure above stores are visible */ |
| 1799 | while (start <= end) { |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1800 | if (rm) |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 1801 | __raw_rm_writeq(cpu_to_be64(start), invalidate); |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1802 | else |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 1803 | __raw_writeq(cpu_to_be64(start), invalidate); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1804 | start += inc; |
| 1805 | } |
| 1806 | |
| 1807 | /* |
| 1808 | * The iommu layer will do another mb() for us on build() |
| 1809 | * and we don't care on free() |
| 1810 | */ |
| 1811 | } |
| 1812 | |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1813 | static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index, |
| 1814 | long npages, unsigned long uaddr, |
| 1815 | enum dma_data_direction direction, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 1816 | unsigned long attrs) |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1817 | { |
| 1818 | int ret = pnv_tce_build(tbl, index, npages, uaddr, direction, |
| 1819 | attrs); |
| 1820 | |
Benjamin Herrenschmidt | 08acce1 | 2016-07-08 16:37:13 +1000 | [diff] [blame] | 1821 | if (!ret) |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 1822 | pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false); |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1823 | |
| 1824 | return ret; |
| 1825 | } |
| 1826 | |
Alexey Kardashevskiy | 05c6cfb | 2015-06-05 16:35:15 +1000 | [diff] [blame] | 1827 | #ifdef CONFIG_IOMMU_API |
| 1828 | static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index, |
| 1829 | unsigned long *hpa, enum dma_data_direction *direction) |
| 1830 | { |
| 1831 | long ret = pnv_tce_xchg(tbl, index, hpa, direction); |
| 1832 | |
Benjamin Herrenschmidt | 08acce1 | 2016-07-08 16:37:13 +1000 | [diff] [blame] | 1833 | if (!ret) |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 1834 | pnv_pci_p7ioc_tce_invalidate(tbl, index, 1, false); |
Alexey Kardashevskiy | 05c6cfb | 2015-06-05 16:35:15 +1000 | [diff] [blame] | 1835 | |
| 1836 | return ret; |
| 1837 | } |
| 1838 | #endif |
| 1839 | |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1840 | static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index, |
| 1841 | long npages) |
| 1842 | { |
| 1843 | pnv_tce_free(tbl, index, npages); |
| 1844 | |
Benjamin Herrenschmidt | 08acce1 | 2016-07-08 16:37:13 +1000 | [diff] [blame] | 1845 | pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false); |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1846 | } |
| 1847 | |
Alexey Kardashevskiy | da004c3 | 2015-06-05 16:35:06 +1000 | [diff] [blame] | 1848 | static struct iommu_table_ops pnv_ioda1_iommu_ops = { |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1849 | .set = pnv_ioda1_tce_build, |
Alexey Kardashevskiy | 05c6cfb | 2015-06-05 16:35:15 +1000 | [diff] [blame] | 1850 | #ifdef CONFIG_IOMMU_API |
| 1851 | .exchange = pnv_ioda1_tce_xchg, |
| 1852 | #endif |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1853 | .clear = pnv_ioda1_tce_free, |
Alexey Kardashevskiy | da004c3 | 2015-06-05 16:35:06 +1000 | [diff] [blame] | 1854 | .get = pnv_tce_get, |
| 1855 | }; |
| 1856 | |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 1857 | #define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0) |
| 1858 | #define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1) |
| 1859 | #define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2) |
Alexey Kardashevskiy | bef9253 | 2016-04-29 18:55:17 +1000 | [diff] [blame] | 1860 | |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 1861 | void pnv_pci_phb3_tce_invalidate_entire(struct pnv_phb *phb, bool rm) |
Alexey Kardashevskiy | 0bbcdb4 | 2016-04-29 18:55:18 +1000 | [diff] [blame] | 1862 | { |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 1863 | __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(phb, rm); |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 1864 | const unsigned long val = PHB3_TCE_KILL_INVAL_ALL; |
Alexey Kardashevskiy | 0bbcdb4 | 2016-04-29 18:55:18 +1000 | [diff] [blame] | 1865 | |
| 1866 | mb(); /* Ensure previous TCE table stores are visible */ |
| 1867 | if (rm) |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 1868 | __raw_rm_writeq(cpu_to_be64(val), invalidate); |
Alexey Kardashevskiy | 0bbcdb4 | 2016-04-29 18:55:18 +1000 | [diff] [blame] | 1869 | else |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 1870 | __raw_writeq(cpu_to_be64(val), invalidate); |
Alexey Kardashevskiy | 0bbcdb4 | 2016-04-29 18:55:18 +1000 | [diff] [blame] | 1871 | } |
| 1872 | |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 1873 | static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe) |
Alexey Kardashevskiy | 5780fb0 | 2015-06-05 16:35:12 +1000 | [diff] [blame] | 1874 | { |
| 1875 | /* 01xb - invalidate TCEs that match the specified PE# */ |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 1876 | __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, false); |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 1877 | unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF); |
Alexey Kardashevskiy | 5780fb0 | 2015-06-05 16:35:12 +1000 | [diff] [blame] | 1878 | |
| 1879 | mb(); /* Ensure above stores are visible */ |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 1880 | __raw_writeq(cpu_to_be64(val), invalidate); |
Alexey Kardashevskiy | 5780fb0 | 2015-06-05 16:35:12 +1000 | [diff] [blame] | 1881 | } |
| 1882 | |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 1883 | static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe, bool rm, |
| 1884 | unsigned shift, unsigned long index, |
| 1885 | unsigned long npages) |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1886 | { |
Alexey Kardashevskiy | 4d90219 | 2016-08-03 18:40:45 +1000 | [diff] [blame] | 1887 | __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1888 | unsigned long start, end, inc; |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1889 | |
| 1890 | /* We'll invalidate DMA address in PE scope */ |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 1891 | start = PHB3_TCE_KILL_INVAL_ONE; |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 1892 | start |= (pe->pe_number & 0xFF); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1893 | end = start; |
| 1894 | |
| 1895 | /* Figure out the start, end and step */ |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1896 | start |= (index << shift); |
| 1897 | end |= ((index + npages - 1) << shift); |
Alexey Kardashevskiy | b0376c9 | 2014-06-06 18:44:01 +1000 | [diff] [blame] | 1898 | inc = (0x1ull << shift); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1899 | mb(); |
| 1900 | |
| 1901 | while (start <= end) { |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1902 | if (rm) |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 1903 | __raw_rm_writeq(cpu_to_be64(start), invalidate); |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1904 | else |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 1905 | __raw_writeq(cpu_to_be64(start), invalidate); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1906 | start += inc; |
| 1907 | } |
| 1908 | } |
| 1909 | |
Benjamin Herrenschmidt | f0228c4 | 2016-07-08 16:37:15 +1000 | [diff] [blame] | 1910 | static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe) |
| 1911 | { |
| 1912 | struct pnv_phb *phb = pe->phb; |
| 1913 | |
| 1914 | if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs) |
| 1915 | pnv_pci_phb3_tce_invalidate_pe(pe); |
| 1916 | else |
| 1917 | opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE, |
| 1918 | pe->pe_number, 0, 0, 0); |
| 1919 | } |
| 1920 | |
Alexey Kardashevskiy | e57080f | 2015-06-05 16:35:13 +1000 | [diff] [blame] | 1921 | static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl, |
| 1922 | unsigned long index, unsigned long npages, bool rm) |
| 1923 | { |
| 1924 | struct iommu_table_group_link *tgl; |
| 1925 | |
| 1926 | list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) { |
| 1927 | struct pnv_ioda_pe *pe = container_of(tgl->table_group, |
| 1928 | struct pnv_ioda_pe, table_group); |
Benjamin Herrenschmidt | f0228c4 | 2016-07-08 16:37:15 +1000 | [diff] [blame] | 1929 | struct pnv_phb *phb = pe->phb; |
| 1930 | unsigned int shift = tbl->it_page_shift; |
| 1931 | |
| 1932 | if (phb->type == PNV_PHB_NPU) { |
Alexey Kardashevskiy | 0bbcdb4 | 2016-04-29 18:55:18 +1000 | [diff] [blame] | 1933 | /* |
| 1934 | * The NVLink hardware does not support TCE kill |
| 1935 | * per TCE entry so we have to invalidate |
| 1936 | * the entire cache for it. |
| 1937 | */ |
Benjamin Herrenschmidt | f0228c4 | 2016-07-08 16:37:15 +1000 | [diff] [blame] | 1938 | pnv_pci_phb3_tce_invalidate_entire(phb, rm); |
Alexey Kardashevskiy | 8567486 | 2016-04-29 18:55:23 +1000 | [diff] [blame] | 1939 | continue; |
| 1940 | } |
Benjamin Herrenschmidt | f0228c4 | 2016-07-08 16:37:15 +1000 | [diff] [blame] | 1941 | if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs) |
| 1942 | pnv_pci_phb3_tce_invalidate(pe, rm, shift, |
| 1943 | index, npages); |
| 1944 | else if (rm) |
| 1945 | opal_rm_pci_tce_kill(phb->opal_id, |
| 1946 | OPAL_PCI_TCE_KILL_PAGES, |
| 1947 | pe->pe_number, 1u << shift, |
| 1948 | index << shift, npages); |
| 1949 | else |
| 1950 | opal_pci_tce_kill(phb->opal_id, |
| 1951 | OPAL_PCI_TCE_KILL_PAGES, |
| 1952 | pe->pe_number, 1u << shift, |
| 1953 | index << shift, npages); |
Alexey Kardashevskiy | e57080f | 2015-06-05 16:35:13 +1000 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1957 | static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index, |
| 1958 | long npages, unsigned long uaddr, |
| 1959 | enum dma_data_direction direction, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 1960 | unsigned long attrs) |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1961 | { |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1962 | int ret = pnv_tce_build(tbl, index, npages, uaddr, direction, |
| 1963 | attrs); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1964 | |
Benjamin Herrenschmidt | 08acce1 | 2016-07-08 16:37:13 +1000 | [diff] [blame] | 1965 | if (!ret) |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1966 | pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false); |
| 1967 | |
| 1968 | return ret; |
| 1969 | } |
| 1970 | |
Alexey Kardashevskiy | 05c6cfb | 2015-06-05 16:35:15 +1000 | [diff] [blame] | 1971 | #ifdef CONFIG_IOMMU_API |
| 1972 | static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index, |
| 1973 | unsigned long *hpa, enum dma_data_direction *direction) |
| 1974 | { |
| 1975 | long ret = pnv_tce_xchg(tbl, index, hpa, direction); |
| 1976 | |
Benjamin Herrenschmidt | 08acce1 | 2016-07-08 16:37:13 +1000 | [diff] [blame] | 1977 | if (!ret) |
Alexey Kardashevskiy | 05c6cfb | 2015-06-05 16:35:15 +1000 | [diff] [blame] | 1978 | pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false); |
| 1979 | |
| 1980 | return ret; |
| 1981 | } |
| 1982 | #endif |
| 1983 | |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1984 | static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index, |
| 1985 | long npages) |
| 1986 | { |
| 1987 | pnv_tce_free(tbl, index, npages); |
| 1988 | |
Benjamin Herrenschmidt | 08acce1 | 2016-07-08 16:37:13 +1000 | [diff] [blame] | 1989 | pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 1992 | static void pnv_ioda2_table_free(struct iommu_table *tbl) |
| 1993 | { |
| 1994 | pnv_pci_ioda2_table_free_pages(tbl); |
| 1995 | iommu_free_table(tbl, "pnv"); |
| 1996 | } |
| 1997 | |
Alexey Kardashevskiy | da004c3 | 2015-06-05 16:35:06 +1000 | [diff] [blame] | 1998 | static struct iommu_table_ops pnv_ioda2_iommu_ops = { |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 1999 | .set = pnv_ioda2_tce_build, |
Alexey Kardashevskiy | 05c6cfb | 2015-06-05 16:35:15 +1000 | [diff] [blame] | 2000 | #ifdef CONFIG_IOMMU_API |
| 2001 | .exchange = pnv_ioda2_tce_xchg, |
| 2002 | #endif |
Alexey Kardashevskiy | decbda2 | 2015-06-05 16:35:07 +1000 | [diff] [blame] | 2003 | .clear = pnv_ioda2_tce_free, |
Alexey Kardashevskiy | da004c3 | 2015-06-05 16:35:06 +1000 | [diff] [blame] | 2004 | .get = pnv_tce_get, |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 2005 | .free = pnv_ioda2_table_free, |
Alexey Kardashevskiy | da004c3 | 2015-06-05 16:35:06 +1000 | [diff] [blame] | 2006 | }; |
| 2007 | |
Gavin Shan | 801846d | 2016-05-03 15:41:34 +1000 | [diff] [blame] | 2008 | static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data) |
| 2009 | { |
| 2010 | unsigned int *weight = (unsigned int *)data; |
| 2011 | |
| 2012 | /* This is quite simplistic. The "base" weight of a device |
| 2013 | * is 10. 0 means no DMA is to be accounted for it. |
| 2014 | */ |
| 2015 | if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) |
| 2016 | return 0; |
| 2017 | |
| 2018 | if (dev->class == PCI_CLASS_SERIAL_USB_UHCI || |
| 2019 | dev->class == PCI_CLASS_SERIAL_USB_OHCI || |
| 2020 | dev->class == PCI_CLASS_SERIAL_USB_EHCI) |
| 2021 | *weight += 3; |
| 2022 | else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID) |
| 2023 | *weight += 15; |
| 2024 | else |
| 2025 | *weight += 10; |
| 2026 | |
| 2027 | return 0; |
| 2028 | } |
| 2029 | |
| 2030 | static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe) |
| 2031 | { |
| 2032 | unsigned int weight = 0; |
| 2033 | |
| 2034 | /* SRIOV VF has same DMA32 weight as its PF */ |
| 2035 | #ifdef CONFIG_PCI_IOV |
| 2036 | if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) { |
| 2037 | pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight); |
| 2038 | return weight; |
| 2039 | } |
| 2040 | #endif |
| 2041 | |
| 2042 | if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) { |
| 2043 | pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight); |
| 2044 | } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) { |
| 2045 | struct pci_dev *pdev; |
| 2046 | |
| 2047 | list_for_each_entry(pdev, &pe->pbus->devices, bus_list) |
| 2048 | pnv_pci_ioda_dev_dma_weight(pdev, &weight); |
| 2049 | } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) { |
| 2050 | pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight); |
| 2051 | } |
| 2052 | |
| 2053 | return weight; |
| 2054 | } |
| 2055 | |
Gavin Shan | b30d936 | 2016-05-03 15:41:32 +1000 | [diff] [blame] | 2056 | static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb, |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 2057 | struct pnv_ioda_pe *pe) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2058 | { |
| 2059 | |
| 2060 | struct page *tce_mem = NULL; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2061 | struct iommu_table *tbl; |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 2062 | unsigned int weight, total_weight = 0; |
| 2063 | unsigned int tce32_segsz, base, segs, avail, i; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2064 | int64_t rc; |
| 2065 | void *addr; |
| 2066 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2067 | /* XXX FIXME: Handle 64-bit only DMA devices */ |
| 2068 | /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */ |
| 2069 | /* XXX FIXME: Allocate multi-level tables on PHB3 */ |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 2070 | weight = pnv_pci_ioda_pe_dma_weight(pe); |
| 2071 | if (!weight) |
| 2072 | return; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2073 | |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 2074 | pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight, |
| 2075 | &total_weight); |
| 2076 | segs = (weight * phb->ioda.dma32_count) / total_weight; |
| 2077 | if (!segs) |
| 2078 | segs = 1; |
| 2079 | |
| 2080 | /* |
| 2081 | * Allocate contiguous DMA32 segments. We begin with the expected |
| 2082 | * number of segments. With one more attempt, the number of DMA32 |
| 2083 | * segments to be allocated is decreased by one until one segment |
| 2084 | * is allocated successfully. |
| 2085 | */ |
| 2086 | do { |
| 2087 | for (base = 0; base <= phb->ioda.dma32_count - segs; base++) { |
| 2088 | for (avail = 0, i = base; i < base + segs; i++) { |
| 2089 | if (phb->ioda.dma32_segmap[i] == |
| 2090 | IODA_INVALID_PE) |
| 2091 | avail++; |
| 2092 | } |
| 2093 | |
| 2094 | if (avail == segs) |
| 2095 | goto found; |
| 2096 | } |
| 2097 | } while (--segs); |
| 2098 | |
| 2099 | if (!segs) { |
| 2100 | pe_warn(pe, "No available DMA32 segments\n"); |
| 2101 | return; |
| 2102 | } |
| 2103 | |
| 2104 | found: |
Alexey Kardashevskiy | 0eaf4de | 2015-06-05 16:35:09 +1000 | [diff] [blame] | 2105 | tbl = pnv_pci_table_alloc(phb->hose->node); |
Alexey Kardashevskiy | b348aa6 | 2015-06-05 16:35:08 +1000 | [diff] [blame] | 2106 | iommu_register_group(&pe->table_group, phb->hose->global_number, |
| 2107 | pe->pe_number); |
Alexey Kardashevskiy | 0eaf4de | 2015-06-05 16:35:09 +1000 | [diff] [blame] | 2108 | pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group); |
Alexey Kardashevskiy | c577382 | 2015-06-05 16:34:55 +1000 | [diff] [blame] | 2109 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2110 | /* Grab a 32-bit TCE table */ |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 2111 | pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n", |
| 2112 | weight, total_weight, base, segs); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2113 | pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n", |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 2114 | base * PNV_IODA1_DMA32_SEGSIZE, |
| 2115 | (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2116 | |
| 2117 | /* XXX Currently, we allocate one big contiguous table for the |
| 2118 | * TCEs. We only really need one chunk per 256M of TCE space |
| 2119 | * (ie per segment) but that's an optimization for later, it |
| 2120 | * requires some added smarts with our get/put_tce implementation |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 2121 | * |
| 2122 | * Each TCE page is 4KB in size and each TCE entry occupies 8 |
| 2123 | * bytes |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2124 | */ |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 2125 | tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2126 | tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL, |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 2127 | get_order(tce32_segsz * segs)); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2128 | if (!tce_mem) { |
| 2129 | pe_err(pe, " Failed to allocate a 32-bit TCE memory\n"); |
| 2130 | goto fail; |
| 2131 | } |
| 2132 | addr = page_address(tce_mem); |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 2133 | memset(addr, 0, tce32_segsz * segs); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2134 | |
| 2135 | /* Configure HW */ |
| 2136 | for (i = 0; i < segs; i++) { |
| 2137 | rc = opal_pci_map_pe_dma_window(phb->opal_id, |
| 2138 | pe->pe_number, |
| 2139 | base + i, 1, |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 2140 | __pa(addr) + tce32_segsz * i, |
| 2141 | tce32_segsz, IOMMU_PAGE_SIZE_4K); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2142 | if (rc) { |
| 2143 | pe_err(pe, " Failed to configure 32-bit TCE table," |
| 2144 | " err %ld\n", rc); |
| 2145 | goto fail; |
| 2146 | } |
| 2147 | } |
| 2148 | |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 2149 | /* Setup DMA32 segment mapping */ |
| 2150 | for (i = base; i < base + segs; i++) |
| 2151 | phb->ioda.dma32_segmap[i] = pe->pe_number; |
| 2152 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2153 | /* Setup linux iommu table */ |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 2154 | pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs, |
| 2155 | base * PNV_IODA1_DMA32_SEGSIZE, |
| 2156 | IOMMU_PAGE_SHIFT_4K); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2157 | |
Alexey Kardashevskiy | da004c3 | 2015-06-05 16:35:06 +1000 | [diff] [blame] | 2158 | tbl->it_ops = &pnv_ioda1_iommu_ops; |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 2159 | pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift; |
| 2160 | pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2161 | iommu_init_table(tbl, phb->hose->node); |
| 2162 | |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 2163 | if (pe->flags & PNV_IODA_PE_DEV) { |
Alexey Kardashevskiy | 4617082 | 2015-06-05 16:34:54 +1000 | [diff] [blame] | 2164 | /* |
| 2165 | * Setting table base here only for carrying iommu_group |
| 2166 | * further down to let iommu_add_device() do the job. |
| 2167 | * pnv_pci_ioda_dma_dev_setup will override it later anyway. |
| 2168 | */ |
| 2169 | set_iommu_table_base(&pe->pdev->dev, tbl); |
| 2170 | iommu_add_device(&pe->pdev->dev); |
Alexey Kardashevskiy | c577382 | 2015-06-05 16:34:55 +1000 | [diff] [blame] | 2171 | } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)) |
Alexey Kardashevskiy | ea30e99 | 2015-06-05 16:34:53 +1000 | [diff] [blame] | 2172 | pnv_ioda_setup_bus_dma(pe, pe->pbus); |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 2173 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2174 | return; |
| 2175 | fail: |
| 2176 | /* XXX Failure: Try to fallback to 64-bit only ? */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2177 | if (tce_mem) |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 2178 | __free_pages(tce_mem, get_order(tce32_segsz * segs)); |
Alexey Kardashevskiy | 0eaf4de | 2015-06-05 16:35:09 +1000 | [diff] [blame] | 2179 | if (tbl) { |
| 2180 | pnv_pci_unlink_table_and_group(tbl, &pe->table_group); |
| 2181 | iommu_free_table(tbl, "pnv"); |
| 2182 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Alexey Kardashevskiy | 43cb60a | 2015-06-05 16:35:18 +1000 | [diff] [blame] | 2185 | static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group, |
| 2186 | int num, struct iommu_table *tbl) |
| 2187 | { |
| 2188 | struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, |
| 2189 | table_group); |
| 2190 | struct pnv_phb *phb = pe->phb; |
| 2191 | int64_t rc; |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2192 | const unsigned long size = tbl->it_indirect_levels ? |
| 2193 | tbl->it_level_size : tbl->it_size; |
Alexey Kardashevskiy | 43cb60a | 2015-06-05 16:35:18 +1000 | [diff] [blame] | 2194 | const __u64 start_addr = tbl->it_offset << tbl->it_page_shift; |
| 2195 | const __u64 win_size = tbl->it_size << tbl->it_page_shift; |
| 2196 | |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 2197 | pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num, |
Alexey Kardashevskiy | 43cb60a | 2015-06-05 16:35:18 +1000 | [diff] [blame] | 2198 | start_addr, start_addr + win_size - 1, |
| 2199 | IOMMU_PAGE_SIZE(tbl)); |
| 2200 | |
| 2201 | /* |
| 2202 | * Map TCE table through TVT. The TVE index is the PE number |
| 2203 | * shifted by 1 bit for 32-bits DMA space. |
| 2204 | */ |
| 2205 | rc = opal_pci_map_pe_dma_window(phb->opal_id, |
| 2206 | pe->pe_number, |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 2207 | (pe->pe_number << 1) + num, |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2208 | tbl->it_indirect_levels + 1, |
Alexey Kardashevskiy | 43cb60a | 2015-06-05 16:35:18 +1000 | [diff] [blame] | 2209 | __pa(tbl->it_base), |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2210 | size << 3, |
Alexey Kardashevskiy | 43cb60a | 2015-06-05 16:35:18 +1000 | [diff] [blame] | 2211 | IOMMU_PAGE_SIZE(tbl)); |
| 2212 | if (rc) { |
| 2213 | pe_err(pe, "Failed to configure TCE table, err %ld\n", rc); |
| 2214 | return rc; |
| 2215 | } |
| 2216 | |
| 2217 | pnv_pci_link_table_and_group(phb->hose->node, num, |
| 2218 | tbl, &pe->table_group); |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 2219 | pnv_pci_phb3_tce_invalidate_pe(pe); |
Alexey Kardashevskiy | 43cb60a | 2015-06-05 16:35:18 +1000 | [diff] [blame] | 2220 | |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2224 | static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable) |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 2225 | { |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 2226 | uint16_t window_id = (pe->pe_number << 1 ) + 1; |
| 2227 | int64_t rc; |
| 2228 | |
| 2229 | pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis"); |
| 2230 | if (enable) { |
| 2231 | phys_addr_t top = memblock_end_of_DRAM(); |
| 2232 | |
| 2233 | top = roundup_pow_of_two(top); |
| 2234 | rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, |
| 2235 | pe->pe_number, |
| 2236 | window_id, |
| 2237 | pe->tce_bypass_base, |
| 2238 | top); |
| 2239 | } else { |
| 2240 | rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, |
| 2241 | pe->pe_number, |
| 2242 | window_id, |
| 2243 | pe->tce_bypass_base, |
| 2244 | 0); |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 2245 | } |
| 2246 | if (rc) |
| 2247 | pe_err(pe, "OPAL error %lld configuring bypass window\n", rc); |
| 2248 | else |
| 2249 | pe->tce_bypass_enabled = enable; |
| 2250 | } |
| 2251 | |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 2252 | static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset, |
| 2253 | __u32 page_shift, __u64 window_size, __u32 levels, |
| 2254 | struct iommu_table *tbl); |
| 2255 | |
| 2256 | static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group, |
| 2257 | int num, __u32 page_shift, __u64 window_size, __u32 levels, |
| 2258 | struct iommu_table **ptbl) |
| 2259 | { |
| 2260 | struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, |
| 2261 | table_group); |
| 2262 | int nid = pe->phb->hose->node; |
| 2263 | __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start; |
| 2264 | long ret; |
| 2265 | struct iommu_table *tbl; |
| 2266 | |
| 2267 | tbl = pnv_pci_table_alloc(nid); |
| 2268 | if (!tbl) |
| 2269 | return -ENOMEM; |
| 2270 | |
| 2271 | ret = pnv_pci_ioda2_table_alloc_pages(nid, |
| 2272 | bus_offset, page_shift, window_size, |
| 2273 | levels, tbl); |
| 2274 | if (ret) { |
| 2275 | iommu_free_table(tbl, "pnv"); |
| 2276 | return ret; |
| 2277 | } |
| 2278 | |
| 2279 | tbl->it_ops = &pnv_ioda2_iommu_ops; |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 2280 | |
| 2281 | *ptbl = tbl; |
| 2282 | |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2286 | static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe) |
| 2287 | { |
| 2288 | struct iommu_table *tbl = NULL; |
| 2289 | long rc; |
| 2290 | |
Nishanth Aravamudan | bb00545 | 2015-09-02 08:39:28 -0700 | [diff] [blame] | 2291 | /* |
Nishanth Aravamudan | fa14486 | 2015-09-04 11:22:52 -0700 | [diff] [blame] | 2292 | * crashkernel= specifies the kdump kernel's maximum memory at |
| 2293 | * some offset and there is no guaranteed the result is a power |
| 2294 | * of 2, which will cause errors later. |
| 2295 | */ |
| 2296 | const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max()); |
| 2297 | |
| 2298 | /* |
Nishanth Aravamudan | bb00545 | 2015-09-02 08:39:28 -0700 | [diff] [blame] | 2299 | * In memory constrained environments, e.g. kdump kernel, the |
| 2300 | * DMA window can be larger than available memory, which will |
| 2301 | * cause errors later. |
| 2302 | */ |
Nishanth Aravamudan | fa14486 | 2015-09-04 11:22:52 -0700 | [diff] [blame] | 2303 | const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory); |
Nishanth Aravamudan | bb00545 | 2015-09-02 08:39:28 -0700 | [diff] [blame] | 2304 | |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2305 | rc = pnv_pci_ioda2_create_table(&pe->table_group, 0, |
| 2306 | IOMMU_PAGE_SHIFT_4K, |
Nishanth Aravamudan | bb00545 | 2015-09-02 08:39:28 -0700 | [diff] [blame] | 2307 | window_size, |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2308 | POWERNV_IOMMU_DEFAULT_LEVELS, &tbl); |
| 2309 | if (rc) { |
| 2310 | pe_err(pe, "Failed to create 32-bit TCE table, err %ld", |
| 2311 | rc); |
| 2312 | return rc; |
| 2313 | } |
| 2314 | |
| 2315 | iommu_init_table(tbl, pe->phb->hose->node); |
| 2316 | |
| 2317 | rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl); |
| 2318 | if (rc) { |
| 2319 | pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", |
| 2320 | rc); |
| 2321 | pnv_ioda2_table_free(tbl); |
| 2322 | return rc; |
| 2323 | } |
| 2324 | |
| 2325 | if (!pnv_iommu_bypass_disabled) |
| 2326 | pnv_pci_ioda2_set_bypass(pe, true); |
| 2327 | |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2328 | /* |
| 2329 | * Setting table base here only for carrying iommu_group |
| 2330 | * further down to let iommu_add_device() do the job. |
| 2331 | * pnv_pci_ioda_dma_dev_setup will override it later anyway. |
| 2332 | */ |
| 2333 | if (pe->flags & PNV_IODA_PE_DEV) |
| 2334 | set_iommu_table_base(&pe->pdev->dev, tbl); |
| 2335 | |
| 2336 | return 0; |
| 2337 | } |
| 2338 | |
Alexey Kardashevskiy | b592643 | 2015-06-15 17:49:59 +1000 | [diff] [blame] | 2339 | #if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV) |
| 2340 | static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group, |
| 2341 | int num) |
| 2342 | { |
| 2343 | struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, |
| 2344 | table_group); |
| 2345 | struct pnv_phb *phb = pe->phb; |
| 2346 | long ret; |
| 2347 | |
| 2348 | pe_info(pe, "Removing DMA window #%d\n", num); |
| 2349 | |
| 2350 | ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number, |
| 2351 | (pe->pe_number << 1) + num, |
| 2352 | 0/* levels */, 0/* table address */, |
| 2353 | 0/* table size */, 0/* page size */); |
| 2354 | if (ret) |
| 2355 | pe_warn(pe, "Unmapping failed, ret = %ld\n", ret); |
| 2356 | else |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 2357 | pnv_pci_phb3_tce_invalidate_pe(pe); |
Alexey Kardashevskiy | b592643 | 2015-06-15 17:49:59 +1000 | [diff] [blame] | 2358 | |
| 2359 | pnv_pci_unlink_table_and_group(table_group->tables[num], table_group); |
| 2360 | |
| 2361 | return ret; |
| 2362 | } |
| 2363 | #endif |
| 2364 | |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2365 | #ifdef CONFIG_IOMMU_API |
Alexey Kardashevskiy | 0054719 | 2015-06-05 16:35:22 +1000 | [diff] [blame] | 2366 | static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift, |
| 2367 | __u64 window_size, __u32 levels) |
| 2368 | { |
| 2369 | unsigned long bytes = 0; |
| 2370 | const unsigned window_shift = ilog2(window_size); |
| 2371 | unsigned entries_shift = window_shift - page_shift; |
| 2372 | unsigned table_shift = entries_shift + 3; |
| 2373 | unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift); |
| 2374 | unsigned long direct_table_size; |
| 2375 | |
| 2376 | if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) || |
| 2377 | (window_size > memory_hotplug_max()) || |
| 2378 | !is_power_of_2(window_size)) |
| 2379 | return 0; |
| 2380 | |
| 2381 | /* Calculate a direct table size from window_size and levels */ |
| 2382 | entries_shift = (entries_shift + levels - 1) / levels; |
| 2383 | table_shift = entries_shift + 3; |
| 2384 | table_shift = max_t(unsigned, table_shift, PAGE_SHIFT); |
| 2385 | direct_table_size = 1UL << table_shift; |
| 2386 | |
| 2387 | for ( ; levels; --levels) { |
| 2388 | bytes += _ALIGN_UP(tce_table_size, direct_table_size); |
| 2389 | |
| 2390 | tce_table_size /= direct_table_size; |
| 2391 | tce_table_size <<= 3; |
| 2392 | tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size); |
| 2393 | } |
| 2394 | |
| 2395 | return bytes; |
| 2396 | } |
| 2397 | |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2398 | static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group) |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 2399 | { |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2400 | struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, |
| 2401 | table_group); |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2402 | /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */ |
| 2403 | struct iommu_table *tbl = pe->table_group.tables[0]; |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 2404 | |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2405 | pnv_pci_ioda2_set_bypass(pe, false); |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2406 | pnv_pci_ioda2_unset_window(&pe->table_group, 0); |
| 2407 | pnv_ioda2_table_free(tbl); |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 2408 | } |
| 2409 | |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2410 | static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group) |
| 2411 | { |
| 2412 | struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, |
| 2413 | table_group); |
| 2414 | |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2415 | pnv_pci_ioda2_setup_default_config(pe); |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | static struct iommu_table_group_ops pnv_pci_ioda2_ops = { |
Alexey Kardashevskiy | 0054719 | 2015-06-05 16:35:22 +1000 | [diff] [blame] | 2419 | .get_table_size = pnv_pci_ioda2_get_table_size, |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 2420 | .create_table = pnv_pci_ioda2_create_table, |
| 2421 | .set_window = pnv_pci_ioda2_set_window, |
| 2422 | .unset_window = pnv_pci_ioda2_unset_window, |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2423 | .take_ownership = pnv_ioda2_take_ownership, |
| 2424 | .release_ownership = pnv_ioda2_release_ownership, |
| 2425 | }; |
Alexey Kardashevskiy | b5cb9ab | 2016-04-29 18:55:24 +1000 | [diff] [blame] | 2426 | |
| 2427 | static int gpe_table_group_to_npe_cb(struct device *dev, void *opaque) |
| 2428 | { |
| 2429 | struct pci_controller *hose; |
| 2430 | struct pnv_phb *phb; |
| 2431 | struct pnv_ioda_pe **ptmppe = opaque; |
| 2432 | struct pci_dev *pdev = container_of(dev, struct pci_dev, dev); |
| 2433 | struct pci_dn *pdn = pci_get_pdn(pdev); |
| 2434 | |
| 2435 | if (!pdn || pdn->pe_number == IODA_INVALID_PE) |
| 2436 | return 0; |
| 2437 | |
| 2438 | hose = pci_bus_to_host(pdev->bus); |
| 2439 | phb = hose->private_data; |
| 2440 | if (phb->type != PNV_PHB_NPU) |
| 2441 | return 0; |
| 2442 | |
| 2443 | *ptmppe = &phb->ioda.pe_array[pdn->pe_number]; |
| 2444 | |
| 2445 | return 1; |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | * This returns PE of associated NPU. |
| 2450 | * This assumes that NPU is in the same IOMMU group with GPU and there is |
| 2451 | * no other PEs. |
| 2452 | */ |
| 2453 | static struct pnv_ioda_pe *gpe_table_group_to_npe( |
| 2454 | struct iommu_table_group *table_group) |
| 2455 | { |
| 2456 | struct pnv_ioda_pe *npe = NULL; |
| 2457 | int ret = iommu_group_for_each_dev(table_group->group, &npe, |
| 2458 | gpe_table_group_to_npe_cb); |
| 2459 | |
| 2460 | BUG_ON(!ret || !npe); |
| 2461 | |
| 2462 | return npe; |
| 2463 | } |
| 2464 | |
| 2465 | static long pnv_pci_ioda2_npu_set_window(struct iommu_table_group *table_group, |
| 2466 | int num, struct iommu_table *tbl) |
| 2467 | { |
| 2468 | long ret = pnv_pci_ioda2_set_window(table_group, num, tbl); |
| 2469 | |
| 2470 | if (ret) |
| 2471 | return ret; |
| 2472 | |
| 2473 | ret = pnv_npu_set_window(gpe_table_group_to_npe(table_group), num, tbl); |
| 2474 | if (ret) |
| 2475 | pnv_pci_ioda2_unset_window(table_group, num); |
| 2476 | |
| 2477 | return ret; |
| 2478 | } |
| 2479 | |
| 2480 | static long pnv_pci_ioda2_npu_unset_window( |
| 2481 | struct iommu_table_group *table_group, |
| 2482 | int num) |
| 2483 | { |
| 2484 | long ret = pnv_pci_ioda2_unset_window(table_group, num); |
| 2485 | |
| 2486 | if (ret) |
| 2487 | return ret; |
| 2488 | |
| 2489 | return pnv_npu_unset_window(gpe_table_group_to_npe(table_group), num); |
| 2490 | } |
| 2491 | |
| 2492 | static void pnv_ioda2_npu_take_ownership(struct iommu_table_group *table_group) |
| 2493 | { |
| 2494 | /* |
| 2495 | * Detach NPU first as pnv_ioda2_take_ownership() will destroy |
| 2496 | * the iommu_table if 32bit DMA is enabled. |
| 2497 | */ |
| 2498 | pnv_npu_take_ownership(gpe_table_group_to_npe(table_group)); |
| 2499 | pnv_ioda2_take_ownership(table_group); |
| 2500 | } |
| 2501 | |
| 2502 | static struct iommu_table_group_ops pnv_pci_ioda2_npu_ops = { |
| 2503 | .get_table_size = pnv_pci_ioda2_get_table_size, |
| 2504 | .create_table = pnv_pci_ioda2_create_table, |
| 2505 | .set_window = pnv_pci_ioda2_npu_set_window, |
| 2506 | .unset_window = pnv_pci_ioda2_npu_unset_window, |
| 2507 | .take_ownership = pnv_ioda2_npu_take_ownership, |
| 2508 | .release_ownership = pnv_ioda2_release_ownership, |
| 2509 | }; |
| 2510 | |
| 2511 | static void pnv_pci_ioda_setup_iommu_api(void) |
| 2512 | { |
| 2513 | struct pci_controller *hose, *tmp; |
| 2514 | struct pnv_phb *phb; |
| 2515 | struct pnv_ioda_pe *pe, *gpe; |
| 2516 | |
| 2517 | /* |
| 2518 | * Now we have all PHBs discovered, time to add NPU devices to |
| 2519 | * the corresponding IOMMU groups. |
| 2520 | */ |
| 2521 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
| 2522 | phb = hose->private_data; |
| 2523 | |
| 2524 | if (phb->type != PNV_PHB_NPU) |
| 2525 | continue; |
| 2526 | |
| 2527 | list_for_each_entry(pe, &phb->ioda.pe_list, list) { |
| 2528 | gpe = pnv_pci_npu_setup_iommu(pe); |
| 2529 | if (gpe) |
| 2530 | gpe->table_group.ops = &pnv_pci_ioda2_npu_ops; |
| 2531 | } |
| 2532 | } |
| 2533 | } |
| 2534 | #else /* !CONFIG_IOMMU_API */ |
| 2535 | static void pnv_pci_ioda_setup_iommu_api(void) { }; |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2536 | #endif |
| 2537 | |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2538 | static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift, |
| 2539 | unsigned levels, unsigned long limit, |
Alexey Kardashevskiy | 3ba3a73 | 2015-07-20 20:45:51 +1000 | [diff] [blame] | 2540 | unsigned long *current_offset, unsigned long *total_allocated) |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2541 | { |
| 2542 | struct page *tce_mem = NULL; |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2543 | __be64 *addr, *tmp; |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2544 | unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT; |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2545 | unsigned long allocated = 1UL << (order + PAGE_SHIFT); |
| 2546 | unsigned entries = 1UL << (shift - 3); |
| 2547 | long i; |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2548 | |
| 2549 | tce_mem = alloc_pages_node(nid, GFP_KERNEL, order); |
| 2550 | if (!tce_mem) { |
| 2551 | pr_err("Failed to allocate a TCE memory, order=%d\n", order); |
| 2552 | return NULL; |
| 2553 | } |
| 2554 | addr = page_address(tce_mem); |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2555 | memset(addr, 0, allocated); |
Alexey Kardashevskiy | 3ba3a73 | 2015-07-20 20:45:51 +1000 | [diff] [blame] | 2556 | *total_allocated += allocated; |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2557 | |
| 2558 | --levels; |
| 2559 | if (!levels) { |
| 2560 | *current_offset += allocated; |
| 2561 | return addr; |
| 2562 | } |
| 2563 | |
| 2564 | for (i = 0; i < entries; ++i) { |
| 2565 | tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift, |
Alexey Kardashevskiy | 3ba3a73 | 2015-07-20 20:45:51 +1000 | [diff] [blame] | 2566 | levels, limit, current_offset, total_allocated); |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2567 | if (!tmp) |
| 2568 | break; |
| 2569 | |
| 2570 | addr[i] = cpu_to_be64(__pa(tmp) | |
| 2571 | TCE_PCI_READ | TCE_PCI_WRITE); |
| 2572 | |
| 2573 | if (*current_offset >= limit) |
| 2574 | break; |
| 2575 | } |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2576 | |
| 2577 | return addr; |
| 2578 | } |
| 2579 | |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2580 | static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr, |
| 2581 | unsigned long size, unsigned level); |
| 2582 | |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2583 | static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset, |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2584 | __u32 page_shift, __u64 window_size, __u32 levels, |
| 2585 | struct iommu_table *tbl) |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2586 | { |
| 2587 | void *addr; |
Alexey Kardashevskiy | 3ba3a73 | 2015-07-20 20:45:51 +1000 | [diff] [blame] | 2588 | unsigned long offset = 0, level_shift, total_allocated = 0; |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2589 | const unsigned window_shift = ilog2(window_size); |
| 2590 | unsigned entries_shift = window_shift - page_shift; |
| 2591 | unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT); |
| 2592 | const unsigned long tce_table_size = 1UL << table_shift; |
| 2593 | |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2594 | if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS)) |
| 2595 | return -EINVAL; |
| 2596 | |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2597 | if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size)) |
| 2598 | return -EINVAL; |
| 2599 | |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2600 | /* Adjust direct table size from window_size and levels */ |
| 2601 | entries_shift = (entries_shift + levels - 1) / levels; |
| 2602 | level_shift = entries_shift + 3; |
| 2603 | level_shift = max_t(unsigned, level_shift, PAGE_SHIFT); |
| 2604 | |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2605 | /* Allocate TCE table */ |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2606 | addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift, |
Alexey Kardashevskiy | 3ba3a73 | 2015-07-20 20:45:51 +1000 | [diff] [blame] | 2607 | levels, tce_table_size, &offset, &total_allocated); |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2608 | |
| 2609 | /* addr==NULL means that the first level allocation failed */ |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2610 | if (!addr) |
| 2611 | return -ENOMEM; |
| 2612 | |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2613 | /* |
| 2614 | * First level was allocated but some lower level failed as |
| 2615 | * we did not allocate as much as we wanted, |
| 2616 | * release partially allocated table. |
| 2617 | */ |
| 2618 | if (offset < tce_table_size) { |
| 2619 | pnv_pci_ioda2_table_do_free_pages(addr, |
| 2620 | 1ULL << (level_shift - 3), levels - 1); |
| 2621 | return -ENOMEM; |
| 2622 | } |
| 2623 | |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2624 | /* Setup linux iommu table */ |
| 2625 | pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset, |
| 2626 | page_shift); |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2627 | tbl->it_level_size = 1ULL << (level_shift - 3); |
| 2628 | tbl->it_indirect_levels = levels - 1; |
Alexey Kardashevskiy | 3ba3a73 | 2015-07-20 20:45:51 +1000 | [diff] [blame] | 2629 | tbl->it_allocated_size = total_allocated; |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2630 | |
| 2631 | pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n", |
| 2632 | window_size, tce_table_size, bus_offset); |
| 2633 | |
| 2634 | return 0; |
| 2635 | } |
| 2636 | |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2637 | static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr, |
| 2638 | unsigned long size, unsigned level) |
| 2639 | { |
| 2640 | const unsigned long addr_ul = (unsigned long) addr & |
| 2641 | ~(TCE_PCI_READ | TCE_PCI_WRITE); |
| 2642 | |
| 2643 | if (level) { |
| 2644 | long i; |
| 2645 | u64 *tmp = (u64 *) addr_ul; |
| 2646 | |
| 2647 | for (i = 0; i < size; ++i) { |
| 2648 | unsigned long hpa = be64_to_cpu(tmp[i]); |
| 2649 | |
| 2650 | if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE))) |
| 2651 | continue; |
| 2652 | |
| 2653 | pnv_pci_ioda2_table_do_free_pages(__va(hpa), size, |
| 2654 | level - 1); |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | free_pages(addr_ul, get_order(size << 3)); |
| 2659 | } |
| 2660 | |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2661 | static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl) |
| 2662 | { |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2663 | const unsigned long size = tbl->it_indirect_levels ? |
| 2664 | tbl->it_level_size : tbl->it_size; |
| 2665 | |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2666 | if (!tbl->it_size) |
| 2667 | return; |
| 2668 | |
Alexey Kardashevskiy | bbb845c | 2015-06-05 16:35:19 +1000 | [diff] [blame] | 2669 | pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size, |
| 2670 | tbl->it_indirect_levels); |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2671 | } |
| 2672 | |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 2673 | static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb, |
| 2674 | struct pnv_ioda_pe *pe) |
| 2675 | { |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 2676 | int64_t rc; |
| 2677 | |
Gavin Shan | ccd1c19 | 2016-05-20 16:41:31 +1000 | [diff] [blame] | 2678 | if (!pnv_pci_ioda_pe_dma_weight(pe)) |
| 2679 | return; |
| 2680 | |
Alexey Kardashevskiy | f87a886 | 2015-06-05 16:35:10 +1000 | [diff] [blame] | 2681 | /* TVE #1 is selected by PCI address bit 59 */ |
| 2682 | pe->tce_bypass_base = 1ull << 59; |
| 2683 | |
Alexey Kardashevskiy | b348aa6 | 2015-06-05 16:35:08 +1000 | [diff] [blame] | 2684 | iommu_register_group(&pe->table_group, phb->hose->global_number, |
| 2685 | pe->pe_number); |
Alexey Kardashevskiy | c577382 | 2015-06-05 16:34:55 +1000 | [diff] [blame] | 2686 | |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 2687 | /* The PE will reserve all possible 32-bits space */ |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 2688 | pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n", |
Alexey Kardashevskiy | aca6913 | 2015-06-05 16:35:17 +1000 | [diff] [blame] | 2689 | phb->ioda.m32_pci_base); |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 2690 | |
Alexey Kardashevskiy | e5aad1e | 2015-06-05 16:35:16 +1000 | [diff] [blame] | 2691 | /* Setup linux iommu table */ |
Alexey Kardashevskiy | 4793d65 | 2015-06-05 16:35:20 +1000 | [diff] [blame] | 2692 | pe->table_group.tce32_start = 0; |
| 2693 | pe->table_group.tce32_size = phb->ioda.m32_pci_base; |
| 2694 | pe->table_group.max_dynamic_windows_supported = |
| 2695 | IOMMU_TABLE_GROUP_MAX_TABLES; |
| 2696 | pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS; |
| 2697 | pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M; |
Alexey Kardashevskiy | e5aad1e | 2015-06-05 16:35:16 +1000 | [diff] [blame] | 2698 | #ifdef CONFIG_IOMMU_API |
| 2699 | pe->table_group.ops = &pnv_pci_ioda2_ops; |
| 2700 | #endif |
| 2701 | |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2702 | rc = pnv_pci_ioda2_setup_default_config(pe); |
Gavin Shan | 801846d | 2016-05-03 15:41:34 +1000 | [diff] [blame] | 2703 | if (rc) |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2704 | return; |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 2705 | |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2706 | if (pe->flags & PNV_IODA_PE_DEV) |
Alexey Kardashevskiy | 4617082 | 2015-06-05 16:34:54 +1000 | [diff] [blame] | 2707 | iommu_add_device(&pe->pdev->dev); |
Alexey Kardashevskiy | 46d3e1e | 2015-06-05 16:35:23 +1000 | [diff] [blame] | 2708 | else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)) |
Alexey Kardashevskiy | ea30e99 | 2015-06-05 16:34:53 +1000 | [diff] [blame] | 2709 | pnv_ioda_setup_bus_dma(pe, pe->pbus); |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2712 | #ifdef CONFIG_PCI_MSI |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame] | 2713 | static void pnv_ioda2_msi_eoi(struct irq_data *d) |
| 2714 | { |
| 2715 | unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d); |
| 2716 | struct irq_chip *chip = irq_data_get_irq_chip(d); |
| 2717 | struct pnv_phb *phb = container_of(chip, struct pnv_phb, |
| 2718 | ioda.irq_chip); |
| 2719 | int64_t rc; |
| 2720 | |
| 2721 | rc = opal_pci_msi_eoi(phb->opal_id, hw_irq); |
| 2722 | WARN_ON_ONCE(rc); |
| 2723 | |
| 2724 | icp_native_eoi(d); |
| 2725 | } |
| 2726 | |
Ian Munsie | fd9a1c2 | 2014-10-08 19:54:55 +1100 | [diff] [blame] | 2727 | |
Ian Munsie | f456834 | 2016-07-14 07:17:00 +1000 | [diff] [blame] | 2728 | void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq) |
Ian Munsie | fd9a1c2 | 2014-10-08 19:54:55 +1100 | [diff] [blame] | 2729 | { |
| 2730 | struct irq_data *idata; |
| 2731 | struct irq_chip *ichip; |
| 2732 | |
Benjamin Herrenschmidt | fb11133 | 2016-07-08 16:37:09 +1000 | [diff] [blame] | 2733 | /* The MSI EOI OPAL call is only needed on PHB3 */ |
| 2734 | if (phb->model != PNV_PHB_MODEL_PHB3) |
Ian Munsie | fd9a1c2 | 2014-10-08 19:54:55 +1100 | [diff] [blame] | 2735 | return; |
| 2736 | |
| 2737 | if (!phb->ioda.irq_chip_init) { |
| 2738 | /* |
| 2739 | * First time we setup an MSI IRQ, we need to setup the |
| 2740 | * corresponding IRQ chip to route correctly. |
| 2741 | */ |
| 2742 | idata = irq_get_irq_data(virq); |
| 2743 | ichip = irq_data_get_irq_chip(idata); |
| 2744 | phb->ioda.irq_chip_init = 1; |
| 2745 | phb->ioda.irq_chip = *ichip; |
| 2746 | phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi; |
| 2747 | } |
| 2748 | irq_set_chip(virq, &phb->ioda.irq_chip); |
| 2749 | } |
| 2750 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2751 | static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev, |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame] | 2752 | unsigned int hwirq, unsigned int virq, |
| 2753 | unsigned int is_64, struct msi_msg *msg) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2754 | { |
| 2755 | struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev); |
| 2756 | unsigned int xive_num = hwirq - phb->msi_base; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 2757 | __be32 data; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2758 | int rc; |
| 2759 | |
| 2760 | /* No PE assigned ? bail out ... no MSI for you ! */ |
| 2761 | if (pe == NULL) |
| 2762 | return -ENXIO; |
| 2763 | |
| 2764 | /* Check if we have an MVE */ |
| 2765 | if (pe->mve_number < 0) |
| 2766 | return -ENXIO; |
| 2767 | |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 2768 | /* Force 32-bit MSI on some broken devices */ |
Benjamin Herrenschmidt | 3607438 | 2014-10-07 16:12:36 +1100 | [diff] [blame] | 2769 | if (dev->no_64bit_msi) |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 2770 | is_64 = 0; |
| 2771 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2772 | /* Assign XIVE to PE */ |
| 2773 | rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num); |
| 2774 | if (rc) { |
| 2775 | pr_warn("%s: OPAL error %d setting XIVE %d PE\n", |
| 2776 | pci_name(dev), rc, xive_num); |
| 2777 | return -EIO; |
| 2778 | } |
| 2779 | |
| 2780 | if (is_64) { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 2781 | __be64 addr64; |
| 2782 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2783 | rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1, |
| 2784 | &addr64, &data); |
| 2785 | if (rc) { |
| 2786 | pr_warn("%s: OPAL error %d getting 64-bit MSI data\n", |
| 2787 | pci_name(dev), rc); |
| 2788 | return -EIO; |
| 2789 | } |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 2790 | msg->address_hi = be64_to_cpu(addr64) >> 32; |
| 2791 | msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2792 | } else { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 2793 | __be32 addr32; |
| 2794 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2795 | rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1, |
| 2796 | &addr32, &data); |
| 2797 | if (rc) { |
| 2798 | pr_warn("%s: OPAL error %d getting 32-bit MSI data\n", |
| 2799 | pci_name(dev), rc); |
| 2800 | return -EIO; |
| 2801 | } |
| 2802 | msg->address_hi = 0; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 2803 | msg->address_lo = be32_to_cpu(addr32); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2804 | } |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 2805 | msg->data = be32_to_cpu(data); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2806 | |
Ian Munsie | f456834 | 2016-07-14 07:17:00 +1000 | [diff] [blame] | 2807 | pnv_set_msi_irq_chip(phb, virq); |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame] | 2808 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2809 | pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d)," |
| 2810 | " address=%x_%08x data=%x PE# %d\n", |
| 2811 | pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num, |
| 2812 | msg->address_hi, msg->address_lo, data, pe->pe_number); |
| 2813 | |
| 2814 | return 0; |
| 2815 | } |
| 2816 | |
| 2817 | static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) |
| 2818 | { |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 2819 | unsigned int count; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2820 | const __be32 *prop = of_get_property(phb->hose->dn, |
| 2821 | "ibm,opal-msi-ranges", NULL); |
| 2822 | if (!prop) { |
| 2823 | /* BML Fallback */ |
| 2824 | prop = of_get_property(phb->hose->dn, "msi-ranges", NULL); |
| 2825 | } |
| 2826 | if (!prop) |
| 2827 | return; |
| 2828 | |
| 2829 | phb->msi_base = be32_to_cpup(prop); |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 2830 | count = be32_to_cpup(prop + 1); |
| 2831 | if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) { |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2832 | pr_err("PCI %d: Failed to allocate MSI bitmap !\n", |
| 2833 | phb->hose->global_number); |
| 2834 | return; |
| 2835 | } |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 2836 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2837 | phb->msi_setup = pnv_pci_ioda_msi_setup; |
| 2838 | phb->msi32_support = 1; |
| 2839 | pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n", |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 2840 | count, phb->msi_base); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 2841 | } |
| 2842 | #else |
| 2843 | static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { } |
| 2844 | #endif /* CONFIG_PCI_MSI */ |
| 2845 | |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2846 | #ifdef CONFIG_PCI_IOV |
| 2847 | static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev) |
| 2848 | { |
Wei Yang | f2dd0af | 2015-10-22 09:22:17 +0800 | [diff] [blame] | 2849 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 2850 | struct pnv_phb *phb = hose->private_data; |
| 2851 | const resource_size_t gate = phb->ioda.m64_segsize >> 2; |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2852 | struct resource *res; |
| 2853 | int i; |
Wei Yang | dfcc8d4 | 2015-10-22 09:22:18 +0800 | [diff] [blame] | 2854 | resource_size_t size, total_vf_bar_sz; |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2855 | struct pci_dn *pdn; |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2856 | int mul, total_vfs; |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2857 | |
| 2858 | if (!pdev->is_physfn || pdev->is_added) |
| 2859 | return; |
| 2860 | |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2861 | pdn = pci_get_pdn(pdev); |
| 2862 | pdn->vfs_expanded = 0; |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 2863 | pdn->m64_single_mode = false; |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2864 | |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2865 | total_vfs = pci_sriov_get_totalvfs(pdev); |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 2866 | mul = phb->ioda.total_pe_num; |
Wei Yang | dfcc8d4 | 2015-10-22 09:22:18 +0800 | [diff] [blame] | 2867 | total_vf_bar_sz = 0; |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2868 | |
| 2869 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 2870 | res = &pdev->resource[i + PCI_IOV_RESOURCES]; |
| 2871 | if (!res->flags || res->parent) |
| 2872 | continue; |
Benjamin Herrenschmidt | 5958d19 | 2016-07-08 15:55:43 +1000 | [diff] [blame] | 2873 | if (!pnv_pci_is_m64(phb, res)) { |
Wei Yang | b033185 | 2015-10-22 09:22:14 +0800 | [diff] [blame] | 2874 | dev_warn(&pdev->dev, "Don't support SR-IOV with" |
| 2875 | " non M64 VF BAR%d: %pR. \n", |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2876 | i, res); |
Wei Yang | b033185 | 2015-10-22 09:22:14 +0800 | [diff] [blame] | 2877 | goto truncate_iov; |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2878 | } |
| 2879 | |
Wei Yang | dfcc8d4 | 2015-10-22 09:22:18 +0800 | [diff] [blame] | 2880 | total_vf_bar_sz += pci_iov_resource_size(pdev, |
| 2881 | i + PCI_IOV_RESOURCES); |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2882 | |
Wei Yang | f2dd0af | 2015-10-22 09:22:17 +0800 | [diff] [blame] | 2883 | /* |
| 2884 | * If bigger than quarter of M64 segment size, just round up |
| 2885 | * power of two. |
| 2886 | * |
| 2887 | * Generally, one M64 BAR maps one IOV BAR. To avoid conflict |
| 2888 | * with other devices, IOV BAR size is expanded to be |
| 2889 | * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64 |
| 2890 | * segment size , the expanded size would equal to half of the |
| 2891 | * whole M64 space size, which will exhaust the M64 Space and |
| 2892 | * limit the system flexibility. This is a design decision to |
| 2893 | * set the boundary to quarter of the M64 segment size. |
| 2894 | */ |
Wei Yang | dfcc8d4 | 2015-10-22 09:22:18 +0800 | [diff] [blame] | 2895 | if (total_vf_bar_sz > gate) { |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2896 | mul = roundup_pow_of_two(total_vfs); |
Wei Yang | dfcc8d4 | 2015-10-22 09:22:18 +0800 | [diff] [blame] | 2897 | dev_info(&pdev->dev, |
| 2898 | "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n", |
| 2899 | total_vf_bar_sz, gate, mul); |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 2900 | pdn->m64_single_mode = true; |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2901 | break; |
| 2902 | } |
| 2903 | } |
| 2904 | |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2905 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 2906 | res = &pdev->resource[i + PCI_IOV_RESOURCES]; |
| 2907 | if (!res->flags || res->parent) |
| 2908 | continue; |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2909 | |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2910 | size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES); |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 2911 | /* |
| 2912 | * On PHB3, the minimum size alignment of M64 BAR in single |
| 2913 | * mode is 32MB. |
| 2914 | */ |
| 2915 | if (pdn->m64_single_mode && (size < SZ_32M)) |
| 2916 | goto truncate_iov; |
| 2917 | dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res); |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2918 | res->end = res->start + size * mul - 1; |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2919 | dev_dbg(&pdev->dev, " %pR\n", res); |
| 2920 | dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)", |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2921 | i, res, mul); |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2922 | } |
Wei Yang | 5b88ec2 | 2015-03-25 16:23:58 +0800 | [diff] [blame] | 2923 | pdn->vfs_expanded = mul; |
Wei Yang | b033185 | 2015-10-22 09:22:14 +0800 | [diff] [blame] | 2924 | |
| 2925 | return; |
| 2926 | |
| 2927 | truncate_iov: |
| 2928 | /* To save MMIO space, IOV BAR is truncated. */ |
| 2929 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
| 2930 | res = &pdev->resource[i + PCI_IOV_RESOURCES]; |
| 2931 | res->flags = 0; |
| 2932 | res->end = res->start - 1; |
| 2933 | } |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 2934 | } |
| 2935 | #endif /* CONFIG_PCI_IOV */ |
| 2936 | |
Gavin Shan | 23e7942 | 2016-05-03 15:41:27 +1000 | [diff] [blame] | 2937 | static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe, |
| 2938 | struct resource *res) |
| 2939 | { |
| 2940 | struct pnv_phb *phb = pe->phb; |
| 2941 | struct pci_bus_region region; |
| 2942 | int index; |
| 2943 | int64_t rc; |
| 2944 | |
| 2945 | if (!res || !res->flags || res->start > res->end) |
| 2946 | return; |
| 2947 | |
| 2948 | if (res->flags & IORESOURCE_IO) { |
| 2949 | region.start = res->start - phb->ioda.io_pci_base; |
| 2950 | region.end = res->end - phb->ioda.io_pci_base; |
| 2951 | index = region.start / phb->ioda.io_segsize; |
| 2952 | |
| 2953 | while (index < phb->ioda.total_pe_num && |
| 2954 | region.start <= region.end) { |
| 2955 | phb->ioda.io_segmap[index] = pe->pe_number; |
| 2956 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 2957 | pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index); |
| 2958 | if (rc != OPAL_SUCCESS) { |
| 2959 | pr_err("%s: Error %lld mapping IO segment#%d to PE#%d\n", |
| 2960 | __func__, rc, index, pe->pe_number); |
| 2961 | break; |
| 2962 | } |
| 2963 | |
| 2964 | region.start += phb->ioda.io_segsize; |
| 2965 | index++; |
| 2966 | } |
| 2967 | } else if ((res->flags & IORESOURCE_MEM) && |
Benjamin Herrenschmidt | 5958d19 | 2016-07-08 15:55:43 +1000 | [diff] [blame] | 2968 | !pnv_pci_is_m64(phb, res)) { |
Gavin Shan | 23e7942 | 2016-05-03 15:41:27 +1000 | [diff] [blame] | 2969 | region.start = res->start - |
| 2970 | phb->hose->mem_offset[0] - |
| 2971 | phb->ioda.m32_pci_base; |
| 2972 | region.end = res->end - |
| 2973 | phb->hose->mem_offset[0] - |
| 2974 | phb->ioda.m32_pci_base; |
| 2975 | index = region.start / phb->ioda.m32_segsize; |
| 2976 | |
| 2977 | while (index < phb->ioda.total_pe_num && |
| 2978 | region.start <= region.end) { |
| 2979 | phb->ioda.m32_segmap[index] = pe->pe_number; |
| 2980 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 2981 | pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index); |
| 2982 | if (rc != OPAL_SUCCESS) { |
| 2983 | pr_err("%s: Error %lld mapping M32 segment#%d to PE#%d", |
| 2984 | __func__, rc, index, pe->pe_number); |
| 2985 | break; |
| 2986 | } |
| 2987 | |
| 2988 | region.start += phb->ioda.m32_segsize; |
| 2989 | index++; |
| 2990 | } |
| 2991 | } |
| 2992 | } |
| 2993 | |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 2994 | /* |
| 2995 | * This function is supposed to be called on basis of PE from top |
| 2996 | * to bottom style. So the the I/O or MMIO segment assigned to |
| 2997 | * parent PE could be overrided by its child PEs if necessary. |
| 2998 | */ |
Gavin Shan | 23e7942 | 2016-05-03 15:41:27 +1000 | [diff] [blame] | 2999 | static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe) |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 3000 | { |
Gavin Shan | 69d733e | 2016-05-03 15:41:28 +1000 | [diff] [blame] | 3001 | struct pci_dev *pdev; |
Gavin Shan | 23e7942 | 2016-05-03 15:41:27 +1000 | [diff] [blame] | 3002 | int i; |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 3003 | |
| 3004 | /* |
| 3005 | * NOTE: We only care PCI bus based PE for now. For PCI |
| 3006 | * device based PE, for example SRIOV sensitive VF should |
| 3007 | * be figured out later. |
| 3008 | */ |
| 3009 | BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))); |
| 3010 | |
Gavin Shan | 69d733e | 2016-05-03 15:41:28 +1000 | [diff] [blame] | 3011 | list_for_each_entry(pdev, &pe->pbus->devices, bus_list) { |
| 3012 | for (i = 0; i <= PCI_ROM_RESOURCE; i++) |
| 3013 | pnv_ioda_setup_pe_res(pe, &pdev->resource[i]); |
| 3014 | |
| 3015 | /* |
| 3016 | * If the PE contains all subordinate PCI buses, the |
| 3017 | * windows of the child bridges should be mapped to |
| 3018 | * the PE as well. |
| 3019 | */ |
| 3020 | if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev)) |
| 3021 | continue; |
| 3022 | for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) |
| 3023 | pnv_ioda_setup_pe_res(pe, |
| 3024 | &pdev->resource[PCI_BRIDGE_RESOURCES + i]); |
| 3025 | } |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Russell Currey | 98b665d | 2016-07-28 15:05:03 +1000 | [diff] [blame] | 3028 | #ifdef CONFIG_DEBUG_FS |
| 3029 | static int pnv_pci_diag_data_set(void *data, u64 val) |
| 3030 | { |
| 3031 | struct pci_controller *hose; |
| 3032 | struct pnv_phb *phb; |
| 3033 | s64 ret; |
| 3034 | |
| 3035 | if (val != 1ULL) |
| 3036 | return -EINVAL; |
| 3037 | |
| 3038 | hose = (struct pci_controller *)data; |
| 3039 | if (!hose || !hose->private_data) |
| 3040 | return -ENODEV; |
| 3041 | |
| 3042 | phb = hose->private_data; |
| 3043 | |
| 3044 | /* Retrieve the diag data from firmware */ |
| 3045 | ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob, |
| 3046 | PNV_PCI_DIAG_BUF_SIZE); |
| 3047 | if (ret != OPAL_SUCCESS) |
| 3048 | return -EIO; |
| 3049 | |
| 3050 | /* Print the diag data to the kernel log */ |
| 3051 | pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob); |
| 3052 | return 0; |
| 3053 | } |
| 3054 | |
| 3055 | DEFINE_SIMPLE_ATTRIBUTE(pnv_pci_diag_data_fops, NULL, |
| 3056 | pnv_pci_diag_data_set, "%llu\n"); |
| 3057 | |
| 3058 | #endif /* CONFIG_DEBUG_FS */ |
| 3059 | |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 3060 | static void pnv_pci_ioda_create_dbgfs(void) |
| 3061 | { |
| 3062 | #ifdef CONFIG_DEBUG_FS |
| 3063 | struct pci_controller *hose, *tmp; |
| 3064 | struct pnv_phb *phb; |
| 3065 | char name[16]; |
| 3066 | |
| 3067 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
| 3068 | phb = hose->private_data; |
| 3069 | |
Gavin Shan | ccd1c19 | 2016-05-20 16:41:31 +1000 | [diff] [blame] | 3070 | /* Notify initialization of PHB done */ |
| 3071 | phb->initialized = 1; |
| 3072 | |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 3073 | sprintf(name, "PCI%04x", hose->global_number); |
| 3074 | phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root); |
Russell Currey | 98b665d | 2016-07-28 15:05:03 +1000 | [diff] [blame] | 3075 | if (!phb->dbgfs) { |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 3076 | pr_warning("%s: Error on creating debugfs on PHB#%x\n", |
| 3077 | __func__, hose->global_number); |
Russell Currey | 98b665d | 2016-07-28 15:05:03 +1000 | [diff] [blame] | 3078 | continue; |
| 3079 | } |
| 3080 | |
| 3081 | debugfs_create_file("dump_diag_regs", 0200, phb->dbgfs, hose, |
| 3082 | &pnv_pci_diag_data_fops); |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 3083 | } |
| 3084 | #endif /* CONFIG_DEBUG_FS */ |
| 3085 | } |
| 3086 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 3087 | static void pnv_pci_ioda_fixup(void) |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 3088 | { |
| 3089 | pnv_pci_ioda_setup_PEs(); |
Gavin Shan | ccd1c19 | 2016-05-20 16:41:31 +1000 | [diff] [blame] | 3090 | pnv_pci_ioda_setup_iommu_api(); |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 3091 | pnv_pci_ioda_create_dbgfs(); |
| 3092 | |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 3093 | #ifdef CONFIG_EEH |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 3094 | eeh_init(); |
Mike Qiu | dadcd6d | 2014-06-26 02:58:47 -0400 | [diff] [blame] | 3095 | eeh_addr_cache_build(); |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 3096 | #endif |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
Gavin Shan | 271fd03 | 2012-09-11 16:59:47 -0600 | [diff] [blame] | 3099 | /* |
| 3100 | * Returns the alignment for I/O or memory windows for P2P |
| 3101 | * bridges. That actually depends on how PEs are segmented. |
| 3102 | * For now, we return I/O or M32 segment size for PE sensitive |
| 3103 | * P2P bridges. Otherwise, the default values (4KiB for I/O, |
| 3104 | * 1MiB for memory) will be returned. |
| 3105 | * |
| 3106 | * The current PCI bus might be put into one PE, which was |
| 3107 | * create against the parent PCI bridge. For that case, we |
| 3108 | * needn't enlarge the alignment so that we can save some |
| 3109 | * resources. |
| 3110 | */ |
| 3111 | static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus, |
| 3112 | unsigned long type) |
| 3113 | { |
| 3114 | struct pci_dev *bridge; |
| 3115 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 3116 | struct pnv_phb *phb = hose->private_data; |
| 3117 | int num_pci_bridges = 0; |
| 3118 | |
| 3119 | bridge = bus->self; |
| 3120 | while (bridge) { |
| 3121 | if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) { |
| 3122 | num_pci_bridges++; |
| 3123 | if (num_pci_bridges >= 2) |
| 3124 | return 1; |
| 3125 | } |
| 3126 | |
| 3127 | bridge = bridge->bus->self; |
| 3128 | } |
| 3129 | |
Benjamin Herrenschmidt | 5958d19 | 2016-07-08 15:55:43 +1000 | [diff] [blame] | 3130 | /* |
| 3131 | * We fall back to M32 if M64 isn't supported. We enforce the M64 |
| 3132 | * alignment for any 64-bit resource, PCIe doesn't care and |
| 3133 | * bridges only do 64-bit prefetchable anyway. |
| 3134 | */ |
| 3135 | if (phb->ioda.m64_segsize && (type & IORESOURCE_MEM_64)) |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 3136 | return phb->ioda.m64_segsize; |
Gavin Shan | 271fd03 | 2012-09-11 16:59:47 -0600 | [diff] [blame] | 3137 | if (type & IORESOURCE_MEM) |
| 3138 | return phb->ioda.m32_segsize; |
| 3139 | |
| 3140 | return phb->ioda.io_segsize; |
| 3141 | } |
| 3142 | |
Gavin Shan | 40e2a47 | 2016-05-20 16:41:33 +1000 | [diff] [blame] | 3143 | /* |
| 3144 | * We are updating root port or the upstream port of the |
| 3145 | * bridge behind the root port with PHB's windows in order |
| 3146 | * to accommodate the changes on required resources during |
| 3147 | * PCI (slot) hotplug, which is connected to either root |
| 3148 | * port or the downstream ports of PCIe switch behind the |
| 3149 | * root port. |
| 3150 | */ |
| 3151 | static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus, |
| 3152 | unsigned long type) |
| 3153 | { |
| 3154 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 3155 | struct pnv_phb *phb = hose->private_data; |
| 3156 | struct pci_dev *bridge = bus->self; |
| 3157 | struct resource *r, *w; |
| 3158 | bool msi_region = false; |
| 3159 | int i; |
| 3160 | |
| 3161 | /* Check if we need apply fixup to the bridge's windows */ |
| 3162 | if (!pci_is_root_bus(bridge->bus) && |
| 3163 | !pci_is_root_bus(bridge->bus->self->bus)) |
| 3164 | return; |
| 3165 | |
| 3166 | /* Fixup the resources */ |
| 3167 | for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) { |
| 3168 | r = &bridge->resource[PCI_BRIDGE_RESOURCES + i]; |
| 3169 | if (!r->flags || !r->parent) |
| 3170 | continue; |
| 3171 | |
| 3172 | w = NULL; |
| 3173 | if (r->flags & type & IORESOURCE_IO) |
| 3174 | w = &hose->io_resource; |
Benjamin Herrenschmidt | 5958d19 | 2016-07-08 15:55:43 +1000 | [diff] [blame] | 3175 | else if (pnv_pci_is_m64(phb, r) && |
Gavin Shan | 40e2a47 | 2016-05-20 16:41:33 +1000 | [diff] [blame] | 3176 | (type & IORESOURCE_PREFETCH) && |
| 3177 | phb->ioda.m64_segsize) |
| 3178 | w = &hose->mem_resources[1]; |
| 3179 | else if (r->flags & type & IORESOURCE_MEM) { |
| 3180 | w = &hose->mem_resources[0]; |
| 3181 | msi_region = true; |
| 3182 | } |
| 3183 | |
| 3184 | r->start = w->start; |
| 3185 | r->end = w->end; |
| 3186 | |
| 3187 | /* The 64KB 32-bits MSI region shouldn't be included in |
| 3188 | * the 32-bits bridge window. Otherwise, we can see strange |
| 3189 | * issues. One of them is EEH error observed on Garrison. |
| 3190 | * |
| 3191 | * Exclude top 1MB region which is the minimal alignment of |
| 3192 | * 32-bits bridge window. |
| 3193 | */ |
| 3194 | if (msi_region) { |
| 3195 | r->end += 0x10000; |
| 3196 | r->end -= 0x100000; |
| 3197 | } |
| 3198 | } |
| 3199 | } |
| 3200 | |
Gavin Shan | ccd1c19 | 2016-05-20 16:41:31 +1000 | [diff] [blame] | 3201 | static void pnv_pci_setup_bridge(struct pci_bus *bus, unsigned long type) |
| 3202 | { |
| 3203 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 3204 | struct pnv_phb *phb = hose->private_data; |
| 3205 | struct pci_dev *bridge = bus->self; |
| 3206 | struct pnv_ioda_pe *pe; |
| 3207 | bool all = (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE); |
| 3208 | |
Gavin Shan | 40e2a47 | 2016-05-20 16:41:33 +1000 | [diff] [blame] | 3209 | /* Extend bridge's windows if necessary */ |
| 3210 | pnv_pci_fixup_bridge_resources(bus, type); |
| 3211 | |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 3212 | /* The PE for root bus should be realized before any one else */ |
| 3213 | if (!phb->ioda.root_pe_populated) { |
| 3214 | pe = pnv_ioda_setup_bus_PE(phb->hose->bus, false); |
| 3215 | if (pe) { |
| 3216 | phb->ioda.root_pe_idx = pe->pe_number; |
| 3217 | phb->ioda.root_pe_populated = true; |
| 3218 | } |
| 3219 | } |
| 3220 | |
Gavin Shan | ccd1c19 | 2016-05-20 16:41:31 +1000 | [diff] [blame] | 3221 | /* Don't assign PE to PCI bus, which doesn't have subordinate devices */ |
| 3222 | if (list_empty(&bus->devices)) |
| 3223 | return; |
| 3224 | |
| 3225 | /* Reserve PEs according to used M64 resources */ |
| 3226 | if (phb->reserve_m64_pe) |
| 3227 | phb->reserve_m64_pe(bus, NULL, all); |
| 3228 | |
| 3229 | /* |
| 3230 | * Assign PE. We might run here because of partial hotplug. |
| 3231 | * For the case, we just pick up the existing PE and should |
| 3232 | * not allocate resources again. |
| 3233 | */ |
| 3234 | pe = pnv_ioda_setup_bus_PE(bus, all); |
| 3235 | if (!pe) |
| 3236 | return; |
| 3237 | |
| 3238 | pnv_ioda_setup_pe_seg(pe); |
| 3239 | switch (phb->type) { |
| 3240 | case PNV_PHB_IODA1: |
| 3241 | pnv_pci_ioda1_setup_dma_pe(phb, pe); |
| 3242 | break; |
| 3243 | case PNV_PHB_IODA2: |
| 3244 | pnv_pci_ioda2_setup_dma_pe(phb, pe); |
| 3245 | break; |
| 3246 | default: |
| 3247 | pr_warn("%s: No DMA for PHB#%d (type %d)\n", |
| 3248 | __func__, phb->hose->global_number, phb->type); |
| 3249 | } |
| 3250 | } |
| 3251 | |
Wei Yang | 5350ab3 | 2015-03-25 16:23:56 +0800 | [diff] [blame] | 3252 | #ifdef CONFIG_PCI_IOV |
| 3253 | static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev, |
| 3254 | int resno) |
| 3255 | { |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 3256 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 3257 | struct pnv_phb *phb = hose->private_data; |
Wei Yang | 5350ab3 | 2015-03-25 16:23:56 +0800 | [diff] [blame] | 3258 | struct pci_dn *pdn = pci_get_pdn(pdev); |
Wei Yang | 7fbe7a9 | 2015-10-22 09:22:15 +0800 | [diff] [blame] | 3259 | resource_size_t align; |
Wei Yang | 5350ab3 | 2015-03-25 16:23:56 +0800 | [diff] [blame] | 3260 | |
Wei Yang | 7fbe7a9 | 2015-10-22 09:22:15 +0800 | [diff] [blame] | 3261 | /* |
| 3262 | * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the |
| 3263 | * SR-IOV. While from hardware perspective, the range mapped by M64 |
| 3264 | * BAR should be size aligned. |
| 3265 | * |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 3266 | * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra |
| 3267 | * powernv-specific hardware restriction is gone. But if just use the |
| 3268 | * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with |
| 3269 | * in one segment of M64 #15, which introduces the PE conflict between |
| 3270 | * PF and VF. Based on this, the minimum alignment of an IOV BAR is |
| 3271 | * m64_segsize. |
| 3272 | * |
Wei Yang | 7fbe7a9 | 2015-10-22 09:22:15 +0800 | [diff] [blame] | 3273 | * This function returns the total IOV BAR size if M64 BAR is in |
| 3274 | * Shared PE mode or just VF BAR size if not. |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 3275 | * If the M64 BAR is in Single PE mode, return the VF BAR size or |
| 3276 | * M64 segment size if IOV BAR size is less. |
Wei Yang | 7fbe7a9 | 2015-10-22 09:22:15 +0800 | [diff] [blame] | 3277 | */ |
Wei Yang | 5350ab3 | 2015-03-25 16:23:56 +0800 | [diff] [blame] | 3278 | align = pci_iov_resource_size(pdev, resno); |
Wei Yang | 7fbe7a9 | 2015-10-22 09:22:15 +0800 | [diff] [blame] | 3279 | if (!pdn->vfs_expanded) |
| 3280 | return align; |
Wei Yang | ee8222f | 2015-10-22 09:22:16 +0800 | [diff] [blame] | 3281 | if (pdn->m64_single_mode) |
| 3282 | return max(align, (resource_size_t)phb->ioda.m64_segsize); |
Wei Yang | 5350ab3 | 2015-03-25 16:23:56 +0800 | [diff] [blame] | 3283 | |
Wei Yang | 7fbe7a9 | 2015-10-22 09:22:15 +0800 | [diff] [blame] | 3284 | return pdn->vfs_expanded * align; |
Wei Yang | 5350ab3 | 2015-03-25 16:23:56 +0800 | [diff] [blame] | 3285 | } |
| 3286 | #endif /* CONFIG_PCI_IOV */ |
| 3287 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3288 | /* Prevent enabling devices for which we couldn't properly |
| 3289 | * assign a PE |
| 3290 | */ |
Ian Munsie | 4361b03 | 2016-07-14 07:17:06 +1000 | [diff] [blame] | 3291 | bool pnv_pci_enable_device_hook(struct pci_dev *dev) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3292 | { |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 3293 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 3294 | struct pnv_phb *phb = hose->private_data; |
| 3295 | struct pci_dn *pdn; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3296 | |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 3297 | /* The function is probably called while the PEs have |
| 3298 | * not be created yet. For example, resource reassignment |
| 3299 | * during PCI probe period. We just skip the check if |
| 3300 | * PEs isn't ready. |
| 3301 | */ |
| 3302 | if (!phb->initialized) |
Daniel Axtens | c88c2a1 | 2015-03-31 16:00:41 +1100 | [diff] [blame] | 3303 | return true; |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 3304 | |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 3305 | pdn = pci_get_pdn(dev); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3306 | if (!pdn || pdn->pe_number == IODA_INVALID_PE) |
Daniel Axtens | c88c2a1 | 2015-03-31 16:00:41 +1100 | [diff] [blame] | 3307 | return false; |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 3308 | |
Daniel Axtens | c88c2a1 | 2015-03-31 16:00:41 +1100 | [diff] [blame] | 3309 | return true; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
Gavin Shan | c5f7700 | 2016-05-20 16:41:35 +1000 | [diff] [blame] | 3312 | static long pnv_pci_ioda1_unset_window(struct iommu_table_group *table_group, |
| 3313 | int num) |
| 3314 | { |
| 3315 | struct pnv_ioda_pe *pe = container_of(table_group, |
| 3316 | struct pnv_ioda_pe, table_group); |
| 3317 | struct pnv_phb *phb = pe->phb; |
| 3318 | unsigned int idx; |
| 3319 | long rc; |
| 3320 | |
| 3321 | pe_info(pe, "Removing DMA window #%d\n", num); |
| 3322 | for (idx = 0; idx < phb->ioda.dma32_count; idx++) { |
| 3323 | if (phb->ioda.dma32_segmap[idx] != pe->pe_number) |
| 3324 | continue; |
| 3325 | |
| 3326 | rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number, |
| 3327 | idx, 0, 0ul, 0ul, 0ul); |
| 3328 | if (rc != OPAL_SUCCESS) { |
| 3329 | pe_warn(pe, "Failure %ld unmapping DMA32 segment#%d\n", |
| 3330 | rc, idx); |
| 3331 | return rc; |
| 3332 | } |
| 3333 | |
| 3334 | phb->ioda.dma32_segmap[idx] = IODA_INVALID_PE; |
| 3335 | } |
| 3336 | |
| 3337 | pnv_pci_unlink_table_and_group(table_group->tables[num], table_group); |
| 3338 | return OPAL_SUCCESS; |
| 3339 | } |
| 3340 | |
| 3341 | static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe) |
| 3342 | { |
| 3343 | unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe); |
| 3344 | struct iommu_table *tbl = pe->table_group.tables[0]; |
| 3345 | int64_t rc; |
| 3346 | |
| 3347 | if (!weight) |
| 3348 | return; |
| 3349 | |
| 3350 | rc = pnv_pci_ioda1_unset_window(&pe->table_group, 0); |
| 3351 | if (rc != OPAL_SUCCESS) |
| 3352 | return; |
| 3353 | |
Benjamin Herrenschmidt | a34ab7c | 2016-07-08 16:37:12 +1000 | [diff] [blame] | 3354 | pnv_pci_p7ioc_tce_invalidate(tbl, tbl->it_offset, tbl->it_size, false); |
Gavin Shan | c5f7700 | 2016-05-20 16:41:35 +1000 | [diff] [blame] | 3355 | if (pe->table_group.group) { |
| 3356 | iommu_group_put(pe->table_group.group); |
| 3357 | WARN_ON(pe->table_group.group); |
| 3358 | } |
| 3359 | |
| 3360 | free_pages(tbl->it_base, get_order(tbl->it_size << 3)); |
| 3361 | iommu_free_table(tbl, "pnv"); |
| 3362 | } |
| 3363 | |
| 3364 | static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe) |
| 3365 | { |
| 3366 | struct iommu_table *tbl = pe->table_group.tables[0]; |
| 3367 | unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe); |
| 3368 | #ifdef CONFIG_IOMMU_API |
| 3369 | int64_t rc; |
| 3370 | #endif |
| 3371 | |
| 3372 | if (!weight) |
| 3373 | return; |
| 3374 | |
| 3375 | #ifdef CONFIG_IOMMU_API |
| 3376 | rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0); |
| 3377 | if (rc) |
| 3378 | pe_warn(pe, "OPAL error %ld release DMA window\n", rc); |
| 3379 | #endif |
| 3380 | |
| 3381 | pnv_pci_ioda2_set_bypass(pe, false); |
| 3382 | if (pe->table_group.group) { |
| 3383 | iommu_group_put(pe->table_group.group); |
| 3384 | WARN_ON(pe->table_group.group); |
| 3385 | } |
| 3386 | |
| 3387 | pnv_pci_ioda2_table_free_pages(tbl); |
| 3388 | iommu_free_table(tbl, "pnv"); |
| 3389 | } |
| 3390 | |
| 3391 | static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe, |
| 3392 | unsigned short win, |
| 3393 | unsigned int *map) |
| 3394 | { |
| 3395 | struct pnv_phb *phb = pe->phb; |
| 3396 | int idx; |
| 3397 | int64_t rc; |
| 3398 | |
| 3399 | for (idx = 0; idx < phb->ioda.total_pe_num; idx++) { |
| 3400 | if (map[idx] != pe->pe_number) |
| 3401 | continue; |
| 3402 | |
| 3403 | if (win == OPAL_M64_WINDOW_TYPE) |
| 3404 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 3405 | phb->ioda.reserved_pe_idx, win, |
| 3406 | idx / PNV_IODA1_M64_SEGS, |
| 3407 | idx % PNV_IODA1_M64_SEGS); |
| 3408 | else |
| 3409 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 3410 | phb->ioda.reserved_pe_idx, win, 0, idx); |
| 3411 | |
| 3412 | if (rc != OPAL_SUCCESS) |
| 3413 | pe_warn(pe, "Error %ld unmapping (%d) segment#%d\n", |
| 3414 | rc, win, idx); |
| 3415 | |
| 3416 | map[idx] = IODA_INVALID_PE; |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe) |
| 3421 | { |
| 3422 | struct pnv_phb *phb = pe->phb; |
| 3423 | |
| 3424 | if (phb->type == PNV_PHB_IODA1) { |
| 3425 | pnv_ioda_free_pe_seg(pe, OPAL_IO_WINDOW_TYPE, |
| 3426 | phb->ioda.io_segmap); |
| 3427 | pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE, |
| 3428 | phb->ioda.m32_segmap); |
| 3429 | pnv_ioda_free_pe_seg(pe, OPAL_M64_WINDOW_TYPE, |
| 3430 | phb->ioda.m64_segmap); |
| 3431 | } else if (phb->type == PNV_PHB_IODA2) { |
| 3432 | pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE, |
| 3433 | phb->ioda.m32_segmap); |
| 3434 | } |
| 3435 | } |
| 3436 | |
| 3437 | static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe) |
| 3438 | { |
| 3439 | struct pnv_phb *phb = pe->phb; |
| 3440 | struct pnv_ioda_pe *slave, *tmp; |
| 3441 | |
| 3442 | /* Release slave PEs in compound PE */ |
| 3443 | if (pe->flags & PNV_IODA_PE_MASTER) { |
| 3444 | list_for_each_entry_safe(slave, tmp, &pe->slaves, list) |
| 3445 | pnv_ioda_release_pe(slave); |
| 3446 | } |
| 3447 | |
| 3448 | list_del(&pe->list); |
| 3449 | switch (phb->type) { |
| 3450 | case PNV_PHB_IODA1: |
| 3451 | pnv_pci_ioda1_release_pe_dma(pe); |
| 3452 | break; |
| 3453 | case PNV_PHB_IODA2: |
| 3454 | pnv_pci_ioda2_release_pe_dma(pe); |
| 3455 | break; |
| 3456 | default: |
| 3457 | WARN_ON(1); |
| 3458 | } |
| 3459 | |
| 3460 | pnv_ioda_release_pe_seg(pe); |
| 3461 | pnv_ioda_deconfigure_pe(pe->phb, pe); |
| 3462 | pnv_ioda_free_pe(pe); |
| 3463 | } |
| 3464 | |
| 3465 | static void pnv_pci_release_device(struct pci_dev *pdev) |
| 3466 | { |
| 3467 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 3468 | struct pnv_phb *phb = hose->private_data; |
| 3469 | struct pci_dn *pdn = pci_get_pdn(pdev); |
| 3470 | struct pnv_ioda_pe *pe; |
| 3471 | |
| 3472 | if (pdev->is_virtfn) |
| 3473 | return; |
| 3474 | |
| 3475 | if (!pdn || pdn->pe_number == IODA_INVALID_PE) |
| 3476 | return; |
| 3477 | |
| 3478 | pe = &phb->ioda.pe_array[pdn->pe_number]; |
| 3479 | WARN_ON(--pe->device_count < 0); |
| 3480 | if (pe->device_count == 0) |
| 3481 | pnv_ioda_release_pe(pe); |
| 3482 | } |
| 3483 | |
Michael Neuling | 7a8e6bb | 2015-05-27 16:06:59 +1000 | [diff] [blame] | 3484 | static void pnv_pci_ioda_shutdown(struct pci_controller *hose) |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 3485 | { |
Michael Neuling | 7a8e6bb | 2015-05-27 16:06:59 +1000 | [diff] [blame] | 3486 | struct pnv_phb *phb = hose->private_data; |
| 3487 | |
Gavin Shan | d1a85ee | 2014-09-30 12:39:05 +1000 | [diff] [blame] | 3488 | opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE, |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 3489 | OPAL_ASSERT_RESET); |
| 3490 | } |
| 3491 | |
Daniel Axtens | 92ae035 | 2015-04-28 15:12:05 +1000 | [diff] [blame] | 3492 | static const struct pci_controller_ops pnv_pci_ioda_controller_ops = { |
Gavin Shan | cb4224c | 2016-05-03 15:41:21 +1000 | [diff] [blame] | 3493 | .dma_dev_setup = pnv_pci_dma_dev_setup, |
| 3494 | .dma_bus_setup = pnv_pci_dma_bus_setup, |
Daniel Axtens | 92ae035 | 2015-04-28 15:12:05 +1000 | [diff] [blame] | 3495 | #ifdef CONFIG_PCI_MSI |
Gavin Shan | cb4224c | 2016-05-03 15:41:21 +1000 | [diff] [blame] | 3496 | .setup_msi_irqs = pnv_setup_msi_irqs, |
| 3497 | .teardown_msi_irqs = pnv_teardown_msi_irqs, |
Daniel Axtens | 92ae035 | 2015-04-28 15:12:05 +1000 | [diff] [blame] | 3498 | #endif |
Gavin Shan | cb4224c | 2016-05-03 15:41:21 +1000 | [diff] [blame] | 3499 | .enable_device_hook = pnv_pci_enable_device_hook, |
Gavin Shan | c5f7700 | 2016-05-20 16:41:35 +1000 | [diff] [blame] | 3500 | .release_device = pnv_pci_release_device, |
Gavin Shan | cb4224c | 2016-05-03 15:41:21 +1000 | [diff] [blame] | 3501 | .window_alignment = pnv_pci_window_alignment, |
Gavin Shan | ccd1c19 | 2016-05-20 16:41:31 +1000 | [diff] [blame] | 3502 | .setup_bridge = pnv_pci_setup_bridge, |
Gavin Shan | cb4224c | 2016-05-03 15:41:21 +1000 | [diff] [blame] | 3503 | .reset_secondary_bus = pnv_pci_reset_secondary_bus, |
| 3504 | .dma_set_mask = pnv_pci_ioda_dma_set_mask, |
| 3505 | .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask, |
| 3506 | .shutdown = pnv_pci_ioda_shutdown, |
Daniel Axtens | 92ae035 | 2015-04-28 15:12:05 +1000 | [diff] [blame] | 3507 | }; |
| 3508 | |
Alexey Kardashevskiy | f9f8345 | 2016-04-29 18:55:20 +1000 | [diff] [blame] | 3509 | static int pnv_npu_dma_set_mask(struct pci_dev *npdev, u64 dma_mask) |
| 3510 | { |
| 3511 | dev_err_once(&npdev->dev, |
| 3512 | "%s operation unsupported for NVLink devices\n", |
| 3513 | __func__); |
| 3514 | return -EPERM; |
| 3515 | } |
| 3516 | |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 3517 | static const struct pci_controller_ops pnv_npu_ioda_controller_ops = { |
Gavin Shan | cb4224c | 2016-05-03 15:41:21 +1000 | [diff] [blame] | 3518 | .dma_dev_setup = pnv_pci_dma_dev_setup, |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 3519 | #ifdef CONFIG_PCI_MSI |
Gavin Shan | cb4224c | 2016-05-03 15:41:21 +1000 | [diff] [blame] | 3520 | .setup_msi_irqs = pnv_setup_msi_irqs, |
| 3521 | .teardown_msi_irqs = pnv_teardown_msi_irqs, |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 3522 | #endif |
Gavin Shan | cb4224c | 2016-05-03 15:41:21 +1000 | [diff] [blame] | 3523 | .enable_device_hook = pnv_pci_enable_device_hook, |
| 3524 | .window_alignment = pnv_pci_window_alignment, |
| 3525 | .reset_secondary_bus = pnv_pci_reset_secondary_bus, |
| 3526 | .dma_set_mask = pnv_npu_dma_set_mask, |
| 3527 | .shutdown = pnv_pci_ioda_shutdown, |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 3528 | }; |
| 3529 | |
Ian Munsie | 4361b03 | 2016-07-14 07:17:06 +1000 | [diff] [blame] | 3530 | #ifdef CONFIG_CXL_BASE |
| 3531 | const struct pci_controller_ops pnv_cxl_cx4_ioda_controller_ops = { |
| 3532 | .dma_dev_setup = pnv_pci_dma_dev_setup, |
| 3533 | .dma_bus_setup = pnv_pci_dma_bus_setup, |
Ian Munsie | a2f67d5 | 2016-07-14 07:17:10 +1000 | [diff] [blame] | 3534 | #ifdef CONFIG_PCI_MSI |
| 3535 | .setup_msi_irqs = pnv_cxl_cx4_setup_msi_irqs, |
| 3536 | .teardown_msi_irqs = pnv_cxl_cx4_teardown_msi_irqs, |
| 3537 | #endif |
Ian Munsie | 4361b03 | 2016-07-14 07:17:06 +1000 | [diff] [blame] | 3538 | .enable_device_hook = pnv_cxl_enable_device_hook, |
| 3539 | .disable_device = pnv_cxl_disable_device, |
| 3540 | .release_device = pnv_pci_release_device, |
| 3541 | .window_alignment = pnv_pci_window_alignment, |
| 3542 | .setup_bridge = pnv_pci_setup_bridge, |
| 3543 | .reset_secondary_bus = pnv_pci_reset_secondary_bus, |
| 3544 | .dma_set_mask = pnv_pci_ioda_dma_set_mask, |
| 3545 | .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask, |
| 3546 | .shutdown = pnv_pci_ioda_shutdown, |
| 3547 | }; |
| 3548 | #endif |
| 3549 | |
Anton Blanchard | e51df2c | 2014-08-20 08:55:18 +1000 | [diff] [blame] | 3550 | static void __init pnv_pci_init_ioda_phb(struct device_node *np, |
| 3551 | u64 hub_id, int ioda_type) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3552 | { |
| 3553 | struct pci_controller *hose; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3554 | struct pnv_phb *phb; |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 3555 | unsigned long size, m64map_off, m32map_off, pemap_off; |
| 3556 | unsigned long iomap_off = 0, dma32map_off = 0; |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 3557 | struct resource r; |
Alistair Popple | c681b93 | 2013-09-23 12:04:57 +1000 | [diff] [blame] | 3558 | const __be64 *prop64; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 3559 | const __be32 *prop32; |
Gavin Shan | f1b7cc3 | 2013-07-31 16:47:01 +0800 | [diff] [blame] | 3560 | int len; |
Gavin Shan | 3fa23ff | 2016-05-03 15:41:26 +1000 | [diff] [blame] | 3561 | unsigned int segno; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3562 | u64 phb_id; |
| 3563 | void *aux; |
| 3564 | long rc; |
| 3565 | |
Benjamin Herrenschmidt | 08a45b3 | 2016-07-08 16:37:17 +1000 | [diff] [blame] | 3566 | if (!of_device_is_available(np)) |
| 3567 | return; |
| 3568 | |
Gavin Shan | 9497a1c | 2016-06-21 12:35:56 +1000 | [diff] [blame] | 3569 | pr_info("Initializing %s PHB (%s)\n", |
| 3570 | pnv_phb_names[ioda_type], of_node_full_name(np)); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3571 | |
| 3572 | prop64 = of_get_property(np, "ibm,opal-phbid", NULL); |
| 3573 | if (!prop64) { |
| 3574 | pr_err(" Missing \"ibm,opal-phbid\" property !\n"); |
| 3575 | return; |
| 3576 | } |
| 3577 | phb_id = be64_to_cpup(prop64); |
| 3578 | pr_debug(" PHB-ID : 0x%016llx\n", phb_id); |
| 3579 | |
Michael Ellerman | e39f223f | 2014-11-18 16:47:35 +1100 | [diff] [blame] | 3580 | phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0); |
Gavin Shan | 58d714e | 2013-07-31 16:47:00 +0800 | [diff] [blame] | 3581 | |
| 3582 | /* Allocate PCI controller */ |
Gavin Shan | 58d714e | 2013-07-31 16:47:00 +0800 | [diff] [blame] | 3583 | phb->hose = hose = pcibios_alloc_controller(np); |
| 3584 | if (!phb->hose) { |
| 3585 | pr_err(" Can't allocate PCI controller for %s\n", |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3586 | np->full_name); |
Michael Ellerman | e39f223f | 2014-11-18 16:47:35 +1100 | [diff] [blame] | 3587 | memblock_free(__pa(phb), sizeof(struct pnv_phb)); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3588 | return; |
| 3589 | } |
| 3590 | |
| 3591 | spin_lock_init(&phb->lock); |
Gavin Shan | f1b7cc3 | 2013-07-31 16:47:01 +0800 | [diff] [blame] | 3592 | prop32 = of_get_property(np, "bus-range", &len); |
| 3593 | if (prop32 && len == 8) { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 3594 | hose->first_busno = be32_to_cpu(prop32[0]); |
| 3595 | hose->last_busno = be32_to_cpu(prop32[1]); |
Gavin Shan | f1b7cc3 | 2013-07-31 16:47:01 +0800 | [diff] [blame] | 3596 | } else { |
| 3597 | pr_warn(" Broken <bus-range> on %s\n", np->full_name); |
| 3598 | hose->first_busno = 0; |
| 3599 | hose->last_busno = 0xff; |
| 3600 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3601 | hose->private_data = phb; |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 3602 | phb->hub_id = hub_id; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3603 | phb->opal_id = phb_id; |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 3604 | phb->type = ioda_type; |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 3605 | mutex_init(&phb->ioda.pe_alloc_mutex); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3606 | |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 3607 | /* Detect specific models for error handling */ |
| 3608 | if (of_device_is_compatible(np, "ibm,p7ioc-pciex")) |
| 3609 | phb->model = PNV_PHB_MODEL_P7IOC; |
Benjamin Herrenschmidt | f3d40c2 | 2013-05-04 14:24:32 +0000 | [diff] [blame] | 3610 | else if (of_device_is_compatible(np, "ibm,power8-pciex")) |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 3611 | phb->model = PNV_PHB_MODEL_PHB3; |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 3612 | else if (of_device_is_compatible(np, "ibm,power8-npu-pciex")) |
| 3613 | phb->model = PNV_PHB_MODEL_NPU; |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 3614 | else |
| 3615 | phb->model = PNV_PHB_MODEL_UNKNOWN; |
| 3616 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 3617 | /* Parse 32-bit and IO ranges (if any) */ |
Gavin Shan | 2f1ec02 | 2013-07-31 16:47:02 +0800 | [diff] [blame] | 3618 | pci_process_bridge_OF_ranges(hose, np, !hose->global_number); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3619 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 3620 | /* Get registers */ |
Benjamin Herrenschmidt | fd141d1a | 2016-07-08 16:37:14 +1000 | [diff] [blame] | 3621 | if (!of_address_to_resource(np, 0, &r)) { |
| 3622 | phb->regs_phys = r.start; |
| 3623 | phb->regs = ioremap(r.start, resource_size(&r)); |
| 3624 | if (phb->regs == NULL) |
| 3625 | pr_err(" Failed to map registers !\n"); |
| 3626 | } |
Gavin Shan | 577c8c8 | 2016-05-20 16:41:28 +1000 | [diff] [blame] | 3627 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3628 | /* Initialize more IODA stuff */ |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 3629 | phb->ioda.total_pe_num = 1; |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 3630 | prop32 = of_get_property(np, "ibm,opal-num-pes", NULL); |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 3631 | if (prop32) |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 3632 | phb->ioda.total_pe_num = be32_to_cpup(prop32); |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 3633 | prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL); |
| 3634 | if (prop32) |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 3635 | phb->ioda.reserved_pe_idx = be32_to_cpup(prop32); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 3636 | |
Gavin Shan | c127562 | 2016-05-20 16:41:29 +1000 | [diff] [blame] | 3637 | /* Invalidate RID to PE# mapping */ |
| 3638 | for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++) |
| 3639 | phb->ioda.pe_rmap[segno] = IODA_INVALID_PE; |
| 3640 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 3641 | /* Parse 64-bit MMIO range */ |
| 3642 | pnv_ioda_parse_m64_window(phb); |
| 3643 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3644 | phb->ioda.m32_size = resource_size(&hose->mem_resources[0]); |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 3645 | /* FW Has already off top 64k of M32 space (MSI space) */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3646 | phb->ioda.m32_size += 0x10000; |
| 3647 | |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 3648 | phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num; |
Benjamin Herrenschmidt | 3fd47f0 | 2013-05-06 13:40:40 +1000 | [diff] [blame] | 3649 | phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0]; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3650 | phb->ioda.io_size = hose->pci_io_size; |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 3651 | phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3652 | phb->ioda.io_pci_base = 0; /* XXX calculate this ? */ |
| 3653 | |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 3654 | /* Calculate how many 32-bit TCE segments we have */ |
| 3655 | phb->ioda.dma32_count = phb->ioda.m32_pci_base / |
| 3656 | PNV_IODA1_DMA32_SEGSIZE; |
| 3657 | |
Gavin Shan | c35d2a8 | 2013-07-31 16:47:04 +0800 | [diff] [blame] | 3658 | /* Allocate aux data & arrays. We don't have IO ports on PHB3 */ |
Alexey Kardashevskiy | 92a8675 | 2016-05-12 15:47:09 +1000 | [diff] [blame] | 3659 | size = _ALIGN_UP(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8, |
| 3660 | sizeof(unsigned long)); |
Gavin Shan | 93289d8 | 2016-05-03 15:41:29 +1000 | [diff] [blame] | 3661 | m64map_off = size; |
| 3662 | size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3663 | m32map_off = size; |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 3664 | size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]); |
Gavin Shan | c35d2a8 | 2013-07-31 16:47:04 +0800 | [diff] [blame] | 3665 | if (phb->type == PNV_PHB_IODA1) { |
| 3666 | iomap_off = size; |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 3667 | size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]); |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 3668 | dma32map_off = size; |
| 3669 | size += phb->ioda.dma32_count * |
| 3670 | sizeof(phb->ioda.dma32_segmap[0]); |
Gavin Shan | c35d2a8 | 2013-07-31 16:47:04 +0800 | [diff] [blame] | 3671 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3672 | pemap_off = size; |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 3673 | size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe); |
Michael Ellerman | e39f223f | 2014-11-18 16:47:35 +1100 | [diff] [blame] | 3674 | aux = memblock_virt_alloc(size, 0); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3675 | phb->ioda.pe_alloc = aux; |
Gavin Shan | 93289d8 | 2016-05-03 15:41:29 +1000 | [diff] [blame] | 3676 | phb->ioda.m64_segmap = aux + m64map_off; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3677 | phb->ioda.m32_segmap = aux + m32map_off; |
Gavin Shan | 93289d8 | 2016-05-03 15:41:29 +1000 | [diff] [blame] | 3678 | for (segno = 0; segno < phb->ioda.total_pe_num; segno++) { |
| 3679 | phb->ioda.m64_segmap[segno] = IODA_INVALID_PE; |
Gavin Shan | 3fa23ff | 2016-05-03 15:41:26 +1000 | [diff] [blame] | 3680 | phb->ioda.m32_segmap[segno] = IODA_INVALID_PE; |
Gavin Shan | 93289d8 | 2016-05-03 15:41:29 +1000 | [diff] [blame] | 3681 | } |
Gavin Shan | 3fa23ff | 2016-05-03 15:41:26 +1000 | [diff] [blame] | 3682 | if (phb->type == PNV_PHB_IODA1) { |
Gavin Shan | c35d2a8 | 2013-07-31 16:47:04 +0800 | [diff] [blame] | 3683 | phb->ioda.io_segmap = aux + iomap_off; |
Gavin Shan | 3fa23ff | 2016-05-03 15:41:26 +1000 | [diff] [blame] | 3684 | for (segno = 0; segno < phb->ioda.total_pe_num; segno++) |
| 3685 | phb->ioda.io_segmap[segno] = IODA_INVALID_PE; |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 3686 | |
| 3687 | phb->ioda.dma32_segmap = aux + dma32map_off; |
| 3688 | for (segno = 0; segno < phb->ioda.dma32_count; segno++) |
| 3689 | phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE; |
Gavin Shan | 3fa23ff | 2016-05-03 15:41:26 +1000 | [diff] [blame] | 3690 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3691 | phb->ioda.pe_array = aux + pemap_off; |
Gavin Shan | 63803c3 | 2016-05-20 16:41:32 +1000 | [diff] [blame] | 3692 | |
| 3693 | /* |
| 3694 | * Choose PE number for root bus, which shouldn't have |
| 3695 | * M64 resources consumed by its child devices. To pick |
| 3696 | * the PE number adjacent to the reserved one if possible. |
| 3697 | */ |
| 3698 | pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx); |
| 3699 | if (phb->ioda.reserved_pe_idx == 0) { |
| 3700 | phb->ioda.root_pe_idx = 1; |
| 3701 | pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx); |
| 3702 | } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) { |
| 3703 | phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1; |
| 3704 | pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx); |
| 3705 | } else { |
| 3706 | phb->ioda.root_pe_idx = IODA_INVALID_PE; |
| 3707 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3708 | |
| 3709 | INIT_LIST_HEAD(&phb->ioda.pe_list); |
Wei Yang | 781a868 | 2015-03-25 16:23:57 +0800 | [diff] [blame] | 3710 | mutex_init(&phb->ioda.pe_list_mutex); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3711 | |
| 3712 | /* Calculate how many 32-bit TCE segments we have */ |
Gavin Shan | 2b923ed | 2016-05-05 12:04:16 +1000 | [diff] [blame] | 3713 | phb->ioda.dma32_count = phb->ioda.m32_pci_base / |
Gavin Shan | acce971 | 2016-05-03 15:41:33 +1000 | [diff] [blame] | 3714 | PNV_IODA1_DMA32_SEGSIZE; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3715 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 3716 | #if 0 /* We should really do that ... */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3717 | rc = opal_pci_set_phb_mem_window(opal->phb_id, |
| 3718 | window_type, |
| 3719 | window_num, |
| 3720 | starting_real_address, |
| 3721 | starting_pci_address, |
| 3722 | segment_size); |
| 3723 | #endif |
| 3724 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 3725 | pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n", |
Gavin Shan | 92b8f13 | 2016-05-03 15:41:24 +1000 | [diff] [blame] | 3726 | phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx, |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 3727 | phb->ioda.m32_size, phb->ioda.m32_segsize); |
| 3728 | if (phb->ioda.m64_size) |
| 3729 | pr_info(" M64: 0x%lx [segment=0x%lx]\n", |
| 3730 | phb->ioda.m64_size, phb->ioda.m64_segsize); |
| 3731 | if (phb->ioda.io_size) |
| 3732 | pr_info(" IO: 0x%x [segment=0x%x]\n", |
| 3733 | phb->ioda.io_size, phb->ioda.io_segsize); |
| 3734 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3735 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3736 | phb->hose->ops = &pnv_pci_ops; |
Gavin Shan | 49dec92 | 2014-07-21 14:42:33 +1000 | [diff] [blame] | 3737 | phb->get_pe_state = pnv_ioda_get_pe_state; |
| 3738 | phb->freeze_pe = pnv_ioda_freeze_pe; |
| 3739 | phb->unfreeze_pe = pnv_ioda_unfreeze_pe; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3740 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3741 | /* Setup MSI support */ |
| 3742 | pnv_pci_init_ioda_msis(phb); |
| 3743 | |
Gavin Shan | c40a421 | 2012-08-20 03:49:20 +0000 | [diff] [blame] | 3744 | /* |
| 3745 | * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here |
| 3746 | * to let the PCI core do resource assignment. It's supposed |
| 3747 | * that the PCI core will do correct I/O and MMIO alignment |
| 3748 | * for the P2P bridge bars so that each PCI bus (excluding |
| 3749 | * the child P2P bridges) can form individual PE. |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3750 | */ |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 3751 | ppc_md.pcibios_fixup = pnv_pci_ioda_fixup; |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 3752 | |
Alexey Kardashevskiy | f9f8345 | 2016-04-29 18:55:20 +1000 | [diff] [blame] | 3753 | if (phb->type == PNV_PHB_NPU) { |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 3754 | hose->controller_ops = pnv_npu_ioda_controller_ops; |
Alexey Kardashevskiy | f9f8345 | 2016-04-29 18:55:20 +1000 | [diff] [blame] | 3755 | } else { |
| 3756 | phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup; |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 3757 | hose->controller_ops = pnv_pci_ioda_controller_ops; |
Alexey Kardashevskiy | f9f8345 | 2016-04-29 18:55:20 +1000 | [diff] [blame] | 3758 | } |
Michael Ellerman | ad30cb9 | 2015-04-14 09:29:23 +1000 | [diff] [blame] | 3759 | |
Wei Yang | 6e628c7 | 2015-03-25 16:23:55 +0800 | [diff] [blame] | 3760 | #ifdef CONFIG_PCI_IOV |
| 3761 | ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources; |
Wei Yang | 5350ab3 | 2015-03-25 16:23:56 +0800 | [diff] [blame] | 3762 | ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment; |
Michael Ellerman | ad30cb9 | 2015-04-14 09:29:23 +1000 | [diff] [blame] | 3763 | #endif |
| 3764 | |
Gavin Shan | c40a421 | 2012-08-20 03:49:20 +0000 | [diff] [blame] | 3765 | pci_add_flags(PCI_REASSIGN_ALL_RSRC); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3766 | |
| 3767 | /* Reset IODA tables to a clean state */ |
Gavin Shan | d1a85ee | 2014-09-30 12:39:05 +1000 | [diff] [blame] | 3768 | rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3769 | if (rc) |
Benjamin Herrenschmidt | f11fe55 | 2011-11-29 18:22:50 +0000 | [diff] [blame] | 3770 | pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc); |
Gavin Shan | 361f2a2 | 2014-04-24 18:00:25 +1000 | [diff] [blame] | 3771 | |
Andrew Donnellan | 6060e9e | 2016-09-16 20:39:44 +1000 | [diff] [blame^] | 3772 | /* |
| 3773 | * If we're running in kdump kernel, the previous kernel never |
Gavin Shan | 361f2a2 | 2014-04-24 18:00:25 +1000 | [diff] [blame] | 3774 | * shutdown PCI devices correctly. We already got IODA table |
| 3775 | * cleaned out. So we have to issue PHB reset to stop all PCI |
Andrew Donnellan | 6060e9e | 2016-09-16 20:39:44 +1000 | [diff] [blame^] | 3776 | * transactions from previous kernel. |
Gavin Shan | 361f2a2 | 2014-04-24 18:00:25 +1000 | [diff] [blame] | 3777 | */ |
| 3778 | if (is_kdump_kernel()) { |
| 3779 | pr_info(" Issue PHB reset ...\n"); |
Gavin Shan | cadf364 | 2015-02-16 14:45:47 +1100 | [diff] [blame] | 3780 | pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL); |
| 3781 | pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE); |
Gavin Shan | 361f2a2 | 2014-04-24 18:00:25 +1000 | [diff] [blame] | 3782 | } |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 3783 | |
Gavin Shan | 9e9e893 | 2014-11-12 13:36:05 +1100 | [diff] [blame] | 3784 | /* Remove M64 resource if we can't configure it successfully */ |
| 3785 | if (!phb->init_m64 || phb->init_m64(phb)) |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 3786 | hose->mem_resources[1].flags = 0; |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Bjorn Helgaas | 6797500 | 2013-07-02 12:20:03 -0600 | [diff] [blame] | 3789 | void __init pnv_pci_init_ioda2_phb(struct device_node *np) |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 3790 | { |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 3791 | pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
Alistair Popple | 5d2aa71 | 2015-12-17 13:43:13 +1100 | [diff] [blame] | 3794 | void __init pnv_pci_init_npu_phb(struct device_node *np) |
| 3795 | { |
| 3796 | pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU); |
| 3797 | } |
| 3798 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3799 | void __init pnv_pci_init_ioda_hub(struct device_node *np) |
| 3800 | { |
| 3801 | struct device_node *phbn; |
Alistair Popple | c681b93 | 2013-09-23 12:04:57 +1000 | [diff] [blame] | 3802 | const __be64 *prop64; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3803 | u64 hub_id; |
| 3804 | |
| 3805 | pr_info("Probing IODA IO-Hub %s\n", np->full_name); |
| 3806 | |
| 3807 | prop64 = of_get_property(np, "ibm,opal-hubid", NULL); |
| 3808 | if (!prop64) { |
| 3809 | pr_err(" Missing \"ibm,opal-hubid\" property !\n"); |
| 3810 | return; |
| 3811 | } |
| 3812 | hub_id = be64_to_cpup(prop64); |
| 3813 | pr_devel(" HUB-ID : 0x%016llx\n", hub_id); |
| 3814 | |
| 3815 | /* Count child PHBs */ |
| 3816 | for_each_child_of_node(np, phbn) { |
| 3817 | /* Look for IODA1 PHBs */ |
| 3818 | if (of_device_is_compatible(phbn, "ibm,ioda-phb")) |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 3819 | pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 3820 | } |
| 3821 | } |