1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013-2015 ARM Limited, All Rights Reserved.
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6 
7 #include <linux/acpi_iort.h>
8 #include <linux/msi.h>
9 #include <linux/of.h>
10 #include <linux/of_irq.h>
11 #include <linux/of_pci.h>
12 
13 static void its_mask_msi_irq(struct irq_data *d)
14 {
15 	pci_msi_mask_irq(d);
16 	irq_chip_mask_parent(d);
17 }
18 
19 static void its_unmask_msi_irq(struct irq_data *d)
20 {
21 	pci_msi_unmask_irq(d);
22 	irq_chip_unmask_parent(d);
23 }
24 
25 static struct irq_chip its_msi_irq_chip = {
26 	.name			= "ITS-MSI",
27 	.irq_unmask		= its_unmask_msi_irq,
28 	.irq_mask		= its_mask_msi_irq,
29 	.irq_eoi		= irq_chip_eoi_parent,
30 	.irq_write_msi_msg	= pci_msi_domain_write_msg,
31 };
32 
33 static int its_pci_msi_vec_count(struct pci_dev *pdev, void *data)
34 {
35 	int msi, msix, *count = data;
36 
37 	msi = max(pci_msi_vec_count(pdev), 0);
38 	msix = max(pci_msix_vec_count(pdev), 0);
39 	*count += max(msi, msix);
40 
41 	return 0;
42 }
43 
44 static int its_get_pci_alias(struct pci_dev *pdev, u16 alias, void *data)
45 {
46 	struct pci_dev **alias_dev = data;
47 
48 	*alias_dev = pdev;
49 
50 	return 0;
51 }
52 
53 static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
54 			       int nvec, msi_alloc_info_t *info)
55 {
56 	struct pci_dev *pdev, *alias_dev;
57 	struct msi_domain_info *msi_info;
58 	int alias_count = 0, minnvec = 1;
59 
60 	if (!dev_is_pci(dev))
61 		return -EINVAL;
62 
63 	msi_info = msi_get_domain_info(domain->parent);
64 
65 	pdev = to_pci_dev(dev);
66 	/*
67 	 * If pdev is downstream of any aliasing bridges, take an upper
68 	 * bound of how many other vectors could map to the same DevID.
69 	 */
70 	pci_for_each_dma_alias(pdev, its_get_pci_alias, &alias_dev);
71 	if (alias_dev != pdev && alias_dev->subordinate)
72 		pci_walk_bus(alias_dev->subordinate, its_pci_msi_vec_count,
73 			     &alias_count);
74 
75 	/* ITS specific DeviceID, as the core ITS ignores dev. */
76 	info->scratchpad[0].ul = pci_msi_domain_get_msi_rid(domain, pdev);
77 
78 	/*
79 	 * Always allocate a power of 2, and special case device 0 for
80 	 * broken systems where the DevID is not wired (and all devices
81 	 * appear as DevID 0). For that reason, we generously allocate a
82 	 * minimum of 32 MSIs for DevID 0. If you want more because all
83 	 * your devices are aliasing to DevID 0, consider fixing your HW.
84 	 */
85 	nvec = max(nvec, alias_count);
86 	if (!info->scratchpad[0].ul)
87 		minnvec = 32;
88 	nvec = max_t(int, minnvec, roundup_pow_of_two(nvec));
89 	return msi_info->ops->msi_prepare(domain->parent, dev, nvec, info);
90 }
91 
92 static struct msi_domain_ops its_pci_msi_ops = {
93 	.msi_prepare	= its_pci_msi_prepare,
94 };
95 
96 static struct msi_domain_info its_pci_msi_domain_info = {
97 	.flags	= (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
98 		   MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
99 	.ops	= &its_pci_msi_ops,
100 	.chip	= &its_msi_irq_chip,
101 };
102 
103 static struct of_device_id its_device_id[] = {
104 	{	.compatible	= "arm,gic-v3-its",	},
105 	{},
106 };
107 
108 static int __init its_pci_msi_init_one(struct fwnode_handle *handle,
109 				       const char *name)
110 {
111 	struct irq_domain *parent;
112 
113 	parent = irq_find_matching_fwnode(handle, DOMAIN_BUS_NEXUS);
114 	if (!parent || !msi_get_domain_info(parent)) {
115 		pr_err("%s: Unable to locate ITS domain\n", name);
116 		return -ENXIO;
117 	}
118 
119 	if (!pci_msi_create_irq_domain(handle, &its_pci_msi_domain_info,
120 				       parent)) {
121 		pr_err("%s: Unable to create PCI domain\n", name);
122 		return -ENOMEM;
123 	}
124 
125 	return 0;
126 }
127 
128 static int __init its_pci_of_msi_init(void)
129 {
130 	struct device_node *np;
131 
132 	for (np = of_find_matching_node(NULL, its_device_id); np;
133 	     np = of_find_matching_node(np, its_device_id)) {
134 		if (!of_device_is_available(np))
135 			continue;
136 		if (!of_property_read_bool(np, "msi-controller"))
137 			continue;
138 
139 		if (its_pci_msi_init_one(of_node_to_fwnode(np), np->full_name))
140 			continue;
141 
142 		pr_info("PCI/MSI: %pOF domain created\n", np);
143 	}
144 
145 	return 0;
146 }
147 
148 #ifdef CONFIG_ACPI
149 
150 static int __init
151 its_pci_msi_parse_madt(union acpi_subtable_headers *header,
152 		       const unsigned long end)
153 {
154 	struct acpi_madt_generic_translator *its_entry;
155 	struct fwnode_handle *dom_handle;
156 	const char *node_name;
157 	int err = -ENXIO;
158 
159 	its_entry = (struct acpi_madt_generic_translator *)header;
160 	node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
161 			      (long)its_entry->base_address);
162 	dom_handle = iort_find_domain_token(its_entry->translation_id);
163 	if (!dom_handle) {
164 		pr_err("%s: Unable to locate ITS domain handle\n", node_name);
165 		goto out;
166 	}
167 
168 	err = its_pci_msi_init_one(dom_handle, node_name);
169 	if (!err)
170 		pr_info("PCI/MSI: %s domain created\n", node_name);
171 
172 out:
173 	kfree(node_name);
174 	return err;
175 }
176 
177 static int __init its_pci_acpi_msi_init(void)
178 {
179 	acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
180 			      its_pci_msi_parse_madt, 0);
181 	return 0;
182 }
183 #else
184 static int __init its_pci_acpi_msi_init(void)
185 {
186 	return 0;
187 }
188 #endif
189 
190 static int __init its_pci_msi_init(void)
191 {
192 	its_pci_of_msi_init();
193 	its_pci_acpi_msi_init();
194 
195 	return 0;
196 }
197 early_initcall(its_pci_msi_init);
198