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!): 9*bedef755SJan Glauber * Alexander Schmidt 10*bedef755SJan Glauber * Christoph Raisch 11*bedef755SJan Glauber * Hannes Hering 12*bedef755SJan Glauber * Hoang-Nam Nguyen 13*bedef755SJan Glauber * Jan-Bernd Themann 14*bedef755SJan Glauber * Stefan Roscher 15*bedef755SJan 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 547441b062SJan Glauber struct pci_hp_callback_ops hotplug_ops; 557441b062SJan Glauber EXPORT_SYMBOL_GPL(hotplug_ops); 56cd248341SJan Glauber 57cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 58cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 59cd248341SJan Glauber 609a4da8a5SJan Glauber struct callback { 619a4da8a5SJan Glauber irq_handler_t handler; 629a4da8a5SJan Glauber void *data; 639a4da8a5SJan Glauber }; 649a4da8a5SJan Glauber 659a4da8a5SJan Glauber struct zdev_irq_map { 669a4da8a5SJan Glauber unsigned long aibv; /* AI bit vector */ 679a4da8a5SJan Glauber int msi_vecs; /* consecutive MSI-vectors used */ 689a4da8a5SJan Glauber int __unused; 699a4da8a5SJan Glauber struct callback cb[ZPCI_NR_MSI_VECS]; /* callback handler array */ 709a4da8a5SJan Glauber spinlock_t lock; /* protect callbacks against de-reg */ 719a4da8a5SJan Glauber }; 729a4da8a5SJan Glauber 739a4da8a5SJan Glauber struct intr_bucket { 749a4da8a5SJan Glauber /* amap of adapters, one bit per dev, corresponds to one irq nr */ 759a4da8a5SJan Glauber unsigned long *alloc; 769a4da8a5SJan Glauber /* AI summary bit, global page for all devices */ 779a4da8a5SJan Glauber unsigned long *aisb; 789a4da8a5SJan Glauber /* pointer to aibv and callback data in zdev */ 799a4da8a5SJan Glauber struct zdev_irq_map *imap[ZPCI_NR_DEVICES]; 809a4da8a5SJan Glauber /* protects the whole bucket struct */ 819a4da8a5SJan Glauber spinlock_t lock; 829a4da8a5SJan Glauber }; 839a4da8a5SJan Glauber 849a4da8a5SJan Glauber static struct intr_bucket *bucket; 859a4da8a5SJan Glauber 869a4da8a5SJan Glauber /* Adapter local summary indicator */ 879a4da8a5SJan Glauber static u8 *zpci_irq_si; 889a4da8a5SJan Glauber 899a4da8a5SJan Glauber static atomic_t irq_retries = ATOMIC_INIT(0); 909a4da8a5SJan Glauber 91cd248341SJan Glauber /* I/O Map */ 92cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 93cd248341SJan Glauber static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 94cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 95cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 96cd248341SJan Glauber 979a4da8a5SJan Glauber /* highest irq summary bit */ 989a4da8a5SJan Glauber static int __read_mostly aisb_max; 999a4da8a5SJan Glauber 1009a4da8a5SJan Glauber static struct kmem_cache *zdev_irq_cache; 1019a4da8a5SJan 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 159cd248341SJan Glauber /* Store PCI function information block */ 160cd248341SJan Glauber static int zpci_store_fib(struct zpci_dev *zdev, u8 *fc) 161cd248341SJan Glauber { 162cd248341SJan Glauber struct zpci_fib *fib; 163cd248341SJan Glauber u8 status, cc; 164cd248341SJan Glauber 165cd248341SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 166cd248341SJan Glauber if (!fib) 167cd248341SJan Glauber return -ENOMEM; 168cd248341SJan Glauber 169cd248341SJan Glauber do { 170cd248341SJan Glauber cc = __stpcifc(zdev->fh, 0, fib, &status); 171cd248341SJan Glauber if (cc == 2) { 172cd248341SJan Glauber msleep(ZPCI_INSN_BUSY_DELAY); 173cd248341SJan Glauber memset(fib, 0, PAGE_SIZE); 174cd248341SJan Glauber } 175cd248341SJan Glauber } while (cc == 2); 176cd248341SJan Glauber 177cd248341SJan Glauber if (cc) 178cd248341SJan Glauber pr_err_once("%s: cc: %u status: %u\n", 179cd248341SJan Glauber __func__, cc, status); 180cd248341SJan Glauber 181cd248341SJan Glauber /* Return PCI function controls */ 182cd248341SJan Glauber *fc = fib->fc; 183cd248341SJan Glauber 184cd248341SJan Glauber free_page((unsigned long) fib); 185cd248341SJan Glauber return (cc) ? -EIO : 0; 186cd248341SJan Glauber } 187cd248341SJan Glauber 1889a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1899a4da8a5SJan Glauber static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb, 1909a4da8a5SJan Glauber u64 aibv) 1919a4da8a5SJan Glauber { 1929a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 1939a4da8a5SJan Glauber struct zpci_fib *fib; 1949a4da8a5SJan Glauber int rc; 1959a4da8a5SJan Glauber 1969a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 1979a4da8a5SJan Glauber if (!fib) 1989a4da8a5SJan Glauber return -ENOMEM; 1999a4da8a5SJan Glauber 2009a4da8a5SJan Glauber fib->isc = PCI_ISC; 2019a4da8a5SJan Glauber fib->noi = zdev->irq_map->msi_vecs; 2029a4da8a5SJan Glauber fib->sum = 1; /* enable summary notifications */ 2039a4da8a5SJan Glauber fib->aibv = aibv; 2049a4da8a5SJan Glauber fib->aibvo = 0; /* every function has its own page */ 2059a4da8a5SJan Glauber fib->aisb = (u64) bucket->aisb + aisb / 8; 2069a4da8a5SJan Glauber fib->aisbo = aisb & ZPCI_MSI_MASK; 2079a4da8a5SJan Glauber 2089a4da8a5SJan Glauber rc = mpcifc_instr(req, fib); 2099a4da8a5SJan Glauber pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi); 2109a4da8a5SJan Glauber 2119a4da8a5SJan Glauber free_page((unsigned long) fib); 2129a4da8a5SJan Glauber return rc; 2139a4da8a5SJan Glauber } 2149a4da8a5SJan Glauber 2159a4da8a5SJan Glauber struct mod_pci_args { 2169a4da8a5SJan Glauber u64 base; 2179a4da8a5SJan Glauber u64 limit; 2189a4da8a5SJan Glauber u64 iota; 2199a4da8a5SJan Glauber }; 2209a4da8a5SJan Glauber 2219a4da8a5SJan Glauber static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) 2229a4da8a5SJan Glauber { 2239a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); 2249a4da8a5SJan Glauber struct zpci_fib *fib; 2259a4da8a5SJan Glauber int rc; 2269a4da8a5SJan Glauber 2279a4da8a5SJan Glauber /* The FIB must be available even if it's not used */ 2289a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 2299a4da8a5SJan Glauber if (!fib) 2309a4da8a5SJan Glauber return -ENOMEM; 2319a4da8a5SJan Glauber 2329a4da8a5SJan Glauber fib->pba = args->base; 2339a4da8a5SJan Glauber fib->pal = args->limit; 2349a4da8a5SJan Glauber fib->iota = args->iota; 2359a4da8a5SJan Glauber 2369a4da8a5SJan Glauber rc = mpcifc_instr(req, fib); 2379a4da8a5SJan Glauber free_page((unsigned long) fib); 2389a4da8a5SJan Glauber return rc; 2399a4da8a5SJan Glauber } 2409a4da8a5SJan Glauber 241828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 242828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 243828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 244828b35f6SJan Glauber { 245828b35f6SJan Glauber struct mod_pci_args args = { base, limit, iota }; 246828b35f6SJan Glauber 247828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 248828b35f6SJan Glauber args.iota |= ZPCI_IOTA_RTTO_FLAG; 249828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); 250828b35f6SJan Glauber } 251828b35f6SJan Glauber 252828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 253828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 254828b35f6SJan Glauber { 255828b35f6SJan Glauber struct mod_pci_args args = { 0, 0, 0 }; 256828b35f6SJan Glauber 257828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); 258828b35f6SJan Glauber } 259828b35f6SJan Glauber 2609a4da8a5SJan Glauber /* Modify PCI: Unregister adapter interruptions */ 2619a4da8a5SJan Glauber static int zpci_unregister_airq(struct zpci_dev *zdev) 2629a4da8a5SJan Glauber { 2639a4da8a5SJan Glauber struct mod_pci_args args = { 0, 0, 0 }; 2649a4da8a5SJan Glauber 2659a4da8a5SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); 2669a4da8a5SJan Glauber } 2679a4da8a5SJan Glauber 268cd248341SJan Glauber #define ZPCI_PCIAS_CFGSPC 15 269cd248341SJan Glauber 270cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 271cd248341SJan Glauber { 272cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 273cd248341SJan Glauber u64 data; 274cd248341SJan Glauber int rc; 275cd248341SJan Glauber 276cd248341SJan Glauber rc = pcilg_instr(&data, req, offset); 277cd248341SJan Glauber data = data << ((8 - len) * 8); 278cd248341SJan Glauber data = le64_to_cpu(data); 279cd248341SJan Glauber if (!rc) 280cd248341SJan Glauber *val = (u32) data; 281cd248341SJan Glauber else 282cd248341SJan Glauber *val = 0xffffffff; 283cd248341SJan Glauber return rc; 284cd248341SJan Glauber } 285cd248341SJan Glauber 286cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 287cd248341SJan Glauber { 288cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 289cd248341SJan Glauber u64 data = val; 290cd248341SJan Glauber int rc; 291cd248341SJan Glauber 292cd248341SJan Glauber data = cpu_to_le64(data); 293cd248341SJan Glauber data = data >> ((8 - len) * 8); 294cd248341SJan Glauber rc = pcistg_instr(data, req, offset); 295cd248341SJan Glauber return rc; 296cd248341SJan Glauber } 297cd248341SJan Glauber 2989a4da8a5SJan Glauber void synchronize_irq(unsigned int irq) 2999a4da8a5SJan Glauber { 3009a4da8a5SJan Glauber /* 3019a4da8a5SJan Glauber * Not needed, the handler is protected by a lock and IRQs that occur 3029a4da8a5SJan Glauber * after the handler is deleted are just NOPs. 3039a4da8a5SJan Glauber */ 3049a4da8a5SJan Glauber } 3059a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(synchronize_irq); 3069a4da8a5SJan Glauber 3079a4da8a5SJan Glauber void enable_irq(unsigned int irq) 3089a4da8a5SJan Glauber { 3099a4da8a5SJan Glauber struct msi_desc *msi = irq_get_msi_desc(irq); 3109a4da8a5SJan Glauber 3119a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 0); 3129a4da8a5SJan Glauber } 3139a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(enable_irq); 3149a4da8a5SJan Glauber 3159a4da8a5SJan Glauber void disable_irq(unsigned int irq) 3169a4da8a5SJan Glauber { 3179a4da8a5SJan Glauber struct msi_desc *msi = irq_get_msi_desc(irq); 3189a4da8a5SJan Glauber 3199a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 1); 3209a4da8a5SJan Glauber } 3219a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(disable_irq); 3229a4da8a5SJan Glauber 3239a4da8a5SJan Glauber void disable_irq_nosync(unsigned int irq) 3249a4da8a5SJan Glauber { 3259a4da8a5SJan Glauber disable_irq(irq); 3269a4da8a5SJan Glauber } 3279a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(disable_irq_nosync); 3289a4da8a5SJan Glauber 3299a4da8a5SJan Glauber unsigned long probe_irq_on(void) 3309a4da8a5SJan Glauber { 3319a4da8a5SJan Glauber return 0; 3329a4da8a5SJan Glauber } 3339a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(probe_irq_on); 3349a4da8a5SJan Glauber 3359a4da8a5SJan Glauber int probe_irq_off(unsigned long val) 3369a4da8a5SJan Glauber { 3379a4da8a5SJan Glauber return 0; 3389a4da8a5SJan Glauber } 3399a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(probe_irq_off); 3409a4da8a5SJan Glauber 3419a4da8a5SJan Glauber unsigned int probe_irq_mask(unsigned long val) 3429a4da8a5SJan Glauber { 3439a4da8a5SJan Glauber return val; 3449a4da8a5SJan Glauber } 3459a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(probe_irq_mask); 3469a4da8a5SJan Glauber 347cd248341SJan Glauber void __devinit pcibios_fixup_bus(struct pci_bus *bus) 348cd248341SJan Glauber { 349cd248341SJan Glauber } 350cd248341SJan Glauber 351cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 352cd248341SJan Glauber resource_size_t size, 353cd248341SJan Glauber resource_size_t align) 354cd248341SJan Glauber { 355cd248341SJan Glauber return 0; 356cd248341SJan Glauber } 357cd248341SJan Glauber 35887bc359bSJan Glauber /* combine single writes by using store-block insn */ 35987bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 36087bc359bSJan Glauber { 36187bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 36287bc359bSJan Glauber } 36387bc359bSJan Glauber 364cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 365cd248341SJan Glauber void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max) 366cd248341SJan Glauber { 367cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 368cd248341SJan Glauber u64 addr; 369cd248341SJan Glauber int idx; 370cd248341SJan Glauber 371cd248341SJan Glauber if ((bar & 7) != bar) 372cd248341SJan Glauber return NULL; 373cd248341SJan Glauber 374cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 375cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 376cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 377cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 378cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 379cd248341SJan Glauber 380cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48); 381cd248341SJan Glauber return (void __iomem *) addr; 382cd248341SJan Glauber } 383cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iomap); 384cd248341SJan Glauber 385cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 386cd248341SJan Glauber { 387cd248341SJan Glauber unsigned int idx; 388cd248341SJan Glauber 389cd248341SJan Glauber idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48; 390cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 391cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 392cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 393cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 394cd248341SJan Glauber } 395cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iounmap); 396cd248341SJan Glauber 397cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 398cd248341SJan Glauber int size, u32 *val) 399cd248341SJan Glauber { 400cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 401cd248341SJan Glauber 402cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 403cd248341SJan Glauber return 0; 404cd248341SJan Glauber return zpci_cfg_load(zdev, where, val, size); 405cd248341SJan Glauber } 406cd248341SJan Glauber 407cd248341SJan Glauber static int pci_write(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_store(zdev, where, val, size); 415cd248341SJan Glauber } 416cd248341SJan Glauber 417cd248341SJan Glauber static struct pci_ops pci_root_ops = { 418cd248341SJan Glauber .read = pci_read, 419cd248341SJan Glauber .write = pci_write, 420cd248341SJan Glauber }; 421cd248341SJan Glauber 4229a4da8a5SJan Glauber /* store the last handled bit to implement fair scheduling of devices */ 4239a4da8a5SJan Glauber static DEFINE_PER_CPU(unsigned long, next_sbit); 4249a4da8a5SJan Glauber 4259a4da8a5SJan Glauber static void zpci_irq_handler(void *dont, void *need) 4269a4da8a5SJan Glauber { 4279a4da8a5SJan Glauber unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit); 4289a4da8a5SJan Glauber int rescan = 0, max = aisb_max; 4299a4da8a5SJan Glauber struct zdev_irq_map *imap; 4309a4da8a5SJan Glauber 4319a4da8a5SJan Glauber kstat_cpu(smp_processor_id()).irqs[IOINT_PCI]++; 4329a4da8a5SJan Glauber sbit = start; 4339a4da8a5SJan Glauber 4349a4da8a5SJan Glauber scan: 4359a4da8a5SJan Glauber /* find summary_bit */ 4369a4da8a5SJan Glauber for_each_set_bit_left_cont(sbit, bucket->aisb, max) { 4379a4da8a5SJan Glauber clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6)); 4389a4da8a5SJan Glauber last = sbit; 4399a4da8a5SJan Glauber 4409a4da8a5SJan Glauber /* find vector bit */ 4419a4da8a5SJan Glauber imap = bucket->imap[sbit]; 4429a4da8a5SJan Glauber for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) { 4439a4da8a5SJan Glauber kstat_cpu(smp_processor_id()).irqs[IOINT_MSI]++; 4449a4da8a5SJan Glauber clear_bit(63 - mbit, &imap->aibv); 4459a4da8a5SJan Glauber 4469a4da8a5SJan Glauber spin_lock(&imap->lock); 4479a4da8a5SJan Glauber if (imap->cb[mbit].handler) 4489a4da8a5SJan Glauber imap->cb[mbit].handler(mbit, 4499a4da8a5SJan Glauber imap->cb[mbit].data); 4509a4da8a5SJan Glauber spin_unlock(&imap->lock); 4519a4da8a5SJan Glauber } 4529a4da8a5SJan Glauber } 4539a4da8a5SJan Glauber 4549a4da8a5SJan Glauber if (rescan) 4559a4da8a5SJan Glauber goto out; 4569a4da8a5SJan Glauber 4579a4da8a5SJan Glauber /* scan the skipped bits */ 4589a4da8a5SJan Glauber if (start > 0) { 4599a4da8a5SJan Glauber sbit = 0; 4609a4da8a5SJan Glauber max = start; 4619a4da8a5SJan Glauber start = 0; 4629a4da8a5SJan Glauber goto scan; 4639a4da8a5SJan Glauber } 4649a4da8a5SJan Glauber 4659a4da8a5SJan Glauber /* enable interrupts again */ 4669a4da8a5SJan Glauber sic_instr(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 4679a4da8a5SJan Glauber 4689a4da8a5SJan Glauber /* check again to not lose initiative */ 4699a4da8a5SJan Glauber rmb(); 4709a4da8a5SJan Glauber max = aisb_max; 4719a4da8a5SJan Glauber sbit = find_first_bit_left(bucket->aisb, max); 4729a4da8a5SJan Glauber if (sbit != max) { 4739a4da8a5SJan Glauber atomic_inc(&irq_retries); 4749a4da8a5SJan Glauber rescan++; 4759a4da8a5SJan Glauber goto scan; 4769a4da8a5SJan Glauber } 4779a4da8a5SJan Glauber out: 4789a4da8a5SJan Glauber /* store next device bit to scan */ 4799a4da8a5SJan Glauber __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last; 4809a4da8a5SJan Glauber } 4819a4da8a5SJan Glauber 4829a4da8a5SJan Glauber /* msi_vecs - number of requested interrupts, 0 place function to error state */ 4839a4da8a5SJan Glauber static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs) 4849a4da8a5SJan Glauber { 4859a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 4869a4da8a5SJan Glauber unsigned int aisb, msi_nr; 4879a4da8a5SJan Glauber struct msi_desc *msi; 4889a4da8a5SJan Glauber int rc; 4899a4da8a5SJan Glauber 4909a4da8a5SJan Glauber /* store the number of used MSI vectors */ 4919a4da8a5SJan Glauber zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS); 4929a4da8a5SJan Glauber 4939a4da8a5SJan Glauber spin_lock(&bucket->lock); 4949a4da8a5SJan Glauber aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE); 4959a4da8a5SJan Glauber /* alloc map exhausted? */ 4969a4da8a5SJan Glauber if (aisb == PAGE_SIZE) { 4979a4da8a5SJan Glauber spin_unlock(&bucket->lock); 4989a4da8a5SJan Glauber return -EIO; 4999a4da8a5SJan Glauber } 5009a4da8a5SJan Glauber set_bit(aisb, bucket->alloc); 5019a4da8a5SJan Glauber spin_unlock(&bucket->lock); 5029a4da8a5SJan Glauber 5039a4da8a5SJan Glauber zdev->aisb = aisb; 5049a4da8a5SJan Glauber if (aisb + 1 > aisb_max) 5059a4da8a5SJan Glauber aisb_max = aisb + 1; 5069a4da8a5SJan Glauber 5079a4da8a5SJan Glauber /* wire up IRQ shortcut pointer */ 5089a4da8a5SJan Glauber bucket->imap[zdev->aisb] = zdev->irq_map; 5099a4da8a5SJan Glauber pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map); 5109a4da8a5SJan Glauber 5119a4da8a5SJan Glauber /* TODO: irq number 0 wont be found if we return less than requested MSIs. 5129a4da8a5SJan Glauber * ignore it for now and fix in common code. 5139a4da8a5SJan Glauber */ 5149a4da8a5SJan Glauber msi_nr = aisb << ZPCI_MSI_VEC_BITS; 5159a4da8a5SJan Glauber 5169a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) { 5179a4da8a5SJan Glauber rc = zpci_setup_msi_irq(zdev, msi, msi_nr, 5189a4da8a5SJan Glauber aisb << ZPCI_MSI_VEC_BITS); 5199a4da8a5SJan Glauber if (rc) 5209a4da8a5SJan Glauber return rc; 5219a4da8a5SJan Glauber msi_nr++; 5229a4da8a5SJan Glauber } 5239a4da8a5SJan Glauber 5249a4da8a5SJan Glauber rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv); 5259a4da8a5SJan Glauber if (rc) { 5269a4da8a5SJan Glauber clear_bit(aisb, bucket->alloc); 5279a4da8a5SJan Glauber dev_err(&pdev->dev, "register MSI failed with: %d\n", rc); 5289a4da8a5SJan Glauber return rc; 5299a4da8a5SJan Glauber } 5309a4da8a5SJan Glauber return (zdev->irq_map->msi_vecs == msi_vecs) ? 5319a4da8a5SJan Glauber 0 : zdev->irq_map->msi_vecs; 5329a4da8a5SJan Glauber } 5339a4da8a5SJan Glauber 5349a4da8a5SJan Glauber static void zpci_teardown_msi(struct pci_dev *pdev) 5359a4da8a5SJan Glauber { 5369a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 5379a4da8a5SJan Glauber struct msi_desc *msi; 5389a4da8a5SJan Glauber int aisb, rc; 5399a4da8a5SJan Glauber 5409a4da8a5SJan Glauber rc = zpci_unregister_airq(zdev); 5419a4da8a5SJan Glauber if (rc) { 5429a4da8a5SJan Glauber dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc); 5439a4da8a5SJan Glauber return; 5449a4da8a5SJan Glauber } 5459a4da8a5SJan Glauber 5469a4da8a5SJan Glauber msi = list_first_entry(&pdev->msi_list, struct msi_desc, list); 5479a4da8a5SJan Glauber aisb = irq_to_dev_nr(msi->irq); 5489a4da8a5SJan Glauber 5499a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) 5509a4da8a5SJan Glauber zpci_teardown_msi_irq(zdev, msi); 5519a4da8a5SJan Glauber 5529a4da8a5SJan Glauber clear_bit(aisb, bucket->alloc); 5539a4da8a5SJan Glauber if (aisb + 1 == aisb_max) 5549a4da8a5SJan Glauber aisb_max--; 5559a4da8a5SJan Glauber } 5569a4da8a5SJan Glauber 5579a4da8a5SJan Glauber int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 5589a4da8a5SJan Glauber { 5599a4da8a5SJan Glauber pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec); 5609a4da8a5SJan Glauber if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI) 5619a4da8a5SJan Glauber return -EINVAL; 5629a4da8a5SJan Glauber return zpci_setup_msi(pdev, nvec); 5639a4da8a5SJan Glauber } 5649a4da8a5SJan Glauber 5659a4da8a5SJan Glauber void arch_teardown_msi_irqs(struct pci_dev *pdev) 5669a4da8a5SJan Glauber { 5679a4da8a5SJan Glauber pr_info("%s: on pdev: %p\n", __func__, pdev); 5689a4da8a5SJan Glauber zpci_teardown_msi(pdev); 5699a4da8a5SJan Glauber } 5709a4da8a5SJan Glauber 571cd248341SJan Glauber static void zpci_map_resources(struct zpci_dev *zdev) 572cd248341SJan Glauber { 573cd248341SJan Glauber struct pci_dev *pdev = zdev->pdev; 574cd248341SJan Glauber resource_size_t len; 575cd248341SJan Glauber int i; 576cd248341SJan Glauber 577cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 578cd248341SJan Glauber len = pci_resource_len(pdev, i); 579cd248341SJan Glauber if (!len) 580cd248341SJan Glauber continue; 581cd248341SJan Glauber pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0); 582cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 583cd248341SJan Glauber pr_debug("BAR%i: -> start: %Lx end: %Lx\n", 584cd248341SJan Glauber i, pdev->resource[i].start, pdev->resource[i].end); 585cd248341SJan Glauber } 586cd248341SJan Glauber }; 587cd248341SJan Glauber 588cd248341SJan Glauber static void zpci_unmap_resources(struct pci_dev *pdev) 589cd248341SJan Glauber { 590cd248341SJan Glauber resource_size_t len; 591cd248341SJan Glauber int i; 592cd248341SJan Glauber 593cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 594cd248341SJan Glauber len = pci_resource_len(pdev, i); 595cd248341SJan Glauber if (!len) 596cd248341SJan Glauber continue; 597cd248341SJan Glauber pci_iounmap(pdev, (void *) pdev->resource[i].start); 598cd248341SJan Glauber } 599cd248341SJan Glauber }; 600cd248341SJan Glauber 601cd248341SJan Glauber struct zpci_dev *zpci_alloc_device(void) 602cd248341SJan Glauber { 603cd248341SJan Glauber struct zpci_dev *zdev; 604cd248341SJan Glauber 605cd248341SJan Glauber /* Alloc memory for our private pci device data */ 606cd248341SJan Glauber zdev = kzalloc(sizeof(*zdev), GFP_KERNEL); 607cd248341SJan Glauber if (!zdev) 608cd248341SJan Glauber return ERR_PTR(-ENOMEM); 6099a4da8a5SJan Glauber 6109a4da8a5SJan Glauber /* Alloc aibv & callback space */ 6114118fee7SWei Yongjun zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL); 6129a4da8a5SJan Glauber if (!zdev->irq_map) 6139a4da8a5SJan Glauber goto error; 6149a4da8a5SJan Glauber WARN_ON((u64) zdev->irq_map & 0xff); 615cd248341SJan Glauber return zdev; 6169a4da8a5SJan Glauber 6179a4da8a5SJan Glauber error: 6189a4da8a5SJan Glauber kfree(zdev); 6199a4da8a5SJan Glauber return ERR_PTR(-ENOMEM); 620cd248341SJan Glauber } 621cd248341SJan Glauber 622cd248341SJan Glauber void zpci_free_device(struct zpci_dev *zdev) 623cd248341SJan Glauber { 6249a4da8a5SJan Glauber kmem_cache_free(zdev_irq_cache, zdev->irq_map); 625cd248341SJan Glauber kfree(zdev); 626cd248341SJan Glauber } 627cd248341SJan Glauber 628cd248341SJan Glauber /* Called on removal of pci_dev, leaves zpci and bus device */ 629cd248341SJan Glauber static void zpci_remove_device(struct pci_dev *pdev) 630cd248341SJan Glauber { 631cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 632cd248341SJan Glauber 633cd248341SJan Glauber dev_info(&pdev->dev, "Removing device %u\n", zdev->domain); 634cd248341SJan Glauber zdev->state = ZPCI_FN_STATE_CONFIGURED; 635828b35f6SJan Glauber zpci_dma_exit_device(zdev); 6361e8da956SJan Glauber zpci_sysfs_remove_device(&pdev->dev); 637cd248341SJan Glauber zpci_unmap_resources(pdev); 638cd248341SJan Glauber list_del(&zdev->entry); /* can be called from init */ 639cd248341SJan Glauber zdev->pdev = NULL; 640cd248341SJan Glauber } 641cd248341SJan Glauber 642cd248341SJan Glauber static void zpci_scan_devices(void) 643cd248341SJan Glauber { 644cd248341SJan Glauber struct zpci_dev *zdev; 645cd248341SJan Glauber 646cd248341SJan Glauber mutex_lock(&zpci_list_lock); 647cd248341SJan Glauber list_for_each_entry(zdev, &zpci_list, entry) 648cd248341SJan Glauber if (zdev->state == ZPCI_FN_STATE_CONFIGURED) 649cd248341SJan Glauber zpci_scan_device(zdev); 650cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 651cd248341SJan Glauber } 652cd248341SJan Glauber 653cd248341SJan Glauber /* 654cd248341SJan Glauber * Too late for any s390 specific setup, since interrupts must be set up 655cd248341SJan Glauber * already which requires DMA setup too and the pci scan will access the 656cd248341SJan Glauber * config space, which only works if the function handle is enabled. 657cd248341SJan Glauber */ 658cd248341SJan Glauber int pcibios_enable_device(struct pci_dev *pdev, int mask) 659cd248341SJan Glauber { 660cd248341SJan Glauber struct resource *res; 661cd248341SJan Glauber u16 cmd; 662cd248341SJan Glauber int i; 663cd248341SJan Glauber 664cd248341SJan Glauber pci_read_config_word(pdev, PCI_COMMAND, &cmd); 665cd248341SJan Glauber 666cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 667cd248341SJan Glauber res = &pdev->resource[i]; 668cd248341SJan Glauber 669cd248341SJan Glauber if (res->flags & IORESOURCE_IO) 670cd248341SJan Glauber return -EINVAL; 671cd248341SJan Glauber 672cd248341SJan Glauber if (res->flags & IORESOURCE_MEM) 673cd248341SJan Glauber cmd |= PCI_COMMAND_MEMORY; 674cd248341SJan Glauber } 675cd248341SJan Glauber pci_write_config_word(pdev, PCI_COMMAND, cmd); 676cd248341SJan Glauber return 0; 677cd248341SJan Glauber } 678cd248341SJan Glauber 679cd248341SJan Glauber void pcibios_disable_device(struct pci_dev *pdev) 680cd248341SJan Glauber { 681cd248341SJan Glauber zpci_remove_device(pdev); 682cd248341SJan Glauber pdev->sysdata = NULL; 683cd248341SJan Glauber } 684cd248341SJan Glauber 6851e8da956SJan Glauber int pcibios_add_platform_entries(struct pci_dev *pdev) 6861e8da956SJan Glauber { 6871e8da956SJan Glauber return zpci_sysfs_add_device(&pdev->dev); 6881e8da956SJan Glauber } 6891e8da956SJan Glauber 6909a4da8a5SJan Glauber int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data) 6919a4da8a5SJan Glauber { 6929a4da8a5SJan Glauber int msi_nr = irq_to_msi_nr(irq); 6939a4da8a5SJan Glauber struct zdev_irq_map *imap; 6949a4da8a5SJan Glauber struct msi_desc *msi; 6959a4da8a5SJan Glauber 6969a4da8a5SJan Glauber msi = irq_get_msi_desc(irq); 6979a4da8a5SJan Glauber if (!msi) 6989a4da8a5SJan Glauber return -EIO; 6999a4da8a5SJan Glauber 7009a4da8a5SJan Glauber imap = get_imap(irq); 7019a4da8a5SJan Glauber spin_lock_init(&imap->lock); 7029a4da8a5SJan Glauber 7039a4da8a5SJan Glauber pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr); 7049a4da8a5SJan Glauber imap->cb[msi_nr].handler = handler; 7059a4da8a5SJan Glauber imap->cb[msi_nr].data = data; 7069a4da8a5SJan Glauber 7079a4da8a5SJan Glauber /* 7089a4da8a5SJan Glauber * The generic MSI code returns with the interrupt disabled on the 7099a4da8a5SJan Glauber * card, using the MSI mask bits. Firmware doesn't appear to unmask 7109a4da8a5SJan Glauber * at that level, so we do it here by hand. 7119a4da8a5SJan Glauber */ 7129a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 0); 7139a4da8a5SJan Glauber return 0; 7149a4da8a5SJan Glauber } 7159a4da8a5SJan Glauber 7169a4da8a5SJan Glauber void zpci_free_irq(unsigned int irq) 7179a4da8a5SJan Glauber { 7189a4da8a5SJan Glauber struct zdev_irq_map *imap = get_imap(irq); 7199a4da8a5SJan Glauber int msi_nr = irq_to_msi_nr(irq); 7209a4da8a5SJan Glauber unsigned long flags; 7219a4da8a5SJan Glauber 7229a4da8a5SJan Glauber pr_debug("%s: for irq: %d\n", __func__, irq); 7239a4da8a5SJan Glauber 7249a4da8a5SJan Glauber spin_lock_irqsave(&imap->lock, flags); 7259a4da8a5SJan Glauber imap->cb[msi_nr].handler = NULL; 7269a4da8a5SJan Glauber imap->cb[msi_nr].data = NULL; 7279a4da8a5SJan Glauber spin_unlock_irqrestore(&imap->lock, flags); 7289a4da8a5SJan Glauber } 7299a4da8a5SJan Glauber 7309a4da8a5SJan Glauber int request_irq(unsigned int irq, irq_handler_t handler, 7319a4da8a5SJan Glauber unsigned long irqflags, const char *devname, void *dev_id) 7329a4da8a5SJan Glauber { 7339a4da8a5SJan Glauber pr_debug("%s: irq: %d handler: %p flags: %lx dev: %s\n", 7349a4da8a5SJan Glauber __func__, irq, handler, irqflags, devname); 7359a4da8a5SJan Glauber 7369a4da8a5SJan Glauber return zpci_request_irq(irq, handler, dev_id); 7379a4da8a5SJan Glauber } 7389a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(request_irq); 7399a4da8a5SJan Glauber 7409a4da8a5SJan Glauber void free_irq(unsigned int irq, void *dev_id) 7419a4da8a5SJan Glauber { 7429a4da8a5SJan Glauber zpci_free_irq(irq); 7439a4da8a5SJan Glauber } 7449a4da8a5SJan Glauber EXPORT_SYMBOL_GPL(free_irq); 7459a4da8a5SJan Glauber 7469a4da8a5SJan Glauber static int __init zpci_irq_init(void) 7479a4da8a5SJan Glauber { 7489a4da8a5SJan Glauber int cpu, rc; 7499a4da8a5SJan Glauber 7509a4da8a5SJan Glauber bucket = kzalloc(sizeof(*bucket), GFP_KERNEL); 7519a4da8a5SJan Glauber if (!bucket) 7529a4da8a5SJan Glauber return -ENOMEM; 7539a4da8a5SJan Glauber 7549a4da8a5SJan Glauber bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL); 7559a4da8a5SJan Glauber if (!bucket->aisb) { 7569a4da8a5SJan Glauber rc = -ENOMEM; 7579a4da8a5SJan Glauber goto out_aisb; 7589a4da8a5SJan Glauber } 7599a4da8a5SJan Glauber 7609a4da8a5SJan Glauber bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL); 7619a4da8a5SJan Glauber if (!bucket->alloc) { 7629a4da8a5SJan Glauber rc = -ENOMEM; 7639a4da8a5SJan Glauber goto out_alloc; 7649a4da8a5SJan Glauber } 7659a4da8a5SJan Glauber 7669a4da8a5SJan Glauber isc_register(PCI_ISC); 7679a4da8a5SJan Glauber zpci_irq_si = s390_register_adapter_interrupt(&zpci_irq_handler, NULL, PCI_ISC); 7689a4da8a5SJan Glauber if (IS_ERR(zpci_irq_si)) { 7699a4da8a5SJan Glauber rc = PTR_ERR(zpci_irq_si); 7709a4da8a5SJan Glauber zpci_irq_si = NULL; 7719a4da8a5SJan Glauber goto out_ai; 7729a4da8a5SJan Glauber } 7739a4da8a5SJan Glauber 7749a4da8a5SJan Glauber for_each_online_cpu(cpu) 7759a4da8a5SJan Glauber per_cpu(next_sbit, cpu) = 0; 7769a4da8a5SJan Glauber 7779a4da8a5SJan Glauber spin_lock_init(&bucket->lock); 7789a4da8a5SJan Glauber /* set summary to 1 to be called every time for the ISC */ 7799a4da8a5SJan Glauber *zpci_irq_si = 1; 7809a4da8a5SJan Glauber sic_instr(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 7819a4da8a5SJan Glauber return 0; 7829a4da8a5SJan Glauber 7839a4da8a5SJan Glauber out_ai: 7849a4da8a5SJan Glauber isc_unregister(PCI_ISC); 7859a4da8a5SJan Glauber free_page((unsigned long) bucket->alloc); 7869a4da8a5SJan Glauber out_alloc: 7879a4da8a5SJan Glauber free_page((unsigned long) bucket->aisb); 7889a4da8a5SJan Glauber out_aisb: 7899a4da8a5SJan Glauber kfree(bucket); 7909a4da8a5SJan Glauber return rc; 7919a4da8a5SJan Glauber } 7929a4da8a5SJan Glauber 7939a4da8a5SJan Glauber static void zpci_irq_exit(void) 7949a4da8a5SJan Glauber { 7959a4da8a5SJan Glauber free_page((unsigned long) bucket->alloc); 7969a4da8a5SJan Glauber free_page((unsigned long) bucket->aisb); 7979a4da8a5SJan Glauber s390_unregister_adapter_interrupt(zpci_irq_si, PCI_ISC); 7989a4da8a5SJan Glauber isc_unregister(PCI_ISC); 7999a4da8a5SJan Glauber kfree(bucket); 8009a4da8a5SJan Glauber } 8019a4da8a5SJan Glauber 802cd248341SJan Glauber static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size, 803cd248341SJan Glauber unsigned long flags, int domain) 804cd248341SJan Glauber { 805cd248341SJan Glauber struct resource *r; 806cd248341SJan Glauber char *name; 807cd248341SJan Glauber int rc; 808cd248341SJan Glauber 809cd248341SJan Glauber r = kzalloc(sizeof(*r), GFP_KERNEL); 810cd248341SJan Glauber if (!r) 811cd248341SJan Glauber return ERR_PTR(-ENOMEM); 812cd248341SJan Glauber r->start = start; 813cd248341SJan Glauber r->end = r->start + size - 1; 814cd248341SJan Glauber r->flags = flags; 815cd248341SJan Glauber r->parent = &iomem_resource; 816cd248341SJan Glauber name = kmalloc(18, GFP_KERNEL); 817cd248341SJan Glauber if (!name) { 818cd248341SJan Glauber kfree(r); 819cd248341SJan Glauber return ERR_PTR(-ENOMEM); 820cd248341SJan Glauber } 821cd248341SJan Glauber sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR); 822cd248341SJan Glauber r->name = name; 823cd248341SJan Glauber 824cd248341SJan Glauber rc = request_resource(&iomem_resource, r); 825cd248341SJan Glauber if (rc) 826cd248341SJan Glauber pr_debug("request resource %pR failed\n", r); 827cd248341SJan Glauber return r; 828cd248341SJan Glauber } 829cd248341SJan Glauber 830cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 831cd248341SJan Glauber { 832cd248341SJan Glauber int entry; 833cd248341SJan Glauber 834cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 835cd248341SJan Glauber entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 836cd248341SJan Glauber if (entry == ZPCI_IOMAP_MAX_ENTRIES) { 837cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 838cd248341SJan Glauber return -ENOSPC; 839cd248341SJan Glauber } 840cd248341SJan Glauber set_bit(entry, zpci_iomap); 841cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 842cd248341SJan Glauber return entry; 843cd248341SJan Glauber } 844cd248341SJan Glauber 845cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 846cd248341SJan Glauber { 847cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 848cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 849cd248341SJan Glauber clear_bit(entry, zpci_iomap); 850cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 851cd248341SJan Glauber } 852cd248341SJan Glauber 853cd248341SJan Glauber static int zpci_create_device_bus(struct zpci_dev *zdev) 854cd248341SJan Glauber { 855cd248341SJan Glauber struct resource *res; 856cd248341SJan Glauber LIST_HEAD(resources); 857cd248341SJan Glauber int i; 858cd248341SJan Glauber 859cd248341SJan Glauber /* allocate mapping entry for each used bar */ 860cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 861cd248341SJan Glauber unsigned long addr, size, flags; 862cd248341SJan Glauber int entry; 863cd248341SJan Glauber 864cd248341SJan Glauber if (!zdev->bars[i].size) 865cd248341SJan Glauber continue; 866cd248341SJan Glauber entry = zpci_alloc_iomap(zdev); 867cd248341SJan Glauber if (entry < 0) 868cd248341SJan Glauber return entry; 869cd248341SJan Glauber zdev->bars[i].map_idx = entry; 870cd248341SJan Glauber 871cd248341SJan Glauber /* only MMIO is supported */ 872cd248341SJan Glauber flags = IORESOURCE_MEM; 873cd248341SJan Glauber if (zdev->bars[i].val & 8) 874cd248341SJan Glauber flags |= IORESOURCE_PREFETCH; 875cd248341SJan Glauber if (zdev->bars[i].val & 4) 876cd248341SJan Glauber flags |= IORESOURCE_MEM_64; 877cd248341SJan Glauber 878cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48); 879cd248341SJan Glauber 880cd248341SJan Glauber size = 1UL << zdev->bars[i].size; 881cd248341SJan Glauber 882cd248341SJan Glauber res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain); 883cd248341SJan Glauber if (IS_ERR(res)) { 884cd248341SJan Glauber zpci_free_iomap(zdev, entry); 885cd248341SJan Glauber return PTR_ERR(res); 886cd248341SJan Glauber } 887cd248341SJan Glauber pci_add_resource(&resources, res); 888cd248341SJan Glauber } 889cd248341SJan Glauber 890cd248341SJan Glauber zdev->bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 891cd248341SJan Glauber zdev, &resources); 892cd248341SJan Glauber if (!zdev->bus) 893cd248341SJan Glauber return -EIO; 894cd248341SJan Glauber 895cd248341SJan Glauber zdev->bus->max_bus_speed = zdev->max_bus_speed; 896cd248341SJan Glauber return 0; 897cd248341SJan Glauber } 898cd248341SJan Glauber 899cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 900cd248341SJan Glauber { 901cd248341SJan Glauber spin_lock(&zpci_domain_lock); 902cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 903cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 904cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 905cd248341SJan Glauber return -ENOSPC; 906cd248341SJan Glauber } 907cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 908cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 909cd248341SJan Glauber return 0; 910cd248341SJan Glauber } 911cd248341SJan Glauber 912cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 913cd248341SJan Glauber { 914cd248341SJan Glauber spin_lock(&zpci_domain_lock); 915cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 916cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 917cd248341SJan Glauber } 918cd248341SJan Glauber 919a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 920a755a45dSJan Glauber { 921a755a45dSJan Glauber int rc; 922a755a45dSJan Glauber 923a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 924a755a45dSJan Glauber if (rc) 925a755a45dSJan Glauber goto out; 926a755a45dSJan Glauber pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid); 927828b35f6SJan Glauber 928828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 929828b35f6SJan Glauber if (rc) 930828b35f6SJan Glauber goto out_dma; 931a755a45dSJan Glauber return 0; 932828b35f6SJan Glauber 933828b35f6SJan Glauber out_dma: 934828b35f6SJan Glauber clp_disable_fh(zdev); 935a755a45dSJan Glauber out: 936a755a45dSJan Glauber return rc; 937a755a45dSJan Glauber } 938a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 939a755a45dSJan Glauber 940cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 941cd248341SJan Glauber { 942cd248341SJan Glauber int rc; 943cd248341SJan Glauber 944cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 945cd248341SJan Glauber if (rc) 946cd248341SJan Glauber goto out; 947cd248341SJan Glauber 948cd248341SJan Glauber rc = zpci_create_device_bus(zdev); 949cd248341SJan Glauber if (rc) 950cd248341SJan Glauber goto out_bus; 951cd248341SJan Glauber 952cd248341SJan Glauber mutex_lock(&zpci_list_lock); 953cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 9547441b062SJan Glauber if (hotplug_ops.create_slot) 9557441b062SJan Glauber hotplug_ops.create_slot(zdev); 956cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 957cd248341SJan Glauber 958cd248341SJan Glauber if (zdev->state == ZPCI_FN_STATE_STANDBY) 959cd248341SJan Glauber return 0; 960cd248341SJan Glauber 961a755a45dSJan Glauber rc = zpci_enable_device(zdev); 962a755a45dSJan Glauber if (rc) 963a755a45dSJan Glauber goto out_start; 964cd248341SJan Glauber return 0; 965cd248341SJan Glauber 966a755a45dSJan Glauber out_start: 967a755a45dSJan Glauber mutex_lock(&zpci_list_lock); 968a755a45dSJan Glauber list_del(&zdev->entry); 9697441b062SJan Glauber if (hotplug_ops.remove_slot) 9707441b062SJan Glauber hotplug_ops.remove_slot(zdev); 971a755a45dSJan Glauber mutex_unlock(&zpci_list_lock); 972cd248341SJan Glauber out_bus: 973cd248341SJan Glauber zpci_free_domain(zdev); 974cd248341SJan Glauber out: 975cd248341SJan Glauber return rc; 976cd248341SJan Glauber } 977cd248341SJan Glauber 978cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 979cd248341SJan Glauber { 980828b35f6SJan Glauber zpci_dma_exit_device(zdev); 981cd248341SJan Glauber /* 982cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 983cd248341SJan Glauber * do that here. 984cd248341SJan Glauber */ 985cd248341SJan Glauber } 986cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 987cd248341SJan Glauber 988cd248341SJan Glauber int zpci_scan_device(struct zpci_dev *zdev) 989cd248341SJan Glauber { 990cd248341SJan Glauber zdev->pdev = pci_scan_single_device(zdev->bus, ZPCI_DEVFN); 991cd248341SJan Glauber if (!zdev->pdev) { 992cd248341SJan Glauber pr_err("pci_scan_single_device failed for fid: 0x%x\n", 993cd248341SJan Glauber zdev->fid); 994cd248341SJan Glauber goto out; 995cd248341SJan Glauber } 996cd248341SJan Glauber 997cd248341SJan Glauber zpci_map_resources(zdev); 998cd248341SJan Glauber pci_bus_add_devices(zdev->bus); 999cd248341SJan Glauber 1000cd248341SJan Glauber /* now that pdev was added to the bus mark it as used */ 1001cd248341SJan Glauber zdev->state = ZPCI_FN_STATE_ONLINE; 1002cd248341SJan Glauber return 0; 1003cd248341SJan Glauber 1004cd248341SJan Glauber out: 1005828b35f6SJan Glauber zpci_dma_exit_device(zdev); 1006a755a45dSJan Glauber clp_disable_fh(zdev); 1007cd248341SJan Glauber return -EIO; 1008cd248341SJan Glauber } 1009cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_scan_device); 1010cd248341SJan Glauber 1011cd248341SJan Glauber static inline int barsize(u8 size) 1012cd248341SJan Glauber { 1013cd248341SJan Glauber return (size) ? (1 << size) >> 10 : 0; 1014cd248341SJan Glauber } 1015cd248341SJan Glauber 1016cd248341SJan Glauber static int zpci_mem_init(void) 1017cd248341SJan Glauber { 10189a4da8a5SJan Glauber zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map), 10199a4da8a5SJan Glauber L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL); 10209a4da8a5SJan Glauber if (!zdev_irq_cache) 10219a4da8a5SJan Glauber goto error_zdev; 10229a4da8a5SJan Glauber 1023cd248341SJan Glauber /* TODO: use realloc */ 1024cd248341SJan Glauber zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start), 1025cd248341SJan Glauber GFP_KERNEL); 1026cd248341SJan Glauber if (!zpci_iomap_start) 10279a4da8a5SJan Glauber goto error_iomap; 1028cd248341SJan Glauber return 0; 1029cd248341SJan Glauber 10309a4da8a5SJan Glauber error_iomap: 10319a4da8a5SJan Glauber kmem_cache_destroy(zdev_irq_cache); 1032cd248341SJan Glauber error_zdev: 1033cd248341SJan Glauber return -ENOMEM; 1034cd248341SJan Glauber } 1035cd248341SJan Glauber 1036cd248341SJan Glauber static void zpci_mem_exit(void) 1037cd248341SJan Glauber { 1038cd248341SJan Glauber kfree(zpci_iomap_start); 10399a4da8a5SJan Glauber kmem_cache_destroy(zdev_irq_cache); 1040cd248341SJan Glauber } 1041cd248341SJan Glauber 1042cd248341SJan Glauber unsigned int pci_probe = 1; 1043cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_probe); 1044cd248341SJan Glauber 1045cd248341SJan Glauber char * __init pcibios_setup(char *str) 1046cd248341SJan Glauber { 1047cd248341SJan Glauber if (!strcmp(str, "off")) { 1048cd248341SJan Glauber pci_probe = 0; 1049cd248341SJan Glauber return NULL; 1050cd248341SJan Glauber } 1051cd248341SJan Glauber return str; 1052cd248341SJan Glauber } 1053cd248341SJan Glauber 1054cd248341SJan Glauber static int __init pci_base_init(void) 1055cd248341SJan Glauber { 1056cd248341SJan Glauber int rc; 1057cd248341SJan Glauber 1058cd248341SJan Glauber if (!pci_probe) 1059cd248341SJan Glauber return 0; 1060cd248341SJan Glauber 1061cd248341SJan Glauber if (!test_facility(2) || !test_facility(69) 1062cd248341SJan Glauber || !test_facility(71) || !test_facility(72)) 1063cd248341SJan Glauber return 0; 1064cd248341SJan Glauber 1065cd248341SJan Glauber pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n", 1066cd248341SJan Glauber test_facility(69), test_facility(70), 1067cd248341SJan Glauber test_facility(71)); 1068cd248341SJan Glauber 1069cd248341SJan Glauber rc = zpci_mem_init(); 1070cd248341SJan Glauber if (rc) 1071cd248341SJan Glauber goto out_mem; 1072cd248341SJan Glauber 10739a4da8a5SJan Glauber rc = zpci_msihash_init(); 10749a4da8a5SJan Glauber if (rc) 10759a4da8a5SJan Glauber goto out_hash; 10769a4da8a5SJan Glauber 10779a4da8a5SJan Glauber rc = zpci_irq_init(); 10789a4da8a5SJan Glauber if (rc) 10799a4da8a5SJan Glauber goto out_irq; 10809a4da8a5SJan Glauber 1081828b35f6SJan Glauber rc = zpci_dma_init(); 1082828b35f6SJan Glauber if (rc) 1083828b35f6SJan Glauber goto out_dma; 1084828b35f6SJan Glauber 1085a755a45dSJan Glauber rc = clp_find_pci_devices(); 1086a755a45dSJan Glauber if (rc) 1087a755a45dSJan Glauber goto out_find; 1088a755a45dSJan Glauber 1089cd248341SJan Glauber zpci_scan_devices(); 1090cd248341SJan Glauber return 0; 1091cd248341SJan Glauber 1092a755a45dSJan Glauber out_find: 1093828b35f6SJan Glauber zpci_dma_exit(); 1094828b35f6SJan Glauber out_dma: 10959a4da8a5SJan Glauber zpci_irq_exit(); 10969a4da8a5SJan Glauber out_irq: 10979a4da8a5SJan Glauber zpci_msihash_exit(); 10989a4da8a5SJan Glauber out_hash: 1099cd248341SJan Glauber zpci_mem_exit(); 1100cd248341SJan Glauber out_mem: 1101cd248341SJan Glauber return rc; 1102cd248341SJan Glauber } 1103cd248341SJan Glauber subsys_initcall(pci_base_init); 1104