genirq/msi: Provide domain flags to allocate/free MSI descriptors automatically
[linux-block.git] / kernel / irq / msi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2014 Intel Corp.
4  * Author: Jiang Liu <jiang.liu@linux.intel.com>
5  *
6  * This file is licensed under GPLv2.
7  *
8  * This file contains common code to support Message Signaled Interrupts for
9  * PCI compatible and non PCI compatible devices.
10  */
11 #include <linux/types.h>
12 #include <linux/device.h>
13 #include <linux/irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/msi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/pci.h>
19
20 #include "internals.h"
21
22 /**
23  * alloc_msi_entry - Allocate an initialized msi_desc
24  * @dev:        Pointer to the device for which this is allocated
25  * @nvec:       The number of vectors used in this entry
26  * @affinity:   Optional pointer to an affinity mask array size of @nvec
27  *
28  * If @affinity is not %NULL then an affinity array[@nvec] is allocated
29  * and the affinity masks and flags from @affinity are copied.
30  *
31  * Return: pointer to allocated &msi_desc on success or %NULL on failure
32  */
33 struct msi_desc *alloc_msi_entry(struct device *dev, int nvec,
34                                  const struct irq_affinity_desc *affinity)
35 {
36         struct msi_desc *desc;
37
38         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
39         if (!desc)
40                 return NULL;
41
42         INIT_LIST_HEAD(&desc->list);
43         desc->dev = dev;
44         desc->nvec_used = nvec;
45         if (affinity) {
46                 desc->affinity = kmemdup(affinity,
47                         nvec * sizeof(*desc->affinity), GFP_KERNEL);
48                 if (!desc->affinity) {
49                         kfree(desc);
50                         return NULL;
51                 }
52         }
53
54         return desc;
55 }
56
57 void free_msi_entry(struct msi_desc *entry)
58 {
59         kfree(entry->affinity);
60         kfree(entry);
61 }
62
63 /**
64  * msi_add_msi_desc - Allocate and initialize a MSI descriptor
65  * @dev:        Pointer to the device for which the descriptor is allocated
66  * @init_desc:  Pointer to an MSI descriptor to initialize the new descriptor
67  *
68  * Return: 0 on success or an appropriate failure code.
69  */
70 int msi_add_msi_desc(struct device *dev, struct msi_desc *init_desc)
71 {
72         struct msi_desc *desc;
73
74         lockdep_assert_held(&dev->msi.data->mutex);
75
76         desc = alloc_msi_entry(dev, init_desc->nvec_used, init_desc->affinity);
77         if (!desc)
78                 return -ENOMEM;
79
80         /* Copy the MSI index and type specific data to the new descriptor. */
81         desc->msi_index = init_desc->msi_index;
82         desc->pci = init_desc->pci;
83
84         list_add_tail(&desc->list, &dev->msi.data->list);
85         return 0;
86 }
87
88 /**
89  * msi_add_simple_msi_descs - Allocate and initialize MSI descriptors
90  * @dev:        Pointer to the device for which the descriptors are allocated
91  * @index:      Index for the first MSI descriptor
92  * @ndesc:      Number of descriptors to allocate
93  *
94  * Return: 0 on success or an appropriate failure code.
95  */
96 static int msi_add_simple_msi_descs(struct device *dev, unsigned int index, unsigned int ndesc)
97 {
98         struct msi_desc *desc, *tmp;
99         LIST_HEAD(list);
100         unsigned int i;
101
102         lockdep_assert_held(&dev->msi.data->mutex);
103
104         for (i = 0; i < ndesc; i++) {
105                 desc = alloc_msi_entry(dev, 1, NULL);
106                 if (!desc)
107                         goto fail;
108                 desc->msi_index = index + i;
109                 list_add_tail(&desc->list, &list);
110         }
111         list_splice_tail(&list, &dev->msi.data->list);
112         return 0;
113
114 fail:
115         list_for_each_entry_safe(desc, tmp, &list, list) {
116                 list_del(&desc->list);
117                 free_msi_entry(desc);
118         }
119         return -ENOMEM;
120 }
121
122 /**
123  * msi_free_msi_descs_range - Free MSI descriptors of a device
124  * @dev:                Device to free the descriptors
125  * @filter:             Descriptor state filter
126  * @first_index:        Index to start freeing from
127  * @last_index:         Last index to be freed
128  */
129 void msi_free_msi_descs_range(struct device *dev, enum msi_desc_filter filter,
130                               unsigned int first_index, unsigned int last_index)
131 {
132         struct msi_desc *desc;
133
134         lockdep_assert_held(&dev->msi.data->mutex);
135
136         msi_for_each_desc(desc, dev, filter) {
137                 /*
138                  * Stupid for now to handle MSI device domain until the
139                  * storage is switched over to an xarray.
140                  */
141                 if (desc->msi_index < first_index || desc->msi_index > last_index)
142                         continue;
143                 list_del(&desc->list);
144                 free_msi_entry(desc);
145         }
146 }
147
148 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
149 {
150         *msg = entry->msg;
151 }
152
153 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
154 {
155         struct msi_desc *entry = irq_get_msi_desc(irq);
156
157         __get_cached_msi_msg(entry, msg);
158 }
159 EXPORT_SYMBOL_GPL(get_cached_msi_msg);
160
161 static void msi_device_data_release(struct device *dev, void *res)
162 {
163         struct msi_device_data *md = res;
164
165         WARN_ON_ONCE(!list_empty(&md->list));
166         dev->msi.data = NULL;
167 }
168
169 /**
170  * msi_setup_device_data - Setup MSI device data
171  * @dev:        Device for which MSI device data should be set up
172  *
173  * Return: 0 on success, appropriate error code otherwise
174  *
175  * This can be called more than once for @dev. If the MSI device data is
176  * already allocated the call succeeds. The allocated memory is
177  * automatically released when the device is destroyed.
178  */
179 int msi_setup_device_data(struct device *dev)
180 {
181         struct msi_device_data *md;
182
183         if (dev->msi.data)
184                 return 0;
185
186         md = devres_alloc(msi_device_data_release, sizeof(*md), GFP_KERNEL);
187         if (!md)
188                 return -ENOMEM;
189
190         INIT_LIST_HEAD(&md->list);
191         mutex_init(&md->mutex);
192         dev->msi.data = md;
193         devres_add(dev, md);
194         return 0;
195 }
196
197 /**
198  * msi_lock_descs - Lock the MSI descriptor storage of a device
199  * @dev:        Device to operate on
200  */
201 void msi_lock_descs(struct device *dev)
202 {
203         mutex_lock(&dev->msi.data->mutex);
204 }
205 EXPORT_SYMBOL_GPL(msi_lock_descs);
206
207 /**
208  * msi_unlock_descs - Unlock the MSI descriptor storage of a device
209  * @dev:        Device to operate on
210  */
211 void msi_unlock_descs(struct device *dev)
212 {
213         /* Clear the next pointer which was cached by the iterator */
214         dev->msi.data->__next = NULL;
215         mutex_unlock(&dev->msi.data->mutex);
216 }
217 EXPORT_SYMBOL_GPL(msi_unlock_descs);
218
219 static bool msi_desc_match(struct msi_desc *desc, enum msi_desc_filter filter)
220 {
221         switch (filter) {
222         case MSI_DESC_ALL:
223                 return true;
224         case MSI_DESC_NOTASSOCIATED:
225                 return !desc->irq;
226         case MSI_DESC_ASSOCIATED:
227                 return !!desc->irq;
228         }
229         WARN_ON_ONCE(1);
230         return false;
231 }
232
233 static struct msi_desc *msi_find_first_desc(struct device *dev, enum msi_desc_filter filter)
234 {
235         struct msi_desc *desc;
236
237         list_for_each_entry(desc, dev_to_msi_list(dev), list) {
238                 if (msi_desc_match(desc, filter))
239                         return desc;
240         }
241         return NULL;
242 }
243
244 /**
245  * msi_first_desc - Get the first MSI descriptor of a device
246  * @dev:        Device to operate on
247  * @filter:     Descriptor state filter
248  *
249  * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs()
250  * must be invoked before the call.
251  *
252  * Return: Pointer to the first MSI descriptor matching the search
253  *         criteria, NULL if none found.
254  */
255 struct msi_desc *msi_first_desc(struct device *dev, enum msi_desc_filter filter)
256 {
257         struct msi_desc *desc;
258
259         if (WARN_ON_ONCE(!dev->msi.data))
260                 return NULL;
261
262         lockdep_assert_held(&dev->msi.data->mutex);
263
264         desc = msi_find_first_desc(dev, filter);
265         dev->msi.data->__next = desc ? list_next_entry(desc, list) : NULL;
266         return desc;
267 }
268 EXPORT_SYMBOL_GPL(msi_first_desc);
269
270 static struct msi_desc *__msi_next_desc(struct device *dev, enum msi_desc_filter filter,
271                                         struct msi_desc *from)
272 {
273         struct msi_desc *desc = from;
274
275         list_for_each_entry_from(desc, dev_to_msi_list(dev), list) {
276                 if (msi_desc_match(desc, filter))
277                         return desc;
278         }
279         return NULL;
280 }
281
282 /**
283  * msi_next_desc - Get the next MSI descriptor of a device
284  * @dev:        Device to operate on
285  *
286  * The first invocation of msi_next_desc() has to be preceeded by a
287  * successful incovation of __msi_first_desc(). Consecutive invocations are
288  * only valid if the previous one was successful. All these operations have
289  * to be done within the same MSI mutex held region.
290  *
291  * Return: Pointer to the next MSI descriptor matching the search
292  *         criteria, NULL if none found.
293  */
294 struct msi_desc *msi_next_desc(struct device *dev, enum msi_desc_filter filter)
295 {
296         struct msi_device_data *data = dev->msi.data;
297         struct msi_desc *desc;
298
299         if (WARN_ON_ONCE(!data))
300                 return NULL;
301
302         lockdep_assert_held(&data->mutex);
303
304         if (!data->__next)
305                 return NULL;
306
307         desc = __msi_next_desc(dev, filter, data->__next);
308         dev->msi.data->__next = desc ? list_next_entry(desc, list) : NULL;
309         return desc;
310 }
311 EXPORT_SYMBOL_GPL(msi_next_desc);
312
313 /**
314  * msi_get_virq - Return Linux interrupt number of a MSI interrupt
315  * @dev:        Device to operate on
316  * @index:      MSI interrupt index to look for (0-based)
317  *
318  * Return: The Linux interrupt number on success (> 0), 0 if not found
319  */
320 unsigned int msi_get_virq(struct device *dev, unsigned int index)
321 {
322         struct msi_desc *desc;
323         bool pcimsi;
324
325         if (!dev->msi.data)
326                 return 0;
327
328         pcimsi = dev_is_pci(dev) ? to_pci_dev(dev)->msi_enabled : false;
329
330         for_each_msi_entry(desc, dev) {
331                 /* PCI-MSI has only one descriptor for multiple interrupts. */
332                 if (pcimsi) {
333                         if (desc->irq && index < desc->nvec_used)
334                                 return desc->irq + index;
335                         break;
336                 }
337
338                 /*
339                  * PCI-MSIX and platform MSI use a descriptor per
340                  * interrupt.
341                  */
342                 if (desc->msi_index == index)
343                         return desc->irq;
344         }
345         return 0;
346 }
347 EXPORT_SYMBOL_GPL(msi_get_virq);
348
349 #ifdef CONFIG_SYSFS
350 static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
351                              char *buf)
352 {
353         /* MSI vs. MSIX is per device not per interrupt */
354         bool is_msix = dev_is_pci(dev) ? to_pci_dev(dev)->msix_enabled : false;
355
356         return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi");
357 }
358
359 /**
360  * msi_populate_sysfs - Populate msi_irqs sysfs entries for devices
361  * @dev:        The device(PCI, platform etc) who will get sysfs entries
362  */
363 static const struct attribute_group **msi_populate_sysfs(struct device *dev)
364 {
365         const struct attribute_group **msi_irq_groups;
366         struct attribute **msi_attrs, *msi_attr;
367         struct device_attribute *msi_dev_attr;
368         struct attribute_group *msi_irq_group;
369         struct msi_desc *entry;
370         int ret = -ENOMEM;
371         int num_msi = 0;
372         int count = 0;
373         int i;
374
375         /* Determine how many msi entries we have */
376         for_each_msi_entry(entry, dev)
377                 num_msi += entry->nvec_used;
378         if (!num_msi)
379                 return NULL;
380
381         /* Dynamically create the MSI attributes for the device */
382         msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL);
383         if (!msi_attrs)
384                 return ERR_PTR(-ENOMEM);
385
386         for_each_msi_entry(entry, dev) {
387                 for (i = 0; i < entry->nvec_used; i++) {
388                         msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
389                         if (!msi_dev_attr)
390                                 goto error_attrs;
391                         msi_attrs[count] = &msi_dev_attr->attr;
392
393                         sysfs_attr_init(&msi_dev_attr->attr);
394                         msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
395                                                             entry->irq + i);
396                         if (!msi_dev_attr->attr.name)
397                                 goto error_attrs;
398                         msi_dev_attr->attr.mode = 0444;
399                         msi_dev_attr->show = msi_mode_show;
400                         ++count;
401                 }
402         }
403
404         msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
405         if (!msi_irq_group)
406                 goto error_attrs;
407         msi_irq_group->name = "msi_irqs";
408         msi_irq_group->attrs = msi_attrs;
409
410         msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL);
411         if (!msi_irq_groups)
412                 goto error_irq_group;
413         msi_irq_groups[0] = msi_irq_group;
414
415         ret = sysfs_create_groups(&dev->kobj, msi_irq_groups);
416         if (ret)
417                 goto error_irq_groups;
418
419         return msi_irq_groups;
420
421 error_irq_groups:
422         kfree(msi_irq_groups);
423 error_irq_group:
424         kfree(msi_irq_group);
425 error_attrs:
426         count = 0;
427         msi_attr = msi_attrs[count];
428         while (msi_attr) {
429                 msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
430                 kfree(msi_attr->name);
431                 kfree(msi_dev_attr);
432                 ++count;
433                 msi_attr = msi_attrs[count];
434         }
435         kfree(msi_attrs);
436         return ERR_PTR(ret);
437 }
438
439 /**
440  * msi_device_populate_sysfs - Populate msi_irqs sysfs entries for a device
441  * @dev:        The device (PCI, platform etc) which will get sysfs entries
442  */
443 int msi_device_populate_sysfs(struct device *dev)
444 {
445         const struct attribute_group **group = msi_populate_sysfs(dev);
446
447         if (IS_ERR(group))
448                 return PTR_ERR(group);
449         dev->msi.data->attrs = group;
450         return 0;
451 }
452
453 /**
454  * msi_device_destroy_sysfs - Destroy msi_irqs sysfs entries for a device
455  * @dev:                The device (PCI, platform etc) for which to remove
456  *                      sysfs entries
457  */
458 void msi_device_destroy_sysfs(struct device *dev)
459 {
460         const struct attribute_group **msi_irq_groups = dev->msi.data->attrs;
461         struct device_attribute *dev_attr;
462         struct attribute **msi_attrs;
463         int count = 0;
464
465         dev->msi.data->attrs = NULL;
466         if (!msi_irq_groups)
467                 return;
468
469         sysfs_remove_groups(&dev->kobj, msi_irq_groups);
470         msi_attrs = msi_irq_groups[0]->attrs;
471         while (msi_attrs[count]) {
472                 dev_attr = container_of(msi_attrs[count], struct device_attribute, attr);
473                 kfree(dev_attr->attr.name);
474                 kfree(dev_attr);
475                 ++count;
476         }
477         kfree(msi_attrs);
478         kfree(msi_irq_groups[0]);
479         kfree(msi_irq_groups);
480 }
481 #endif
482
483 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
484 static inline void irq_chip_write_msi_msg(struct irq_data *data,
485                                           struct msi_msg *msg)
486 {
487         data->chip->irq_write_msi_msg(data, msg);
488 }
489
490 static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg)
491 {
492         struct msi_domain_info *info = domain->host_data;
493
494         /*
495          * If the MSI provider has messed with the second message and
496          * not advertized that it is level-capable, signal the breakage.
497          */
498         WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
499                   (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) &&
500                 (msg[1].address_lo || msg[1].address_hi || msg[1].data));
501 }
502
503 /**
504  * msi_domain_set_affinity - Generic affinity setter function for MSI domains
505  * @irq_data:   The irq data associated to the interrupt
506  * @mask:       The affinity mask to set
507  * @force:      Flag to enforce setting (disable online checks)
508  *
509  * Intended to be used by MSI interrupt controllers which are
510  * implemented with hierarchical domains.
511  *
512  * Return: IRQ_SET_MASK_* result code
513  */
514 int msi_domain_set_affinity(struct irq_data *irq_data,
515                             const struct cpumask *mask, bool force)
516 {
517         struct irq_data *parent = irq_data->parent_data;
518         struct msi_msg msg[2] = { [1] = { }, };
519         int ret;
520
521         ret = parent->chip->irq_set_affinity(parent, mask, force);
522         if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
523                 BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
524                 msi_check_level(irq_data->domain, msg);
525                 irq_chip_write_msi_msg(irq_data, msg);
526         }
527
528         return ret;
529 }
530
531 static int msi_domain_activate(struct irq_domain *domain,
532                                struct irq_data *irq_data, bool early)
533 {
534         struct msi_msg msg[2] = { [1] = { }, };
535
536         BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
537         msi_check_level(irq_data->domain, msg);
538         irq_chip_write_msi_msg(irq_data, msg);
539         return 0;
540 }
541
542 static void msi_domain_deactivate(struct irq_domain *domain,
543                                   struct irq_data *irq_data)
544 {
545         struct msi_msg msg[2];
546
547         memset(msg, 0, sizeof(msg));
548         irq_chip_write_msi_msg(irq_data, msg);
549 }
550
551 static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
552                             unsigned int nr_irqs, void *arg)
553 {
554         struct msi_domain_info *info = domain->host_data;
555         struct msi_domain_ops *ops = info->ops;
556         irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
557         int i, ret;
558
559         if (irq_find_mapping(domain, hwirq) > 0)
560                 return -EEXIST;
561
562         if (domain->parent) {
563                 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
564                 if (ret < 0)
565                         return ret;
566         }
567
568         for (i = 0; i < nr_irqs; i++) {
569                 ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
570                 if (ret < 0) {
571                         if (ops->msi_free) {
572                                 for (i--; i > 0; i--)
573                                         ops->msi_free(domain, info, virq + i);
574                         }
575                         irq_domain_free_irqs_top(domain, virq, nr_irqs);
576                         return ret;
577                 }
578         }
579
580         return 0;
581 }
582
583 static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
584                             unsigned int nr_irqs)
585 {
586         struct msi_domain_info *info = domain->host_data;
587         int i;
588
589         if (info->ops->msi_free) {
590                 for (i = 0; i < nr_irqs; i++)
591                         info->ops->msi_free(domain, info, virq + i);
592         }
593         irq_domain_free_irqs_top(domain, virq, nr_irqs);
594 }
595
596 static const struct irq_domain_ops msi_domain_ops = {
597         .alloc          = msi_domain_alloc,
598         .free           = msi_domain_free,
599         .activate       = msi_domain_activate,
600         .deactivate     = msi_domain_deactivate,
601 };
602
603 static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
604                                                 msi_alloc_info_t *arg)
605 {
606         return arg->hwirq;
607 }
608
609 static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
610                                   int nvec, msi_alloc_info_t *arg)
611 {
612         memset(arg, 0, sizeof(*arg));
613         return 0;
614 }
615
616 static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
617                                     struct msi_desc *desc)
618 {
619         arg->desc = desc;
620 }
621
622 static int msi_domain_ops_init(struct irq_domain *domain,
623                                struct msi_domain_info *info,
624                                unsigned int virq, irq_hw_number_t hwirq,
625                                msi_alloc_info_t *arg)
626 {
627         irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
628                                       info->chip_data);
629         if (info->handler && info->handler_name) {
630                 __irq_set_handler(virq, info->handler, 0, info->handler_name);
631                 if (info->handler_data)
632                         irq_set_handler_data(virq, info->handler_data);
633         }
634         return 0;
635 }
636
637 static int msi_domain_ops_check(struct irq_domain *domain,
638                                 struct msi_domain_info *info,
639                                 struct device *dev)
640 {
641         return 0;
642 }
643
644 static struct msi_domain_ops msi_domain_ops_default = {
645         .get_hwirq              = msi_domain_ops_get_hwirq,
646         .msi_init               = msi_domain_ops_init,
647         .msi_check              = msi_domain_ops_check,
648         .msi_prepare            = msi_domain_ops_prepare,
649         .set_desc               = msi_domain_ops_set_desc,
650         .domain_alloc_irqs      = __msi_domain_alloc_irqs,
651         .domain_free_irqs       = __msi_domain_free_irqs,
652 };
653
654 static void msi_domain_update_dom_ops(struct msi_domain_info *info)
655 {
656         struct msi_domain_ops *ops = info->ops;
657
658         if (ops == NULL) {
659                 info->ops = &msi_domain_ops_default;
660                 return;
661         }
662
663         if (ops->domain_alloc_irqs == NULL)
664                 ops->domain_alloc_irqs = msi_domain_ops_default.domain_alloc_irqs;
665         if (ops->domain_free_irqs == NULL)
666                 ops->domain_free_irqs = msi_domain_ops_default.domain_free_irqs;
667
668         if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS))
669                 return;
670
671         if (ops->get_hwirq == NULL)
672                 ops->get_hwirq = msi_domain_ops_default.get_hwirq;
673         if (ops->msi_init == NULL)
674                 ops->msi_init = msi_domain_ops_default.msi_init;
675         if (ops->msi_check == NULL)
676                 ops->msi_check = msi_domain_ops_default.msi_check;
677         if (ops->msi_prepare == NULL)
678                 ops->msi_prepare = msi_domain_ops_default.msi_prepare;
679         if (ops->set_desc == NULL)
680                 ops->set_desc = msi_domain_ops_default.set_desc;
681 }
682
683 static void msi_domain_update_chip_ops(struct msi_domain_info *info)
684 {
685         struct irq_chip *chip = info->chip;
686
687         BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
688         if (!chip->irq_set_affinity)
689                 chip->irq_set_affinity = msi_domain_set_affinity;
690 }
691
692 /**
693  * msi_create_irq_domain - Create an MSI interrupt domain
694  * @fwnode:     Optional fwnode of the interrupt controller
695  * @info:       MSI domain info
696  * @parent:     Parent irq domain
697  *
698  * Return: pointer to the created &struct irq_domain or %NULL on failure
699  */
700 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
701                                          struct msi_domain_info *info,
702                                          struct irq_domain *parent)
703 {
704         struct irq_domain *domain;
705
706         msi_domain_update_dom_ops(info);
707         if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
708                 msi_domain_update_chip_ops(info);
709
710         domain = irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0,
711                                              fwnode, &msi_domain_ops, info);
712
713         if (domain && !domain->name && info->chip)
714                 domain->name = info->chip->name;
715
716         return domain;
717 }
718
719 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
720                             int nvec, msi_alloc_info_t *arg)
721 {
722         struct msi_domain_info *info = domain->host_data;
723         struct msi_domain_ops *ops = info->ops;
724         int ret;
725
726         ret = ops->msi_check(domain, info, dev);
727         if (ret == 0)
728                 ret = ops->msi_prepare(domain, dev, nvec, arg);
729
730         return ret;
731 }
732
733 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
734                              int virq, int nvec, msi_alloc_info_t *arg)
735 {
736         struct msi_domain_info *info = domain->host_data;
737         struct msi_domain_ops *ops = info->ops;
738         struct msi_desc *desc;
739         int ret = 0;
740
741         for_each_msi_entry(desc, dev) {
742                 /* Don't even try the multi-MSI brain damage. */
743                 if (WARN_ON(!desc->irq || desc->nvec_used != 1)) {
744                         ret = -EINVAL;
745                         break;
746                 }
747
748                 if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
749                         continue;
750
751                 ops->set_desc(arg, desc);
752                 /* Assumes the domain mutex is held! */
753                 ret = irq_domain_alloc_irqs_hierarchy(domain, desc->irq, 1,
754                                                       arg);
755                 if (ret)
756                         break;
757
758                 irq_set_msi_desc_off(desc->irq, 0, desc);
759         }
760
761         if (ret) {
762                 /* Mop up the damage */
763                 for_each_msi_entry(desc, dev) {
764                         if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
765                                 continue;
766
767                         irq_domain_free_irqs_common(domain, desc->irq, 1);
768                 }
769         }
770
771         return ret;
772 }
773
774 /*
775  * Carefully check whether the device can use reservation mode. If
776  * reservation mode is enabled then the early activation will assign a
777  * dummy vector to the device. If the PCI/MSI device does not support
778  * masking of the entry then this can result in spurious interrupts when
779  * the device driver is not absolutely careful. But even then a malfunction
780  * of the hardware could result in a spurious interrupt on the dummy vector
781  * and render the device unusable. If the entry can be masked then the core
782  * logic will prevent the spurious interrupt and reservation mode can be
783  * used. For now reservation mode is restricted to PCI/MSI.
784  */
785 static bool msi_check_reservation_mode(struct irq_domain *domain,
786                                        struct msi_domain_info *info,
787                                        struct device *dev)
788 {
789         struct msi_desc *desc;
790
791         switch(domain->bus_token) {
792         case DOMAIN_BUS_PCI_MSI:
793         case DOMAIN_BUS_VMD_MSI:
794                 break;
795         default:
796                 return false;
797         }
798
799         if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
800                 return false;
801
802         if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask)
803                 return false;
804
805         /*
806          * Checking the first MSI descriptor is sufficient. MSIX supports
807          * masking and MSI does so when the can_mask attribute is set.
808          */
809         desc = first_msi_entry(dev);
810         return desc->pci.msi_attrib.is_msix || desc->pci.msi_attrib.can_mask;
811 }
812
813 static int msi_handle_pci_fail(struct irq_domain *domain, struct msi_desc *desc,
814                                int allocated)
815 {
816         switch(domain->bus_token) {
817         case DOMAIN_BUS_PCI_MSI:
818         case DOMAIN_BUS_VMD_MSI:
819                 if (IS_ENABLED(CONFIG_PCI_MSI))
820                         break;
821                 fallthrough;
822         default:
823                 return -ENOSPC;
824         }
825
826         /* Let a failed PCI multi MSI allocation retry */
827         if (desc->nvec_used > 1)
828                 return 1;
829
830         /* If there was a successful allocation let the caller know */
831         return allocated ? allocated : -ENOSPC;
832 }
833
834 int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
835                             int nvec)
836 {
837         struct msi_domain_info *info = domain->host_data;
838         struct msi_domain_ops *ops = info->ops;
839         struct irq_data *irq_data;
840         struct msi_desc *desc;
841         msi_alloc_info_t arg = { };
842         int allocated = 0;
843         int i, ret, virq;
844         bool can_reserve;
845
846         ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg);
847         if (ret)
848                 return ret;
849
850         for_each_msi_entry(desc, dev) {
851                 ops->set_desc(&arg, desc);
852
853                 virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
854                                                dev_to_node(dev), &arg, false,
855                                                desc->affinity);
856                 if (virq < 0)
857                         return msi_handle_pci_fail(domain, desc, allocated);
858
859                 for (i = 0; i < desc->nvec_used; i++) {
860                         irq_set_msi_desc_off(virq, i, desc);
861                         irq_debugfs_copy_devname(virq + i, dev);
862                 }
863                 allocated++;
864         }
865
866         can_reserve = msi_check_reservation_mode(domain, info, dev);
867
868         /*
869          * This flag is set by the PCI layer as we need to activate
870          * the MSI entries before the PCI layer enables MSI in the
871          * card. Otherwise the card latches a random msi message.
872          */
873         if (!(info->flags & MSI_FLAG_ACTIVATE_EARLY))
874                 goto skip_activate;
875
876         for_each_msi_vector(desc, i, dev) {
877                 if (desc->irq == i) {
878                         virq = desc->irq;
879                         dev_dbg(dev, "irq [%d-%d] for MSI\n",
880                                 virq, virq + desc->nvec_used - 1);
881                 }
882
883                 irq_data = irq_domain_get_irq_data(domain, i);
884                 if (!can_reserve) {
885                         irqd_clr_can_reserve(irq_data);
886                         if (domain->flags & IRQ_DOMAIN_MSI_NOMASK_QUIRK)
887                                 irqd_set_msi_nomask_quirk(irq_data);
888                 }
889                 ret = irq_domain_activate_irq(irq_data, can_reserve);
890                 if (ret)
891                         return ret;
892         }
893
894 skip_activate:
895         /*
896          * If these interrupts use reservation mode, clear the activated bit
897          * so request_irq() will assign the final vector.
898          */
899         if (can_reserve) {
900                 for_each_msi_vector(desc, i, dev) {
901                         irq_data = irq_domain_get_irq_data(domain, i);
902                         irqd_clr_activated(irq_data);
903                 }
904         }
905         return 0;
906 }
907
908 static int msi_domain_add_simple_msi_descs(struct msi_domain_info *info,
909                                            struct device *dev,
910                                            unsigned int num_descs)
911 {
912         if (!(info->flags & MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS))
913                 return 0;
914
915         return msi_add_simple_msi_descs(dev, 0, num_descs);
916 }
917
918 /**
919  * msi_domain_alloc_irqs_descs_locked - Allocate interrupts from a MSI interrupt domain
920  * @domain:     The domain to allocate from
921  * @dev:        Pointer to device struct of the device for which the interrupts
922  *              are allocated
923  * @nvec:       The number of interrupts to allocate
924  *
925  * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
926  * pair. Use this for MSI irqdomains which implement their own vector
927  * allocation/free.
928  *
929  * Return: %0 on success or an error code.
930  */
931 int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device *dev,
932                                        int nvec)
933 {
934         struct msi_domain_info *info = domain->host_data;
935         struct msi_domain_ops *ops = info->ops;
936         int ret;
937
938         lockdep_assert_held(&dev->msi.data->mutex);
939
940         ret = msi_domain_add_simple_msi_descs(info, dev, nvec);
941         if (ret)
942                 return ret;
943
944         ret = ops->domain_alloc_irqs(domain, dev, nvec);
945         if (ret)
946                 goto cleanup;
947
948         if (!(info->flags & MSI_FLAG_DEV_SYSFS))
949                 return 0;
950
951         ret = msi_device_populate_sysfs(dev);
952         if (ret)
953                 goto cleanup;
954         return 0;
955
956 cleanup:
957         msi_domain_free_irqs_descs_locked(domain, dev);
958         return ret;
959 }
960
961 /**
962  * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain
963  * @domain:     The domain to allocate from
964  * @dev:        Pointer to device struct of the device for which the interrupts
965  *              are allocated
966  * @nvec:       The number of interrupts to allocate
967  *
968  * Return: %0 on success or an error code.
969  */
970 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, int nvec)
971 {
972         int ret;
973
974         msi_lock_descs(dev);
975         ret = msi_domain_alloc_irqs_descs_locked(domain, dev, nvec);
976         msi_unlock_descs(dev);
977         return ret;
978 }
979
980 void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
981 {
982         struct irq_data *irq_data;
983         struct msi_desc *desc;
984         int i;
985
986         for_each_msi_vector(desc, i, dev) {
987                 irq_data = irq_domain_get_irq_data(domain, i);
988                 if (irqd_is_activated(irq_data))
989                         irq_domain_deactivate_irq(irq_data);
990         }
991
992         for_each_msi_entry(desc, dev) {
993                 /*
994                  * We might have failed to allocate an MSI early
995                  * enough that there is no IRQ associated to this
996                  * entry. If that's the case, don't do anything.
997                  */
998                 if (desc->irq) {
999                         irq_domain_free_irqs(desc->irq, desc->nvec_used);
1000                         desc->irq = 0;
1001                 }
1002         }
1003 }
1004
1005 static void msi_domain_free_msi_descs(struct msi_domain_info *info,
1006                                       struct device *dev)
1007 {
1008         if (info->flags & MSI_FLAG_FREE_MSI_DESCS)
1009                 msi_free_msi_descs(dev);
1010 }
1011
1012 /**
1013  * msi_domain_free_irqs_descs_locked - Free interrupts from a MSI interrupt @domain associated to @dev
1014  * @domain:     The domain to managing the interrupts
1015  * @dev:        Pointer to device struct of the device for which the interrupts
1016  *              are free
1017  *
1018  * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
1019  * pair. Use this for MSI irqdomains which implement their own vector
1020  * allocation.
1021  */
1022 void msi_domain_free_irqs_descs_locked(struct irq_domain *domain, struct device *dev)
1023 {
1024         struct msi_domain_info *info = domain->host_data;
1025         struct msi_domain_ops *ops = info->ops;
1026
1027         lockdep_assert_held(&dev->msi.data->mutex);
1028
1029         if (info->flags & MSI_FLAG_DEV_SYSFS)
1030                 msi_device_destroy_sysfs(dev);
1031         ops->domain_free_irqs(domain, dev);
1032         msi_domain_free_msi_descs(info, dev);
1033 }
1034
1035 /**
1036  * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated to @dev
1037  * @domain:     The domain to managing the interrupts
1038  * @dev:        Pointer to device struct of the device for which the interrupts
1039  *              are free
1040  */
1041 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
1042 {
1043         msi_lock_descs(dev);
1044         msi_domain_free_irqs_descs_locked(domain, dev);
1045         msi_unlock_descs(dev);
1046 }
1047
1048 /**
1049  * msi_get_domain_info - Get the MSI interrupt domain info for @domain
1050  * @domain:     The interrupt domain to retrieve data from
1051  *
1052  * Return: the pointer to the msi_domain_info stored in @domain->host_data.
1053  */
1054 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
1055 {
1056         return (struct msi_domain_info *)domain->host_data;
1057 }
1058
1059 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */