1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Freescale Management Complex (MC) bus driver MSI support
4  *
5  * Copyright (C) 2015-2016 Freescale Semiconductor, Inc.
6  * Author: German Rivera <German.Rivera@freescale.com>
7  *
8  */
9 
10 #include <linux/of_device.h>
11 #include <linux/of_address.h>
12 #include <linux/of_irq.h>
13 #include <linux/irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/msi.h>
16 #include <linux/acpi_iort.h>
17 
18 #include "fsl-mc-private.h"
19 
20 #ifdef GENERIC_MSI_DOMAIN_OPS
21 /*
22  * Generate a unique ID identifying the interrupt (only used within the MSI
23  * irqdomain.  Combine the icid with the interrupt index.
24  */
fsl_mc_domain_calc_hwirq(struct fsl_mc_device * dev,struct msi_desc * desc)25 static irq_hw_number_t fsl_mc_domain_calc_hwirq(struct fsl_mc_device *dev,
26 						struct msi_desc *desc)
27 {
28 	/*
29 	 * Make the base hwirq value for ICID*10000 so it is readable
30 	 * as a decimal value in /proc/interrupts.
31 	 */
32 	return (irq_hw_number_t)(desc->fsl_mc.msi_index + (dev->icid * 10000));
33 }
34 
fsl_mc_msi_set_desc(msi_alloc_info_t * arg,struct msi_desc * desc)35 static void fsl_mc_msi_set_desc(msi_alloc_info_t *arg,
36 				struct msi_desc *desc)
37 {
38 	arg->desc = desc;
39 	arg->hwirq = fsl_mc_domain_calc_hwirq(to_fsl_mc_device(desc->dev),
40 					      desc);
41 }
42 #else
43 #define fsl_mc_msi_set_desc NULL
44 #endif
45 
fsl_mc_msi_update_dom_ops(struct msi_domain_info * info)46 static void fsl_mc_msi_update_dom_ops(struct msi_domain_info *info)
47 {
48 	struct msi_domain_ops *ops = info->ops;
49 
50 	if (!ops)
51 		return;
52 
53 	/*
54 	 * set_desc should not be set by the caller
55 	 */
56 	if (!ops->set_desc)
57 		ops->set_desc = fsl_mc_msi_set_desc;
58 }
59 
__fsl_mc_msi_write_msg(struct fsl_mc_device * mc_bus_dev,struct fsl_mc_device_irq * mc_dev_irq)60 static void __fsl_mc_msi_write_msg(struct fsl_mc_device *mc_bus_dev,
61 				   struct fsl_mc_device_irq *mc_dev_irq)
62 {
63 	int error;
64 	struct fsl_mc_device *owner_mc_dev = mc_dev_irq->mc_dev;
65 	struct msi_desc *msi_desc = mc_dev_irq->msi_desc;
66 	struct dprc_irq_cfg irq_cfg;
67 
68 	/*
69 	 * msi_desc->msg.address is 0x0 when this function is invoked in
70 	 * the free_irq() code path. In this case, for the MC, we don't
71 	 * really need to "unprogram" the MSI, so we just return.
72 	 */
73 	if (msi_desc->msg.address_lo == 0x0 && msi_desc->msg.address_hi == 0x0)
74 		return;
75 
76 	if (!owner_mc_dev)
77 		return;
78 
79 	irq_cfg.paddr = ((u64)msi_desc->msg.address_hi << 32) |
80 			msi_desc->msg.address_lo;
81 	irq_cfg.val = msi_desc->msg.data;
82 	irq_cfg.irq_num = msi_desc->irq;
83 
84 	if (owner_mc_dev == mc_bus_dev) {
85 		/*
86 		 * IRQ is for the mc_bus_dev's DPRC itself
87 		 */
88 		error = dprc_set_irq(mc_bus_dev->mc_io,
89 				     MC_CMD_FLAG_INTR_DIS | MC_CMD_FLAG_PRI,
90 				     mc_bus_dev->mc_handle,
91 				     mc_dev_irq->dev_irq_index,
92 				     &irq_cfg);
93 		if (error < 0) {
94 			dev_err(&owner_mc_dev->dev,
95 				"dprc_set_irq() failed: %d\n", error);
96 		}
97 	} else {
98 		/*
99 		 * IRQ is for for a child device of mc_bus_dev
100 		 */
101 		error = dprc_set_obj_irq(mc_bus_dev->mc_io,
102 					 MC_CMD_FLAG_INTR_DIS | MC_CMD_FLAG_PRI,
103 					 mc_bus_dev->mc_handle,
104 					 owner_mc_dev->obj_desc.type,
105 					 owner_mc_dev->obj_desc.id,
106 					 mc_dev_irq->dev_irq_index,
107 					 &irq_cfg);
108 		if (error < 0) {
109 			dev_err(&owner_mc_dev->dev,
110 				"dprc_obj_set_irq() failed: %d\n", error);
111 		}
112 	}
113 }
114 
115 /*
116  * NOTE: This function is invoked with interrupts disabled
117  */
fsl_mc_msi_write_msg(struct irq_data * irq_data,struct msi_msg * msg)118 static void fsl_mc_msi_write_msg(struct irq_data *irq_data,
119 				 struct msi_msg *msg)
120 {
121 	struct msi_desc *msi_desc = irq_data_get_msi_desc(irq_data);
122 	struct fsl_mc_device *mc_bus_dev = to_fsl_mc_device(msi_desc->dev);
123 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
124 	struct fsl_mc_device_irq *mc_dev_irq =
125 		&mc_bus->irq_resources[msi_desc->fsl_mc.msi_index];
126 
127 	msi_desc->msg = *msg;
128 
129 	/*
130 	 * Program the MSI (paddr, value) pair in the device:
131 	 */
132 	__fsl_mc_msi_write_msg(mc_bus_dev, mc_dev_irq);
133 }
134 
fsl_mc_msi_update_chip_ops(struct msi_domain_info * info)135 static void fsl_mc_msi_update_chip_ops(struct msi_domain_info *info)
136 {
137 	struct irq_chip *chip = info->chip;
138 
139 	if (!chip)
140 		return;
141 
142 	/*
143 	 * irq_write_msi_msg should not be set by the caller
144 	 */
145 	if (!chip->irq_write_msi_msg)
146 		chip->irq_write_msi_msg = fsl_mc_msi_write_msg;
147 }
148 
149 /**
150  * fsl_mc_msi_create_irq_domain - Create a fsl-mc MSI interrupt domain
151  * @fwnode:	Optional firmware node of the interrupt controller
152  * @info:	MSI domain info
153  * @parent:	Parent irq domain
154  *
155  * Updates the domain and chip ops and creates a fsl-mc MSI
156  * interrupt domain.
157  *
158  * Returns:
159  * A domain pointer or NULL in case of failure.
160  */
fsl_mc_msi_create_irq_domain(struct fwnode_handle * fwnode,struct msi_domain_info * info,struct irq_domain * parent)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)
164 {
165 	struct irq_domain *domain;
166 
167 	if (WARN_ON((info->flags & MSI_FLAG_LEVEL_CAPABLE)))
168 		info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
169 	if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
170 		fsl_mc_msi_update_dom_ops(info);
171 	if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
172 		fsl_mc_msi_update_chip_ops(info);
173 
174 	domain = msi_create_irq_domain(fwnode, info, parent);
175 	if (domain)
176 		irq_domain_update_bus_token(domain, DOMAIN_BUS_FSL_MC_MSI);
177 
178 	return domain;
179 }
180 
fsl_mc_find_msi_domain(struct device * dev)181 struct irq_domain *fsl_mc_find_msi_domain(struct device *dev)
182 {
183 	struct device *root_dprc_dev;
184 	struct device *bus_dev;
185 	struct irq_domain *msi_domain;
186 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
187 
188 	fsl_mc_get_root_dprc(dev, &root_dprc_dev);
189 	bus_dev = root_dprc_dev->parent;
190 
191 	if (bus_dev->of_node) {
192 		msi_domain = of_msi_map_get_device_domain(dev,
193 						  mc_dev->icid,
194 						  DOMAIN_BUS_FSL_MC_MSI);
195 
196 		/*
197 		 * if the msi-map property is missing assume that all the
198 		 * child containers inherit the domain from the parent
199 		 */
200 		if (!msi_domain)
201 
202 			msi_domain = of_msi_get_domain(bus_dev,
203 						bus_dev->of_node,
204 						DOMAIN_BUS_FSL_MC_MSI);
205 	} else {
206 		msi_domain = iort_get_device_domain(dev, mc_dev->icid,
207 						    DOMAIN_BUS_FSL_MC_MSI);
208 	}
209 
210 	return msi_domain;
211 }
212 
fsl_mc_msi_free_descs(struct device * dev)213 static void fsl_mc_msi_free_descs(struct device *dev)
214 {
215 	struct msi_desc *desc, *tmp;
216 
217 	list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) {
218 		list_del(&desc->list);
219 		free_msi_entry(desc);
220 	}
221 }
222 
fsl_mc_msi_alloc_descs(struct device * dev,unsigned int irq_count)223 static int fsl_mc_msi_alloc_descs(struct device *dev, unsigned int irq_count)
224 
225 {
226 	unsigned int i;
227 	int error;
228 	struct msi_desc *msi_desc;
229 
230 	for (i = 0; i < irq_count; i++) {
231 		msi_desc = alloc_msi_entry(dev, 1, NULL);
232 		if (!msi_desc) {
233 			dev_err(dev, "Failed to allocate msi entry\n");
234 			error = -ENOMEM;
235 			goto cleanup_msi_descs;
236 		}
237 
238 		msi_desc->fsl_mc.msi_index = i;
239 		INIT_LIST_HEAD(&msi_desc->list);
240 		list_add_tail(&msi_desc->list, dev_to_msi_list(dev));
241 	}
242 
243 	return 0;
244 
245 cleanup_msi_descs:
246 	fsl_mc_msi_free_descs(dev);
247 	return error;
248 }
249 
fsl_mc_msi_domain_alloc_irqs(struct device * dev,unsigned int irq_count)250 int fsl_mc_msi_domain_alloc_irqs(struct device *dev,
251 				 unsigned int irq_count)
252 {
253 	struct irq_domain *msi_domain;
254 	int error;
255 
256 	if (!list_empty(dev_to_msi_list(dev)))
257 		return -EINVAL;
258 
259 	error = fsl_mc_msi_alloc_descs(dev, irq_count);
260 	if (error < 0)
261 		return error;
262 
263 	msi_domain = dev_get_msi_domain(dev);
264 	if (!msi_domain) {
265 		error = -EINVAL;
266 		goto cleanup_msi_descs;
267 	}
268 
269 	/*
270 	 * NOTE: Calling this function will trigger the invocation of the
271 	 * its_fsl_mc_msi_prepare() callback
272 	 */
273 	error = msi_domain_alloc_irqs(msi_domain, dev, irq_count);
274 
275 	if (error) {
276 		dev_err(dev, "Failed to allocate IRQs\n");
277 		goto cleanup_msi_descs;
278 	}
279 
280 	return 0;
281 
282 cleanup_msi_descs:
283 	fsl_mc_msi_free_descs(dev);
284 	return error;
285 }
286 
fsl_mc_msi_domain_free_irqs(struct device * dev)287 void fsl_mc_msi_domain_free_irqs(struct device *dev)
288 {
289 	struct irq_domain *msi_domain;
290 
291 	msi_domain = dev_get_msi_domain(dev);
292 	if (!msi_domain)
293 		return;
294 
295 	msi_domain_free_irqs(msi_domain, dev);
296 
297 	if (list_empty(dev_to_msi_list(dev)))
298 		return;
299 
300 	fsl_mc_msi_free_descs(dev);
301 }
302