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); 49*57b5918cSSebastian 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 91*57b5918cSSebastian 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 } 98*57b5918cSSebastian 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); 1239a4da8a5SJan Glauber struct zpci_fib *fib; 1249a4da8a5SJan Glauber int rc; 1259a4da8a5SJan Glauber 1269a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 1279a4da8a5SJan Glauber if (!fib) 1289a4da8a5SJan Glauber return -ENOMEM; 1299a4da8a5SJan Glauber 1309a4da8a5SJan Glauber fib->isc = PCI_ISC; 1319a4da8a5SJan Glauber fib->sum = 1; /* enable summary notifications */ 1325d0d8f43SMartin Schwidefsky fib->noi = airq_iv_end(zdev->aibv); 1335d0d8f43SMartin Schwidefsky fib->aibv = (unsigned long) zdev->aibv->vector; 1345d0d8f43SMartin Schwidefsky fib->aibvo = 0; /* each zdev has its own interrupt vector */ 1355d0d8f43SMartin Schwidefsky fib->aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8; 1365d0d8f43SMartin Schwidefsky fib->aisbo = zdev->aisb & 63; 1379a4da8a5SJan Glauber 1389389339fSMartin Schwidefsky rc = zpci_mod_fc(req, fib); 1399a4da8a5SJan Glauber pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi); 1409a4da8a5SJan Glauber 1419a4da8a5SJan Glauber free_page((unsigned long) fib); 1429a4da8a5SJan Glauber return rc; 1439a4da8a5SJan Glauber } 1449a4da8a5SJan Glauber 1459a4da8a5SJan Glauber struct mod_pci_args { 1469a4da8a5SJan Glauber u64 base; 1479a4da8a5SJan Glauber u64 limit; 1489a4da8a5SJan Glauber u64 iota; 149d0b08853SJan Glauber u64 fmb_addr; 1509a4da8a5SJan Glauber }; 1519a4da8a5SJan Glauber 1529a4da8a5SJan Glauber static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) 1539a4da8a5SJan Glauber { 1549a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); 1559a4da8a5SJan Glauber struct zpci_fib *fib; 1569a4da8a5SJan Glauber int rc; 1579a4da8a5SJan Glauber 1589a4da8a5SJan Glauber /* The FIB must be available even if it's not used */ 1599a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 1609a4da8a5SJan Glauber if (!fib) 1619a4da8a5SJan Glauber return -ENOMEM; 1629a4da8a5SJan Glauber 1639a4da8a5SJan Glauber fib->pba = args->base; 1649a4da8a5SJan Glauber fib->pal = args->limit; 1659a4da8a5SJan Glauber fib->iota = args->iota; 166d0b08853SJan Glauber fib->fmb_addr = args->fmb_addr; 1679a4da8a5SJan Glauber 1689389339fSMartin Schwidefsky rc = zpci_mod_fc(req, fib); 1699a4da8a5SJan Glauber free_page((unsigned long) fib); 1709a4da8a5SJan Glauber return rc; 1719a4da8a5SJan Glauber } 1729a4da8a5SJan Glauber 173828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 174828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 175828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 176828b35f6SJan Glauber { 177d0b08853SJan Glauber struct mod_pci_args args = { base, limit, iota, 0 }; 178828b35f6SJan Glauber 179828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 180828b35f6SJan Glauber args.iota |= ZPCI_IOTA_RTTO_FLAG; 181828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); 182828b35f6SJan Glauber } 183828b35f6SJan Glauber 184828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 185828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 186828b35f6SJan Glauber { 187d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 188828b35f6SJan Glauber 189828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); 190828b35f6SJan Glauber } 191828b35f6SJan Glauber 1929a4da8a5SJan Glauber /* Modify PCI: Unregister adapter interruptions */ 1935d0d8f43SMartin Schwidefsky static int zpci_clear_airq(struct zpci_dev *zdev) 1949a4da8a5SJan Glauber { 195d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 1969a4da8a5SJan Glauber 1979a4da8a5SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); 1989a4da8a5SJan Glauber } 1999a4da8a5SJan Glauber 200d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 201d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 202d0b08853SJan Glauber { 203d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 204d0b08853SJan Glauber 205d0b08853SJan Glauber if (zdev->fmb) 206d0b08853SJan Glauber return -EINVAL; 207d0b08853SJan Glauber 20808b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 209d0b08853SJan Glauber if (!zdev->fmb) 210d0b08853SJan Glauber return -ENOMEM; 211d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 212d0b08853SJan Glauber 213d0b08853SJan Glauber args.fmb_addr = virt_to_phys(zdev->fmb); 214d0b08853SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 215d0b08853SJan Glauber } 216d0b08853SJan Glauber 217d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 218d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 219d0b08853SJan Glauber { 220d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 221d0b08853SJan Glauber int rc; 222d0b08853SJan Glauber 223d0b08853SJan Glauber if (!zdev->fmb) 224d0b08853SJan Glauber return -EINVAL; 225d0b08853SJan Glauber 226d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 227d0b08853SJan Glauber rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 228d0b08853SJan Glauber 229d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 230d0b08853SJan Glauber zdev->fmb = NULL; 231d0b08853SJan Glauber return rc; 232d0b08853SJan Glauber } 233d0b08853SJan Glauber 234cd248341SJan Glauber #define ZPCI_PCIAS_CFGSPC 15 235cd248341SJan Glauber 236cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 237cd248341SJan Glauber { 238cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 239cd248341SJan Glauber u64 data; 240cd248341SJan Glauber int rc; 241cd248341SJan Glauber 2429389339fSMartin Schwidefsky rc = zpci_load(&data, req, offset); 243b170bad4SSebastian Ott if (!rc) { 244cd248341SJan Glauber data = data << ((8 - len) * 8); 245cd248341SJan Glauber data = le64_to_cpu(data); 246cd248341SJan Glauber *val = (u32) data; 247b170bad4SSebastian Ott } else 248cd248341SJan Glauber *val = 0xffffffff; 249cd248341SJan Glauber return rc; 250cd248341SJan Glauber } 251cd248341SJan Glauber 252cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 253cd248341SJan Glauber { 254cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 255cd248341SJan Glauber u64 data = val; 256cd248341SJan Glauber int rc; 257cd248341SJan Glauber 258cd248341SJan Glauber data = cpu_to_le64(data); 259cd248341SJan Glauber data = data >> ((8 - len) * 8); 2609389339fSMartin Schwidefsky rc = zpci_store(data, req, offset); 261cd248341SJan Glauber return rc; 262cd248341SJan Glauber } 263cd248341SJan Glauber 2641f44a225SMartin Schwidefsky static int zpci_msi_set_mask_bits(struct msi_desc *msi, u32 mask, u32 flag) 2659a4da8a5SJan Glauber { 2661f44a225SMartin Schwidefsky int offset, pos; 2671f44a225SMartin Schwidefsky u32 mask_bits; 2681f44a225SMartin Schwidefsky 2691f44a225SMartin Schwidefsky if (msi->msi_attrib.is_msix) { 2701f44a225SMartin Schwidefsky offset = msi->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE + 2711f44a225SMartin Schwidefsky PCI_MSIX_ENTRY_VECTOR_CTRL; 2721f44a225SMartin Schwidefsky msi->masked = readl(msi->mask_base + offset); 2731f44a225SMartin Schwidefsky writel(flag, msi->mask_base + offset); 2741f44a225SMartin Schwidefsky } else if (msi->msi_attrib.maskbit) { 2751f44a225SMartin Schwidefsky pos = (long) msi->mask_base; 2761f44a225SMartin Schwidefsky pci_read_config_dword(msi->dev, pos, &mask_bits); 2771f44a225SMartin Schwidefsky mask_bits &= ~(mask); 2781f44a225SMartin Schwidefsky mask_bits |= flag & mask; 2791f44a225SMartin Schwidefsky pci_write_config_dword(msi->dev, pos, mask_bits); 2801f44a225SMartin Schwidefsky } else 2811f44a225SMartin Schwidefsky return 0; 2821f44a225SMartin Schwidefsky 2831f44a225SMartin Schwidefsky msi->msi_attrib.maskbit = !!flag; 2841f44a225SMartin Schwidefsky return 1; 2851f44a225SMartin Schwidefsky } 2861f44a225SMartin Schwidefsky 2871f44a225SMartin Schwidefsky static void zpci_enable_irq(struct irq_data *data) 2881f44a225SMartin Schwidefsky { 2891f44a225SMartin Schwidefsky struct msi_desc *msi = irq_get_msi_desc(data->irq); 2909a4da8a5SJan Glauber 2919a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 0); 2929a4da8a5SJan Glauber } 2939a4da8a5SJan Glauber 2941f44a225SMartin Schwidefsky static void zpci_disable_irq(struct irq_data *data) 2959a4da8a5SJan Glauber { 2961f44a225SMartin Schwidefsky struct msi_desc *msi = irq_get_msi_desc(data->irq); 2979a4da8a5SJan Glauber 2989a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 1); 2999a4da8a5SJan Glauber } 3009a4da8a5SJan Glauber 301b881bc46SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus) 302cd248341SJan Glauber { 303cd248341SJan Glauber } 304cd248341SJan Glauber 305cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 306cd248341SJan Glauber resource_size_t size, 307cd248341SJan Glauber resource_size_t align) 308cd248341SJan Glauber { 309cd248341SJan Glauber return 0; 310cd248341SJan Glauber } 311cd248341SJan Glauber 31287bc359bSJan Glauber /* combine single writes by using store-block insn */ 31387bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 31487bc359bSJan Glauber { 31587bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 31687bc359bSJan Glauber } 31787bc359bSJan Glauber 318cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 319cd248341SJan Glauber void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max) 320cd248341SJan Glauber { 321cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 322cd248341SJan Glauber u64 addr; 323cd248341SJan Glauber int idx; 324cd248341SJan Glauber 325cd248341SJan Glauber if ((bar & 7) != bar) 326cd248341SJan Glauber return NULL; 327cd248341SJan Glauber 328cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 329cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 330cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 331cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 332cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 333cd248341SJan Glauber 334cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48); 335cd248341SJan Glauber return (void __iomem *) addr; 336cd248341SJan Glauber } 337cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iomap); 338cd248341SJan Glauber 339cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 340cd248341SJan Glauber { 341cd248341SJan Glauber unsigned int idx; 342cd248341SJan Glauber 343cd248341SJan Glauber idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48; 344cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 345cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 346cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 347cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 348cd248341SJan Glauber } 349cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iounmap); 350cd248341SJan Glauber 351cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 352cd248341SJan Glauber int size, u32 *val) 353cd248341SJan Glauber { 354cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3552c3700bbSSebastian Ott int ret; 356cd248341SJan Glauber 357cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3582c3700bbSSebastian Ott ret = -ENODEV; 3592c3700bbSSebastian Ott else 3602c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3612c3700bbSSebastian Ott 3622c3700bbSSebastian Ott return ret; 363cd248341SJan Glauber } 364cd248341SJan Glauber 365cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 366cd248341SJan Glauber int size, u32 val) 367cd248341SJan Glauber { 368cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3692c3700bbSSebastian Ott int ret; 370cd248341SJan Glauber 371cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3722c3700bbSSebastian Ott ret = -ENODEV; 3732c3700bbSSebastian Ott else 3742c3700bbSSebastian Ott ret = zpci_cfg_store(zdev, where, val, size); 3752c3700bbSSebastian Ott 3762c3700bbSSebastian Ott return ret; 377cd248341SJan Glauber } 378cd248341SJan Glauber 379cd248341SJan Glauber static struct pci_ops pci_root_ops = { 380cd248341SJan Glauber .read = pci_read, 381cd248341SJan Glauber .write = pci_write, 382cd248341SJan Glauber }; 383cd248341SJan Glauber 384f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq) 3859a4da8a5SJan Glauber { 3865d0d8f43SMartin Schwidefsky unsigned long si, ai; 3871f44a225SMartin Schwidefsky struct airq_iv *aibv; 3885d0d8f43SMartin Schwidefsky int irqs_on = 0; 3899a4da8a5SJan Glauber 390420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 3915d0d8f43SMartin Schwidefsky for (si = 0;;) { 3925d0d8f43SMartin Schwidefsky /* Scan adapter summary indicator bit vector */ 3935d0d8f43SMartin Schwidefsky si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv)); 3945d0d8f43SMartin Schwidefsky if (si == -1UL) { 3955d0d8f43SMartin Schwidefsky if (irqs_on++) 3965d0d8f43SMartin Schwidefsky /* End of second scan with interrupts on. */ 3975d0d8f43SMartin Schwidefsky break; 3985d0d8f43SMartin Schwidefsky /* First scan complete, reenable interrupts. */ 3999389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 4005d0d8f43SMartin Schwidefsky si = 0; 4015d0d8f43SMartin Schwidefsky continue; 4029a4da8a5SJan Glauber } 4035d0d8f43SMartin Schwidefsky 4045d0d8f43SMartin Schwidefsky /* Scan the adapter interrupt vector for this device. */ 4051f44a225SMartin Schwidefsky aibv = zpci_aibv[si]; 4065d0d8f43SMartin Schwidefsky for (ai = 0;;) { 4071f44a225SMartin Schwidefsky ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 4085d0d8f43SMartin Schwidefsky if (ai == -1UL) 4095d0d8f43SMartin Schwidefsky break; 4105d0d8f43SMartin Schwidefsky inc_irq_stat(IRQIO_MSI); 4111f44a225SMartin Schwidefsky airq_iv_lock(aibv, ai); 4121f44a225SMartin Schwidefsky generic_handle_irq(airq_iv_get_data(aibv, ai)); 4131f44a225SMartin Schwidefsky airq_iv_unlock(aibv, ai); 4145d0d8f43SMartin Schwidefsky } 4155d0d8f43SMartin Schwidefsky } 4165d0d8f43SMartin Schwidefsky } 4175d0d8f43SMartin Schwidefsky 4185d0d8f43SMartin Schwidefsky int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 4199a4da8a5SJan Glauber { 4209a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 4211f44a225SMartin Schwidefsky unsigned int hwirq, irq, msi_vecs; 4225d0d8f43SMartin Schwidefsky unsigned long aisb; 4239a4da8a5SJan Glauber struct msi_desc *msi; 4241f44a225SMartin Schwidefsky struct msi_msg msg; 4259a4da8a5SJan Glauber int rc; 4269a4da8a5SJan Glauber 4275d0d8f43SMartin Schwidefsky pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec); 4285d0d8f43SMartin Schwidefsky if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI) 4295d0d8f43SMartin Schwidefsky return -EINVAL; 4305d0d8f43SMartin Schwidefsky msi_vecs = min(nvec, ZPCI_MSI_VEC_MAX); 4311f44a225SMartin Schwidefsky msi_vecs = min_t(unsigned int, msi_vecs, CONFIG_PCI_NR_MSI); 4329a4da8a5SJan Glauber 4335d0d8f43SMartin Schwidefsky /* Allocate adapter summary indicator bit */ 4345d0d8f43SMartin Schwidefsky rc = -EIO; 4355d0d8f43SMartin Schwidefsky aisb = airq_iv_alloc_bit(zpci_aisb_iv); 4365d0d8f43SMartin Schwidefsky if (aisb == -1UL) 4375d0d8f43SMartin Schwidefsky goto out; 4389a4da8a5SJan Glauber zdev->aisb = aisb; 4399a4da8a5SJan Glauber 4405d0d8f43SMartin Schwidefsky /* Create adapter interrupt vector */ 4415d0d8f43SMartin Schwidefsky rc = -ENOMEM; 4421f44a225SMartin Schwidefsky zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK); 4435d0d8f43SMartin Schwidefsky if (!zdev->aibv) 4445d0d8f43SMartin Schwidefsky goto out_si; 4459a4da8a5SJan Glauber 4465d0d8f43SMartin Schwidefsky /* Wire up shortcut pointer */ 4471f44a225SMartin Schwidefsky zpci_aibv[aisb] = zdev->aibv; 4485d0d8f43SMartin Schwidefsky 4491f44a225SMartin Schwidefsky /* Request MSI interrupts */ 4501f44a225SMartin Schwidefsky hwirq = 0; 4519a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) { 4521f44a225SMartin Schwidefsky rc = -EIO; 4531f44a225SMartin Schwidefsky irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 4541f44a225SMartin Schwidefsky if (irq == NO_IRQ) 4551f44a225SMartin Schwidefsky goto out_msi; 4561f44a225SMartin Schwidefsky rc = irq_set_msi_desc(irq, msi); 4579a4da8a5SJan Glauber if (rc) 4585d0d8f43SMartin Schwidefsky goto out_msi; 4591f44a225SMartin Schwidefsky irq_set_chip_and_handler(irq, &zpci_irq_chip, 4601f44a225SMartin Schwidefsky handle_simple_irq); 4611f44a225SMartin Schwidefsky msg.data = hwirq; 4621f44a225SMartin Schwidefsky msg.address_lo = zdev->msi_addr & 0xffffffff; 4631f44a225SMartin Schwidefsky msg.address_hi = zdev->msi_addr >> 32; 4641f44a225SMartin Schwidefsky write_msi_msg(irq, &msg); 4651f44a225SMartin Schwidefsky airq_iv_set_data(zdev->aibv, hwirq, irq); 4661f44a225SMartin Schwidefsky hwirq++; 4679a4da8a5SJan Glauber } 4689a4da8a5SJan Glauber 4695d0d8f43SMartin Schwidefsky /* Enable adapter interrupts */ 4705d0d8f43SMartin Schwidefsky rc = zpci_set_airq(zdev); 4715d0d8f43SMartin Schwidefsky if (rc) 4725d0d8f43SMartin Schwidefsky goto out_msi; 4735d0d8f43SMartin Schwidefsky 4745d0d8f43SMartin Schwidefsky return (msi_vecs == nvec) ? 0 : msi_vecs; 4755d0d8f43SMartin Schwidefsky 4765d0d8f43SMartin Schwidefsky out_msi: 4775d0d8f43SMartin Schwidefsky list_for_each_entry(msi, &pdev->msi_list, list) { 4781f44a225SMartin Schwidefsky if (hwirq-- == 0) 4795d0d8f43SMartin Schwidefsky break; 4801f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4811f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4821f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4831f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4841f44a225SMartin Schwidefsky msi->msg.data = 0; 4851f44a225SMartin Schwidefsky msi->irq = 0; 4865d0d8f43SMartin Schwidefsky } 4871f44a225SMartin Schwidefsky zpci_aibv[aisb] = NULL; 4885d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 4895d0d8f43SMartin Schwidefsky out_si: 4905d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, aisb); 4915d0d8f43SMartin Schwidefsky out: 4929a4da8a5SJan Glauber dev_err(&pdev->dev, "register MSI failed with: %d\n", rc); 4939a4da8a5SJan Glauber return rc; 4949a4da8a5SJan Glauber } 4959a4da8a5SJan Glauber 4965d0d8f43SMartin Schwidefsky void arch_teardown_msi_irqs(struct pci_dev *pdev) 4979a4da8a5SJan Glauber { 4989a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 4999a4da8a5SJan Glauber struct msi_desc *msi; 5005d0d8f43SMartin Schwidefsky int rc; 5019a4da8a5SJan Glauber 5025d0d8f43SMartin Schwidefsky pr_info("%s: on pdev: %p\n", __func__, pdev); 5035d0d8f43SMartin Schwidefsky 5045d0d8f43SMartin Schwidefsky /* Disable adapter interrupts */ 5055d0d8f43SMartin Schwidefsky rc = zpci_clear_airq(zdev); 5069a4da8a5SJan Glauber if (rc) { 5079a4da8a5SJan Glauber dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc); 5089a4da8a5SJan Glauber return; 5099a4da8a5SJan Glauber } 5109a4da8a5SJan Glauber 5111f44a225SMartin Schwidefsky /* Release MSI interrupts */ 5121f44a225SMartin Schwidefsky list_for_each_entry(msi, &pdev->msi_list, list) { 5131f44a225SMartin Schwidefsky zpci_msi_set_mask_bits(msi, 1, 1); 5141f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 5151f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 5161f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 5171f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 5181f44a225SMartin Schwidefsky msi->msg.data = 0; 5191f44a225SMartin Schwidefsky msi->irq = 0; 5201f44a225SMartin Schwidefsky } 5219a4da8a5SJan Glauber 5221f44a225SMartin Schwidefsky zpci_aibv[zdev->aisb] = NULL; 5235d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 5245d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 5259a4da8a5SJan Glauber } 5269a4da8a5SJan Glauber 527cd248341SJan Glauber static void zpci_map_resources(struct zpci_dev *zdev) 528cd248341SJan Glauber { 529cd248341SJan Glauber struct pci_dev *pdev = zdev->pdev; 530cd248341SJan Glauber resource_size_t len; 531cd248341SJan Glauber int i; 532cd248341SJan Glauber 533cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 534cd248341SJan Glauber len = pci_resource_len(pdev, i); 535cd248341SJan Glauber if (!len) 536cd248341SJan Glauber continue; 537cd248341SJan Glauber pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0); 538cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 539cd248341SJan Glauber } 540944239c5SSebastian Ott } 541944239c5SSebastian Ott 542944239c5SSebastian Ott static void zpci_unmap_resources(struct zpci_dev *zdev) 543944239c5SSebastian Ott { 544944239c5SSebastian Ott struct pci_dev *pdev = zdev->pdev; 545944239c5SSebastian Ott resource_size_t len; 546944239c5SSebastian Ott int i; 547944239c5SSebastian Ott 548944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 549944239c5SSebastian Ott len = pci_resource_len(pdev, i); 550944239c5SSebastian Ott if (!len) 551944239c5SSebastian Ott continue; 552944239c5SSebastian Ott pci_iounmap(pdev, (void *) pdev->resource[i].start); 553944239c5SSebastian Ott } 554944239c5SSebastian Ott } 555cd248341SJan Glauber 556cd248341SJan Glauber struct zpci_dev *zpci_alloc_device(void) 557cd248341SJan Glauber { 558cd248341SJan Glauber struct zpci_dev *zdev; 559cd248341SJan Glauber 560cd248341SJan Glauber /* Alloc memory for our private pci device data */ 561cd248341SJan Glauber zdev = kzalloc(sizeof(*zdev), GFP_KERNEL); 5625d0d8f43SMartin Schwidefsky return zdev ? : ERR_PTR(-ENOMEM); 563cd248341SJan Glauber } 564cd248341SJan Glauber 565cd248341SJan Glauber void zpci_free_device(struct zpci_dev *zdev) 566cd248341SJan Glauber { 567cd248341SJan Glauber kfree(zdev); 568cd248341SJan Glauber } 569cd248341SJan Glauber 5701e8da956SJan Glauber int pcibios_add_platform_entries(struct pci_dev *pdev) 5711e8da956SJan Glauber { 5721e8da956SJan Glauber return zpci_sysfs_add_device(&pdev->dev); 5731e8da956SJan Glauber } 5741e8da956SJan Glauber 5759a4da8a5SJan Glauber static int __init zpci_irq_init(void) 5769a4da8a5SJan Glauber { 5775d0d8f43SMartin Schwidefsky int rc; 5789a4da8a5SJan Glauber 579f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 580f4eae94fSMartin Schwidefsky if (rc) 5815d0d8f43SMartin Schwidefsky goto out; 582f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 583f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 5849a4da8a5SJan Glauber 5855d0d8f43SMartin Schwidefsky rc = -ENOMEM; 5865d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 5875d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 5885d0d8f43SMartin Schwidefsky goto out_airq; 5899a4da8a5SJan Glauber 5909389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 5919a4da8a5SJan Glauber return 0; 5929a4da8a5SJan Glauber 5935d0d8f43SMartin Schwidefsky out_airq: 5945d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5955d0d8f43SMartin Schwidefsky out: 5969a4da8a5SJan Glauber return rc; 5979a4da8a5SJan Glauber } 5989a4da8a5SJan Glauber 5999a4da8a5SJan Glauber static void zpci_irq_exit(void) 6009a4da8a5SJan Glauber { 6015d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 602f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 6039a4da8a5SJan Glauber } 6049a4da8a5SJan Glauber 605cd248341SJan Glauber static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size, 606cd248341SJan Glauber unsigned long flags, int domain) 607cd248341SJan Glauber { 608cd248341SJan Glauber struct resource *r; 609cd248341SJan Glauber char *name; 610cd248341SJan Glauber int rc; 611cd248341SJan Glauber 612cd248341SJan Glauber r = kzalloc(sizeof(*r), GFP_KERNEL); 613cd248341SJan Glauber if (!r) 614cd248341SJan Glauber return ERR_PTR(-ENOMEM); 615cd248341SJan Glauber r->start = start; 616cd248341SJan Glauber r->end = r->start + size - 1; 617cd248341SJan Glauber r->flags = flags; 618cd248341SJan Glauber r->parent = &iomem_resource; 619cd248341SJan Glauber name = kmalloc(18, GFP_KERNEL); 620cd248341SJan Glauber if (!name) { 621cd248341SJan Glauber kfree(r); 622cd248341SJan Glauber return ERR_PTR(-ENOMEM); 623cd248341SJan Glauber } 624cd248341SJan Glauber sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR); 625cd248341SJan Glauber r->name = name; 626cd248341SJan Glauber 627cd248341SJan Glauber rc = request_resource(&iomem_resource, r); 628cd248341SJan Glauber if (rc) 629cd248341SJan Glauber pr_debug("request resource %pR failed\n", r); 630cd248341SJan Glauber return r; 631cd248341SJan Glauber } 632cd248341SJan Glauber 633cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 634cd248341SJan Glauber { 635cd248341SJan Glauber int entry; 636cd248341SJan Glauber 637cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 638cd248341SJan Glauber entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 639cd248341SJan Glauber if (entry == ZPCI_IOMAP_MAX_ENTRIES) { 640cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 641cd248341SJan Glauber return -ENOSPC; 642cd248341SJan Glauber } 643cd248341SJan Glauber set_bit(entry, zpci_iomap); 644cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 645cd248341SJan Glauber return entry; 646cd248341SJan Glauber } 647cd248341SJan Glauber 648cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 649cd248341SJan Glauber { 650cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 651cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 652cd248341SJan Glauber clear_bit(entry, zpci_iomap); 653cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 654cd248341SJan Glauber } 655cd248341SJan Glauber 656af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 657af0a8a84SSebastian Ott { 658af0a8a84SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 659cb809182SSebastian Ott struct resource *res; 660cb809182SSebastian Ott int i; 661cb809182SSebastian Ott 662cb809182SSebastian Ott zdev->pdev = pdev; 663cb809182SSebastian Ott zpci_map_resources(zdev); 664cb809182SSebastian Ott 665cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 666cb809182SSebastian Ott res = &pdev->resource[i]; 667cb809182SSebastian Ott if (res->parent || !res->flags) 668cb809182SSebastian Ott continue; 669cb809182SSebastian Ott pci_claim_resource(pdev, i); 670cb809182SSebastian Ott } 671cb809182SSebastian Ott 672cb809182SSebastian Ott return 0; 673cb809182SSebastian Ott } 674cb809182SSebastian Ott 675cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 676cb809182SSebastian Ott { 677cb809182SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 678cb809182SSebastian Ott struct resource *res; 679cb809182SSebastian Ott u16 cmd; 680cb809182SSebastian Ott int i; 681af0a8a84SSebastian Ott 6821c21351bSSebastian Ott zdev->pdev = pdev; 683af0a8a84SSebastian Ott zpci_debug_init_device(zdev); 684af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 685af0a8a84SSebastian Ott zpci_map_resources(zdev); 686af0a8a84SSebastian Ott 687cb809182SSebastian Ott pci_read_config_word(pdev, PCI_COMMAND, &cmd); 688cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 689cb809182SSebastian Ott res = &pdev->resource[i]; 690cb809182SSebastian Ott 691cb809182SSebastian Ott if (res->flags & IORESOURCE_IO) 692cb809182SSebastian Ott return -EINVAL; 693cb809182SSebastian Ott 694cb809182SSebastian Ott if (res->flags & IORESOURCE_MEM) 695cb809182SSebastian Ott cmd |= PCI_COMMAND_MEMORY; 696cb809182SSebastian Ott } 697cb809182SSebastian Ott pci_write_config_word(pdev, PCI_COMMAND, cmd); 698af0a8a84SSebastian Ott return 0; 699af0a8a84SSebastian Ott } 700af0a8a84SSebastian Ott 701cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 702944239c5SSebastian Ott { 703944239c5SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 704944239c5SSebastian Ott 705944239c5SSebastian Ott zpci_unmap_resources(zdev); 706944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 707944239c5SSebastian Ott zpci_debug_exit_device(zdev); 708944239c5SSebastian Ott zdev->pdev = NULL; 709944239c5SSebastian Ott } 710944239c5SSebastian Ott 7111c21351bSSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 712cd248341SJan Glauber { 713cd248341SJan Glauber struct resource *res; 714cd248341SJan Glauber LIST_HEAD(resources); 715cd248341SJan Glauber int i; 716cd248341SJan Glauber 717cd248341SJan Glauber /* allocate mapping entry for each used bar */ 718cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 719cd248341SJan Glauber unsigned long addr, size, flags; 720cd248341SJan Glauber int entry; 721cd248341SJan Glauber 722cd248341SJan Glauber if (!zdev->bars[i].size) 723cd248341SJan Glauber continue; 724cd248341SJan Glauber entry = zpci_alloc_iomap(zdev); 725cd248341SJan Glauber if (entry < 0) 726cd248341SJan Glauber return entry; 727cd248341SJan Glauber zdev->bars[i].map_idx = entry; 728cd248341SJan Glauber 729cd248341SJan Glauber /* only MMIO is supported */ 730cd248341SJan Glauber flags = IORESOURCE_MEM; 731cd248341SJan Glauber if (zdev->bars[i].val & 8) 732cd248341SJan Glauber flags |= IORESOURCE_PREFETCH; 733cd248341SJan Glauber if (zdev->bars[i].val & 4) 734cd248341SJan Glauber flags |= IORESOURCE_MEM_64; 735cd248341SJan Glauber 736cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48); 737cd248341SJan Glauber 738cd248341SJan Glauber size = 1UL << zdev->bars[i].size; 739cd248341SJan Glauber 740cd248341SJan Glauber res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain); 741cd248341SJan Glauber if (IS_ERR(res)) { 742cd248341SJan Glauber zpci_free_iomap(zdev, entry); 743cd248341SJan Glauber return PTR_ERR(res); 744cd248341SJan Glauber } 745cd248341SJan Glauber pci_add_resource(&resources, res); 746cd248341SJan Glauber } 747cd248341SJan Glauber 7481c21351bSSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 749cd248341SJan Glauber zdev, &resources); 750cd248341SJan Glauber if (!zdev->bus) 751cd248341SJan Glauber return -EIO; 752cd248341SJan Glauber 753cd248341SJan Glauber zdev->bus->max_bus_speed = zdev->max_bus_speed; 754cd248341SJan Glauber return 0; 755cd248341SJan Glauber } 756cd248341SJan Glauber 757cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 758cd248341SJan Glauber { 759cd248341SJan Glauber spin_lock(&zpci_domain_lock); 760cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 761cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 762cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 763cd248341SJan Glauber return -ENOSPC; 764cd248341SJan Glauber } 765cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 766cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 767cd248341SJan Glauber return 0; 768cd248341SJan Glauber } 769cd248341SJan Glauber 770cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 771cd248341SJan Glauber { 772cd248341SJan Glauber spin_lock(&zpci_domain_lock); 773cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 774cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 775cd248341SJan Glauber } 776cd248341SJan Glauber 777a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 778a755a45dSJan Glauber { 779a755a45dSJan Glauber int rc; 780a755a45dSJan Glauber 781a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 782a755a45dSJan Glauber if (rc) 783a755a45dSJan Glauber goto out; 784a755a45dSJan Glauber pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid); 785828b35f6SJan Glauber 786828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 787828b35f6SJan Glauber if (rc) 788828b35f6SJan Glauber goto out_dma; 7890ff70ec8SSebastian Ott 7900ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 791a755a45dSJan Glauber return 0; 792828b35f6SJan Glauber 793828b35f6SJan Glauber out_dma: 794828b35f6SJan Glauber clp_disable_fh(zdev); 795a755a45dSJan Glauber out: 796a755a45dSJan Glauber return rc; 797a755a45dSJan Glauber } 798a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 799a755a45dSJan Glauber 800cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 801cb65a669SSebastian Ott { 802cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 803cb65a669SSebastian Ott return clp_disable_fh(zdev); 804cb65a669SSebastian Ott } 805cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 806cb65a669SSebastian Ott 807cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 808cd248341SJan Glauber { 809cd248341SJan Glauber int rc; 810cd248341SJan Glauber 811cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 812cd248341SJan Glauber if (rc) 813cd248341SJan Glauber goto out; 814cd248341SJan Glauber 8151c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8161c21351bSSebastian Ott rc = zpci_enable_device(zdev); 817cd248341SJan Glauber if (rc) 8181c21351bSSebastian Ott goto out_free; 8191c21351bSSebastian Ott } 8201c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8211c21351bSSebastian Ott if (rc) 8221c21351bSSebastian Ott goto out_disable; 823cd248341SJan Glauber 824*57b5918cSSebastian Ott spin_lock(&zpci_list_lock); 825cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 826*57b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 827cd248341SJan Glauber 82867f43f38SSebastian Ott zpci_init_slot(zdev); 82967f43f38SSebastian Ott 830cd248341SJan Glauber return 0; 831cd248341SJan Glauber 8321c21351bSSebastian Ott out_disable: 8331c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8341c21351bSSebastian Ott zpci_disable_device(zdev); 8351c21351bSSebastian Ott out_free: 836cd248341SJan Glauber zpci_free_domain(zdev); 837cd248341SJan Glauber out: 838cd248341SJan Glauber return rc; 839cd248341SJan Glauber } 840cd248341SJan Glauber 841cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 842cd248341SJan Glauber { 843828b35f6SJan Glauber zpci_dma_exit_device(zdev); 844cd248341SJan Glauber /* 845cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 846cd248341SJan Glauber * do that here. 847cd248341SJan Glauber */ 848cd248341SJan Glauber } 849cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 850cd248341SJan Glauber 851cd248341SJan Glauber static inline int barsize(u8 size) 852cd248341SJan Glauber { 853cd248341SJan Glauber return (size) ? (1 << size) >> 10 : 0; 854cd248341SJan Glauber } 855cd248341SJan Glauber 856cd248341SJan Glauber static int zpci_mem_init(void) 857cd248341SJan Glauber { 858d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 859d0b08853SJan Glauber 16, 0, NULL); 860d0b08853SJan Glauber if (!zdev_fmb_cache) 8611f44a225SMartin Schwidefsky goto error_zdev; 862d0b08853SJan Glauber 863cd248341SJan Glauber /* TODO: use realloc */ 864cd248341SJan Glauber zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start), 865cd248341SJan Glauber GFP_KERNEL); 866cd248341SJan Glauber if (!zpci_iomap_start) 8679a4da8a5SJan Glauber goto error_iomap; 868cd248341SJan Glauber return 0; 869cd248341SJan Glauber 8709a4da8a5SJan Glauber error_iomap: 871d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 872cd248341SJan Glauber error_zdev: 873cd248341SJan Glauber return -ENOMEM; 874cd248341SJan Glauber } 875cd248341SJan Glauber 876cd248341SJan Glauber static void zpci_mem_exit(void) 877cd248341SJan Glauber { 878cd248341SJan Glauber kfree(zpci_iomap_start); 879d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 880cd248341SJan Glauber } 881cd248341SJan Glauber 88267f43f38SSebastian Ott static unsigned int s390_pci_probe; 883cd248341SJan Glauber 884cd248341SJan Glauber char * __init pcibios_setup(char *str) 885cd248341SJan Glauber { 88689b0dc95SSebastian Ott if (!strcmp(str, "on")) { 88789b0dc95SSebastian Ott s390_pci_probe = 1; 888cd248341SJan Glauber return NULL; 889cd248341SJan Glauber } 890cd248341SJan Glauber return str; 891cd248341SJan Glauber } 892cd248341SJan Glauber 893cd248341SJan Glauber static int __init pci_base_init(void) 894cd248341SJan Glauber { 895cd248341SJan Glauber int rc; 896cd248341SJan Glauber 8971e5635d1SHeiko Carstens if (!s390_pci_probe) 898cd248341SJan Glauber return 0; 899cd248341SJan Glauber 900cd248341SJan Glauber if (!test_facility(2) || !test_facility(69) 901cd248341SJan Glauber || !test_facility(71) || !test_facility(72)) 902cd248341SJan Glauber return 0; 903cd248341SJan Glauber 904cd248341SJan Glauber pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n", 905cd248341SJan Glauber test_facility(69), test_facility(70), 906cd248341SJan Glauber test_facility(71)); 907cd248341SJan Glauber 908d0b08853SJan Glauber rc = zpci_debug_init(); 909d0b08853SJan Glauber if (rc) 9101f44a225SMartin Schwidefsky goto out; 911d0b08853SJan Glauber 912cd248341SJan Glauber rc = zpci_mem_init(); 913cd248341SJan Glauber if (rc) 914cd248341SJan Glauber goto out_mem; 915cd248341SJan Glauber 9169a4da8a5SJan Glauber rc = zpci_irq_init(); 9179a4da8a5SJan Glauber if (rc) 9189a4da8a5SJan Glauber goto out_irq; 9199a4da8a5SJan Glauber 920828b35f6SJan Glauber rc = zpci_dma_init(); 921828b35f6SJan Glauber if (rc) 922828b35f6SJan Glauber goto out_dma; 923828b35f6SJan Glauber 9241d578966SSebastian Ott rc = clp_scan_pci_devices(); 925a755a45dSJan Glauber if (rc) 926a755a45dSJan Glauber goto out_find; 927a755a45dSJan Glauber 928cd248341SJan Glauber return 0; 929cd248341SJan Glauber 930a755a45dSJan Glauber out_find: 931828b35f6SJan Glauber zpci_dma_exit(); 932828b35f6SJan Glauber out_dma: 9339a4da8a5SJan Glauber zpci_irq_exit(); 9349a4da8a5SJan Glauber out_irq: 935cd248341SJan Glauber zpci_mem_exit(); 936cd248341SJan Glauber out_mem: 937d0b08853SJan Glauber zpci_debug_exit(); 9381f44a225SMartin Schwidefsky out: 939cd248341SJan Glauber return rc; 940cd248341SJan Glauber } 94167f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 942*57b5918cSSebastian Ott 943*57b5918cSSebastian Ott void zpci_rescan(void) 944*57b5918cSSebastian Ott { 945*57b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 946*57b5918cSSebastian Ott } 947