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