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