1cd248341SJan Glauber /* 2cd248341SJan Glauber * Copyright IBM Corp. 2012 3cd248341SJan Glauber * 4cd248341SJan Glauber * Author(s): 5cd248341SJan Glauber * Jan Glauber <jang@linux.vnet.ibm.com> 6cd248341SJan Glauber * 7cd248341SJan Glauber * The System z PCI code is a rewrite from a prototype by 8cd248341SJan Glauber * the following people (Kudoz!): 9bedef755SJan Glauber * Alexander Schmidt 10bedef755SJan Glauber * Christoph Raisch 11bedef755SJan Glauber * Hannes Hering 12bedef755SJan Glauber * Hoang-Nam Nguyen 13bedef755SJan Glauber * Jan-Bernd Themann 14bedef755SJan Glauber * Stefan Roscher 15bedef755SJan Glauber * Thomas Klein 16cd248341SJan Glauber */ 17cd248341SJan Glauber 18cd248341SJan Glauber #define COMPONENT "zPCI" 19cd248341SJan Glauber #define pr_fmt(fmt) COMPONENT ": " fmt 20cd248341SJan Glauber 21cd248341SJan Glauber #include <linux/kernel.h> 22cd248341SJan Glauber #include <linux/slab.h> 23cd248341SJan Glauber #include <linux/err.h> 24cd248341SJan Glauber #include <linux/export.h> 25cd248341SJan Glauber #include <linux/delay.h> 269a4da8a5SJan Glauber #include <linux/irq.h> 279a4da8a5SJan Glauber #include <linux/kernel_stat.h> 28cd248341SJan Glauber #include <linux/seq_file.h> 29cd248341SJan Glauber #include <linux/pci.h> 30cd248341SJan Glauber #include <linux/msi.h> 31cd248341SJan Glauber 329a4da8a5SJan Glauber #include <asm/isc.h> 339a4da8a5SJan Glauber #include <asm/airq.h> 34cd248341SJan Glauber #include <asm/facility.h> 35cd248341SJan Glauber #include <asm/pci_insn.h> 36a755a45dSJan Glauber #include <asm/pci_clp.h> 37828b35f6SJan Glauber #include <asm/pci_dma.h> 38cd248341SJan Glauber 39cd248341SJan Glauber #define DEBUG /* enable pr_debug */ 40cd248341SJan Glauber 419a4da8a5SJan Glauber #define SIC_IRQ_MODE_ALL 0 429a4da8a5SJan Glauber #define SIC_IRQ_MODE_SINGLE 1 439a4da8a5SJan Glauber 44cd248341SJan Glauber #define ZPCI_NR_DMA_SPACES 1 45cd248341SJan Glauber #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS 46cd248341SJan Glauber 47cd248341SJan Glauber /* list of all detected zpci devices */ 4867f43f38SSebastian Ott static LIST_HEAD(zpci_list); 4957b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock); 501f44a225SMartin Schwidefsky 511f44a225SMartin Schwidefsky static void zpci_enable_irq(struct irq_data *data); 521f44a225SMartin Schwidefsky static void zpci_disable_irq(struct irq_data *data); 531f44a225SMartin Schwidefsky 541f44a225SMartin Schwidefsky static struct irq_chip zpci_irq_chip = { 551f44a225SMartin Schwidefsky .name = "zPCI", 561f44a225SMartin Schwidefsky .irq_unmask = zpci_enable_irq, 571f44a225SMartin Schwidefsky .irq_mask = zpci_disable_irq, 581f44a225SMartin Schwidefsky }; 591f44a225SMartin Schwidefsky 60cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 61cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 62cd248341SJan Glauber 635d0d8f43SMartin Schwidefsky static struct airq_iv *zpci_aisb_iv; 641f44a225SMartin Schwidefsky static struct airq_iv *zpci_aibv[ZPCI_NR_DEVICES]; 659a4da8a5SJan Glauber 66f4eae94fSMartin Schwidefsky /* Adapter interrupt definitions */ 67f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq); 68f4eae94fSMartin Schwidefsky 69f4eae94fSMartin Schwidefsky static struct airq_struct zpci_airq = { 70f4eae94fSMartin Schwidefsky .handler = zpci_irq_handler, 71f4eae94fSMartin Schwidefsky .isc = PCI_ISC, 72f4eae94fSMartin Schwidefsky }; 739a4da8a5SJan Glauber 74cd248341SJan Glauber /* I/O Map */ 75cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 76cd248341SJan Glauber static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 77cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 78cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 79cd248341SJan Glauber 80d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 81d0b08853SJan Glauber 82cd248341SJan Glauber struct zpci_dev *get_zdev(struct pci_dev *pdev) 83cd248341SJan Glauber { 84cd248341SJan Glauber return (struct zpci_dev *) pdev->sysdata; 85cd248341SJan Glauber } 86cd248341SJan Glauber 87cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 88cd248341SJan Glauber { 89cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 90cd248341SJan Glauber 9157b5918cSSebastian Ott spin_lock(&zpci_list_lock); 92cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 93cd248341SJan Glauber if (tmp->fid == fid) { 94cd248341SJan Glauber zdev = tmp; 95cd248341SJan Glauber break; 96cd248341SJan Glauber } 97cd248341SJan Glauber } 9857b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 99cd248341SJan Glauber return zdev; 100cd248341SJan Glauber } 101cd248341SJan Glauber 102cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 103cd248341SJan Glauber { 104cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 105cd248341SJan Glauber } 106cd248341SJan Glauber 107cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 108cd248341SJan Glauber { 109cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 110cd248341SJan Glauber } 111cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 112cd248341SJan Glauber 113cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 114cd248341SJan Glauber { 115cd248341SJan Glauber return pci_domain_nr(bus); 116cd248341SJan Glauber } 117cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 118cd248341SJan Glauber 1199a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1205d0d8f43SMartin Schwidefsky static int zpci_set_airq(struct zpci_dev *zdev) 1219a4da8a5SJan Glauber { 1229a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 123cb4deb69SSebastian Ott struct zpci_fib fib = {0}; 1249a4da8a5SJan Glauber 125cb4deb69SSebastian Ott fib.isc = PCI_ISC; 126cb4deb69SSebastian Ott fib.sum = 1; /* enable summary notifications */ 127cb4deb69SSebastian Ott fib.noi = airq_iv_end(zdev->aibv); 128cb4deb69SSebastian Ott fib.aibv = (unsigned long) zdev->aibv->vector; 129cb4deb69SSebastian Ott fib.aibvo = 0; /* each zdev has its own interrupt vector */ 130cb4deb69SSebastian Ott fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8; 131cb4deb69SSebastian Ott fib.aisbo = zdev->aisb & 63; 1329a4da8a5SJan Glauber 133cb4deb69SSebastian Ott return zpci_mod_fc(req, &fib); 1349a4da8a5SJan Glauber } 1359a4da8a5SJan Glauber 1369a4da8a5SJan Glauber struct mod_pci_args { 1379a4da8a5SJan Glauber u64 base; 1389a4da8a5SJan Glauber u64 limit; 1399a4da8a5SJan Glauber u64 iota; 140d0b08853SJan Glauber u64 fmb_addr; 1419a4da8a5SJan Glauber }; 1429a4da8a5SJan Glauber 1439a4da8a5SJan Glauber static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) 1449a4da8a5SJan Glauber { 1459a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); 146cb4deb69SSebastian Ott struct zpci_fib fib = {0}; 1479a4da8a5SJan Glauber 148cb4deb69SSebastian Ott fib.pba = args->base; 149cb4deb69SSebastian Ott fib.pal = args->limit; 150cb4deb69SSebastian Ott fib.iota = args->iota; 151cb4deb69SSebastian Ott fib.fmb_addr = args->fmb_addr; 1529a4da8a5SJan Glauber 153cb4deb69SSebastian Ott return zpci_mod_fc(req, &fib); 1549a4da8a5SJan Glauber } 1559a4da8a5SJan Glauber 156828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 157828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 158828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 159828b35f6SJan Glauber { 160d0b08853SJan Glauber struct mod_pci_args args = { base, limit, iota, 0 }; 161828b35f6SJan Glauber 162828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 163828b35f6SJan Glauber args.iota |= ZPCI_IOTA_RTTO_FLAG; 164828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); 165828b35f6SJan Glauber } 166828b35f6SJan Glauber 167828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 168828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 169828b35f6SJan Glauber { 170d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 171828b35f6SJan Glauber 172828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); 173828b35f6SJan Glauber } 174828b35f6SJan Glauber 1759a4da8a5SJan Glauber /* Modify PCI: Unregister adapter interruptions */ 1765d0d8f43SMartin Schwidefsky static int zpci_clear_airq(struct zpci_dev *zdev) 1779a4da8a5SJan Glauber { 178d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 1799a4da8a5SJan Glauber 1809a4da8a5SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); 1819a4da8a5SJan Glauber } 1829a4da8a5SJan Glauber 183d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 184d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 185d0b08853SJan Glauber { 186d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 187d0b08853SJan Glauber 188d0b08853SJan Glauber if (zdev->fmb) 189d0b08853SJan Glauber return -EINVAL; 190d0b08853SJan Glauber 19108b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 192d0b08853SJan Glauber if (!zdev->fmb) 193d0b08853SJan Glauber return -ENOMEM; 194d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 195d0b08853SJan Glauber 196d0b08853SJan Glauber args.fmb_addr = virt_to_phys(zdev->fmb); 197d0b08853SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 198d0b08853SJan Glauber } 199d0b08853SJan Glauber 200d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 201d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 202d0b08853SJan Glauber { 203d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 204d0b08853SJan Glauber int rc; 205d0b08853SJan Glauber 206d0b08853SJan Glauber if (!zdev->fmb) 207d0b08853SJan Glauber return -EINVAL; 208d0b08853SJan Glauber 209d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 210d0b08853SJan Glauber rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 211d0b08853SJan Glauber 212d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 213d0b08853SJan Glauber zdev->fmb = NULL; 214d0b08853SJan Glauber return rc; 215d0b08853SJan Glauber } 216d0b08853SJan Glauber 217cd248341SJan Glauber #define ZPCI_PCIAS_CFGSPC 15 218cd248341SJan Glauber 219cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 220cd248341SJan Glauber { 221cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 222cd248341SJan Glauber u64 data; 223cd248341SJan Glauber int rc; 224cd248341SJan Glauber 2259389339fSMartin Schwidefsky rc = zpci_load(&data, req, offset); 226b170bad4SSebastian Ott if (!rc) { 227cd248341SJan Glauber data = data << ((8 - len) * 8); 228cd248341SJan Glauber data = le64_to_cpu(data); 229cd248341SJan Glauber *val = (u32) data; 230b170bad4SSebastian Ott } else 231cd248341SJan Glauber *val = 0xffffffff; 232cd248341SJan Glauber return rc; 233cd248341SJan Glauber } 234cd248341SJan Glauber 235cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 236cd248341SJan Glauber { 237cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 238cd248341SJan Glauber u64 data = val; 239cd248341SJan Glauber int rc; 240cd248341SJan Glauber 241cd248341SJan Glauber data = cpu_to_le64(data); 242cd248341SJan Glauber data = data >> ((8 - len) * 8); 2439389339fSMartin Schwidefsky rc = zpci_store(data, req, offset); 244cd248341SJan Glauber return rc; 245cd248341SJan Glauber } 246cd248341SJan Glauber 2471f44a225SMartin Schwidefsky static int zpci_msi_set_mask_bits(struct msi_desc *msi, u32 mask, u32 flag) 2489a4da8a5SJan Glauber { 2491f44a225SMartin Schwidefsky int offset, pos; 2501f44a225SMartin Schwidefsky u32 mask_bits; 2511f44a225SMartin Schwidefsky 2521f44a225SMartin Schwidefsky if (msi->msi_attrib.is_msix) { 2531f44a225SMartin Schwidefsky offset = msi->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE + 2541f44a225SMartin Schwidefsky PCI_MSIX_ENTRY_VECTOR_CTRL; 2551f44a225SMartin Schwidefsky msi->masked = readl(msi->mask_base + offset); 2561f44a225SMartin Schwidefsky writel(flag, msi->mask_base + offset); 2571f44a225SMartin Schwidefsky } else if (msi->msi_attrib.maskbit) { 2581f44a225SMartin Schwidefsky pos = (long) msi->mask_base; 2591f44a225SMartin Schwidefsky pci_read_config_dword(msi->dev, pos, &mask_bits); 2601f44a225SMartin Schwidefsky mask_bits &= ~(mask); 2611f44a225SMartin Schwidefsky mask_bits |= flag & mask; 2621f44a225SMartin Schwidefsky pci_write_config_dword(msi->dev, pos, mask_bits); 2631f44a225SMartin Schwidefsky } else 2641f44a225SMartin Schwidefsky return 0; 2651f44a225SMartin Schwidefsky 2661f44a225SMartin Schwidefsky msi->msi_attrib.maskbit = !!flag; 2671f44a225SMartin Schwidefsky return 1; 2681f44a225SMartin Schwidefsky } 2691f44a225SMartin Schwidefsky 2701f44a225SMartin Schwidefsky static void zpci_enable_irq(struct irq_data *data) 2711f44a225SMartin Schwidefsky { 2721f44a225SMartin Schwidefsky struct msi_desc *msi = irq_get_msi_desc(data->irq); 2739a4da8a5SJan Glauber 2749a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 0); 2759a4da8a5SJan Glauber } 2769a4da8a5SJan Glauber 2771f44a225SMartin Schwidefsky static void zpci_disable_irq(struct irq_data *data) 2789a4da8a5SJan Glauber { 2791f44a225SMartin Schwidefsky struct msi_desc *msi = irq_get_msi_desc(data->irq); 2809a4da8a5SJan Glauber 2819a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 1); 2829a4da8a5SJan Glauber } 2839a4da8a5SJan Glauber 284b881bc46SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus) 285cd248341SJan Glauber { 286cd248341SJan Glauber } 287cd248341SJan Glauber 288cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 289cd248341SJan Glauber resource_size_t size, 290cd248341SJan Glauber resource_size_t align) 291cd248341SJan Glauber { 292cd248341SJan Glauber return 0; 293cd248341SJan Glauber } 294cd248341SJan Glauber 29587bc359bSJan Glauber /* combine single writes by using store-block insn */ 29687bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 29787bc359bSJan Glauber { 29887bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 29987bc359bSJan Glauber } 30087bc359bSJan Glauber 301cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 302cd248341SJan Glauber void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max) 303cd248341SJan Glauber { 304cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 305cd248341SJan Glauber u64 addr; 306cd248341SJan Glauber int idx; 307cd248341SJan Glauber 308cd248341SJan Glauber if ((bar & 7) != bar) 309cd248341SJan Glauber return NULL; 310cd248341SJan Glauber 311cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 312cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 313cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 314cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 315cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 316cd248341SJan Glauber 317cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48); 318cd248341SJan Glauber return (void __iomem *) addr; 319cd248341SJan Glauber } 320cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iomap); 321cd248341SJan Glauber 322cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 323cd248341SJan Glauber { 324cd248341SJan Glauber unsigned int idx; 325cd248341SJan Glauber 326cd248341SJan Glauber idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48; 327cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 328cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 329cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 330cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 331cd248341SJan Glauber } 332cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iounmap); 333cd248341SJan Glauber 334cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 335cd248341SJan Glauber int size, u32 *val) 336cd248341SJan Glauber { 337cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3382c3700bbSSebastian Ott int ret; 339cd248341SJan Glauber 340cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3412c3700bbSSebastian Ott ret = -ENODEV; 3422c3700bbSSebastian Ott else 3432c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3442c3700bbSSebastian Ott 3452c3700bbSSebastian Ott return ret; 346cd248341SJan Glauber } 347cd248341SJan Glauber 348cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 349cd248341SJan Glauber int size, u32 val) 350cd248341SJan Glauber { 351cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3522c3700bbSSebastian Ott int ret; 353cd248341SJan Glauber 354cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3552c3700bbSSebastian Ott ret = -ENODEV; 3562c3700bbSSebastian Ott else 3572c3700bbSSebastian Ott ret = zpci_cfg_store(zdev, where, val, size); 3582c3700bbSSebastian Ott 3592c3700bbSSebastian Ott return ret; 360cd248341SJan Glauber } 361cd248341SJan Glauber 362cd248341SJan Glauber static struct pci_ops pci_root_ops = { 363cd248341SJan Glauber .read = pci_read, 364cd248341SJan Glauber .write = pci_write, 365cd248341SJan Glauber }; 366cd248341SJan Glauber 367f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq) 3689a4da8a5SJan Glauber { 3695d0d8f43SMartin Schwidefsky unsigned long si, ai; 3701f44a225SMartin Schwidefsky struct airq_iv *aibv; 3715d0d8f43SMartin Schwidefsky int irqs_on = 0; 3729a4da8a5SJan Glauber 373420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 3745d0d8f43SMartin Schwidefsky for (si = 0;;) { 3755d0d8f43SMartin Schwidefsky /* Scan adapter summary indicator bit vector */ 3765d0d8f43SMartin Schwidefsky si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv)); 3775d0d8f43SMartin Schwidefsky if (si == -1UL) { 3785d0d8f43SMartin Schwidefsky if (irqs_on++) 3795d0d8f43SMartin Schwidefsky /* End of second scan with interrupts on. */ 3805d0d8f43SMartin Schwidefsky break; 3815d0d8f43SMartin Schwidefsky /* First scan complete, reenable interrupts. */ 3829389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 3835d0d8f43SMartin Schwidefsky si = 0; 3845d0d8f43SMartin Schwidefsky continue; 3859a4da8a5SJan Glauber } 3865d0d8f43SMartin Schwidefsky 3875d0d8f43SMartin Schwidefsky /* Scan the adapter interrupt vector for this device. */ 3881f44a225SMartin Schwidefsky aibv = zpci_aibv[si]; 3895d0d8f43SMartin Schwidefsky for (ai = 0;;) { 3901f44a225SMartin Schwidefsky ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 3915d0d8f43SMartin Schwidefsky if (ai == -1UL) 3925d0d8f43SMartin Schwidefsky break; 3935d0d8f43SMartin Schwidefsky inc_irq_stat(IRQIO_MSI); 3941f44a225SMartin Schwidefsky airq_iv_lock(aibv, ai); 3951f44a225SMartin Schwidefsky generic_handle_irq(airq_iv_get_data(aibv, ai)); 3961f44a225SMartin Schwidefsky airq_iv_unlock(aibv, ai); 3975d0d8f43SMartin Schwidefsky } 3985d0d8f43SMartin Schwidefsky } 3995d0d8f43SMartin Schwidefsky } 4005d0d8f43SMartin Schwidefsky 4015d0d8f43SMartin Schwidefsky int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 4029a4da8a5SJan Glauber { 4039a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 4041f44a225SMartin Schwidefsky unsigned int hwirq, irq, msi_vecs; 4055d0d8f43SMartin Schwidefsky unsigned long aisb; 4069a4da8a5SJan Glauber struct msi_desc *msi; 4071f44a225SMartin Schwidefsky struct msi_msg msg; 4089a4da8a5SJan Glauber int rc; 4099a4da8a5SJan Glauber 4105d0d8f43SMartin Schwidefsky if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI) 4115d0d8f43SMartin Schwidefsky return -EINVAL; 4125d0d8f43SMartin Schwidefsky msi_vecs = min(nvec, ZPCI_MSI_VEC_MAX); 4131f44a225SMartin Schwidefsky msi_vecs = min_t(unsigned int, msi_vecs, CONFIG_PCI_NR_MSI); 4149a4da8a5SJan Glauber 4155d0d8f43SMartin Schwidefsky /* Allocate adapter summary indicator bit */ 4165d0d8f43SMartin Schwidefsky rc = -EIO; 4175d0d8f43SMartin Schwidefsky aisb = airq_iv_alloc_bit(zpci_aisb_iv); 4185d0d8f43SMartin Schwidefsky if (aisb == -1UL) 4195d0d8f43SMartin Schwidefsky goto out; 4209a4da8a5SJan Glauber zdev->aisb = aisb; 4219a4da8a5SJan Glauber 4225d0d8f43SMartin Schwidefsky /* Create adapter interrupt vector */ 4235d0d8f43SMartin Schwidefsky rc = -ENOMEM; 4241f44a225SMartin Schwidefsky zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK); 4255d0d8f43SMartin Schwidefsky if (!zdev->aibv) 4265d0d8f43SMartin Schwidefsky goto out_si; 4279a4da8a5SJan Glauber 4285d0d8f43SMartin Schwidefsky /* Wire up shortcut pointer */ 4291f44a225SMartin Schwidefsky zpci_aibv[aisb] = zdev->aibv; 4305d0d8f43SMartin Schwidefsky 4311f44a225SMartin Schwidefsky /* Request MSI interrupts */ 4321f44a225SMartin Schwidefsky hwirq = 0; 4339a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) { 4341f44a225SMartin Schwidefsky rc = -EIO; 4351f44a225SMartin Schwidefsky irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 4361f44a225SMartin Schwidefsky if (irq == NO_IRQ) 4371f44a225SMartin Schwidefsky goto out_msi; 4381f44a225SMartin Schwidefsky rc = irq_set_msi_desc(irq, msi); 4399a4da8a5SJan Glauber if (rc) 4405d0d8f43SMartin Schwidefsky goto out_msi; 4411f44a225SMartin Schwidefsky irq_set_chip_and_handler(irq, &zpci_irq_chip, 4421f44a225SMartin Schwidefsky handle_simple_irq); 4431f44a225SMartin Schwidefsky msg.data = hwirq; 4441f44a225SMartin Schwidefsky msg.address_lo = zdev->msi_addr & 0xffffffff; 4451f44a225SMartin Schwidefsky msg.address_hi = zdev->msi_addr >> 32; 4461f44a225SMartin Schwidefsky write_msi_msg(irq, &msg); 4471f44a225SMartin Schwidefsky airq_iv_set_data(zdev->aibv, hwirq, irq); 4481f44a225SMartin Schwidefsky hwirq++; 4499a4da8a5SJan Glauber } 4509a4da8a5SJan Glauber 4515d0d8f43SMartin Schwidefsky /* Enable adapter interrupts */ 4525d0d8f43SMartin Schwidefsky rc = zpci_set_airq(zdev); 4535d0d8f43SMartin Schwidefsky if (rc) 4545d0d8f43SMartin Schwidefsky goto out_msi; 4555d0d8f43SMartin Schwidefsky 4565d0d8f43SMartin Schwidefsky return (msi_vecs == nvec) ? 0 : msi_vecs; 4575d0d8f43SMartin Schwidefsky 4585d0d8f43SMartin Schwidefsky out_msi: 4595d0d8f43SMartin Schwidefsky list_for_each_entry(msi, &pdev->msi_list, list) { 4601f44a225SMartin Schwidefsky if (hwirq-- == 0) 4615d0d8f43SMartin Schwidefsky break; 4621f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4631f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4641f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4651f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4661f44a225SMartin Schwidefsky msi->msg.data = 0; 4671f44a225SMartin Schwidefsky msi->irq = 0; 4685d0d8f43SMartin Schwidefsky } 4691f44a225SMartin Schwidefsky zpci_aibv[aisb] = NULL; 4705d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 4715d0d8f43SMartin Schwidefsky out_si: 4725d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, aisb); 4735d0d8f43SMartin Schwidefsky out: 4749a4da8a5SJan Glauber return rc; 4759a4da8a5SJan Glauber } 4769a4da8a5SJan Glauber 4775d0d8f43SMartin Schwidefsky void arch_teardown_msi_irqs(struct pci_dev *pdev) 4789a4da8a5SJan Glauber { 4799a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 4809a4da8a5SJan Glauber struct msi_desc *msi; 4815d0d8f43SMartin Schwidefsky int rc; 4829a4da8a5SJan Glauber 4835d0d8f43SMartin Schwidefsky /* Disable adapter interrupts */ 4845d0d8f43SMartin Schwidefsky rc = zpci_clear_airq(zdev); 4851f1dcbd4SSebastian Ott if (rc) 4869a4da8a5SJan Glauber return; 4879a4da8a5SJan Glauber 4881f44a225SMartin Schwidefsky /* Release MSI interrupts */ 4891f44a225SMartin Schwidefsky list_for_each_entry(msi, &pdev->msi_list, list) { 4901f44a225SMartin Schwidefsky zpci_msi_set_mask_bits(msi, 1, 1); 4911f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4921f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4931f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4941f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4951f44a225SMartin Schwidefsky msi->msg.data = 0; 4961f44a225SMartin Schwidefsky msi->irq = 0; 4971f44a225SMartin Schwidefsky } 4989a4da8a5SJan Glauber 4991f44a225SMartin Schwidefsky zpci_aibv[zdev->aisb] = NULL; 5005d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 5015d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 5029a4da8a5SJan Glauber } 5039a4da8a5SJan Glauber 504cd248341SJan Glauber static void zpci_map_resources(struct zpci_dev *zdev) 505cd248341SJan Glauber { 506cd248341SJan Glauber struct pci_dev *pdev = zdev->pdev; 507cd248341SJan Glauber resource_size_t len; 508cd248341SJan Glauber int i; 509cd248341SJan Glauber 510cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 511cd248341SJan Glauber len = pci_resource_len(pdev, i); 512cd248341SJan Glauber if (!len) 513cd248341SJan Glauber continue; 514cd248341SJan Glauber pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0); 515cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 516cd248341SJan Glauber } 517944239c5SSebastian Ott } 518944239c5SSebastian Ott 519944239c5SSebastian Ott static void zpci_unmap_resources(struct zpci_dev *zdev) 520944239c5SSebastian Ott { 521944239c5SSebastian Ott struct pci_dev *pdev = zdev->pdev; 522944239c5SSebastian Ott resource_size_t len; 523944239c5SSebastian Ott int i; 524944239c5SSebastian Ott 525944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 526944239c5SSebastian Ott len = pci_resource_len(pdev, i); 527944239c5SSebastian Ott if (!len) 528944239c5SSebastian Ott continue; 529944239c5SSebastian Ott pci_iounmap(pdev, (void *) pdev->resource[i].start); 530944239c5SSebastian Ott } 531944239c5SSebastian Ott } 532cd248341SJan Glauber 5331e8da956SJan Glauber int pcibios_add_platform_entries(struct pci_dev *pdev) 5341e8da956SJan Glauber { 5351e8da956SJan Glauber return zpci_sysfs_add_device(&pdev->dev); 5361e8da956SJan Glauber } 5371e8da956SJan Glauber 5389a4da8a5SJan Glauber static int __init zpci_irq_init(void) 5399a4da8a5SJan Glauber { 5405d0d8f43SMartin Schwidefsky int rc; 5419a4da8a5SJan Glauber 542f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 543f4eae94fSMartin Schwidefsky if (rc) 5445d0d8f43SMartin Schwidefsky goto out; 545f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 546f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 5479a4da8a5SJan Glauber 5485d0d8f43SMartin Schwidefsky rc = -ENOMEM; 5495d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 5505d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 5515d0d8f43SMartin Schwidefsky goto out_airq; 5529a4da8a5SJan Glauber 5539389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 5549a4da8a5SJan Glauber return 0; 5559a4da8a5SJan Glauber 5565d0d8f43SMartin Schwidefsky out_airq: 5575d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5585d0d8f43SMartin Schwidefsky out: 5599a4da8a5SJan Glauber return rc; 5609a4da8a5SJan Glauber } 5619a4da8a5SJan Glauber 5629a4da8a5SJan Glauber static void zpci_irq_exit(void) 5639a4da8a5SJan Glauber { 5645d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 565f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5669a4da8a5SJan Glauber } 5679a4da8a5SJan Glauber 568cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 569cd248341SJan Glauber { 570cd248341SJan Glauber int entry; 571cd248341SJan Glauber 572cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 573cd248341SJan Glauber entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 574cd248341SJan Glauber if (entry == ZPCI_IOMAP_MAX_ENTRIES) { 575cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 576cd248341SJan Glauber return -ENOSPC; 577cd248341SJan Glauber } 578cd248341SJan Glauber set_bit(entry, zpci_iomap); 579cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 580cd248341SJan Glauber return entry; 581cd248341SJan Glauber } 582cd248341SJan Glauber 583cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 584cd248341SJan Glauber { 585cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 586cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 587cd248341SJan Glauber clear_bit(entry, zpci_iomap); 588cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 589cd248341SJan Glauber } 590cd248341SJan Glauber 5917a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 5927a572a3aSSebastian Ott unsigned long size, unsigned long flags) 5937a572a3aSSebastian Ott { 5947a572a3aSSebastian Ott struct resource *r; 5957a572a3aSSebastian Ott 5967a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 5977a572a3aSSebastian Ott if (!r) 5987a572a3aSSebastian Ott return NULL; 5997a572a3aSSebastian Ott 6007a572a3aSSebastian Ott r->start = start; 6017a572a3aSSebastian Ott r->end = r->start + size - 1; 6027a572a3aSSebastian Ott r->flags = flags; 6037a572a3aSSebastian Ott r->name = zdev->res_name; 6047a572a3aSSebastian Ott 6057a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 6067a572a3aSSebastian Ott kfree(r); 6077a572a3aSSebastian Ott return NULL; 6087a572a3aSSebastian Ott } 6097a572a3aSSebastian Ott return r; 6107a572a3aSSebastian Ott } 6117a572a3aSSebastian Ott 6127a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 6137a572a3aSSebastian Ott struct list_head *resources) 6147a572a3aSSebastian Ott { 6157a572a3aSSebastian Ott unsigned long addr, size, flags; 6167a572a3aSSebastian Ott struct resource *res; 6177a572a3aSSebastian Ott int i, entry; 6187a572a3aSSebastian Ott 6197a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 6207a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 6217a572a3aSSebastian Ott 6227a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6237a572a3aSSebastian Ott if (!zdev->bars[i].size) 6247a572a3aSSebastian Ott continue; 6257a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 6267a572a3aSSebastian Ott if (entry < 0) 6277a572a3aSSebastian Ott return entry; 6287a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 6297a572a3aSSebastian Ott 6307a572a3aSSebastian Ott /* only MMIO is supported */ 6317a572a3aSSebastian Ott flags = IORESOURCE_MEM; 6327a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 6337a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 6347a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 6357a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 6367a572a3aSSebastian Ott 6377a572a3aSSebastian Ott addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48); 6387a572a3aSSebastian Ott 6397a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 6407a572a3aSSebastian Ott 6417a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 6427a572a3aSSebastian Ott if (!res) { 6437a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 6447a572a3aSSebastian Ott return -ENOMEM; 6457a572a3aSSebastian Ott } 6467a572a3aSSebastian Ott zdev->bars[i].res = res; 6477a572a3aSSebastian Ott pci_add_resource(resources, res); 6487a572a3aSSebastian Ott } 6497a572a3aSSebastian Ott 6507a572a3aSSebastian Ott return 0; 6517a572a3aSSebastian Ott } 6527a572a3aSSebastian Ott 6537a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 6547a572a3aSSebastian Ott { 6557a572a3aSSebastian Ott int i; 6567a572a3aSSebastian Ott 6577a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6587a572a3aSSebastian Ott if (!zdev->bars[i].size) 6597a572a3aSSebastian Ott continue; 6607a572a3aSSebastian Ott 6617a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 6627a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 6637a572a3aSSebastian Ott kfree(zdev->bars[i].res); 6647a572a3aSSebastian Ott } 6657a572a3aSSebastian Ott } 6667a572a3aSSebastian Ott 667af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 668af0a8a84SSebastian Ott { 669af0a8a84SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 670cb809182SSebastian Ott struct resource *res; 671cb809182SSebastian Ott int i; 672cb809182SSebastian Ott 673cb809182SSebastian Ott zdev->pdev = pdev; 674cb809182SSebastian Ott zpci_map_resources(zdev); 675cb809182SSebastian Ott 676cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 677cb809182SSebastian Ott res = &pdev->resource[i]; 678cb809182SSebastian Ott if (res->parent || !res->flags) 679cb809182SSebastian Ott continue; 680cb809182SSebastian Ott pci_claim_resource(pdev, i); 681cb809182SSebastian Ott } 682cb809182SSebastian Ott 683cb809182SSebastian Ott return 0; 684cb809182SSebastian Ott } 685cb809182SSebastian Ott 686cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 687cb809182SSebastian Ott { 688cb809182SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 689cb809182SSebastian Ott struct resource *res; 690cb809182SSebastian Ott u16 cmd; 691cb809182SSebastian Ott int i; 692af0a8a84SSebastian Ott 6931c21351bSSebastian Ott zdev->pdev = pdev; 694af0a8a84SSebastian Ott zpci_debug_init_device(zdev); 695af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 696af0a8a84SSebastian Ott zpci_map_resources(zdev); 697af0a8a84SSebastian Ott 698cb809182SSebastian Ott pci_read_config_word(pdev, PCI_COMMAND, &cmd); 699cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 700cb809182SSebastian Ott res = &pdev->resource[i]; 701cb809182SSebastian Ott 702cb809182SSebastian Ott if (res->flags & IORESOURCE_IO) 703cb809182SSebastian Ott return -EINVAL; 704cb809182SSebastian Ott 705cb809182SSebastian Ott if (res->flags & IORESOURCE_MEM) 706cb809182SSebastian Ott cmd |= PCI_COMMAND_MEMORY; 707cb809182SSebastian Ott } 708cb809182SSebastian Ott pci_write_config_word(pdev, PCI_COMMAND, cmd); 709af0a8a84SSebastian Ott return 0; 710af0a8a84SSebastian Ott } 711af0a8a84SSebastian Ott 712cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 713944239c5SSebastian Ott { 714944239c5SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 715944239c5SSebastian Ott 716944239c5SSebastian Ott zpci_unmap_resources(zdev); 717944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 718944239c5SSebastian Ott zpci_debug_exit_device(zdev); 719944239c5SSebastian Ott zdev->pdev = NULL; 720944239c5SSebastian Ott } 721944239c5SSebastian Ott 72269db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 72369db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 72469db3b5eSSebastian Ott { 72569db3b5eSSebastian Ott struct zpci_dev *zdev = get_zdev(to_pci_dev(dev)); 72669db3b5eSSebastian Ott int ret = 0; 72769db3b5eSSebastian Ott 72869db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 72969db3b5eSSebastian Ott goto out; 73069db3b5eSSebastian Ott 73169db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 73269db3b5eSSebastian Ott if (ret) 73369db3b5eSSebastian Ott goto out; 73469db3b5eSSebastian Ott 73569db3b5eSSebastian Ott zpci_map_resources(zdev); 73669db3b5eSSebastian Ott zpci_register_ioat(zdev, 0, zdev->start_dma + PAGE_OFFSET, 73769db3b5eSSebastian Ott zdev->start_dma + zdev->iommu_size - 1, 73869db3b5eSSebastian Ott (u64) zdev->dma_table); 73969db3b5eSSebastian Ott 74069db3b5eSSebastian Ott out: 74169db3b5eSSebastian Ott return ret; 74269db3b5eSSebastian Ott } 74369db3b5eSSebastian Ott 74469db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 74569db3b5eSSebastian Ott { 74669db3b5eSSebastian Ott struct zpci_dev *zdev = get_zdev(to_pci_dev(dev)); 74769db3b5eSSebastian Ott 74869db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 74969db3b5eSSebastian Ott return 0; 75069db3b5eSSebastian Ott 75169db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 75269db3b5eSSebastian Ott return clp_disable_fh(zdev); 75369db3b5eSSebastian Ott } 75469db3b5eSSebastian Ott 75569db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 75669db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 75769db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 75869db3b5eSSebastian Ott .restore_noirq = zpci_restore, 75969db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 76069db3b5eSSebastian Ott }; 76169db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 76269db3b5eSSebastian Ott 763cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 764cd248341SJan Glauber { 765cd248341SJan Glauber spin_lock(&zpci_domain_lock); 766cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 767cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 768cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 769cd248341SJan Glauber return -ENOSPC; 770cd248341SJan Glauber } 771cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 772cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 773cd248341SJan Glauber return 0; 774cd248341SJan Glauber } 775cd248341SJan Glauber 776cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 777cd248341SJan Glauber { 778cd248341SJan Glauber spin_lock(&zpci_domain_lock); 779cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 780cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 781cd248341SJan Glauber } 782cd248341SJan Glauber 7837d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus) 7847d594322SSebastian Ott { 7857d594322SSebastian Ott struct zpci_dev *zdev = get_zdev_by_bus(bus); 7867d594322SSebastian Ott 7877d594322SSebastian Ott zpci_exit_slot(zdev); 7887d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 7897d594322SSebastian Ott zpci_free_domain(zdev); 7907d594322SSebastian Ott 7917d594322SSebastian Ott spin_lock(&zpci_list_lock); 7927d594322SSebastian Ott list_del(&zdev->entry); 7937d594322SSebastian Ott spin_unlock(&zpci_list_lock); 7947d594322SSebastian Ott 7957d594322SSebastian Ott kfree(zdev); 7967d594322SSebastian Ott } 7977d594322SSebastian Ott 7987d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 7997d594322SSebastian Ott { 8007d594322SSebastian Ott LIST_HEAD(resources); 8017d594322SSebastian Ott int ret; 8027d594322SSebastian Ott 8037d594322SSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 8047d594322SSebastian Ott if (ret) 8057d594322SSebastian Ott return ret; 8067d594322SSebastian Ott 8077d594322SSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 8087d594322SSebastian Ott zdev, &resources); 8097d594322SSebastian Ott if (!zdev->bus) { 8107d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 8117d594322SSebastian Ott return -EIO; 8127d594322SSebastian Ott } 8137d594322SSebastian Ott 8147d594322SSebastian Ott zdev->bus->max_bus_speed = zdev->max_bus_speed; 8157d594322SSebastian Ott return 0; 8167d594322SSebastian Ott } 8177d594322SSebastian Ott 818a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 819a755a45dSJan Glauber { 820a755a45dSJan Glauber int rc; 821a755a45dSJan Glauber 822a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 823a755a45dSJan Glauber if (rc) 824a755a45dSJan Glauber goto out; 825828b35f6SJan Glauber 826828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 827828b35f6SJan Glauber if (rc) 828828b35f6SJan Glauber goto out_dma; 8290ff70ec8SSebastian Ott 8300ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 831a755a45dSJan Glauber return 0; 832828b35f6SJan Glauber 833828b35f6SJan Glauber out_dma: 834828b35f6SJan Glauber clp_disable_fh(zdev); 835a755a45dSJan Glauber out: 836a755a45dSJan Glauber return rc; 837a755a45dSJan Glauber } 838a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 839a755a45dSJan Glauber 840cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 841cb65a669SSebastian Ott { 842cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 843cb65a669SSebastian Ott return clp_disable_fh(zdev); 844cb65a669SSebastian Ott } 845cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 846cb65a669SSebastian Ott 847cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 848cd248341SJan Glauber { 849cd248341SJan Glauber int rc; 850cd248341SJan Glauber 851cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 852cd248341SJan Glauber if (rc) 853cd248341SJan Glauber goto out; 854cd248341SJan Glauber 8551c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8561c21351bSSebastian Ott rc = zpci_enable_device(zdev); 857cd248341SJan Glauber if (rc) 8581c21351bSSebastian Ott goto out_free; 8591c21351bSSebastian Ott } 8601c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8611c21351bSSebastian Ott if (rc) 8621c21351bSSebastian Ott goto out_disable; 863cd248341SJan Glauber 86457b5918cSSebastian Ott spin_lock(&zpci_list_lock); 865cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 86657b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 867cd248341SJan Glauber 86867f43f38SSebastian Ott zpci_init_slot(zdev); 86967f43f38SSebastian Ott 870cd248341SJan Glauber return 0; 871cd248341SJan Glauber 8721c21351bSSebastian Ott out_disable: 8731c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8741c21351bSSebastian Ott zpci_disable_device(zdev); 8751c21351bSSebastian Ott out_free: 876cd248341SJan Glauber zpci_free_domain(zdev); 877cd248341SJan Glauber out: 878cd248341SJan Glauber return rc; 879cd248341SJan Glauber } 880cd248341SJan Glauber 881cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 882cd248341SJan Glauber { 883828b35f6SJan Glauber zpci_dma_exit_device(zdev); 884cd248341SJan Glauber /* 885cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 886cd248341SJan Glauber * do that here. 887cd248341SJan Glauber */ 888cd248341SJan Glauber } 889cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 890cd248341SJan Glauber 891cd248341SJan Glauber static inline int barsize(u8 size) 892cd248341SJan Glauber { 893cd248341SJan Glauber return (size) ? (1 << size) >> 10 : 0; 894cd248341SJan Glauber } 895cd248341SJan Glauber 896cd248341SJan Glauber static int zpci_mem_init(void) 897cd248341SJan Glauber { 898d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 899d0b08853SJan Glauber 16, 0, NULL); 900d0b08853SJan Glauber if (!zdev_fmb_cache) 9011f44a225SMartin Schwidefsky goto error_zdev; 902d0b08853SJan Glauber 903cd248341SJan Glauber /* TODO: use realloc */ 904cd248341SJan Glauber zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start), 905cd248341SJan Glauber GFP_KERNEL); 906cd248341SJan Glauber if (!zpci_iomap_start) 9079a4da8a5SJan Glauber goto error_iomap; 908cd248341SJan Glauber return 0; 909cd248341SJan Glauber 9109a4da8a5SJan Glauber error_iomap: 911d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 912cd248341SJan Glauber error_zdev: 913cd248341SJan Glauber return -ENOMEM; 914cd248341SJan Glauber } 915cd248341SJan Glauber 916cd248341SJan Glauber static void zpci_mem_exit(void) 917cd248341SJan Glauber { 918cd248341SJan Glauber kfree(zpci_iomap_start); 919d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 920cd248341SJan Glauber } 921cd248341SJan Glauber 92267f43f38SSebastian Ott static unsigned int s390_pci_probe; 923*aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 924cd248341SJan Glauber 925cd248341SJan Glauber char * __init pcibios_setup(char *str) 926cd248341SJan Glauber { 92789b0dc95SSebastian Ott if (!strcmp(str, "on")) { 92889b0dc95SSebastian Ott s390_pci_probe = 1; 929cd248341SJan Glauber return NULL; 930cd248341SJan Glauber } 931cd248341SJan Glauber return str; 932cd248341SJan Glauber } 933cd248341SJan Glauber 934*aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 935*aa3b7c29SSebastian Ott { 936*aa3b7c29SSebastian Ott return s390_pci_initialized; 937*aa3b7c29SSebastian Ott } 938*aa3b7c29SSebastian Ott 939cd248341SJan Glauber static int __init pci_base_init(void) 940cd248341SJan Glauber { 941cd248341SJan Glauber int rc; 942cd248341SJan Glauber 9431e5635d1SHeiko Carstens if (!s390_pci_probe) 944cd248341SJan Glauber return 0; 945cd248341SJan Glauber 946cd248341SJan Glauber if (!test_facility(2) || !test_facility(69) 947cd248341SJan Glauber || !test_facility(71) || !test_facility(72)) 948cd248341SJan Glauber return 0; 949cd248341SJan Glauber 950d0b08853SJan Glauber rc = zpci_debug_init(); 951d0b08853SJan Glauber if (rc) 9521f44a225SMartin Schwidefsky goto out; 953d0b08853SJan Glauber 954cd248341SJan Glauber rc = zpci_mem_init(); 955cd248341SJan Glauber if (rc) 956cd248341SJan Glauber goto out_mem; 957cd248341SJan Glauber 9589a4da8a5SJan Glauber rc = zpci_irq_init(); 9599a4da8a5SJan Glauber if (rc) 9609a4da8a5SJan Glauber goto out_irq; 9619a4da8a5SJan Glauber 962828b35f6SJan Glauber rc = zpci_dma_init(); 963828b35f6SJan Glauber if (rc) 964828b35f6SJan Glauber goto out_dma; 965828b35f6SJan Glauber 9661d578966SSebastian Ott rc = clp_scan_pci_devices(); 967a755a45dSJan Glauber if (rc) 968a755a45dSJan Glauber goto out_find; 969a755a45dSJan Glauber 970*aa3b7c29SSebastian Ott s390_pci_initialized = 1; 971cd248341SJan Glauber return 0; 972cd248341SJan Glauber 973a755a45dSJan Glauber out_find: 974828b35f6SJan Glauber zpci_dma_exit(); 975828b35f6SJan Glauber out_dma: 9769a4da8a5SJan Glauber zpci_irq_exit(); 9779a4da8a5SJan Glauber out_irq: 978cd248341SJan Glauber zpci_mem_exit(); 979cd248341SJan Glauber out_mem: 980d0b08853SJan Glauber zpci_debug_exit(); 9811f44a225SMartin Schwidefsky out: 982cd248341SJan Glauber return rc; 983cd248341SJan Glauber } 98467f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 98557b5918cSSebastian Ott 98657b5918cSSebastian Ott void zpci_rescan(void) 98757b5918cSSebastian Ott { 988*aa3b7c29SSebastian Ott if (zpci_is_enabled()) 98957b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 99057b5918cSSebastian Ott } 991