Path: blob/master/arch/powerpc/platforms/cell/axon_msi.c
10818 views
/*1* Copyright 2007, Michael Ellerman, IBM Corporation.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation; either version6* 2 of the License, or (at your option) any later version.7*/8910#include <linux/interrupt.h>11#include <linux/irq.h>12#include <linux/kernel.h>13#include <linux/pci.h>14#include <linux/msi.h>15#include <linux/of_platform.h>16#include <linux/debugfs.h>17#include <linux/slab.h>1819#include <asm/dcr.h>20#include <asm/machdep.h>21#include <asm/prom.h>222324/*25* MSIC registers, specified as offsets from dcr_base26*/27#define MSIC_CTRL_REG 0x02829/* Base Address registers specify FIFO location in BE memory */30#define MSIC_BASE_ADDR_HI_REG 0x331#define MSIC_BASE_ADDR_LO_REG 0x43233/* Hold the read/write offsets into the FIFO */34#define MSIC_READ_OFFSET_REG 0x535#define MSIC_WRITE_OFFSET_REG 0x6363738/* MSIC control register flags */39#define MSIC_CTRL_ENABLE 0x000140#define MSIC_CTRL_FIFO_FULL_ENABLE 0x000241#define MSIC_CTRL_IRQ_ENABLE 0x000842#define MSIC_CTRL_FULL_STOP_ENABLE 0x00104344/*45* The MSIC can be configured to use a FIFO of 32KB, 64KB, 128KB or 256KB.46* Currently we're using a 64KB FIFO size.47*/48#define MSIC_FIFO_SIZE_SHIFT 1649#define MSIC_FIFO_SIZE_BYTES (1 << MSIC_FIFO_SIZE_SHIFT)5051/*52* To configure the FIFO size as (1 << n) bytes, we write (n - 15) into bits53* 8-9 of the MSIC control reg.54*/55#define MSIC_CTRL_FIFO_SIZE (((MSIC_FIFO_SIZE_SHIFT - 15) << 8) & 0x300)5657/*58* We need to mask the read/write offsets to make sure they stay within59* the bounds of the FIFO. Also they should always be 16-byte aligned.60*/61#define MSIC_FIFO_SIZE_MASK ((MSIC_FIFO_SIZE_BYTES - 1) & ~0xFu)6263/* Each entry in the FIFO is 16 bytes, the first 4 bytes hold the irq # */64#define MSIC_FIFO_ENTRY_SIZE 0x10656667struct axon_msic {68struct irq_host *irq_host;69__le32 *fifo_virt;70dma_addr_t fifo_phys;71dcr_host_t dcr_host;72u32 read_offset;73#ifdef DEBUG74u32 __iomem *trigger;75#endif76};7778#ifdef DEBUG79void axon_msi_debug_setup(struct device_node *dn, struct axon_msic *msic);80#else81static inline void axon_msi_debug_setup(struct device_node *dn,82struct axon_msic *msic) { }83#endif848586static void msic_dcr_write(struct axon_msic *msic, unsigned int dcr_n, u32 val)87{88pr_devel("axon_msi: dcr_write(0x%x, 0x%x)\n", val, dcr_n);8990dcr_write(msic->dcr_host, dcr_n, val);91}9293static void axon_msi_cascade(unsigned int irq, struct irq_desc *desc)94{95struct irq_chip *chip = irq_desc_get_chip(desc);96struct axon_msic *msic = irq_get_handler_data(irq);97u32 write_offset, msi;98int idx;99int retry = 0;100101write_offset = dcr_read(msic->dcr_host, MSIC_WRITE_OFFSET_REG);102pr_devel("axon_msi: original write_offset 0x%x\n", write_offset);103104/* write_offset doesn't wrap properly, so we have to mask it */105write_offset &= MSIC_FIFO_SIZE_MASK;106107while (msic->read_offset != write_offset && retry < 100) {108idx = msic->read_offset / sizeof(__le32);109msi = le32_to_cpu(msic->fifo_virt[idx]);110msi &= 0xFFFF;111112pr_devel("axon_msi: woff %x roff %x msi %x\n",113write_offset, msic->read_offset, msi);114115if (msi < NR_IRQS && irq_get_chip_data(msi) == msic) {116generic_handle_irq(msi);117msic->fifo_virt[idx] = cpu_to_le32(0xffffffff);118} else {119/*120* Reading the MSIC_WRITE_OFFSET_REG does not121* reliably flush the outstanding DMA to the122* FIFO buffer. Here we were reading stale123* data, so we need to retry.124*/125udelay(1);126retry++;127pr_devel("axon_msi: invalid irq 0x%x!\n", msi);128continue;129}130131if (retry) {132pr_devel("axon_msi: late irq 0x%x, retry %d\n",133msi, retry);134retry = 0;135}136137msic->read_offset += MSIC_FIFO_ENTRY_SIZE;138msic->read_offset &= MSIC_FIFO_SIZE_MASK;139}140141if (retry) {142printk(KERN_WARNING "axon_msi: irq timed out\n");143144msic->read_offset += MSIC_FIFO_ENTRY_SIZE;145msic->read_offset &= MSIC_FIFO_SIZE_MASK;146}147148chip->irq_eoi(&desc->irq_data);149}150151static struct axon_msic *find_msi_translator(struct pci_dev *dev)152{153struct irq_host *irq_host;154struct device_node *dn, *tmp;155const phandle *ph;156struct axon_msic *msic = NULL;157158dn = of_node_get(pci_device_to_OF_node(dev));159if (!dn) {160dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");161return NULL;162}163164for (; dn; dn = of_get_next_parent(dn)) {165ph = of_get_property(dn, "msi-translator", NULL);166if (ph)167break;168}169170if (!ph) {171dev_dbg(&dev->dev,172"axon_msi: no msi-translator property found\n");173goto out_error;174}175176tmp = dn;177dn = of_find_node_by_phandle(*ph);178of_node_put(tmp);179if (!dn) {180dev_dbg(&dev->dev,181"axon_msi: msi-translator doesn't point to a node\n");182goto out_error;183}184185irq_host = irq_find_host(dn);186if (!irq_host) {187dev_dbg(&dev->dev, "axon_msi: no irq_host found for node %s\n",188dn->full_name);189goto out_error;190}191192msic = irq_host->host_data;193194out_error:195of_node_put(dn);196197return msic;198}199200static int axon_msi_check_device(struct pci_dev *dev, int nvec, int type)201{202if (!find_msi_translator(dev))203return -ENODEV;204205return 0;206}207208static int setup_msi_msg_address(struct pci_dev *dev, struct msi_msg *msg)209{210struct device_node *dn;211struct msi_desc *entry;212int len;213const u32 *prop;214215dn = of_node_get(pci_device_to_OF_node(dev));216if (!dn) {217dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");218return -ENODEV;219}220221entry = list_first_entry(&dev->msi_list, struct msi_desc, list);222223for (; dn; dn = of_get_next_parent(dn)) {224if (entry->msi_attrib.is_64) {225prop = of_get_property(dn, "msi-address-64", &len);226if (prop)227break;228}229230prop = of_get_property(dn, "msi-address-32", &len);231if (prop)232break;233}234235if (!prop) {236dev_dbg(&dev->dev,237"axon_msi: no msi-address-(32|64) properties found\n");238return -ENOENT;239}240241switch (len) {242case 8:243msg->address_hi = prop[0];244msg->address_lo = prop[1];245break;246case 4:247msg->address_hi = 0;248msg->address_lo = prop[0];249break;250default:251dev_dbg(&dev->dev,252"axon_msi: malformed msi-address-(32|64) property\n");253of_node_put(dn);254return -EINVAL;255}256257of_node_put(dn);258259return 0;260}261262static int axon_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)263{264unsigned int virq, rc;265struct msi_desc *entry;266struct msi_msg msg;267struct axon_msic *msic;268269msic = find_msi_translator(dev);270if (!msic)271return -ENODEV;272273rc = setup_msi_msg_address(dev, &msg);274if (rc)275return rc;276277/* We rely on being able to stash a virq in a u16 */278BUILD_BUG_ON(NR_IRQS > 65536);279280list_for_each_entry(entry, &dev->msi_list, list) {281virq = irq_create_direct_mapping(msic->irq_host);282if (virq == NO_IRQ) {283dev_warn(&dev->dev,284"axon_msi: virq allocation failed!\n");285return -1;286}287dev_dbg(&dev->dev, "axon_msi: allocated virq 0x%x\n", virq);288289irq_set_msi_desc(virq, entry);290msg.data = virq;291write_msi_msg(virq, &msg);292}293294return 0;295}296297static void axon_msi_teardown_msi_irqs(struct pci_dev *dev)298{299struct msi_desc *entry;300301dev_dbg(&dev->dev, "axon_msi: tearing down msi irqs\n");302303list_for_each_entry(entry, &dev->msi_list, list) {304if (entry->irq == NO_IRQ)305continue;306307irq_set_msi_desc(entry->irq, NULL);308irq_dispose_mapping(entry->irq);309}310}311312static struct irq_chip msic_irq_chip = {313.irq_mask = mask_msi_irq,314.irq_unmask = unmask_msi_irq,315.irq_shutdown = mask_msi_irq,316.name = "AXON-MSI",317};318319static int msic_host_map(struct irq_host *h, unsigned int virq,320irq_hw_number_t hw)321{322irq_set_chip_data(virq, h->host_data);323irq_set_chip_and_handler(virq, &msic_irq_chip, handle_simple_irq);324325return 0;326}327328static struct irq_host_ops msic_host_ops = {329.map = msic_host_map,330};331332static void axon_msi_shutdown(struct platform_device *device)333{334struct axon_msic *msic = dev_get_drvdata(&device->dev);335u32 tmp;336337pr_devel("axon_msi: disabling %s\n",338msic->irq_host->of_node->full_name);339tmp = dcr_read(msic->dcr_host, MSIC_CTRL_REG);340tmp &= ~MSIC_CTRL_ENABLE & ~MSIC_CTRL_IRQ_ENABLE;341msic_dcr_write(msic, MSIC_CTRL_REG, tmp);342}343344static int axon_msi_probe(struct platform_device *device)345{346struct device_node *dn = device->dev.of_node;347struct axon_msic *msic;348unsigned int virq;349int dcr_base, dcr_len;350351pr_devel("axon_msi: setting up dn %s\n", dn->full_name);352353msic = kzalloc(sizeof(struct axon_msic), GFP_KERNEL);354if (!msic) {355printk(KERN_ERR "axon_msi: couldn't allocate msic for %s\n",356dn->full_name);357goto out;358}359360dcr_base = dcr_resource_start(dn, 0);361dcr_len = dcr_resource_len(dn, 0);362363if (dcr_base == 0 || dcr_len == 0) {364printk(KERN_ERR365"axon_msi: couldn't parse dcr properties on %s\n",366dn->full_name);367goto out_free_msic;368}369370msic->dcr_host = dcr_map(dn, dcr_base, dcr_len);371if (!DCR_MAP_OK(msic->dcr_host)) {372printk(KERN_ERR "axon_msi: dcr_map failed for %s\n",373dn->full_name);374goto out_free_msic;375}376377msic->fifo_virt = dma_alloc_coherent(&device->dev, MSIC_FIFO_SIZE_BYTES,378&msic->fifo_phys, GFP_KERNEL);379if (!msic->fifo_virt) {380printk(KERN_ERR "axon_msi: couldn't allocate fifo for %s\n",381dn->full_name);382goto out_free_msic;383}384385virq = irq_of_parse_and_map(dn, 0);386if (virq == NO_IRQ) {387printk(KERN_ERR "axon_msi: irq parse and map failed for %s\n",388dn->full_name);389goto out_free_fifo;390}391memset(msic->fifo_virt, 0xff, MSIC_FIFO_SIZE_BYTES);392393msic->irq_host = irq_alloc_host(dn, IRQ_HOST_MAP_NOMAP,394NR_IRQS, &msic_host_ops, 0);395if (!msic->irq_host) {396printk(KERN_ERR "axon_msi: couldn't allocate irq_host for %s\n",397dn->full_name);398goto out_free_fifo;399}400401msic->irq_host->host_data = msic;402403irq_set_handler_data(virq, msic);404irq_set_chained_handler(virq, axon_msi_cascade);405pr_devel("axon_msi: irq 0x%x setup for axon_msi\n", virq);406407/* Enable the MSIC hardware */408msic_dcr_write(msic, MSIC_BASE_ADDR_HI_REG, msic->fifo_phys >> 32);409msic_dcr_write(msic, MSIC_BASE_ADDR_LO_REG,410msic->fifo_phys & 0xFFFFFFFF);411msic_dcr_write(msic, MSIC_CTRL_REG,412MSIC_CTRL_IRQ_ENABLE | MSIC_CTRL_ENABLE |413MSIC_CTRL_FIFO_SIZE);414415msic->read_offset = dcr_read(msic->dcr_host, MSIC_WRITE_OFFSET_REG)416& MSIC_FIFO_SIZE_MASK;417418dev_set_drvdata(&device->dev, msic);419420ppc_md.setup_msi_irqs = axon_msi_setup_msi_irqs;421ppc_md.teardown_msi_irqs = axon_msi_teardown_msi_irqs;422ppc_md.msi_check_device = axon_msi_check_device;423424axon_msi_debug_setup(dn, msic);425426printk(KERN_DEBUG "axon_msi: setup MSIC on %s\n", dn->full_name);427428return 0;429430out_free_fifo:431dma_free_coherent(&device->dev, MSIC_FIFO_SIZE_BYTES, msic->fifo_virt,432msic->fifo_phys);433out_free_msic:434kfree(msic);435out:436437return -1;438}439440static const struct of_device_id axon_msi_device_id[] = {441{442.compatible = "ibm,axon-msic"443},444{}445};446447static struct platform_driver axon_msi_driver = {448.probe = axon_msi_probe,449.shutdown = axon_msi_shutdown,450.driver = {451.name = "axon-msi",452.owner = THIS_MODULE,453.of_match_table = axon_msi_device_id,454},455};456457static int __init axon_msi_init(void)458{459return platform_driver_register(&axon_msi_driver);460}461subsys_initcall(axon_msi_init);462463464#ifdef DEBUG465static int msic_set(void *data, u64 val)466{467struct axon_msic *msic = data;468out_le32(msic->trigger, val);469return 0;470}471472static int msic_get(void *data, u64 *val)473{474*val = 0;475return 0;476}477478DEFINE_SIMPLE_ATTRIBUTE(fops_msic, msic_get, msic_set, "%llu\n");479480void axon_msi_debug_setup(struct device_node *dn, struct axon_msic *msic)481{482char name[8];483u64 addr;484485addr = of_translate_address(dn, of_get_property(dn, "reg", NULL));486if (addr == OF_BAD_ADDR) {487pr_devel("axon_msi: couldn't translate reg property\n");488return;489}490491msic->trigger = ioremap(addr, 0x4);492if (!msic->trigger) {493pr_devel("axon_msi: ioremap failed\n");494return;495}496497snprintf(name, sizeof(name), "msic_%d", of_node_to_nid(dn));498499if (!debugfs_create_file(name, 0600, powerpc_debugfs_root,500msic, &fops_msic)) {501pr_devel("axon_msi: debugfs_create_file failed!\n");502return;503}504}505#endif /* DEBUG */506507508