1
2
3
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
26#define NR_HW_IRQS 6
27
28
29#define EQ_LEN 64
30
31
32#define EQ_MEM_REGION_SIZE SZ_4K
33
34
35#define MSI_MEM_REGION_SIZE SZ_4K
36
37enum 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
49struct iproc_msi;
50
51
52
53
54
55
56
57
58
59
60
61struct iproc_msi_grp {
62 struct iproc_msi *msi;
63 int gic_irq;
64 unsigned int eq;
65};
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93struct 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
112static 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
121static 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
128static 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
137static 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
146static inline u32 hwirq_to_group(struct iproc_msi *msi, unsigned long hwirq)
147{
148 return (hwirq % msi->nr_irqs);
149}
150
151static 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
160static 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
168static struct irq_chip iproc_msi_irq_chip = {
169 .name = "iProc-MSI",
170};
171
172static struct msi_domain_info iproc_msi_domain_info = {
173 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
174 MSI_FLAG_PCI_MSIX,
175 .chip = &iproc_msi_irq_chip,
176};
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195static inline int hwirq_to_cpu(struct iproc_msi *msi, unsigned long hwirq)
196{
197 return (hwirq % msi->nr_cpus);
198}
199
200static 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
206static 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;
212 int ret;
213
214 curr_cpu = hwirq_to_cpu(msi, data->hwirq);
215 if (curr_cpu == target_cpu)
216 ret = IRQ_SET_MASK_OK_DONE;
217 else {
218
219 data->hwirq = hwirq_to_canonical_hwirq(msi, data->hwirq) + target_cpu;
220 ret = IRQ_SET_MASK_OK;
221 }
222
223 irq_data_update_effective_affinity(data, cpumask_of(target_cpu));
224
225 return ret;
226}
227
228static 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
240static 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
246static 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
253 if (msi->nr_cpus > 1 && nr_irqs > 1)
254 return -EINVAL;
255
256 mutex_lock(&msi->bitmap_lock);
257
258
259
260
261
262 hwirq = bitmap_find_free_region(msi->bitmap, msi->nr_msi_vecs,
263 order_base_2(msi->nr_cpus * nr_irqs));
264
265 mutex_unlock(&msi->bitmap_lock);
266
267 if (hwirq < 0)
268 return -ENOSPC;
269
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
277 return 0;
278}
279
280static 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);
290 bitmap_release_region(msi->bitmap, hwirq,
291 order_base_2(msi->nr_cpus * nr_irqs));
292
293 mutex_unlock(&msi->bitmap_lock);
294
295 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
296}
297
298static const struct irq_domain_ops msi_domain_ops = {
299 .alloc = iproc_msi_irq_domain_alloc,
300 .free = iproc_msi_irq_domain_free,
301};
302
303static inline u32 decode_msi_hwirq(struct iproc_msi *msi, u32 eq, u32 head)
304{
305 u32 __iomem *msg;
306 u32 hwirq;
307 unsigned int offs;
308
309 offs = iproc_msi_eq_offset(msi, eq) + head * sizeof(u32);
310 msg = (u32 __iomem *)(msi->eq_cpu + offs);
311 hwirq = readl(msg);
312 hwirq = (hwirq >> 5) + (hwirq & 0x1f);
313
314
315
316
317
318
319 return hwirq_to_canonical_hwirq(msi, hwirq);
320}
321
322static void iproc_msi_handler(struct irq_desc *desc)
323{
324 struct irq_chip *chip = irq_desc_get_chip(desc);
325 struct iproc_msi_grp *grp;
326 struct iproc_msi *msi;
327 u32 eq, head, tail, nr_events;
328 unsigned long hwirq;
329 int virq;
330
331 chained_irq_enter(chip, desc);
332
333 grp = irq_desc_get_handler_data(desc);
334 msi = grp->msi;
335 eq = grp->eq;
336
337
338
339
340
341
342
343
344
345
346
347 head = iproc_msi_read_reg(msi, IPROC_MSI_EQ_HEAD,
348 eq) & IPROC_MSI_EQ_MASK;
349 do {
350 tail = iproc_msi_read_reg(msi, IPROC_MSI_EQ_TAIL,
351 eq) & IPROC_MSI_EQ_MASK;
352
353
354
355
356
357 nr_events = (tail < head) ?
358 (EQ_LEN - (head - tail)) : (tail - head);
359 if (!nr_events)
360 break;
361
362
363 while (nr_events--) {
364 hwirq = decode_msi_hwirq(msi, eq, head);
365 virq = irq_find_mapping(msi->inner_domain, hwirq);
366 generic_handle_irq(virq);
367
368 head++;
369 head %= EQ_LEN;
370 }
371
372
373
374
375
376 iproc_msi_write_reg(msi, IPROC_MSI_EQ_HEAD, eq, head);
377
378
379
380
381
382 } while (true);
383
384 chained_irq_exit(chip, desc);
385}
386
387static void iproc_msi_enable(struct iproc_msi *msi)
388{
389 int i, eq;
390 u32 val;
391
392
393 for (i = 0; i < msi->nr_eq_region; i++) {
394 dma_addr_t addr = msi->eq_dma + (i * EQ_MEM_REGION_SIZE);
395
396 iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE, i,
397 lower_32_bits(addr));
398 iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE_UPPER, i,
399 upper_32_bits(addr));
400 }
401
402
403 for (i = 0; i < msi->nr_msi_region; i++) {
404 phys_addr_t addr = msi->msi_addr + (i * MSI_MEM_REGION_SIZE);
405
406 iproc_msi_write_reg(msi, IPROC_MSI_PAGE, i,
407 lower_32_bits(addr));
408 iproc_msi_write_reg(msi, IPROC_MSI_PAGE_UPPER, i,
409 upper_32_bits(addr));
410 }
411
412 for (eq = 0; eq < msi->nr_irqs; eq++) {
413
414 val = IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT |
415 IPROC_MSI_EQ_EN;
416 iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val);
417
418
419
420
421
422 if (msi->has_inten_reg) {
423 val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq);
424 val |= BIT(eq);
425 iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val);
426 }
427 }
428}
429
430static void iproc_msi_disable(struct iproc_msi *msi)
431{
432 u32 eq, val;
433
434 for (eq = 0; eq < msi->nr_irqs; eq++) {
435 if (msi->has_inten_reg) {
436 val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq);
437 val &= ~BIT(eq);
438 iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val);
439 }
440
441 val = iproc_msi_read_reg(msi, IPROC_MSI_CTRL, eq);
442 val &= ~(IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT |
443 IPROC_MSI_EQ_EN);
444 iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val);
445 }
446}
447
448static int iproc_msi_alloc_domains(struct device_node *node,
449 struct iproc_msi *msi)
450{
451 msi->inner_domain = irq_domain_add_linear(NULL, msi->nr_msi_vecs,
452 &msi_domain_ops, msi);
453 if (!msi->inner_domain)
454 return -ENOMEM;
455
456 msi->msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(node),
457 &iproc_msi_domain_info,
458 msi->inner_domain);
459 if (!msi->msi_domain) {
460 irq_domain_remove(msi->inner_domain);
461 return -ENOMEM;
462 }
463
464 return 0;
465}
466
467static void iproc_msi_free_domains(struct iproc_msi *msi)
468{
469 if (msi->msi_domain)
470 irq_domain_remove(msi->msi_domain);
471
472 if (msi->inner_domain)
473 irq_domain_remove(msi->inner_domain);
474}
475
476static void iproc_msi_irq_free(struct iproc_msi *msi, unsigned int cpu)
477{
478 int i;
479
480 for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) {
481 irq_set_chained_handler_and_data(msi->grps[i].gic_irq,
482 NULL, NULL);
483 }
484}
485
486static int iproc_msi_irq_setup(struct iproc_msi *msi, unsigned int cpu)
487{
488 int i, ret;
489 cpumask_var_t mask;
490 struct iproc_pcie *pcie = msi->pcie;
491
492 for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) {
493 irq_set_chained_handler_and_data(msi->grps[i].gic_irq,
494 iproc_msi_handler,
495 &msi->grps[i]);
496
497 if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
498 cpumask_clear(mask);
499 cpumask_set_cpu(cpu, mask);
500 ret = irq_set_affinity(msi->grps[i].gic_irq, mask);
501 if (ret)
502 dev_err(pcie->dev,
503 "failed to set affinity for IRQ%d\n",
504 msi->grps[i].gic_irq);
505 free_cpumask_var(mask);
506 } else {
507 dev_err(pcie->dev, "failed to alloc CPU mask\n");
508 ret = -EINVAL;
509 }
510
511 if (ret) {
512
513 iproc_msi_irq_free(msi, cpu);
514 return ret;
515 }
516 }
517
518 return 0;
519}
520
521int iproc_msi_init(struct iproc_pcie *pcie, struct device_node *node)
522{
523 struct iproc_msi *msi;
524 int i, ret;
525 unsigned int cpu;
526
527 if (!of_device_is_compatible(node, "brcm,iproc-msi"))
528 return -ENODEV;
529
530 if (!of_find_property(node, "msi-controller", NULL))
531 return -ENODEV;
532
533 if (pcie->msi)
534 return -EBUSY;
535
536 msi = devm_kzalloc(pcie->dev, sizeof(*msi), GFP_KERNEL);
537 if (!msi)
538 return -ENOMEM;
539
540 msi->pcie = pcie;
541 pcie->msi = msi;
542 msi->msi_addr = pcie->base_addr;
543 mutex_init(&msi->bitmap_lock);
544 msi->nr_cpus = num_possible_cpus();
545
546 if (msi->nr_cpus == 1)
547 iproc_msi_domain_info.flags |= MSI_FLAG_MULTI_PCI_MSI;
548
549 msi->nr_irqs = of_irq_count(node);
550 if (!msi->nr_irqs) {
551 dev_err(pcie->dev, "found no MSI GIC interrupt\n");
552 return -ENODEV;
553 }
554
555 if (msi->nr_irqs > NR_HW_IRQS) {
556 dev_warn(pcie->dev, "too many MSI GIC interrupts defined %d\n",
557 msi->nr_irqs);
558 msi->nr_irqs = NR_HW_IRQS;
559 }
560
561 if (msi->nr_irqs < msi->nr_cpus) {
562 dev_err(pcie->dev,
563 "not enough GIC interrupts for MSI affinity\n");
564 return -EINVAL;
565 }
566
567 if (msi->nr_irqs % msi->nr_cpus != 0) {
568 msi->nr_irqs -= msi->nr_irqs % msi->nr_cpus;
569 dev_warn(pcie->dev, "Reducing number of interrupts to %d\n",
570 msi->nr_irqs);
571 }
572
573 switch (pcie->type) {
574 case IPROC_PCIE_PAXB_BCMA:
575 case IPROC_PCIE_PAXB:
576 msi->reg_offsets = iproc_msi_reg_paxb;
577 msi->nr_eq_region = 1;
578 msi->nr_msi_region = 1;
579 break;
580 case IPROC_PCIE_PAXC:
581 msi->reg_offsets = iproc_msi_reg_paxc;
582 msi->nr_eq_region = msi->nr_irqs;
583 msi->nr_msi_region = msi->nr_irqs;
584 break;
585 default:
586 dev_err(pcie->dev, "incompatible iProc PCIe interface\n");
587 return -EINVAL;
588 }
589
590 if (of_find_property(node, "brcm,pcie-msi-inten", NULL))
591 msi->has_inten_reg = true;
592
593 msi->nr_msi_vecs = msi->nr_irqs * EQ_LEN;
594 msi->bitmap = devm_kcalloc(pcie->dev, BITS_TO_LONGS(msi->nr_msi_vecs),
595 sizeof(*msi->bitmap), GFP_KERNEL);
596 if (!msi->bitmap)
597 return -ENOMEM;
598
599 msi->grps = devm_kcalloc(pcie->dev, msi->nr_irqs, sizeof(*msi->grps),
600 GFP_KERNEL);
601 if (!msi->grps)
602 return -ENOMEM;
603
604 for (i = 0; i < msi->nr_irqs; i++) {
605 unsigned int irq = irq_of_parse_and_map(node, i);
606
607 if (!irq) {
608 dev_err(pcie->dev, "unable to parse/map interrupt\n");
609 ret = -ENODEV;
610 goto free_irqs;
611 }
612 msi->grps[i].gic_irq = irq;
613 msi->grps[i].msi = msi;
614 msi->grps[i].eq = i;
615 }
616
617
618 msi->eq_cpu = dma_alloc_coherent(pcie->dev,
619 msi->nr_eq_region * EQ_MEM_REGION_SIZE,
620 &msi->eq_dma, GFP_KERNEL);
621 if (!msi->eq_cpu) {
622 ret = -ENOMEM;
623 goto free_irqs;
624 }
625
626 ret = iproc_msi_alloc_domains(node, msi);
627 if (ret) {
628 dev_err(pcie->dev, "failed to create MSI domains\n");
629 goto free_eq_dma;
630 }
631
632 for_each_online_cpu(cpu) {
633 ret = iproc_msi_irq_setup(msi, cpu);
634 if (ret)
635 goto free_msi_irq;
636 }
637
638 iproc_msi_enable(msi);
639
640 return 0;
641
642free_msi_irq:
643 for_each_online_cpu(cpu)
644 iproc_msi_irq_free(msi, cpu);
645 iproc_msi_free_domains(msi);
646
647free_eq_dma:
648 dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE,
649 msi->eq_cpu, msi->eq_dma);
650
651free_irqs:
652 for (i = 0; i < msi->nr_irqs; i++) {
653 if (msi->grps[i].gic_irq)
654 irq_dispose_mapping(msi->grps[i].gic_irq);
655 }
656 pcie->msi = NULL;
657 return ret;
658}
659EXPORT_SYMBOL(iproc_msi_init);
660
661void iproc_msi_exit(struct iproc_pcie *pcie)
662{
663 struct iproc_msi *msi = pcie->msi;
664 unsigned int i, cpu;
665
666 if (!msi)
667 return;
668
669 iproc_msi_disable(msi);
670
671 for_each_online_cpu(cpu)
672 iproc_msi_irq_free(msi, cpu);
673
674 iproc_msi_free_domains(msi);
675
676 dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE,
677 msi->eq_cpu, msi->eq_dma);
678
679 for (i = 0; i < msi->nr_irqs; i++) {
680 if (msi->grps[i].gic_irq)
681 irq_dispose_mapping(msi->grps[i].gic_irq);
682 }
683}
684EXPORT_SYMBOL(iproc_msi_exit);
685