Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2013-2015 ARM Limited, All Rights Reserved.
0004  * Author: Marc Zyngier <marc.zyngier@arm.com>
0005  */
0006 
0007 #include <linux/acpi_iort.h>
0008 #include <linux/device.h>
0009 #include <linux/msi.h>
0010 #include <linux/of.h>
0011 #include <linux/of_irq.h>
0012 
0013 static struct irq_chip its_pmsi_irq_chip = {
0014     .name           = "ITS-pMSI",
0015 };
0016 
0017 static int of_pmsi_get_dev_id(struct irq_domain *domain, struct device *dev,
0018                   u32 *dev_id)
0019 {
0020     int ret, index = 0;
0021 
0022     /* Suck the DeviceID out of the msi-parent property */
0023     do {
0024         struct of_phandle_args args;
0025 
0026         ret = of_parse_phandle_with_args(dev->of_node,
0027                          "msi-parent", "#msi-cells",
0028                          index, &args);
0029         if (args.np == irq_domain_get_of_node(domain)) {
0030             if (WARN_ON(args.args_count != 1))
0031                 return -EINVAL;
0032             *dev_id = args.args[0];
0033             break;
0034         }
0035         index++;
0036     } while (!ret);
0037 
0038     return ret;
0039 }
0040 
0041 int __weak iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
0042 {
0043     return -1;
0044 }
0045 
0046 static int its_pmsi_prepare(struct irq_domain *domain, struct device *dev,
0047                 int nvec, msi_alloc_info_t *info)
0048 {
0049     struct msi_domain_info *msi_info;
0050     u32 dev_id;
0051     int ret;
0052 
0053     msi_info = msi_get_domain_info(domain->parent);
0054 
0055     if (dev->of_node)
0056         ret = of_pmsi_get_dev_id(domain, dev, &dev_id);
0057     else
0058         ret = iort_pmsi_get_dev_id(dev, &dev_id);
0059     if (ret)
0060         return ret;
0061 
0062     /* ITS specific DeviceID, as the core ITS ignores dev. */
0063     info->scratchpad[0].ul = dev_id;
0064 
0065     /* Allocate at least 32 MSIs, and always as a power of 2 */
0066     nvec = max_t(int, 32, roundup_pow_of_two(nvec));
0067     return msi_info->ops->msi_prepare(domain->parent,
0068                       dev, nvec, info);
0069 }
0070 
0071 static struct msi_domain_ops its_pmsi_ops = {
0072     .msi_prepare    = its_pmsi_prepare,
0073 };
0074 
0075 static struct msi_domain_info its_pmsi_domain_info = {
0076     .flags  = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
0077     .ops    = &its_pmsi_ops,
0078     .chip   = &its_pmsi_irq_chip,
0079 };
0080 
0081 static const struct of_device_id its_device_id[] = {
0082     {   .compatible = "arm,gic-v3-its", },
0083     {},
0084 };
0085 
0086 static int __init its_pmsi_init_one(struct fwnode_handle *fwnode,
0087                 const char *name)
0088 {
0089     struct irq_domain *parent;
0090 
0091     parent = irq_find_matching_fwnode(fwnode, DOMAIN_BUS_NEXUS);
0092     if (!parent || !msi_get_domain_info(parent)) {
0093         pr_err("%s: unable to locate ITS domain\n", name);
0094         return -ENXIO;
0095     }
0096 
0097     if (!platform_msi_create_irq_domain(fwnode, &its_pmsi_domain_info,
0098                         parent)) {
0099         pr_err("%s: unable to create platform domain\n", name);
0100         return -ENXIO;
0101     }
0102 
0103     pr_info("Platform MSI: %s domain created\n", name);
0104     return 0;
0105 }
0106 
0107 #ifdef CONFIG_ACPI
0108 static int __init
0109 its_pmsi_parse_madt(union acpi_subtable_headers *header,
0110             const unsigned long end)
0111 {
0112     struct acpi_madt_generic_translator *its_entry;
0113     struct fwnode_handle *domain_handle;
0114     const char *node_name;
0115     int err = -ENXIO;
0116 
0117     its_entry = (struct acpi_madt_generic_translator *)header;
0118     node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
0119                   (long)its_entry->base_address);
0120     domain_handle = iort_find_domain_token(its_entry->translation_id);
0121     if (!domain_handle) {
0122         pr_err("%s: Unable to locate ITS domain handle\n", node_name);
0123         goto out;
0124     }
0125 
0126     err = its_pmsi_init_one(domain_handle, node_name);
0127 
0128 out:
0129     kfree(node_name);
0130     return err;
0131 }
0132 
0133 static void __init its_pmsi_acpi_init(void)
0134 {
0135     acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
0136                   its_pmsi_parse_madt, 0);
0137 }
0138 #else
0139 static inline void its_pmsi_acpi_init(void) { }
0140 #endif
0141 
0142 static void __init its_pmsi_of_init(void)
0143 {
0144     struct device_node *np;
0145 
0146     for (np = of_find_matching_node(NULL, its_device_id); np;
0147          np = of_find_matching_node(np, its_device_id)) {
0148         if (!of_device_is_available(np))
0149             continue;
0150         if (!of_property_read_bool(np, "msi-controller"))
0151             continue;
0152 
0153         its_pmsi_init_one(of_node_to_fwnode(np), np->full_name);
0154     }
0155 }
0156 
0157 static int __init its_pmsi_init(void)
0158 {
0159     its_pmsi_of_init();
0160     its_pmsi_acpi_init();
0161     return 0;
0162 }
0163 early_initcall(its_pmsi_init);