Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Freescale Management Complex (MC) bus driver MSI support
0004  *
0005  * Copyright (C) 2015-2016 Freescale Semiconductor, Inc.
0006  * Author: German Rivera <German.Rivera@freescale.com>
0007  *
0008  */
0009 
0010 #include <linux/of_device.h>
0011 #include <linux/of_address.h>
0012 #include <linux/of_irq.h>
0013 #include <linux/irq.h>
0014 #include <linux/irqdomain.h>
0015 #include <linux/msi.h>
0016 #include <linux/acpi_iort.h>
0017 
0018 #include "fsl-mc-private.h"
0019 
0020 #ifdef GENERIC_MSI_DOMAIN_OPS
0021 /*
0022  * Generate a unique ID identifying the interrupt (only used within the MSI
0023  * irqdomain.  Combine the icid with the interrupt index.
0024  */
0025 static irq_hw_number_t fsl_mc_domain_calc_hwirq(struct fsl_mc_device *dev,
0026                         struct msi_desc *desc)
0027 {
0028     /*
0029      * Make the base hwirq value for ICID*10000 so it is readable
0030      * as a decimal value in /proc/interrupts.
0031      */
0032     return (irq_hw_number_t)(desc->msi_index + (dev->icid * 10000));
0033 }
0034 
0035 static void fsl_mc_msi_set_desc(msi_alloc_info_t *arg,
0036                 struct msi_desc *desc)
0037 {
0038     arg->desc = desc;
0039     arg->hwirq = fsl_mc_domain_calc_hwirq(to_fsl_mc_device(desc->dev),
0040                           desc);
0041 }
0042 #else
0043 #define fsl_mc_msi_set_desc NULL
0044 #endif
0045 
0046 static void fsl_mc_msi_update_dom_ops(struct msi_domain_info *info)
0047 {
0048     struct msi_domain_ops *ops = info->ops;
0049 
0050     if (!ops)
0051         return;
0052 
0053     /*
0054      * set_desc should not be set by the caller
0055      */
0056     if (!ops->set_desc)
0057         ops->set_desc = fsl_mc_msi_set_desc;
0058 }
0059 
0060 static void __fsl_mc_msi_write_msg(struct fsl_mc_device *mc_bus_dev,
0061                    struct fsl_mc_device_irq *mc_dev_irq,
0062                    struct msi_desc *msi_desc)
0063 {
0064     int error;
0065     struct fsl_mc_device *owner_mc_dev = mc_dev_irq->mc_dev;
0066     struct dprc_irq_cfg irq_cfg;
0067 
0068     /*
0069      * msi_desc->msg.address is 0x0 when this function is invoked in
0070      * the free_irq() code path. In this case, for the MC, we don't
0071      * really need to "unprogram" the MSI, so we just return.
0072      */
0073     if (msi_desc->msg.address_lo == 0x0 && msi_desc->msg.address_hi == 0x0)
0074         return;
0075 
0076     if (!owner_mc_dev)
0077         return;
0078 
0079     irq_cfg.paddr = ((u64)msi_desc->msg.address_hi << 32) |
0080             msi_desc->msg.address_lo;
0081     irq_cfg.val = msi_desc->msg.data;
0082     irq_cfg.irq_num = msi_desc->irq;
0083 
0084     if (owner_mc_dev == mc_bus_dev) {
0085         /*
0086          * IRQ is for the mc_bus_dev's DPRC itself
0087          */
0088         error = dprc_set_irq(mc_bus_dev->mc_io,
0089                      MC_CMD_FLAG_INTR_DIS | MC_CMD_FLAG_PRI,
0090                      mc_bus_dev->mc_handle,
0091                      mc_dev_irq->dev_irq_index,
0092                      &irq_cfg);
0093         if (error < 0) {
0094             dev_err(&owner_mc_dev->dev,
0095                 "dprc_set_irq() failed: %d\n", error);
0096         }
0097     } else {
0098         /*
0099          * IRQ is for for a child device of mc_bus_dev
0100          */
0101         error = dprc_set_obj_irq(mc_bus_dev->mc_io,
0102                      MC_CMD_FLAG_INTR_DIS | MC_CMD_FLAG_PRI,
0103                      mc_bus_dev->mc_handle,
0104                      owner_mc_dev->obj_desc.type,
0105                      owner_mc_dev->obj_desc.id,
0106                      mc_dev_irq->dev_irq_index,
0107                      &irq_cfg);
0108         if (error < 0) {
0109             dev_err(&owner_mc_dev->dev,
0110                 "dprc_obj_set_irq() failed: %d\n", error);
0111         }
0112     }
0113 }
0114 
0115 /*
0116  * NOTE: This function is invoked with interrupts disabled
0117  */
0118 static void fsl_mc_msi_write_msg(struct irq_data *irq_data,
0119                  struct msi_msg *msg)
0120 {
0121     struct msi_desc *msi_desc = irq_data_get_msi_desc(irq_data);
0122     struct fsl_mc_device *mc_bus_dev = to_fsl_mc_device(msi_desc->dev);
0123     struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
0124     struct fsl_mc_device_irq *mc_dev_irq =
0125         &mc_bus->irq_resources[msi_desc->msi_index];
0126 
0127     msi_desc->msg = *msg;
0128 
0129     /*
0130      * Program the MSI (paddr, value) pair in the device:
0131      */
0132     __fsl_mc_msi_write_msg(mc_bus_dev, mc_dev_irq, msi_desc);
0133 }
0134 
0135 static void fsl_mc_msi_update_chip_ops(struct msi_domain_info *info)
0136 {
0137     struct irq_chip *chip = info->chip;
0138 
0139     if (!chip)
0140         return;
0141 
0142     /*
0143      * irq_write_msi_msg should not be set by the caller
0144      */
0145     if (!chip->irq_write_msi_msg)
0146         chip->irq_write_msi_msg = fsl_mc_msi_write_msg;
0147 }
0148 
0149 /**
0150  * fsl_mc_msi_create_irq_domain - Create a fsl-mc MSI interrupt domain
0151  * @fwnode: Optional firmware node of the interrupt controller
0152  * @info:   MSI domain info
0153  * @parent: Parent irq domain
0154  *
0155  * Updates the domain and chip ops and creates a fsl-mc MSI
0156  * interrupt domain.
0157  *
0158  * Returns:
0159  * A domain pointer or NULL in case of failure.
0160  */
0161 struct irq_domain *fsl_mc_msi_create_irq_domain(struct fwnode_handle *fwnode,
0162                         struct msi_domain_info *info,
0163                         struct irq_domain *parent)
0164 {
0165     struct irq_domain *domain;
0166 
0167     if (WARN_ON((info->flags & MSI_FLAG_LEVEL_CAPABLE)))
0168         info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
0169     if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
0170         fsl_mc_msi_update_dom_ops(info);
0171     if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
0172         fsl_mc_msi_update_chip_ops(info);
0173     info->flags |= MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS | MSI_FLAG_FREE_MSI_DESCS;
0174 
0175     domain = msi_create_irq_domain(fwnode, info, parent);
0176     if (domain)
0177         irq_domain_update_bus_token(domain, DOMAIN_BUS_FSL_MC_MSI);
0178 
0179     return domain;
0180 }
0181 
0182 struct irq_domain *fsl_mc_find_msi_domain(struct device *dev)
0183 {
0184     struct device *root_dprc_dev;
0185     struct device *bus_dev;
0186     struct irq_domain *msi_domain;
0187     struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
0188 
0189     fsl_mc_get_root_dprc(dev, &root_dprc_dev);
0190     bus_dev = root_dprc_dev->parent;
0191 
0192     if (bus_dev->of_node) {
0193         msi_domain = of_msi_map_get_device_domain(dev,
0194                           mc_dev->icid,
0195                           DOMAIN_BUS_FSL_MC_MSI);
0196 
0197         /*
0198          * if the msi-map property is missing assume that all the
0199          * child containers inherit the domain from the parent
0200          */
0201         if (!msi_domain)
0202 
0203             msi_domain = of_msi_get_domain(bus_dev,
0204                         bus_dev->of_node,
0205                         DOMAIN_BUS_FSL_MC_MSI);
0206     } else {
0207         msi_domain = iort_get_device_domain(dev, mc_dev->icid,
0208                             DOMAIN_BUS_FSL_MC_MSI);
0209     }
0210 
0211     return msi_domain;
0212 }
0213 
0214 int fsl_mc_msi_domain_alloc_irqs(struct device *dev,  unsigned int irq_count)
0215 {
0216     struct irq_domain *msi_domain;
0217     int error;
0218 
0219     msi_domain = dev_get_msi_domain(dev);
0220     if (!msi_domain)
0221         return -EINVAL;
0222 
0223     error = msi_setup_device_data(dev);
0224     if (error)
0225         return error;
0226 
0227     msi_lock_descs(dev);
0228     if (msi_first_desc(dev, MSI_DESC_ALL))
0229         error = -EINVAL;
0230     msi_unlock_descs(dev);
0231     if (error)
0232         return error;
0233 
0234     /*
0235      * NOTE: Calling this function will trigger the invocation of the
0236      * its_fsl_mc_msi_prepare() callback
0237      */
0238     error = msi_domain_alloc_irqs(msi_domain, dev, irq_count);
0239 
0240     if (error)
0241         dev_err(dev, "Failed to allocate IRQs\n");
0242     return error;
0243 }
0244 
0245 void fsl_mc_msi_domain_free_irqs(struct device *dev)
0246 {
0247     struct irq_domain *msi_domain;
0248 
0249     msi_domain = dev_get_msi_domain(dev);
0250     if (!msi_domain)
0251         return;
0252 
0253     msi_domain_free_irqs(msi_domain, dev);
0254 }