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> |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 26 | |
| 27 | #include <asm/sections.h> |
| 28 | #include <asm/io.h> |
| 29 | #include <asm/prom.h> |
| 30 | #include <asm/pci-bridge.h> |
| 31 | #include <asm/machdep.h> |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 32 | #include <asm/msi_bitmap.h> |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 33 | #include <asm/ppc-pci.h> |
| 34 | #include <asm/opal.h> |
| 35 | #include <asm/iommu.h> |
| 36 | #include <asm/tce.h> |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame] | 37 | #include <asm/xics.h> |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 38 | #include <asm/debug.h> |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 39 | #include <asm/firmware.h> |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 40 | |
| 41 | #include "powernv.h" |
| 42 | #include "pci.h" |
| 43 | |
Joe Perches | 6d31c2fa | 2014-09-21 10:55:06 -0700 | [diff] [blame] | 44 | static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level, |
| 45 | const char *fmt, ...) |
| 46 | { |
| 47 | struct va_format vaf; |
| 48 | va_list args; |
| 49 | char pfix[32]; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 50 | |
Joe Perches | 6d31c2fa | 2014-09-21 10:55:06 -0700 | [diff] [blame] | 51 | va_start(args, fmt); |
| 52 | |
| 53 | vaf.fmt = fmt; |
| 54 | vaf.va = &args; |
| 55 | |
| 56 | if (pe->pdev) |
| 57 | strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix)); |
| 58 | else |
| 59 | sprintf(pfix, "%04x:%02x ", |
| 60 | pci_domain_nr(pe->pbus), pe->pbus->number); |
| 61 | |
| 62 | printk("%spci %s: [PE# %.3d] %pV", |
| 63 | level, pfix, pe->pe_number, &vaf); |
| 64 | |
| 65 | va_end(args); |
| 66 | } |
| 67 | |
| 68 | #define pe_err(pe, fmt, ...) \ |
| 69 | pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__) |
| 70 | #define pe_warn(pe, fmt, ...) \ |
| 71 | pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__) |
| 72 | #define pe_info(pe, fmt, ...) \ |
| 73 | pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 74 | |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 75 | /* |
| 76 | * stdcix is only supposed to be used in hypervisor real mode as per |
| 77 | * the architecture spec |
| 78 | */ |
| 79 | static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr) |
| 80 | { |
| 81 | __asm__ __volatile__("stdcix %0,0,%1" |
| 82 | : : "r" (val), "r" (paddr) : "memory"); |
| 83 | } |
| 84 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 85 | static inline bool pnv_pci_is_mem_pref_64(unsigned long flags) |
| 86 | { |
| 87 | return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) == |
| 88 | (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)); |
| 89 | } |
| 90 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 91 | static int pnv_ioda_alloc_pe(struct pnv_phb *phb) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 92 | { |
| 93 | unsigned long pe; |
| 94 | |
| 95 | do { |
| 96 | pe = find_next_zero_bit(phb->ioda.pe_alloc, |
| 97 | phb->ioda.total_pe, 0); |
| 98 | if (pe >= phb->ioda.total_pe) |
| 99 | return IODA_INVALID_PE; |
| 100 | } while(test_and_set_bit(pe, phb->ioda.pe_alloc)); |
| 101 | |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 102 | phb->ioda.pe_array[pe].phb = phb; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 103 | phb->ioda.pe_array[pe].pe_number = pe; |
| 104 | return pe; |
| 105 | } |
| 106 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 107 | static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 108 | { |
| 109 | WARN_ON(phb->ioda.pe_array[pe].pdev); |
| 110 | |
| 111 | memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe)); |
| 112 | clear_bit(pe, phb->ioda.pe_alloc); |
| 113 | } |
| 114 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 115 | /* The default M64 BAR is shared by all PEs */ |
| 116 | static int pnv_ioda2_init_m64(struct pnv_phb *phb) |
| 117 | { |
| 118 | const char *desc; |
| 119 | struct resource *r; |
| 120 | s64 rc; |
| 121 | |
| 122 | /* Configure the default M64 BAR */ |
| 123 | rc = opal_pci_set_phb_mem_window(phb->opal_id, |
| 124 | OPAL_M64_WINDOW_TYPE, |
| 125 | phb->ioda.m64_bar_idx, |
| 126 | phb->ioda.m64_base, |
| 127 | 0, /* unused */ |
| 128 | phb->ioda.m64_size); |
| 129 | if (rc != OPAL_SUCCESS) { |
| 130 | desc = "configuring"; |
| 131 | goto fail; |
| 132 | } |
| 133 | |
| 134 | /* Enable the default M64 BAR */ |
| 135 | rc = opal_pci_phb_mmio_enable(phb->opal_id, |
| 136 | OPAL_M64_WINDOW_TYPE, |
| 137 | phb->ioda.m64_bar_idx, |
| 138 | OPAL_ENABLE_M64_SPLIT); |
| 139 | if (rc != OPAL_SUCCESS) { |
| 140 | desc = "enabling"; |
| 141 | goto fail; |
| 142 | } |
| 143 | |
| 144 | /* Mark the M64 BAR assigned */ |
| 145 | set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc); |
| 146 | |
| 147 | /* |
| 148 | * Strip off the segment used by the reserved PE, which is |
| 149 | * expected to be 0 or last one of PE capabicity. |
| 150 | */ |
| 151 | r = &phb->hose->mem_resources[1]; |
| 152 | if (phb->ioda.reserved_pe == 0) |
| 153 | r->start += phb->ioda.m64_segsize; |
| 154 | else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1)) |
| 155 | r->end -= phb->ioda.m64_segsize; |
| 156 | else |
| 157 | pr_warn(" Cannot strip M64 segment for reserved PE#%d\n", |
| 158 | phb->ioda.reserved_pe); |
| 159 | |
| 160 | return 0; |
| 161 | |
| 162 | fail: |
| 163 | pr_warn(" Failure %lld %s M64 BAR#%d\n", |
| 164 | rc, desc, phb->ioda.m64_bar_idx); |
| 165 | opal_pci_phb_mmio_enable(phb->opal_id, |
| 166 | OPAL_M64_WINDOW_TYPE, |
| 167 | phb->ioda.m64_bar_idx, |
| 168 | OPAL_DISABLE_M64); |
| 169 | return -EIO; |
| 170 | } |
| 171 | |
| 172 | static void pnv_ioda2_alloc_m64_pe(struct pnv_phb *phb) |
| 173 | { |
| 174 | resource_size_t sgsz = phb->ioda.m64_segsize; |
| 175 | struct pci_dev *pdev; |
| 176 | struct resource *r; |
| 177 | int base, step, i; |
| 178 | |
| 179 | /* |
| 180 | * Root bus always has full M64 range and root port has |
| 181 | * M64 range used in reality. So we're checking root port |
| 182 | * instead of root bus. |
| 183 | */ |
| 184 | list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) { |
| 185 | for (i = PCI_BRIDGE_RESOURCES; |
| 186 | i <= PCI_BRIDGE_RESOURCE_END; i++) { |
| 187 | r = &pdev->resource[i]; |
| 188 | if (!r->parent || |
| 189 | !pnv_pci_is_mem_pref_64(r->flags)) |
| 190 | continue; |
| 191 | |
| 192 | base = (r->start - phb->ioda.m64_base) / sgsz; |
| 193 | for (step = 0; step < resource_size(r) / sgsz; step++) |
| 194 | set_bit(base + step, phb->ioda.pe_alloc); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb, |
| 200 | struct pci_bus *bus, int all) |
| 201 | { |
| 202 | resource_size_t segsz = phb->ioda.m64_segsize; |
| 203 | struct pci_dev *pdev; |
| 204 | struct resource *r; |
| 205 | struct pnv_ioda_pe *master_pe, *pe; |
| 206 | unsigned long size, *pe_alloc; |
| 207 | bool found; |
| 208 | int start, i, j; |
| 209 | |
| 210 | /* Root bus shouldn't use M64 */ |
| 211 | if (pci_is_root_bus(bus)) |
| 212 | return IODA_INVALID_PE; |
| 213 | |
| 214 | /* We support only one M64 window on each bus */ |
| 215 | found = false; |
| 216 | pci_bus_for_each_resource(bus, r, i) { |
| 217 | if (r && r->parent && |
| 218 | pnv_pci_is_mem_pref_64(r->flags)) { |
| 219 | found = true; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* No M64 window found ? */ |
| 225 | if (!found) |
| 226 | return IODA_INVALID_PE; |
| 227 | |
| 228 | /* Allocate bitmap */ |
| 229 | size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long)); |
| 230 | pe_alloc = kzalloc(size, GFP_KERNEL); |
| 231 | if (!pe_alloc) { |
| 232 | pr_warn("%s: Out of memory !\n", |
| 233 | __func__); |
| 234 | return IODA_INVALID_PE; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Figure out reserved PE numbers by the PE |
| 239 | * the its child PEs. |
| 240 | */ |
| 241 | start = (r->start - phb->ioda.m64_base) / segsz; |
| 242 | for (i = 0; i < resource_size(r) / segsz; i++) |
| 243 | set_bit(start + i, pe_alloc); |
| 244 | |
| 245 | if (all) |
| 246 | goto done; |
| 247 | |
| 248 | /* |
| 249 | * If the PE doesn't cover all subordinate buses, |
| 250 | * we need subtract from reserved PEs for children. |
| 251 | */ |
| 252 | list_for_each_entry(pdev, &bus->devices, bus_list) { |
| 253 | if (!pdev->subordinate) |
| 254 | continue; |
| 255 | |
| 256 | pci_bus_for_each_resource(pdev->subordinate, r, i) { |
| 257 | if (!r || !r->parent || |
| 258 | !pnv_pci_is_mem_pref_64(r->flags)) |
| 259 | continue; |
| 260 | |
| 261 | start = (r->start - phb->ioda.m64_base) / segsz; |
| 262 | for (j = 0; j < resource_size(r) / segsz ; j++) |
| 263 | clear_bit(start + j, pe_alloc); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * the current bus might not own M64 window and that's all |
| 269 | * contributed by its child buses. For the case, we needn't |
| 270 | * pick M64 dependent PE#. |
| 271 | */ |
| 272 | if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) { |
| 273 | kfree(pe_alloc); |
| 274 | return IODA_INVALID_PE; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Figure out the master PE and put all slave PEs to master |
| 279 | * PE's list to form compound PE. |
| 280 | */ |
| 281 | done: |
| 282 | master_pe = NULL; |
| 283 | i = -1; |
| 284 | while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) < |
| 285 | phb->ioda.total_pe) { |
| 286 | pe = &phb->ioda.pe_array[i]; |
| 287 | pe->phb = phb; |
| 288 | pe->pe_number = i; |
| 289 | |
| 290 | if (!master_pe) { |
| 291 | pe->flags |= PNV_IODA_PE_MASTER; |
| 292 | INIT_LIST_HEAD(&pe->slaves); |
| 293 | master_pe = pe; |
| 294 | } else { |
| 295 | pe->flags |= PNV_IODA_PE_SLAVE; |
| 296 | pe->master = master_pe; |
| 297 | list_add_tail(&pe->list, &master_pe->slaves); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | kfree(pe_alloc); |
| 302 | return master_pe->pe_number; |
| 303 | } |
| 304 | |
| 305 | static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb) |
| 306 | { |
| 307 | struct pci_controller *hose = phb->hose; |
| 308 | struct device_node *dn = hose->dn; |
| 309 | struct resource *res; |
| 310 | const u32 *r; |
| 311 | u64 pci_addr; |
| 312 | |
| 313 | if (!firmware_has_feature(FW_FEATURE_OPALv3)) { |
| 314 | pr_info(" Firmware too old to support M64 window\n"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | r = of_get_property(dn, "ibm,opal-m64-window", NULL); |
| 319 | if (!r) { |
| 320 | pr_info(" No <ibm,opal-m64-window> on %s\n", |
| 321 | dn->full_name); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | /* FIXME: Support M64 for P7IOC */ |
| 326 | if (phb->type != PNV_PHB_IODA2) { |
| 327 | pr_info(" Not support M64 window\n"); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | res = &hose->mem_resources[1]; |
| 332 | res->start = of_translate_address(dn, r + 2); |
| 333 | res->end = res->start + of_read_number(r + 4, 2) - 1; |
| 334 | res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH); |
| 335 | pci_addr = of_read_number(r, 2); |
| 336 | hose->mem_offset[1] = res->start - pci_addr; |
| 337 | |
| 338 | phb->ioda.m64_size = resource_size(res); |
| 339 | phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe; |
| 340 | phb->ioda.m64_base = pci_addr; |
| 341 | |
| 342 | /* Use last M64 BAR to cover M64 window */ |
| 343 | phb->ioda.m64_bar_idx = 15; |
| 344 | phb->init_m64 = pnv_ioda2_init_m64; |
| 345 | phb->alloc_m64_pe = pnv_ioda2_alloc_m64_pe; |
| 346 | phb->pick_m64_pe = pnv_ioda2_pick_m64_pe; |
| 347 | } |
| 348 | |
Gavin Shan | 49dec92 | 2014-07-21 14:42:33 +1000 | [diff] [blame] | 349 | static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no) |
| 350 | { |
| 351 | struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no]; |
| 352 | struct pnv_ioda_pe *slave; |
| 353 | s64 rc; |
| 354 | |
| 355 | /* Fetch master PE */ |
| 356 | if (pe->flags & PNV_IODA_PE_SLAVE) { |
| 357 | pe = pe->master; |
| 358 | WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)); |
| 359 | pe_no = pe->pe_number; |
| 360 | } |
| 361 | |
| 362 | /* Freeze master PE */ |
| 363 | rc = opal_pci_eeh_freeze_set(phb->opal_id, |
| 364 | pe_no, |
| 365 | OPAL_EEH_ACTION_SET_FREEZE_ALL); |
| 366 | if (rc != OPAL_SUCCESS) { |
| 367 | pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n", |
| 368 | __func__, rc, phb->hose->global_number, pe_no); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | /* Freeze slave PEs */ |
| 373 | if (!(pe->flags & PNV_IODA_PE_MASTER)) |
| 374 | return; |
| 375 | |
| 376 | list_for_each_entry(slave, &pe->slaves, list) { |
| 377 | rc = opal_pci_eeh_freeze_set(phb->opal_id, |
| 378 | slave->pe_number, |
| 379 | OPAL_EEH_ACTION_SET_FREEZE_ALL); |
| 380 | if (rc != OPAL_SUCCESS) |
| 381 | pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n", |
| 382 | __func__, rc, phb->hose->global_number, |
| 383 | slave->pe_number); |
| 384 | } |
| 385 | } |
| 386 | |
Anton Blanchard | e51df2c | 2014-08-20 08:55:18 +1000 | [diff] [blame] | 387 | 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] | 388 | { |
| 389 | struct pnv_ioda_pe *pe, *slave; |
| 390 | s64 rc; |
| 391 | |
| 392 | /* Find master PE */ |
| 393 | pe = &phb->ioda.pe_array[pe_no]; |
| 394 | if (pe->flags & PNV_IODA_PE_SLAVE) { |
| 395 | pe = pe->master; |
| 396 | WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)); |
| 397 | pe_no = pe->pe_number; |
| 398 | } |
| 399 | |
| 400 | /* Clear frozen state for master PE */ |
| 401 | rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt); |
| 402 | if (rc != OPAL_SUCCESS) { |
| 403 | pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n", |
| 404 | __func__, rc, opt, phb->hose->global_number, pe_no); |
| 405 | return -EIO; |
| 406 | } |
| 407 | |
| 408 | if (!(pe->flags & PNV_IODA_PE_MASTER)) |
| 409 | return 0; |
| 410 | |
| 411 | /* Clear frozen state for slave PEs */ |
| 412 | list_for_each_entry(slave, &pe->slaves, list) { |
| 413 | rc = opal_pci_eeh_freeze_clear(phb->opal_id, |
| 414 | slave->pe_number, |
| 415 | opt); |
| 416 | if (rc != OPAL_SUCCESS) { |
| 417 | pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n", |
| 418 | __func__, rc, opt, phb->hose->global_number, |
| 419 | slave->pe_number); |
| 420 | return -EIO; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no) |
| 428 | { |
| 429 | struct pnv_ioda_pe *slave, *pe; |
| 430 | u8 fstate, state; |
| 431 | __be16 pcierr; |
| 432 | s64 rc; |
| 433 | |
| 434 | /* Sanity check on PE number */ |
| 435 | if (pe_no < 0 || pe_no >= phb->ioda.total_pe) |
| 436 | return OPAL_EEH_STOPPED_PERM_UNAVAIL; |
| 437 | |
| 438 | /* |
| 439 | * Fetch the master PE and the PE instance might be |
| 440 | * not initialized yet. |
| 441 | */ |
| 442 | pe = &phb->ioda.pe_array[pe_no]; |
| 443 | if (pe->flags & PNV_IODA_PE_SLAVE) { |
| 444 | pe = pe->master; |
| 445 | WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)); |
| 446 | pe_no = pe->pe_number; |
| 447 | } |
| 448 | |
| 449 | /* Check the master PE */ |
| 450 | rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, |
| 451 | &state, &pcierr, NULL); |
| 452 | if (rc != OPAL_SUCCESS) { |
| 453 | pr_warn("%s: Failure %lld getting " |
| 454 | "PHB#%x-PE#%x state\n", |
| 455 | __func__, rc, |
| 456 | phb->hose->global_number, pe_no); |
| 457 | return OPAL_EEH_STOPPED_TEMP_UNAVAIL; |
| 458 | } |
| 459 | |
| 460 | /* Check the slave PE */ |
| 461 | if (!(pe->flags & PNV_IODA_PE_MASTER)) |
| 462 | return state; |
| 463 | |
| 464 | list_for_each_entry(slave, &pe->slaves, list) { |
| 465 | rc = opal_pci_eeh_freeze_status(phb->opal_id, |
| 466 | slave->pe_number, |
| 467 | &fstate, |
| 468 | &pcierr, |
| 469 | NULL); |
| 470 | if (rc != OPAL_SUCCESS) { |
| 471 | pr_warn("%s: Failure %lld getting " |
| 472 | "PHB#%x-PE#%x state\n", |
| 473 | __func__, rc, |
| 474 | phb->hose->global_number, slave->pe_number); |
| 475 | return OPAL_EEH_STOPPED_TEMP_UNAVAIL; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Override the result based on the ascending |
| 480 | * priority. |
| 481 | */ |
| 482 | if (fstate > state) |
| 483 | state = fstate; |
| 484 | } |
| 485 | |
| 486 | return state; |
| 487 | } |
| 488 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 489 | /* Currently those 2 are only used when MSIs are enabled, this will change |
| 490 | * but in the meantime, we need to protect them to avoid warnings |
| 491 | */ |
| 492 | #ifdef CONFIG_PCI_MSI |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 493 | 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] | 494 | { |
| 495 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 496 | struct pnv_phb *phb = hose->private_data; |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 497 | struct pci_dn *pdn = pci_get_pdn(dev); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 498 | |
| 499 | if (!pdn) |
| 500 | return NULL; |
| 501 | if (pdn->pe_number == IODA_INVALID_PE) |
| 502 | return NULL; |
| 503 | return &phb->ioda.pe_array[pdn->pe_number]; |
| 504 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 505 | #endif /* CONFIG_PCI_MSI */ |
| 506 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 507 | 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] | 508 | { |
| 509 | struct pci_dev *parent; |
| 510 | uint8_t bcomp, dcomp, fcomp; |
| 511 | long rc, rid_end, rid; |
| 512 | |
| 513 | /* Bus validation ? */ |
| 514 | if (pe->pbus) { |
| 515 | int count; |
| 516 | |
| 517 | dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER; |
| 518 | fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER; |
| 519 | parent = pe->pbus->self; |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 520 | if (pe->flags & PNV_IODA_PE_BUS_ALL) |
| 521 | count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1; |
| 522 | else |
| 523 | count = 1; |
| 524 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 525 | switch(count) { |
| 526 | case 1: bcomp = OpalPciBusAll; break; |
| 527 | case 2: bcomp = OpalPciBus7Bits; break; |
| 528 | case 4: bcomp = OpalPciBus6Bits; break; |
| 529 | case 8: bcomp = OpalPciBus5Bits; break; |
| 530 | case 16: bcomp = OpalPciBus4Bits; break; |
| 531 | case 32: bcomp = OpalPciBus3Bits; break; |
| 532 | default: |
| 533 | pr_err("%s: Number of subordinate busses %d" |
| 534 | " unsupported\n", |
| 535 | pci_name(pe->pbus->self), count); |
| 536 | /* Do an exact match only */ |
| 537 | bcomp = OpalPciBusAll; |
| 538 | } |
| 539 | rid_end = pe->rid + (count << 8); |
| 540 | } else { |
| 541 | parent = pe->pdev->bus->self; |
| 542 | bcomp = OpalPciBusAll; |
| 543 | dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER; |
| 544 | fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER; |
| 545 | rid_end = pe->rid + 1; |
| 546 | } |
| 547 | |
Gavin Shan | 631ad69 | 2013-11-04 16:32:46 +0800 | [diff] [blame] | 548 | /* |
| 549 | * Associate PE in PELT. We need add the PE into the |
| 550 | * corresponding PELT-V as well. Otherwise, the error |
| 551 | * originated from the PE might contribute to other |
| 552 | * PEs. |
| 553 | */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 554 | rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid, |
| 555 | bcomp, dcomp, fcomp, OPAL_MAP_PE); |
| 556 | if (rc) { |
| 557 | pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc); |
| 558 | return -ENXIO; |
| 559 | } |
Gavin Shan | 631ad69 | 2013-11-04 16:32:46 +0800 | [diff] [blame] | 560 | |
| 561 | rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number, |
| 562 | pe->pe_number, OPAL_ADD_PE_TO_DOMAIN); |
| 563 | if (rc) |
| 564 | pe_warn(pe, "OPAL error %d adding self to PELTV\n", rc); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 565 | opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number, |
| 566 | OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); |
| 567 | |
| 568 | /* Add to all parents PELT-V */ |
| 569 | while (parent) { |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 570 | struct pci_dn *pdn = pci_get_pdn(parent); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 571 | if (pdn && pdn->pe_number != IODA_INVALID_PE) { |
| 572 | rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number, |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 573 | pe->pe_number, OPAL_ADD_PE_TO_DOMAIN); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 574 | /* XXX What to do in case of error ? */ |
| 575 | } |
| 576 | parent = parent->bus->self; |
| 577 | } |
| 578 | /* Setup reverse map */ |
| 579 | for (rid = pe->rid; rid < rid_end; rid++) |
| 580 | phb->ioda.pe_rmap[rid] = pe->pe_number; |
| 581 | |
| 582 | /* Setup one MVTs on IODA1 */ |
| 583 | if (phb->type == PNV_PHB_IODA1) { |
| 584 | pe->mve_number = pe->pe_number; |
| 585 | rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, |
| 586 | pe->pe_number); |
| 587 | if (rc) { |
| 588 | pe_err(pe, "OPAL error %ld setting up MVE %d\n", |
| 589 | rc, pe->mve_number); |
| 590 | pe->mve_number = -1; |
| 591 | } else { |
| 592 | rc = opal_pci_set_mve_enable(phb->opal_id, |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 593 | pe->mve_number, OPAL_ENABLE_MVE); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 594 | if (rc) { |
| 595 | pe_err(pe, "OPAL error %ld enabling MVE %d\n", |
| 596 | rc, pe->mve_number); |
| 597 | pe->mve_number = -1; |
| 598 | } |
| 599 | } |
| 600 | } else if (phb->type == PNV_PHB_IODA2) |
| 601 | pe->mve_number = 0; |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 606 | static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb, |
| 607 | struct pnv_ioda_pe *pe) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 608 | { |
| 609 | struct pnv_ioda_pe *lpe; |
| 610 | |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 611 | list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) { |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 612 | if (lpe->dma_weight < pe->dma_weight) { |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 613 | list_add_tail(&pe->dma_link, &lpe->dma_link); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | } |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 617 | list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev) |
| 621 | { |
| 622 | /* This is quite simplistic. The "base" weight of a device |
| 623 | * is 10. 0 means no DMA is to be accounted for it. |
| 624 | */ |
| 625 | |
| 626 | /* If it's a bridge, no DMA */ |
| 627 | if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) |
| 628 | return 0; |
| 629 | |
| 630 | /* Reduce the weight of slow USB controllers */ |
| 631 | if (dev->class == PCI_CLASS_SERIAL_USB_UHCI || |
| 632 | dev->class == PCI_CLASS_SERIAL_USB_OHCI || |
| 633 | dev->class == PCI_CLASS_SERIAL_USB_EHCI) |
| 634 | return 3; |
| 635 | |
| 636 | /* Increase the weight of RAID (includes Obsidian) */ |
| 637 | if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID) |
| 638 | return 15; |
| 639 | |
| 640 | /* Default */ |
| 641 | return 10; |
| 642 | } |
| 643 | |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 644 | #if 0 |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 645 | 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] | 646 | { |
| 647 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 648 | struct pnv_phb *phb = hose->private_data; |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 649 | struct pci_dn *pdn = pci_get_pdn(dev); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 650 | struct pnv_ioda_pe *pe; |
| 651 | int pe_num; |
| 652 | |
| 653 | if (!pdn) { |
| 654 | pr_err("%s: Device tree node not associated properly\n", |
| 655 | pci_name(dev)); |
| 656 | return NULL; |
| 657 | } |
| 658 | if (pdn->pe_number != IODA_INVALID_PE) |
| 659 | return NULL; |
| 660 | |
| 661 | /* PE#0 has been pre-set */ |
| 662 | if (dev->bus->number == 0) |
| 663 | pe_num = 0; |
| 664 | else |
| 665 | pe_num = pnv_ioda_alloc_pe(phb); |
| 666 | if (pe_num == IODA_INVALID_PE) { |
| 667 | pr_warning("%s: Not enough PE# available, disabling device\n", |
| 668 | pci_name(dev)); |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | /* NOTE: We get only one ref to the pci_dev for the pdn, not for the |
| 673 | * pointer in the PE data structure, both should be destroyed at the |
| 674 | * same time. However, this needs to be looked at more closely again |
| 675 | * once we actually start removing things (Hotplug, SR-IOV, ...) |
| 676 | * |
| 677 | * At some point we want to remove the PDN completely anyways |
| 678 | */ |
| 679 | pe = &phb->ioda.pe_array[pe_num]; |
| 680 | pci_dev_get(dev); |
| 681 | pdn->pcidev = dev; |
| 682 | pdn->pe_number = pe_num; |
| 683 | pe->pdev = dev; |
| 684 | pe->pbus = NULL; |
| 685 | pe->tce32_seg = -1; |
| 686 | pe->mve_number = -1; |
| 687 | pe->rid = dev->bus->number << 8 | pdn->devfn; |
| 688 | |
| 689 | pe_info(pe, "Associated device to PE\n"); |
| 690 | |
| 691 | if (pnv_ioda_configure_pe(phb, pe)) { |
| 692 | /* XXX What do we do here ? */ |
| 693 | if (pe_num) |
| 694 | pnv_ioda_free_pe(phb, pe_num); |
| 695 | pdn->pe_number = IODA_INVALID_PE; |
| 696 | pe->pdev = NULL; |
| 697 | pci_dev_put(dev); |
| 698 | return NULL; |
| 699 | } |
| 700 | |
| 701 | /* Assign a DMA weight to the device */ |
| 702 | pe->dma_weight = pnv_ioda_dma_weight(dev); |
| 703 | if (pe->dma_weight != 0) { |
| 704 | phb->ioda.dma_weight += pe->dma_weight; |
| 705 | phb->ioda.dma_pe_count++; |
| 706 | } |
| 707 | |
| 708 | /* Link the PE */ |
| 709 | pnv_ioda_link_pe_by_weight(phb, pe); |
| 710 | |
| 711 | return pe; |
| 712 | } |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 713 | #endif /* Useful for SRIOV case */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 714 | |
| 715 | static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe) |
| 716 | { |
| 717 | struct pci_dev *dev; |
| 718 | |
| 719 | list_for_each_entry(dev, &bus->devices, bus_list) { |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 720 | struct pci_dn *pdn = pci_get_pdn(dev); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 721 | |
| 722 | if (pdn == NULL) { |
| 723 | pr_warn("%s: No device node associated with device !\n", |
| 724 | pci_name(dev)); |
| 725 | continue; |
| 726 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 727 | pdn->pcidev = dev; |
| 728 | pdn->pe_number = pe->pe_number; |
| 729 | pe->dma_weight += pnv_ioda_dma_weight(dev); |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 730 | if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 731 | pnv_ioda_setup_same_PE(dev->subordinate, pe); |
| 732 | } |
| 733 | } |
| 734 | |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 735 | /* |
| 736 | * There're 2 types of PCI bus sensitive PEs: One that is compromised of |
| 737 | * single PCI bus. Another one that contains the primary PCI bus and its |
| 738 | * subordinate PCI devices and buses. The second type of PE is normally |
| 739 | * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports. |
| 740 | */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 741 | static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 742 | { |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 743 | struct pci_controller *hose = pci_bus_to_host(bus); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 744 | struct pnv_phb *phb = hose->private_data; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 745 | struct pnv_ioda_pe *pe; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 746 | int pe_num = IODA_INVALID_PE; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 747 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 748 | /* Check if PE is determined by M64 */ |
| 749 | if (phb->pick_m64_pe) |
| 750 | pe_num = phb->pick_m64_pe(phb, bus, all); |
| 751 | |
| 752 | /* The PE number isn't pinned by M64 */ |
| 753 | if (pe_num == IODA_INVALID_PE) |
| 754 | pe_num = pnv_ioda_alloc_pe(phb); |
| 755 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 756 | if (pe_num == IODA_INVALID_PE) { |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 757 | pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n", |
| 758 | __func__, pci_domain_nr(bus), bus->number); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 759 | return; |
| 760 | } |
| 761 | |
| 762 | pe = &phb->ioda.pe_array[pe_num]; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 763 | pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 764 | pe->pbus = bus; |
| 765 | pe->pdev = NULL; |
| 766 | pe->tce32_seg = -1; |
| 767 | pe->mve_number = -1; |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 768 | pe->rid = bus->busn_res.start << 8; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 769 | pe->dma_weight = 0; |
| 770 | |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 771 | if (all) |
| 772 | pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n", |
| 773 | bus->busn_res.start, bus->busn_res.end, pe_num); |
| 774 | else |
| 775 | pe_info(pe, "Secondary bus %d associated with PE#%d\n", |
| 776 | bus->busn_res.start, pe_num); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 777 | |
| 778 | if (pnv_ioda_configure_pe(phb, pe)) { |
| 779 | /* XXX What do we do here ? */ |
| 780 | if (pe_num) |
| 781 | pnv_ioda_free_pe(phb, pe_num); |
| 782 | pe->pbus = NULL; |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | /* Associate it with all child devices */ |
| 787 | pnv_ioda_setup_same_PE(bus, pe); |
| 788 | |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 789 | /* Put PE to the list */ |
| 790 | list_add_tail(&pe->list, &phb->ioda.pe_list); |
| 791 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 792 | /* Account for one DMA PE if at least one DMA capable device exist |
| 793 | * below the bridge |
| 794 | */ |
| 795 | if (pe->dma_weight != 0) { |
| 796 | phb->ioda.dma_weight += pe->dma_weight; |
| 797 | phb->ioda.dma_pe_count++; |
| 798 | } |
| 799 | |
| 800 | /* Link the PE */ |
| 801 | pnv_ioda_link_pe_by_weight(phb, pe); |
| 802 | } |
| 803 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 804 | static void pnv_ioda_setup_PEs(struct pci_bus *bus) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 805 | { |
| 806 | struct pci_dev *dev; |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 807 | |
| 808 | pnv_ioda_setup_bus_PE(bus, 0); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 809 | |
| 810 | list_for_each_entry(dev, &bus->devices, bus_list) { |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 811 | if (dev->subordinate) { |
| 812 | if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) |
| 813 | pnv_ioda_setup_bus_PE(dev->subordinate, 1); |
| 814 | else |
| 815 | pnv_ioda_setup_PEs(dev->subordinate); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Configure PEs so that the downstream PCI buses and devices |
| 822 | * could have their associated PE#. Unfortunately, we didn't |
| 823 | * figure out the way to identify the PLX bridge yet. So we |
| 824 | * simply put the PCI bus and the subordinate behind the root |
| 825 | * port to PE# here. The game rule here is expected to be changed |
| 826 | * as soon as we can detected PLX bridge correctly. |
| 827 | */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 828 | static void pnv_pci_ioda_setup_PEs(void) |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 829 | { |
| 830 | struct pci_controller *hose, *tmp; |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 831 | struct pnv_phb *phb; |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 832 | |
| 833 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 834 | phb = hose->private_data; |
| 835 | |
| 836 | /* M64 layout might affect PE allocation */ |
| 837 | if (phb->alloc_m64_pe) |
| 838 | phb->alloc_m64_pe(phb); |
| 839 | |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 840 | pnv_ioda_setup_PEs(hose->bus); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Gavin Shan | 959c9bd | 2013-04-25 19:21:02 +0000 | [diff] [blame] | 844 | 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] | 845 | { |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 846 | struct pci_dn *pdn = pci_get_pdn(pdev); |
Gavin Shan | 959c9bd | 2013-04-25 19:21:02 +0000 | [diff] [blame] | 847 | struct pnv_ioda_pe *pe; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 848 | |
Gavin Shan | 959c9bd | 2013-04-25 19:21:02 +0000 | [diff] [blame] | 849 | /* |
| 850 | * The function can be called while the PE# |
| 851 | * hasn't been assigned. Do nothing for the |
| 852 | * case. |
| 853 | */ |
| 854 | if (!pdn || pdn->pe_number == IODA_INVALID_PE) |
| 855 | return; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 856 | |
Gavin Shan | 959c9bd | 2013-04-25 19:21:02 +0000 | [diff] [blame] | 857 | pe = &phb->ioda.pe_array[pdn->pe_number]; |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 858 | WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops); |
Gavin Shan | 763fe0a | 2014-08-06 17:10:16 +1000 | [diff] [blame] | 859 | set_iommu_table_base_and_group(&pdev->dev, &pe->tce32_table); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 862 | static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb, |
| 863 | struct pci_dev *pdev, u64 dma_mask) |
| 864 | { |
| 865 | struct pci_dn *pdn = pci_get_pdn(pdev); |
| 866 | struct pnv_ioda_pe *pe; |
| 867 | uint64_t top; |
| 868 | bool bypass = false; |
| 869 | |
| 870 | if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE)) |
| 871 | return -ENODEV;; |
| 872 | |
| 873 | pe = &phb->ioda.pe_array[pdn->pe_number]; |
| 874 | if (pe->tce_bypass_enabled) { |
| 875 | top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1; |
| 876 | bypass = (dma_mask >= top); |
| 877 | } |
| 878 | |
| 879 | if (bypass) { |
| 880 | dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n"); |
| 881 | set_dma_ops(&pdev->dev, &dma_direct_ops); |
| 882 | set_dma_offset(&pdev->dev, pe->tce_bypass_base); |
| 883 | } else { |
| 884 | dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n"); |
| 885 | set_dma_ops(&pdev->dev, &dma_iommu_ops); |
| 886 | set_iommu_table_base(&pdev->dev, &pe->tce32_table); |
| 887 | } |
Brian W Hart | a32305b | 2014-07-31 14:24:37 -0500 | [diff] [blame] | 888 | *pdev->dev.dma_mask = dma_mask; |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 889 | return 0; |
| 890 | } |
| 891 | |
Gavin Shan | fe7e85c | 2014-09-30 12:39:10 +1000 | [diff] [blame^] | 892 | static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb, |
| 893 | struct pci_dev *pdev) |
| 894 | { |
| 895 | struct pci_dn *pdn = pci_get_pdn(pdev); |
| 896 | struct pnv_ioda_pe *pe; |
| 897 | u64 end, mask; |
| 898 | |
| 899 | if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE)) |
| 900 | return 0; |
| 901 | |
| 902 | pe = &phb->ioda.pe_array[pdn->pe_number]; |
| 903 | if (!pe->tce_bypass_enabled) |
| 904 | return __dma_get_required_mask(&pdev->dev); |
| 905 | |
| 906 | |
| 907 | end = pe->tce_bypass_base + memblock_end_of_DRAM(); |
| 908 | mask = 1ULL << (fls64(end) - 1); |
| 909 | mask += mask - 1; |
| 910 | |
| 911 | return mask; |
| 912 | } |
| 913 | |
Gavin Shan | dff4a39 | 2014-07-15 17:00:55 +1000 | [diff] [blame] | 914 | static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, |
| 915 | struct pci_bus *bus, |
| 916 | bool add_to_iommu_group) |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 917 | { |
| 918 | struct pci_dev *dev; |
| 919 | |
| 920 | list_for_each_entry(dev, &bus->devices, bus_list) { |
Gavin Shan | dff4a39 | 2014-07-15 17:00:55 +1000 | [diff] [blame] | 921 | if (add_to_iommu_group) |
| 922 | set_iommu_table_base_and_group(&dev->dev, |
| 923 | &pe->tce32_table); |
| 924 | else |
| 925 | set_iommu_table_base(&dev->dev, &pe->tce32_table); |
| 926 | |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 927 | if (dev->subordinate) |
Gavin Shan | dff4a39 | 2014-07-15 17:00:55 +1000 | [diff] [blame] | 928 | pnv_ioda_setup_bus_dma(pe, dev->subordinate, |
| 929 | add_to_iommu_group); |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 933 | static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe, |
| 934 | struct iommu_table *tbl, |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 935 | __be64 *startp, __be64 *endp, bool rm) |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 936 | { |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 937 | __be64 __iomem *invalidate = rm ? |
| 938 | (__be64 __iomem *)pe->tce_inval_reg_phys : |
| 939 | (__be64 __iomem *)tbl->it_index; |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 940 | unsigned long start, end, inc; |
Alexey Kardashevskiy | b0376c9 | 2014-06-06 18:44:01 +1000 | [diff] [blame] | 941 | const unsigned shift = tbl->it_page_shift; |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 942 | |
| 943 | start = __pa(startp); |
| 944 | end = __pa(endp); |
| 945 | |
| 946 | /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */ |
| 947 | if (tbl->it_busno) { |
Alexey Kardashevskiy | b0376c9 | 2014-06-06 18:44:01 +1000 | [diff] [blame] | 948 | start <<= shift; |
| 949 | end <<= shift; |
| 950 | inc = 128ull << shift; |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 951 | start |= tbl->it_busno; |
| 952 | end |= tbl->it_busno; |
| 953 | } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) { |
| 954 | /* p7ioc-style invalidation, 2 TCEs per write */ |
| 955 | start |= (1ull << 63); |
| 956 | end |= (1ull << 63); |
| 957 | inc = 16; |
| 958 | } else { |
| 959 | /* Default (older HW) */ |
| 960 | inc = 128; |
| 961 | } |
| 962 | |
| 963 | end |= inc - 1; /* round up end to be different than start */ |
| 964 | |
| 965 | mb(); /* Ensure above stores are visible */ |
| 966 | while (start <= end) { |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 967 | if (rm) |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 968 | __raw_rm_writeq(cpu_to_be64(start), invalidate); |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 969 | else |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 970 | __raw_writeq(cpu_to_be64(start), invalidate); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 971 | start += inc; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * The iommu layer will do another mb() for us on build() |
| 976 | * and we don't care on free() |
| 977 | */ |
| 978 | } |
| 979 | |
| 980 | static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe, |
| 981 | struct iommu_table *tbl, |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 982 | __be64 *startp, __be64 *endp, bool rm) |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 983 | { |
| 984 | unsigned long start, end, inc; |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 985 | __be64 __iomem *invalidate = rm ? |
| 986 | (__be64 __iomem *)pe->tce_inval_reg_phys : |
| 987 | (__be64 __iomem *)tbl->it_index; |
Alexey Kardashevskiy | b0376c9 | 2014-06-06 18:44:01 +1000 | [diff] [blame] | 988 | const unsigned shift = tbl->it_page_shift; |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 989 | |
| 990 | /* We'll invalidate DMA address in PE scope */ |
Alexey Kardashevskiy | b0376c9 | 2014-06-06 18:44:01 +1000 | [diff] [blame] | 991 | start = 0x2ull << 60; |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 992 | start |= (pe->pe_number & 0xFF); |
| 993 | end = start; |
| 994 | |
| 995 | /* Figure out the start, end and step */ |
| 996 | inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64)); |
Alexey Kardashevskiy | b0376c9 | 2014-06-06 18:44:01 +1000 | [diff] [blame] | 997 | start |= (inc << shift); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 998 | inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64)); |
Alexey Kardashevskiy | b0376c9 | 2014-06-06 18:44:01 +1000 | [diff] [blame] | 999 | end |= (inc << shift); |
| 1000 | inc = (0x1ull << shift); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1001 | mb(); |
| 1002 | |
| 1003 | while (start <= end) { |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1004 | if (rm) |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 1005 | __raw_rm_writeq(cpu_to_be64(start), invalidate); |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1006 | else |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 1007 | __raw_writeq(cpu_to_be64(start), invalidate); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1008 | start += inc; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl, |
Benjamin Herrenschmidt | 3ad26e5 | 2013-10-11 18:23:53 +1100 | [diff] [blame] | 1013 | __be64 *startp, __be64 *endp, bool rm) |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1014 | { |
| 1015 | struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe, |
| 1016 | tce32_table); |
| 1017 | struct pnv_phb *phb = pe->phb; |
| 1018 | |
| 1019 | if (phb->type == PNV_PHB_IODA1) |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1020 | pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1021 | else |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1022 | pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm); |
Gavin Shan | 4cce955 | 2013-04-25 19:21:00 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 1025 | static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb, |
| 1026 | struct pnv_ioda_pe *pe, unsigned int base, |
| 1027 | unsigned int segs) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1028 | { |
| 1029 | |
| 1030 | struct page *tce_mem = NULL; |
| 1031 | const __be64 *swinvp; |
| 1032 | struct iommu_table *tbl; |
| 1033 | unsigned int i; |
| 1034 | int64_t rc; |
| 1035 | void *addr; |
| 1036 | |
| 1037 | /* 256M DMA window, 4K TCE pages, 8 bytes TCE */ |
| 1038 | #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8) |
| 1039 | |
| 1040 | /* XXX FIXME: Handle 64-bit only DMA devices */ |
| 1041 | /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */ |
| 1042 | /* XXX FIXME: Allocate multi-level tables on PHB3 */ |
| 1043 | |
| 1044 | /* We shouldn't already have a 32-bit DMA associated */ |
| 1045 | if (WARN_ON(pe->tce32_seg >= 0)) |
| 1046 | return; |
| 1047 | |
| 1048 | /* Grab a 32-bit TCE table */ |
| 1049 | pe->tce32_seg = base; |
| 1050 | pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n", |
| 1051 | (base << 28), ((base + segs) << 28) - 1); |
| 1052 | |
| 1053 | /* XXX Currently, we allocate one big contiguous table for the |
| 1054 | * TCEs. We only really need one chunk per 256M of TCE space |
| 1055 | * (ie per segment) but that's an optimization for later, it |
| 1056 | * requires some added smarts with our get/put_tce implementation |
| 1057 | */ |
| 1058 | tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL, |
| 1059 | get_order(TCE32_TABLE_SIZE * segs)); |
| 1060 | if (!tce_mem) { |
| 1061 | pe_err(pe, " Failed to allocate a 32-bit TCE memory\n"); |
| 1062 | goto fail; |
| 1063 | } |
| 1064 | addr = page_address(tce_mem); |
| 1065 | memset(addr, 0, TCE32_TABLE_SIZE * segs); |
| 1066 | |
| 1067 | /* Configure HW */ |
| 1068 | for (i = 0; i < segs; i++) { |
| 1069 | rc = opal_pci_map_pe_dma_window(phb->opal_id, |
| 1070 | pe->pe_number, |
| 1071 | base + i, 1, |
| 1072 | __pa(addr) + TCE32_TABLE_SIZE * i, |
| 1073 | TCE32_TABLE_SIZE, 0x1000); |
| 1074 | if (rc) { |
| 1075 | pe_err(pe, " Failed to configure 32-bit TCE table," |
| 1076 | " err %ld\n", rc); |
| 1077 | goto fail; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /* Setup linux iommu table */ |
| 1082 | tbl = &pe->tce32_table; |
| 1083 | pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs, |
Alexey Kardashevskiy | 8fa5d45 | 2014-06-06 18:44:03 +1000 | [diff] [blame] | 1084 | base << 28, IOMMU_PAGE_SHIFT_4K); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1085 | |
| 1086 | /* OPAL variant of P7IOC SW invalidated TCEs */ |
| 1087 | swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL); |
| 1088 | if (swinvp) { |
| 1089 | /* We need a couple more fields -- an address and a data |
| 1090 | * to or. Since the bus is only printed out on table free |
| 1091 | * errors, and on the first pass the data will be a relative |
| 1092 | * bus number, print that out instead. |
| 1093 | */ |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1094 | pe->tce_inval_reg_phys = be64_to_cpup(swinvp); |
| 1095 | tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys, |
| 1096 | 8); |
Gavin Shan | 65fd766 | 2014-04-24 18:00:28 +1000 | [diff] [blame] | 1097 | tbl->it_type |= (TCE_PCI_SWINV_CREATE | |
| 1098 | TCE_PCI_SWINV_FREE | |
| 1099 | TCE_PCI_SWINV_PAIR); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1100 | } |
| 1101 | iommu_init_table(tbl, phb->hose->node); |
Gavin Shan | e9bc03f | 2014-04-24 18:00:29 +1000 | [diff] [blame] | 1102 | iommu_register_group(tbl, phb->hose->global_number, pe->pe_number); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1103 | |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 1104 | if (pe->pdev) |
Alexey Kardashevskiy | d905c5d | 2013-11-21 17:43:14 +1100 | [diff] [blame] | 1105 | set_iommu_table_base_and_group(&pe->pdev->dev, tbl); |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 1106 | else |
Gavin Shan | dff4a39 | 2014-07-15 17:00:55 +1000 | [diff] [blame] | 1107 | pnv_ioda_setup_bus_dma(pe, pe->pbus, true); |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 1108 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1109 | return; |
| 1110 | fail: |
| 1111 | /* XXX Failure: Try to fallback to 64-bit only ? */ |
| 1112 | if (pe->tce32_seg >= 0) |
| 1113 | pe->tce32_seg = -1; |
| 1114 | if (tce_mem) |
| 1115 | __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs)); |
| 1116 | } |
| 1117 | |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1118 | static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable) |
| 1119 | { |
| 1120 | struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe, |
| 1121 | tce32_table); |
| 1122 | uint16_t window_id = (pe->pe_number << 1 ) + 1; |
| 1123 | int64_t rc; |
| 1124 | |
| 1125 | pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis"); |
| 1126 | if (enable) { |
| 1127 | phys_addr_t top = memblock_end_of_DRAM(); |
| 1128 | |
| 1129 | top = roundup_pow_of_two(top); |
| 1130 | rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, |
| 1131 | pe->pe_number, |
| 1132 | window_id, |
| 1133 | pe->tce_bypass_base, |
| 1134 | top); |
| 1135 | } else { |
| 1136 | rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, |
| 1137 | pe->pe_number, |
| 1138 | window_id, |
| 1139 | pe->tce_bypass_base, |
| 1140 | 0); |
| 1141 | |
| 1142 | /* |
Gavin Shan | dff4a39 | 2014-07-15 17:00:55 +1000 | [diff] [blame] | 1143 | * EEH needs the mapping between IOMMU table and group |
| 1144 | * of those VFIO/KVM pass-through devices. We can postpone |
| 1145 | * resetting DMA ops until the DMA mask is configured in |
| 1146 | * host side. |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1147 | */ |
Gavin Shan | dff4a39 | 2014-07-15 17:00:55 +1000 | [diff] [blame] | 1148 | if (pe->pdev) |
| 1149 | set_iommu_table_base(&pe->pdev->dev, tbl); |
| 1150 | else |
| 1151 | pnv_ioda_setup_bus_dma(pe, pe->pbus, false); |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1152 | } |
| 1153 | if (rc) |
| 1154 | pe_err(pe, "OPAL error %lld configuring bypass window\n", rc); |
| 1155 | else |
| 1156 | pe->tce_bypass_enabled = enable; |
| 1157 | } |
| 1158 | |
| 1159 | static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb, |
| 1160 | struct pnv_ioda_pe *pe) |
| 1161 | { |
| 1162 | /* TVE #1 is selected by PCI address bit 59 */ |
| 1163 | pe->tce_bypass_base = 1ull << 59; |
| 1164 | |
| 1165 | /* Install set_bypass callback for VFIO */ |
| 1166 | pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass; |
| 1167 | |
| 1168 | /* Enable bypass by default */ |
| 1169 | pnv_pci_ioda2_set_bypass(&pe->tce32_table, true); |
| 1170 | } |
| 1171 | |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 1172 | static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb, |
| 1173 | struct pnv_ioda_pe *pe) |
| 1174 | { |
| 1175 | struct page *tce_mem = NULL; |
| 1176 | void *addr; |
| 1177 | const __be64 *swinvp; |
| 1178 | struct iommu_table *tbl; |
| 1179 | unsigned int tce_table_size, end; |
| 1180 | int64_t rc; |
| 1181 | |
| 1182 | /* We shouldn't already have a 32-bit DMA associated */ |
| 1183 | if (WARN_ON(pe->tce32_seg >= 0)) |
| 1184 | return; |
| 1185 | |
| 1186 | /* The PE will reserve all possible 32-bits space */ |
| 1187 | pe->tce32_seg = 0; |
| 1188 | end = (1 << ilog2(phb->ioda.m32_pci_base)); |
| 1189 | tce_table_size = (end / 0x1000) * 8; |
| 1190 | pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n", |
| 1191 | end); |
| 1192 | |
| 1193 | /* Allocate TCE table */ |
| 1194 | tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL, |
| 1195 | get_order(tce_table_size)); |
| 1196 | if (!tce_mem) { |
| 1197 | pe_err(pe, "Failed to allocate a 32-bit TCE memory\n"); |
| 1198 | goto fail; |
| 1199 | } |
| 1200 | addr = page_address(tce_mem); |
| 1201 | memset(addr, 0, tce_table_size); |
| 1202 | |
| 1203 | /* |
| 1204 | * Map TCE table through TVT. The TVE index is the PE number |
| 1205 | * shifted by 1 bit for 32-bits DMA space. |
| 1206 | */ |
| 1207 | rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number, |
| 1208 | pe->pe_number << 1, 1, __pa(addr), |
| 1209 | tce_table_size, 0x1000); |
| 1210 | if (rc) { |
| 1211 | pe_err(pe, "Failed to configure 32-bit TCE table," |
| 1212 | " err %ld\n", rc); |
| 1213 | goto fail; |
| 1214 | } |
| 1215 | |
| 1216 | /* Setup linux iommu table */ |
| 1217 | tbl = &pe->tce32_table; |
Alexey Kardashevskiy | 8fa5d45 | 2014-06-06 18:44:03 +1000 | [diff] [blame] | 1218 | pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0, |
| 1219 | IOMMU_PAGE_SHIFT_4K); |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 1220 | |
| 1221 | /* OPAL variant of PHB3 invalidated TCEs */ |
| 1222 | swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL); |
| 1223 | if (swinvp) { |
| 1224 | /* We need a couple more fields -- an address and a data |
| 1225 | * to or. Since the bus is only printed out on table free |
| 1226 | * errors, and on the first pass the data will be a relative |
| 1227 | * bus number, print that out instead. |
| 1228 | */ |
Alexey Kardashevskiy | 8e0a161 | 2013-08-28 18:37:43 +1000 | [diff] [blame] | 1229 | pe->tce_inval_reg_phys = be64_to_cpup(swinvp); |
| 1230 | tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys, |
| 1231 | 8); |
Gavin Shan | 65fd766 | 2014-04-24 18:00:28 +1000 | [diff] [blame] | 1232 | tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE); |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 1233 | } |
| 1234 | iommu_init_table(tbl, phb->hose->node); |
Gavin Shan | e9bc03f | 2014-04-24 18:00:29 +1000 | [diff] [blame] | 1235 | iommu_register_group(tbl, phb->hose->global_number, pe->pe_number); |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 1236 | |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 1237 | if (pe->pdev) |
Alexey Kardashevskiy | d905c5d | 2013-11-21 17:43:14 +1100 | [diff] [blame] | 1238 | set_iommu_table_base_and_group(&pe->pdev->dev, tbl); |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 1239 | else |
Gavin Shan | dff4a39 | 2014-07-15 17:00:55 +1000 | [diff] [blame] | 1240 | pnv_ioda_setup_bus_dma(pe, pe->pbus, true); |
Benjamin Herrenschmidt | 74251fe | 2013-07-01 17:54:09 +1000 | [diff] [blame] | 1241 | |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1242 | /* Also create a bypass window */ |
| 1243 | pnv_pci_ioda2_setup_bypass_pe(phb, pe); |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 1244 | return; |
| 1245 | fail: |
| 1246 | if (pe->tce32_seg >= 0) |
| 1247 | pe->tce32_seg = -1; |
| 1248 | if (tce_mem) |
| 1249 | __free_pages(tce_mem, get_order(tce_table_size)); |
| 1250 | } |
| 1251 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 1252 | static void pnv_ioda_setup_dma(struct pnv_phb *phb) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1253 | { |
| 1254 | struct pci_controller *hose = phb->hose; |
| 1255 | unsigned int residual, remaining, segs, tw, base; |
| 1256 | struct pnv_ioda_pe *pe; |
| 1257 | |
| 1258 | /* If we have more PE# than segments available, hand out one |
| 1259 | * per PE until we run out and let the rest fail. If not, |
| 1260 | * then we assign at least one segment per PE, plus more based |
| 1261 | * on the amount of devices under that PE |
| 1262 | */ |
| 1263 | if (phb->ioda.dma_pe_count > phb->ioda.tce32_count) |
| 1264 | residual = 0; |
| 1265 | else |
| 1266 | residual = phb->ioda.tce32_count - |
| 1267 | phb->ioda.dma_pe_count; |
| 1268 | |
| 1269 | pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n", |
| 1270 | hose->global_number, phb->ioda.tce32_count); |
| 1271 | pr_info("PCI: %d PE# for a total weight of %d\n", |
| 1272 | phb->ioda.dma_pe_count, phb->ioda.dma_weight); |
| 1273 | |
| 1274 | /* Walk our PE list and configure their DMA segments, hand them |
| 1275 | * out one base segment plus any residual segments based on |
| 1276 | * weight |
| 1277 | */ |
| 1278 | remaining = phb->ioda.tce32_count; |
| 1279 | tw = phb->ioda.dma_weight; |
| 1280 | base = 0; |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 1281 | list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) { |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1282 | if (!pe->dma_weight) |
| 1283 | continue; |
| 1284 | if (!remaining) { |
| 1285 | pe_warn(pe, "No DMA32 resources available\n"); |
| 1286 | continue; |
| 1287 | } |
| 1288 | segs = 1; |
| 1289 | if (residual) { |
| 1290 | segs += ((pe->dma_weight * residual) + (tw / 2)) / tw; |
| 1291 | if (segs > remaining) |
| 1292 | segs = remaining; |
| 1293 | } |
Gavin Shan | 373f565 | 2013-04-25 19:21:01 +0000 | [diff] [blame] | 1294 | |
| 1295 | /* |
| 1296 | * For IODA2 compliant PHB3, we needn't care about the weight. |
| 1297 | * The all available 32-bits DMA space will be assigned to |
| 1298 | * the specific PE. |
| 1299 | */ |
| 1300 | if (phb->type == PNV_PHB_IODA1) { |
| 1301 | pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n", |
| 1302 | pe->dma_weight, segs); |
| 1303 | pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs); |
| 1304 | } else { |
| 1305 | pe_info(pe, "Assign DMA32 space\n"); |
| 1306 | segs = 0; |
| 1307 | pnv_pci_ioda2_setup_dma_pe(phb, pe); |
| 1308 | } |
| 1309 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1310 | remaining -= segs; |
| 1311 | base += segs; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | #ifdef CONFIG_PCI_MSI |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame] | 1316 | static void pnv_ioda2_msi_eoi(struct irq_data *d) |
| 1317 | { |
| 1318 | unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d); |
| 1319 | struct irq_chip *chip = irq_data_get_irq_chip(d); |
| 1320 | struct pnv_phb *phb = container_of(chip, struct pnv_phb, |
| 1321 | ioda.irq_chip); |
| 1322 | int64_t rc; |
| 1323 | |
| 1324 | rc = opal_pci_msi_eoi(phb->opal_id, hw_irq); |
| 1325 | WARN_ON_ONCE(rc); |
| 1326 | |
| 1327 | icp_native_eoi(d); |
| 1328 | } |
| 1329 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1330 | 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] | 1331 | unsigned int hwirq, unsigned int virq, |
| 1332 | unsigned int is_64, struct msi_msg *msg) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1333 | { |
| 1334 | struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev); |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 1335 | struct pci_dn *pdn = pci_get_pdn(dev); |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame] | 1336 | struct irq_data *idata; |
| 1337 | struct irq_chip *ichip; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1338 | unsigned int xive_num = hwirq - phb->msi_base; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 1339 | __be32 data; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1340 | int rc; |
| 1341 | |
| 1342 | /* No PE assigned ? bail out ... no MSI for you ! */ |
| 1343 | if (pe == NULL) |
| 1344 | return -ENXIO; |
| 1345 | |
| 1346 | /* Check if we have an MVE */ |
| 1347 | if (pe->mve_number < 0) |
| 1348 | return -ENXIO; |
| 1349 | |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 1350 | /* Force 32-bit MSI on some broken devices */ |
| 1351 | if (pdn && pdn->force_32bit_msi) |
| 1352 | is_64 = 0; |
| 1353 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1354 | /* Assign XIVE to PE */ |
| 1355 | rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num); |
| 1356 | if (rc) { |
| 1357 | pr_warn("%s: OPAL error %d setting XIVE %d PE\n", |
| 1358 | pci_name(dev), rc, xive_num); |
| 1359 | return -EIO; |
| 1360 | } |
| 1361 | |
| 1362 | if (is_64) { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 1363 | __be64 addr64; |
| 1364 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1365 | rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1, |
| 1366 | &addr64, &data); |
| 1367 | if (rc) { |
| 1368 | pr_warn("%s: OPAL error %d getting 64-bit MSI data\n", |
| 1369 | pci_name(dev), rc); |
| 1370 | return -EIO; |
| 1371 | } |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 1372 | msg->address_hi = be64_to_cpu(addr64) >> 32; |
| 1373 | msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1374 | } else { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 1375 | __be32 addr32; |
| 1376 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1377 | rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1, |
| 1378 | &addr32, &data); |
| 1379 | if (rc) { |
| 1380 | pr_warn("%s: OPAL error %d getting 32-bit MSI data\n", |
| 1381 | pci_name(dev), rc); |
| 1382 | return -EIO; |
| 1383 | } |
| 1384 | msg->address_hi = 0; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 1385 | msg->address_lo = be32_to_cpu(addr32); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1386 | } |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 1387 | msg->data = be32_to_cpu(data); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1388 | |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * Change the IRQ chip for the MSI interrupts on PHB3. |
| 1391 | * The corresponding IRQ chip should be populated for |
| 1392 | * the first time. |
| 1393 | */ |
| 1394 | if (phb->type == PNV_PHB_IODA2) { |
| 1395 | if (!phb->ioda.irq_chip_init) { |
| 1396 | idata = irq_get_irq_data(virq); |
| 1397 | ichip = irq_data_get_irq_chip(idata); |
| 1398 | phb->ioda.irq_chip_init = 1; |
| 1399 | phb->ioda.irq_chip = *ichip; |
| 1400 | phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi; |
| 1401 | } |
| 1402 | |
| 1403 | irq_set_chip(virq, &phb->ioda.irq_chip); |
| 1404 | } |
| 1405 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1406 | pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d)," |
| 1407 | " address=%x_%08x data=%x PE# %d\n", |
| 1408 | pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num, |
| 1409 | msg->address_hi, msg->address_lo, data, pe->pe_number); |
| 1410 | |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
| 1414 | static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) |
| 1415 | { |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 1416 | unsigned int count; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1417 | const __be32 *prop = of_get_property(phb->hose->dn, |
| 1418 | "ibm,opal-msi-ranges", NULL); |
| 1419 | if (!prop) { |
| 1420 | /* BML Fallback */ |
| 1421 | prop = of_get_property(phb->hose->dn, "msi-ranges", NULL); |
| 1422 | } |
| 1423 | if (!prop) |
| 1424 | return; |
| 1425 | |
| 1426 | phb->msi_base = be32_to_cpup(prop); |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 1427 | count = be32_to_cpup(prop + 1); |
| 1428 | if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) { |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1429 | pr_err("PCI %d: Failed to allocate MSI bitmap !\n", |
| 1430 | phb->hose->global_number); |
| 1431 | return; |
| 1432 | } |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 1433 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1434 | phb->msi_setup = pnv_pci_ioda_msi_setup; |
| 1435 | phb->msi32_support = 1; |
| 1436 | pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n", |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 1437 | count, phb->msi_base); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1438 | } |
| 1439 | #else |
| 1440 | static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { } |
| 1441 | #endif /* CONFIG_PCI_MSI */ |
| 1442 | |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 1443 | /* |
| 1444 | * This function is supposed to be called on basis of PE from top |
| 1445 | * to bottom style. So the the I/O or MMIO segment assigned to |
| 1446 | * parent PE could be overrided by its child PEs if necessary. |
| 1447 | */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 1448 | static void pnv_ioda_setup_pe_seg(struct pci_controller *hose, |
| 1449 | struct pnv_ioda_pe *pe) |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 1450 | { |
| 1451 | struct pnv_phb *phb = hose->private_data; |
| 1452 | struct pci_bus_region region; |
| 1453 | struct resource *res; |
| 1454 | int i, index; |
| 1455 | int rc; |
| 1456 | |
| 1457 | /* |
| 1458 | * NOTE: We only care PCI bus based PE for now. For PCI |
| 1459 | * device based PE, for example SRIOV sensitive VF should |
| 1460 | * be figured out later. |
| 1461 | */ |
| 1462 | BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))); |
| 1463 | |
| 1464 | pci_bus_for_each_resource(pe->pbus, res, i) { |
| 1465 | if (!res || !res->flags || |
| 1466 | res->start > res->end) |
| 1467 | continue; |
| 1468 | |
| 1469 | if (res->flags & IORESOURCE_IO) { |
| 1470 | region.start = res->start - phb->ioda.io_pci_base; |
| 1471 | region.end = res->end - phb->ioda.io_pci_base; |
| 1472 | index = region.start / phb->ioda.io_segsize; |
| 1473 | |
| 1474 | while (index < phb->ioda.total_pe && |
| 1475 | region.start <= region.end) { |
| 1476 | phb->ioda.io_segmap[index] = pe->pe_number; |
| 1477 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 1478 | pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index); |
| 1479 | if (rc != OPAL_SUCCESS) { |
| 1480 | pr_err("%s: OPAL error %d when mapping IO " |
| 1481 | "segment #%d to PE#%d\n", |
| 1482 | __func__, rc, index, pe->pe_number); |
| 1483 | break; |
| 1484 | } |
| 1485 | |
| 1486 | region.start += phb->ioda.io_segsize; |
| 1487 | index++; |
| 1488 | } |
| 1489 | } else if (res->flags & IORESOURCE_MEM) { |
| 1490 | region.start = res->start - |
Benjamin Herrenschmidt | 3fd47f0 | 2013-05-06 13:40:40 +1000 | [diff] [blame] | 1491 | hose->mem_offset[0] - |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 1492 | phb->ioda.m32_pci_base; |
| 1493 | region.end = res->end - |
Benjamin Herrenschmidt | 3fd47f0 | 2013-05-06 13:40:40 +1000 | [diff] [blame] | 1494 | hose->mem_offset[0] - |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 1495 | phb->ioda.m32_pci_base; |
| 1496 | index = region.start / phb->ioda.m32_segsize; |
| 1497 | |
| 1498 | while (index < phb->ioda.total_pe && |
| 1499 | region.start <= region.end) { |
| 1500 | phb->ioda.m32_segmap[index] = pe->pe_number; |
| 1501 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 1502 | pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index); |
| 1503 | if (rc != OPAL_SUCCESS) { |
| 1504 | pr_err("%s: OPAL error %d when mapping M32 " |
| 1505 | "segment#%d to PE#%d", |
| 1506 | __func__, rc, index, pe->pe_number); |
| 1507 | break; |
| 1508 | } |
| 1509 | |
| 1510 | region.start += phb->ioda.m32_segsize; |
| 1511 | index++; |
| 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 1517 | static void pnv_pci_ioda_setup_seg(void) |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 1518 | { |
| 1519 | struct pci_controller *tmp, *hose; |
| 1520 | struct pnv_phb *phb; |
| 1521 | struct pnv_ioda_pe *pe; |
| 1522 | |
| 1523 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
| 1524 | phb = hose->private_data; |
| 1525 | list_for_each_entry(pe, &phb->ioda.pe_list, list) { |
| 1526 | pnv_ioda_setup_pe_seg(hose, pe); |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 1531 | static void pnv_pci_ioda_setup_DMA(void) |
Gavin Shan | 13395c4 | 2012-08-20 03:49:17 +0000 | [diff] [blame] | 1532 | { |
| 1533 | struct pci_controller *hose, *tmp; |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 1534 | struct pnv_phb *phb; |
Gavin Shan | 13395c4 | 2012-08-20 03:49:17 +0000 | [diff] [blame] | 1535 | |
| 1536 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
| 1537 | pnv_ioda_setup_dma(hose->private_data); |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 1538 | |
| 1539 | /* Mark the PHB initialization done */ |
| 1540 | phb = hose->private_data; |
| 1541 | phb->initialized = 1; |
Gavin Shan | 13395c4 | 2012-08-20 03:49:17 +0000 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 1545 | static void pnv_pci_ioda_create_dbgfs(void) |
| 1546 | { |
| 1547 | #ifdef CONFIG_DEBUG_FS |
| 1548 | struct pci_controller *hose, *tmp; |
| 1549 | struct pnv_phb *phb; |
| 1550 | char name[16]; |
| 1551 | |
| 1552 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
| 1553 | phb = hose->private_data; |
| 1554 | |
| 1555 | sprintf(name, "PCI%04x", hose->global_number); |
| 1556 | phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root); |
| 1557 | if (!phb->dbgfs) |
| 1558 | pr_warning("%s: Error on creating debugfs on PHB#%x\n", |
| 1559 | __func__, hose->global_number); |
| 1560 | } |
| 1561 | #endif /* CONFIG_DEBUG_FS */ |
| 1562 | } |
| 1563 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 1564 | static void pnv_pci_ioda_fixup(void) |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1565 | { |
| 1566 | pnv_pci_ioda_setup_PEs(); |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 1567 | pnv_pci_ioda_setup_seg(); |
Gavin Shan | 13395c4 | 2012-08-20 03:49:17 +0000 | [diff] [blame] | 1568 | pnv_pci_ioda_setup_DMA(); |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 1569 | |
Gavin Shan | 37c367f | 2013-06-20 18:13:25 +0800 | [diff] [blame] | 1570 | pnv_pci_ioda_create_dbgfs(); |
| 1571 | |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 1572 | #ifdef CONFIG_EEH |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 1573 | eeh_init(); |
Mike Qiu | dadcd6d | 2014-06-26 02:58:47 -0400 | [diff] [blame] | 1574 | eeh_addr_cache_build(); |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 1575 | #endif |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Gavin Shan | 271fd03 | 2012-09-11 16:59:47 -0600 | [diff] [blame] | 1578 | /* |
| 1579 | * Returns the alignment for I/O or memory windows for P2P |
| 1580 | * bridges. That actually depends on how PEs are segmented. |
| 1581 | * For now, we return I/O or M32 segment size for PE sensitive |
| 1582 | * P2P bridges. Otherwise, the default values (4KiB for I/O, |
| 1583 | * 1MiB for memory) will be returned. |
| 1584 | * |
| 1585 | * The current PCI bus might be put into one PE, which was |
| 1586 | * create against the parent PCI bridge. For that case, we |
| 1587 | * needn't enlarge the alignment so that we can save some |
| 1588 | * resources. |
| 1589 | */ |
| 1590 | static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus, |
| 1591 | unsigned long type) |
| 1592 | { |
| 1593 | struct pci_dev *bridge; |
| 1594 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 1595 | struct pnv_phb *phb = hose->private_data; |
| 1596 | int num_pci_bridges = 0; |
| 1597 | |
| 1598 | bridge = bus->self; |
| 1599 | while (bridge) { |
| 1600 | if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) { |
| 1601 | num_pci_bridges++; |
| 1602 | if (num_pci_bridges >= 2) |
| 1603 | return 1; |
| 1604 | } |
| 1605 | |
| 1606 | bridge = bridge->bus->self; |
| 1607 | } |
| 1608 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1609 | /* We fail back to M32 if M64 isn't supported */ |
| 1610 | if (phb->ioda.m64_segsize && |
| 1611 | pnv_pci_is_mem_pref_64(type)) |
| 1612 | return phb->ioda.m64_segsize; |
Gavin Shan | 271fd03 | 2012-09-11 16:59:47 -0600 | [diff] [blame] | 1613 | if (type & IORESOURCE_MEM) |
| 1614 | return phb->ioda.m32_segsize; |
| 1615 | |
| 1616 | return phb->ioda.io_segsize; |
| 1617 | } |
| 1618 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1619 | /* Prevent enabling devices for which we couldn't properly |
| 1620 | * assign a PE |
| 1621 | */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 1622 | static int pnv_pci_enable_device_hook(struct pci_dev *dev) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1623 | { |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 1624 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 1625 | struct pnv_phb *phb = hose->private_data; |
| 1626 | struct pci_dn *pdn; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1627 | |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 1628 | /* The function is probably called while the PEs have |
| 1629 | * not be created yet. For example, resource reassignment |
| 1630 | * during PCI probe period. We just skip the check if |
| 1631 | * PEs isn't ready. |
| 1632 | */ |
| 1633 | if (!phb->initialized) |
| 1634 | return 0; |
| 1635 | |
Benjamin Herrenschmidt | b72c1f6 | 2013-05-21 22:58:21 +0000 | [diff] [blame] | 1636 | pdn = pci_get_pdn(dev); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1637 | if (!pdn || pdn->pe_number == IODA_INVALID_PE) |
| 1638 | return -EINVAL; |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 1639 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1640 | return 0; |
| 1641 | } |
| 1642 | |
| 1643 | static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus, |
| 1644 | u32 devfn) |
| 1645 | { |
| 1646 | return phb->ioda.pe_rmap[(bus->number << 8) | devfn]; |
| 1647 | } |
| 1648 | |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 1649 | static void pnv_pci_ioda_shutdown(struct pnv_phb *phb) |
| 1650 | { |
Gavin Shan | d1a85ee | 2014-09-30 12:39:05 +1000 | [diff] [blame] | 1651 | opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE, |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 1652 | OPAL_ASSERT_RESET); |
| 1653 | } |
| 1654 | |
Anton Blanchard | e51df2c | 2014-08-20 08:55:18 +1000 | [diff] [blame] | 1655 | static void __init pnv_pci_init_ioda_phb(struct device_node *np, |
| 1656 | u64 hub_id, int ioda_type) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1657 | { |
| 1658 | struct pci_controller *hose; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1659 | struct pnv_phb *phb; |
Gavin Shan | 8184616 | 2013-12-26 09:29:40 +0800 | [diff] [blame] | 1660 | unsigned long size, m32map_off, pemap_off, iomap_off = 0; |
Alistair Popple | c681b93 | 2013-09-23 12:04:57 +1000 | [diff] [blame] | 1661 | const __be64 *prop64; |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 1662 | const __be32 *prop32; |
Gavin Shan | f1b7cc3 | 2013-07-31 16:47:01 +0800 | [diff] [blame] | 1663 | int len; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1664 | u64 phb_id; |
| 1665 | void *aux; |
| 1666 | long rc; |
| 1667 | |
Gavin Shan | 58d714e | 2013-07-31 16:47:00 +0800 | [diff] [blame] | 1668 | 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] | 1669 | |
| 1670 | prop64 = of_get_property(np, "ibm,opal-phbid", NULL); |
| 1671 | if (!prop64) { |
| 1672 | pr_err(" Missing \"ibm,opal-phbid\" property !\n"); |
| 1673 | return; |
| 1674 | } |
| 1675 | phb_id = be64_to_cpup(prop64); |
| 1676 | pr_debug(" PHB-ID : 0x%016llx\n", phb_id); |
| 1677 | |
| 1678 | phb = alloc_bootmem(sizeof(struct pnv_phb)); |
Gavin Shan | 58d714e | 2013-07-31 16:47:00 +0800 | [diff] [blame] | 1679 | if (!phb) { |
| 1680 | pr_err(" Out of memory !\n"); |
| 1681 | return; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1682 | } |
Gavin Shan | 58d714e | 2013-07-31 16:47:00 +0800 | [diff] [blame] | 1683 | |
| 1684 | /* Allocate PCI controller */ |
| 1685 | memset(phb, 0, sizeof(struct pnv_phb)); |
| 1686 | phb->hose = hose = pcibios_alloc_controller(np); |
| 1687 | if (!phb->hose) { |
| 1688 | pr_err(" Can't allocate PCI controller for %s\n", |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1689 | np->full_name); |
Gavin Shan | 58d714e | 2013-07-31 16:47:00 +0800 | [diff] [blame] | 1690 | free_bootmem((unsigned long)phb, sizeof(struct pnv_phb)); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1691 | return; |
| 1692 | } |
| 1693 | |
| 1694 | spin_lock_init(&phb->lock); |
Gavin Shan | f1b7cc3 | 2013-07-31 16:47:01 +0800 | [diff] [blame] | 1695 | prop32 = of_get_property(np, "bus-range", &len); |
| 1696 | if (prop32 && len == 8) { |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 1697 | hose->first_busno = be32_to_cpu(prop32[0]); |
| 1698 | hose->last_busno = be32_to_cpu(prop32[1]); |
Gavin Shan | f1b7cc3 | 2013-07-31 16:47:01 +0800 | [diff] [blame] | 1699 | } else { |
| 1700 | pr_warn(" Broken <bus-range> on %s\n", np->full_name); |
| 1701 | hose->first_busno = 0; |
| 1702 | hose->last_busno = 0xff; |
| 1703 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1704 | hose->private_data = phb; |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 1705 | phb->hub_id = hub_id; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1706 | phb->opal_id = phb_id; |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1707 | phb->type = ioda_type; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1708 | |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 1709 | /* Detect specific models for error handling */ |
| 1710 | if (of_device_is_compatible(np, "ibm,p7ioc-pciex")) |
| 1711 | phb->model = PNV_PHB_MODEL_P7IOC; |
Benjamin Herrenschmidt | f3d40c2 | 2013-05-04 14:24:32 +0000 | [diff] [blame] | 1712 | else if (of_device_is_compatible(np, "ibm,power8-pciex")) |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1713 | phb->model = PNV_PHB_MODEL_PHB3; |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 1714 | else |
| 1715 | phb->model = PNV_PHB_MODEL_UNKNOWN; |
| 1716 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1717 | /* Parse 32-bit and IO ranges (if any) */ |
Gavin Shan | 2f1ec02 | 2013-07-31 16:47:02 +0800 | [diff] [blame] | 1718 | pci_process_bridge_OF_ranges(hose, np, !hose->global_number); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1719 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1720 | /* Get registers */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1721 | phb->regs = of_iomap(np, 0); |
| 1722 | if (phb->regs == NULL) |
| 1723 | pr_err(" Failed to map registers !\n"); |
| 1724 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1725 | /* Initialize more IODA stuff */ |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 1726 | phb->ioda.total_pe = 1; |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1727 | prop32 = of_get_property(np, "ibm,opal-num-pes", NULL); |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 1728 | if (prop32) |
Benjamin Herrenschmidt | 3a1a466 | 2013-09-23 12:05:01 +1000 | [diff] [blame] | 1729 | phb->ioda.total_pe = be32_to_cpup(prop32); |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 1730 | prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL); |
| 1731 | if (prop32) |
| 1732 | phb->ioda.reserved_pe = be32_to_cpup(prop32); |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1733 | |
| 1734 | /* Parse 64-bit MMIO range */ |
| 1735 | pnv_ioda_parse_m64_window(phb); |
| 1736 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1737 | phb->ioda.m32_size = resource_size(&hose->mem_resources[0]); |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1738 | /* FW Has already off top 64k of M32 space (MSI space) */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1739 | phb->ioda.m32_size += 0x10000; |
| 1740 | |
| 1741 | phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe; |
Benjamin Herrenschmidt | 3fd47f0 | 2013-05-06 13:40:40 +1000 | [diff] [blame] | 1742 | 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] | 1743 | phb->ioda.io_size = hose->pci_io_size; |
| 1744 | phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe; |
| 1745 | phb->ioda.io_pci_base = 0; /* XXX calculate this ? */ |
| 1746 | |
Gavin Shan | c35d2a8 | 2013-07-31 16:47:04 +0800 | [diff] [blame] | 1747 | /* Allocate aux data & arrays. We don't have IO ports on PHB3 */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1748 | size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long)); |
| 1749 | m32map_off = size; |
Gavin Shan | e47747f | 2012-08-20 03:49:19 +0000 | [diff] [blame] | 1750 | size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]); |
Gavin Shan | c35d2a8 | 2013-07-31 16:47:04 +0800 | [diff] [blame] | 1751 | if (phb->type == PNV_PHB_IODA1) { |
| 1752 | iomap_off = size; |
| 1753 | size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]); |
| 1754 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1755 | pemap_off = size; |
| 1756 | size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe); |
| 1757 | aux = alloc_bootmem(size); |
| 1758 | memset(aux, 0, size); |
| 1759 | phb->ioda.pe_alloc = aux; |
| 1760 | phb->ioda.m32_segmap = aux + m32map_off; |
Gavin Shan | c35d2a8 | 2013-07-31 16:47:04 +0800 | [diff] [blame] | 1761 | if (phb->type == PNV_PHB_IODA1) |
| 1762 | phb->ioda.io_segmap = aux + iomap_off; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1763 | phb->ioda.pe_array = aux + pemap_off; |
Gavin Shan | 36954dc | 2013-11-04 16:32:47 +0800 | [diff] [blame] | 1764 | set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1765 | |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 1766 | INIT_LIST_HEAD(&phb->ioda.pe_dma_list); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1767 | INIT_LIST_HEAD(&phb->ioda.pe_list); |
| 1768 | |
| 1769 | /* Calculate how many 32-bit TCE segments we have */ |
| 1770 | phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28; |
| 1771 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1772 | #if 0 /* We should really do that ... */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1773 | rc = opal_pci_set_phb_mem_window(opal->phb_id, |
| 1774 | window_type, |
| 1775 | window_num, |
| 1776 | starting_real_address, |
| 1777 | starting_pci_address, |
| 1778 | segment_size); |
| 1779 | #endif |
| 1780 | |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1781 | pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n", |
| 1782 | phb->ioda.total_pe, phb->ioda.reserved_pe, |
| 1783 | phb->ioda.m32_size, phb->ioda.m32_segsize); |
| 1784 | if (phb->ioda.m64_size) |
| 1785 | pr_info(" M64: 0x%lx [segment=0x%lx]\n", |
| 1786 | phb->ioda.m64_size, phb->ioda.m64_segsize); |
| 1787 | if (phb->ioda.io_size) |
| 1788 | pr_info(" IO: 0x%x [segment=0x%x]\n", |
| 1789 | phb->ioda.io_size, phb->ioda.io_segsize); |
| 1790 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1791 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1792 | phb->hose->ops = &pnv_pci_ops; |
Gavin Shan | 49dec92 | 2014-07-21 14:42:33 +1000 | [diff] [blame] | 1793 | phb->get_pe_state = pnv_ioda_get_pe_state; |
| 1794 | phb->freeze_pe = pnv_ioda_freeze_pe; |
| 1795 | phb->unfreeze_pe = pnv_ioda_unfreeze_pe; |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 1796 | #ifdef CONFIG_EEH |
| 1797 | phb->eeh_ops = &ioda_eeh_ops; |
| 1798 | #endif |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1799 | |
| 1800 | /* Setup RID -> PE mapping function */ |
| 1801 | phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe; |
| 1802 | |
| 1803 | /* Setup TCEs */ |
| 1804 | phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup; |
Benjamin Herrenschmidt | cd15b04 | 2014-02-11 11:32:38 +1100 | [diff] [blame] | 1805 | phb->dma_set_mask = pnv_pci_ioda_dma_set_mask; |
Gavin Shan | fe7e85c | 2014-09-30 12:39:10 +1000 | [diff] [blame^] | 1806 | phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1807 | |
Benjamin Herrenschmidt | 73ed148 | 2013-05-10 16:59:18 +1000 | [diff] [blame] | 1808 | /* Setup shutdown function for kexec */ |
| 1809 | phb->shutdown = pnv_pci_ioda_shutdown; |
| 1810 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1811 | /* Setup MSI support */ |
| 1812 | pnv_pci_init_ioda_msis(phb); |
| 1813 | |
Gavin Shan | c40a421 | 2012-08-20 03:49:20 +0000 | [diff] [blame] | 1814 | /* |
| 1815 | * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here |
| 1816 | * to let the PCI core do resource assignment. It's supposed |
| 1817 | * that the PCI core will do correct I/O and MMIO alignment |
| 1818 | * for the P2P bridge bars so that each PCI bus (excluding |
| 1819 | * the child P2P bridges) can form individual PE. |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1820 | */ |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1821 | ppc_md.pcibios_fixup = pnv_pci_ioda_fixup; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1822 | ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook; |
Gavin Shan | 271fd03 | 2012-09-11 16:59:47 -0600 | [diff] [blame] | 1823 | ppc_md.pcibios_window_alignment = pnv_pci_window_alignment; |
Gavin Shan | d92a208 | 2014-04-24 18:00:24 +1000 | [diff] [blame] | 1824 | ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus; |
Gavin Shan | c40a421 | 2012-08-20 03:49:20 +0000 | [diff] [blame] | 1825 | pci_add_flags(PCI_REASSIGN_ALL_RSRC); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1826 | |
| 1827 | /* Reset IODA tables to a clean state */ |
Gavin Shan | d1a85ee | 2014-09-30 12:39:05 +1000 | [diff] [blame] | 1828 | 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] | 1829 | if (rc) |
Benjamin Herrenschmidt | f11fe55 | 2011-11-29 18:22:50 +0000 | [diff] [blame] | 1830 | pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc); |
Gavin Shan | 361f2a2 | 2014-04-24 18:00:25 +1000 | [diff] [blame] | 1831 | |
| 1832 | /* If we're running in kdump kerenl, the previous kerenl never |
| 1833 | * shutdown PCI devices correctly. We already got IODA table |
| 1834 | * cleaned out. So we have to issue PHB reset to stop all PCI |
| 1835 | * transactions from previous kerenl. |
| 1836 | */ |
| 1837 | if (is_kdump_kernel()) { |
| 1838 | pr_info(" Issue PHB reset ...\n"); |
| 1839 | ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL); |
| 1840 | ioda_eeh_phb_reset(hose, OPAL_DEASSERT_RESET); |
| 1841 | } |
Guo Chao | 262af55 | 2014-07-21 14:42:30 +1000 | [diff] [blame] | 1842 | |
| 1843 | /* Configure M64 window */ |
| 1844 | if (phb->init_m64 && phb->init_m64(phb)) |
| 1845 | hose->mem_resources[1].flags = 0; |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Bjorn Helgaas | 6797500 | 2013-07-02 12:20:03 -0600 | [diff] [blame] | 1848 | void __init pnv_pci_init_ioda2_phb(struct device_node *np) |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1849 | { |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 1850 | pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | void __init pnv_pci_init_ioda_hub(struct device_node *np) |
| 1854 | { |
| 1855 | struct device_node *phbn; |
Alistair Popple | c681b93 | 2013-09-23 12:04:57 +1000 | [diff] [blame] | 1856 | const __be64 *prop64; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1857 | u64 hub_id; |
| 1858 | |
| 1859 | pr_info("Probing IODA IO-Hub %s\n", np->full_name); |
| 1860 | |
| 1861 | prop64 = of_get_property(np, "ibm,opal-hubid", NULL); |
| 1862 | if (!prop64) { |
| 1863 | pr_err(" Missing \"ibm,opal-hubid\" property !\n"); |
| 1864 | return; |
| 1865 | } |
| 1866 | hub_id = be64_to_cpup(prop64); |
| 1867 | pr_devel(" HUB-ID : 0x%016llx\n", hub_id); |
| 1868 | |
| 1869 | /* Count child PHBs */ |
| 1870 | for_each_child_of_node(np, phbn) { |
| 1871 | /* Look for IODA1 PHBs */ |
| 1872 | if (of_device_is_compatible(phbn, "ibm,ioda-phb")) |
Gavin Shan | e9cc17d | 2013-06-20 13:21:14 +0800 | [diff] [blame] | 1873 | pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1874 | } |
| 1875 | } |