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 533cd248341SJan Glauber struct zpci_dev *zpci_alloc_device(void) 534cd248341SJan Glauber { 535cd248341SJan Glauber struct zpci_dev *zdev; 536cd248341SJan Glauber 537cd248341SJan Glauber /* Alloc memory for our private pci device data */ 538cd248341SJan Glauber zdev = kzalloc(sizeof(*zdev), GFP_KERNEL); 5395d0d8f43SMartin Schwidefsky return zdev ? : ERR_PTR(-ENOMEM); 540cd248341SJan Glauber } 541cd248341SJan Glauber 542cd248341SJan Glauber void zpci_free_device(struct zpci_dev *zdev) 543cd248341SJan Glauber { 544cd248341SJan Glauber kfree(zdev); 545cd248341SJan Glauber } 546cd248341SJan Glauber 5471e8da956SJan Glauber int pcibios_add_platform_entries(struct pci_dev *pdev) 5481e8da956SJan Glauber { 5491e8da956SJan Glauber return zpci_sysfs_add_device(&pdev->dev); 5501e8da956SJan Glauber } 5511e8da956SJan Glauber 5529a4da8a5SJan Glauber static int __init zpci_irq_init(void) 5539a4da8a5SJan Glauber { 5545d0d8f43SMartin Schwidefsky int rc; 5559a4da8a5SJan Glauber 556f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 557f4eae94fSMartin Schwidefsky if (rc) 5585d0d8f43SMartin Schwidefsky goto out; 559f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 560f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 5619a4da8a5SJan Glauber 5625d0d8f43SMartin Schwidefsky rc = -ENOMEM; 5635d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 5645d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 5655d0d8f43SMartin Schwidefsky goto out_airq; 5669a4da8a5SJan Glauber 5679389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 5689a4da8a5SJan Glauber return 0; 5699a4da8a5SJan Glauber 5705d0d8f43SMartin Schwidefsky out_airq: 5715d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5725d0d8f43SMartin Schwidefsky out: 5739a4da8a5SJan Glauber return rc; 5749a4da8a5SJan Glauber } 5759a4da8a5SJan Glauber 5769a4da8a5SJan Glauber static void zpci_irq_exit(void) 5779a4da8a5SJan Glauber { 5785d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 579f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5809a4da8a5SJan Glauber } 5819a4da8a5SJan Glauber 582cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 583cd248341SJan Glauber { 584cd248341SJan Glauber int entry; 585cd248341SJan Glauber 586cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 587cd248341SJan Glauber entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 588cd248341SJan Glauber if (entry == ZPCI_IOMAP_MAX_ENTRIES) { 589cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 590cd248341SJan Glauber return -ENOSPC; 591cd248341SJan Glauber } 592cd248341SJan Glauber set_bit(entry, zpci_iomap); 593cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 594cd248341SJan Glauber return entry; 595cd248341SJan Glauber } 596cd248341SJan Glauber 597cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 598cd248341SJan Glauber { 599cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 600cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 601cd248341SJan Glauber clear_bit(entry, zpci_iomap); 602cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 603cd248341SJan Glauber } 604cd248341SJan Glauber 605*7a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 606*7a572a3aSSebastian Ott unsigned long size, unsigned long flags) 607*7a572a3aSSebastian Ott { 608*7a572a3aSSebastian Ott struct resource *r; 609*7a572a3aSSebastian Ott 610*7a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 611*7a572a3aSSebastian Ott if (!r) 612*7a572a3aSSebastian Ott return NULL; 613*7a572a3aSSebastian Ott 614*7a572a3aSSebastian Ott r->start = start; 615*7a572a3aSSebastian Ott r->end = r->start + size - 1; 616*7a572a3aSSebastian Ott r->flags = flags; 617*7a572a3aSSebastian Ott r->name = zdev->res_name; 618*7a572a3aSSebastian Ott 619*7a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 620*7a572a3aSSebastian Ott kfree(r); 621*7a572a3aSSebastian Ott return NULL; 622*7a572a3aSSebastian Ott } 623*7a572a3aSSebastian Ott return r; 624*7a572a3aSSebastian Ott } 625*7a572a3aSSebastian Ott 626*7a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 627*7a572a3aSSebastian Ott struct list_head *resources) 628*7a572a3aSSebastian Ott { 629*7a572a3aSSebastian Ott unsigned long addr, size, flags; 630*7a572a3aSSebastian Ott struct resource *res; 631*7a572a3aSSebastian Ott int i, entry; 632*7a572a3aSSebastian Ott 633*7a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 634*7a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 635*7a572a3aSSebastian Ott 636*7a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 637*7a572a3aSSebastian Ott if (!zdev->bars[i].size) 638*7a572a3aSSebastian Ott continue; 639*7a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 640*7a572a3aSSebastian Ott if (entry < 0) 641*7a572a3aSSebastian Ott return entry; 642*7a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 643*7a572a3aSSebastian Ott 644*7a572a3aSSebastian Ott /* only MMIO is supported */ 645*7a572a3aSSebastian Ott flags = IORESOURCE_MEM; 646*7a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 647*7a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 648*7a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 649*7a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 650*7a572a3aSSebastian Ott 651*7a572a3aSSebastian Ott addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48); 652*7a572a3aSSebastian Ott 653*7a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 654*7a572a3aSSebastian Ott 655*7a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 656*7a572a3aSSebastian Ott if (!res) { 657*7a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 658*7a572a3aSSebastian Ott return -ENOMEM; 659*7a572a3aSSebastian Ott } 660*7a572a3aSSebastian Ott zdev->bars[i].res = res; 661*7a572a3aSSebastian Ott pci_add_resource(resources, res); 662*7a572a3aSSebastian Ott } 663*7a572a3aSSebastian Ott 664*7a572a3aSSebastian Ott return 0; 665*7a572a3aSSebastian Ott } 666*7a572a3aSSebastian Ott 667*7a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 668*7a572a3aSSebastian Ott { 669*7a572a3aSSebastian Ott int i; 670*7a572a3aSSebastian Ott 671*7a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 672*7a572a3aSSebastian Ott if (!zdev->bars[i].size) 673*7a572a3aSSebastian Ott continue; 674*7a572a3aSSebastian Ott 675*7a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 676*7a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 677*7a572a3aSSebastian Ott kfree(zdev->bars[i].res); 678*7a572a3aSSebastian Ott } 679*7a572a3aSSebastian Ott } 680*7a572a3aSSebastian Ott 681af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 682af0a8a84SSebastian Ott { 683af0a8a84SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 684cb809182SSebastian Ott struct resource *res; 685cb809182SSebastian Ott int i; 686cb809182SSebastian Ott 687cb809182SSebastian Ott zdev->pdev = pdev; 688cb809182SSebastian Ott zpci_map_resources(zdev); 689cb809182SSebastian Ott 690cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 691cb809182SSebastian Ott res = &pdev->resource[i]; 692cb809182SSebastian Ott if (res->parent || !res->flags) 693cb809182SSebastian Ott continue; 694cb809182SSebastian Ott pci_claim_resource(pdev, i); 695cb809182SSebastian Ott } 696cb809182SSebastian Ott 697cb809182SSebastian Ott return 0; 698cb809182SSebastian Ott } 699cb809182SSebastian Ott 700cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 701cb809182SSebastian Ott { 702cb809182SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 703cb809182SSebastian Ott struct resource *res; 704cb809182SSebastian Ott u16 cmd; 705cb809182SSebastian Ott int i; 706af0a8a84SSebastian Ott 7071c21351bSSebastian Ott zdev->pdev = pdev; 708af0a8a84SSebastian Ott zpci_debug_init_device(zdev); 709af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 710af0a8a84SSebastian Ott zpci_map_resources(zdev); 711af0a8a84SSebastian Ott 712cb809182SSebastian Ott pci_read_config_word(pdev, PCI_COMMAND, &cmd); 713cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 714cb809182SSebastian Ott res = &pdev->resource[i]; 715cb809182SSebastian Ott 716cb809182SSebastian Ott if (res->flags & IORESOURCE_IO) 717cb809182SSebastian Ott return -EINVAL; 718cb809182SSebastian Ott 719cb809182SSebastian Ott if (res->flags & IORESOURCE_MEM) 720cb809182SSebastian Ott cmd |= PCI_COMMAND_MEMORY; 721cb809182SSebastian Ott } 722cb809182SSebastian Ott pci_write_config_word(pdev, PCI_COMMAND, cmd); 723af0a8a84SSebastian Ott return 0; 724af0a8a84SSebastian Ott } 725af0a8a84SSebastian Ott 726cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 727944239c5SSebastian Ott { 728944239c5SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 729944239c5SSebastian Ott 730944239c5SSebastian Ott zpci_unmap_resources(zdev); 731944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 732944239c5SSebastian Ott zpci_debug_exit_device(zdev); 733944239c5SSebastian Ott zdev->pdev = NULL; 734944239c5SSebastian Ott } 735944239c5SSebastian Ott 73669db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 73769db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 73869db3b5eSSebastian Ott { 73969db3b5eSSebastian Ott struct zpci_dev *zdev = get_zdev(to_pci_dev(dev)); 74069db3b5eSSebastian Ott int ret = 0; 74169db3b5eSSebastian Ott 74269db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 74369db3b5eSSebastian Ott goto out; 74469db3b5eSSebastian Ott 74569db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 74669db3b5eSSebastian Ott if (ret) 74769db3b5eSSebastian Ott goto out; 74869db3b5eSSebastian Ott 74969db3b5eSSebastian Ott zpci_map_resources(zdev); 75069db3b5eSSebastian Ott zpci_register_ioat(zdev, 0, zdev->start_dma + PAGE_OFFSET, 75169db3b5eSSebastian Ott zdev->start_dma + zdev->iommu_size - 1, 75269db3b5eSSebastian Ott (u64) zdev->dma_table); 75369db3b5eSSebastian Ott 75469db3b5eSSebastian Ott out: 75569db3b5eSSebastian Ott return ret; 75669db3b5eSSebastian Ott } 75769db3b5eSSebastian Ott 75869db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 75969db3b5eSSebastian Ott { 76069db3b5eSSebastian Ott struct zpci_dev *zdev = get_zdev(to_pci_dev(dev)); 76169db3b5eSSebastian Ott 76269db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 76369db3b5eSSebastian Ott return 0; 76469db3b5eSSebastian Ott 76569db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 76669db3b5eSSebastian Ott return clp_disable_fh(zdev); 76769db3b5eSSebastian Ott } 76869db3b5eSSebastian Ott 76969db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 77069db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 77169db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 77269db3b5eSSebastian Ott .restore_noirq = zpci_restore, 77369db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 77469db3b5eSSebastian Ott }; 77569db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 77669db3b5eSSebastian Ott 7771c21351bSSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 778cd248341SJan Glauber { 779cd248341SJan Glauber LIST_HEAD(resources); 780*7a572a3aSSebastian Ott int ret; 781cd248341SJan Glauber 782*7a572a3aSSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 783*7a572a3aSSebastian Ott if (ret) 784*7a572a3aSSebastian Ott return ret; 785cd248341SJan Glauber 7861c21351bSSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 787cd248341SJan Glauber zdev, &resources); 788*7a572a3aSSebastian Ott if (!zdev->bus) { 789*7a572a3aSSebastian Ott zpci_cleanup_bus_resources(zdev); 790cd248341SJan Glauber return -EIO; 791*7a572a3aSSebastian Ott } 792cd248341SJan Glauber 793cd248341SJan Glauber zdev->bus->max_bus_speed = zdev->max_bus_speed; 794cd248341SJan Glauber return 0; 795cd248341SJan Glauber } 796cd248341SJan Glauber 797cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 798cd248341SJan Glauber { 799cd248341SJan Glauber spin_lock(&zpci_domain_lock); 800cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 801cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 802cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 803cd248341SJan Glauber return -ENOSPC; 804cd248341SJan Glauber } 805cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 806cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 807cd248341SJan Glauber return 0; 808cd248341SJan Glauber } 809cd248341SJan Glauber 810cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 811cd248341SJan Glauber { 812cd248341SJan Glauber spin_lock(&zpci_domain_lock); 813cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 814cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 815cd248341SJan Glauber } 816cd248341SJan Glauber 817a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 818a755a45dSJan Glauber { 819a755a45dSJan Glauber int rc; 820a755a45dSJan Glauber 821a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 822a755a45dSJan Glauber if (rc) 823a755a45dSJan Glauber goto out; 824828b35f6SJan Glauber 825828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 826828b35f6SJan Glauber if (rc) 827828b35f6SJan Glauber goto out_dma; 8280ff70ec8SSebastian Ott 8290ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 830a755a45dSJan Glauber return 0; 831828b35f6SJan Glauber 832828b35f6SJan Glauber out_dma: 833828b35f6SJan Glauber clp_disable_fh(zdev); 834a755a45dSJan Glauber out: 835a755a45dSJan Glauber return rc; 836a755a45dSJan Glauber } 837a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 838a755a45dSJan Glauber 839cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 840cb65a669SSebastian Ott { 841cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 842cb65a669SSebastian Ott return clp_disable_fh(zdev); 843cb65a669SSebastian Ott } 844cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 845cb65a669SSebastian Ott 846cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 847cd248341SJan Glauber { 848cd248341SJan Glauber int rc; 849cd248341SJan Glauber 850cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 851cd248341SJan Glauber if (rc) 852cd248341SJan Glauber goto out; 853cd248341SJan Glauber 8541c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8551c21351bSSebastian Ott rc = zpci_enable_device(zdev); 856cd248341SJan Glauber if (rc) 8571c21351bSSebastian Ott goto out_free; 8581c21351bSSebastian Ott } 8591c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8601c21351bSSebastian Ott if (rc) 8611c21351bSSebastian Ott goto out_disable; 862cd248341SJan Glauber 86357b5918cSSebastian Ott spin_lock(&zpci_list_lock); 864cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 86557b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 866cd248341SJan Glauber 86767f43f38SSebastian Ott zpci_init_slot(zdev); 86867f43f38SSebastian Ott 869cd248341SJan Glauber return 0; 870cd248341SJan Glauber 8711c21351bSSebastian Ott out_disable: 8721c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8731c21351bSSebastian Ott zpci_disable_device(zdev); 8741c21351bSSebastian Ott out_free: 875cd248341SJan Glauber zpci_free_domain(zdev); 876cd248341SJan Glauber out: 877cd248341SJan Glauber return rc; 878cd248341SJan Glauber } 879cd248341SJan Glauber 880cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 881cd248341SJan Glauber { 882828b35f6SJan Glauber zpci_dma_exit_device(zdev); 883cd248341SJan Glauber /* 884cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 885cd248341SJan Glauber * do that here. 886cd248341SJan Glauber */ 887cd248341SJan Glauber } 888cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 889cd248341SJan Glauber 890cd248341SJan Glauber static inline int barsize(u8 size) 891cd248341SJan Glauber { 892cd248341SJan Glauber return (size) ? (1 << size) >> 10 : 0; 893cd248341SJan Glauber } 894cd248341SJan Glauber 895cd248341SJan Glauber static int zpci_mem_init(void) 896cd248341SJan Glauber { 897d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 898d0b08853SJan Glauber 16, 0, NULL); 899d0b08853SJan Glauber if (!zdev_fmb_cache) 9001f44a225SMartin Schwidefsky goto error_zdev; 901d0b08853SJan Glauber 902cd248341SJan Glauber /* TODO: use realloc */ 903cd248341SJan Glauber zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start), 904cd248341SJan Glauber GFP_KERNEL); 905cd248341SJan Glauber if (!zpci_iomap_start) 9069a4da8a5SJan Glauber goto error_iomap; 907cd248341SJan Glauber return 0; 908cd248341SJan Glauber 9099a4da8a5SJan Glauber error_iomap: 910d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 911cd248341SJan Glauber error_zdev: 912cd248341SJan Glauber return -ENOMEM; 913cd248341SJan Glauber } 914cd248341SJan Glauber 915cd248341SJan Glauber static void zpci_mem_exit(void) 916cd248341SJan Glauber { 917cd248341SJan Glauber kfree(zpci_iomap_start); 918d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 919cd248341SJan Glauber } 920cd248341SJan Glauber 92167f43f38SSebastian Ott static unsigned int s390_pci_probe; 922cd248341SJan Glauber 923cd248341SJan Glauber char * __init pcibios_setup(char *str) 924cd248341SJan Glauber { 92589b0dc95SSebastian Ott if (!strcmp(str, "on")) { 92689b0dc95SSebastian Ott s390_pci_probe = 1; 927cd248341SJan Glauber return NULL; 928cd248341SJan Glauber } 929cd248341SJan Glauber return str; 930cd248341SJan Glauber } 931cd248341SJan Glauber 932cd248341SJan Glauber static int __init pci_base_init(void) 933cd248341SJan Glauber { 934cd248341SJan Glauber int rc; 935cd248341SJan Glauber 9361e5635d1SHeiko Carstens if (!s390_pci_probe) 937cd248341SJan Glauber return 0; 938cd248341SJan Glauber 939cd248341SJan Glauber if (!test_facility(2) || !test_facility(69) 940cd248341SJan Glauber || !test_facility(71) || !test_facility(72)) 941cd248341SJan Glauber return 0; 942cd248341SJan Glauber 943d0b08853SJan Glauber rc = zpci_debug_init(); 944d0b08853SJan Glauber if (rc) 9451f44a225SMartin Schwidefsky goto out; 946d0b08853SJan Glauber 947cd248341SJan Glauber rc = zpci_mem_init(); 948cd248341SJan Glauber if (rc) 949cd248341SJan Glauber goto out_mem; 950cd248341SJan Glauber 9519a4da8a5SJan Glauber rc = zpci_irq_init(); 9529a4da8a5SJan Glauber if (rc) 9539a4da8a5SJan Glauber goto out_irq; 9549a4da8a5SJan Glauber 955828b35f6SJan Glauber rc = zpci_dma_init(); 956828b35f6SJan Glauber if (rc) 957828b35f6SJan Glauber goto out_dma; 958828b35f6SJan Glauber 9591d578966SSebastian Ott rc = clp_scan_pci_devices(); 960a755a45dSJan Glauber if (rc) 961a755a45dSJan Glauber goto out_find; 962a755a45dSJan Glauber 963cd248341SJan Glauber return 0; 964cd248341SJan Glauber 965a755a45dSJan Glauber out_find: 966828b35f6SJan Glauber zpci_dma_exit(); 967828b35f6SJan Glauber out_dma: 9689a4da8a5SJan Glauber zpci_irq_exit(); 9699a4da8a5SJan Glauber out_irq: 970cd248341SJan Glauber zpci_mem_exit(); 971cd248341SJan Glauber out_mem: 972d0b08853SJan Glauber zpci_debug_exit(); 9731f44a225SMartin Schwidefsky out: 974cd248341SJan Glauber return rc; 975cd248341SJan Glauber } 97667f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 97757b5918cSSebastian Ott 97857b5918cSSebastian Ott void zpci_rescan(void) 97957b5918cSSebastian Ott { 98057b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 98157b5918cSSebastian Ott } 982