Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * MSI framework for platform devices
4 *
5 * Copyright (C) 2015 ARM Limited, All Rights Reserved.
6 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 */
8
9#include <linux/device.h>
10#include <linux/idr.h>
11#include <linux/irq.h>
12#include <linux/irqdomain.h>
13#include <linux/msi.h>
14#include <linux/slab.h>
15
16#define DEV_ID_SHIFT 21
17#define MAX_DEV_MSIS (1 << (32 - DEV_ID_SHIFT))
18
19/*
20 * Internal data structure containing a (made up, but unique) devid
21 * and the callback to write the MSI message.
22 */
23struct platform_msi_priv_data {
24 struct device *dev;
25 void *host_data;
26 msi_alloc_info_t arg;
27 irq_write_msi_msg_t write_msg;
28 int devid;
29};
30
31/* The devid allocator */
32static DEFINE_IDA(platform_msi_devid_ida);
33
34#ifdef GENERIC_MSI_DOMAIN_OPS
35/*
36 * Convert an msi_desc to a globaly unique identifier (per-device
37 * devid + msi_desc position in the msi_list).
38 */
39static irq_hw_number_t platform_msi_calc_hwirq(struct msi_desc *desc)
40{
41 u32 devid = desc->dev->msi.data->platform_data->devid;
42
43 return (devid << (32 - DEV_ID_SHIFT)) | desc->msi_index;
44}
45
46static void platform_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
47{
48 arg->desc = desc;
49 arg->hwirq = platform_msi_calc_hwirq(desc);
50}
51
52static int platform_msi_init(struct irq_domain *domain,
53 struct msi_domain_info *info,
54 unsigned int virq, irq_hw_number_t hwirq,
55 msi_alloc_info_t *arg)
56{
57 return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
58 info->chip, info->chip_data);
59}
60
61static void platform_msi_set_proxy_dev(msi_alloc_info_t *arg)
62{
63 arg->flags |= MSI_ALLOC_FLAGS_PROXY_DEVICE;
64}
65#else
66#define platform_msi_set_desc NULL
67#define platform_msi_init NULL
68#define platform_msi_set_proxy_dev(x) do {} while(0)
69#endif
70
71static void platform_msi_update_dom_ops(struct msi_domain_info *info)
72{
73 struct msi_domain_ops *ops = info->ops;
74
75 BUG_ON(!ops);
76
77 if (ops->msi_init == NULL)
78 ops->msi_init = platform_msi_init;
79 if (ops->set_desc == NULL)
80 ops->set_desc = platform_msi_set_desc;
81}
82
83static void platform_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
84{
85 struct msi_desc *desc = irq_data_get_msi_desc(data);
86
87 desc->dev->msi.data->platform_data->write_msg(desc, msg);
88}
89
90static void platform_msi_update_chip_ops(struct msi_domain_info *info)
91{
92 struct irq_chip *chip = info->chip;
93
94 BUG_ON(!chip);
95 if (!chip->irq_mask)
96 chip->irq_mask = irq_chip_mask_parent;
97 if (!chip->irq_unmask)
98 chip->irq_unmask = irq_chip_unmask_parent;
99 if (!chip->irq_eoi)
100 chip->irq_eoi = irq_chip_eoi_parent;
101 if (!chip->irq_set_affinity)
102 chip->irq_set_affinity = msi_domain_set_affinity;
103 if (!chip->irq_write_msi_msg)
104 chip->irq_write_msi_msg = platform_msi_write_msg;
105 if (WARN_ON((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
106 !(chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)))
107 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
108}
109
110/**
111 * platform_msi_create_irq_domain - Create a platform MSI interrupt domain
112 * @fwnode: Optional fwnode of the interrupt controller
113 * @info: MSI domain info
114 * @parent: Parent irq domain
115 *
116 * Updates the domain and chip ops and creates a platform MSI
117 * interrupt domain.
118 *
119 * Returns:
120 * A domain pointer or NULL in case of failure.
121 */
122struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
123 struct msi_domain_info *info,
124 struct irq_domain *parent)
125{
126 struct irq_domain *domain;
127
128 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
129 platform_msi_update_dom_ops(info);
130 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
131 platform_msi_update_chip_ops(info);
132 info->flags |= MSI_FLAG_DEV_SYSFS | MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS |
133 MSI_FLAG_FREE_MSI_DESCS;
134
135 domain = msi_create_irq_domain(fwnode, info, parent);
136 if (domain)
137 irq_domain_update_bus_token(domain, DOMAIN_BUS_PLATFORM_MSI);
138
139 return domain;
140}
141EXPORT_SYMBOL_GPL(platform_msi_create_irq_domain);
142
143static int platform_msi_alloc_priv_data(struct device *dev, unsigned int nvec,
144 irq_write_msi_msg_t write_msi_msg)
145{
146 struct platform_msi_priv_data *datap;
147 int err;
148
149 /*
150 * Limit the number of interrupts to 2048 per device. Should we
151 * need to bump this up, DEV_ID_SHIFT should be adjusted
152 * accordingly (which would impact the max number of MSI
153 * capable devices).
154 */
155 if (!dev->msi.domain || !write_msi_msg || !nvec || nvec > MAX_DEV_MSIS)
156 return -EINVAL;
157
158 if (dev->msi.domain->bus_token != DOMAIN_BUS_PLATFORM_MSI) {
159 dev_err(dev, "Incompatible msi_domain, giving up\n");
160 return -EINVAL;
161 }
162
163 err = msi_setup_device_data(dev);
164 if (err)
165 return err;
166
167 /* Already initialized? */
168 if (dev->msi.data->platform_data)
169 return -EBUSY;
170
171 datap = kzalloc(sizeof(*datap), GFP_KERNEL);
172 if (!datap)
173 return -ENOMEM;
174
175 datap->devid = ida_simple_get(&platform_msi_devid_ida,
176 0, 1 << DEV_ID_SHIFT, GFP_KERNEL);
177 if (datap->devid < 0) {
178 err = datap->devid;
179 kfree(datap);
180 return err;
181 }
182
183 datap->write_msg = write_msi_msg;
184 datap->dev = dev;
185 dev->msi.data->platform_data = datap;
186 return 0;
187}
188
189static void platform_msi_free_priv_data(struct device *dev)
190{
191 struct platform_msi_priv_data *data = dev->msi.data->platform_data;
192
193 dev->msi.data->platform_data = NULL;
194 ida_simple_remove(&platform_msi_devid_ida, data->devid);
195 kfree(data);
196}
197
198/**
199 * platform_msi_domain_alloc_irqs - Allocate MSI interrupts for @dev
200 * @dev: The device for which to allocate interrupts
201 * @nvec: The number of interrupts to allocate
202 * @write_msi_msg: Callback to write an interrupt message for @dev
203 *
204 * Returns:
205 * Zero for success, or an error code in case of failure
206 */
207int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
208 irq_write_msi_msg_t write_msi_msg)
209{
210 int err;
211
212 err = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
213 if (err)
214 return err;
215
216 err = msi_domain_alloc_irqs_range(dev, MSI_DEFAULT_DOMAIN, 0, nvec - 1);
217 if (err)
218 platform_msi_free_priv_data(dev);
219
220 return err;
221}
222EXPORT_SYMBOL_GPL(platform_msi_domain_alloc_irqs);
223
224/**
225 * platform_msi_domain_free_irqs - Free MSI interrupts for @dev
226 * @dev: The device for which to free interrupts
227 */
228void platform_msi_domain_free_irqs(struct device *dev)
229{
230 msi_domain_free_irqs_all(dev, MSI_DEFAULT_DOMAIN);
231 platform_msi_free_priv_data(dev);
232}
233EXPORT_SYMBOL_GPL(platform_msi_domain_free_irqs);
234
235/**
236 * platform_msi_get_host_data - Query the private data associated with
237 * a platform-msi domain
238 * @domain: The platform-msi domain
239 *
240 * Return: The private data provided when calling
241 * platform_msi_create_device_domain().
242 */
243void *platform_msi_get_host_data(struct irq_domain *domain)
244{
245 struct platform_msi_priv_data *data = domain->host_data;
246
247 return data->host_data;
248}
249
250static struct lock_class_key platform_device_msi_lock_class;
251
252/**
253 * __platform_msi_create_device_domain - Create a platform-msi device domain
254 *
255 * @dev: The device generating the MSIs
256 * @nvec: The number of MSIs that need to be allocated
257 * @is_tree: flag to indicate tree hierarchy
258 * @write_msi_msg: Callback to write an interrupt message for @dev
259 * @ops: The hierarchy domain operations to use
260 * @host_data: Private data associated to this domain
261 *
262 * Return: An irqdomain for @nvec interrupts on success, NULL in case of error.
263 *
264 * This is for interrupt domains which stack on a platform-msi domain
265 * created by platform_msi_create_irq_domain(). @dev->msi.domain points to
266 * that platform-msi domain which is the parent for the new domain.
267 */
268struct irq_domain *
269__platform_msi_create_device_domain(struct device *dev,
270 unsigned int nvec,
271 bool is_tree,
272 irq_write_msi_msg_t write_msi_msg,
273 const struct irq_domain_ops *ops,
274 void *host_data)
275{
276 struct platform_msi_priv_data *data;
277 struct irq_domain *domain;
278 int err;
279
280 err = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
281 if (err)
282 return NULL;
283
284 /*
285 * Use a separate lock class for the MSI descriptor mutex on
286 * platform MSI device domains because the descriptor mutex nests
287 * into the domain mutex. See alloc/free below.
288 */
289 lockdep_set_class(&dev->msi.data->mutex, &platform_device_msi_lock_class);
290
291 data = dev->msi.data->platform_data;
292 data->host_data = host_data;
293 domain = irq_domain_create_hierarchy(dev->msi.domain, 0,
294 is_tree ? 0 : nvec,
295 dev->fwnode, ops, data);
296 if (!domain)
297 goto free_priv;
298
299 platform_msi_set_proxy_dev(&data->arg);
300 err = msi_domain_prepare_irqs(domain->parent, dev, nvec, &data->arg);
301 if (err)
302 goto free_domain;
303
304 return domain;
305
306free_domain:
307 irq_domain_remove(domain);
308free_priv:
309 platform_msi_free_priv_data(dev);
310 return NULL;
311}
312
313/**
314 * platform_msi_device_domain_free - Free interrupts associated with a platform-msi
315 * device domain
316 *
317 * @domain: The platform-msi device domain
318 * @virq: The base irq from which to perform the free operation
319 * @nr_irqs: How many interrupts to free from @virq
320 */
321void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq,
322 unsigned int nr_irqs)
323{
324 struct platform_msi_priv_data *data = domain->host_data;
325
326 msi_lock_descs(data->dev);
327 msi_domain_depopulate_descs(data->dev, virq, nr_irqs);
328 irq_domain_free_irqs_common(domain, virq, nr_irqs);
329 msi_free_msi_descs_range(data->dev, virq, virq + nr_irqs - 1);
330 msi_unlock_descs(data->dev);
331}
332
333/**
334 * platform_msi_device_domain_alloc - Allocate interrupts associated with
335 * a platform-msi device domain
336 *
337 * @domain: The platform-msi device domain
338 * @virq: The base irq from which to perform the allocate operation
339 * @nr_irqs: How many interrupts to allocate from @virq
340 *
341 * Return 0 on success, or an error code on failure. Must be called
342 * with irq_domain_mutex held (which can only be done as part of a
343 * top-level interrupt allocation).
344 */
345int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq,
346 unsigned int nr_irqs)
347{
348 struct platform_msi_priv_data *data = domain->host_data;
349 struct device *dev = data->dev;
350
351 return msi_domain_populate_irqs(domain->parent, dev, virq, nr_irqs, &data->arg);
352}
1/*
2 * MSI framework for platform devices
3 *
4 * Copyright (C) 2015 ARM Limited, All Rights Reserved.
5 * Author: Marc Zyngier <marc.zyngier@arm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/device.h>
21#include <linux/idr.h>
22#include <linux/irq.h>
23#include <linux/irqdomain.h>
24#include <linux/msi.h>
25#include <linux/slab.h>
26
27#define DEV_ID_SHIFT 21
28#define MAX_DEV_MSIS (1 << (32 - DEV_ID_SHIFT))
29
30/*
31 * Internal data structure containing a (made up, but unique) devid
32 * and the callback to write the MSI message.
33 */
34struct platform_msi_priv_data {
35 struct device *dev;
36 void *host_data;
37 msi_alloc_info_t arg;
38 irq_write_msi_msg_t write_msg;
39 int devid;
40};
41
42/* The devid allocator */
43static DEFINE_IDA(platform_msi_devid_ida);
44
45#ifdef GENERIC_MSI_DOMAIN_OPS
46/*
47 * Convert an msi_desc to a globaly unique identifier (per-device
48 * devid + msi_desc position in the msi_list).
49 */
50static irq_hw_number_t platform_msi_calc_hwirq(struct msi_desc *desc)
51{
52 u32 devid;
53
54 devid = desc->platform.msi_priv_data->devid;
55
56 return (devid << (32 - DEV_ID_SHIFT)) | desc->platform.msi_index;
57}
58
59static void platform_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
60{
61 arg->desc = desc;
62 arg->hwirq = platform_msi_calc_hwirq(desc);
63}
64
65static int platform_msi_init(struct irq_domain *domain,
66 struct msi_domain_info *info,
67 unsigned int virq, irq_hw_number_t hwirq,
68 msi_alloc_info_t *arg)
69{
70 return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
71 info->chip, info->chip_data);
72}
73#else
74#define platform_msi_set_desc NULL
75#define platform_msi_init NULL
76#endif
77
78static void platform_msi_update_dom_ops(struct msi_domain_info *info)
79{
80 struct msi_domain_ops *ops = info->ops;
81
82 BUG_ON(!ops);
83
84 if (ops->msi_init == NULL)
85 ops->msi_init = platform_msi_init;
86 if (ops->set_desc == NULL)
87 ops->set_desc = platform_msi_set_desc;
88}
89
90static void platform_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
91{
92 struct msi_desc *desc = irq_data_get_msi_desc(data);
93 struct platform_msi_priv_data *priv_data;
94
95 priv_data = desc->platform.msi_priv_data;
96
97 priv_data->write_msg(desc, msg);
98}
99
100static void platform_msi_update_chip_ops(struct msi_domain_info *info)
101{
102 struct irq_chip *chip = info->chip;
103
104 BUG_ON(!chip);
105 if (!chip->irq_mask)
106 chip->irq_mask = irq_chip_mask_parent;
107 if (!chip->irq_unmask)
108 chip->irq_unmask = irq_chip_unmask_parent;
109 if (!chip->irq_eoi)
110 chip->irq_eoi = irq_chip_eoi_parent;
111 if (!chip->irq_set_affinity)
112 chip->irq_set_affinity = msi_domain_set_affinity;
113 if (!chip->irq_write_msi_msg)
114 chip->irq_write_msi_msg = platform_msi_write_msg;
115}
116
117static void platform_msi_free_descs(struct device *dev, int base, int nvec)
118{
119 struct msi_desc *desc, *tmp;
120
121 list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) {
122 if (desc->platform.msi_index >= base &&
123 desc->platform.msi_index < (base + nvec)) {
124 list_del(&desc->list);
125 free_msi_entry(desc);
126 }
127 }
128}
129
130static int platform_msi_alloc_descs_with_irq(struct device *dev, int virq,
131 int nvec,
132 struct platform_msi_priv_data *data)
133
134{
135 struct msi_desc *desc;
136 int i, base = 0;
137
138 if (!list_empty(dev_to_msi_list(dev))) {
139 desc = list_last_entry(dev_to_msi_list(dev),
140 struct msi_desc, list);
141 base = desc->platform.msi_index + 1;
142 }
143
144 for (i = 0; i < nvec; i++) {
145 desc = alloc_msi_entry(dev);
146 if (!desc)
147 break;
148
149 desc->platform.msi_priv_data = data;
150 desc->platform.msi_index = base + i;
151 desc->nvec_used = 1;
152 desc->irq = virq ? virq + i : 0;
153
154 list_add_tail(&desc->list, dev_to_msi_list(dev));
155 }
156
157 if (i != nvec) {
158 /* Clean up the mess */
159 platform_msi_free_descs(dev, base, nvec);
160
161 return -ENOMEM;
162 }
163
164 return 0;
165}
166
167static int platform_msi_alloc_descs(struct device *dev, int nvec,
168 struct platform_msi_priv_data *data)
169
170{
171 return platform_msi_alloc_descs_with_irq(dev, 0, nvec, data);
172}
173
174/**
175 * platform_msi_create_irq_domain - Create a platform MSI interrupt domain
176 * @fwnode: Optional fwnode of the interrupt controller
177 * @info: MSI domain info
178 * @parent: Parent irq domain
179 *
180 * Updates the domain and chip ops and creates a platform MSI
181 * interrupt domain.
182 *
183 * Returns:
184 * A domain pointer or NULL in case of failure.
185 */
186struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
187 struct msi_domain_info *info,
188 struct irq_domain *parent)
189{
190 struct irq_domain *domain;
191
192 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
193 platform_msi_update_dom_ops(info);
194 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
195 platform_msi_update_chip_ops(info);
196
197 domain = msi_create_irq_domain(fwnode, info, parent);
198 if (domain)
199 domain->bus_token = DOMAIN_BUS_PLATFORM_MSI;
200
201 return domain;
202}
203
204static struct platform_msi_priv_data *
205platform_msi_alloc_priv_data(struct device *dev, unsigned int nvec,
206 irq_write_msi_msg_t write_msi_msg)
207{
208 struct platform_msi_priv_data *datap;
209 /*
210 * Limit the number of interrupts to 256 per device. Should we
211 * need to bump this up, DEV_ID_SHIFT should be adjusted
212 * accordingly (which would impact the max number of MSI
213 * capable devices).
214 */
215 if (!dev->msi_domain || !write_msi_msg || !nvec || nvec > MAX_DEV_MSIS)
216 return ERR_PTR(-EINVAL);
217
218 if (dev->msi_domain->bus_token != DOMAIN_BUS_PLATFORM_MSI) {
219 dev_err(dev, "Incompatible msi_domain, giving up\n");
220 return ERR_PTR(-EINVAL);
221 }
222
223 /* Already had a helping of MSI? Greed... */
224 if (!list_empty(dev_to_msi_list(dev)))
225 return ERR_PTR(-EBUSY);
226
227 datap = kzalloc(sizeof(*datap), GFP_KERNEL);
228 if (!datap)
229 return ERR_PTR(-ENOMEM);
230
231 datap->devid = ida_simple_get(&platform_msi_devid_ida,
232 0, 1 << DEV_ID_SHIFT, GFP_KERNEL);
233 if (datap->devid < 0) {
234 int err = datap->devid;
235 kfree(datap);
236 return ERR_PTR(err);
237 }
238
239 datap->write_msg = write_msi_msg;
240 datap->dev = dev;
241
242 return datap;
243}
244
245static void platform_msi_free_priv_data(struct platform_msi_priv_data *data)
246{
247 ida_simple_remove(&platform_msi_devid_ida, data->devid);
248 kfree(data);
249}
250
251/**
252 * platform_msi_domain_alloc_irqs - Allocate MSI interrupts for @dev
253 * @dev: The device for which to allocate interrupts
254 * @nvec: The number of interrupts to allocate
255 * @write_msi_msg: Callback to write an interrupt message for @dev
256 *
257 * Returns:
258 * Zero for success, or an error code in case of failure
259 */
260int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
261 irq_write_msi_msg_t write_msi_msg)
262{
263 struct platform_msi_priv_data *priv_data;
264 int err;
265
266 priv_data = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
267 if (IS_ERR(priv_data))
268 return PTR_ERR(priv_data);
269
270 err = platform_msi_alloc_descs(dev, nvec, priv_data);
271 if (err)
272 goto out_free_priv_data;
273
274 err = msi_domain_alloc_irqs(dev->msi_domain, dev, nvec);
275 if (err)
276 goto out_free_desc;
277
278 return 0;
279
280out_free_desc:
281 platform_msi_free_descs(dev, 0, nvec);
282out_free_priv_data:
283 platform_msi_free_priv_data(priv_data);
284
285 return err;
286}
287EXPORT_SYMBOL_GPL(platform_msi_domain_alloc_irqs);
288
289/**
290 * platform_msi_domain_free_irqs - Free MSI interrupts for @dev
291 * @dev: The device for which to free interrupts
292 */
293void platform_msi_domain_free_irqs(struct device *dev)
294{
295 if (!list_empty(dev_to_msi_list(dev))) {
296 struct msi_desc *desc;
297
298 desc = first_msi_entry(dev);
299 platform_msi_free_priv_data(desc->platform.msi_priv_data);
300 }
301
302 msi_domain_free_irqs(dev->msi_domain, dev);
303 platform_msi_free_descs(dev, 0, MAX_DEV_MSIS);
304}
305EXPORT_SYMBOL_GPL(platform_msi_domain_free_irqs);
306
307/**
308 * platform_msi_get_host_data - Query the private data associated with
309 * a platform-msi domain
310 * @domain: The platform-msi domain
311 *
312 * Returns the private data provided when calling
313 * platform_msi_create_device_domain.
314 */
315void *platform_msi_get_host_data(struct irq_domain *domain)
316{
317 struct platform_msi_priv_data *data = domain->host_data;
318 return data->host_data;
319}
320
321/**
322 * platform_msi_create_device_domain - Create a platform-msi domain
323 *
324 * @dev: The device generating the MSIs
325 * @nvec: The number of MSIs that need to be allocated
326 * @write_msi_msg: Callback to write an interrupt message for @dev
327 * @ops: The hierarchy domain operations to use
328 * @host_data: Private data associated to this domain
329 *
330 * Returns an irqdomain for @nvec interrupts
331 */
332struct irq_domain *
333platform_msi_create_device_domain(struct device *dev,
334 unsigned int nvec,
335 irq_write_msi_msg_t write_msi_msg,
336 const struct irq_domain_ops *ops,
337 void *host_data)
338{
339 struct platform_msi_priv_data *data;
340 struct irq_domain *domain;
341 int err;
342
343 data = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
344 if (IS_ERR(data))
345 return NULL;
346
347 data->host_data = host_data;
348 domain = irq_domain_create_hierarchy(dev->msi_domain, 0, nvec,
349 of_node_to_fwnode(dev->of_node),
350 ops, data);
351 if (!domain)
352 goto free_priv;
353
354 err = msi_domain_prepare_irqs(domain->parent, dev, nvec, &data->arg);
355 if (err)
356 goto free_domain;
357
358 return domain;
359
360free_domain:
361 irq_domain_remove(domain);
362free_priv:
363 platform_msi_free_priv_data(data);
364 return NULL;
365}
366
367/**
368 * platform_msi_domain_free - Free interrupts associated with a platform-msi
369 * domain
370 *
371 * @domain: The platform-msi domain
372 * @virq: The base irq from which to perform the free operation
373 * @nvec: How many interrupts to free from @virq
374 */
375void platform_msi_domain_free(struct irq_domain *domain, unsigned int virq,
376 unsigned int nvec)
377{
378 struct platform_msi_priv_data *data = domain->host_data;
379 struct msi_desc *desc;
380 for_each_msi_entry(desc, data->dev) {
381 if (WARN_ON(!desc->irq || desc->nvec_used != 1))
382 return;
383 if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
384 continue;
385
386 irq_domain_free_irqs_common(domain, desc->irq, 1);
387 }
388}
389
390/**
391 * platform_msi_domain_alloc - Allocate interrupts associated with
392 * a platform-msi domain
393 *
394 * @domain: The platform-msi domain
395 * @virq: The base irq from which to perform the allocate operation
396 * @nvec: How many interrupts to free from @virq
397 *
398 * Return 0 on success, or an error code on failure. Must be called
399 * with irq_domain_mutex held (which can only be done as part of a
400 * top-level interrupt allocation).
401 */
402int platform_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
403 unsigned int nr_irqs)
404{
405 struct platform_msi_priv_data *data = domain->host_data;
406 int err;
407
408 err = platform_msi_alloc_descs_with_irq(data->dev, virq, nr_irqs, data);
409 if (err)
410 return err;
411
412 err = msi_domain_populate_irqs(domain->parent, data->dev,
413 virq, nr_irqs, &data->arg);
414 if (err)
415 platform_msi_domain_free(domain, virq, nr_irqs);
416
417 return err;
418}