1 // SPDX-License-Identifier: GPL-2.0-only
3 * Freescale SCFG MSI(-X) support
5 * Copyright (C) 2016 Freescale Semiconductor.
7 * Author: Minghuan Lian <Minghuan.Lian@nxp.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/msi.h>
13 #include <linux/interrupt.h>
14 #include <linux/iommu.h>
15 #include <linux/irq.h>
16 #include <linux/irqchip/chained_irq.h>
17 #include <linux/irqdomain.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_pci.h>
20 #include <linux/platform_device.h>
21 #include <linux/property.h>
22 #include <linux/spinlock.h>
24 #define MSI_IRQS_PER_MSIR 32
25 #define MSI_MSIR_OFFSET 4
27 #define MSI_LS1043V1_1_IRQS_PER_MSIR 8
28 #define MSI_LS1043V1_1_MSIR_OFFSET 0x10
30 struct ls_scfg_msi_cfg
{
31 u32 ibs_shift
; /* Shift of interrupt bit select */
32 u32 msir_irqs
; /* The irq number per MSIR */
33 u32 msir_base
; /* The base address of MSIR */
37 struct ls_scfg_msi
*msi_data
;
40 unsigned int bit_start
;
42 unsigned int srs
; /* Shared interrupt register select */
48 struct platform_device
*pdev
;
49 struct irq_domain
*parent
;
50 struct irq_domain
*msi_domain
;
52 phys_addr_t msiir_addr
;
53 struct ls_scfg_msi_cfg
*cfg
;
55 struct ls_scfg_msir
*msir
;
60 static struct irq_chip ls_scfg_msi_irq_chip
= {
62 .irq_mask
= pci_msi_mask_irq
,
63 .irq_unmask
= pci_msi_unmask_irq
,
66 static struct msi_domain_info ls_scfg_msi_domain_info
= {
67 .flags
= (MSI_FLAG_USE_DEF_DOM_OPS
|
68 MSI_FLAG_USE_DEF_CHIP_OPS
|
70 .chip
= &ls_scfg_msi_irq_chip
,
73 static int msi_affinity_flag
= 1;
75 static int __init
early_parse_ls_scfg_msi(char *p
)
77 if (p
&& strncmp(p
, "no-affinity", 11) == 0)
78 msi_affinity_flag
= 0;
80 msi_affinity_flag
= 1;
84 early_param("lsmsi", early_parse_ls_scfg_msi
);
86 static void ls_scfg_msi_compose_msg(struct irq_data
*data
, struct msi_msg
*msg
)
88 struct ls_scfg_msi
*msi_data
= irq_data_get_irq_chip_data(data
);
90 msg
->address_hi
= upper_32_bits(msi_data
->msiir_addr
);
91 msg
->address_lo
= lower_32_bits(msi_data
->msiir_addr
);
92 msg
->data
= data
->hwirq
;
94 if (msi_affinity_flag
) {
95 const struct cpumask
*mask
;
97 mask
= irq_data_get_effective_affinity_mask(data
);
98 msg
->data
|= cpumask_first(mask
);
101 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(data
), msg
);
104 static int ls_scfg_msi_set_affinity(struct irq_data
*irq_data
,
105 const struct cpumask
*mask
, bool force
)
107 struct ls_scfg_msi
*msi_data
= irq_data_get_irq_chip_data(irq_data
);
110 if (!msi_affinity_flag
)
114 cpu
= cpumask_any_and(mask
, cpu_online_mask
);
116 cpu
= cpumask_first(mask
);
118 if (cpu
>= msi_data
->msir_num
)
121 if (msi_data
->msir
[cpu
].gic_irq
<= 0) {
122 pr_warn("cannot bind the irq to cpu%d\n", cpu
);
126 irq_data_update_effective_affinity(irq_data
, cpumask_of(cpu
));
128 return IRQ_SET_MASK_OK
;
131 static struct irq_chip ls_scfg_msi_parent_chip
= {
133 .irq_compose_msi_msg
= ls_scfg_msi_compose_msg
,
134 .irq_set_affinity
= ls_scfg_msi_set_affinity
,
137 static int ls_scfg_msi_domain_irq_alloc(struct irq_domain
*domain
,
139 unsigned int nr_irqs
,
142 msi_alloc_info_t
*info
= args
;
143 struct ls_scfg_msi
*msi_data
= domain
->host_data
;
146 WARN_ON(nr_irqs
!= 1);
148 spin_lock(&msi_data
->lock
);
149 pos
= find_first_zero_bit(msi_data
->used
, msi_data
->irqs_num
);
150 if (pos
< msi_data
->irqs_num
)
151 __set_bit(pos
, msi_data
->used
);
154 spin_unlock(&msi_data
->lock
);
159 err
= iommu_dma_prepare_msi(info
->desc
, msi_data
->msiir_addr
);
163 irq_domain_set_info(domain
, virq
, pos
,
164 &ls_scfg_msi_parent_chip
, msi_data
,
165 handle_simple_irq
, NULL
, NULL
);
170 static void ls_scfg_msi_domain_irq_free(struct irq_domain
*domain
,
171 unsigned int virq
, unsigned int nr_irqs
)
173 struct irq_data
*d
= irq_domain_get_irq_data(domain
, virq
);
174 struct ls_scfg_msi
*msi_data
= irq_data_get_irq_chip_data(d
);
178 if (pos
< 0 || pos
>= msi_data
->irqs_num
) {
179 pr_err("failed to teardown msi. Invalid hwirq %d\n", pos
);
183 spin_lock(&msi_data
->lock
);
184 __clear_bit(pos
, msi_data
->used
);
185 spin_unlock(&msi_data
->lock
);
188 static const struct irq_domain_ops ls_scfg_msi_domain_ops
= {
189 .alloc
= ls_scfg_msi_domain_irq_alloc
,
190 .free
= ls_scfg_msi_domain_irq_free
,
193 static void ls_scfg_msi_irq_handler(struct irq_desc
*desc
)
195 struct ls_scfg_msir
*msir
= irq_desc_get_handler_data(desc
);
196 struct ls_scfg_msi
*msi_data
= msir
->msi_data
;
198 int pos
, size
, hwirq
;
200 chained_irq_enter(irq_desc_get_chip(desc
), desc
);
202 val
= ioread32be(msir
->reg
);
204 pos
= msir
->bit_start
;
205 size
= msir
->bit_end
+ 1;
207 for_each_set_bit_from(pos
, &val
, size
) {
208 hwirq
= ((msir
->bit_end
- pos
) << msi_data
->cfg
->ibs_shift
) |
210 generic_handle_domain_irq(msi_data
->parent
, hwirq
);
213 chained_irq_exit(irq_desc_get_chip(desc
), desc
);
216 static int ls_scfg_msi_domains_init(struct ls_scfg_msi
*msi_data
)
218 /* Initialize MSI domain parent */
219 msi_data
->parent
= irq_domain_add_linear(NULL
,
221 &ls_scfg_msi_domain_ops
,
223 if (!msi_data
->parent
) {
224 dev_err(&msi_data
->pdev
->dev
, "failed to create IRQ domain\n");
228 msi_data
->msi_domain
= pci_msi_create_irq_domain(
229 of_node_to_fwnode(msi_data
->pdev
->dev
.of_node
),
230 &ls_scfg_msi_domain_info
,
232 if (!msi_data
->msi_domain
) {
233 dev_err(&msi_data
->pdev
->dev
, "failed to create MSI domain\n");
234 irq_domain_remove(msi_data
->parent
);
241 static int ls_scfg_msi_setup_hwirq(struct ls_scfg_msi
*msi_data
, int index
)
243 struct ls_scfg_msir
*msir
;
246 virq
= platform_get_irq(msi_data
->pdev
, index
);
250 msir
= &msi_data
->msir
[index
];
252 msir
->msi_data
= msi_data
;
253 msir
->gic_irq
= virq
;
254 msir
->reg
= msi_data
->regs
+ msi_data
->cfg
->msir_base
+ 4 * index
;
256 if (msi_data
->cfg
->msir_irqs
== MSI_LS1043V1_1_IRQS_PER_MSIR
) {
257 msir
->bit_start
= 32 - ((msir
->index
+ 1) *
258 MSI_LS1043V1_1_IRQS_PER_MSIR
);
259 msir
->bit_end
= msir
->bit_start
+
260 MSI_LS1043V1_1_IRQS_PER_MSIR
- 1;
263 msir
->bit_end
= msi_data
->cfg
->msir_irqs
- 1;
266 irq_set_chained_handler_and_data(msir
->gic_irq
,
267 ls_scfg_msi_irq_handler
,
270 if (msi_affinity_flag
) {
271 /* Associate MSIR interrupt to the cpu */
272 irq_set_affinity(msir
->gic_irq
, get_cpu_mask(index
));
273 msir
->srs
= 0; /* This value is determined by the CPU */
277 /* Release the hwirqs corresponding to this MSIR */
278 if (!msi_affinity_flag
|| msir
->index
== 0) {
279 for (i
= 0; i
< msi_data
->cfg
->msir_irqs
; i
++) {
280 hwirq
= i
<< msi_data
->cfg
->ibs_shift
| msir
->index
;
281 bitmap_clear(msi_data
->used
, hwirq
, 1);
288 static int ls_scfg_msi_teardown_hwirq(struct ls_scfg_msir
*msir
)
290 struct ls_scfg_msi
*msi_data
= msir
->msi_data
;
293 if (msir
->gic_irq
> 0)
294 irq_set_chained_handler_and_data(msir
->gic_irq
, NULL
, NULL
);
296 for (i
= 0; i
< msi_data
->cfg
->msir_irqs
; i
++) {
297 hwirq
= i
<< msi_data
->cfg
->ibs_shift
| msir
->index
;
298 bitmap_set(msi_data
->used
, hwirq
, 1);
304 static struct ls_scfg_msi_cfg ls1021_msi_cfg
= {
306 .msir_irqs
= MSI_IRQS_PER_MSIR
,
307 .msir_base
= MSI_MSIR_OFFSET
,
310 static struct ls_scfg_msi_cfg ls1046_msi_cfg
= {
312 .msir_irqs
= MSI_IRQS_PER_MSIR
,
313 .msir_base
= MSI_MSIR_OFFSET
,
316 static struct ls_scfg_msi_cfg ls1043_v1_1_msi_cfg
= {
318 .msir_irqs
= MSI_LS1043V1_1_IRQS_PER_MSIR
,
319 .msir_base
= MSI_LS1043V1_1_MSIR_OFFSET
,
322 static const struct of_device_id ls_scfg_msi_id
[] = {
323 /* The following two misspelled compatibles are obsolete */
324 { .compatible
= "fsl,1s1021a-msi", .data
= &ls1021_msi_cfg
},
325 { .compatible
= "fsl,1s1043a-msi", .data
= &ls1021_msi_cfg
},
327 { .compatible
= "fsl,ls1012a-msi", .data
= &ls1021_msi_cfg
},
328 { .compatible
= "fsl,ls1021a-msi", .data
= &ls1021_msi_cfg
},
329 { .compatible
= "fsl,ls1043a-msi", .data
= &ls1021_msi_cfg
},
330 { .compatible
= "fsl,ls1043a-v1.1-msi", .data
= &ls1043_v1_1_msi_cfg
},
331 { .compatible
= "fsl,ls1046a-msi", .data
= &ls1046_msi_cfg
},
334 MODULE_DEVICE_TABLE(of
, ls_scfg_msi_id
);
336 static int ls_scfg_msi_probe(struct platform_device
*pdev
)
338 struct ls_scfg_msi
*msi_data
;
339 struct resource
*res
;
342 msi_data
= devm_kzalloc(&pdev
->dev
, sizeof(*msi_data
), GFP_KERNEL
);
346 msi_data
->cfg
= (struct ls_scfg_msi_cfg
*)device_get_match_data(&pdev
->dev
);
350 msi_data
->regs
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
351 if (IS_ERR(msi_data
->regs
)) {
352 dev_err(&pdev
->dev
, "failed to initialize 'regs'\n");
353 return PTR_ERR(msi_data
->regs
);
355 msi_data
->msiir_addr
= res
->start
;
357 msi_data
->pdev
= pdev
;
358 spin_lock_init(&msi_data
->lock
);
360 msi_data
->irqs_num
= MSI_IRQS_PER_MSIR
*
361 (1 << msi_data
->cfg
->ibs_shift
);
362 msi_data
->used
= devm_bitmap_zalloc(&pdev
->dev
, msi_data
->irqs_num
, GFP_KERNEL
);
366 * Reserve all the hwirqs
367 * The available hwirqs will be released in ls1_msi_setup_hwirq()
369 bitmap_set(msi_data
->used
, 0, msi_data
->irqs_num
);
371 msi_data
->msir_num
= of_irq_count(pdev
->dev
.of_node
);
373 if (msi_affinity_flag
) {
376 cpu_num
= num_possible_cpus();
377 if (msi_data
->msir_num
>= cpu_num
)
378 msi_data
->msir_num
= cpu_num
;
380 msi_affinity_flag
= 0;
383 msi_data
->msir
= devm_kcalloc(&pdev
->dev
, msi_data
->msir_num
,
384 sizeof(*msi_data
->msir
),
389 for (i
= 0; i
< msi_data
->msir_num
; i
++)
390 ls_scfg_msi_setup_hwirq(msi_data
, i
);
392 ret
= ls_scfg_msi_domains_init(msi_data
);
396 platform_set_drvdata(pdev
, msi_data
);
401 static void ls_scfg_msi_remove(struct platform_device
*pdev
)
403 struct ls_scfg_msi
*msi_data
= platform_get_drvdata(pdev
);
406 for (i
= 0; i
< msi_data
->msir_num
; i
++)
407 ls_scfg_msi_teardown_hwirq(&msi_data
->msir
[i
]);
409 irq_domain_remove(msi_data
->msi_domain
);
410 irq_domain_remove(msi_data
->parent
);
412 platform_set_drvdata(pdev
, NULL
);
415 static struct platform_driver ls_scfg_msi_driver
= {
417 .name
= "ls-scfg-msi",
418 .of_match_table
= ls_scfg_msi_id
,
420 .probe
= ls_scfg_msi_probe
,
421 .remove
= ls_scfg_msi_remove
,
424 module_platform_driver(ls_scfg_msi_driver
);
426 MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@nxp.com>");
427 MODULE_DESCRIPTION("Freescale Layerscape SCFG MSI controller driver");