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