Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 1 | /* |
| 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 Nowicki | 723344d | 2016-09-12 20:32:27 +0200 | [diff] [blame] | 18 | #include <linux/acpi_iort.h> |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 19 | #include <linux/msi.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/of_irq.h> |
| 22 | #include <linux/of_pci.h> |
| 23 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 24 | static void its_mask_msi_irq(struct irq_data *d) |
| 25 | { |
| 26 | pci_msi_mask_irq(d); |
| 27 | irq_chip_mask_parent(d); |
| 28 | } |
| 29 | |
| 30 | static void its_unmask_msi_irq(struct irq_data *d) |
| 31 | { |
| 32 | pci_msi_unmask_irq(d); |
| 33 | irq_chip_unmask_parent(d); |
| 34 | } |
| 35 | |
| 36 | static 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 Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 44 | static int its_pci_msi_vec_count(struct pci_dev *pdev, void *data) |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 45 | { |
Robin Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 46 | int msi, msix, *count = data; |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 47 | |
| 48 | msi = max(pci_msi_vec_count(pdev), 0); |
| 49 | msix = max(pci_msix_vec_count(pdev), 0); |
Robin Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 50 | *count += max(msi, msix); |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 51 | |
Robin Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 52 | return 0; |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int its_get_pci_alias(struct pci_dev *pdev, u16 alias, void *data) |
| 56 | { |
Robin Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 57 | struct pci_dev **alias_dev = data; |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 58 | |
Robin Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 59 | *alias_dev = pdev; |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev, |
| 65 | int nvec, msi_alloc_info_t *info) |
| 66 | { |
Robin Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 67 | struct pci_dev *pdev, *alias_dev; |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 68 | struct msi_domain_info *msi_info; |
Robin Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 69 | int alias_count = 0; |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 70 | |
| 71 | if (!dev_is_pci(dev)) |
| 72 | return -EINVAL; |
| 73 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 74 | msi_info = msi_get_domain_info(domain->parent); |
| 75 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 76 | pdev = to_pci_dev(dev); |
Robin Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 77 | /* |
| 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 Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 85 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 86 | /* ITS specific DeviceID, as the core ITS ignores dev. */ |
David Daney | ccf91e6 | 2015-10-08 15:10:50 -0700 | [diff] [blame] | 87 | info->scratchpad[0].ul = pci_msi_domain_get_msi_rid(domain, pdev); |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 88 | |
| 89 | return msi_info->ops->msi_prepare(domain->parent, |
Robin Murphy | 3403b02 | 2017-05-31 18:52:27 +0100 | [diff] [blame] | 90 | dev, max(nvec, alias_count), info); |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static struct msi_domain_ops its_pci_msi_ops = { |
| 94 | .msi_prepare = its_pci_msi_prepare, |
| 95 | }; |
| 96 | |
| 97 | static struct msi_domain_info its_pci_msi_domain_info = { |
| 98 | .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | |
| 99 | MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX), |
| 100 | .ops = &its_pci_msi_ops, |
| 101 | .chip = &its_msi_irq_chip, |
| 102 | }; |
| 103 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 104 | static struct of_device_id its_device_id[] = { |
| 105 | { .compatible = "arm,gic-v3-its", }, |
| 106 | {}, |
| 107 | }; |
| 108 | |
Tomasz Nowicki | db744aaa | 2016-09-12 20:32:26 +0200 | [diff] [blame] | 109 | static int __init its_pci_msi_init_one(struct fwnode_handle *handle, |
| 110 | const char *name) |
| 111 | { |
| 112 | struct irq_domain *parent; |
| 113 | |
| 114 | parent = irq_find_matching_fwnode(handle, DOMAIN_BUS_NEXUS); |
| 115 | if (!parent || !msi_get_domain_info(parent)) { |
| 116 | pr_err("%s: Unable to locate ITS domain\n", name); |
| 117 | return -ENXIO; |
| 118 | } |
| 119 | |
| 120 | if (!pci_msi_create_irq_domain(handle, &its_pci_msi_domain_info, |
| 121 | parent)) { |
| 122 | pr_err("%s: Unable to create PCI domain\n", name); |
| 123 | return -ENOMEM; |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int __init its_pci_of_msi_init(void) |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 130 | { |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 131 | struct device_node *np; |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 132 | |
| 133 | for (np = of_find_matching_node(NULL, its_device_id); np; |
| 134 | np = of_find_matching_node(np, its_device_id)) { |
Stephen Boyd | 95a2562 | 2018-02-01 09:03:29 -0800 | [diff] [blame] | 135 | if (!of_device_is_available(np)) |
| 136 | continue; |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 137 | if (!of_property_read_bool(np, "msi-controller")) |
| 138 | continue; |
| 139 | |
Tomasz Nowicki | db744aaa | 2016-09-12 20:32:26 +0200 | [diff] [blame] | 140 | if (its_pci_msi_init_one(of_node_to_fwnode(np), np->full_name)) |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 141 | continue; |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 142 | |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 143 | pr_info("PCI/MSI: %pOF domain created\n", np); |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return 0; |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 147 | } |
Tomasz Nowicki | db744aaa | 2016-09-12 20:32:26 +0200 | [diff] [blame] | 148 | |
Tomasz Nowicki | 723344d | 2016-09-12 20:32:27 +0200 | [diff] [blame] | 149 | #ifdef CONFIG_ACPI |
| 150 | |
| 151 | static int __init |
| 152 | its_pci_msi_parse_madt(struct acpi_subtable_header *header, |
| 153 | const unsigned long end) |
| 154 | { |
| 155 | struct acpi_madt_generic_translator *its_entry; |
| 156 | struct fwnode_handle *dom_handle; |
| 157 | const char *node_name; |
| 158 | int err = -ENXIO; |
| 159 | |
| 160 | its_entry = (struct acpi_madt_generic_translator *)header; |
| 161 | node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx", |
| 162 | (long)its_entry->base_address); |
| 163 | dom_handle = iort_find_domain_token(its_entry->translation_id); |
| 164 | if (!dom_handle) { |
| 165 | pr_err("%s: Unable to locate ITS domain handle\n", node_name); |
| 166 | goto out; |
| 167 | } |
| 168 | |
| 169 | err = its_pci_msi_init_one(dom_handle, node_name); |
| 170 | if (!err) |
| 171 | pr_info("PCI/MSI: %s domain created\n", node_name); |
| 172 | |
| 173 | out: |
| 174 | kfree(node_name); |
| 175 | return err; |
| 176 | } |
| 177 | |
| 178 | static int __init its_pci_acpi_msi_init(void) |
| 179 | { |
| 180 | acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, |
| 181 | its_pci_msi_parse_madt, 0); |
| 182 | return 0; |
| 183 | } |
| 184 | #else |
| 185 | static int __init its_pci_acpi_msi_init(void) |
| 186 | { |
| 187 | return 0; |
| 188 | } |
| 189 | #endif |
| 190 | |
Tomasz Nowicki | db744aaa | 2016-09-12 20:32:26 +0200 | [diff] [blame] | 191 | static int __init its_pci_msi_init(void) |
| 192 | { |
| 193 | its_pci_of_msi_init(); |
Tomasz Nowicki | 723344d | 2016-09-12 20:32:27 +0200 | [diff] [blame] | 194 | its_pci_acpi_msi_init(); |
Tomasz Nowicki | db744aaa | 2016-09-12 20:32:26 +0200 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 198 | early_initcall(its_pci_msi_init); |