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 102d0b08853SJan Glauber debug_info_t *pci_debug_msg_id; 103d0b08853SJan Glauber debug_info_t *pci_debug_err_id; 1049a4da8a5SJan Glauber 1059a4da8a5SJan Glauber static inline int irq_to_msi_nr(unsigned int irq) 1069a4da8a5SJan Glauber { 1079a4da8a5SJan Glauber return irq & ZPCI_MSI_MASK; 1089a4da8a5SJan Glauber } 1099a4da8a5SJan Glauber 1109a4da8a5SJan Glauber static inline int irq_to_dev_nr(unsigned int irq) 1119a4da8a5SJan Glauber { 1129a4da8a5SJan Glauber return irq >> ZPCI_MSI_VEC_BITS; 1139a4da8a5SJan Glauber } 1149a4da8a5SJan Glauber 1159a4da8a5SJan Glauber static inline struct zdev_irq_map *get_imap(unsigned int irq) 1169a4da8a5SJan Glauber { 1179a4da8a5SJan Glauber return bucket->imap[irq_to_dev_nr(irq)]; 1189a4da8a5SJan Glauber } 1199a4da8a5SJan Glauber 120cd248341SJan Glauber struct zpci_dev *get_zdev(struct pci_dev *pdev) 121cd248341SJan Glauber { 122cd248341SJan Glauber return (struct zpci_dev *) pdev->sysdata; 123cd248341SJan Glauber } 124cd248341SJan Glauber 125cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 126cd248341SJan Glauber { 127cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 128cd248341SJan Glauber 129cd248341SJan Glauber mutex_lock(&zpci_list_lock); 130cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 131cd248341SJan Glauber if (tmp->fid == fid) { 132cd248341SJan Glauber zdev = tmp; 133cd248341SJan Glauber break; 134cd248341SJan Glauber } 135cd248341SJan Glauber } 136cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 137cd248341SJan Glauber return zdev; 138cd248341SJan Glauber } 139cd248341SJan Glauber 140cd248341SJan Glauber bool zpci_fid_present(u32 fid) 141cd248341SJan Glauber { 142cd248341SJan Glauber return (get_zdev_by_fid(fid) != NULL) ? true : false; 143cd248341SJan Glauber } 144cd248341SJan Glauber 145cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 146cd248341SJan Glauber { 147cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 148cd248341SJan Glauber } 149cd248341SJan Glauber 150cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 151cd248341SJan Glauber { 152cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 153cd248341SJan Glauber } 154cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 155cd248341SJan Glauber 156cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 157cd248341SJan Glauber { 158cd248341SJan Glauber return pci_domain_nr(bus); 159cd248341SJan Glauber } 160cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 161cd248341SJan Glauber 1629a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1639a4da8a5SJan Glauber static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb, 1649a4da8a5SJan Glauber u64 aibv) 1659a4da8a5SJan Glauber { 1669a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 1679a4da8a5SJan Glauber struct zpci_fib *fib; 1689a4da8a5SJan Glauber int rc; 1699a4da8a5SJan Glauber 1709a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 1719a4da8a5SJan Glauber if (!fib) 1729a4da8a5SJan Glauber return -ENOMEM; 1739a4da8a5SJan Glauber 1749a4da8a5SJan Glauber fib->isc = PCI_ISC; 1759a4da8a5SJan Glauber fib->noi = zdev->irq_map->msi_vecs; 1769a4da8a5SJan Glauber fib->sum = 1; /* enable summary notifications */ 1779a4da8a5SJan Glauber fib->aibv = aibv; 1789a4da8a5SJan Glauber fib->aibvo = 0; /* every function has its own page */ 1799a4da8a5SJan Glauber fib->aisb = (u64) bucket->aisb + aisb / 8; 1809a4da8a5SJan Glauber fib->aisbo = aisb & ZPCI_MSI_MASK; 1819a4da8a5SJan Glauber 1829a4da8a5SJan Glauber rc = mpcifc_instr(req, fib); 1839a4da8a5SJan Glauber pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi); 1849a4da8a5SJan Glauber 1859a4da8a5SJan Glauber free_page((unsigned long) fib); 1869a4da8a5SJan Glauber return rc; 1879a4da8a5SJan Glauber } 1889a4da8a5SJan Glauber 1899a4da8a5SJan Glauber struct mod_pci_args { 1909a4da8a5SJan Glauber u64 base; 1919a4da8a5SJan Glauber u64 limit; 1929a4da8a5SJan Glauber u64 iota; 193d0b08853SJan Glauber u64 fmb_addr; 1949a4da8a5SJan Glauber }; 1959a4da8a5SJan Glauber 1969a4da8a5SJan Glauber static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) 1979a4da8a5SJan Glauber { 1989a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); 1999a4da8a5SJan Glauber struct zpci_fib *fib; 2009a4da8a5SJan Glauber int rc; 2019a4da8a5SJan Glauber 2029a4da8a5SJan Glauber /* The FIB must be available even if it's not used */ 2039a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 2049a4da8a5SJan Glauber if (!fib) 2059a4da8a5SJan Glauber return -ENOMEM; 2069a4da8a5SJan Glauber 2079a4da8a5SJan Glauber fib->pba = args->base; 2089a4da8a5SJan Glauber fib->pal = args->limit; 2099a4da8a5SJan Glauber fib->iota = args->iota; 210d0b08853SJan Glauber fib->fmb_addr = args->fmb_addr; 2119a4da8a5SJan Glauber 2129a4da8a5SJan Glauber rc = mpcifc_instr(req, fib); 2139a4da8a5SJan Glauber free_page((unsigned long) fib); 2149a4da8a5SJan Glauber return rc; 2159a4da8a5SJan Glauber } 2169a4da8a5SJan Glauber 217828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 218828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 219828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 220828b35f6SJan Glauber { 221d0b08853SJan Glauber struct mod_pci_args args = { base, limit, iota, 0 }; 222828b35f6SJan Glauber 223828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 224828b35f6SJan Glauber args.iota |= ZPCI_IOTA_RTTO_FLAG; 225828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); 226828b35f6SJan Glauber } 227828b35f6SJan Glauber 228828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 229828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 230828b35f6SJan Glauber { 231d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 232828b35f6SJan Glauber 233828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); 234828b35f6SJan Glauber } 235828b35f6SJan Glauber 2369a4da8a5SJan Glauber /* Modify PCI: Unregister adapter interruptions */ 2379a4da8a5SJan Glauber static int zpci_unregister_airq(struct zpci_dev *zdev) 2389a4da8a5SJan Glauber { 239d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 2409a4da8a5SJan Glauber 2419a4da8a5SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); 2429a4da8a5SJan Glauber } 2439a4da8a5SJan Glauber 244d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 245d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 246d0b08853SJan Glauber { 247d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 248d0b08853SJan Glauber 249d0b08853SJan Glauber if (zdev->fmb) 250d0b08853SJan Glauber return -EINVAL; 251d0b08853SJan Glauber 252*08b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 253d0b08853SJan Glauber if (!zdev->fmb) 254d0b08853SJan Glauber return -ENOMEM; 255d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 256d0b08853SJan Glauber 257d0b08853SJan Glauber args.fmb_addr = virt_to_phys(zdev->fmb); 258d0b08853SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 259d0b08853SJan Glauber } 260d0b08853SJan Glauber 261d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 262d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 263d0b08853SJan Glauber { 264d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 265d0b08853SJan Glauber int rc; 266d0b08853SJan Glauber 267d0b08853SJan Glauber if (!zdev->fmb) 268d0b08853SJan Glauber return -EINVAL; 269d0b08853SJan Glauber 270d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 271d0b08853SJan Glauber rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 272d0b08853SJan Glauber 273d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 274d0b08853SJan Glauber zdev->fmb = NULL; 275d0b08853SJan Glauber return rc; 276d0b08853SJan Glauber } 277d0b08853SJan Glauber 278cd248341SJan Glauber #define ZPCI_PCIAS_CFGSPC 15 279cd248341SJan Glauber 280cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 281cd248341SJan Glauber { 282cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 283cd248341SJan Glauber u64 data; 284cd248341SJan Glauber int rc; 285cd248341SJan Glauber 286cd248341SJan Glauber rc = pcilg_instr(&data, req, offset); 287cd248341SJan Glauber data = data << ((8 - len) * 8); 288cd248341SJan Glauber data = le64_to_cpu(data); 289cd248341SJan Glauber if (!rc) 290cd248341SJan Glauber *val = (u32) data; 291cd248341SJan Glauber else 292cd248341SJan Glauber *val = 0xffffffff; 293cd248341SJan Glauber return rc; 294cd248341SJan Glauber } 295cd248341SJan Glauber 296cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 297cd248341SJan Glauber { 298cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 299cd248341SJan Glauber u64 data = val; 300cd248341SJan Glauber int rc; 301cd248341SJan Glauber 302cd248341SJan Glauber data = cpu_to_le64(data); 303cd248341SJan Glauber data = data >> ((8 - len) * 8); 304cd248341SJan Glauber rc = pcistg_instr(data, req, offset); 305cd248341SJan Glauber return rc; 306cd248341SJan Glauber } 307cd248341SJan Glauber 3089a4da8a5SJan Glauber void synchronize_irq(unsigned int irq) 3099a4da8a5SJan Glauber { 3109a4da8a5SJan Glauber /* 3119a4da8a5SJan Glauber * Not needed, the handler is protected by a lock and IRQs that occur 3129a4da8a5SJan Glauber * after the handler is deleted are just NOPs. 3139a4da8a5SJan Glauber */ 3149a4da8a5SJan Glauber } 3159a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(synchronize_irq); 3169a4da8a5SJan Glauber 3179a4da8a5SJan Glauber void enable_irq(unsigned int irq) 3189a4da8a5SJan Glauber { 3199a4da8a5SJan Glauber struct msi_desc *msi = irq_get_msi_desc(irq); 3209a4da8a5SJan Glauber 3219a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 0); 3229a4da8a5SJan Glauber } 3239a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(enable_irq); 3249a4da8a5SJan Glauber 3259a4da8a5SJan Glauber void disable_irq(unsigned int irq) 3269a4da8a5SJan Glauber { 3279a4da8a5SJan Glauber struct msi_desc *msi = irq_get_msi_desc(irq); 3289a4da8a5SJan Glauber 3299a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 1); 3309a4da8a5SJan Glauber } 3319a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(disable_irq); 3329a4da8a5SJan Glauber 3339a4da8a5SJan Glauber void disable_irq_nosync(unsigned int irq) 3349a4da8a5SJan Glauber { 3359a4da8a5SJan Glauber disable_irq(irq); 3369a4da8a5SJan Glauber } 3379a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(disable_irq_nosync); 3389a4da8a5SJan Glauber 3399a4da8a5SJan Glauber unsigned long probe_irq_on(void) 3409a4da8a5SJan Glauber { 3419a4da8a5SJan Glauber return 0; 3429a4da8a5SJan Glauber } 3439a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(probe_irq_on); 3449a4da8a5SJan Glauber 3459a4da8a5SJan Glauber int probe_irq_off(unsigned long val) 3469a4da8a5SJan Glauber { 3479a4da8a5SJan Glauber return 0; 3489a4da8a5SJan Glauber } 3499a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(probe_irq_off); 3509a4da8a5SJan Glauber 3519a4da8a5SJan Glauber unsigned int probe_irq_mask(unsigned long val) 3529a4da8a5SJan Glauber { 3539a4da8a5SJan Glauber return val; 3549a4da8a5SJan Glauber } 3559a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(probe_irq_mask); 3569a4da8a5SJan Glauber 357b881bc46SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus) 358cd248341SJan Glauber { 359cd248341SJan Glauber } 360cd248341SJan Glauber 361cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 362cd248341SJan Glauber resource_size_t size, 363cd248341SJan Glauber resource_size_t align) 364cd248341SJan Glauber { 365cd248341SJan Glauber return 0; 366cd248341SJan Glauber } 367cd248341SJan Glauber 36887bc359bSJan Glauber /* combine single writes by using store-block insn */ 36987bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 37087bc359bSJan Glauber { 37187bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 37287bc359bSJan Glauber } 37387bc359bSJan Glauber 374cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 375cd248341SJan Glauber void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max) 376cd248341SJan Glauber { 377cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 378cd248341SJan Glauber u64 addr; 379cd248341SJan Glauber int idx; 380cd248341SJan Glauber 381cd248341SJan Glauber if ((bar & 7) != bar) 382cd248341SJan Glauber return NULL; 383cd248341SJan Glauber 384cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 385cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 386cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 387cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 388cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 389cd248341SJan Glauber 390cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48); 391cd248341SJan Glauber return (void __iomem *) addr; 392cd248341SJan Glauber } 393cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iomap); 394cd248341SJan Glauber 395cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 396cd248341SJan Glauber { 397cd248341SJan Glauber unsigned int idx; 398cd248341SJan Glauber 399cd248341SJan Glauber idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48; 400cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 401cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 402cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 403cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 404cd248341SJan Glauber } 405cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iounmap); 406cd248341SJan Glauber 407cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 408cd248341SJan Glauber int size, u32 *val) 409cd248341SJan Glauber { 410cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 411cd248341SJan Glauber 412cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 413cd248341SJan Glauber return 0; 414cd248341SJan Glauber return zpci_cfg_load(zdev, where, val, size); 415cd248341SJan Glauber } 416cd248341SJan Glauber 417cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 418cd248341SJan Glauber int size, u32 val) 419cd248341SJan Glauber { 420cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 421cd248341SJan Glauber 422cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 423cd248341SJan Glauber return 0; 424cd248341SJan Glauber return zpci_cfg_store(zdev, where, val, size); 425cd248341SJan Glauber } 426cd248341SJan Glauber 427cd248341SJan Glauber static struct pci_ops pci_root_ops = { 428cd248341SJan Glauber .read = pci_read, 429cd248341SJan Glauber .write = pci_write, 430cd248341SJan Glauber }; 431cd248341SJan Glauber 4329a4da8a5SJan Glauber /* store the last handled bit to implement fair scheduling of devices */ 4339a4da8a5SJan Glauber static DEFINE_PER_CPU(unsigned long, next_sbit); 4349a4da8a5SJan Glauber 4359a4da8a5SJan Glauber static void zpci_irq_handler(void *dont, void *need) 4369a4da8a5SJan Glauber { 4379a4da8a5SJan Glauber unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit); 4389a4da8a5SJan Glauber int rescan = 0, max = aisb_max; 4399a4da8a5SJan Glauber struct zdev_irq_map *imap; 4409a4da8a5SJan Glauber 441420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 4429a4da8a5SJan Glauber sbit = start; 4439a4da8a5SJan Glauber 4449a4da8a5SJan Glauber scan: 4459a4da8a5SJan Glauber /* find summary_bit */ 4469a4da8a5SJan Glauber for_each_set_bit_left_cont(sbit, bucket->aisb, max) { 4479a4da8a5SJan Glauber clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6)); 4489a4da8a5SJan Glauber last = sbit; 4499a4da8a5SJan Glauber 4509a4da8a5SJan Glauber /* find vector bit */ 4519a4da8a5SJan Glauber imap = bucket->imap[sbit]; 4529a4da8a5SJan Glauber for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) { 453420f42ecSHeiko Carstens inc_irq_stat(IRQIO_MSI); 4549a4da8a5SJan Glauber clear_bit(63 - mbit, &imap->aibv); 4559a4da8a5SJan Glauber 4569a4da8a5SJan Glauber spin_lock(&imap->lock); 4579a4da8a5SJan Glauber if (imap->cb[mbit].handler) 4589a4da8a5SJan Glauber imap->cb[mbit].handler(mbit, 4599a4da8a5SJan Glauber imap->cb[mbit].data); 4609a4da8a5SJan Glauber spin_unlock(&imap->lock); 4619a4da8a5SJan Glauber } 4629a4da8a5SJan Glauber } 4639a4da8a5SJan Glauber 4649a4da8a5SJan Glauber if (rescan) 4659a4da8a5SJan Glauber goto out; 4669a4da8a5SJan Glauber 4679a4da8a5SJan Glauber /* scan the skipped bits */ 4689a4da8a5SJan Glauber if (start > 0) { 4699a4da8a5SJan Glauber sbit = 0; 4709a4da8a5SJan Glauber max = start; 4719a4da8a5SJan Glauber start = 0; 4729a4da8a5SJan Glauber goto scan; 4739a4da8a5SJan Glauber } 4749a4da8a5SJan Glauber 4759a4da8a5SJan Glauber /* enable interrupts again */ 4769a4da8a5SJan Glauber sic_instr(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 4779a4da8a5SJan Glauber 4789a4da8a5SJan Glauber /* check again to not lose initiative */ 4799a4da8a5SJan Glauber rmb(); 4809a4da8a5SJan Glauber max = aisb_max; 4819a4da8a5SJan Glauber sbit = find_first_bit_left(bucket->aisb, max); 4829a4da8a5SJan Glauber if (sbit != max) { 4839a4da8a5SJan Glauber atomic_inc(&irq_retries); 4849a4da8a5SJan Glauber rescan++; 4859a4da8a5SJan Glauber goto scan; 4869a4da8a5SJan Glauber } 4879a4da8a5SJan Glauber out: 4889a4da8a5SJan Glauber /* store next device bit to scan */ 4899a4da8a5SJan Glauber __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last; 4909a4da8a5SJan Glauber } 4919a4da8a5SJan Glauber 4929a4da8a5SJan Glauber /* msi_vecs - number of requested interrupts, 0 place function to error state */ 4939a4da8a5SJan Glauber static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs) 4949a4da8a5SJan Glauber { 4959a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 4969a4da8a5SJan Glauber unsigned int aisb, msi_nr; 4979a4da8a5SJan Glauber struct msi_desc *msi; 4989a4da8a5SJan Glauber int rc; 4999a4da8a5SJan Glauber 5009a4da8a5SJan Glauber /* store the number of used MSI vectors */ 5019a4da8a5SJan Glauber zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS); 5029a4da8a5SJan Glauber 5039a4da8a5SJan Glauber spin_lock(&bucket->lock); 5049a4da8a5SJan Glauber aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE); 5059a4da8a5SJan Glauber /* alloc map exhausted? */ 5069a4da8a5SJan Glauber if (aisb == PAGE_SIZE) { 5079a4da8a5SJan Glauber spin_unlock(&bucket->lock); 5089a4da8a5SJan Glauber return -EIO; 5099a4da8a5SJan Glauber } 5109a4da8a5SJan Glauber set_bit(aisb, bucket->alloc); 5119a4da8a5SJan Glauber spin_unlock(&bucket->lock); 5129a4da8a5SJan Glauber 5139a4da8a5SJan Glauber zdev->aisb = aisb; 5149a4da8a5SJan Glauber if (aisb + 1 > aisb_max) 5159a4da8a5SJan Glauber aisb_max = aisb + 1; 5169a4da8a5SJan Glauber 5179a4da8a5SJan Glauber /* wire up IRQ shortcut pointer */ 5189a4da8a5SJan Glauber bucket->imap[zdev->aisb] = zdev->irq_map; 5199a4da8a5SJan Glauber pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map); 5209a4da8a5SJan Glauber 5219a4da8a5SJan Glauber /* TODO: irq number 0 wont be found if we return less than requested MSIs. 5229a4da8a5SJan Glauber * ignore it for now and fix in common code. 5239a4da8a5SJan Glauber */ 5249a4da8a5SJan Glauber msi_nr = aisb << ZPCI_MSI_VEC_BITS; 5259a4da8a5SJan Glauber 5269a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) { 5279a4da8a5SJan Glauber rc = zpci_setup_msi_irq(zdev, msi, msi_nr, 5289a4da8a5SJan Glauber aisb << ZPCI_MSI_VEC_BITS); 5299a4da8a5SJan Glauber if (rc) 5309a4da8a5SJan Glauber return rc; 5319a4da8a5SJan Glauber msi_nr++; 5329a4da8a5SJan Glauber } 5339a4da8a5SJan Glauber 5349a4da8a5SJan Glauber rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv); 5359a4da8a5SJan Glauber if (rc) { 5369a4da8a5SJan Glauber clear_bit(aisb, bucket->alloc); 5379a4da8a5SJan Glauber dev_err(&pdev->dev, "register MSI failed with: %d\n", rc); 5389a4da8a5SJan Glauber return rc; 5399a4da8a5SJan Glauber } 5409a4da8a5SJan Glauber return (zdev->irq_map->msi_vecs == msi_vecs) ? 5419a4da8a5SJan Glauber 0 : zdev->irq_map->msi_vecs; 5429a4da8a5SJan Glauber } 5439a4da8a5SJan Glauber 5449a4da8a5SJan Glauber static void zpci_teardown_msi(struct pci_dev *pdev) 5459a4da8a5SJan Glauber { 5469a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 5479a4da8a5SJan Glauber struct msi_desc *msi; 5489a4da8a5SJan Glauber int aisb, rc; 5499a4da8a5SJan Glauber 5509a4da8a5SJan Glauber rc = zpci_unregister_airq(zdev); 5519a4da8a5SJan Glauber if (rc) { 5529a4da8a5SJan Glauber dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc); 5539a4da8a5SJan Glauber return; 5549a4da8a5SJan Glauber } 5559a4da8a5SJan Glauber 5569a4da8a5SJan Glauber msi = list_first_entry(&pdev->msi_list, struct msi_desc, list); 5579a4da8a5SJan Glauber aisb = irq_to_dev_nr(msi->irq); 5589a4da8a5SJan Glauber 5599a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) 5609a4da8a5SJan Glauber zpci_teardown_msi_irq(zdev, msi); 5619a4da8a5SJan Glauber 5629a4da8a5SJan Glauber clear_bit(aisb, bucket->alloc); 5639a4da8a5SJan Glauber if (aisb + 1 == aisb_max) 5649a4da8a5SJan Glauber aisb_max--; 5659a4da8a5SJan Glauber } 5669a4da8a5SJan Glauber 5679a4da8a5SJan Glauber int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 5689a4da8a5SJan Glauber { 5699a4da8a5SJan Glauber pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec); 5709a4da8a5SJan Glauber if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI) 5719a4da8a5SJan Glauber return -EINVAL; 5729a4da8a5SJan Glauber return zpci_setup_msi(pdev, nvec); 5739a4da8a5SJan Glauber } 5749a4da8a5SJan Glauber 5759a4da8a5SJan Glauber void arch_teardown_msi_irqs(struct pci_dev *pdev) 5769a4da8a5SJan Glauber { 5779a4da8a5SJan Glauber pr_info("%s: on pdev: %p\n", __func__, pdev); 5789a4da8a5SJan Glauber zpci_teardown_msi(pdev); 5799a4da8a5SJan Glauber } 5809a4da8a5SJan Glauber 581cd248341SJan Glauber static void zpci_map_resources(struct zpci_dev *zdev) 582cd248341SJan Glauber { 583cd248341SJan Glauber struct pci_dev *pdev = zdev->pdev; 584cd248341SJan Glauber resource_size_t len; 585cd248341SJan Glauber int i; 586cd248341SJan Glauber 587cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 588cd248341SJan Glauber len = pci_resource_len(pdev, i); 589cd248341SJan Glauber if (!len) 590cd248341SJan Glauber continue; 591cd248341SJan Glauber pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0); 592cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 593cd248341SJan Glauber pr_debug("BAR%i: -> start: %Lx end: %Lx\n", 594cd248341SJan Glauber i, pdev->resource[i].start, pdev->resource[i].end); 595cd248341SJan Glauber } 596cd248341SJan Glauber }; 597cd248341SJan Glauber 598cd248341SJan Glauber static void zpci_unmap_resources(struct pci_dev *pdev) 599cd248341SJan Glauber { 600cd248341SJan Glauber resource_size_t len; 601cd248341SJan Glauber int i; 602cd248341SJan Glauber 603cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 604cd248341SJan Glauber len = pci_resource_len(pdev, i); 605cd248341SJan Glauber if (!len) 606cd248341SJan Glauber continue; 607cd248341SJan Glauber pci_iounmap(pdev, (void *) pdev->resource[i].start); 608cd248341SJan Glauber } 609cd248341SJan Glauber }; 610cd248341SJan Glauber 611cd248341SJan Glauber struct zpci_dev *zpci_alloc_device(void) 612cd248341SJan Glauber { 613cd248341SJan Glauber struct zpci_dev *zdev; 614cd248341SJan Glauber 615cd248341SJan Glauber /* Alloc memory for our private pci device data */ 616cd248341SJan Glauber zdev = kzalloc(sizeof(*zdev), GFP_KERNEL); 617cd248341SJan Glauber if (!zdev) 618cd248341SJan Glauber return ERR_PTR(-ENOMEM); 6199a4da8a5SJan Glauber 6209a4da8a5SJan Glauber /* Alloc aibv & callback space */ 6214118fee7SWei Yongjun zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL); 6229a4da8a5SJan Glauber if (!zdev->irq_map) 6239a4da8a5SJan Glauber goto error; 6249a4da8a5SJan Glauber WARN_ON((u64) zdev->irq_map & 0xff); 625cd248341SJan Glauber return zdev; 6269a4da8a5SJan Glauber 6279a4da8a5SJan Glauber error: 6289a4da8a5SJan Glauber kfree(zdev); 6299a4da8a5SJan Glauber return ERR_PTR(-ENOMEM); 630cd248341SJan Glauber } 631cd248341SJan Glauber 632cd248341SJan Glauber void zpci_free_device(struct zpci_dev *zdev) 633cd248341SJan Glauber { 6349a4da8a5SJan Glauber kmem_cache_free(zdev_irq_cache, zdev->irq_map); 635cd248341SJan Glauber kfree(zdev); 636cd248341SJan Glauber } 637cd248341SJan Glauber 638cd248341SJan Glauber /* Called on removal of pci_dev, leaves zpci and bus device */ 639cd248341SJan Glauber static void zpci_remove_device(struct pci_dev *pdev) 640cd248341SJan Glauber { 641cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 642cd248341SJan Glauber 643cd248341SJan Glauber dev_info(&pdev->dev, "Removing device %u\n", zdev->domain); 644cd248341SJan Glauber zdev->state = ZPCI_FN_STATE_CONFIGURED; 645828b35f6SJan Glauber zpci_dma_exit_device(zdev); 646d0b08853SJan Glauber zpci_fmb_disable_device(zdev); 6471e8da956SJan Glauber zpci_sysfs_remove_device(&pdev->dev); 648cd248341SJan Glauber zpci_unmap_resources(pdev); 649cd248341SJan Glauber list_del(&zdev->entry); /* can be called from init */ 650cd248341SJan Glauber zdev->pdev = NULL; 651cd248341SJan Glauber } 652cd248341SJan Glauber 653cd248341SJan Glauber static void zpci_scan_devices(void) 654cd248341SJan Glauber { 655cd248341SJan Glauber struct zpci_dev *zdev; 656cd248341SJan Glauber 657cd248341SJan Glauber mutex_lock(&zpci_list_lock); 658cd248341SJan Glauber list_for_each_entry(zdev, &zpci_list, entry) 659cd248341SJan Glauber if (zdev->state == ZPCI_FN_STATE_CONFIGURED) 660cd248341SJan Glauber zpci_scan_device(zdev); 661cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 662cd248341SJan Glauber } 663cd248341SJan Glauber 664cd248341SJan Glauber /* 665cd248341SJan Glauber * Too late for any s390 specific setup, since interrupts must be set up 666cd248341SJan Glauber * already which requires DMA setup too and the pci scan will access the 667cd248341SJan Glauber * config space, which only works if the function handle is enabled. 668cd248341SJan Glauber */ 669cd248341SJan Glauber int pcibios_enable_device(struct pci_dev *pdev, int mask) 670cd248341SJan Glauber { 671cd248341SJan Glauber struct resource *res; 672cd248341SJan Glauber u16 cmd; 673cd248341SJan Glauber int i; 674cd248341SJan Glauber 675cd248341SJan Glauber pci_read_config_word(pdev, PCI_COMMAND, &cmd); 676cd248341SJan Glauber 677cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 678cd248341SJan Glauber res = &pdev->resource[i]; 679cd248341SJan Glauber 680cd248341SJan Glauber if (res->flags & IORESOURCE_IO) 681cd248341SJan Glauber return -EINVAL; 682cd248341SJan Glauber 683cd248341SJan Glauber if (res->flags & IORESOURCE_MEM) 684cd248341SJan Glauber cmd |= PCI_COMMAND_MEMORY; 685cd248341SJan Glauber } 686cd248341SJan Glauber pci_write_config_word(pdev, PCI_COMMAND, cmd); 687cd248341SJan Glauber return 0; 688cd248341SJan Glauber } 689cd248341SJan Glauber 690cd248341SJan Glauber void pcibios_disable_device(struct pci_dev *pdev) 691cd248341SJan Glauber { 692cd248341SJan Glauber zpci_remove_device(pdev); 693cd248341SJan Glauber pdev->sysdata = NULL; 694cd248341SJan Glauber } 695cd248341SJan Glauber 6961e8da956SJan Glauber int pcibios_add_platform_entries(struct pci_dev *pdev) 6971e8da956SJan Glauber { 6981e8da956SJan Glauber return zpci_sysfs_add_device(&pdev->dev); 6991e8da956SJan Glauber } 7001e8da956SJan Glauber 7019a4da8a5SJan Glauber int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data) 7029a4da8a5SJan Glauber { 7039a4da8a5SJan Glauber int msi_nr = irq_to_msi_nr(irq); 7049a4da8a5SJan Glauber struct zdev_irq_map *imap; 7059a4da8a5SJan Glauber struct msi_desc *msi; 7069a4da8a5SJan Glauber 7079a4da8a5SJan Glauber msi = irq_get_msi_desc(irq); 7089a4da8a5SJan Glauber if (!msi) 7099a4da8a5SJan Glauber return -EIO; 7109a4da8a5SJan Glauber 7119a4da8a5SJan Glauber imap = get_imap(irq); 7129a4da8a5SJan Glauber spin_lock_init(&imap->lock); 7139a4da8a5SJan Glauber 7149a4da8a5SJan Glauber pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr); 7159a4da8a5SJan Glauber imap->cb[msi_nr].handler = handler; 7169a4da8a5SJan Glauber imap->cb[msi_nr].data = data; 7179a4da8a5SJan Glauber 7189a4da8a5SJan Glauber /* 7199a4da8a5SJan Glauber * The generic MSI code returns with the interrupt disabled on the 7209a4da8a5SJan Glauber * card, using the MSI mask bits. Firmware doesn't appear to unmask 7219a4da8a5SJan Glauber * at that level, so we do it here by hand. 7229a4da8a5SJan Glauber */ 7239a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 0); 7249a4da8a5SJan Glauber return 0; 7259a4da8a5SJan Glauber } 7269a4da8a5SJan Glauber 7279a4da8a5SJan Glauber void zpci_free_irq(unsigned int irq) 7289a4da8a5SJan Glauber { 7299a4da8a5SJan Glauber struct zdev_irq_map *imap = get_imap(irq); 7309a4da8a5SJan Glauber int msi_nr = irq_to_msi_nr(irq); 7319a4da8a5SJan Glauber unsigned long flags; 7329a4da8a5SJan Glauber 7339a4da8a5SJan Glauber pr_debug("%s: for irq: %d\n", __func__, irq); 7349a4da8a5SJan Glauber 7359a4da8a5SJan Glauber spin_lock_irqsave(&imap->lock, flags); 7369a4da8a5SJan Glauber imap->cb[msi_nr].handler = NULL; 7379a4da8a5SJan Glauber imap->cb[msi_nr].data = NULL; 7389a4da8a5SJan Glauber spin_unlock_irqrestore(&imap->lock, flags); 7399a4da8a5SJan Glauber } 7409a4da8a5SJan Glauber 7419a4da8a5SJan Glauber int request_irq(unsigned int irq, irq_handler_t handler, 7429a4da8a5SJan Glauber unsigned long irqflags, const char *devname, void *dev_id) 7439a4da8a5SJan Glauber { 7449a4da8a5SJan Glauber pr_debug("%s: irq: %d handler: %p flags: %lx dev: %s\n", 7459a4da8a5SJan Glauber __func__, irq, handler, irqflags, devname); 7469a4da8a5SJan Glauber 7479a4da8a5SJan Glauber return zpci_request_irq(irq, handler, dev_id); 7489a4da8a5SJan Glauber } 7499a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(request_irq); 7509a4da8a5SJan Glauber 7519a4da8a5SJan Glauber void free_irq(unsigned int irq, void *dev_id) 7529a4da8a5SJan Glauber { 7539a4da8a5SJan Glauber zpci_free_irq(irq); 7549a4da8a5SJan Glauber } 7559a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(free_irq); 7569a4da8a5SJan Glauber 7579a4da8a5SJan Glauber static int __init zpci_irq_init(void) 7589a4da8a5SJan Glauber { 7599a4da8a5SJan Glauber int cpu, rc; 7609a4da8a5SJan Glauber 7619a4da8a5SJan Glauber bucket = kzalloc(sizeof(*bucket), GFP_KERNEL); 7629a4da8a5SJan Glauber if (!bucket) 7639a4da8a5SJan Glauber return -ENOMEM; 7649a4da8a5SJan Glauber 7659a4da8a5SJan Glauber bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL); 7669a4da8a5SJan Glauber if (!bucket->aisb) { 7679a4da8a5SJan Glauber rc = -ENOMEM; 7689a4da8a5SJan Glauber goto out_aisb; 7699a4da8a5SJan Glauber } 7709a4da8a5SJan Glauber 7719a4da8a5SJan Glauber bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL); 7729a4da8a5SJan Glauber if (!bucket->alloc) { 7739a4da8a5SJan Glauber rc = -ENOMEM; 7749a4da8a5SJan Glauber goto out_alloc; 7759a4da8a5SJan Glauber } 7769a4da8a5SJan Glauber 7779a4da8a5SJan Glauber isc_register(PCI_ISC); 7789a4da8a5SJan Glauber zpci_irq_si = s390_register_adapter_interrupt(&zpci_irq_handler, NULL, PCI_ISC); 7799a4da8a5SJan Glauber if (IS_ERR(zpci_irq_si)) { 7809a4da8a5SJan Glauber rc = PTR_ERR(zpci_irq_si); 7819a4da8a5SJan Glauber zpci_irq_si = NULL; 7829a4da8a5SJan Glauber goto out_ai; 7839a4da8a5SJan Glauber } 7849a4da8a5SJan Glauber 7859a4da8a5SJan Glauber for_each_online_cpu(cpu) 7869a4da8a5SJan Glauber per_cpu(next_sbit, cpu) = 0; 7879a4da8a5SJan Glauber 7889a4da8a5SJan Glauber spin_lock_init(&bucket->lock); 7899a4da8a5SJan Glauber /* set summary to 1 to be called every time for the ISC */ 7909a4da8a5SJan Glauber *zpci_irq_si = 1; 7919a4da8a5SJan Glauber sic_instr(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 7929a4da8a5SJan Glauber return 0; 7939a4da8a5SJan Glauber 7949a4da8a5SJan Glauber out_ai: 7959a4da8a5SJan Glauber isc_unregister(PCI_ISC); 7969a4da8a5SJan Glauber free_page((unsigned long) bucket->alloc); 7979a4da8a5SJan Glauber out_alloc: 7989a4da8a5SJan Glauber free_page((unsigned long) bucket->aisb); 7999a4da8a5SJan Glauber out_aisb: 8009a4da8a5SJan Glauber kfree(bucket); 8019a4da8a5SJan Glauber return rc; 8029a4da8a5SJan Glauber } 8039a4da8a5SJan Glauber 8049a4da8a5SJan Glauber static void zpci_irq_exit(void) 8059a4da8a5SJan Glauber { 8069a4da8a5SJan Glauber free_page((unsigned long) bucket->alloc); 8079a4da8a5SJan Glauber free_page((unsigned long) bucket->aisb); 8089a4da8a5SJan Glauber s390_unregister_adapter_interrupt(zpci_irq_si, PCI_ISC); 8099a4da8a5SJan Glauber isc_unregister(PCI_ISC); 8109a4da8a5SJan Glauber kfree(bucket); 8119a4da8a5SJan Glauber } 8129a4da8a5SJan Glauber 813d0b08853SJan Glauber void zpci_debug_info(struct zpci_dev *zdev, struct seq_file *m) 814d0b08853SJan Glauber { 815d0b08853SJan Glauber if (!zdev) 816d0b08853SJan Glauber return; 817d0b08853SJan Glauber 818d0b08853SJan Glauber seq_printf(m, "global irq retries: %u\n", atomic_read(&irq_retries)); 819d0b08853SJan Glauber seq_printf(m, "aibv[0]:%016lx aibv[1]:%016lx aisb:%016lx\n", 820d0b08853SJan Glauber get_imap(0)->aibv, get_imap(1)->aibv, *bucket->aisb); 821d0b08853SJan Glauber } 822d0b08853SJan Glauber 823cd248341SJan Glauber static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size, 824cd248341SJan Glauber unsigned long flags, int domain) 825cd248341SJan Glauber { 826cd248341SJan Glauber struct resource *r; 827cd248341SJan Glauber char *name; 828cd248341SJan Glauber int rc; 829cd248341SJan Glauber 830cd248341SJan Glauber r = kzalloc(sizeof(*r), GFP_KERNEL); 831cd248341SJan Glauber if (!r) 832cd248341SJan Glauber return ERR_PTR(-ENOMEM); 833cd248341SJan Glauber r->start = start; 834cd248341SJan Glauber r->end = r->start + size - 1; 835cd248341SJan Glauber r->flags = flags; 836cd248341SJan Glauber r->parent = &iomem_resource; 837cd248341SJan Glauber name = kmalloc(18, GFP_KERNEL); 838cd248341SJan Glauber if (!name) { 839cd248341SJan Glauber kfree(r); 840cd248341SJan Glauber return ERR_PTR(-ENOMEM); 841cd248341SJan Glauber } 842cd248341SJan Glauber sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR); 843cd248341SJan Glauber r->name = name; 844cd248341SJan Glauber 845cd248341SJan Glauber rc = request_resource(&iomem_resource, r); 846cd248341SJan Glauber if (rc) 847cd248341SJan Glauber pr_debug("request resource %pR failed\n", r); 848cd248341SJan Glauber return r; 849cd248341SJan Glauber } 850cd248341SJan Glauber 851cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 852cd248341SJan Glauber { 853cd248341SJan Glauber int entry; 854cd248341SJan Glauber 855cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 856cd248341SJan Glauber entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 857cd248341SJan Glauber if (entry == ZPCI_IOMAP_MAX_ENTRIES) { 858cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 859cd248341SJan Glauber return -ENOSPC; 860cd248341SJan Glauber } 861cd248341SJan Glauber set_bit(entry, zpci_iomap); 862cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 863cd248341SJan Glauber return entry; 864cd248341SJan Glauber } 865cd248341SJan Glauber 866cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 867cd248341SJan Glauber { 868cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 869cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 870cd248341SJan Glauber clear_bit(entry, zpci_iomap); 871cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 872cd248341SJan Glauber } 873cd248341SJan Glauber 874cd248341SJan Glauber static int zpci_create_device_bus(struct zpci_dev *zdev) 875cd248341SJan Glauber { 876cd248341SJan Glauber struct resource *res; 877cd248341SJan Glauber LIST_HEAD(resources); 878cd248341SJan Glauber int i; 879cd248341SJan Glauber 880cd248341SJan Glauber /* allocate mapping entry for each used bar */ 881cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 882cd248341SJan Glauber unsigned long addr, size, flags; 883cd248341SJan Glauber int entry; 884cd248341SJan Glauber 885cd248341SJan Glauber if (!zdev->bars[i].size) 886cd248341SJan Glauber continue; 887cd248341SJan Glauber entry = zpci_alloc_iomap(zdev); 888cd248341SJan Glauber if (entry < 0) 889cd248341SJan Glauber return entry; 890cd248341SJan Glauber zdev->bars[i].map_idx = entry; 891cd248341SJan Glauber 892cd248341SJan Glauber /* only MMIO is supported */ 893cd248341SJan Glauber flags = IORESOURCE_MEM; 894cd248341SJan Glauber if (zdev->bars[i].val & 8) 895cd248341SJan Glauber flags |= IORESOURCE_PREFETCH; 896cd248341SJan Glauber if (zdev->bars[i].val & 4) 897cd248341SJan Glauber flags |= IORESOURCE_MEM_64; 898cd248341SJan Glauber 899cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48); 900cd248341SJan Glauber 901cd248341SJan Glauber size = 1UL << zdev->bars[i].size; 902cd248341SJan Glauber 903cd248341SJan Glauber res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain); 904cd248341SJan Glauber if (IS_ERR(res)) { 905cd248341SJan Glauber zpci_free_iomap(zdev, entry); 906cd248341SJan Glauber return PTR_ERR(res); 907cd248341SJan Glauber } 908cd248341SJan Glauber pci_add_resource(&resources, res); 909cd248341SJan Glauber } 910cd248341SJan Glauber 911cd248341SJan Glauber zdev->bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 912cd248341SJan Glauber zdev, &resources); 913cd248341SJan Glauber if (!zdev->bus) 914cd248341SJan Glauber return -EIO; 915cd248341SJan Glauber 916cd248341SJan Glauber zdev->bus->max_bus_speed = zdev->max_bus_speed; 917cd248341SJan Glauber return 0; 918cd248341SJan Glauber } 919cd248341SJan Glauber 920cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 921cd248341SJan Glauber { 922cd248341SJan Glauber spin_lock(&zpci_domain_lock); 923cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 924cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 925cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 926cd248341SJan Glauber return -ENOSPC; 927cd248341SJan Glauber } 928cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 929cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 930cd248341SJan Glauber return 0; 931cd248341SJan Glauber } 932cd248341SJan Glauber 933cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 934cd248341SJan Glauber { 935cd248341SJan Glauber spin_lock(&zpci_domain_lock); 936cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 937cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 938cd248341SJan Glauber } 939cd248341SJan Glauber 940a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 941a755a45dSJan Glauber { 942a755a45dSJan Glauber int rc; 943a755a45dSJan Glauber 944a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 945a755a45dSJan Glauber if (rc) 946a755a45dSJan Glauber goto out; 947a755a45dSJan Glauber pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid); 948828b35f6SJan Glauber 949828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 950828b35f6SJan Glauber if (rc) 951828b35f6SJan Glauber goto out_dma; 952a755a45dSJan Glauber return 0; 953828b35f6SJan Glauber 954828b35f6SJan Glauber out_dma: 955828b35f6SJan Glauber clp_disable_fh(zdev); 956a755a45dSJan Glauber out: 957a755a45dSJan Glauber return rc; 958a755a45dSJan Glauber } 959a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 960a755a45dSJan Glauber 961cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 962cd248341SJan Glauber { 963cd248341SJan Glauber int rc; 964cd248341SJan Glauber 965cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 966cd248341SJan Glauber if (rc) 967cd248341SJan Glauber goto out; 968cd248341SJan Glauber 969cd248341SJan Glauber rc = zpci_create_device_bus(zdev); 970cd248341SJan Glauber if (rc) 971cd248341SJan Glauber goto out_bus; 972cd248341SJan Glauber 973cd248341SJan Glauber mutex_lock(&zpci_list_lock); 974cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 97553923354SSebastian Ott if (hotplug_ops) 97653923354SSebastian Ott hotplug_ops->create_slot(zdev); 977cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 978cd248341SJan Glauber 979cd248341SJan Glauber if (zdev->state == ZPCI_FN_STATE_STANDBY) 980cd248341SJan Glauber return 0; 981cd248341SJan Glauber 982a755a45dSJan Glauber rc = zpci_enable_device(zdev); 983a755a45dSJan Glauber if (rc) 984a755a45dSJan Glauber goto out_start; 985cd248341SJan Glauber return 0; 986cd248341SJan Glauber 987a755a45dSJan Glauber out_start: 988a755a45dSJan Glauber mutex_lock(&zpci_list_lock); 989a755a45dSJan Glauber list_del(&zdev->entry); 99053923354SSebastian Ott if (hotplug_ops) 99153923354SSebastian Ott hotplug_ops->remove_slot(zdev); 992a755a45dSJan Glauber mutex_unlock(&zpci_list_lock); 993cd248341SJan Glauber out_bus: 994cd248341SJan Glauber zpci_free_domain(zdev); 995cd248341SJan Glauber out: 996cd248341SJan Glauber return rc; 997cd248341SJan Glauber } 998cd248341SJan Glauber 999cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 1000cd248341SJan Glauber { 1001828b35f6SJan Glauber zpci_dma_exit_device(zdev); 1002cd248341SJan Glauber /* 1003cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 1004cd248341SJan Glauber * do that here. 1005cd248341SJan Glauber */ 1006cd248341SJan Glauber } 1007cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 1008cd248341SJan Glauber 1009cd248341SJan Glauber int zpci_scan_device(struct zpci_dev *zdev) 1010cd248341SJan Glauber { 1011cd248341SJan Glauber zdev->pdev = pci_scan_single_device(zdev->bus, ZPCI_DEVFN); 1012cd248341SJan Glauber if (!zdev->pdev) { 1013cd248341SJan Glauber pr_err("pci_scan_single_device failed for fid: 0x%x\n", 1014cd248341SJan Glauber zdev->fid); 1015cd248341SJan Glauber goto out; 1016cd248341SJan Glauber } 1017cd248341SJan Glauber 1018d0b08853SJan Glauber zpci_debug_init_device(zdev); 1019d0b08853SJan Glauber zpci_fmb_enable_device(zdev); 1020cd248341SJan Glauber zpci_map_resources(zdev); 1021cd248341SJan Glauber pci_bus_add_devices(zdev->bus); 1022cd248341SJan Glauber 1023cd248341SJan Glauber /* now that pdev was added to the bus mark it as used */ 1024cd248341SJan Glauber zdev->state = ZPCI_FN_STATE_ONLINE; 1025cd248341SJan Glauber return 0; 1026cd248341SJan Glauber 1027cd248341SJan Glauber out: 1028828b35f6SJan Glauber zpci_dma_exit_device(zdev); 1029a755a45dSJan Glauber clp_disable_fh(zdev); 1030cd248341SJan Glauber return -EIO; 1031cd248341SJan Glauber } 1032cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_scan_device); 1033cd248341SJan Glauber 1034cd248341SJan Glauber static inline int barsize(u8 size) 1035cd248341SJan Glauber { 1036cd248341SJan Glauber return (size) ? (1 << size) >> 10 : 0; 1037cd248341SJan Glauber } 1038cd248341SJan Glauber 1039cd248341SJan Glauber static int zpci_mem_init(void) 1040cd248341SJan Glauber { 10419a4da8a5SJan Glauber zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map), 10429a4da8a5SJan Glauber L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL); 10439a4da8a5SJan Glauber if (!zdev_irq_cache) 10449a4da8a5SJan Glauber goto error_zdev; 10459a4da8a5SJan Glauber 1046d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 1047d0b08853SJan Glauber 16, 0, NULL); 1048d0b08853SJan Glauber if (!zdev_fmb_cache) 1049d0b08853SJan Glauber goto error_fmb; 1050d0b08853SJan Glauber 1051cd248341SJan Glauber /* TODO: use realloc */ 1052cd248341SJan Glauber zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start), 1053cd248341SJan Glauber GFP_KERNEL); 1054cd248341SJan Glauber if (!zpci_iomap_start) 10559a4da8a5SJan Glauber goto error_iomap; 1056cd248341SJan Glauber return 0; 1057cd248341SJan Glauber 10589a4da8a5SJan Glauber error_iomap: 1059d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 1060d0b08853SJan Glauber error_fmb: 10619a4da8a5SJan Glauber kmem_cache_destroy(zdev_irq_cache); 1062cd248341SJan Glauber error_zdev: 1063cd248341SJan Glauber return -ENOMEM; 1064cd248341SJan Glauber } 1065cd248341SJan Glauber 1066cd248341SJan Glauber static void zpci_mem_exit(void) 1067cd248341SJan Glauber { 1068cd248341SJan Glauber kfree(zpci_iomap_start); 10699a4da8a5SJan Glauber kmem_cache_destroy(zdev_irq_cache); 1070d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 1071cd248341SJan Glauber } 1072cd248341SJan Glauber 107353923354SSebastian Ott void zpci_register_hp_ops(struct pci_hp_callback_ops *ops) 107453923354SSebastian Ott { 107553923354SSebastian Ott mutex_lock(&zpci_list_lock); 107653923354SSebastian Ott hotplug_ops = ops; 107753923354SSebastian Ott mutex_unlock(&zpci_list_lock); 107853923354SSebastian Ott } 107953923354SSebastian Ott EXPORT_SYMBOL_GPL(zpci_register_hp_ops); 108053923354SSebastian Ott 108153923354SSebastian Ott void zpci_deregister_hp_ops(void) 108253923354SSebastian Ott { 108353923354SSebastian Ott mutex_lock(&zpci_list_lock); 108453923354SSebastian Ott hotplug_ops = NULL; 108553923354SSebastian Ott mutex_unlock(&zpci_list_lock); 108653923354SSebastian Ott } 108753923354SSebastian Ott EXPORT_SYMBOL_GPL(zpci_deregister_hp_ops); 108853923354SSebastian Ott 10891e5635d1SHeiko Carstens unsigned int s390_pci_probe = 1; 10901e5635d1SHeiko Carstens EXPORT_SYMBOL_GPL(s390_pci_probe); 1091cd248341SJan Glauber 1092cd248341SJan Glauber char * __init pcibios_setup(char *str) 1093cd248341SJan Glauber { 1094cd248341SJan Glauber if (!strcmp(str, "off")) { 10951e5635d1SHeiko Carstens s390_pci_probe = 0; 1096cd248341SJan Glauber return NULL; 1097cd248341SJan Glauber } 1098cd248341SJan Glauber return str; 1099cd248341SJan Glauber } 1100cd248341SJan Glauber 1101cd248341SJan Glauber static int __init pci_base_init(void) 1102cd248341SJan Glauber { 1103cd248341SJan Glauber int rc; 1104cd248341SJan Glauber 11051e5635d1SHeiko Carstens if (!s390_pci_probe) 1106cd248341SJan Glauber return 0; 1107cd248341SJan Glauber 1108cd248341SJan Glauber if (!test_facility(2) || !test_facility(69) 1109cd248341SJan Glauber || !test_facility(71) || !test_facility(72)) 1110cd248341SJan Glauber return 0; 1111cd248341SJan Glauber 1112cd248341SJan Glauber pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n", 1113cd248341SJan Glauber test_facility(69), test_facility(70), 1114cd248341SJan Glauber test_facility(71)); 1115cd248341SJan Glauber 1116d0b08853SJan Glauber rc = zpci_debug_init(); 1117d0b08853SJan Glauber if (rc) 1118d0b08853SJan Glauber return rc; 1119d0b08853SJan Glauber 1120cd248341SJan Glauber rc = zpci_mem_init(); 1121cd248341SJan Glauber if (rc) 1122cd248341SJan Glauber goto out_mem; 1123cd248341SJan Glauber 11249a4da8a5SJan Glauber rc = zpci_msihash_init(); 11259a4da8a5SJan Glauber if (rc) 11269a4da8a5SJan Glauber goto out_hash; 11279a4da8a5SJan Glauber 11289a4da8a5SJan Glauber rc = zpci_irq_init(); 11299a4da8a5SJan Glauber if (rc) 11309a4da8a5SJan Glauber goto out_irq; 11319a4da8a5SJan Glauber 1132828b35f6SJan Glauber rc = zpci_dma_init(); 1133828b35f6SJan Glauber if (rc) 1134828b35f6SJan Glauber goto out_dma; 1135828b35f6SJan Glauber 1136a755a45dSJan Glauber rc = clp_find_pci_devices(); 1137a755a45dSJan Glauber if (rc) 1138a755a45dSJan Glauber goto out_find; 1139a755a45dSJan Glauber 1140cd248341SJan Glauber zpci_scan_devices(); 1141cd248341SJan Glauber return 0; 1142cd248341SJan Glauber 1143a755a45dSJan Glauber out_find: 1144828b35f6SJan Glauber zpci_dma_exit(); 1145828b35f6SJan Glauber out_dma: 11469a4da8a5SJan Glauber zpci_irq_exit(); 11479a4da8a5SJan Glauber out_irq: 11489a4da8a5SJan Glauber zpci_msihash_exit(); 11499a4da8a5SJan Glauber out_hash: 1150cd248341SJan Glauber zpci_mem_exit(); 1151cd248341SJan Glauber out_mem: 1152d0b08853SJan Glauber zpci_debug_exit(); 1153cd248341SJan Glauber return rc; 1154cd248341SJan Glauber } 1155cd248341SJan Glauber subsys_initcall(pci_base_init); 1156