0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/interrupt.h>
0011 #include <linux/irqchip/chained_irq.h>
0012 #include <linux/init.h>
0013 #include <linux/module.h>
0014 #include <linux/msi.h>
0015 #include <linux/of_address.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/of_pci.h>
0018 #include <linux/pci.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/slab.h>
0021
0022 #define MSI_STATUS 0x0
0023 #define MSI_ERROR 0x4
0024 #define MSI_INTMASK 0x8
0025
0026 #define MAX_MSI_VECTORS 32
0027
0028 struct altera_msi {
0029 DECLARE_BITMAP(used, MAX_MSI_VECTORS);
0030 struct mutex lock;
0031 struct platform_device *pdev;
0032 struct irq_domain *msi_domain;
0033 struct irq_domain *inner_domain;
0034 void __iomem *csr_base;
0035 void __iomem *vector_base;
0036 phys_addr_t vector_phy;
0037 u32 num_of_vectors;
0038 int irq;
0039 };
0040
0041 static inline void msi_writel(struct altera_msi *msi, const u32 value,
0042 const u32 reg)
0043 {
0044 writel_relaxed(value, msi->csr_base + reg);
0045 }
0046
0047 static inline u32 msi_readl(struct altera_msi *msi, const u32 reg)
0048 {
0049 return readl_relaxed(msi->csr_base + reg);
0050 }
0051
0052 static void altera_msi_isr(struct irq_desc *desc)
0053 {
0054 struct irq_chip *chip = irq_desc_get_chip(desc);
0055 struct altera_msi *msi;
0056 unsigned long status;
0057 u32 bit;
0058 int ret;
0059
0060 chained_irq_enter(chip, desc);
0061 msi = irq_desc_get_handler_data(desc);
0062
0063 while ((status = msi_readl(msi, MSI_STATUS)) != 0) {
0064 for_each_set_bit(bit, &status, msi->num_of_vectors) {
0065
0066 readl_relaxed(msi->vector_base + (bit * sizeof(u32)));
0067
0068 ret = generic_handle_domain_irq(msi->inner_domain, bit);
0069 if (ret)
0070 dev_err_ratelimited(&msi->pdev->dev, "unexpected MSI\n");
0071 }
0072 }
0073
0074 chained_irq_exit(chip, desc);
0075 }
0076
0077 static struct irq_chip altera_msi_irq_chip = {
0078 .name = "Altera PCIe MSI",
0079 .irq_mask = pci_msi_mask_irq,
0080 .irq_unmask = pci_msi_unmask_irq,
0081 };
0082
0083 static struct msi_domain_info altera_msi_domain_info = {
0084 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
0085 MSI_FLAG_PCI_MSIX),
0086 .chip = &altera_msi_irq_chip,
0087 };
0088
0089 static void altera_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
0090 {
0091 struct altera_msi *msi = irq_data_get_irq_chip_data(data);
0092 phys_addr_t addr = msi->vector_phy + (data->hwirq * sizeof(u32));
0093
0094 msg->address_lo = lower_32_bits(addr);
0095 msg->address_hi = upper_32_bits(addr);
0096 msg->data = data->hwirq;
0097
0098 dev_dbg(&msi->pdev->dev, "msi#%d address_hi %#x address_lo %#x\n",
0099 (int)data->hwirq, msg->address_hi, msg->address_lo);
0100 }
0101
0102 static int altera_msi_set_affinity(struct irq_data *irq_data,
0103 const struct cpumask *mask, bool force)
0104 {
0105 return -EINVAL;
0106 }
0107
0108 static struct irq_chip altera_msi_bottom_irq_chip = {
0109 .name = "Altera MSI",
0110 .irq_compose_msi_msg = altera_compose_msi_msg,
0111 .irq_set_affinity = altera_msi_set_affinity,
0112 };
0113
0114 static int altera_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
0115 unsigned int nr_irqs, void *args)
0116 {
0117 struct altera_msi *msi = domain->host_data;
0118 unsigned long bit;
0119 u32 mask;
0120
0121 WARN_ON(nr_irqs != 1);
0122 mutex_lock(&msi->lock);
0123
0124 bit = find_first_zero_bit(msi->used, msi->num_of_vectors);
0125 if (bit >= msi->num_of_vectors) {
0126 mutex_unlock(&msi->lock);
0127 return -ENOSPC;
0128 }
0129
0130 set_bit(bit, msi->used);
0131
0132 mutex_unlock(&msi->lock);
0133
0134 irq_domain_set_info(domain, virq, bit, &altera_msi_bottom_irq_chip,
0135 domain->host_data, handle_simple_irq,
0136 NULL, NULL);
0137
0138 mask = msi_readl(msi, MSI_INTMASK);
0139 mask |= 1 << bit;
0140 msi_writel(msi, mask, MSI_INTMASK);
0141
0142 return 0;
0143 }
0144
0145 static void altera_irq_domain_free(struct irq_domain *domain,
0146 unsigned int virq, unsigned int nr_irqs)
0147 {
0148 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
0149 struct altera_msi *msi = irq_data_get_irq_chip_data(d);
0150 u32 mask;
0151
0152 mutex_lock(&msi->lock);
0153
0154 if (!test_bit(d->hwirq, msi->used)) {
0155 dev_err(&msi->pdev->dev, "trying to free unused MSI#%lu\n",
0156 d->hwirq);
0157 } else {
0158 __clear_bit(d->hwirq, msi->used);
0159 mask = msi_readl(msi, MSI_INTMASK);
0160 mask &= ~(1 << d->hwirq);
0161 msi_writel(msi, mask, MSI_INTMASK);
0162 }
0163
0164 mutex_unlock(&msi->lock);
0165 }
0166
0167 static const struct irq_domain_ops msi_domain_ops = {
0168 .alloc = altera_irq_domain_alloc,
0169 .free = altera_irq_domain_free,
0170 };
0171
0172 static int altera_allocate_domains(struct altera_msi *msi)
0173 {
0174 struct fwnode_handle *fwnode = of_node_to_fwnode(msi->pdev->dev.of_node);
0175
0176 msi->inner_domain = irq_domain_add_linear(NULL, msi->num_of_vectors,
0177 &msi_domain_ops, msi);
0178 if (!msi->inner_domain) {
0179 dev_err(&msi->pdev->dev, "failed to create IRQ domain\n");
0180 return -ENOMEM;
0181 }
0182
0183 msi->msi_domain = pci_msi_create_irq_domain(fwnode,
0184 &altera_msi_domain_info, msi->inner_domain);
0185 if (!msi->msi_domain) {
0186 dev_err(&msi->pdev->dev, "failed to create MSI domain\n");
0187 irq_domain_remove(msi->inner_domain);
0188 return -ENOMEM;
0189 }
0190
0191 return 0;
0192 }
0193
0194 static void altera_free_domains(struct altera_msi *msi)
0195 {
0196 irq_domain_remove(msi->msi_domain);
0197 irq_domain_remove(msi->inner_domain);
0198 }
0199
0200 static int altera_msi_remove(struct platform_device *pdev)
0201 {
0202 struct altera_msi *msi = platform_get_drvdata(pdev);
0203
0204 msi_writel(msi, 0, MSI_INTMASK);
0205 irq_set_chained_handler_and_data(msi->irq, NULL, NULL);
0206
0207 altera_free_domains(msi);
0208
0209 platform_set_drvdata(pdev, NULL);
0210 return 0;
0211 }
0212
0213 static int altera_msi_probe(struct platform_device *pdev)
0214 {
0215 struct altera_msi *msi;
0216 struct device_node *np = pdev->dev.of_node;
0217 struct resource *res;
0218 int ret;
0219
0220 msi = devm_kzalloc(&pdev->dev, sizeof(struct altera_msi),
0221 GFP_KERNEL);
0222 if (!msi)
0223 return -ENOMEM;
0224
0225 mutex_init(&msi->lock);
0226 msi->pdev = pdev;
0227
0228 msi->csr_base = devm_platform_ioremap_resource_byname(pdev, "csr");
0229 if (IS_ERR(msi->csr_base)) {
0230 dev_err(&pdev->dev, "failed to map csr memory\n");
0231 return PTR_ERR(msi->csr_base);
0232 }
0233
0234 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
0235 "vector_slave");
0236 msi->vector_base = devm_ioremap_resource(&pdev->dev, res);
0237 if (IS_ERR(msi->vector_base))
0238 return PTR_ERR(msi->vector_base);
0239
0240 msi->vector_phy = res->start;
0241
0242 if (of_property_read_u32(np, "num-vectors", &msi->num_of_vectors)) {
0243 dev_err(&pdev->dev, "failed to parse the number of vectors\n");
0244 return -EINVAL;
0245 }
0246
0247 ret = altera_allocate_domains(msi);
0248 if (ret)
0249 return ret;
0250
0251 msi->irq = platform_get_irq(pdev, 0);
0252 if (msi->irq < 0) {
0253 ret = msi->irq;
0254 goto err;
0255 }
0256
0257 irq_set_chained_handler_and_data(msi->irq, altera_msi_isr, msi);
0258 platform_set_drvdata(pdev, msi);
0259
0260 return 0;
0261
0262 err:
0263 altera_msi_remove(pdev);
0264 return ret;
0265 }
0266
0267 static const struct of_device_id altera_msi_of_match[] = {
0268 { .compatible = "altr,msi-1.0", NULL },
0269 { },
0270 };
0271
0272 static struct platform_driver altera_msi_driver = {
0273 .driver = {
0274 .name = "altera-msi",
0275 .of_match_table = altera_msi_of_match,
0276 },
0277 .probe = altera_msi_probe,
0278 .remove = altera_msi_remove,
0279 };
0280
0281 static int __init altera_msi_init(void)
0282 {
0283 return platform_driver_register(&altera_msi_driver);
0284 }
0285
0286 static void __exit altera_msi_exit(void)
0287 {
0288 platform_driver_unregister(&altera_msi_driver);
0289 }
0290
0291 subsys_initcall(altera_msi_init);
0292 MODULE_DEVICE_TABLE(of, altera_msi_of_match);
0293 module_exit(altera_msi_exit);
0294 MODULE_LICENSE("GPL v2");