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