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> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/bootmem.h> |
| 20 | #include <linux/irq.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/msi.h> |
| 23 | |
| 24 | #include <asm/sections.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/prom.h> |
| 27 | #include <asm/pci-bridge.h> |
| 28 | #include <asm/machdep.h> |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 29 | #include <asm/msi_bitmap.h> |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 30 | #include <asm/ppc-pci.h> |
| 31 | #include <asm/opal.h> |
| 32 | #include <asm/iommu.h> |
| 33 | #include <asm/tce.h> |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame^] | 34 | #include <asm/xics.h> |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 35 | |
| 36 | #include "powernv.h" |
| 37 | #include "pci.h" |
| 38 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 39 | #define define_pe_printk_level(func, kern_level) \ |
| 40 | static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \ |
| 41 | { \ |
| 42 | struct va_format vaf; \ |
| 43 | va_list args; \ |
Gavin Shan | 490e078 | 2012-10-17 19:53:30 +0000 | [diff] [blame] | 44 | char pfix[32]; \ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 45 | int r; \ |
| 46 | \ |
| 47 | va_start(args, fmt); \ |
| 48 | \ |
| 49 | vaf.fmt = fmt; \ |
| 50 | vaf.va = &args; \ |
| 51 | \ |
Gavin Shan | 490e078 | 2012-10-17 19:53:30 +0000 | [diff] [blame] | 52 | if (pe->pdev) \ |
| 53 | strlcpy(pfix, dev_name(&pe->pdev->dev), \ |
| 54 | sizeof(pfix)); \ |
| 55 | else \ |
| 56 | sprintf(pfix, "%04x:%02x ", \ |
| 57 | pci_domain_nr(pe->pbus), \ |
| 58 | pe->pbus->number); \ |
| 59 | r = printk(kern_level "pci %s: [PE# %.3d] %pV", \ |
| 60 | pfix, pe->pe_number, &vaf); \ |
| 61 | \ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 62 | va_end(args); \ |
| 63 | \ |
| 64 | return r; \ |
| 65 | } \ |
| 66 | |
| 67 | define_pe_printk_level(pe_err, KERN_ERR); |
| 68 | define_pe_printk_level(pe_warn, KERN_WARNING); |
| 69 | define_pe_printk_level(pe_info, KERN_INFO); |
| 70 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 71 | static struct pci_dn *pnv_ioda_get_pdn(struct pci_dev *dev) |
| 72 | { |
| 73 | struct device_node *np; |
| 74 | |
| 75 | np = pci_device_to_OF_node(dev); |
| 76 | if (!np) |
| 77 | return NULL; |
| 78 | return PCI_DN(np); |
| 79 | } |
| 80 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 81 | static int pnv_ioda_alloc_pe(struct pnv_phb *phb) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 82 | { |
| 83 | unsigned long pe; |
| 84 | |
| 85 | do { |
| 86 | pe = find_next_zero_bit(phb->ioda.pe_alloc, |
| 87 | phb->ioda.total_pe, 0); |
| 88 | if (pe >= phb->ioda.total_pe) |
| 89 | return IODA_INVALID_PE; |
| 90 | } while(test_and_set_bit(pe, phb->ioda.pe_alloc)); |
| 91 | |
| 92 | phb->ioda.pe_array[pe].pe_number = pe; |
| 93 | return pe; |
| 94 | } |
| 95 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 96 | static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 97 | { |
| 98 | WARN_ON(phb->ioda.pe_array[pe].pdev); |
| 99 | |
| 100 | memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe)); |
| 101 | clear_bit(pe, phb->ioda.pe_alloc); |
| 102 | } |
| 103 | |
| 104 | /* Currently those 2 are only used when MSIs are enabled, this will change |
| 105 | * but in the meantime, we need to protect them to avoid warnings |
| 106 | */ |
| 107 | #ifdef CONFIG_PCI_MSI |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 108 | 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] | 109 | { |
| 110 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 111 | struct pnv_phb *phb = hose->private_data; |
| 112 | struct pci_dn *pdn = pnv_ioda_get_pdn(dev); |
| 113 | |
| 114 | if (!pdn) |
| 115 | return NULL; |
| 116 | if (pdn->pe_number == IODA_INVALID_PE) |
| 117 | return NULL; |
| 118 | return &phb->ioda.pe_array[pdn->pe_number]; |
| 119 | } |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 120 | #endif /* CONFIG_PCI_MSI */ |
| 121 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 122 | 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] | 123 | { |
| 124 | struct pci_dev *parent; |
| 125 | uint8_t bcomp, dcomp, fcomp; |
| 126 | long rc, rid_end, rid; |
| 127 | |
| 128 | /* Bus validation ? */ |
| 129 | if (pe->pbus) { |
| 130 | int count; |
| 131 | |
| 132 | dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER; |
| 133 | fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER; |
| 134 | parent = pe->pbus->self; |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 135 | if (pe->flags & PNV_IODA_PE_BUS_ALL) |
| 136 | count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1; |
| 137 | else |
| 138 | count = 1; |
| 139 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 140 | switch(count) { |
| 141 | case 1: bcomp = OpalPciBusAll; break; |
| 142 | case 2: bcomp = OpalPciBus7Bits; break; |
| 143 | case 4: bcomp = OpalPciBus6Bits; break; |
| 144 | case 8: bcomp = OpalPciBus5Bits; break; |
| 145 | case 16: bcomp = OpalPciBus4Bits; break; |
| 146 | case 32: bcomp = OpalPciBus3Bits; break; |
| 147 | default: |
| 148 | pr_err("%s: Number of subordinate busses %d" |
| 149 | " unsupported\n", |
| 150 | pci_name(pe->pbus->self), count); |
| 151 | /* Do an exact match only */ |
| 152 | bcomp = OpalPciBusAll; |
| 153 | } |
| 154 | rid_end = pe->rid + (count << 8); |
| 155 | } else { |
| 156 | parent = pe->pdev->bus->self; |
| 157 | bcomp = OpalPciBusAll; |
| 158 | dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER; |
| 159 | fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER; |
| 160 | rid_end = pe->rid + 1; |
| 161 | } |
| 162 | |
| 163 | /* Associate PE in PELT */ |
| 164 | rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid, |
| 165 | bcomp, dcomp, fcomp, OPAL_MAP_PE); |
| 166 | if (rc) { |
| 167 | pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc); |
| 168 | return -ENXIO; |
| 169 | } |
| 170 | opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number, |
| 171 | OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); |
| 172 | |
| 173 | /* Add to all parents PELT-V */ |
| 174 | while (parent) { |
| 175 | struct pci_dn *pdn = pnv_ioda_get_pdn(parent); |
| 176 | if (pdn && pdn->pe_number != IODA_INVALID_PE) { |
| 177 | rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number, |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 178 | pe->pe_number, OPAL_ADD_PE_TO_DOMAIN); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 179 | /* XXX What to do in case of error ? */ |
| 180 | } |
| 181 | parent = parent->bus->self; |
| 182 | } |
| 183 | /* Setup reverse map */ |
| 184 | for (rid = pe->rid; rid < rid_end; rid++) |
| 185 | phb->ioda.pe_rmap[rid] = pe->pe_number; |
| 186 | |
| 187 | /* Setup one MVTs on IODA1 */ |
| 188 | if (phb->type == PNV_PHB_IODA1) { |
| 189 | pe->mve_number = pe->pe_number; |
| 190 | rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, |
| 191 | pe->pe_number); |
| 192 | if (rc) { |
| 193 | pe_err(pe, "OPAL error %ld setting up MVE %d\n", |
| 194 | rc, pe->mve_number); |
| 195 | pe->mve_number = -1; |
| 196 | } else { |
| 197 | rc = opal_pci_set_mve_enable(phb->opal_id, |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 198 | pe->mve_number, OPAL_ENABLE_MVE); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 199 | if (rc) { |
| 200 | pe_err(pe, "OPAL error %ld enabling MVE %d\n", |
| 201 | rc, pe->mve_number); |
| 202 | pe->mve_number = -1; |
| 203 | } |
| 204 | } |
| 205 | } else if (phb->type == PNV_PHB_IODA2) |
| 206 | pe->mve_number = 0; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 211 | static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb, |
| 212 | struct pnv_ioda_pe *pe) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 213 | { |
| 214 | struct pnv_ioda_pe *lpe; |
| 215 | |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 216 | list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) { |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 217 | if (lpe->dma_weight < pe->dma_weight) { |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 218 | list_add_tail(&pe->dma_link, &lpe->dma_link); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | } |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 222 | list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev) |
| 226 | { |
| 227 | /* This is quite simplistic. The "base" weight of a device |
| 228 | * is 10. 0 means no DMA is to be accounted for it. |
| 229 | */ |
| 230 | |
| 231 | /* If it's a bridge, no DMA */ |
| 232 | if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) |
| 233 | return 0; |
| 234 | |
| 235 | /* Reduce the weight of slow USB controllers */ |
| 236 | if (dev->class == PCI_CLASS_SERIAL_USB_UHCI || |
| 237 | dev->class == PCI_CLASS_SERIAL_USB_OHCI || |
| 238 | dev->class == PCI_CLASS_SERIAL_USB_EHCI) |
| 239 | return 3; |
| 240 | |
| 241 | /* Increase the weight of RAID (includes Obsidian) */ |
| 242 | if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID) |
| 243 | return 15; |
| 244 | |
| 245 | /* Default */ |
| 246 | return 10; |
| 247 | } |
| 248 | |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 249 | #if 0 |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 250 | 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] | 251 | { |
| 252 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 253 | struct pnv_phb *phb = hose->private_data; |
| 254 | struct pci_dn *pdn = pnv_ioda_get_pdn(dev); |
| 255 | struct pnv_ioda_pe *pe; |
| 256 | int pe_num; |
| 257 | |
| 258 | if (!pdn) { |
| 259 | pr_err("%s: Device tree node not associated properly\n", |
| 260 | pci_name(dev)); |
| 261 | return NULL; |
| 262 | } |
| 263 | if (pdn->pe_number != IODA_INVALID_PE) |
| 264 | return NULL; |
| 265 | |
| 266 | /* PE#0 has been pre-set */ |
| 267 | if (dev->bus->number == 0) |
| 268 | pe_num = 0; |
| 269 | else |
| 270 | pe_num = pnv_ioda_alloc_pe(phb); |
| 271 | if (pe_num == IODA_INVALID_PE) { |
| 272 | pr_warning("%s: Not enough PE# available, disabling device\n", |
| 273 | pci_name(dev)); |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | /* NOTE: We get only one ref to the pci_dev for the pdn, not for the |
| 278 | * pointer in the PE data structure, both should be destroyed at the |
| 279 | * same time. However, this needs to be looked at more closely again |
| 280 | * once we actually start removing things (Hotplug, SR-IOV, ...) |
| 281 | * |
| 282 | * At some point we want to remove the PDN completely anyways |
| 283 | */ |
| 284 | pe = &phb->ioda.pe_array[pe_num]; |
| 285 | pci_dev_get(dev); |
| 286 | pdn->pcidev = dev; |
| 287 | pdn->pe_number = pe_num; |
| 288 | pe->pdev = dev; |
| 289 | pe->pbus = NULL; |
| 290 | pe->tce32_seg = -1; |
| 291 | pe->mve_number = -1; |
| 292 | pe->rid = dev->bus->number << 8 | pdn->devfn; |
| 293 | |
| 294 | pe_info(pe, "Associated device to PE\n"); |
| 295 | |
| 296 | if (pnv_ioda_configure_pe(phb, pe)) { |
| 297 | /* XXX What do we do here ? */ |
| 298 | if (pe_num) |
| 299 | pnv_ioda_free_pe(phb, pe_num); |
| 300 | pdn->pe_number = IODA_INVALID_PE; |
| 301 | pe->pdev = NULL; |
| 302 | pci_dev_put(dev); |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| 306 | /* Assign a DMA weight to the device */ |
| 307 | pe->dma_weight = pnv_ioda_dma_weight(dev); |
| 308 | if (pe->dma_weight != 0) { |
| 309 | phb->ioda.dma_weight += pe->dma_weight; |
| 310 | phb->ioda.dma_pe_count++; |
| 311 | } |
| 312 | |
| 313 | /* Link the PE */ |
| 314 | pnv_ioda_link_pe_by_weight(phb, pe); |
| 315 | |
| 316 | return pe; |
| 317 | } |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 318 | #endif /* Useful for SRIOV case */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 319 | |
| 320 | static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe) |
| 321 | { |
| 322 | struct pci_dev *dev; |
| 323 | |
| 324 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 325 | struct pci_dn *pdn = pnv_ioda_get_pdn(dev); |
| 326 | |
| 327 | if (pdn == NULL) { |
| 328 | pr_warn("%s: No device node associated with device !\n", |
| 329 | pci_name(dev)); |
| 330 | continue; |
| 331 | } |
| 332 | pci_dev_get(dev); |
| 333 | pdn->pcidev = dev; |
| 334 | pdn->pe_number = pe->pe_number; |
| 335 | pe->dma_weight += pnv_ioda_dma_weight(dev); |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 336 | if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 337 | pnv_ioda_setup_same_PE(dev->subordinate, pe); |
| 338 | } |
| 339 | } |
| 340 | |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 341 | /* |
| 342 | * There're 2 types of PCI bus sensitive PEs: One that is compromised of |
| 343 | * single PCI bus. Another one that contains the primary PCI bus and its |
| 344 | * subordinate PCI devices and buses. The second type of PE is normally |
| 345 | * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports. |
| 346 | */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 347 | 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] | 348 | { |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 349 | struct pci_controller *hose = pci_bus_to_host(bus); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 350 | struct pnv_phb *phb = hose->private_data; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 351 | struct pnv_ioda_pe *pe; |
| 352 | int pe_num; |
| 353 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 354 | pe_num = pnv_ioda_alloc_pe(phb); |
| 355 | if (pe_num == IODA_INVALID_PE) { |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 356 | pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n", |
| 357 | __func__, pci_domain_nr(bus), bus->number); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
| 361 | pe = &phb->ioda.pe_array[pe_num]; |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 362 | pe->flags = (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 363 | pe->pbus = bus; |
| 364 | pe->pdev = NULL; |
| 365 | pe->tce32_seg = -1; |
| 366 | pe->mve_number = -1; |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 367 | pe->rid = bus->busn_res.start << 8; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 368 | pe->dma_weight = 0; |
| 369 | |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 370 | if (all) |
| 371 | pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n", |
| 372 | bus->busn_res.start, bus->busn_res.end, pe_num); |
| 373 | else |
| 374 | pe_info(pe, "Secondary bus %d associated with PE#%d\n", |
| 375 | bus->busn_res.start, pe_num); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 376 | |
| 377 | if (pnv_ioda_configure_pe(phb, pe)) { |
| 378 | /* XXX What do we do here ? */ |
| 379 | if (pe_num) |
| 380 | pnv_ioda_free_pe(phb, pe_num); |
| 381 | pe->pbus = NULL; |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | /* Associate it with all child devices */ |
| 386 | pnv_ioda_setup_same_PE(bus, pe); |
| 387 | |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 388 | /* Put PE to the list */ |
| 389 | list_add_tail(&pe->list, &phb->ioda.pe_list); |
| 390 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 391 | /* Account for one DMA PE if at least one DMA capable device exist |
| 392 | * below the bridge |
| 393 | */ |
| 394 | if (pe->dma_weight != 0) { |
| 395 | phb->ioda.dma_weight += pe->dma_weight; |
| 396 | phb->ioda.dma_pe_count++; |
| 397 | } |
| 398 | |
| 399 | /* Link the PE */ |
| 400 | pnv_ioda_link_pe_by_weight(phb, pe); |
| 401 | } |
| 402 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 403 | static void pnv_ioda_setup_PEs(struct pci_bus *bus) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 404 | { |
| 405 | struct pci_dev *dev; |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 406 | |
| 407 | pnv_ioda_setup_bus_PE(bus, 0); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 408 | |
| 409 | list_for_each_entry(dev, &bus->devices, bus_list) { |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 410 | if (dev->subordinate) { |
| 411 | if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) |
| 412 | pnv_ioda_setup_bus_PE(dev->subordinate, 1); |
| 413 | else |
| 414 | pnv_ioda_setup_PEs(dev->subordinate); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Configure PEs so that the downstream PCI buses and devices |
| 421 | * could have their associated PE#. Unfortunately, we didn't |
| 422 | * figure out the way to identify the PLX bridge yet. So we |
| 423 | * simply put the PCI bus and the subordinate behind the root |
| 424 | * port to PE# here. The game rule here is expected to be changed |
| 425 | * as soon as we can detected PLX bridge correctly. |
| 426 | */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 427 | static void pnv_pci_ioda_setup_PEs(void) |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 428 | { |
| 429 | struct pci_controller *hose, *tmp; |
| 430 | |
| 431 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
| 432 | pnv_ioda_setup_PEs(hose->bus); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 436 | static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *dev) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 437 | { |
| 438 | /* We delay DMA setup after we have assigned all PE# */ |
| 439 | } |
| 440 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 441 | static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 442 | { |
| 443 | struct pci_dev *dev; |
| 444 | |
| 445 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 446 | set_iommu_table_base(&dev->dev, &pe->tce32_table); |
| 447 | if (dev->subordinate) |
| 448 | pnv_ioda_setup_bus_dma(pe, dev->subordinate); |
| 449 | } |
| 450 | } |
| 451 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 452 | static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb, |
| 453 | struct pnv_ioda_pe *pe, unsigned int base, |
| 454 | unsigned int segs) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 455 | { |
| 456 | |
| 457 | struct page *tce_mem = NULL; |
| 458 | const __be64 *swinvp; |
| 459 | struct iommu_table *tbl; |
| 460 | unsigned int i; |
| 461 | int64_t rc; |
| 462 | void *addr; |
| 463 | |
| 464 | /* 256M DMA window, 4K TCE pages, 8 bytes TCE */ |
| 465 | #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8) |
| 466 | |
| 467 | /* XXX FIXME: Handle 64-bit only DMA devices */ |
| 468 | /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */ |
| 469 | /* XXX FIXME: Allocate multi-level tables on PHB3 */ |
| 470 | |
| 471 | /* We shouldn't already have a 32-bit DMA associated */ |
| 472 | if (WARN_ON(pe->tce32_seg >= 0)) |
| 473 | return; |
| 474 | |
| 475 | /* Grab a 32-bit TCE table */ |
| 476 | pe->tce32_seg = base; |
| 477 | pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n", |
| 478 | (base << 28), ((base + segs) << 28) - 1); |
| 479 | |
| 480 | /* XXX Currently, we allocate one big contiguous table for the |
| 481 | * TCEs. We only really need one chunk per 256M of TCE space |
| 482 | * (ie per segment) but that's an optimization for later, it |
| 483 | * requires some added smarts with our get/put_tce implementation |
| 484 | */ |
| 485 | tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL, |
| 486 | get_order(TCE32_TABLE_SIZE * segs)); |
| 487 | if (!tce_mem) { |
| 488 | pe_err(pe, " Failed to allocate a 32-bit TCE memory\n"); |
| 489 | goto fail; |
| 490 | } |
| 491 | addr = page_address(tce_mem); |
| 492 | memset(addr, 0, TCE32_TABLE_SIZE * segs); |
| 493 | |
| 494 | /* Configure HW */ |
| 495 | for (i = 0; i < segs; i++) { |
| 496 | rc = opal_pci_map_pe_dma_window(phb->opal_id, |
| 497 | pe->pe_number, |
| 498 | base + i, 1, |
| 499 | __pa(addr) + TCE32_TABLE_SIZE * i, |
| 500 | TCE32_TABLE_SIZE, 0x1000); |
| 501 | if (rc) { |
| 502 | pe_err(pe, " Failed to configure 32-bit TCE table," |
| 503 | " err %ld\n", rc); |
| 504 | goto fail; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* Setup linux iommu table */ |
| 509 | tbl = &pe->tce32_table; |
| 510 | pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs, |
| 511 | base << 28); |
| 512 | |
| 513 | /* OPAL variant of P7IOC SW invalidated TCEs */ |
| 514 | swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL); |
| 515 | if (swinvp) { |
| 516 | /* We need a couple more fields -- an address and a data |
| 517 | * to or. Since the bus is only printed out on table free |
| 518 | * errors, and on the first pass the data will be a relative |
| 519 | * bus number, print that out instead. |
| 520 | */ |
| 521 | tbl->it_busno = 0; |
| 522 | tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8); |
| 523 | tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE |
| 524 | | TCE_PCI_SWINV_PAIR; |
| 525 | } |
| 526 | iommu_init_table(tbl, phb->hose->node); |
| 527 | |
| 528 | if (pe->pdev) |
| 529 | set_iommu_table_base(&pe->pdev->dev, tbl); |
| 530 | else |
| 531 | pnv_ioda_setup_bus_dma(pe, pe->pbus); |
| 532 | |
| 533 | return; |
| 534 | fail: |
| 535 | /* XXX Failure: Try to fallback to 64-bit only ? */ |
| 536 | if (pe->tce32_seg >= 0) |
| 537 | pe->tce32_seg = -1; |
| 538 | if (tce_mem) |
| 539 | __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs)); |
| 540 | } |
| 541 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 542 | static void pnv_ioda_setup_dma(struct pnv_phb *phb) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 543 | { |
| 544 | struct pci_controller *hose = phb->hose; |
| 545 | unsigned int residual, remaining, segs, tw, base; |
| 546 | struct pnv_ioda_pe *pe; |
| 547 | |
| 548 | /* If we have more PE# than segments available, hand out one |
| 549 | * per PE until we run out and let the rest fail. If not, |
| 550 | * then we assign at least one segment per PE, plus more based |
| 551 | * on the amount of devices under that PE |
| 552 | */ |
| 553 | if (phb->ioda.dma_pe_count > phb->ioda.tce32_count) |
| 554 | residual = 0; |
| 555 | else |
| 556 | residual = phb->ioda.tce32_count - |
| 557 | phb->ioda.dma_pe_count; |
| 558 | |
| 559 | pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n", |
| 560 | hose->global_number, phb->ioda.tce32_count); |
| 561 | pr_info("PCI: %d PE# for a total weight of %d\n", |
| 562 | phb->ioda.dma_pe_count, phb->ioda.dma_weight); |
| 563 | |
| 564 | /* Walk our PE list and configure their DMA segments, hand them |
| 565 | * out one base segment plus any residual segments based on |
| 566 | * weight |
| 567 | */ |
| 568 | remaining = phb->ioda.tce32_count; |
| 569 | tw = phb->ioda.dma_weight; |
| 570 | base = 0; |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 571 | list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) { |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 572 | if (!pe->dma_weight) |
| 573 | continue; |
| 574 | if (!remaining) { |
| 575 | pe_warn(pe, "No DMA32 resources available\n"); |
| 576 | continue; |
| 577 | } |
| 578 | segs = 1; |
| 579 | if (residual) { |
| 580 | segs += ((pe->dma_weight * residual) + (tw / 2)) / tw; |
| 581 | if (segs > remaining) |
| 582 | segs = remaining; |
| 583 | } |
| 584 | pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n", |
| 585 | pe->dma_weight, segs); |
| 586 | pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs); |
| 587 | remaining -= segs; |
| 588 | base += segs; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | #ifdef CONFIG_PCI_MSI |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame^] | 593 | static void pnv_ioda2_msi_eoi(struct irq_data *d) |
| 594 | { |
| 595 | unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d); |
| 596 | struct irq_chip *chip = irq_data_get_irq_chip(d); |
| 597 | struct pnv_phb *phb = container_of(chip, struct pnv_phb, |
| 598 | ioda.irq_chip); |
| 599 | int64_t rc; |
| 600 | |
| 601 | rc = opal_pci_msi_eoi(phb->opal_id, hw_irq); |
| 602 | WARN_ON_ONCE(rc); |
| 603 | |
| 604 | icp_native_eoi(d); |
| 605 | } |
| 606 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 607 | 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^] | 608 | unsigned int hwirq, unsigned int virq, |
| 609 | unsigned int is_64, struct msi_msg *msg) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 610 | { |
| 611 | struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev); |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame^] | 612 | struct irq_data *idata; |
| 613 | struct irq_chip *ichip; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 614 | unsigned int xive_num = hwirq - phb->msi_base; |
| 615 | uint64_t addr64; |
| 616 | uint32_t addr32, data; |
| 617 | int rc; |
| 618 | |
| 619 | /* No PE assigned ? bail out ... no MSI for you ! */ |
| 620 | if (pe == NULL) |
| 621 | return -ENXIO; |
| 622 | |
| 623 | /* Check if we have an MVE */ |
| 624 | if (pe->mve_number < 0) |
| 625 | return -ENXIO; |
| 626 | |
| 627 | /* Assign XIVE to PE */ |
| 628 | rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num); |
| 629 | if (rc) { |
| 630 | pr_warn("%s: OPAL error %d setting XIVE %d PE\n", |
| 631 | pci_name(dev), rc, xive_num); |
| 632 | return -EIO; |
| 633 | } |
| 634 | |
| 635 | if (is_64) { |
| 636 | rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1, |
| 637 | &addr64, &data); |
| 638 | if (rc) { |
| 639 | pr_warn("%s: OPAL error %d getting 64-bit MSI data\n", |
| 640 | pci_name(dev), rc); |
| 641 | return -EIO; |
| 642 | } |
| 643 | msg->address_hi = addr64 >> 32; |
| 644 | msg->address_lo = addr64 & 0xfffffffful; |
| 645 | } else { |
| 646 | rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1, |
| 647 | &addr32, &data); |
| 648 | if (rc) { |
| 649 | pr_warn("%s: OPAL error %d getting 32-bit MSI data\n", |
| 650 | pci_name(dev), rc); |
| 651 | return -EIO; |
| 652 | } |
| 653 | msg->address_hi = 0; |
| 654 | msg->address_lo = addr32; |
| 655 | } |
| 656 | msg->data = data; |
| 657 | |
Gavin Shan | 137436c | 2013-04-25 19:20:59 +0000 | [diff] [blame^] | 658 | /* |
| 659 | * Change the IRQ chip for the MSI interrupts on PHB3. |
| 660 | * The corresponding IRQ chip should be populated for |
| 661 | * the first time. |
| 662 | */ |
| 663 | if (phb->type == PNV_PHB_IODA2) { |
| 664 | if (!phb->ioda.irq_chip_init) { |
| 665 | idata = irq_get_irq_data(virq); |
| 666 | ichip = irq_data_get_irq_chip(idata); |
| 667 | phb->ioda.irq_chip_init = 1; |
| 668 | phb->ioda.irq_chip = *ichip; |
| 669 | phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi; |
| 670 | } |
| 671 | |
| 672 | irq_set_chip(virq, &phb->ioda.irq_chip); |
| 673 | } |
| 674 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 675 | pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d)," |
| 676 | " address=%x_%08x data=%x PE# %d\n", |
| 677 | pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num, |
| 678 | msg->address_hi, msg->address_lo, data, pe->pe_number); |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) |
| 684 | { |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 685 | unsigned int count; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 686 | const __be32 *prop = of_get_property(phb->hose->dn, |
| 687 | "ibm,opal-msi-ranges", NULL); |
| 688 | if (!prop) { |
| 689 | /* BML Fallback */ |
| 690 | prop = of_get_property(phb->hose->dn, "msi-ranges", NULL); |
| 691 | } |
| 692 | if (!prop) |
| 693 | return; |
| 694 | |
| 695 | phb->msi_base = be32_to_cpup(prop); |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 696 | count = be32_to_cpup(prop + 1); |
| 697 | if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) { |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 698 | pr_err("PCI %d: Failed to allocate MSI bitmap !\n", |
| 699 | phb->hose->global_number); |
| 700 | return; |
| 701 | } |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 702 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 703 | phb->msi_setup = pnv_pci_ioda_msi_setup; |
| 704 | phb->msi32_support = 1; |
| 705 | pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n", |
Gavin Shan | fb1b55d | 2013-03-05 21:12:37 +0000 | [diff] [blame] | 706 | count, phb->msi_base); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 707 | } |
| 708 | #else |
| 709 | static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { } |
| 710 | #endif /* CONFIG_PCI_MSI */ |
| 711 | |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 712 | /* |
| 713 | * This function is supposed to be called on basis of PE from top |
| 714 | * to bottom style. So the the I/O or MMIO segment assigned to |
| 715 | * parent PE could be overrided by its child PEs if necessary. |
| 716 | */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 717 | static void pnv_ioda_setup_pe_seg(struct pci_controller *hose, |
| 718 | struct pnv_ioda_pe *pe) |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 719 | { |
| 720 | struct pnv_phb *phb = hose->private_data; |
| 721 | struct pci_bus_region region; |
| 722 | struct resource *res; |
| 723 | int i, index; |
| 724 | int rc; |
| 725 | |
| 726 | /* |
| 727 | * NOTE: We only care PCI bus based PE for now. For PCI |
| 728 | * device based PE, for example SRIOV sensitive VF should |
| 729 | * be figured out later. |
| 730 | */ |
| 731 | BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))); |
| 732 | |
| 733 | pci_bus_for_each_resource(pe->pbus, res, i) { |
| 734 | if (!res || !res->flags || |
| 735 | res->start > res->end) |
| 736 | continue; |
| 737 | |
| 738 | if (res->flags & IORESOURCE_IO) { |
| 739 | region.start = res->start - phb->ioda.io_pci_base; |
| 740 | region.end = res->end - phb->ioda.io_pci_base; |
| 741 | index = region.start / phb->ioda.io_segsize; |
| 742 | |
| 743 | while (index < phb->ioda.total_pe && |
| 744 | region.start <= region.end) { |
| 745 | phb->ioda.io_segmap[index] = pe->pe_number; |
| 746 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 747 | pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index); |
| 748 | if (rc != OPAL_SUCCESS) { |
| 749 | pr_err("%s: OPAL error %d when mapping IO " |
| 750 | "segment #%d to PE#%d\n", |
| 751 | __func__, rc, index, pe->pe_number); |
| 752 | break; |
| 753 | } |
| 754 | |
| 755 | region.start += phb->ioda.io_segsize; |
| 756 | index++; |
| 757 | } |
| 758 | } else if (res->flags & IORESOURCE_MEM) { |
| 759 | region.start = res->start - |
| 760 | hose->pci_mem_offset - |
| 761 | phb->ioda.m32_pci_base; |
| 762 | region.end = res->end - |
| 763 | hose->pci_mem_offset - |
| 764 | phb->ioda.m32_pci_base; |
| 765 | index = region.start / phb->ioda.m32_segsize; |
| 766 | |
| 767 | while (index < phb->ioda.total_pe && |
| 768 | region.start <= region.end) { |
| 769 | phb->ioda.m32_segmap[index] = pe->pe_number; |
| 770 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, |
| 771 | pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index); |
| 772 | if (rc != OPAL_SUCCESS) { |
| 773 | pr_err("%s: OPAL error %d when mapping M32 " |
| 774 | "segment#%d to PE#%d", |
| 775 | __func__, rc, index, pe->pe_number); |
| 776 | break; |
| 777 | } |
| 778 | |
| 779 | region.start += phb->ioda.m32_segsize; |
| 780 | index++; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 786 | static void pnv_pci_ioda_setup_seg(void) |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 787 | { |
| 788 | struct pci_controller *tmp, *hose; |
| 789 | struct pnv_phb *phb; |
| 790 | struct pnv_ioda_pe *pe; |
| 791 | |
| 792 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
| 793 | phb = hose->private_data; |
| 794 | list_for_each_entry(pe, &phb->ioda.pe_list, list) { |
| 795 | pnv_ioda_setup_pe_seg(hose, pe); |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 800 | static void pnv_pci_ioda_setup_DMA(void) |
Gavin Shan | 13395c4 | 2012-08-20 03:49:17 +0000 | [diff] [blame] | 801 | { |
| 802 | struct pci_controller *hose, *tmp; |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 803 | struct pnv_phb *phb; |
Gavin Shan | 13395c4 | 2012-08-20 03:49:17 +0000 | [diff] [blame] | 804 | |
| 805 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { |
| 806 | pnv_ioda_setup_dma(hose->private_data); |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 807 | |
| 808 | /* Mark the PHB initialization done */ |
| 809 | phb = hose->private_data; |
| 810 | phb->initialized = 1; |
Gavin Shan | 13395c4 | 2012-08-20 03:49:17 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 814 | static void pnv_pci_ioda_fixup(void) |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 815 | { |
| 816 | pnv_pci_ioda_setup_PEs(); |
Gavin Shan | 11685be | 2012-08-20 03:49:16 +0000 | [diff] [blame] | 817 | pnv_pci_ioda_setup_seg(); |
Gavin Shan | 13395c4 | 2012-08-20 03:49:17 +0000 | [diff] [blame] | 818 | pnv_pci_ioda_setup_DMA(); |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Gavin Shan | 271fd03 | 2012-09-11 16:59:47 -0600 | [diff] [blame] | 821 | /* |
| 822 | * Returns the alignment for I/O or memory windows for P2P |
| 823 | * bridges. That actually depends on how PEs are segmented. |
| 824 | * For now, we return I/O or M32 segment size for PE sensitive |
| 825 | * P2P bridges. Otherwise, the default values (4KiB for I/O, |
| 826 | * 1MiB for memory) will be returned. |
| 827 | * |
| 828 | * The current PCI bus might be put into one PE, which was |
| 829 | * create against the parent PCI bridge. For that case, we |
| 830 | * needn't enlarge the alignment so that we can save some |
| 831 | * resources. |
| 832 | */ |
| 833 | static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus, |
| 834 | unsigned long type) |
| 835 | { |
| 836 | struct pci_dev *bridge; |
| 837 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 838 | struct pnv_phb *phb = hose->private_data; |
| 839 | int num_pci_bridges = 0; |
| 840 | |
| 841 | bridge = bus->self; |
| 842 | while (bridge) { |
| 843 | if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) { |
| 844 | num_pci_bridges++; |
| 845 | if (num_pci_bridges >= 2) |
| 846 | return 1; |
| 847 | } |
| 848 | |
| 849 | bridge = bridge->bus->self; |
| 850 | } |
| 851 | |
| 852 | /* We need support prefetchable memory window later */ |
| 853 | if (type & IORESOURCE_MEM) |
| 854 | return phb->ioda.m32_segsize; |
| 855 | |
| 856 | return phb->ioda.io_segsize; |
| 857 | } |
| 858 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 859 | /* Prevent enabling devices for which we couldn't properly |
| 860 | * assign a PE |
| 861 | */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 862 | static int pnv_pci_enable_device_hook(struct pci_dev *dev) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 863 | { |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 864 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 865 | struct pnv_phb *phb = hose->private_data; |
| 866 | struct pci_dn *pdn; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 867 | |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 868 | /* The function is probably called while the PEs have |
| 869 | * not be created yet. For example, resource reassignment |
| 870 | * during PCI probe period. We just skip the check if |
| 871 | * PEs isn't ready. |
| 872 | */ |
| 873 | if (!phb->initialized) |
| 874 | return 0; |
| 875 | |
| 876 | pdn = pnv_ioda_get_pdn(dev); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 877 | if (!pdn || pdn->pe_number == IODA_INVALID_PE) |
| 878 | return -EINVAL; |
Gavin Shan | db1266c | 2012-08-20 03:49:18 +0000 | [diff] [blame] | 879 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus, |
| 884 | u32 devfn) |
| 885 | { |
| 886 | return phb->ioda.pe_rmap[(bus->number << 8) | devfn]; |
| 887 | } |
| 888 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 889 | void __init pnv_pci_init_ioda_phb(struct device_node *np, int ioda_type) |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 890 | { |
| 891 | struct pci_controller *hose; |
| 892 | static int primary = 1; |
| 893 | struct pnv_phb *phb; |
| 894 | unsigned long size, m32map_off, iomap_off, pemap_off; |
| 895 | const u64 *prop64; |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 896 | const u32 *prop32; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 897 | u64 phb_id; |
| 898 | void *aux; |
| 899 | long rc; |
| 900 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 901 | 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] | 902 | |
| 903 | prop64 = of_get_property(np, "ibm,opal-phbid", NULL); |
| 904 | if (!prop64) { |
| 905 | pr_err(" Missing \"ibm,opal-phbid\" property !\n"); |
| 906 | return; |
| 907 | } |
| 908 | phb_id = be64_to_cpup(prop64); |
| 909 | pr_debug(" PHB-ID : 0x%016llx\n", phb_id); |
| 910 | |
| 911 | phb = alloc_bootmem(sizeof(struct pnv_phb)); |
| 912 | if (phb) { |
| 913 | memset(phb, 0, sizeof(struct pnv_phb)); |
| 914 | phb->hose = hose = pcibios_alloc_controller(np); |
| 915 | } |
| 916 | if (!phb || !phb->hose) { |
| 917 | pr_err("PCI: Failed to allocate PCI controller for %s\n", |
| 918 | np->full_name); |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | spin_lock_init(&phb->lock); |
| 923 | /* XXX Use device-tree */ |
| 924 | hose->first_busno = 0; |
| 925 | hose->last_busno = 0xff; |
| 926 | hose->private_data = phb; |
| 927 | phb->opal_id = phb_id; |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 928 | phb->type = ioda_type; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 929 | |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 930 | /* Detect specific models for error handling */ |
| 931 | if (of_device_is_compatible(np, "ibm,p7ioc-pciex")) |
| 932 | phb->model = PNV_PHB_MODEL_P7IOC; |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 933 | else if (of_device_is_compatible(np, "ibm,p8-pciex")) |
| 934 | phb->model = PNV_PHB_MODEL_PHB3; |
Benjamin Herrenschmidt | cee72d5 | 2011-11-29 18:22:53 +0000 | [diff] [blame] | 935 | else |
| 936 | phb->model = PNV_PHB_MODEL_UNKNOWN; |
| 937 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 938 | /* Parse 32-bit and IO ranges (if any) */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 939 | pci_process_bridge_OF_ranges(phb->hose, np, primary); |
| 940 | primary = 0; |
| 941 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 942 | /* Get registers */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 943 | phb->regs = of_iomap(np, 0); |
| 944 | if (phb->regs == NULL) |
| 945 | pr_err(" Failed to map registers !\n"); |
| 946 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 947 | /* Initialize more IODA stuff */ |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 948 | prop32 = of_get_property(np, "ibm,opal-num-pes", NULL); |
| 949 | if (!prop32) |
| 950 | phb->ioda.total_pe = 1; |
| 951 | else |
| 952 | phb->ioda.total_pe = *prop32; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 953 | |
| 954 | phb->ioda.m32_size = resource_size(&hose->mem_resources[0]); |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 955 | /* FW Has already off top 64k of M32 space (MSI space) */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 956 | phb->ioda.m32_size += 0x10000; |
| 957 | |
| 958 | phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe; |
| 959 | phb->ioda.m32_pci_base = hose->mem_resources[0].start - |
| 960 | hose->pci_mem_offset; |
| 961 | phb->ioda.io_size = hose->pci_io_size; |
| 962 | phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe; |
| 963 | phb->ioda.io_pci_base = 0; /* XXX calculate this ? */ |
| 964 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 965 | /* Allocate aux data & arrays |
| 966 | * |
| 967 | * XXX TODO: Don't allocate io segmap on PHB3 |
| 968 | */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 969 | size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long)); |
| 970 | m32map_off = size; |
Gavin Shan | e47747f | 2012-08-20 03:49:19 +0000 | [diff] [blame] | 971 | size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 972 | iomap_off = size; |
Gavin Shan | e47747f | 2012-08-20 03:49:19 +0000 | [diff] [blame] | 973 | size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 974 | pemap_off = size; |
| 975 | size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe); |
| 976 | aux = alloc_bootmem(size); |
| 977 | memset(aux, 0, size); |
| 978 | phb->ioda.pe_alloc = aux; |
| 979 | phb->ioda.m32_segmap = aux + m32map_off; |
| 980 | phb->ioda.io_segmap = aux + iomap_off; |
| 981 | phb->ioda.pe_array = aux + pemap_off; |
| 982 | set_bit(0, phb->ioda.pe_alloc); |
| 983 | |
Gavin Shan | 7ebdf95 | 2012-08-20 03:49:15 +0000 | [diff] [blame] | 984 | INIT_LIST_HEAD(&phb->ioda.pe_dma_list); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 985 | INIT_LIST_HEAD(&phb->ioda.pe_list); |
| 986 | |
| 987 | /* Calculate how many 32-bit TCE segments we have */ |
| 988 | phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28; |
| 989 | |
| 990 | /* Clear unusable m64 */ |
| 991 | hose->mem_resources[1].flags = 0; |
| 992 | hose->mem_resources[1].start = 0; |
| 993 | hose->mem_resources[1].end = 0; |
| 994 | hose->mem_resources[2].flags = 0; |
| 995 | hose->mem_resources[2].start = 0; |
| 996 | hose->mem_resources[2].end = 0; |
| 997 | |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 998 | #if 0 /* We should really do that ... */ |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 999 | rc = opal_pci_set_phb_mem_window(opal->phb_id, |
| 1000 | window_type, |
| 1001 | window_num, |
| 1002 | starting_real_address, |
| 1003 | starting_pci_address, |
| 1004 | segment_size); |
| 1005 | #endif |
| 1006 | |
| 1007 | pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n", |
| 1008 | phb->ioda.total_pe, |
| 1009 | phb->ioda.m32_size, phb->ioda.m32_segsize, |
| 1010 | phb->ioda.io_size, phb->ioda.io_segsize); |
| 1011 | |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1012 | phb->hose->ops = &pnv_pci_ops; |
| 1013 | |
| 1014 | /* Setup RID -> PE mapping function */ |
| 1015 | phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe; |
| 1016 | |
| 1017 | /* Setup TCEs */ |
| 1018 | phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup; |
| 1019 | |
| 1020 | /* Setup MSI support */ |
| 1021 | pnv_pci_init_ioda_msis(phb); |
| 1022 | |
Gavin Shan | c40a421 | 2012-08-20 03:49:20 +0000 | [diff] [blame] | 1023 | /* |
| 1024 | * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here |
| 1025 | * to let the PCI core do resource assignment. It's supposed |
| 1026 | * that the PCI core will do correct I/O and MMIO alignment |
| 1027 | * for the P2P bridge bars so that each PCI bus (excluding |
| 1028 | * the child P2P bridges) can form individual PE. |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1029 | */ |
Gavin Shan | fb446ad | 2012-08-20 03:49:14 +0000 | [diff] [blame] | 1030 | ppc_md.pcibios_fixup = pnv_pci_ioda_fixup; |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1031 | ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook; |
Gavin Shan | 271fd03 | 2012-09-11 16:59:47 -0600 | [diff] [blame] | 1032 | ppc_md.pcibios_window_alignment = pnv_pci_window_alignment; |
Gavin Shan | c40a421 | 2012-08-20 03:49:20 +0000 | [diff] [blame] | 1033 | pci_add_flags(PCI_REASSIGN_ALL_RSRC); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1034 | |
| 1035 | /* Reset IODA tables to a clean state */ |
Benjamin Herrenschmidt | f11fe55 | 2011-11-29 18:22:50 +0000 | [diff] [blame] | 1036 | rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1037 | if (rc) |
Benjamin Herrenschmidt | f11fe55 | 2011-11-29 18:22:50 +0000 | [diff] [blame] | 1038 | pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc); |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1039 | |
| 1040 | /* |
| 1041 | * On IODA1 map everything to PE#0, on IODA2 we assume the IODA reset |
| 1042 | * has cleared the RTT which has the same effect |
| 1043 | */ |
| 1044 | if (ioda_type == PNV_PHB_IODA1) |
| 1045 | opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE); |
| 1046 | } |
| 1047 | |
| 1048 | void pnv_pci_init_ioda2_phb(struct device_node *np) |
| 1049 | { |
| 1050 | pnv_pci_init_ioda_phb(np, PNV_PHB_IODA2); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | void __init pnv_pci_init_ioda_hub(struct device_node *np) |
| 1054 | { |
| 1055 | struct device_node *phbn; |
| 1056 | const u64 *prop64; |
| 1057 | u64 hub_id; |
| 1058 | |
| 1059 | pr_info("Probing IODA IO-Hub %s\n", np->full_name); |
| 1060 | |
| 1061 | prop64 = of_get_property(np, "ibm,opal-hubid", NULL); |
| 1062 | if (!prop64) { |
| 1063 | pr_err(" Missing \"ibm,opal-hubid\" property !\n"); |
| 1064 | return; |
| 1065 | } |
| 1066 | hub_id = be64_to_cpup(prop64); |
| 1067 | pr_devel(" HUB-ID : 0x%016llx\n", hub_id); |
| 1068 | |
| 1069 | /* Count child PHBs */ |
| 1070 | for_each_child_of_node(np, phbn) { |
| 1071 | /* Look for IODA1 PHBs */ |
| 1072 | if (of_device_is_compatible(phbn, "ibm,ioda-phb")) |
Gavin Shan | aa0c033 | 2013-04-25 19:20:57 +0000 | [diff] [blame] | 1073 | pnv_pci_init_ioda_phb(phbn, PNV_PHB_IODA1); |
Benjamin Herrenschmidt | 184cd4a | 2011-11-15 17:29:08 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |