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