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 459a4da8a5SJan Glauber #define ZPCI_MSI_VEC_BITS 6 46cd248341SJan Glauber #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS 47cd248341SJan Glauber 48cd248341SJan Glauber /* list of all detected zpci devices */ 49cd248341SJan Glauber LIST_HEAD(zpci_list); 507441b062SJan Glauber EXPORT_SYMBOL_GPL(zpci_list); 51cd248341SJan Glauber DEFINE_MUTEX(zpci_list_lock); 527441b062SJan Glauber EXPORT_SYMBOL_GPL(zpci_list_lock); 537441b062SJan Glauber 5453923354SSebastian Ott static struct pci_hp_callback_ops *hotplug_ops; 55cd248341SJan Glauber 56cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 57cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 58cd248341SJan Glauber 599a4da8a5SJan Glauber struct callback { 609a4da8a5SJan Glauber irq_handler_t handler; 619a4da8a5SJan Glauber void *data; 629a4da8a5SJan Glauber }; 639a4da8a5SJan Glauber 649a4da8a5SJan Glauber struct zdev_irq_map { 659a4da8a5SJan Glauber unsigned long aibv; /* AI bit vector */ 669a4da8a5SJan Glauber int msi_vecs; /* consecutive MSI-vectors used */ 679a4da8a5SJan Glauber int __unused; 689a4da8a5SJan Glauber struct callback cb[ZPCI_NR_MSI_VECS]; /* callback handler array */ 699a4da8a5SJan Glauber spinlock_t lock; /* protect callbacks against de-reg */ 709a4da8a5SJan Glauber }; 719a4da8a5SJan Glauber 729a4da8a5SJan Glauber struct intr_bucket { 739a4da8a5SJan Glauber /* amap of adapters, one bit per dev, corresponds to one irq nr */ 749a4da8a5SJan Glauber unsigned long *alloc; 759a4da8a5SJan Glauber /* AI summary bit, global page for all devices */ 769a4da8a5SJan Glauber unsigned long *aisb; 779a4da8a5SJan Glauber /* pointer to aibv and callback data in zdev */ 789a4da8a5SJan Glauber struct zdev_irq_map *imap[ZPCI_NR_DEVICES]; 799a4da8a5SJan Glauber /* protects the whole bucket struct */ 809a4da8a5SJan Glauber spinlock_t lock; 819a4da8a5SJan Glauber }; 829a4da8a5SJan Glauber 839a4da8a5SJan Glauber static struct intr_bucket *bucket; 849a4da8a5SJan Glauber 859a4da8a5SJan Glauber /* Adapter local summary indicator */ 869a4da8a5SJan Glauber static u8 *zpci_irq_si; 879a4da8a5SJan Glauber 889a4da8a5SJan Glauber static atomic_t irq_retries = ATOMIC_INIT(0); 899a4da8a5SJan Glauber 90cd248341SJan Glauber /* I/O Map */ 91cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 92cd248341SJan Glauber static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 93cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 94cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 95cd248341SJan Glauber 969a4da8a5SJan Glauber /* highest irq summary bit */ 979a4da8a5SJan Glauber static int __read_mostly aisb_max; 989a4da8a5SJan Glauber 999a4da8a5SJan Glauber static struct kmem_cache *zdev_irq_cache; 100d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 101d0b08853SJan Glauber 1029a4da8a5SJan Glauber static inline int irq_to_msi_nr(unsigned int irq) 1039a4da8a5SJan Glauber { 1049a4da8a5SJan Glauber return irq & ZPCI_MSI_MASK; 1059a4da8a5SJan Glauber } 1069a4da8a5SJan Glauber 1079a4da8a5SJan Glauber static inline int irq_to_dev_nr(unsigned int irq) 1089a4da8a5SJan Glauber { 1099a4da8a5SJan Glauber return irq >> ZPCI_MSI_VEC_BITS; 1109a4da8a5SJan Glauber } 1119a4da8a5SJan Glauber 1129a4da8a5SJan Glauber static inline struct zdev_irq_map *get_imap(unsigned int irq) 1139a4da8a5SJan Glauber { 1149a4da8a5SJan Glauber return bucket->imap[irq_to_dev_nr(irq)]; 1159a4da8a5SJan Glauber } 1169a4da8a5SJan Glauber 117cd248341SJan Glauber struct zpci_dev *get_zdev(struct pci_dev *pdev) 118cd248341SJan Glauber { 119cd248341SJan Glauber return (struct zpci_dev *) pdev->sysdata; 120cd248341SJan Glauber } 121cd248341SJan Glauber 122cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 123cd248341SJan Glauber { 124cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 125cd248341SJan Glauber 126cd248341SJan Glauber mutex_lock(&zpci_list_lock); 127cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 128cd248341SJan Glauber if (tmp->fid == fid) { 129cd248341SJan Glauber zdev = tmp; 130cd248341SJan Glauber break; 131cd248341SJan Glauber } 132cd248341SJan Glauber } 133cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 134cd248341SJan Glauber return zdev; 135cd248341SJan Glauber } 136cd248341SJan Glauber 137cd248341SJan Glauber bool zpci_fid_present(u32 fid) 138cd248341SJan Glauber { 139cd248341SJan Glauber return (get_zdev_by_fid(fid) != NULL) ? true : false; 140cd248341SJan Glauber } 141cd248341SJan Glauber 142cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 143cd248341SJan Glauber { 144cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 145cd248341SJan Glauber } 146cd248341SJan Glauber 147cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 148cd248341SJan Glauber { 149cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 150cd248341SJan Glauber } 151cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 152cd248341SJan Glauber 153cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 154cd248341SJan Glauber { 155cd248341SJan Glauber return pci_domain_nr(bus); 156cd248341SJan Glauber } 157cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 158cd248341SJan Glauber 1599a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1609a4da8a5SJan Glauber static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb, 1619a4da8a5SJan Glauber u64 aibv) 1629a4da8a5SJan Glauber { 1639a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 1649a4da8a5SJan Glauber struct zpci_fib *fib; 1659a4da8a5SJan Glauber int rc; 1669a4da8a5SJan Glauber 1679a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 1689a4da8a5SJan Glauber if (!fib) 1699a4da8a5SJan Glauber return -ENOMEM; 1709a4da8a5SJan Glauber 1719a4da8a5SJan Glauber fib->isc = PCI_ISC; 1729a4da8a5SJan Glauber fib->noi = zdev->irq_map->msi_vecs; 1739a4da8a5SJan Glauber fib->sum = 1; /* enable summary notifications */ 1749a4da8a5SJan Glauber fib->aibv = aibv; 1759a4da8a5SJan Glauber fib->aibvo = 0; /* every function has its own page */ 1769a4da8a5SJan Glauber fib->aisb = (u64) bucket->aisb + aisb / 8; 1779a4da8a5SJan Glauber fib->aisbo = aisb & ZPCI_MSI_MASK; 1789a4da8a5SJan Glauber 179b2a9e87dSSebastian Ott rc = s390pci_mod_fc(req, fib); 1809a4da8a5SJan Glauber pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi); 1819a4da8a5SJan Glauber 1829a4da8a5SJan Glauber free_page((unsigned long) fib); 1839a4da8a5SJan Glauber return rc; 1849a4da8a5SJan Glauber } 1859a4da8a5SJan Glauber 1869a4da8a5SJan Glauber struct mod_pci_args { 1879a4da8a5SJan Glauber u64 base; 1889a4da8a5SJan Glauber u64 limit; 1899a4da8a5SJan Glauber u64 iota; 190d0b08853SJan Glauber u64 fmb_addr; 1919a4da8a5SJan Glauber }; 1929a4da8a5SJan Glauber 1939a4da8a5SJan Glauber static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) 1949a4da8a5SJan Glauber { 1959a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); 1969a4da8a5SJan Glauber struct zpci_fib *fib; 1979a4da8a5SJan Glauber int rc; 1989a4da8a5SJan Glauber 1999a4da8a5SJan Glauber /* The FIB must be available even if it's not used */ 2009a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 2019a4da8a5SJan Glauber if (!fib) 2029a4da8a5SJan Glauber return -ENOMEM; 2039a4da8a5SJan Glauber 2049a4da8a5SJan Glauber fib->pba = args->base; 2059a4da8a5SJan Glauber fib->pal = args->limit; 2069a4da8a5SJan Glauber fib->iota = args->iota; 207d0b08853SJan Glauber fib->fmb_addr = args->fmb_addr; 2089a4da8a5SJan Glauber 209b2a9e87dSSebastian Ott rc = s390pci_mod_fc(req, fib); 2109a4da8a5SJan Glauber free_page((unsigned long) fib); 2119a4da8a5SJan Glauber return rc; 2129a4da8a5SJan Glauber } 2139a4da8a5SJan Glauber 214828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 215828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 216828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 217828b35f6SJan Glauber { 218d0b08853SJan Glauber struct mod_pci_args args = { base, limit, iota, 0 }; 219828b35f6SJan Glauber 220828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 221828b35f6SJan Glauber args.iota |= ZPCI_IOTA_RTTO_FLAG; 222828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); 223828b35f6SJan Glauber } 224828b35f6SJan Glauber 225828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 226828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 227828b35f6SJan Glauber { 228d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 229828b35f6SJan Glauber 230828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); 231828b35f6SJan Glauber } 232828b35f6SJan Glauber 2339a4da8a5SJan Glauber /* Modify PCI: Unregister adapter interruptions */ 2349a4da8a5SJan Glauber static int zpci_unregister_airq(struct zpci_dev *zdev) 2359a4da8a5SJan Glauber { 236d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 2379a4da8a5SJan Glauber 2389a4da8a5SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); 2399a4da8a5SJan Glauber } 2409a4da8a5SJan Glauber 241d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 242d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 243d0b08853SJan Glauber { 244d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 245d0b08853SJan Glauber 246d0b08853SJan Glauber if (zdev->fmb) 247d0b08853SJan Glauber return -EINVAL; 248d0b08853SJan Glauber 24908b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 250d0b08853SJan Glauber if (!zdev->fmb) 251d0b08853SJan Glauber return -ENOMEM; 252d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 253d0b08853SJan Glauber 254d0b08853SJan Glauber args.fmb_addr = virt_to_phys(zdev->fmb); 255d0b08853SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 256d0b08853SJan Glauber } 257d0b08853SJan Glauber 258d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 259d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 260d0b08853SJan Glauber { 261d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 262d0b08853SJan Glauber int rc; 263d0b08853SJan Glauber 264d0b08853SJan Glauber if (!zdev->fmb) 265d0b08853SJan Glauber return -EINVAL; 266d0b08853SJan Glauber 267d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 268d0b08853SJan Glauber rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 269d0b08853SJan Glauber 270d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 271d0b08853SJan Glauber zdev->fmb = NULL; 272d0b08853SJan Glauber return rc; 273d0b08853SJan Glauber } 274d0b08853SJan Glauber 275cd248341SJan Glauber #define ZPCI_PCIAS_CFGSPC 15 276cd248341SJan Glauber 277cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 278cd248341SJan Glauber { 279cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 280cd248341SJan Glauber u64 data; 281cd248341SJan Glauber int rc; 282cd248341SJan Glauber 283b2a9e87dSSebastian Ott rc = s390pci_load(&data, req, offset); 284*b170bad4SSebastian Ott if (!rc) { 285cd248341SJan Glauber data = data << ((8 - len) * 8); 286cd248341SJan Glauber data = le64_to_cpu(data); 287cd248341SJan Glauber *val = (u32) data; 288*b170bad4SSebastian Ott } else 289cd248341SJan Glauber *val = 0xffffffff; 290cd248341SJan Glauber return rc; 291cd248341SJan Glauber } 292cd248341SJan Glauber 293cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 294cd248341SJan Glauber { 295cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 296cd248341SJan Glauber u64 data = val; 297cd248341SJan Glauber int rc; 298cd248341SJan Glauber 299cd248341SJan Glauber data = cpu_to_le64(data); 300cd248341SJan Glauber data = data >> ((8 - len) * 8); 301b2a9e87dSSebastian Ott rc = s390pci_store(data, req, offset); 302cd248341SJan Glauber return rc; 303cd248341SJan Glauber } 304cd248341SJan Glauber 3059a4da8a5SJan Glauber void synchronize_irq(unsigned int irq) 3069a4da8a5SJan Glauber { 3079a4da8a5SJan Glauber /* 3089a4da8a5SJan Glauber * Not needed, the handler is protected by a lock and IRQs that occur 3099a4da8a5SJan Glauber * after the handler is deleted are just NOPs. 3109a4da8a5SJan Glauber */ 3119a4da8a5SJan Glauber } 3129a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(synchronize_irq); 3139a4da8a5SJan Glauber 3149a4da8a5SJan Glauber void enable_irq(unsigned int irq) 3159a4da8a5SJan Glauber { 3169a4da8a5SJan Glauber struct msi_desc *msi = irq_get_msi_desc(irq); 3179a4da8a5SJan Glauber 3189a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 0); 3199a4da8a5SJan Glauber } 3209a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(enable_irq); 3219a4da8a5SJan Glauber 3229a4da8a5SJan Glauber void disable_irq(unsigned int irq) 3239a4da8a5SJan Glauber { 3249a4da8a5SJan Glauber struct msi_desc *msi = irq_get_msi_desc(irq); 3259a4da8a5SJan Glauber 3269a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 1); 3279a4da8a5SJan Glauber } 3289a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(disable_irq); 3299a4da8a5SJan Glauber 3309a4da8a5SJan Glauber void disable_irq_nosync(unsigned int irq) 3319a4da8a5SJan Glauber { 3329a4da8a5SJan Glauber disable_irq(irq); 3339a4da8a5SJan Glauber } 3349a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(disable_irq_nosync); 3359a4da8a5SJan Glauber 3369a4da8a5SJan Glauber unsigned long probe_irq_on(void) 3379a4da8a5SJan Glauber { 3389a4da8a5SJan Glauber return 0; 3399a4da8a5SJan Glauber } 3409a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(probe_irq_on); 3419a4da8a5SJan Glauber 3429a4da8a5SJan Glauber int probe_irq_off(unsigned long val) 3439a4da8a5SJan Glauber { 3449a4da8a5SJan Glauber return 0; 3459a4da8a5SJan Glauber } 3469a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(probe_irq_off); 3479a4da8a5SJan Glauber 3489a4da8a5SJan Glauber unsigned int probe_irq_mask(unsigned long val) 3499a4da8a5SJan Glauber { 3509a4da8a5SJan Glauber return val; 3519a4da8a5SJan Glauber } 3529a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(probe_irq_mask); 3539a4da8a5SJan Glauber 354b881bc46SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus) 355cd248341SJan Glauber { 356cd248341SJan Glauber } 357cd248341SJan Glauber 358cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 359cd248341SJan Glauber resource_size_t size, 360cd248341SJan Glauber resource_size_t align) 361cd248341SJan Glauber { 362cd248341SJan Glauber return 0; 363cd248341SJan Glauber } 364cd248341SJan Glauber 36587bc359bSJan Glauber /* combine single writes by using store-block insn */ 36687bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 36787bc359bSJan Glauber { 36887bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 36987bc359bSJan Glauber } 37087bc359bSJan Glauber 371cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 372cd248341SJan Glauber void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max) 373cd248341SJan Glauber { 374cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 375cd248341SJan Glauber u64 addr; 376cd248341SJan Glauber int idx; 377cd248341SJan Glauber 378cd248341SJan Glauber if ((bar & 7) != bar) 379cd248341SJan Glauber return NULL; 380cd248341SJan Glauber 381cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 382cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 383cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 384cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 385cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 386cd248341SJan Glauber 387cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48); 388cd248341SJan Glauber return (void __iomem *) addr; 389cd248341SJan Glauber } 390cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iomap); 391cd248341SJan Glauber 392cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 393cd248341SJan Glauber { 394cd248341SJan Glauber unsigned int idx; 395cd248341SJan Glauber 396cd248341SJan Glauber idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48; 397cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 398cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 399cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 400cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 401cd248341SJan Glauber } 402cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iounmap); 403cd248341SJan Glauber 404cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 405cd248341SJan Glauber int size, u32 *val) 406cd248341SJan Glauber { 407cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 408cd248341SJan Glauber 409cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 410cd248341SJan Glauber return 0; 411cd248341SJan Glauber return zpci_cfg_load(zdev, where, val, size); 412cd248341SJan Glauber } 413cd248341SJan Glauber 414cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 415cd248341SJan Glauber int size, u32 val) 416cd248341SJan Glauber { 417cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 418cd248341SJan Glauber 419cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 420cd248341SJan Glauber return 0; 421cd248341SJan Glauber return zpci_cfg_store(zdev, where, val, size); 422cd248341SJan Glauber } 423cd248341SJan Glauber 424cd248341SJan Glauber static struct pci_ops pci_root_ops = { 425cd248341SJan Glauber .read = pci_read, 426cd248341SJan Glauber .write = pci_write, 427cd248341SJan Glauber }; 428cd248341SJan Glauber 4299a4da8a5SJan Glauber /* store the last handled bit to implement fair scheduling of devices */ 4309a4da8a5SJan Glauber static DEFINE_PER_CPU(unsigned long, next_sbit); 4319a4da8a5SJan Glauber 4329a4da8a5SJan Glauber static void zpci_irq_handler(void *dont, void *need) 4339a4da8a5SJan Glauber { 4349a4da8a5SJan Glauber unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit); 4359a4da8a5SJan Glauber int rescan = 0, max = aisb_max; 4369a4da8a5SJan Glauber struct zdev_irq_map *imap; 4379a4da8a5SJan Glauber 438420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 4399a4da8a5SJan Glauber sbit = start; 4409a4da8a5SJan Glauber 4419a4da8a5SJan Glauber scan: 4429a4da8a5SJan Glauber /* find summary_bit */ 4439a4da8a5SJan Glauber for_each_set_bit_left_cont(sbit, bucket->aisb, max) { 4449a4da8a5SJan Glauber clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6)); 4459a4da8a5SJan Glauber last = sbit; 4469a4da8a5SJan Glauber 4479a4da8a5SJan Glauber /* find vector bit */ 4489a4da8a5SJan Glauber imap = bucket->imap[sbit]; 4499a4da8a5SJan Glauber for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) { 450420f42ecSHeiko Carstens inc_irq_stat(IRQIO_MSI); 4519a4da8a5SJan Glauber clear_bit(63 - mbit, &imap->aibv); 4529a4da8a5SJan Glauber 4539a4da8a5SJan Glauber spin_lock(&imap->lock); 4549a4da8a5SJan Glauber if (imap->cb[mbit].handler) 4559a4da8a5SJan Glauber imap->cb[mbit].handler(mbit, 4569a4da8a5SJan Glauber imap->cb[mbit].data); 4579a4da8a5SJan Glauber spin_unlock(&imap->lock); 4589a4da8a5SJan Glauber } 4599a4da8a5SJan Glauber } 4609a4da8a5SJan Glauber 4619a4da8a5SJan Glauber if (rescan) 4629a4da8a5SJan Glauber goto out; 4639a4da8a5SJan Glauber 4649a4da8a5SJan Glauber /* scan the skipped bits */ 4659a4da8a5SJan Glauber if (start > 0) { 4669a4da8a5SJan Glauber sbit = 0; 4679a4da8a5SJan Glauber max = start; 4689a4da8a5SJan Glauber start = 0; 4699a4da8a5SJan Glauber goto scan; 4709a4da8a5SJan Glauber } 4719a4da8a5SJan Glauber 4729a4da8a5SJan Glauber /* enable interrupts again */ 473b2a9e87dSSebastian Ott set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 4749a4da8a5SJan Glauber 4759a4da8a5SJan Glauber /* check again to not lose initiative */ 4769a4da8a5SJan Glauber rmb(); 4779a4da8a5SJan Glauber max = aisb_max; 4789a4da8a5SJan Glauber sbit = find_first_bit_left(bucket->aisb, max); 4799a4da8a5SJan Glauber if (sbit != max) { 4809a4da8a5SJan Glauber atomic_inc(&irq_retries); 4819a4da8a5SJan Glauber rescan++; 4829a4da8a5SJan Glauber goto scan; 4839a4da8a5SJan Glauber } 4849a4da8a5SJan Glauber out: 4859a4da8a5SJan Glauber /* store next device bit to scan */ 4869a4da8a5SJan Glauber __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last; 4879a4da8a5SJan Glauber } 4889a4da8a5SJan Glauber 4899a4da8a5SJan Glauber /* msi_vecs - number of requested interrupts, 0 place function to error state */ 4909a4da8a5SJan Glauber static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs) 4919a4da8a5SJan Glauber { 4929a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 4939a4da8a5SJan Glauber unsigned int aisb, msi_nr; 4949a4da8a5SJan Glauber struct msi_desc *msi; 4959a4da8a5SJan Glauber int rc; 4969a4da8a5SJan Glauber 4979a4da8a5SJan Glauber /* store the number of used MSI vectors */ 4989a4da8a5SJan Glauber zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS); 4999a4da8a5SJan Glauber 5009a4da8a5SJan Glauber spin_lock(&bucket->lock); 5019a4da8a5SJan Glauber aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE); 5029a4da8a5SJan Glauber /* alloc map exhausted? */ 5039a4da8a5SJan Glauber if (aisb == PAGE_SIZE) { 5049a4da8a5SJan Glauber spin_unlock(&bucket->lock); 5059a4da8a5SJan Glauber return -EIO; 5069a4da8a5SJan Glauber } 5079a4da8a5SJan Glauber set_bit(aisb, bucket->alloc); 5089a4da8a5SJan Glauber spin_unlock(&bucket->lock); 5099a4da8a5SJan Glauber 5109a4da8a5SJan Glauber zdev->aisb = aisb; 5119a4da8a5SJan Glauber if (aisb + 1 > aisb_max) 5129a4da8a5SJan Glauber aisb_max = aisb + 1; 5139a4da8a5SJan Glauber 5149a4da8a5SJan Glauber /* wire up IRQ shortcut pointer */ 5159a4da8a5SJan Glauber bucket->imap[zdev->aisb] = zdev->irq_map; 5169a4da8a5SJan Glauber pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map); 5179a4da8a5SJan Glauber 5189a4da8a5SJan Glauber /* TODO: irq number 0 wont be found if we return less than requested MSIs. 5199a4da8a5SJan Glauber * ignore it for now and fix in common code. 5209a4da8a5SJan Glauber */ 5219a4da8a5SJan Glauber msi_nr = aisb << ZPCI_MSI_VEC_BITS; 5229a4da8a5SJan Glauber 5239a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) { 5249a4da8a5SJan Glauber rc = zpci_setup_msi_irq(zdev, msi, msi_nr, 5259a4da8a5SJan Glauber aisb << ZPCI_MSI_VEC_BITS); 5269a4da8a5SJan Glauber if (rc) 5279a4da8a5SJan Glauber return rc; 5289a4da8a5SJan Glauber msi_nr++; 5299a4da8a5SJan Glauber } 5309a4da8a5SJan Glauber 5319a4da8a5SJan Glauber rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv); 5329a4da8a5SJan Glauber if (rc) { 5339a4da8a5SJan Glauber clear_bit(aisb, bucket->alloc); 5349a4da8a5SJan Glauber dev_err(&pdev->dev, "register MSI failed with: %d\n", rc); 5359a4da8a5SJan Glauber return rc; 5369a4da8a5SJan Glauber } 5379a4da8a5SJan Glauber return (zdev->irq_map->msi_vecs == msi_vecs) ? 5389a4da8a5SJan Glauber 0 : zdev->irq_map->msi_vecs; 5399a4da8a5SJan Glauber } 5409a4da8a5SJan Glauber 5419a4da8a5SJan Glauber static void zpci_teardown_msi(struct pci_dev *pdev) 5429a4da8a5SJan Glauber { 5439a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 5449a4da8a5SJan Glauber struct msi_desc *msi; 5459a4da8a5SJan Glauber int aisb, rc; 5469a4da8a5SJan Glauber 5479a4da8a5SJan Glauber rc = zpci_unregister_airq(zdev); 5489a4da8a5SJan Glauber if (rc) { 5499a4da8a5SJan Glauber dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc); 5509a4da8a5SJan Glauber return; 5519a4da8a5SJan Glauber } 5529a4da8a5SJan Glauber 5539a4da8a5SJan Glauber msi = list_first_entry(&pdev->msi_list, struct msi_desc, list); 5549a4da8a5SJan Glauber aisb = irq_to_dev_nr(msi->irq); 5559a4da8a5SJan Glauber 5569a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) 5579a4da8a5SJan Glauber zpci_teardown_msi_irq(zdev, msi); 5589a4da8a5SJan Glauber 5599a4da8a5SJan Glauber clear_bit(aisb, bucket->alloc); 5609a4da8a5SJan Glauber if (aisb + 1 == aisb_max) 5619a4da8a5SJan Glauber aisb_max--; 5629a4da8a5SJan Glauber } 5639a4da8a5SJan Glauber 5649a4da8a5SJan Glauber int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 5659a4da8a5SJan Glauber { 5669a4da8a5SJan Glauber pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec); 5679a4da8a5SJan Glauber if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI) 5689a4da8a5SJan Glauber return -EINVAL; 5699a4da8a5SJan Glauber return zpci_setup_msi(pdev, nvec); 5709a4da8a5SJan Glauber } 5719a4da8a5SJan Glauber 5729a4da8a5SJan Glauber void arch_teardown_msi_irqs(struct pci_dev *pdev) 5739a4da8a5SJan Glauber { 5749a4da8a5SJan Glauber pr_info("%s: on pdev: %p\n", __func__, pdev); 5759a4da8a5SJan Glauber zpci_teardown_msi(pdev); 5769a4da8a5SJan Glauber } 5779a4da8a5SJan Glauber 578cd248341SJan Glauber static void zpci_map_resources(struct zpci_dev *zdev) 579cd248341SJan Glauber { 580cd248341SJan Glauber struct pci_dev *pdev = zdev->pdev; 581cd248341SJan Glauber resource_size_t len; 582cd248341SJan Glauber int i; 583cd248341SJan Glauber 584cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 585cd248341SJan Glauber len = pci_resource_len(pdev, i); 586cd248341SJan Glauber if (!len) 587cd248341SJan Glauber continue; 588cd248341SJan Glauber pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0); 589cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 590cd248341SJan Glauber pr_debug("BAR%i: -> start: %Lx end: %Lx\n", 591cd248341SJan Glauber i, pdev->resource[i].start, pdev->resource[i].end); 592cd248341SJan Glauber } 593cd248341SJan Glauber }; 594cd248341SJan Glauber 595cd248341SJan Glauber static void zpci_unmap_resources(struct pci_dev *pdev) 596cd248341SJan Glauber { 597cd248341SJan Glauber resource_size_t len; 598cd248341SJan Glauber int i; 599cd248341SJan Glauber 600cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 601cd248341SJan Glauber len = pci_resource_len(pdev, i); 602cd248341SJan Glauber if (!len) 603cd248341SJan Glauber continue; 604cd248341SJan Glauber pci_iounmap(pdev, (void *) pdev->resource[i].start); 605cd248341SJan Glauber } 606cd248341SJan Glauber }; 607cd248341SJan Glauber 608cd248341SJan Glauber struct zpci_dev *zpci_alloc_device(void) 609cd248341SJan Glauber { 610cd248341SJan Glauber struct zpci_dev *zdev; 611cd248341SJan Glauber 612cd248341SJan Glauber /* Alloc memory for our private pci device data */ 613cd248341SJan Glauber zdev = kzalloc(sizeof(*zdev), GFP_KERNEL); 614cd248341SJan Glauber if (!zdev) 615cd248341SJan Glauber return ERR_PTR(-ENOMEM); 6169a4da8a5SJan Glauber 6179a4da8a5SJan Glauber /* Alloc aibv & callback space */ 6184118fee7SWei Yongjun zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL); 6199a4da8a5SJan Glauber if (!zdev->irq_map) 6209a4da8a5SJan Glauber goto error; 6219a4da8a5SJan Glauber WARN_ON((u64) zdev->irq_map & 0xff); 622cd248341SJan Glauber return zdev; 6239a4da8a5SJan Glauber 6249a4da8a5SJan Glauber error: 6259a4da8a5SJan Glauber kfree(zdev); 6269a4da8a5SJan Glauber return ERR_PTR(-ENOMEM); 627cd248341SJan Glauber } 628cd248341SJan Glauber 629cd248341SJan Glauber void zpci_free_device(struct zpci_dev *zdev) 630cd248341SJan Glauber { 6319a4da8a5SJan Glauber kmem_cache_free(zdev_irq_cache, zdev->irq_map); 632cd248341SJan Glauber kfree(zdev); 633cd248341SJan Glauber } 634cd248341SJan Glauber 635cd248341SJan Glauber /* Called on removal of pci_dev, leaves zpci and bus device */ 636cd248341SJan Glauber static void zpci_remove_device(struct pci_dev *pdev) 637cd248341SJan Glauber { 638cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 639cd248341SJan Glauber 640cd248341SJan Glauber dev_info(&pdev->dev, "Removing device %u\n", zdev->domain); 641cd248341SJan Glauber zdev->state = ZPCI_FN_STATE_CONFIGURED; 642828b35f6SJan Glauber zpci_dma_exit_device(zdev); 643d0b08853SJan Glauber zpci_fmb_disable_device(zdev); 6441e8da956SJan Glauber zpci_sysfs_remove_device(&pdev->dev); 645cd248341SJan Glauber zpci_unmap_resources(pdev); 646cd248341SJan Glauber list_del(&zdev->entry); /* can be called from init */ 647cd248341SJan Glauber zdev->pdev = NULL; 648cd248341SJan Glauber } 649cd248341SJan Glauber 650cd248341SJan Glauber static void zpci_scan_devices(void) 651cd248341SJan Glauber { 652cd248341SJan Glauber struct zpci_dev *zdev; 653cd248341SJan Glauber 654cd248341SJan Glauber mutex_lock(&zpci_list_lock); 655cd248341SJan Glauber list_for_each_entry(zdev, &zpci_list, entry) 656cd248341SJan Glauber if (zdev->state == ZPCI_FN_STATE_CONFIGURED) 657cd248341SJan Glauber zpci_scan_device(zdev); 658cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 659cd248341SJan Glauber } 660cd248341SJan Glauber 661cd248341SJan Glauber /* 662cd248341SJan Glauber * Too late for any s390 specific setup, since interrupts must be set up 663cd248341SJan Glauber * already which requires DMA setup too and the pci scan will access the 664cd248341SJan Glauber * config space, which only works if the function handle is enabled. 665cd248341SJan Glauber */ 666cd248341SJan Glauber int pcibios_enable_device(struct pci_dev *pdev, int mask) 667cd248341SJan Glauber { 668cd248341SJan Glauber struct resource *res; 669cd248341SJan Glauber u16 cmd; 670cd248341SJan Glauber int i; 671cd248341SJan Glauber 672cd248341SJan Glauber pci_read_config_word(pdev, PCI_COMMAND, &cmd); 673cd248341SJan Glauber 674cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 675cd248341SJan Glauber res = &pdev->resource[i]; 676cd248341SJan Glauber 677cd248341SJan Glauber if (res->flags & IORESOURCE_IO) 678cd248341SJan Glauber return -EINVAL; 679cd248341SJan Glauber 680cd248341SJan Glauber if (res->flags & IORESOURCE_MEM) 681cd248341SJan Glauber cmd |= PCI_COMMAND_MEMORY; 682cd248341SJan Glauber } 683cd248341SJan Glauber pci_write_config_word(pdev, PCI_COMMAND, cmd); 684cd248341SJan Glauber return 0; 685cd248341SJan Glauber } 686cd248341SJan Glauber 687cd248341SJan Glauber void pcibios_disable_device(struct pci_dev *pdev) 688cd248341SJan Glauber { 689cd248341SJan Glauber zpci_remove_device(pdev); 690cd248341SJan Glauber pdev->sysdata = NULL; 691cd248341SJan Glauber } 692cd248341SJan Glauber 6931e8da956SJan Glauber int pcibios_add_platform_entries(struct pci_dev *pdev) 6941e8da956SJan Glauber { 6951e8da956SJan Glauber return zpci_sysfs_add_device(&pdev->dev); 6961e8da956SJan Glauber } 6971e8da956SJan Glauber 6989a4da8a5SJan Glauber int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data) 6999a4da8a5SJan Glauber { 7009a4da8a5SJan Glauber int msi_nr = irq_to_msi_nr(irq); 7019a4da8a5SJan Glauber struct zdev_irq_map *imap; 7029a4da8a5SJan Glauber struct msi_desc *msi; 7039a4da8a5SJan Glauber 7049a4da8a5SJan Glauber msi = irq_get_msi_desc(irq); 7059a4da8a5SJan Glauber if (!msi) 7069a4da8a5SJan Glauber return -EIO; 7079a4da8a5SJan Glauber 7089a4da8a5SJan Glauber imap = get_imap(irq); 7099a4da8a5SJan Glauber spin_lock_init(&imap->lock); 7109a4da8a5SJan Glauber 7119a4da8a5SJan Glauber pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr); 7129a4da8a5SJan Glauber imap->cb[msi_nr].handler = handler; 7139a4da8a5SJan Glauber imap->cb[msi_nr].data = data; 7149a4da8a5SJan Glauber 7159a4da8a5SJan Glauber /* 7169a4da8a5SJan Glauber * The generic MSI code returns with the interrupt disabled on the 7179a4da8a5SJan Glauber * card, using the MSI mask bits. Firmware doesn't appear to unmask 7189a4da8a5SJan Glauber * at that level, so we do it here by hand. 7199a4da8a5SJan Glauber */ 7209a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 0); 7219a4da8a5SJan Glauber return 0; 7229a4da8a5SJan Glauber } 7239a4da8a5SJan Glauber 7249a4da8a5SJan Glauber void zpci_free_irq(unsigned int irq) 7259a4da8a5SJan Glauber { 7269a4da8a5SJan Glauber struct zdev_irq_map *imap = get_imap(irq); 7279a4da8a5SJan Glauber int msi_nr = irq_to_msi_nr(irq); 7289a4da8a5SJan Glauber unsigned long flags; 7299a4da8a5SJan Glauber 7309a4da8a5SJan Glauber pr_debug("%s: for irq: %d\n", __func__, irq); 7319a4da8a5SJan Glauber 7329a4da8a5SJan Glauber spin_lock_irqsave(&imap->lock, flags); 7339a4da8a5SJan Glauber imap->cb[msi_nr].handler = NULL; 7349a4da8a5SJan Glauber imap->cb[msi_nr].data = NULL; 7359a4da8a5SJan Glauber spin_unlock_irqrestore(&imap->lock, flags); 7369a4da8a5SJan Glauber } 7379a4da8a5SJan Glauber 7389a4da8a5SJan Glauber int request_irq(unsigned int irq, irq_handler_t handler, 7399a4da8a5SJan Glauber unsigned long irqflags, const char *devname, void *dev_id) 7409a4da8a5SJan Glauber { 7419a4da8a5SJan Glauber pr_debug("%s: irq: %d handler: %p flags: %lx dev: %s\n", 7429a4da8a5SJan Glauber __func__, irq, handler, irqflags, devname); 7439a4da8a5SJan Glauber 7449a4da8a5SJan Glauber return zpci_request_irq(irq, handler, dev_id); 7459a4da8a5SJan Glauber } 7469a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(request_irq); 7479a4da8a5SJan Glauber 7489a4da8a5SJan Glauber void free_irq(unsigned int irq, void *dev_id) 7499a4da8a5SJan Glauber { 7509a4da8a5SJan Glauber zpci_free_irq(irq); 7519a4da8a5SJan Glauber } 7529a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(free_irq); 7539a4da8a5SJan Glauber 7549a4da8a5SJan Glauber static int __init zpci_irq_init(void) 7559a4da8a5SJan Glauber { 7569a4da8a5SJan Glauber int cpu, rc; 7579a4da8a5SJan Glauber 7589a4da8a5SJan Glauber bucket = kzalloc(sizeof(*bucket), GFP_KERNEL); 7599a4da8a5SJan Glauber if (!bucket) 7609a4da8a5SJan Glauber return -ENOMEM; 7619a4da8a5SJan Glauber 7629a4da8a5SJan Glauber bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL); 7639a4da8a5SJan Glauber if (!bucket->aisb) { 7649a4da8a5SJan Glauber rc = -ENOMEM; 7659a4da8a5SJan Glauber goto out_aisb; 7669a4da8a5SJan Glauber } 7679a4da8a5SJan Glauber 7689a4da8a5SJan Glauber bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL); 7699a4da8a5SJan Glauber if (!bucket->alloc) { 7709a4da8a5SJan Glauber rc = -ENOMEM; 7719a4da8a5SJan Glauber goto out_alloc; 7729a4da8a5SJan Glauber } 7739a4da8a5SJan Glauber 7749a4da8a5SJan Glauber isc_register(PCI_ISC); 7759a4da8a5SJan Glauber zpci_irq_si = s390_register_adapter_interrupt(&zpci_irq_handler, NULL, PCI_ISC); 7769a4da8a5SJan Glauber if (IS_ERR(zpci_irq_si)) { 7779a4da8a5SJan Glauber rc = PTR_ERR(zpci_irq_si); 7789a4da8a5SJan Glauber zpci_irq_si = NULL; 7799a4da8a5SJan Glauber goto out_ai; 7809a4da8a5SJan Glauber } 7819a4da8a5SJan Glauber 7829a4da8a5SJan Glauber for_each_online_cpu(cpu) 7839a4da8a5SJan Glauber per_cpu(next_sbit, cpu) = 0; 7849a4da8a5SJan Glauber 7859a4da8a5SJan Glauber spin_lock_init(&bucket->lock); 7869a4da8a5SJan Glauber /* set summary to 1 to be called every time for the ISC */ 7879a4da8a5SJan Glauber *zpci_irq_si = 1; 788b2a9e87dSSebastian Ott set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 7899a4da8a5SJan Glauber return 0; 7909a4da8a5SJan Glauber 7919a4da8a5SJan Glauber out_ai: 7929a4da8a5SJan Glauber isc_unregister(PCI_ISC); 7939a4da8a5SJan Glauber free_page((unsigned long) bucket->alloc); 7949a4da8a5SJan Glauber out_alloc: 7959a4da8a5SJan Glauber free_page((unsigned long) bucket->aisb); 7969a4da8a5SJan Glauber out_aisb: 7979a4da8a5SJan Glauber kfree(bucket); 7989a4da8a5SJan Glauber return rc; 7999a4da8a5SJan Glauber } 8009a4da8a5SJan Glauber 8019a4da8a5SJan Glauber static void zpci_irq_exit(void) 8029a4da8a5SJan Glauber { 8039a4da8a5SJan Glauber free_page((unsigned long) bucket->alloc); 8049a4da8a5SJan Glauber free_page((unsigned long) bucket->aisb); 8059a4da8a5SJan Glauber s390_unregister_adapter_interrupt(zpci_irq_si, PCI_ISC); 8069a4da8a5SJan Glauber isc_unregister(PCI_ISC); 8079a4da8a5SJan Glauber kfree(bucket); 8089a4da8a5SJan Glauber } 8099a4da8a5SJan Glauber 810d0b08853SJan Glauber void zpci_debug_info(struct zpci_dev *zdev, struct seq_file *m) 811d0b08853SJan Glauber { 812d0b08853SJan Glauber if (!zdev) 813d0b08853SJan Glauber return; 814d0b08853SJan Glauber 815d0b08853SJan Glauber seq_printf(m, "global irq retries: %u\n", atomic_read(&irq_retries)); 816d0b08853SJan Glauber seq_printf(m, "aibv[0]:%016lx aibv[1]:%016lx aisb:%016lx\n", 817d0b08853SJan Glauber get_imap(0)->aibv, get_imap(1)->aibv, *bucket->aisb); 818d0b08853SJan Glauber } 819d0b08853SJan Glauber 820cd248341SJan Glauber static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size, 821cd248341SJan Glauber unsigned long flags, int domain) 822cd248341SJan Glauber { 823cd248341SJan Glauber struct resource *r; 824cd248341SJan Glauber char *name; 825cd248341SJan Glauber int rc; 826cd248341SJan Glauber 827cd248341SJan Glauber r = kzalloc(sizeof(*r), GFP_KERNEL); 828cd248341SJan Glauber if (!r) 829cd248341SJan Glauber return ERR_PTR(-ENOMEM); 830cd248341SJan Glauber r->start = start; 831cd248341SJan Glauber r->end = r->start + size - 1; 832cd248341SJan Glauber r->flags = flags; 833cd248341SJan Glauber r->parent = &iomem_resource; 834cd248341SJan Glauber name = kmalloc(18, GFP_KERNEL); 835cd248341SJan Glauber if (!name) { 836cd248341SJan Glauber kfree(r); 837cd248341SJan Glauber return ERR_PTR(-ENOMEM); 838cd248341SJan Glauber } 839cd248341SJan Glauber sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR); 840cd248341SJan Glauber r->name = name; 841cd248341SJan Glauber 842cd248341SJan Glauber rc = request_resource(&iomem_resource, r); 843cd248341SJan Glauber if (rc) 844cd248341SJan Glauber pr_debug("request resource %pR failed\n", r); 845cd248341SJan Glauber return r; 846cd248341SJan Glauber } 847cd248341SJan Glauber 848cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 849cd248341SJan Glauber { 850cd248341SJan Glauber int entry; 851cd248341SJan Glauber 852cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 853cd248341SJan Glauber entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 854cd248341SJan Glauber if (entry == ZPCI_IOMAP_MAX_ENTRIES) { 855cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 856cd248341SJan Glauber return -ENOSPC; 857cd248341SJan Glauber } 858cd248341SJan Glauber set_bit(entry, zpci_iomap); 859cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 860cd248341SJan Glauber return entry; 861cd248341SJan Glauber } 862cd248341SJan Glauber 863cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 864cd248341SJan Glauber { 865cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 866cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 867cd248341SJan Glauber clear_bit(entry, zpci_iomap); 868cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 869cd248341SJan Glauber } 870cd248341SJan Glauber 871af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 872af0a8a84SSebastian Ott { 873af0a8a84SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 874af0a8a84SSebastian Ott 875af0a8a84SSebastian Ott zpci_debug_init_device(zdev); 876af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 877af0a8a84SSebastian Ott zpci_map_resources(zdev); 878af0a8a84SSebastian Ott 879af0a8a84SSebastian Ott return 0; 880af0a8a84SSebastian Ott } 881af0a8a84SSebastian Ott 882cd248341SJan Glauber static int zpci_create_device_bus(struct zpci_dev *zdev) 883cd248341SJan Glauber { 884cd248341SJan Glauber struct resource *res; 885cd248341SJan Glauber LIST_HEAD(resources); 886cd248341SJan Glauber int i; 887cd248341SJan Glauber 888cd248341SJan Glauber /* allocate mapping entry for each used bar */ 889cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 890cd248341SJan Glauber unsigned long addr, size, flags; 891cd248341SJan Glauber int entry; 892cd248341SJan Glauber 893cd248341SJan Glauber if (!zdev->bars[i].size) 894cd248341SJan Glauber continue; 895cd248341SJan Glauber entry = zpci_alloc_iomap(zdev); 896cd248341SJan Glauber if (entry < 0) 897cd248341SJan Glauber return entry; 898cd248341SJan Glauber zdev->bars[i].map_idx = entry; 899cd248341SJan Glauber 900cd248341SJan Glauber /* only MMIO is supported */ 901cd248341SJan Glauber flags = IORESOURCE_MEM; 902cd248341SJan Glauber if (zdev->bars[i].val & 8) 903cd248341SJan Glauber flags |= IORESOURCE_PREFETCH; 904cd248341SJan Glauber if (zdev->bars[i].val & 4) 905cd248341SJan Glauber flags |= IORESOURCE_MEM_64; 906cd248341SJan Glauber 907cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48); 908cd248341SJan Glauber 909cd248341SJan Glauber size = 1UL << zdev->bars[i].size; 910cd248341SJan Glauber 911cd248341SJan Glauber res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain); 912cd248341SJan Glauber if (IS_ERR(res)) { 913cd248341SJan Glauber zpci_free_iomap(zdev, entry); 914cd248341SJan Glauber return PTR_ERR(res); 915cd248341SJan Glauber } 916cd248341SJan Glauber pci_add_resource(&resources, res); 917cd248341SJan Glauber } 918cd248341SJan Glauber 919cd248341SJan Glauber zdev->bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 920cd248341SJan Glauber zdev, &resources); 921cd248341SJan Glauber if (!zdev->bus) 922cd248341SJan Glauber return -EIO; 923cd248341SJan Glauber 924cd248341SJan Glauber zdev->bus->max_bus_speed = zdev->max_bus_speed; 925cd248341SJan Glauber return 0; 926cd248341SJan Glauber } 927cd248341SJan Glauber 928cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 929cd248341SJan Glauber { 930cd248341SJan Glauber spin_lock(&zpci_domain_lock); 931cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 932cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 933cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 934cd248341SJan Glauber return -ENOSPC; 935cd248341SJan Glauber } 936cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 937cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 938cd248341SJan Glauber return 0; 939cd248341SJan Glauber } 940cd248341SJan Glauber 941cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 942cd248341SJan Glauber { 943cd248341SJan Glauber spin_lock(&zpci_domain_lock); 944cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 945cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 946cd248341SJan Glauber } 947cd248341SJan Glauber 948a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 949a755a45dSJan Glauber { 950a755a45dSJan Glauber int rc; 951a755a45dSJan Glauber 952a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 953a755a45dSJan Glauber if (rc) 954a755a45dSJan Glauber goto out; 955a755a45dSJan Glauber pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid); 956828b35f6SJan Glauber 957828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 958828b35f6SJan Glauber if (rc) 959828b35f6SJan Glauber goto out_dma; 960a755a45dSJan Glauber return 0; 961828b35f6SJan Glauber 962828b35f6SJan Glauber out_dma: 963828b35f6SJan Glauber clp_disable_fh(zdev); 964a755a45dSJan Glauber out: 965a755a45dSJan Glauber return rc; 966a755a45dSJan Glauber } 967a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 968a755a45dSJan Glauber 969cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 970cb65a669SSebastian Ott { 971cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 972cb65a669SSebastian Ott return clp_disable_fh(zdev); 973cb65a669SSebastian Ott } 974cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 975cb65a669SSebastian Ott 976cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 977cd248341SJan Glauber { 978cd248341SJan Glauber int rc; 979cd248341SJan Glauber 980cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 981cd248341SJan Glauber if (rc) 982cd248341SJan Glauber goto out; 983cd248341SJan Glauber 984cd248341SJan Glauber rc = zpci_create_device_bus(zdev); 985cd248341SJan Glauber if (rc) 986cd248341SJan Glauber goto out_bus; 987cd248341SJan Glauber 988cd248341SJan Glauber mutex_lock(&zpci_list_lock); 989cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 99053923354SSebastian Ott if (hotplug_ops) 99153923354SSebastian Ott hotplug_ops->create_slot(zdev); 992cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 993cd248341SJan Glauber 994cd248341SJan Glauber if (zdev->state == ZPCI_FN_STATE_STANDBY) 995cd248341SJan Glauber return 0; 996cd248341SJan Glauber 997a755a45dSJan Glauber rc = zpci_enable_device(zdev); 998a755a45dSJan Glauber if (rc) 999a755a45dSJan Glauber goto out_start; 1000cd248341SJan Glauber return 0; 1001cd248341SJan Glauber 1002a755a45dSJan Glauber out_start: 1003a755a45dSJan Glauber mutex_lock(&zpci_list_lock); 1004a755a45dSJan Glauber list_del(&zdev->entry); 100553923354SSebastian Ott if (hotplug_ops) 100653923354SSebastian Ott hotplug_ops->remove_slot(zdev); 1007a755a45dSJan Glauber mutex_unlock(&zpci_list_lock); 1008cd248341SJan Glauber out_bus: 1009cd248341SJan Glauber zpci_free_domain(zdev); 1010cd248341SJan Glauber out: 1011cd248341SJan Glauber return rc; 1012cd248341SJan Glauber } 1013cd248341SJan Glauber 1014cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 1015cd248341SJan Glauber { 1016828b35f6SJan Glauber zpci_dma_exit_device(zdev); 1017cd248341SJan Glauber /* 1018cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 1019cd248341SJan Glauber * do that here. 1020cd248341SJan Glauber */ 1021cd248341SJan Glauber } 1022cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 1023cd248341SJan Glauber 1024cd248341SJan Glauber int zpci_scan_device(struct zpci_dev *zdev) 1025cd248341SJan Glauber { 1026cd248341SJan Glauber zdev->pdev = pci_scan_single_device(zdev->bus, ZPCI_DEVFN); 1027cd248341SJan Glauber if (!zdev->pdev) { 1028cd248341SJan Glauber pr_err("pci_scan_single_device failed for fid: 0x%x\n", 1029cd248341SJan Glauber zdev->fid); 1030cd248341SJan Glauber goto out; 1031cd248341SJan Glauber } 1032cd248341SJan Glauber 1033cd248341SJan Glauber pci_bus_add_devices(zdev->bus); 1034cd248341SJan Glauber 1035cd248341SJan Glauber /* now that pdev was added to the bus mark it as used */ 1036cd248341SJan Glauber zdev->state = ZPCI_FN_STATE_ONLINE; 1037cd248341SJan Glauber return 0; 1038cd248341SJan Glauber 1039cd248341SJan Glauber out: 1040828b35f6SJan Glauber zpci_dma_exit_device(zdev); 1041a755a45dSJan Glauber clp_disable_fh(zdev); 1042cd248341SJan Glauber return -EIO; 1043cd248341SJan Glauber } 1044cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_scan_device); 1045cd248341SJan Glauber 1046cd248341SJan Glauber static inline int barsize(u8 size) 1047cd248341SJan Glauber { 1048cd248341SJan Glauber return (size) ? (1 << size) >> 10 : 0; 1049cd248341SJan Glauber } 1050cd248341SJan Glauber 1051cd248341SJan Glauber static int zpci_mem_init(void) 1052cd248341SJan Glauber { 10539a4da8a5SJan Glauber zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map), 10549a4da8a5SJan Glauber L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL); 10559a4da8a5SJan Glauber if (!zdev_irq_cache) 10569a4da8a5SJan Glauber goto error_zdev; 10579a4da8a5SJan Glauber 1058d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 1059d0b08853SJan Glauber 16, 0, NULL); 1060d0b08853SJan Glauber if (!zdev_fmb_cache) 1061d0b08853SJan Glauber goto error_fmb; 1062d0b08853SJan Glauber 1063cd248341SJan Glauber /* TODO: use realloc */ 1064cd248341SJan Glauber zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start), 1065cd248341SJan Glauber GFP_KERNEL); 1066cd248341SJan Glauber if (!zpci_iomap_start) 10679a4da8a5SJan Glauber goto error_iomap; 1068cd248341SJan Glauber return 0; 1069cd248341SJan Glauber 10709a4da8a5SJan Glauber error_iomap: 1071d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 1072d0b08853SJan Glauber error_fmb: 10739a4da8a5SJan Glauber kmem_cache_destroy(zdev_irq_cache); 1074cd248341SJan Glauber error_zdev: 1075cd248341SJan Glauber return -ENOMEM; 1076cd248341SJan Glauber } 1077cd248341SJan Glauber 1078cd248341SJan Glauber static void zpci_mem_exit(void) 1079cd248341SJan Glauber { 1080cd248341SJan Glauber kfree(zpci_iomap_start); 10819a4da8a5SJan Glauber kmem_cache_destroy(zdev_irq_cache); 1082d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 1083cd248341SJan Glauber } 1084cd248341SJan Glauber 108553923354SSebastian Ott void zpci_register_hp_ops(struct pci_hp_callback_ops *ops) 108653923354SSebastian Ott { 108753923354SSebastian Ott mutex_lock(&zpci_list_lock); 108853923354SSebastian Ott hotplug_ops = ops; 108953923354SSebastian Ott mutex_unlock(&zpci_list_lock); 109053923354SSebastian Ott } 109153923354SSebastian Ott EXPORT_SYMBOL_GPL(zpci_register_hp_ops); 109253923354SSebastian Ott 109353923354SSebastian Ott void zpci_deregister_hp_ops(void) 109453923354SSebastian Ott { 109553923354SSebastian Ott mutex_lock(&zpci_list_lock); 109653923354SSebastian Ott hotplug_ops = NULL; 109753923354SSebastian Ott mutex_unlock(&zpci_list_lock); 109853923354SSebastian Ott } 109953923354SSebastian Ott EXPORT_SYMBOL_GPL(zpci_deregister_hp_ops); 110053923354SSebastian Ott 11011e5635d1SHeiko Carstens unsigned int s390_pci_probe = 1; 11021e5635d1SHeiko Carstens EXPORT_SYMBOL_GPL(s390_pci_probe); 1103cd248341SJan Glauber 1104cd248341SJan Glauber char * __init pcibios_setup(char *str) 1105cd248341SJan Glauber { 1106cd248341SJan Glauber if (!strcmp(str, "off")) { 11071e5635d1SHeiko Carstens s390_pci_probe = 0; 1108cd248341SJan Glauber return NULL; 1109cd248341SJan Glauber } 1110cd248341SJan Glauber return str; 1111cd248341SJan Glauber } 1112cd248341SJan Glauber 1113cd248341SJan Glauber static int __init pci_base_init(void) 1114cd248341SJan Glauber { 1115cd248341SJan Glauber int rc; 1116cd248341SJan Glauber 11171e5635d1SHeiko Carstens if (!s390_pci_probe) 1118cd248341SJan Glauber return 0; 1119cd248341SJan Glauber 1120cd248341SJan Glauber if (!test_facility(2) || !test_facility(69) 1121cd248341SJan Glauber || !test_facility(71) || !test_facility(72)) 1122cd248341SJan Glauber return 0; 1123cd248341SJan Glauber 1124cd248341SJan Glauber pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n", 1125cd248341SJan Glauber test_facility(69), test_facility(70), 1126cd248341SJan Glauber test_facility(71)); 1127cd248341SJan Glauber 1128d0b08853SJan Glauber rc = zpci_debug_init(); 1129d0b08853SJan Glauber if (rc) 1130d0b08853SJan Glauber return rc; 1131d0b08853SJan Glauber 1132cd248341SJan Glauber rc = zpci_mem_init(); 1133cd248341SJan Glauber if (rc) 1134cd248341SJan Glauber goto out_mem; 1135cd248341SJan Glauber 11369a4da8a5SJan Glauber rc = zpci_msihash_init(); 11379a4da8a5SJan Glauber if (rc) 11389a4da8a5SJan Glauber goto out_hash; 11399a4da8a5SJan Glauber 11409a4da8a5SJan Glauber rc = zpci_irq_init(); 11419a4da8a5SJan Glauber if (rc) 11429a4da8a5SJan Glauber goto out_irq; 11439a4da8a5SJan Glauber 1144828b35f6SJan Glauber rc = zpci_dma_init(); 1145828b35f6SJan Glauber if (rc) 1146828b35f6SJan Glauber goto out_dma; 1147828b35f6SJan Glauber 1148a755a45dSJan Glauber rc = clp_find_pci_devices(); 1149a755a45dSJan Glauber if (rc) 1150a755a45dSJan Glauber goto out_find; 1151a755a45dSJan Glauber 1152cd248341SJan Glauber zpci_scan_devices(); 1153cd248341SJan Glauber return 0; 1154cd248341SJan Glauber 1155a755a45dSJan Glauber out_find: 1156828b35f6SJan Glauber zpci_dma_exit(); 1157828b35f6SJan Glauber out_dma: 11589a4da8a5SJan Glauber zpci_irq_exit(); 11599a4da8a5SJan Glauber out_irq: 11609a4da8a5SJan Glauber zpci_msihash_exit(); 11619a4da8a5SJan Glauber out_hash: 1162cd248341SJan Glauber zpci_mem_exit(); 1163cd248341SJan Glauber out_mem: 1164d0b08853SJan Glauber zpci_debug_exit(); 1165cd248341SJan Glauber return rc; 1166cd248341SJan Glauber } 1167cd248341SJan Glauber subsys_initcall(pci_base_init); 1168