2 * Freescale Management Complex (MC) bus driver MSI support
4 * Copyright (C) 2015-2016 Freescale Semiconductor, Inc.
5 * Author: German Rivera <German.Rivera@freescale.com>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include <linux/of_device.h>
13 #include <linux/of_address.h>
14 #include <linux/irqchip/arm-gic-v3.h>
15 #include <linux/of_irq.h>
16 #include <linux/irq.h>
17 #include <linux/irqdomain.h>
18 #include <linux/msi.h>
19 #include "../include/mc-bus.h"
20 #include "../include/mc-cmd.h"
21 #include "fsl-mc-private.h"
24 * Generate a unique ID identifying the interrupt (only used within the MSI
25 * irqdomain. Combine the icid with the interrupt index.
27 static irq_hw_number_t fsl_mc_domain_calc_hwirq(struct fsl_mc_device *dev,
28 struct msi_desc *desc)
31 * Make the base hwirq value for ICID*10000 so it is readable
32 * as a decimal value in /proc/interrupts.
34 return (irq_hw_number_t)(desc->fsl_mc.msi_index + (dev->icid * 10000));
37 static void fsl_mc_msi_set_desc(msi_alloc_info_t *arg,
38 struct msi_desc *desc)
41 arg->hwirq = fsl_mc_domain_calc_hwirq(to_fsl_mc_device(desc->dev),
45 static void fsl_mc_msi_update_dom_ops(struct msi_domain_info *info)
47 struct msi_domain_ops *ops = info->ops;
53 * set_desc should not be set by the caller
56 ops->set_desc = fsl_mc_msi_set_desc;
59 static void __fsl_mc_msi_write_msg(struct fsl_mc_device *mc_bus_dev,
60 struct fsl_mc_device_irq *mc_dev_irq)
63 struct fsl_mc_device *owner_mc_dev = mc_dev_irq->mc_dev;
64 struct msi_desc *msi_desc = mc_dev_irq->msi_desc;
65 struct dprc_irq_cfg irq_cfg;
68 * msi_desc->msg.address is 0x0 when this function is invoked in
69 * the free_irq() code path. In this case, for the MC, we don't
70 * really need to "unprogram" the MSI, so we just return.
72 if (msi_desc->msg.address_lo == 0x0 && msi_desc->msg.address_hi == 0x0)
75 if (WARN_ON(!owner_mc_dev))
78 irq_cfg.paddr = ((u64)msi_desc->msg.address_hi << 32) |
79 msi_desc->msg.address_lo;
80 irq_cfg.val = msi_desc->msg.data;
81 irq_cfg.irq_num = msi_desc->irq;
83 if (owner_mc_dev == mc_bus_dev) {
85 * IRQ is for the mc_bus_dev's DPRC itself
87 error = dprc_set_irq(mc_bus_dev->mc_io,
88 MC_CMD_FLAG_INTR_DIS | MC_CMD_FLAG_PRI,
89 mc_bus_dev->mc_handle,
90 mc_dev_irq->dev_irq_index,
93 dev_err(&owner_mc_dev->dev,
94 "dprc_set_irq() failed: %d\n", error);
98 * IRQ is for for a child device of mc_bus_dev
100 error = dprc_set_obj_irq(mc_bus_dev->mc_io,
101 MC_CMD_FLAG_INTR_DIS | MC_CMD_FLAG_PRI,
102 mc_bus_dev->mc_handle,
103 owner_mc_dev->obj_desc.type,
104 owner_mc_dev->obj_desc.id,
105 mc_dev_irq->dev_irq_index,
108 dev_err(&owner_mc_dev->dev,
109 "dprc_obj_set_irq() failed: %d\n", error);
115 * NOTE: This function is invoked with interrupts disabled
117 static void fsl_mc_msi_write_msg(struct irq_data *irq_data,
120 struct msi_desc *msi_desc = irq_data_get_msi_desc(irq_data);
121 struct fsl_mc_device *mc_bus_dev = to_fsl_mc_device(msi_desc->dev);
122 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
123 struct fsl_mc_device_irq *mc_dev_irq =
124 &mc_bus->irq_resources[msi_desc->fsl_mc.msi_index];
126 WARN_ON(mc_dev_irq->msi_desc != msi_desc);
127 msi_desc->msg = *msg;
130 * Program the MSI (paddr, value) pair in the device:
132 __fsl_mc_msi_write_msg(mc_bus_dev, mc_dev_irq);
135 static void fsl_mc_msi_update_chip_ops(struct msi_domain_info *info)
137 struct irq_chip *chip = info->chip;
139 if (WARN_ON((!chip)))
143 * irq_write_msi_msg should not be set by the caller
145 if (!chip->irq_write_msi_msg)
146 chip->irq_write_msi_msg = fsl_mc_msi_write_msg;
150 * fsl_mc_msi_create_irq_domain - Create a fsl-mc MSI interrupt domain
151 * @np: Optional device-tree node of the interrupt controller
152 * @info: MSI domain info
153 * @parent: Parent irq domain
155 * Updates the domain and chip ops and creates a fsl-mc MSI
159 * A domain pointer or NULL in case of failure.
161 struct irq_domain *fsl_mc_msi_create_irq_domain(struct fwnode_handle *fwnode,
162 struct msi_domain_info *info,
163 struct irq_domain *parent)
165 struct irq_domain *domain;
167 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
168 fsl_mc_msi_update_dom_ops(info);
169 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
170 fsl_mc_msi_update_chip_ops(info);
172 domain = msi_create_irq_domain(fwnode, info, parent);
174 domain->bus_token = DOMAIN_BUS_FSL_MC_MSI;
179 int fsl_mc_find_msi_domain(struct device *mc_platform_dev,
180 struct irq_domain **mc_msi_domain)
182 struct irq_domain *msi_domain;
183 struct device_node *mc_of_node = mc_platform_dev->of_node;
185 msi_domain = of_msi_get_domain(mc_platform_dev, mc_of_node,
186 DOMAIN_BUS_FSL_MC_MSI);
188 pr_err("Unable to find fsl-mc MSI domain for %s\n",
189 mc_of_node->full_name);
194 *mc_msi_domain = msi_domain;
198 static void fsl_mc_msi_free_descs(struct device *dev)
200 struct msi_desc *desc, *tmp;
202 list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) {
203 list_del(&desc->list);
204 free_msi_entry(desc);
208 static int fsl_mc_msi_alloc_descs(struct device *dev, unsigned int irq_count)
213 struct msi_desc *msi_desc;
215 for (i = 0; i < irq_count; i++) {
216 msi_desc = alloc_msi_entry(dev, 1, NULL);
218 dev_err(dev, "Failed to allocate msi entry\n");
220 goto cleanup_msi_descs;
223 msi_desc->fsl_mc.msi_index = i;
224 INIT_LIST_HEAD(&msi_desc->list);
225 list_add_tail(&msi_desc->list, dev_to_msi_list(dev));
231 fsl_mc_msi_free_descs(dev);
235 int fsl_mc_msi_domain_alloc_irqs(struct device *dev,
236 unsigned int irq_count)
238 struct irq_domain *msi_domain;
241 if (WARN_ON(!list_empty(dev_to_msi_list(dev))))
244 error = fsl_mc_msi_alloc_descs(dev, irq_count);
248 msi_domain = dev_get_msi_domain(dev);
249 if (WARN_ON(!msi_domain)) {
251 goto cleanup_msi_descs;
255 * NOTE: Calling this function will trigger the invocation of the
256 * its_fsl_mc_msi_prepare() callback
258 error = msi_domain_alloc_irqs(msi_domain, dev, irq_count);
261 dev_err(dev, "Failed to allocate IRQs\n");
262 goto cleanup_msi_descs;
268 fsl_mc_msi_free_descs(dev);
272 void fsl_mc_msi_domain_free_irqs(struct device *dev)
274 struct irq_domain *msi_domain;
276 msi_domain = dev_get_msi_domain(dev);
277 if (WARN_ON(!msi_domain))
280 msi_domain_free_irqs(msi_domain, dev);
282 if (WARN_ON(list_empty(dev_to_msi_list(dev))))
285 fsl_mc_msi_free_descs(dev);