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