Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013-2015 ARM Limited, All Rights Reserved. |
| 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Hanjun Guo | f785f7d | 2017-03-07 20:40:02 +0800 | [diff] [blame] | 7 | #include <linux/acpi_iort.h> |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 8 | #include <linux/device.h> |
| 9 | #include <linux/msi.h> |
| 10 | #include <linux/of.h> |
| 11 | #include <linux/of_irq.h> |
| 12 | |
| 13 | static struct irq_chip its_pmsi_irq_chip = { |
| 14 | .name = "ITS-pMSI", |
| 15 | }; |
| 16 | |
Hanjun Guo | 9ab460c | 2017-03-07 20:40:00 +0800 | [diff] [blame] | 17 | static int of_pmsi_get_dev_id(struct irq_domain *domain, struct device *dev, |
| 18 | u32 *dev_id) |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 19 | { |
Marc Zyngier | deac7fc | 2015-09-18 14:07:40 +0100 | [diff] [blame] | 20 | int ret, index = 0; |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 21 | |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 22 | /* Suck the DeviceID out of the msi-parent property */ |
Marc Zyngier | deac7fc | 2015-09-18 14:07:40 +0100 | [diff] [blame] | 23 | do { |
| 24 | struct of_phandle_args args; |
| 25 | |
| 26 | ret = of_parse_phandle_with_args(dev->of_node, |
| 27 | "msi-parent", "#msi-cells", |
| 28 | index, &args); |
| 29 | if (args.np == irq_domain_get_of_node(domain)) { |
| 30 | if (WARN_ON(args.args_count != 1)) |
| 31 | return -EINVAL; |
Hanjun Guo | 9ab460c | 2017-03-07 20:40:00 +0800 | [diff] [blame] | 32 | *dev_id = args.args[0]; |
Marc Zyngier | deac7fc | 2015-09-18 14:07:40 +0100 | [diff] [blame] | 33 | break; |
| 34 | } |
Lorenzo Pieralisi | a0088737 | 2017-08-10 15:41:17 +0100 | [diff] [blame] | 35 | index++; |
Marc Zyngier | deac7fc | 2015-09-18 14:07:40 +0100 | [diff] [blame] | 36 | } while (!ret); |
| 37 | |
Hanjun Guo | 9ab460c | 2017-03-07 20:40:00 +0800 | [diff] [blame] | 38 | return ret; |
| 39 | } |
| 40 | |
Marc Zyngier | c2c8661 | 2017-03-29 15:13:07 +0100 | [diff] [blame] | 41 | int __weak iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id) |
| 42 | { |
| 43 | return -1; |
| 44 | } |
| 45 | |
Hanjun Guo | 9ab460c | 2017-03-07 20:40:00 +0800 | [diff] [blame] | 46 | static int its_pmsi_prepare(struct irq_domain *domain, struct device *dev, |
| 47 | int nvec, msi_alloc_info_t *info) |
| 48 | { |
| 49 | struct msi_domain_info *msi_info; |
| 50 | u32 dev_id; |
| 51 | int ret; |
| 52 | |
| 53 | msi_info = msi_get_domain_info(domain->parent); |
| 54 | |
Marc Zyngier | c2c8661 | 2017-03-29 15:13:07 +0100 | [diff] [blame] | 55 | if (dev->of_node) |
| 56 | ret = of_pmsi_get_dev_id(domain, dev, &dev_id); |
| 57 | else |
| 58 | ret = iort_pmsi_get_dev_id(dev, &dev_id); |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 59 | if (ret) |
| 60 | return ret; |
| 61 | |
| 62 | /* ITS specific DeviceID, as the core ITS ignores dev. */ |
| 63 | info->scratchpad[0].ul = dev_id; |
| 64 | |
Marc Zyngier | 147c8f3 | 2018-05-27 16:39:55 +0100 | [diff] [blame] | 65 | /* Allocate at least 32 MSIs, and always as a power of 2 */ |
| 66 | nvec = max_t(int, 32, roundup_pow_of_two(nvec)); |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 67 | return msi_info->ops->msi_prepare(domain->parent, |
| 68 | dev, nvec, info); |
| 69 | } |
| 70 | |
| 71 | static struct msi_domain_ops its_pmsi_ops = { |
| 72 | .msi_prepare = its_pmsi_prepare, |
| 73 | }; |
| 74 | |
| 75 | static struct msi_domain_info its_pmsi_domain_info = { |
| 76 | .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS), |
| 77 | .ops = &its_pmsi_ops, |
| 78 | .chip = &its_pmsi_irq_chip, |
| 79 | }; |
| 80 | |
Arvind Yadav | 5b86706 | 2017-06-22 16:11:13 +0530 | [diff] [blame] | 81 | static const struct of_device_id its_device_id[] = { |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 82 | { .compatible = "arm,gic-v3-its", }, |
| 83 | {}, |
| 84 | }; |
| 85 | |
Hanjun Guo | 42677db | 2017-03-07 20:40:01 +0800 | [diff] [blame] | 86 | static int __init its_pmsi_init_one(struct fwnode_handle *fwnode, |
| 87 | const char *name) |
| 88 | { |
| 89 | struct irq_domain *parent; |
| 90 | |
| 91 | parent = irq_find_matching_fwnode(fwnode, DOMAIN_BUS_NEXUS); |
| 92 | if (!parent || !msi_get_domain_info(parent)) { |
| 93 | pr_err("%s: unable to locate ITS domain\n", name); |
| 94 | return -ENXIO; |
| 95 | } |
| 96 | |
| 97 | if (!platform_msi_create_irq_domain(fwnode, &its_pmsi_domain_info, |
| 98 | parent)) { |
| 99 | pr_err("%s: unable to create platform domain\n", name); |
| 100 | return -ENXIO; |
| 101 | } |
| 102 | |
| 103 | pr_info("Platform MSI: %s domain created\n", name); |
| 104 | return 0; |
| 105 | } |
| 106 | |
Hanjun Guo | f785f7d | 2017-03-07 20:40:02 +0800 | [diff] [blame] | 107 | #ifdef CONFIG_ACPI |
| 108 | static int __init |
Keith Busch | 60574d1 | 2019-03-11 14:55:57 -0600 | [diff] [blame] | 109 | its_pmsi_parse_madt(union acpi_subtable_headers *header, |
Hanjun Guo | f785f7d | 2017-03-07 20:40:02 +0800 | [diff] [blame] | 110 | const unsigned long end) |
| 111 | { |
| 112 | struct acpi_madt_generic_translator *its_entry; |
| 113 | struct fwnode_handle *domain_handle; |
| 114 | const char *node_name; |
| 115 | int err = -ENXIO; |
| 116 | |
| 117 | its_entry = (struct acpi_madt_generic_translator *)header; |
| 118 | node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx", |
| 119 | (long)its_entry->base_address); |
| 120 | domain_handle = iort_find_domain_token(its_entry->translation_id); |
| 121 | if (!domain_handle) { |
| 122 | pr_err("%s: Unable to locate ITS domain handle\n", node_name); |
| 123 | goto out; |
| 124 | } |
| 125 | |
| 126 | err = its_pmsi_init_one(domain_handle, node_name); |
| 127 | |
| 128 | out: |
| 129 | kfree(node_name); |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | static void __init its_pmsi_acpi_init(void) |
| 134 | { |
| 135 | acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, |
| 136 | its_pmsi_parse_madt, 0); |
| 137 | } |
| 138 | #else |
| 139 | static inline void its_pmsi_acpi_init(void) { } |
| 140 | #endif |
| 141 | |
Hanjun Guo | 42677db | 2017-03-07 20:40:01 +0800 | [diff] [blame] | 142 | static void __init its_pmsi_of_init(void) |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 143 | { |
| 144 | struct device_node *np; |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 145 | |
| 146 | for (np = of_find_matching_node(NULL, its_device_id); np; |
| 147 | np = of_find_matching_node(np, its_device_id)) { |
Stephen Boyd | 95a2562 | 2018-02-01 09:03:29 -0800 | [diff] [blame] | 148 | if (!of_device_is_available(np)) |
| 149 | continue; |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 150 | if (!of_property_read_bool(np, "msi-controller")) |
| 151 | continue; |
| 152 | |
Hanjun Guo | 42677db | 2017-03-07 20:40:01 +0800 | [diff] [blame] | 153 | its_pmsi_init_one(of_node_to_fwnode(np), np->full_name); |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 154 | } |
Hanjun Guo | 42677db | 2017-03-07 20:40:01 +0800 | [diff] [blame] | 155 | } |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 156 | |
Hanjun Guo | 42677db | 2017-03-07 20:40:01 +0800 | [diff] [blame] | 157 | static int __init its_pmsi_init(void) |
| 158 | { |
| 159 | its_pmsi_of_init(); |
Hanjun Guo | f785f7d | 2017-03-07 20:40:02 +0800 | [diff] [blame] | 160 | its_pmsi_acpi_init(); |
Marc Zyngier | 1e6db00 | 2015-07-28 14:46:22 +0100 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | early_initcall(its_pmsi_init); |