1 // SPDX-License-Identifier: GPL-2.0
3 * MSI framework for platform devices
5 * Copyright (C) 2015 ARM Limited, All Rights Reserved.
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>
16 /* Begin of removal area. Once everything is converted over. Cleanup the includes too! */
18 #define DEV_ID_SHIFT 21
19 #define MAX_DEV_MSIS (1 << (32 - DEV_ID_SHIFT))
22 * Internal data structure containing a (made up, but unique) devid
23 * and the callback to write the MSI message.
25 struct platform_msi_priv_data {
29 irq_write_msi_msg_t write_msg;
33 /* The devid allocator */
34 static DEFINE_IDA(platform_msi_devid_ida);
36 #ifdef GENERIC_MSI_DOMAIN_OPS
38 * Convert an msi_desc to a globaly unique identifier (per-device
39 * devid + msi_desc position in the msi_list).
41 static irq_hw_number_t platform_msi_calc_hwirq(struct msi_desc *desc)
43 u32 devid = desc->dev->msi.data->platform_data->devid;
45 return (devid << (32 - DEV_ID_SHIFT)) | desc->msi_index;
48 static void platform_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
51 arg->hwirq = platform_msi_calc_hwirq(desc);
54 static int platform_msi_init(struct irq_domain *domain,
55 struct msi_domain_info *info,
56 unsigned int virq, irq_hw_number_t hwirq,
57 msi_alloc_info_t *arg)
59 return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
60 info->chip, info->chip_data);
63 static void platform_msi_set_proxy_dev(msi_alloc_info_t *arg)
65 arg->flags |= MSI_ALLOC_FLAGS_PROXY_DEVICE;
68 #define platform_msi_set_desc NULL
69 #define platform_msi_init NULL
70 #define platform_msi_set_proxy_dev(x) do {} while(0)
73 static void platform_msi_update_dom_ops(struct msi_domain_info *info)
75 struct msi_domain_ops *ops = info->ops;
79 if (ops->msi_init == NULL)
80 ops->msi_init = platform_msi_init;
81 if (ops->set_desc == NULL)
82 ops->set_desc = platform_msi_set_desc;
85 static void platform_msi_write_msg(struct irq_data *data, struct msi_msg *msg)
87 struct msi_desc *desc = irq_data_get_msi_desc(data);
89 desc->dev->msi.data->platform_data->write_msg(desc, msg);
92 static void platform_msi_update_chip_ops(struct msi_domain_info *info)
94 struct irq_chip *chip = info->chip;
98 chip->irq_mask = irq_chip_mask_parent;
99 if (!chip->irq_unmask)
100 chip->irq_unmask = irq_chip_unmask_parent;
102 chip->irq_eoi = irq_chip_eoi_parent;
103 if (!chip->irq_set_affinity)
104 chip->irq_set_affinity = msi_domain_set_affinity;
105 if (!chip->irq_write_msi_msg)
106 chip->irq_write_msi_msg = platform_msi_write_msg;
107 if (WARN_ON((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
108 !(chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)))
109 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
113 * platform_msi_create_irq_domain - Create a platform MSI interrupt domain
114 * @fwnode: Optional fwnode of the interrupt controller
115 * @info: MSI domain info
116 * @parent: Parent irq domain
118 * Updates the domain and chip ops and creates a platform MSI
122 * A domain pointer or NULL in case of failure.
124 struct irq_domain *platform_msi_create_irq_domain(struct fwnode_handle *fwnode,
125 struct msi_domain_info *info,
126 struct irq_domain *parent)
128 struct irq_domain *domain;
130 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
131 platform_msi_update_dom_ops(info);
132 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
133 platform_msi_update_chip_ops(info);
134 info->flags |= MSI_FLAG_DEV_SYSFS | MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS |
135 MSI_FLAG_FREE_MSI_DESCS;
137 domain = msi_create_irq_domain(fwnode, info, parent);
139 irq_domain_update_bus_token(domain, DOMAIN_BUS_PLATFORM_MSI);
143 EXPORT_SYMBOL_GPL(platform_msi_create_irq_domain);
145 static int platform_msi_alloc_priv_data(struct device *dev, unsigned int nvec,
146 irq_write_msi_msg_t write_msi_msg)
148 struct platform_msi_priv_data *datap;
152 * Limit the number of interrupts to 2048 per device. Should we
153 * need to bump this up, DEV_ID_SHIFT should be adjusted
154 * accordingly (which would impact the max number of MSI
157 if (!dev->msi.domain || !write_msi_msg || !nvec || nvec > MAX_DEV_MSIS)
160 if (dev->msi.domain->bus_token != DOMAIN_BUS_PLATFORM_MSI) {
161 dev_err(dev, "Incompatible msi_domain, giving up\n");
165 err = msi_setup_device_data(dev);
169 /* Already initialized? */
170 if (dev->msi.data->platform_data)
173 datap = kzalloc(sizeof(*datap), GFP_KERNEL);
177 datap->devid = ida_alloc_max(&platform_msi_devid_ida,
178 (1 << DEV_ID_SHIFT) - 1, GFP_KERNEL);
179 if (datap->devid < 0) {
185 datap->write_msg = write_msi_msg;
187 dev->msi.data->platform_data = datap;
191 static void platform_msi_free_priv_data(struct device *dev)
193 struct platform_msi_priv_data *data = dev->msi.data->platform_data;
195 dev->msi.data->platform_data = NULL;
196 ida_free(&platform_msi_devid_ida, data->devid);
201 * platform_msi_domain_alloc_irqs - Allocate MSI interrupts for @dev
202 * @dev: The device for which to allocate interrupts
203 * @nvec: The number of interrupts to allocate
204 * @write_msi_msg: Callback to write an interrupt message for @dev
207 * Zero for success, or an error code in case of failure
209 static int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec,
210 irq_write_msi_msg_t write_msi_msg)
214 err = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
218 err = msi_domain_alloc_irqs_range(dev, MSI_DEFAULT_DOMAIN, 0, nvec - 1);
220 platform_msi_free_priv_data(dev);
226 * platform_msi_get_host_data - Query the private data associated with
227 * a platform-msi domain
228 * @domain: The platform-msi domain
230 * Return: The private data provided when calling
231 * platform_msi_create_device_domain().
233 void *platform_msi_get_host_data(struct irq_domain *domain)
235 struct platform_msi_priv_data *data = domain->host_data;
237 return data->host_data;
240 static struct lock_class_key platform_device_msi_lock_class;
243 * __platform_msi_create_device_domain - Create a platform-msi device domain
245 * @dev: The device generating the MSIs
246 * @nvec: The number of MSIs that need to be allocated
247 * @is_tree: flag to indicate tree hierarchy
248 * @write_msi_msg: Callback to write an interrupt message for @dev
249 * @ops: The hierarchy domain operations to use
250 * @host_data: Private data associated to this domain
252 * Return: An irqdomain for @nvec interrupts on success, NULL in case of error.
254 * This is for interrupt domains which stack on a platform-msi domain
255 * created by platform_msi_create_irq_domain(). @dev->msi.domain points to
256 * that platform-msi domain which is the parent for the new domain.
259 __platform_msi_create_device_domain(struct device *dev,
262 irq_write_msi_msg_t write_msi_msg,
263 const struct irq_domain_ops *ops,
266 struct platform_msi_priv_data *data;
267 struct irq_domain *domain;
270 err = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg);
275 * Use a separate lock class for the MSI descriptor mutex on
276 * platform MSI device domains because the descriptor mutex nests
277 * into the domain mutex. See alloc/free below.
279 lockdep_set_class(&dev->msi.data->mutex, &platform_device_msi_lock_class);
281 data = dev->msi.data->platform_data;
282 data->host_data = host_data;
283 domain = irq_domain_create_hierarchy(dev->msi.domain, 0,
285 dev->fwnode, ops, data);
289 platform_msi_set_proxy_dev(&data->arg);
290 err = msi_domain_prepare_irqs(domain->parent, dev, nvec, &data->arg);
297 irq_domain_remove(domain);
299 platform_msi_free_priv_data(dev);
304 * platform_msi_device_domain_free - Free interrupts associated with a platform-msi
307 * @domain: The platform-msi device domain
308 * @virq: The base irq from which to perform the free operation
309 * @nr_irqs: How many interrupts to free from @virq
311 void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq,
312 unsigned int nr_irqs)
314 struct platform_msi_priv_data *data = domain->host_data;
316 msi_lock_descs(data->dev);
317 msi_domain_depopulate_descs(data->dev, virq, nr_irqs);
318 irq_domain_free_irqs_common(domain, virq, nr_irqs);
319 msi_free_msi_descs_range(data->dev, virq, virq + nr_irqs - 1);
320 msi_unlock_descs(data->dev);
324 * platform_msi_device_domain_alloc - Allocate interrupts associated with
325 * a platform-msi device domain
327 * @domain: The platform-msi device domain
328 * @virq: The base irq from which to perform the allocate operation
329 * @nr_irqs: How many interrupts to allocate from @virq
331 * Return 0 on success, or an error code on failure. Must be called
332 * with irq_domain_mutex held (which can only be done as part of a
333 * top-level interrupt allocation).
335 int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq,
336 unsigned int nr_irqs)
338 struct platform_msi_priv_data *data = domain->host_data;
339 struct device *dev = data->dev;
341 return msi_domain_populate_irqs(domain->parent, dev, virq, nr_irqs, &data->arg);
344 /* End of removal area */
346 /* Real per device domain interfaces */
349 * This indirection can go when platform_device_msi_init_and_alloc_irqs()
350 * is switched to a proper irq_chip::irq_write_msi_msg() callback. Keep it
353 static void platform_msi_write_msi_msg(struct irq_data *d, struct msi_msg *msg)
355 irq_write_msi_msg_t cb = d->chip_data;
357 cb(irq_data_get_msi_desc(d), msg);
360 static void platform_msi_set_desc_byindex(msi_alloc_info_t *arg, struct msi_desc *desc)
363 arg->hwirq = desc->msi_index;
366 static const struct msi_domain_template platform_msi_template = {
369 .irq_mask = irq_chip_mask_parent,
370 .irq_unmask = irq_chip_unmask_parent,
371 .irq_write_msi_msg = platform_msi_write_msi_msg,
372 /* The rest is filled in by the platform MSI parent */
376 .set_desc = platform_msi_set_desc_byindex,
380 .bus_token = DOMAIN_BUS_DEVICE_MSI,
385 * platform_device_msi_init_and_alloc_irqs - Initialize platform device MSI
386 * and allocate interrupts for @dev
387 * @dev: The device for which to allocate interrupts
388 * @nvec: The number of interrupts to allocate
389 * @write_msi_msg: Callback to write an interrupt message for @dev
392 * Zero for success, or an error code in case of failure
394 * This creates a MSI domain on @dev which has @dev->msi.domain as
395 * parent. The parent domain sets up the new domain. The domain has
396 * a fixed size of @nvec. The domain is managed by devres and will
397 * be removed when the device is removed.
399 * Note: For migration purposes this falls back to the original platform_msi code
400 * up to the point where all platforms have been converted to the MSI
403 int platform_device_msi_init_and_alloc_irqs(struct device *dev, unsigned int nvec,
404 irq_write_msi_msg_t write_msi_msg)
406 struct irq_domain *domain = dev->msi.domain;
408 if (!domain || !write_msi_msg)
411 /* Migration support. Will go away once everything is converted */
412 if (!irq_domain_is_msi_parent(domain))
413 return platform_msi_domain_alloc_irqs(dev, nvec, write_msi_msg);
416 * @write_msi_msg is stored in the resulting msi_domain_info::data.
417 * The underlying domain creation mechanism will assign that
418 * callback to the resulting irq chip.
420 if (!msi_create_device_irq_domain(dev, MSI_DEFAULT_DOMAIN,
421 &platform_msi_template,
422 nvec, NULL, write_msi_msg))
425 return msi_domain_alloc_irqs_range(dev, MSI_DEFAULT_DOMAIN, 0, nvec - 1);
427 EXPORT_SYMBOL_GPL(platform_device_msi_init_and_alloc_irqs);
430 * platform_device_msi_free_irqs_all - Free all interrupts for @dev
431 * @dev: The device for which to free interrupts
433 void platform_device_msi_free_irqs_all(struct device *dev)
435 struct irq_domain *domain = dev->msi.domain;
437 msi_domain_free_irqs_all(dev, MSI_DEFAULT_DOMAIN);
439 /* Migration support. Will go away once everything is converted */
440 if (!irq_domain_is_msi_parent(domain))
441 platform_msi_free_priv_data(dev);
443 EXPORT_SYMBOL_GPL(platform_device_msi_free_irqs_all);