Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2015 Broadcom Corporation |
| 4 | */ |
| 5 | |
| 6 | #include <linux/interrupt.h> |
| 7 | #include <linux/irqchip/chained_irq.h> |
| 8 | #include <linux/irqdomain.h> |
| 9 | #include <linux/msi.h> |
| 10 | #include <linux/of_irq.h> |
| 11 | #include <linux/of_pci.h> |
| 12 | #include <linux/pci.h> |
| 13 | |
| 14 | #include "pcie-iproc.h" |
| 15 | |
| 16 | #define IPROC_MSI_INTR_EN_SHIFT 11 |
| 17 | #define IPROC_MSI_INTR_EN BIT(IPROC_MSI_INTR_EN_SHIFT) |
| 18 | #define IPROC_MSI_INT_N_EVENT_SHIFT 1 |
| 19 | #define IPROC_MSI_INT_N_EVENT BIT(IPROC_MSI_INT_N_EVENT_SHIFT) |
| 20 | #define IPROC_MSI_EQ_EN_SHIFT 0 |
| 21 | #define IPROC_MSI_EQ_EN BIT(IPROC_MSI_EQ_EN_SHIFT) |
| 22 | |
| 23 | #define IPROC_MSI_EQ_MASK 0x3f |
| 24 | |
| 25 | /* Max number of GIC interrupts */ |
| 26 | #define NR_HW_IRQS 6 |
| 27 | |
| 28 | /* Number of entries in each event queue */ |
| 29 | #define EQ_LEN 64 |
| 30 | |
| 31 | /* Size of each event queue memory region */ |
| 32 | #define EQ_MEM_REGION_SIZE SZ_4K |
| 33 | |
| 34 | /* Size of each MSI address region */ |
| 35 | #define MSI_MEM_REGION_SIZE SZ_4K |
| 36 | |
| 37 | enum iproc_msi_reg { |
| 38 | IPROC_MSI_EQ_PAGE = 0, |
| 39 | IPROC_MSI_EQ_PAGE_UPPER, |
| 40 | IPROC_MSI_PAGE, |
| 41 | IPROC_MSI_PAGE_UPPER, |
| 42 | IPROC_MSI_CTRL, |
| 43 | IPROC_MSI_EQ_HEAD, |
| 44 | IPROC_MSI_EQ_TAIL, |
| 45 | IPROC_MSI_INTS_EN, |
| 46 | IPROC_MSI_REG_SIZE, |
| 47 | }; |
| 48 | |
| 49 | struct iproc_msi; |
| 50 | |
| 51 | /** |
| 52 | * iProc MSI group |
| 53 | * |
| 54 | * One MSI group is allocated per GIC interrupt, serviced by one iProc MSI |
| 55 | * event queue. |
| 56 | * |
| 57 | * @msi: pointer to iProc MSI data |
| 58 | * @gic_irq: GIC interrupt |
| 59 | * @eq: Event queue number |
| 60 | */ |
| 61 | struct iproc_msi_grp { |
| 62 | struct iproc_msi *msi; |
| 63 | int gic_irq; |
| 64 | unsigned int eq; |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * iProc event queue based MSI |
| 69 | * |
| 70 | * Only meant to be used on platforms without MSI support integrated into the |
| 71 | * GIC. |
| 72 | * |
| 73 | * @pcie: pointer to iProc PCIe data |
| 74 | * @reg_offsets: MSI register offsets |
| 75 | * @grps: MSI groups |
| 76 | * @nr_irqs: number of total interrupts connected to GIC |
| 77 | * @nr_cpus: number of toal CPUs |
| 78 | * @has_inten_reg: indicates the MSI interrupt enable register needs to be |
| 79 | * set explicitly (required for some legacy platforms) |
| 80 | * @bitmap: MSI vector bitmap |
| 81 | * @bitmap_lock: lock to protect access to the MSI bitmap |
| 82 | * @nr_msi_vecs: total number of MSI vectors |
| 83 | * @inner_domain: inner IRQ domain |
| 84 | * @msi_domain: MSI IRQ domain |
| 85 | * @nr_eq_region: required number of 4K aligned memory region for MSI event |
| 86 | * queues |
| 87 | * @nr_msi_region: required number of 4K aligned address region for MSI posted |
| 88 | * writes |
| 89 | * @eq_cpu: pointer to allocated memory region for MSI event queues |
| 90 | * @eq_dma: DMA address of MSI event queues |
| 91 | * @msi_addr: MSI address |
| 92 | */ |
| 93 | struct iproc_msi { |
| 94 | struct iproc_pcie *pcie; |
| 95 | const u16 (*reg_offsets)[IPROC_MSI_REG_SIZE]; |
| 96 | struct iproc_msi_grp *grps; |
| 97 | int nr_irqs; |
| 98 | int nr_cpus; |
| 99 | bool has_inten_reg; |
| 100 | unsigned long *bitmap; |
| 101 | struct mutex bitmap_lock; |
| 102 | unsigned int nr_msi_vecs; |
| 103 | struct irq_domain *inner_domain; |
| 104 | struct irq_domain *msi_domain; |
| 105 | unsigned int nr_eq_region; |
| 106 | unsigned int nr_msi_region; |
| 107 | void *eq_cpu; |
| 108 | dma_addr_t eq_dma; |
| 109 | phys_addr_t msi_addr; |
| 110 | }; |
| 111 | |
| 112 | static const u16 iproc_msi_reg_paxb[NR_HW_IRQS][IPROC_MSI_REG_SIZE] = { |
| 113 | { 0x200, 0x2c0, 0x204, 0x2c4, 0x210, 0x250, 0x254, 0x208 }, |
| 114 | { 0x200, 0x2c0, 0x204, 0x2c4, 0x214, 0x258, 0x25c, 0x208 }, |
| 115 | { 0x200, 0x2c0, 0x204, 0x2c4, 0x218, 0x260, 0x264, 0x208 }, |
| 116 | { 0x200, 0x2c0, 0x204, 0x2c4, 0x21c, 0x268, 0x26c, 0x208 }, |
| 117 | { 0x200, 0x2c0, 0x204, 0x2c4, 0x220, 0x270, 0x274, 0x208 }, |
| 118 | { 0x200, 0x2c0, 0x204, 0x2c4, 0x224, 0x278, 0x27c, 0x208 }, |
| 119 | }; |
| 120 | |
| 121 | static const u16 iproc_msi_reg_paxc[NR_HW_IRQS][IPROC_MSI_REG_SIZE] = { |
| 122 | { 0xc00, 0xc04, 0xc08, 0xc0c, 0xc40, 0xc50, 0xc60 }, |
| 123 | { 0xc10, 0xc14, 0xc18, 0xc1c, 0xc44, 0xc54, 0xc64 }, |
| 124 | { 0xc20, 0xc24, 0xc28, 0xc2c, 0xc48, 0xc58, 0xc68 }, |
| 125 | { 0xc30, 0xc34, 0xc38, 0xc3c, 0xc4c, 0xc5c, 0xc6c }, |
| 126 | }; |
| 127 | |
| 128 | static inline u32 iproc_msi_read_reg(struct iproc_msi *msi, |
| 129 | enum iproc_msi_reg reg, |
| 130 | unsigned int eq) |
| 131 | { |
| 132 | struct iproc_pcie *pcie = msi->pcie; |
| 133 | |
| 134 | return readl_relaxed(pcie->base + msi->reg_offsets[eq][reg]); |
| 135 | } |
| 136 | |
| 137 | static inline void iproc_msi_write_reg(struct iproc_msi *msi, |
| 138 | enum iproc_msi_reg reg, |
| 139 | int eq, u32 val) |
| 140 | { |
| 141 | struct iproc_pcie *pcie = msi->pcie; |
| 142 | |
| 143 | writel_relaxed(val, pcie->base + msi->reg_offsets[eq][reg]); |
| 144 | } |
| 145 | |
| 146 | static inline u32 hwirq_to_group(struct iproc_msi *msi, unsigned long hwirq) |
| 147 | { |
| 148 | return (hwirq % msi->nr_irqs); |
| 149 | } |
| 150 | |
| 151 | static inline unsigned int iproc_msi_addr_offset(struct iproc_msi *msi, |
| 152 | unsigned long hwirq) |
| 153 | { |
| 154 | if (msi->nr_msi_region > 1) |
| 155 | return hwirq_to_group(msi, hwirq) * MSI_MEM_REGION_SIZE; |
| 156 | else |
| 157 | return hwirq_to_group(msi, hwirq) * sizeof(u32); |
| 158 | } |
| 159 | |
| 160 | static inline unsigned int iproc_msi_eq_offset(struct iproc_msi *msi, u32 eq) |
| 161 | { |
| 162 | if (msi->nr_eq_region > 1) |
| 163 | return eq * EQ_MEM_REGION_SIZE; |
| 164 | else |
| 165 | return eq * EQ_LEN * sizeof(u32); |
| 166 | } |
| 167 | |
| 168 | static struct irq_chip iproc_msi_irq_chip = { |
| 169 | .name = "iProc-MSI", |
| 170 | }; |
| 171 | |
| 172 | static struct msi_domain_info iproc_msi_domain_info = { |
| 173 | .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 174 | MSI_FLAG_PCI_MSIX, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 175 | .chip = &iproc_msi_irq_chip, |
| 176 | }; |
| 177 | |
| 178 | /* |
| 179 | * In iProc PCIe core, each MSI group is serviced by a GIC interrupt and a |
| 180 | * dedicated event queue. Each MSI group can support up to 64 MSI vectors. |
| 181 | * |
| 182 | * The number of MSI groups varies between different iProc SoCs. The total |
| 183 | * number of CPU cores also varies. To support MSI IRQ affinity, we |
| 184 | * distribute GIC interrupts across all available CPUs. MSI vector is moved |
| 185 | * from one GIC interrupt to another to steer to the target CPU. |
| 186 | * |
| 187 | * Assuming: |
| 188 | * - the number of MSI groups is M |
| 189 | * - the number of CPU cores is N |
| 190 | * - M is always a multiple of N |
| 191 | * |
| 192 | * Total number of raw MSI vectors = M * 64 |
| 193 | * Total number of supported MSI vectors = (M * 64) / N |
| 194 | */ |
| 195 | static inline int hwirq_to_cpu(struct iproc_msi *msi, unsigned long hwirq) |
| 196 | { |
| 197 | return (hwirq % msi->nr_cpus); |
| 198 | } |
| 199 | |
| 200 | static inline unsigned long hwirq_to_canonical_hwirq(struct iproc_msi *msi, |
| 201 | unsigned long hwirq) |
| 202 | { |
| 203 | return (hwirq - hwirq_to_cpu(msi, hwirq)); |
| 204 | } |
| 205 | |
| 206 | static int iproc_msi_irq_set_affinity(struct irq_data *data, |
| 207 | const struct cpumask *mask, bool force) |
| 208 | { |
| 209 | struct iproc_msi *msi = irq_data_get_irq_chip_data(data); |
| 210 | int target_cpu = cpumask_first(mask); |
| 211 | int curr_cpu; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 212 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 213 | |
| 214 | curr_cpu = hwirq_to_cpu(msi, data->hwirq); |
| 215 | if (curr_cpu == target_cpu) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 216 | ret = IRQ_SET_MASK_OK_DONE; |
| 217 | else { |
| 218 | /* steer MSI to the target CPU */ |
| 219 | data->hwirq = hwirq_to_canonical_hwirq(msi, data->hwirq) + target_cpu; |
| 220 | ret = IRQ_SET_MASK_OK; |
| 221 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 222 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 223 | irq_data_update_effective_affinity(data, cpumask_of(target_cpu)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 224 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 225 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void iproc_msi_irq_compose_msi_msg(struct irq_data *data, |
| 229 | struct msi_msg *msg) |
| 230 | { |
| 231 | struct iproc_msi *msi = irq_data_get_irq_chip_data(data); |
| 232 | dma_addr_t addr; |
| 233 | |
| 234 | addr = msi->msi_addr + iproc_msi_addr_offset(msi, data->hwirq); |
| 235 | msg->address_lo = lower_32_bits(addr); |
| 236 | msg->address_hi = upper_32_bits(addr); |
| 237 | msg->data = data->hwirq << 5; |
| 238 | } |
| 239 | |
| 240 | static struct irq_chip iproc_msi_bottom_irq_chip = { |
| 241 | .name = "MSI", |
| 242 | .irq_set_affinity = iproc_msi_irq_set_affinity, |
| 243 | .irq_compose_msi_msg = iproc_msi_irq_compose_msi_msg, |
| 244 | }; |
| 245 | |
| 246 | static int iproc_msi_irq_domain_alloc(struct irq_domain *domain, |
| 247 | unsigned int virq, unsigned int nr_irqs, |
| 248 | void *args) |
| 249 | { |
| 250 | struct iproc_msi *msi = domain->host_data; |
| 251 | int hwirq, i; |
| 252 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 253 | if (msi->nr_cpus > 1 && nr_irqs > 1) |
| 254 | return -EINVAL; |
| 255 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 256 | mutex_lock(&msi->bitmap_lock); |
| 257 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 258 | /* |
| 259 | * Allocate 'nr_irqs' multiplied by 'nr_cpus' number of MSI vectors |
| 260 | * each time |
| 261 | */ |
| 262 | hwirq = bitmap_find_free_region(msi->bitmap, msi->nr_msi_vecs, |
| 263 | order_base_2(msi->nr_cpus * nr_irqs)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 264 | |
| 265 | mutex_unlock(&msi->bitmap_lock); |
| 266 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 267 | if (hwirq < 0) |
| 268 | return -ENOSPC; |
| 269 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 270 | for (i = 0; i < nr_irqs; i++) { |
| 271 | irq_domain_set_info(domain, virq + i, hwirq + i, |
| 272 | &iproc_msi_bottom_irq_chip, |
| 273 | domain->host_data, handle_simple_irq, |
| 274 | NULL, NULL); |
| 275 | } |
| 276 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 277 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void iproc_msi_irq_domain_free(struct irq_domain *domain, |
| 281 | unsigned int virq, unsigned int nr_irqs) |
| 282 | { |
| 283 | struct irq_data *data = irq_domain_get_irq_data(domain, virq); |
| 284 | struct iproc_msi *msi = irq_data_get_irq_chip_data(data); |
| 285 | unsigned int hwirq; |
| 286 | |
| 287 | mutex_lock(&msi->bitmap_lock); |
| 288 | |
| 289 | hwirq = hwirq_to_canonical_hwirq(msi, data->hwirq); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 290 | bitmap_release_region(msi->bitmap, hwirq, |
| 291 | order_base_2(msi->nr_cpus * nr_irqs)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 292 | |
| 293 | mutex_unlock(&msi->bitmap_lock); |
| 294 | |
| 295 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 296 | } |
| 297 | |
| 298 | static const struct irq_domain_ops msi_domain_ops = { |
| 299 | .alloc = iproc_msi_irq_domain_alloc, |
| 300 | .free = iproc_msi_irq_domain_free, |
| 301 | }; |
| 302 | |
| 303 | static inline u32 decode_msi_hwirq(struct iproc_msi *msi, u32 eq, u32 head) |
| 304 | { |
| 305 | u32 *msg, hwirq; |
| 306 | unsigned int offs; |
| 307 | |
| 308 | offs = iproc_msi_eq_offset(msi, eq) + head * sizeof(u32); |
| 309 | msg = (u32 *)(msi->eq_cpu + offs); |
| 310 | hwirq = readl(msg); |
| 311 | hwirq = (hwirq >> 5) + (hwirq & 0x1f); |
| 312 | |
| 313 | /* |
| 314 | * Since we have multiple hwirq mapped to a single MSI vector, |
| 315 | * now we need to derive the hwirq at CPU0. It can then be used to |
| 316 | * mapped back to virq. |
| 317 | */ |
| 318 | return hwirq_to_canonical_hwirq(msi, hwirq); |
| 319 | } |
| 320 | |
| 321 | static void iproc_msi_handler(struct irq_desc *desc) |
| 322 | { |
| 323 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 324 | struct iproc_msi_grp *grp; |
| 325 | struct iproc_msi *msi; |
| 326 | u32 eq, head, tail, nr_events; |
| 327 | unsigned long hwirq; |
| 328 | int virq; |
| 329 | |
| 330 | chained_irq_enter(chip, desc); |
| 331 | |
| 332 | grp = irq_desc_get_handler_data(desc); |
| 333 | msi = grp->msi; |
| 334 | eq = grp->eq; |
| 335 | |
| 336 | /* |
| 337 | * iProc MSI event queue is tracked by head and tail pointers. Head |
| 338 | * pointer indicates the next entry (MSI data) to be consumed by SW in |
| 339 | * the queue and needs to be updated by SW. iProc MSI core uses the |
| 340 | * tail pointer as the next data insertion point. |
| 341 | * |
| 342 | * Entries between head and tail pointers contain valid MSI data. MSI |
| 343 | * data is guaranteed to be in the event queue memory before the tail |
| 344 | * pointer is updated by the iProc MSI core. |
| 345 | */ |
| 346 | head = iproc_msi_read_reg(msi, IPROC_MSI_EQ_HEAD, |
| 347 | eq) & IPROC_MSI_EQ_MASK; |
| 348 | do { |
| 349 | tail = iproc_msi_read_reg(msi, IPROC_MSI_EQ_TAIL, |
| 350 | eq) & IPROC_MSI_EQ_MASK; |
| 351 | |
| 352 | /* |
| 353 | * Figure out total number of events (MSI data) to be |
| 354 | * processed. |
| 355 | */ |
| 356 | nr_events = (tail < head) ? |
| 357 | (EQ_LEN - (head - tail)) : (tail - head); |
| 358 | if (!nr_events) |
| 359 | break; |
| 360 | |
| 361 | /* process all outstanding events */ |
| 362 | while (nr_events--) { |
| 363 | hwirq = decode_msi_hwirq(msi, eq, head); |
| 364 | virq = irq_find_mapping(msi->inner_domain, hwirq); |
| 365 | generic_handle_irq(virq); |
| 366 | |
| 367 | head++; |
| 368 | head %= EQ_LEN; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Now all outstanding events have been processed. Update the |
| 373 | * head pointer. |
| 374 | */ |
| 375 | iproc_msi_write_reg(msi, IPROC_MSI_EQ_HEAD, eq, head); |
| 376 | |
| 377 | /* |
| 378 | * Now go read the tail pointer again to see if there are new |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 379 | * outstanding events that came in during the above window. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 380 | */ |
| 381 | } while (true); |
| 382 | |
| 383 | chained_irq_exit(chip, desc); |
| 384 | } |
| 385 | |
| 386 | static void iproc_msi_enable(struct iproc_msi *msi) |
| 387 | { |
| 388 | int i, eq; |
| 389 | u32 val; |
| 390 | |
| 391 | /* Program memory region for each event queue */ |
| 392 | for (i = 0; i < msi->nr_eq_region; i++) { |
| 393 | dma_addr_t addr = msi->eq_dma + (i * EQ_MEM_REGION_SIZE); |
| 394 | |
| 395 | iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE, i, |
| 396 | lower_32_bits(addr)); |
| 397 | iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE_UPPER, i, |
| 398 | upper_32_bits(addr)); |
| 399 | } |
| 400 | |
| 401 | /* Program address region for MSI posted writes */ |
| 402 | for (i = 0; i < msi->nr_msi_region; i++) { |
| 403 | phys_addr_t addr = msi->msi_addr + (i * MSI_MEM_REGION_SIZE); |
| 404 | |
| 405 | iproc_msi_write_reg(msi, IPROC_MSI_PAGE, i, |
| 406 | lower_32_bits(addr)); |
| 407 | iproc_msi_write_reg(msi, IPROC_MSI_PAGE_UPPER, i, |
| 408 | upper_32_bits(addr)); |
| 409 | } |
| 410 | |
| 411 | for (eq = 0; eq < msi->nr_irqs; eq++) { |
| 412 | /* Enable MSI event queue */ |
| 413 | val = IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT | |
| 414 | IPROC_MSI_EQ_EN; |
| 415 | iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val); |
| 416 | |
| 417 | /* |
| 418 | * Some legacy platforms require the MSI interrupt enable |
| 419 | * register to be set explicitly. |
| 420 | */ |
| 421 | if (msi->has_inten_reg) { |
| 422 | val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq); |
| 423 | val |= BIT(eq); |
| 424 | iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | static void iproc_msi_disable(struct iproc_msi *msi) |
| 430 | { |
| 431 | u32 eq, val; |
| 432 | |
| 433 | for (eq = 0; eq < msi->nr_irqs; eq++) { |
| 434 | if (msi->has_inten_reg) { |
| 435 | val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq); |
| 436 | val &= ~BIT(eq); |
| 437 | iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val); |
| 438 | } |
| 439 | |
| 440 | val = iproc_msi_read_reg(msi, IPROC_MSI_CTRL, eq); |
| 441 | val &= ~(IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT | |
| 442 | IPROC_MSI_EQ_EN); |
| 443 | iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | static int iproc_msi_alloc_domains(struct device_node *node, |
| 448 | struct iproc_msi *msi) |
| 449 | { |
| 450 | msi->inner_domain = irq_domain_add_linear(NULL, msi->nr_msi_vecs, |
| 451 | &msi_domain_ops, msi); |
| 452 | if (!msi->inner_domain) |
| 453 | return -ENOMEM; |
| 454 | |
| 455 | msi->msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(node), |
| 456 | &iproc_msi_domain_info, |
| 457 | msi->inner_domain); |
| 458 | if (!msi->msi_domain) { |
| 459 | irq_domain_remove(msi->inner_domain); |
| 460 | return -ENOMEM; |
| 461 | } |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static void iproc_msi_free_domains(struct iproc_msi *msi) |
| 467 | { |
| 468 | if (msi->msi_domain) |
| 469 | irq_domain_remove(msi->msi_domain); |
| 470 | |
| 471 | if (msi->inner_domain) |
| 472 | irq_domain_remove(msi->inner_domain); |
| 473 | } |
| 474 | |
| 475 | static void iproc_msi_irq_free(struct iproc_msi *msi, unsigned int cpu) |
| 476 | { |
| 477 | int i; |
| 478 | |
| 479 | for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) { |
| 480 | irq_set_chained_handler_and_data(msi->grps[i].gic_irq, |
| 481 | NULL, NULL); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | static int iproc_msi_irq_setup(struct iproc_msi *msi, unsigned int cpu) |
| 486 | { |
| 487 | int i, ret; |
| 488 | cpumask_var_t mask; |
| 489 | struct iproc_pcie *pcie = msi->pcie; |
| 490 | |
| 491 | for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) { |
| 492 | irq_set_chained_handler_and_data(msi->grps[i].gic_irq, |
| 493 | iproc_msi_handler, |
| 494 | &msi->grps[i]); |
| 495 | /* Dedicate GIC interrupt to each CPU core */ |
| 496 | if (alloc_cpumask_var(&mask, GFP_KERNEL)) { |
| 497 | cpumask_clear(mask); |
| 498 | cpumask_set_cpu(cpu, mask); |
| 499 | ret = irq_set_affinity(msi->grps[i].gic_irq, mask); |
| 500 | if (ret) |
| 501 | dev_err(pcie->dev, |
| 502 | "failed to set affinity for IRQ%d\n", |
| 503 | msi->grps[i].gic_irq); |
| 504 | free_cpumask_var(mask); |
| 505 | } else { |
| 506 | dev_err(pcie->dev, "failed to alloc CPU mask\n"); |
| 507 | ret = -EINVAL; |
| 508 | } |
| 509 | |
| 510 | if (ret) { |
| 511 | /* Free all configured/unconfigured IRQs */ |
| 512 | iproc_msi_irq_free(msi, cpu); |
| 513 | return ret; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | int iproc_msi_init(struct iproc_pcie *pcie, struct device_node *node) |
| 521 | { |
| 522 | struct iproc_msi *msi; |
| 523 | int i, ret; |
| 524 | unsigned int cpu; |
| 525 | |
| 526 | if (!of_device_is_compatible(node, "brcm,iproc-msi")) |
| 527 | return -ENODEV; |
| 528 | |
| 529 | if (!of_find_property(node, "msi-controller", NULL)) |
| 530 | return -ENODEV; |
| 531 | |
| 532 | if (pcie->msi) |
| 533 | return -EBUSY; |
| 534 | |
| 535 | msi = devm_kzalloc(pcie->dev, sizeof(*msi), GFP_KERNEL); |
| 536 | if (!msi) |
| 537 | return -ENOMEM; |
| 538 | |
| 539 | msi->pcie = pcie; |
| 540 | pcie->msi = msi; |
| 541 | msi->msi_addr = pcie->base_addr; |
| 542 | mutex_init(&msi->bitmap_lock); |
| 543 | msi->nr_cpus = num_possible_cpus(); |
| 544 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 545 | if (msi->nr_cpus == 1) |
| 546 | iproc_msi_domain_info.flags |= MSI_FLAG_MULTI_PCI_MSI; |
| 547 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 548 | msi->nr_irqs = of_irq_count(node); |
| 549 | if (!msi->nr_irqs) { |
| 550 | dev_err(pcie->dev, "found no MSI GIC interrupt\n"); |
| 551 | return -ENODEV; |
| 552 | } |
| 553 | |
| 554 | if (msi->nr_irqs > NR_HW_IRQS) { |
| 555 | dev_warn(pcie->dev, "too many MSI GIC interrupts defined %d\n", |
| 556 | msi->nr_irqs); |
| 557 | msi->nr_irqs = NR_HW_IRQS; |
| 558 | } |
| 559 | |
| 560 | if (msi->nr_irqs < msi->nr_cpus) { |
| 561 | dev_err(pcie->dev, |
| 562 | "not enough GIC interrupts for MSI affinity\n"); |
| 563 | return -EINVAL; |
| 564 | } |
| 565 | |
| 566 | if (msi->nr_irqs % msi->nr_cpus != 0) { |
| 567 | msi->nr_irqs -= msi->nr_irqs % msi->nr_cpus; |
| 568 | dev_warn(pcie->dev, "Reducing number of interrupts to %d\n", |
| 569 | msi->nr_irqs); |
| 570 | } |
| 571 | |
| 572 | switch (pcie->type) { |
| 573 | case IPROC_PCIE_PAXB_BCMA: |
| 574 | case IPROC_PCIE_PAXB: |
| 575 | msi->reg_offsets = iproc_msi_reg_paxb; |
| 576 | msi->nr_eq_region = 1; |
| 577 | msi->nr_msi_region = 1; |
| 578 | break; |
| 579 | case IPROC_PCIE_PAXC: |
| 580 | msi->reg_offsets = iproc_msi_reg_paxc; |
| 581 | msi->nr_eq_region = msi->nr_irqs; |
| 582 | msi->nr_msi_region = msi->nr_irqs; |
| 583 | break; |
| 584 | default: |
| 585 | dev_err(pcie->dev, "incompatible iProc PCIe interface\n"); |
| 586 | return -EINVAL; |
| 587 | } |
| 588 | |
| 589 | if (of_find_property(node, "brcm,pcie-msi-inten", NULL)) |
| 590 | msi->has_inten_reg = true; |
| 591 | |
| 592 | msi->nr_msi_vecs = msi->nr_irqs * EQ_LEN; |
| 593 | msi->bitmap = devm_kcalloc(pcie->dev, BITS_TO_LONGS(msi->nr_msi_vecs), |
| 594 | sizeof(*msi->bitmap), GFP_KERNEL); |
| 595 | if (!msi->bitmap) |
| 596 | return -ENOMEM; |
| 597 | |
| 598 | msi->grps = devm_kcalloc(pcie->dev, msi->nr_irqs, sizeof(*msi->grps), |
| 599 | GFP_KERNEL); |
| 600 | if (!msi->grps) |
| 601 | return -ENOMEM; |
| 602 | |
| 603 | for (i = 0; i < msi->nr_irqs; i++) { |
| 604 | unsigned int irq = irq_of_parse_and_map(node, i); |
| 605 | |
| 606 | if (!irq) { |
| 607 | dev_err(pcie->dev, "unable to parse/map interrupt\n"); |
| 608 | ret = -ENODEV; |
| 609 | goto free_irqs; |
| 610 | } |
| 611 | msi->grps[i].gic_irq = irq; |
| 612 | msi->grps[i].msi = msi; |
| 613 | msi->grps[i].eq = i; |
| 614 | } |
| 615 | |
| 616 | /* Reserve memory for event queue and make sure memories are zeroed */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 617 | msi->eq_cpu = dma_alloc_coherent(pcie->dev, |
| 618 | msi->nr_eq_region * EQ_MEM_REGION_SIZE, |
| 619 | &msi->eq_dma, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 620 | if (!msi->eq_cpu) { |
| 621 | ret = -ENOMEM; |
| 622 | goto free_irqs; |
| 623 | } |
| 624 | |
| 625 | ret = iproc_msi_alloc_domains(node, msi); |
| 626 | if (ret) { |
| 627 | dev_err(pcie->dev, "failed to create MSI domains\n"); |
| 628 | goto free_eq_dma; |
| 629 | } |
| 630 | |
| 631 | for_each_online_cpu(cpu) { |
| 632 | ret = iproc_msi_irq_setup(msi, cpu); |
| 633 | if (ret) |
| 634 | goto free_msi_irq; |
| 635 | } |
| 636 | |
| 637 | iproc_msi_enable(msi); |
| 638 | |
| 639 | return 0; |
| 640 | |
| 641 | free_msi_irq: |
| 642 | for_each_online_cpu(cpu) |
| 643 | iproc_msi_irq_free(msi, cpu); |
| 644 | iproc_msi_free_domains(msi); |
| 645 | |
| 646 | free_eq_dma: |
| 647 | dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE, |
| 648 | msi->eq_cpu, msi->eq_dma); |
| 649 | |
| 650 | free_irqs: |
| 651 | for (i = 0; i < msi->nr_irqs; i++) { |
| 652 | if (msi->grps[i].gic_irq) |
| 653 | irq_dispose_mapping(msi->grps[i].gic_irq); |
| 654 | } |
| 655 | pcie->msi = NULL; |
| 656 | return ret; |
| 657 | } |
| 658 | EXPORT_SYMBOL(iproc_msi_init); |
| 659 | |
| 660 | void iproc_msi_exit(struct iproc_pcie *pcie) |
| 661 | { |
| 662 | struct iproc_msi *msi = pcie->msi; |
| 663 | unsigned int i, cpu; |
| 664 | |
| 665 | if (!msi) |
| 666 | return; |
| 667 | |
| 668 | iproc_msi_disable(msi); |
| 669 | |
| 670 | for_each_online_cpu(cpu) |
| 671 | iproc_msi_irq_free(msi, cpu); |
| 672 | |
| 673 | iproc_msi_free_domains(msi); |
| 674 | |
| 675 | dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE, |
| 676 | msi->eq_cpu, msi->eq_dma); |
| 677 | |
| 678 | for (i = 0; i < msi->nr_irqs; i++) { |
| 679 | if (msi->grps[i].gic_irq) |
| 680 | irq_dispose_mapping(msi->grps[i].gic_irq); |
| 681 | } |
| 682 | } |
| 683 | EXPORT_SYMBOL(iproc_msi_exit); |