1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Altera PCIe MSI support 4 * 5 * Author: Ley Foon Tan <lftan@altera.com> 6 * 7 * Copyright Altera Corporation (C) 2013-2015. All rights reserved 8 */ 9 10 #include <linux/interrupt.h> 11 #include <linux/irqchip/chained_irq.h> 12 #include <linux/init.h> 13 #include <linux/msi.h> 14 #include <linux/of_address.h> 15 #include <linux/of_irq.h> 16 #include <linux/of_pci.h> 17 #include <linux/pci.h> 18 #include <linux/platform_device.h> 19 #include <linux/slab.h> 20 21 #define MSI_STATUS 0x0 22 #define MSI_ERROR 0x4 23 #define MSI_INTMASK 0x8 24 25 #define MAX_MSI_VECTORS 32 26 27 struct altera_msi { 28 DECLARE_BITMAP(used, MAX_MSI_VECTORS); 29 struct mutex lock; /* protect "used" bitmap */ 30 struct platform_device *pdev; 31 struct irq_domain *msi_domain; 32 struct irq_domain *inner_domain; 33 void __iomem *csr_base; 34 void __iomem *vector_base; 35 phys_addr_t vector_phy; 36 u32 num_of_vectors; 37 int irq; 38 }; 39 40 static inline void msi_writel(struct altera_msi *msi, const u32 value, 41 const u32 reg) 42 { 43 writel_relaxed(value, msi->csr_base + reg); 44 } 45 46 static inline u32 msi_readl(struct altera_msi *msi, const u32 reg) 47 { 48 return readl_relaxed(msi->csr_base + reg); 49 } 50 51 static void altera_msi_isr(struct irq_desc *desc) 52 { 53 struct irq_chip *chip = irq_desc_get_chip(desc); 54 struct altera_msi *msi; 55 unsigned long status; 56 u32 bit; 57 u32 virq; 58 59 chained_irq_enter(chip, desc); 60 msi = irq_desc_get_handler_data(desc); 61 62 while ((status = msi_readl(msi, MSI_STATUS)) != 0) { 63 for_each_set_bit(bit, &status, msi->num_of_vectors) { 64 /* Dummy read from vector to clear the interrupt */ 65 readl_relaxed(msi->vector_base + (bit * sizeof(u32))); 66 67 virq = irq_find_mapping(msi->inner_domain, bit); 68 if (virq) 69 generic_handle_irq(virq); 70 else 71 dev_err(&msi->pdev->dev, "unexpected MSI\n"); 72 } 73 } 74 75 chained_irq_exit(chip, desc); 76 } 77 78 static struct irq_chip altera_msi_irq_chip = { 79 .name = "Altera PCIe MSI", 80 .irq_mask = pci_msi_mask_irq, 81 .irq_unmask = pci_msi_unmask_irq, 82 }; 83 84 static struct msi_domain_info altera_msi_domain_info = { 85 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | 86 MSI_FLAG_PCI_MSIX), 87 .chip = &altera_msi_irq_chip, 88 }; 89 90 static void altera_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) 91 { 92 struct altera_msi *msi = irq_data_get_irq_chip_data(data); 93 phys_addr_t addr = msi->vector_phy + (data->hwirq * sizeof(u32)); 94 95 msg->address_lo = lower_32_bits(addr); 96 msg->address_hi = upper_32_bits(addr); 97 msg->data = data->hwirq; 98 99 dev_dbg(&msi->pdev->dev, "msi#%d address_hi %#x address_lo %#x\n", 100 (int)data->hwirq, msg->address_hi, msg->address_lo); 101 } 102 103 static int altera_msi_set_affinity(struct irq_data *irq_data, 104 const struct cpumask *mask, bool force) 105 { 106 return -EINVAL; 107 } 108 109 static struct irq_chip altera_msi_bottom_irq_chip = { 110 .name = "Altera MSI", 111 .irq_compose_msi_msg = altera_compose_msi_msg, 112 .irq_set_affinity = altera_msi_set_affinity, 113 }; 114 115 static int altera_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, 116 unsigned int nr_irqs, void *args) 117 { 118 struct altera_msi *msi = domain->host_data; 119 unsigned long bit; 120 u32 mask; 121 122 WARN_ON(nr_irqs != 1); 123 mutex_lock(&msi->lock); 124 125 bit = find_first_zero_bit(msi->used, msi->num_of_vectors); 126 if (bit >= msi->num_of_vectors) { 127 mutex_unlock(&msi->lock); 128 return -ENOSPC; 129 } 130 131 set_bit(bit, msi->used); 132 133 mutex_unlock(&msi->lock); 134 135 irq_domain_set_info(domain, virq, bit, &altera_msi_bottom_irq_chip, 136 domain->host_data, handle_simple_irq, 137 NULL, NULL); 138 139 mask = msi_readl(msi, MSI_INTMASK); 140 mask |= 1 << bit; 141 msi_writel(msi, mask, MSI_INTMASK); 142 143 return 0; 144 } 145 146 static void altera_irq_domain_free(struct irq_domain *domain, 147 unsigned int virq, unsigned int nr_irqs) 148 { 149 struct irq_data *d = irq_domain_get_irq_data(domain, virq); 150 struct altera_msi *msi = irq_data_get_irq_chip_data(d); 151 u32 mask; 152 153 mutex_lock(&msi->lock); 154 155 if (!test_bit(d->hwirq, msi->used)) { 156 dev_err(&msi->pdev->dev, "trying to free unused MSI#%lu\n", 157 d->hwirq); 158 } else { 159 __clear_bit(d->hwirq, msi->used); 160 mask = msi_readl(msi, MSI_INTMASK); 161 mask &= ~(1 << d->hwirq); 162 msi_writel(msi, mask, MSI_INTMASK); 163 } 164 165 mutex_unlock(&msi->lock); 166 } 167 168 static const struct irq_domain_ops msi_domain_ops = { 169 .alloc = altera_irq_domain_alloc, 170 .free = altera_irq_domain_free, 171 }; 172 173 static int altera_allocate_domains(struct altera_msi *msi) 174 { 175 struct fwnode_handle *fwnode = of_node_to_fwnode(msi->pdev->dev.of_node); 176 177 msi->inner_domain = irq_domain_add_linear(NULL, msi->num_of_vectors, 178 &msi_domain_ops, msi); 179 if (!msi->inner_domain) { 180 dev_err(&msi->pdev->dev, "failed to create IRQ domain\n"); 181 return -ENOMEM; 182 } 183 184 msi->msi_domain = pci_msi_create_irq_domain(fwnode, 185 &altera_msi_domain_info, msi->inner_domain); 186 if (!msi->msi_domain) { 187 dev_err(&msi->pdev->dev, "failed to create MSI domain\n"); 188 irq_domain_remove(msi->inner_domain); 189 return -ENOMEM; 190 } 191 192 return 0; 193 } 194 195 static void altera_free_domains(struct altera_msi *msi) 196 { 197 irq_domain_remove(msi->msi_domain); 198 irq_domain_remove(msi->inner_domain); 199 } 200 201 static int altera_msi_remove(struct platform_device *pdev) 202 { 203 struct altera_msi *msi = platform_get_drvdata(pdev); 204 205 msi_writel(msi, 0, MSI_INTMASK); 206 irq_set_chained_handler(msi->irq, NULL); 207 irq_set_handler_data(msi->irq, NULL); 208 209 altera_free_domains(msi); 210 211 platform_set_drvdata(pdev, NULL); 212 return 0; 213 } 214 215 static int altera_msi_probe(struct platform_device *pdev) 216 { 217 struct altera_msi *msi; 218 struct device_node *np = pdev->dev.of_node; 219 struct resource *res; 220 int ret; 221 222 msi = devm_kzalloc(&pdev->dev, sizeof(struct altera_msi), 223 GFP_KERNEL); 224 if (!msi) 225 return -ENOMEM; 226 227 mutex_init(&msi->lock); 228 msi->pdev = pdev; 229 230 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csr"); 231 msi->csr_base = devm_ioremap_resource(&pdev->dev, res); 232 if (IS_ERR(msi->csr_base)) { 233 dev_err(&pdev->dev, "failed to map csr memory\n"); 234 return PTR_ERR(msi->csr_base); 235 } 236 237 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 238 "vector_slave"); 239 msi->vector_base = devm_ioremap_resource(&pdev->dev, res); 240 if (IS_ERR(msi->vector_base)) { 241 dev_err(&pdev->dev, "failed to map vector_slave memory\n"); 242 return PTR_ERR(msi->vector_base); 243 } 244 245 msi->vector_phy = res->start; 246 247 if (of_property_read_u32(np, "num-vectors", &msi->num_of_vectors)) { 248 dev_err(&pdev->dev, "failed to parse the number of vectors\n"); 249 return -EINVAL; 250 } 251 252 ret = altera_allocate_domains(msi); 253 if (ret) 254 return ret; 255 256 msi->irq = platform_get_irq(pdev, 0); 257 if (msi->irq < 0) { 258 dev_err(&pdev->dev, "failed to map IRQ: %d\n", msi->irq); 259 ret = msi->irq; 260 goto err; 261 } 262 263 irq_set_chained_handler_and_data(msi->irq, altera_msi_isr, msi); 264 platform_set_drvdata(pdev, msi); 265 266 return 0; 267 268 err: 269 altera_msi_remove(pdev); 270 return ret; 271 } 272 273 static const struct of_device_id altera_msi_of_match[] = { 274 { .compatible = "altr,msi-1.0", NULL }, 275 { }, 276 }; 277 278 static struct platform_driver altera_msi_driver = { 279 .driver = { 280 .name = "altera-msi", 281 .of_match_table = altera_msi_of_match, 282 }, 283 .probe = altera_msi_probe, 284 .remove = altera_msi_remove, 285 }; 286 287 static int __init altera_msi_init(void) 288 { 289 return platform_driver_register(&altera_msi_driver); 290 } 291 subsys_initcall(altera_msi_init); 292