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