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