blob: 8d6d009d1d586f9271c508138bdbc55c064c8f9e [file] [log] [blame]
Marc Zyngierf1304202015-07-28 14:46:18 +01001/*
2 * Copyright (C) 2013-2015 ARM Limited, All Rights Reserved.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Tomasz Nowicki723344d2016-09-12 20:32:27 +020018#include <linux/acpi_iort.h>
Marc Zyngierf1304202015-07-28 14:46:18 +010019#include <linux/msi.h>
20#include <linux/of.h>
21#include <linux/of_irq.h>
22#include <linux/of_pci.h>
23
Marc Zyngierf1304202015-07-28 14:46:18 +010024static void its_mask_msi_irq(struct irq_data *d)
25{
26 pci_msi_mask_irq(d);
27 irq_chip_mask_parent(d);
28}
29
30static void its_unmask_msi_irq(struct irq_data *d)
31{
32 pci_msi_unmask_irq(d);
33 irq_chip_unmask_parent(d);
34}
35
36static struct irq_chip its_msi_irq_chip = {
37 .name = "ITS-MSI",
38 .irq_unmask = its_unmask_msi_irq,
39 .irq_mask = its_mask_msi_irq,
40 .irq_eoi = irq_chip_eoi_parent,
41 .irq_write_msi_msg = pci_msi_domain_write_msg,
42};
43
Robin Murphy3403b022017-05-31 18:52:27 +010044static int its_pci_msi_vec_count(struct pci_dev *pdev, void *data)
Marc Zyngierf1304202015-07-28 14:46:18 +010045{
Robin Murphy3403b022017-05-31 18:52:27 +010046 int msi, msix, *count = data;
Marc Zyngierf1304202015-07-28 14:46:18 +010047
48 msi = max(pci_msi_vec_count(pdev), 0);
49 msix = max(pci_msix_vec_count(pdev), 0);
Robin Murphy3403b022017-05-31 18:52:27 +010050 *count += max(msi, msix);
Marc Zyngierf1304202015-07-28 14:46:18 +010051
Robin Murphy3403b022017-05-31 18:52:27 +010052 return 0;
Marc Zyngierf1304202015-07-28 14:46:18 +010053}
54
55static int its_get_pci_alias(struct pci_dev *pdev, u16 alias, void *data)
56{
Robin Murphy3403b022017-05-31 18:52:27 +010057 struct pci_dev **alias_dev = data;
Marc Zyngierf1304202015-07-28 14:46:18 +010058
Robin Murphy3403b022017-05-31 18:52:27 +010059 *alias_dev = pdev;
Marc Zyngierf1304202015-07-28 14:46:18 +010060
61 return 0;
62}
63
64static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
65 int nvec, msi_alloc_info_t *info)
66{
Robin Murphy3403b022017-05-31 18:52:27 +010067 struct pci_dev *pdev, *alias_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +010068 struct msi_domain_info *msi_info;
Marc Zyngier30800b32018-05-31 16:21:43 +010069 int alias_count = 0, minnvec = 1;
Marc Zyngierf1304202015-07-28 14:46:18 +010070
71 if (!dev_is_pci(dev))
72 return -EINVAL;
73
Marc Zyngier54456db2015-07-28 14:46:21 +010074 msi_info = msi_get_domain_info(domain->parent);
75
Marc Zyngierf1304202015-07-28 14:46:18 +010076 pdev = to_pci_dev(dev);
Robin Murphy3403b022017-05-31 18:52:27 +010077 /*
78 * If pdev is downstream of any aliasing bridges, take an upper
79 * bound of how many other vectors could map to the same DevID.
80 */
81 pci_for_each_dma_alias(pdev, its_get_pci_alias, &alias_dev);
82 if (alias_dev != pdev && alias_dev->subordinate)
83 pci_walk_bus(alias_dev->subordinate, its_pci_msi_vec_count,
84 &alias_count);
Marc Zyngierf1304202015-07-28 14:46:18 +010085
Marc Zyngier54456db2015-07-28 14:46:21 +010086 /* ITS specific DeviceID, as the core ITS ignores dev. */
David Daneyccf91e62015-10-08 15:10:50 -070087 info->scratchpad[0].ul = pci_msi_domain_get_msi_rid(domain, pdev);
Marc Zyngier54456db2015-07-28 14:46:21 +010088
Marc Zyngier30800b32018-05-31 16:21:43 +010089 /*
90 * Always allocate a power of 2, and special case device 0 for
91 * broken systems where the DevID is not wired (and all devices
92 * appear as DevID 0). For that reason, we generously allocate a
93 * minimum of 32 MSIs for DevID 0. If you want more because all
94 * your devices are aliasing to DevID 0, consider fixing your HW.
95 */
Marc Zyngier147c8f32018-05-27 16:39:55 +010096 nvec = max(nvec, alias_count);
Marc Zyngier30800b32018-05-31 16:21:43 +010097 if (!info->scratchpad[0].ul)
98 minnvec = 32;
99 nvec = max_t(int, minnvec, roundup_pow_of_two(nvec));
Marc Zyngier147c8f32018-05-27 16:39:55 +0100100 return msi_info->ops->msi_prepare(domain->parent, dev, nvec, info);
Marc Zyngierf1304202015-07-28 14:46:18 +0100101}
102
103static struct msi_domain_ops its_pci_msi_ops = {
104 .msi_prepare = its_pci_msi_prepare,
105};
106
107static struct msi_domain_info its_pci_msi_domain_info = {
108 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
109 MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
110 .ops = &its_pci_msi_ops,
111 .chip = &its_msi_irq_chip,
112};
113
Marc Zyngier54456db2015-07-28 14:46:21 +0100114static struct of_device_id its_device_id[] = {
115 { .compatible = "arm,gic-v3-its", },
116 {},
117};
118
Tomasz Nowickidb744aaa2016-09-12 20:32:26 +0200119static int __init its_pci_msi_init_one(struct fwnode_handle *handle,
120 const char *name)
121{
122 struct irq_domain *parent;
123
124 parent = irq_find_matching_fwnode(handle, DOMAIN_BUS_NEXUS);
125 if (!parent || !msi_get_domain_info(parent)) {
126 pr_err("%s: Unable to locate ITS domain\n", name);
127 return -ENXIO;
128 }
129
130 if (!pci_msi_create_irq_domain(handle, &its_pci_msi_domain_info,
131 parent)) {
132 pr_err("%s: Unable to create PCI domain\n", name);
133 return -ENOMEM;
134 }
135
136 return 0;
137}
138
139static int __init its_pci_of_msi_init(void)
Marc Zyngierf1304202015-07-28 14:46:18 +0100140{
Marc Zyngier54456db2015-07-28 14:46:21 +0100141 struct device_node *np;
Marc Zyngier54456db2015-07-28 14:46:21 +0100142
143 for (np = of_find_matching_node(NULL, its_device_id); np;
144 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -0800145 if (!of_device_is_available(np))
146 continue;
Marc Zyngier54456db2015-07-28 14:46:21 +0100147 if (!of_property_read_bool(np, "msi-controller"))
148 continue;
149
Tomasz Nowickidb744aaa2016-09-12 20:32:26 +0200150 if (its_pci_msi_init_one(of_node_to_fwnode(np), np->full_name))
Marc Zyngier54456db2015-07-28 14:46:21 +0100151 continue;
Marc Zyngier54456db2015-07-28 14:46:21 +0100152
Rob Herringe81f54c2017-07-18 16:43:10 -0500153 pr_info("PCI/MSI: %pOF domain created\n", np);
Marc Zyngier54456db2015-07-28 14:46:21 +0100154 }
155
156 return 0;
Marc Zyngierf1304202015-07-28 14:46:18 +0100157}
Tomasz Nowickidb744aaa2016-09-12 20:32:26 +0200158
Tomasz Nowicki723344d2016-09-12 20:32:27 +0200159#ifdef CONFIG_ACPI
160
161static int __init
162its_pci_msi_parse_madt(struct acpi_subtable_header *header,
163 const unsigned long end)
164{
165 struct acpi_madt_generic_translator *its_entry;
166 struct fwnode_handle *dom_handle;
167 const char *node_name;
168 int err = -ENXIO;
169
170 its_entry = (struct acpi_madt_generic_translator *)header;
171 node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
172 (long)its_entry->base_address);
173 dom_handle = iort_find_domain_token(its_entry->translation_id);
174 if (!dom_handle) {
175 pr_err("%s: Unable to locate ITS domain handle\n", node_name);
176 goto out;
177 }
178
179 err = its_pci_msi_init_one(dom_handle, node_name);
180 if (!err)
181 pr_info("PCI/MSI: %s domain created\n", node_name);
182
183out:
184 kfree(node_name);
185 return err;
186}
187
188static int __init its_pci_acpi_msi_init(void)
189{
190 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
191 its_pci_msi_parse_madt, 0);
192 return 0;
193}
194#else
195static int __init its_pci_acpi_msi_init(void)
196{
197 return 0;
198}
199#endif
200
Tomasz Nowickidb744aaa2016-09-12 20:32:26 +0200201static int __init its_pci_msi_init(void)
202{
203 its_pci_of_msi_init();
Tomasz Nowicki723344d2016-09-12 20:32:27 +0200204 its_pci_acpi_msi_init();
Tomasz Nowickidb744aaa2016-09-12 20:32:26 +0200205
206 return 0;
207}
Marc Zyngier54456db2015-07-28 14:46:21 +0100208early_initcall(its_pci_msi_init);