blob: 9e9dda33eb17460bd661c2a992a06594752bffb5 [file] [log] [blame]
Marc Zyngier1e6db002015-07-28 14:46:22 +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
Hanjun Guof785f7d2017-03-07 20:40:02 +080018#include <linux/acpi_iort.h>
Marc Zyngier1e6db002015-07-28 14:46:22 +010019#include <linux/device.h>
20#include <linux/msi.h>
21#include <linux/of.h>
22#include <linux/of_irq.h>
23
24static struct irq_chip its_pmsi_irq_chip = {
25 .name = "ITS-pMSI",
26};
27
Hanjun Guo9ab460c2017-03-07 20:40:00 +080028static int of_pmsi_get_dev_id(struct irq_domain *domain, struct device *dev,
29 u32 *dev_id)
Marc Zyngier1e6db002015-07-28 14:46:22 +010030{
Marc Zyngierdeac7fc2015-09-18 14:07:40 +010031 int ret, index = 0;
Marc Zyngier1e6db002015-07-28 14:46:22 +010032
Marc Zyngier1e6db002015-07-28 14:46:22 +010033 /* Suck the DeviceID out of the msi-parent property */
Marc Zyngierdeac7fc2015-09-18 14:07:40 +010034 do {
35 struct of_phandle_args args;
36
37 ret = of_parse_phandle_with_args(dev->of_node,
38 "msi-parent", "#msi-cells",
39 index, &args);
40 if (args.np == irq_domain_get_of_node(domain)) {
41 if (WARN_ON(args.args_count != 1))
42 return -EINVAL;
Hanjun Guo9ab460c2017-03-07 20:40:00 +080043 *dev_id = args.args[0];
Marc Zyngierdeac7fc2015-09-18 14:07:40 +010044 break;
45 }
46 } while (!ret);
47
Hanjun Guo9ab460c2017-03-07 20:40:00 +080048 return ret;
49}
50
Marc Zyngierc2c86612017-03-29 15:13:07 +010051int __weak iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
52{
53 return -1;
54}
55
Hanjun Guo9ab460c2017-03-07 20:40:00 +080056static int its_pmsi_prepare(struct irq_domain *domain, struct device *dev,
57 int nvec, msi_alloc_info_t *info)
58{
59 struct msi_domain_info *msi_info;
60 u32 dev_id;
61 int ret;
62
63 msi_info = msi_get_domain_info(domain->parent);
64
Marc Zyngierc2c86612017-03-29 15:13:07 +010065 if (dev->of_node)
66 ret = of_pmsi_get_dev_id(domain, dev, &dev_id);
67 else
68 ret = iort_pmsi_get_dev_id(dev, &dev_id);
Marc Zyngier1e6db002015-07-28 14:46:22 +010069 if (ret)
70 return ret;
71
72 /* ITS specific DeviceID, as the core ITS ignores dev. */
73 info->scratchpad[0].ul = dev_id;
74
75 return msi_info->ops->msi_prepare(domain->parent,
76 dev, nvec, info);
77}
78
79static struct msi_domain_ops its_pmsi_ops = {
80 .msi_prepare = its_pmsi_prepare,
81};
82
83static struct msi_domain_info its_pmsi_domain_info = {
84 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
85 .ops = &its_pmsi_ops,
86 .chip = &its_pmsi_irq_chip,
87};
88
89static struct of_device_id its_device_id[] = {
90 { .compatible = "arm,gic-v3-its", },
91 {},
92};
93
Hanjun Guo42677db2017-03-07 20:40:01 +080094static int __init its_pmsi_init_one(struct fwnode_handle *fwnode,
95 const char *name)
96{
97 struct irq_domain *parent;
98
99 parent = irq_find_matching_fwnode(fwnode, DOMAIN_BUS_NEXUS);
100 if (!parent || !msi_get_domain_info(parent)) {
101 pr_err("%s: unable to locate ITS domain\n", name);
102 return -ENXIO;
103 }
104
105 if (!platform_msi_create_irq_domain(fwnode, &its_pmsi_domain_info,
106 parent)) {
107 pr_err("%s: unable to create platform domain\n", name);
108 return -ENXIO;
109 }
110
111 pr_info("Platform MSI: %s domain created\n", name);
112 return 0;
113}
114
Hanjun Guof785f7d2017-03-07 20:40:02 +0800115#ifdef CONFIG_ACPI
116static int __init
117its_pmsi_parse_madt(struct acpi_subtable_header *header,
118 const unsigned long end)
119{
120 struct acpi_madt_generic_translator *its_entry;
121 struct fwnode_handle *domain_handle;
122 const char *node_name;
123 int err = -ENXIO;
124
125 its_entry = (struct acpi_madt_generic_translator *)header;
126 node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
127 (long)its_entry->base_address);
128 domain_handle = iort_find_domain_token(its_entry->translation_id);
129 if (!domain_handle) {
130 pr_err("%s: Unable to locate ITS domain handle\n", node_name);
131 goto out;
132 }
133
134 err = its_pmsi_init_one(domain_handle, node_name);
135
136out:
137 kfree(node_name);
138 return err;
139}
140
141static void __init its_pmsi_acpi_init(void)
142{
143 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
144 its_pmsi_parse_madt, 0);
145}
146#else
147static inline void its_pmsi_acpi_init(void) { }
148#endif
149
Hanjun Guo42677db2017-03-07 20:40:01 +0800150static void __init its_pmsi_of_init(void)
Marc Zyngier1e6db002015-07-28 14:46:22 +0100151{
152 struct device_node *np;
Marc Zyngier1e6db002015-07-28 14:46:22 +0100153
154 for (np = of_find_matching_node(NULL, its_device_id); np;
155 np = of_find_matching_node(np, its_device_id)) {
156 if (!of_property_read_bool(np, "msi-controller"))
157 continue;
158
Hanjun Guo42677db2017-03-07 20:40:01 +0800159 its_pmsi_init_one(of_node_to_fwnode(np), np->full_name);
Marc Zyngier1e6db002015-07-28 14:46:22 +0100160 }
Hanjun Guo42677db2017-03-07 20:40:01 +0800161}
Marc Zyngier1e6db002015-07-28 14:46:22 +0100162
Hanjun Guo42677db2017-03-07 20:40:01 +0800163static int __init its_pmsi_init(void)
164{
165 its_pmsi_of_init();
Hanjun Guof785f7d2017-03-07 20:40:02 +0800166 its_pmsi_acpi_init();
Marc Zyngier1e6db002015-07-28 14:46:22 +0100167 return 0;
168}
169early_initcall(its_pmsi_init);