xref: /openbmc/linux/drivers/irqchip/irq-gic-v3-its-fsl-mc-msi.c (revision 998fb7badf0362a2057694878098642ef363d899)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Freescale Management Complex (MC) bus driver MSI support
4  *
5  * Copyright (C) 2015-2016 Freescale Semiconductor, Inc.
6  * Author: German Rivera <German.Rivera@freescale.com>
7  *
8  */
9 
10 #include <linux/of_device.h>
11 #include <linux/of_address.h>
12 #include <linux/irq.h>
13 #include <linux/msi.h>
14 #include <linux/of.h>
15 #include <linux/of_irq.h>
16 #include <linux/fsl/mc.h>
17 
18 static struct irq_chip its_msi_irq_chip = {
19 	.name = "ITS-fMSI",
20 	.irq_mask = irq_chip_mask_parent,
21 	.irq_unmask = irq_chip_unmask_parent,
22 	.irq_eoi = irq_chip_eoi_parent,
23 	.irq_set_affinity = msi_domain_set_affinity
24 };
25 
26 static u32 fsl_mc_msi_domain_get_msi_id(struct irq_domain *domain,
27 					struct fsl_mc_device *mc_dev)
28 {
29 	struct device_node *of_node;
30 	u32 out_id;
31 
32 	of_node = irq_domain_get_of_node(domain);
33 	out_id = of_msi_map_id(&mc_dev->dev, of_node, mc_dev->icid);
34 
35 	return out_id;
36 }
37 
38 static int its_fsl_mc_msi_prepare(struct irq_domain *msi_domain,
39 				  struct device *dev,
40 				  int nvec, msi_alloc_info_t *info)
41 {
42 	struct fsl_mc_device *mc_bus_dev;
43 	struct msi_domain_info *msi_info;
44 
45 	if (!dev_is_fsl_mc(dev))
46 		return -EINVAL;
47 
48 	mc_bus_dev = to_fsl_mc_device(dev);
49 	if (!(mc_bus_dev->flags & FSL_MC_IS_DPRC))
50 		return -EINVAL;
51 
52 	/*
53 	 * Set the device Id to be passed to the GIC-ITS:
54 	 *
55 	 * NOTE: This device id corresponds to the IOMMU stream ID
56 	 * associated with the DPRC object (ICID).
57 	 */
58 	info->scratchpad[0].ul = fsl_mc_msi_domain_get_msi_id(msi_domain,
59 							      mc_bus_dev);
60 	msi_info = msi_get_domain_info(msi_domain->parent);
61 
62 	/* Allocate at least 32 MSIs, and always as a power of 2 */
63 	nvec = max_t(int, 32, roundup_pow_of_two(nvec));
64 	return msi_info->ops->msi_prepare(msi_domain->parent, dev, nvec, info);
65 }
66 
67 static struct msi_domain_ops its_fsl_mc_msi_ops __ro_after_init = {
68 	.msi_prepare = its_fsl_mc_msi_prepare,
69 };
70 
71 static struct msi_domain_info its_fsl_mc_msi_domain_info = {
72 	.flags	= (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
73 	.ops	= &its_fsl_mc_msi_ops,
74 	.chip	= &its_msi_irq_chip,
75 };
76 
77 static const struct of_device_id its_device_id[] = {
78 	{	.compatible	= "arm,gic-v3-its",	},
79 	{},
80 };
81 
82 static int __init its_fsl_mc_msi_init(void)
83 {
84 	struct device_node *np;
85 	struct irq_domain *parent;
86 	struct irq_domain *mc_msi_domain;
87 
88 	for (np = of_find_matching_node(NULL, its_device_id); np;
89 	     np = of_find_matching_node(np, its_device_id)) {
90 		if (!of_device_is_available(np))
91 			continue;
92 		if (!of_property_read_bool(np, "msi-controller"))
93 			continue;
94 
95 		parent = irq_find_matching_host(np, DOMAIN_BUS_NEXUS);
96 		if (!parent || !msi_get_domain_info(parent)) {
97 			pr_err("%pOF: unable to locate ITS domain\n", np);
98 			continue;
99 		}
100 
101 		mc_msi_domain = fsl_mc_msi_create_irq_domain(
102 						 of_node_to_fwnode(np),
103 						 &its_fsl_mc_msi_domain_info,
104 						 parent);
105 		if (!mc_msi_domain) {
106 			pr_err("%pOF: unable to create fsl-mc domain\n", np);
107 			continue;
108 		}
109 
110 		pr_info("fsl-mc MSI: %pOF domain created\n", np);
111 	}
112 
113 	return 0;
114 }
115 
116 early_initcall(its_fsl_mc_msi_init);
117