bb18040efbe81e736a3a6d8cda267fe6142ce073
[linux-2.6-microblaze.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
18 #include "internals.h"
19
20 /**
21  * alloc_msi_entry - Allocate an initialized msi_desc
22  * @dev:        Pointer to the device for which this is allocated
23  * @nvec:       The number of vectors used in this entry
24  * @affinity:   Optional pointer to an affinity mask array size of @nvec
25  *
26  * If @affinity is not %NULL then an affinity array[@nvec] is allocated
27  * and the affinity masks and flags from @affinity are copied.
28  *
29  * Return: pointer to allocated &msi_desc on success or %NULL on failure
30  */
31 struct msi_desc *alloc_msi_entry(struct device *dev, int nvec,
32                                  const struct irq_affinity_desc *affinity)
33 {
34         struct msi_desc *desc;
35
36         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
37         if (!desc)
38                 return NULL;
39
40         INIT_LIST_HEAD(&desc->list);
41         desc->dev = dev;
42         desc->nvec_used = nvec;
43         if (affinity) {
44                 desc->affinity = kmemdup(affinity,
45                         nvec * sizeof(*desc->affinity), GFP_KERNEL);
46                 if (!desc->affinity) {
47                         kfree(desc);
48                         return NULL;
49                 }
50         }
51
52         return desc;
53 }
54
55 void free_msi_entry(struct msi_desc *entry)
56 {
57         kfree(entry->affinity);
58         kfree(entry);
59 }
60
61 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
62 {
63         *msg = entry->msg;
64 }
65
66 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
67 {
68         struct msi_desc *entry = irq_get_msi_desc(irq);
69
70         __get_cached_msi_msg(entry, msg);
71 }
72 EXPORT_SYMBOL_GPL(get_cached_msi_msg);
73
74 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
75 static inline void irq_chip_write_msi_msg(struct irq_data *data,
76                                           struct msi_msg *msg)
77 {
78         data->chip->irq_write_msi_msg(data, msg);
79 }
80
81 static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg)
82 {
83         struct msi_domain_info *info = domain->host_data;
84
85         /*
86          * If the MSI provider has messed with the second message and
87          * not advertized that it is level-capable, signal the breakage.
88          */
89         WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
90                   (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) &&
91                 (msg[1].address_lo || msg[1].address_hi || msg[1].data));
92 }
93
94 /**
95  * msi_domain_set_affinity - Generic affinity setter function for MSI domains
96  * @irq_data:   The irq data associated to the interrupt
97  * @mask:       The affinity mask to set
98  * @force:      Flag to enforce setting (disable online checks)
99  *
100  * Intended to be used by MSI interrupt controllers which are
101  * implemented with hierarchical domains.
102  *
103  * Return: IRQ_SET_MASK_* result code
104  */
105 int msi_domain_set_affinity(struct irq_data *irq_data,
106                             const struct cpumask *mask, bool force)
107 {
108         struct irq_data *parent = irq_data->parent_data;
109         struct msi_msg msg[2] = { [1] = { }, };
110         int ret;
111
112         ret = parent->chip->irq_set_affinity(parent, mask, force);
113         if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
114                 BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
115                 msi_check_level(irq_data->domain, msg);
116                 irq_chip_write_msi_msg(irq_data, msg);
117         }
118
119         return ret;
120 }
121
122 static int msi_domain_activate(struct irq_domain *domain,
123                                struct irq_data *irq_data, bool early)
124 {
125         struct msi_msg msg[2] = { [1] = { }, };
126
127         BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
128         msi_check_level(irq_data->domain, msg);
129         irq_chip_write_msi_msg(irq_data, msg);
130         return 0;
131 }
132
133 static void msi_domain_deactivate(struct irq_domain *domain,
134                                   struct irq_data *irq_data)
135 {
136         struct msi_msg msg[2];
137
138         memset(msg, 0, sizeof(msg));
139         irq_chip_write_msi_msg(irq_data, msg);
140 }
141
142 static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
143                             unsigned int nr_irqs, void *arg)
144 {
145         struct msi_domain_info *info = domain->host_data;
146         struct msi_domain_ops *ops = info->ops;
147         irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
148         int i, ret;
149
150         if (irq_find_mapping(domain, hwirq) > 0)
151                 return -EEXIST;
152
153         if (domain->parent) {
154                 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
155                 if (ret < 0)
156                         return ret;
157         }
158
159         for (i = 0; i < nr_irqs; i++) {
160                 ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
161                 if (ret < 0) {
162                         if (ops->msi_free) {
163                                 for (i--; i > 0; i--)
164                                         ops->msi_free(domain, info, virq + i);
165                         }
166                         irq_domain_free_irqs_top(domain, virq, nr_irqs);
167                         return ret;
168                 }
169         }
170
171         return 0;
172 }
173
174 static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
175                             unsigned int nr_irqs)
176 {
177         struct msi_domain_info *info = domain->host_data;
178         int i;
179
180         if (info->ops->msi_free) {
181                 for (i = 0; i < nr_irqs; i++)
182                         info->ops->msi_free(domain, info, virq + i);
183         }
184         irq_domain_free_irqs_top(domain, virq, nr_irqs);
185 }
186
187 static const struct irq_domain_ops msi_domain_ops = {
188         .alloc          = msi_domain_alloc,
189         .free           = msi_domain_free,
190         .activate       = msi_domain_activate,
191         .deactivate     = msi_domain_deactivate,
192 };
193
194 static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
195                                                 msi_alloc_info_t *arg)
196 {
197         return arg->hwirq;
198 }
199
200 static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
201                                   int nvec, msi_alloc_info_t *arg)
202 {
203         memset(arg, 0, sizeof(*arg));
204         return 0;
205 }
206
207 static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
208                                     struct msi_desc *desc)
209 {
210         arg->desc = desc;
211 }
212
213 static int msi_domain_ops_init(struct irq_domain *domain,
214                                struct msi_domain_info *info,
215                                unsigned int virq, irq_hw_number_t hwirq,
216                                msi_alloc_info_t *arg)
217 {
218         irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
219                                       info->chip_data);
220         if (info->handler && info->handler_name) {
221                 __irq_set_handler(virq, info->handler, 0, info->handler_name);
222                 if (info->handler_data)
223                         irq_set_handler_data(virq, info->handler_data);
224         }
225         return 0;
226 }
227
228 static int msi_domain_ops_check(struct irq_domain *domain,
229                                 struct msi_domain_info *info,
230                                 struct device *dev)
231 {
232         return 0;
233 }
234
235 static struct msi_domain_ops msi_domain_ops_default = {
236         .get_hwirq              = msi_domain_ops_get_hwirq,
237         .msi_init               = msi_domain_ops_init,
238         .msi_check              = msi_domain_ops_check,
239         .msi_prepare            = msi_domain_ops_prepare,
240         .set_desc               = msi_domain_ops_set_desc,
241         .domain_alloc_irqs      = __msi_domain_alloc_irqs,
242         .domain_free_irqs       = __msi_domain_free_irqs,
243 };
244
245 static void msi_domain_update_dom_ops(struct msi_domain_info *info)
246 {
247         struct msi_domain_ops *ops = info->ops;
248
249         if (ops == NULL) {
250                 info->ops = &msi_domain_ops_default;
251                 return;
252         }
253
254         if (ops->domain_alloc_irqs == NULL)
255                 ops->domain_alloc_irqs = msi_domain_ops_default.domain_alloc_irqs;
256         if (ops->domain_free_irqs == NULL)
257                 ops->domain_free_irqs = msi_domain_ops_default.domain_free_irqs;
258
259         if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS))
260                 return;
261
262         if (ops->get_hwirq == NULL)
263                 ops->get_hwirq = msi_domain_ops_default.get_hwirq;
264         if (ops->msi_init == NULL)
265                 ops->msi_init = msi_domain_ops_default.msi_init;
266         if (ops->msi_check == NULL)
267                 ops->msi_check = msi_domain_ops_default.msi_check;
268         if (ops->msi_prepare == NULL)
269                 ops->msi_prepare = msi_domain_ops_default.msi_prepare;
270         if (ops->set_desc == NULL)
271                 ops->set_desc = msi_domain_ops_default.set_desc;
272 }
273
274 static void msi_domain_update_chip_ops(struct msi_domain_info *info)
275 {
276         struct irq_chip *chip = info->chip;
277
278         BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
279         if (!chip->irq_set_affinity)
280                 chip->irq_set_affinity = msi_domain_set_affinity;
281 }
282
283 /**
284  * msi_create_irq_domain - Create an MSI interrupt domain
285  * @fwnode:     Optional fwnode of the interrupt controller
286  * @info:       MSI domain info
287  * @parent:     Parent irq domain
288  *
289  * Return: pointer to the created &struct irq_domain or %NULL on failure
290  */
291 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
292                                          struct msi_domain_info *info,
293                                          struct irq_domain *parent)
294 {
295         struct irq_domain *domain;
296
297         msi_domain_update_dom_ops(info);
298         if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
299                 msi_domain_update_chip_ops(info);
300
301         domain = irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0,
302                                              fwnode, &msi_domain_ops, info);
303
304         if (domain && !domain->name && info->chip)
305                 domain->name = info->chip->name;
306
307         return domain;
308 }
309
310 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
311                             int nvec, msi_alloc_info_t *arg)
312 {
313         struct msi_domain_info *info = domain->host_data;
314         struct msi_domain_ops *ops = info->ops;
315         int ret;
316
317         ret = ops->msi_check(domain, info, dev);
318         if (ret == 0)
319                 ret = ops->msi_prepare(domain, dev, nvec, arg);
320
321         return ret;
322 }
323
324 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
325                              int virq, int nvec, msi_alloc_info_t *arg)
326 {
327         struct msi_domain_info *info = domain->host_data;
328         struct msi_domain_ops *ops = info->ops;
329         struct msi_desc *desc;
330         int ret = 0;
331
332         for_each_msi_entry(desc, dev) {
333                 /* Don't even try the multi-MSI brain damage. */
334                 if (WARN_ON(!desc->irq || desc->nvec_used != 1)) {
335                         ret = -EINVAL;
336                         break;
337                 }
338
339                 if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
340                         continue;
341
342                 ops->set_desc(arg, desc);
343                 /* Assumes the domain mutex is held! */
344                 ret = irq_domain_alloc_irqs_hierarchy(domain, desc->irq, 1,
345                                                       arg);
346                 if (ret)
347                         break;
348
349                 irq_set_msi_desc_off(desc->irq, 0, desc);
350         }
351
352         if (ret) {
353                 /* Mop up the damage */
354                 for_each_msi_entry(desc, dev) {
355                         if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
356                                 continue;
357
358                         irq_domain_free_irqs_common(domain, desc->irq, 1);
359                 }
360         }
361
362         return ret;
363 }
364
365 /*
366  * Carefully check whether the device can use reservation mode. If
367  * reservation mode is enabled then the early activation will assign a
368  * dummy vector to the device. If the PCI/MSI device does not support
369  * masking of the entry then this can result in spurious interrupts when
370  * the device driver is not absolutely careful. But even then a malfunction
371  * of the hardware could result in a spurious interrupt on the dummy vector
372  * and render the device unusable. If the entry can be masked then the core
373  * logic will prevent the spurious interrupt and reservation mode can be
374  * used. For now reservation mode is restricted to PCI/MSI.
375  */
376 static bool msi_check_reservation_mode(struct irq_domain *domain,
377                                        struct msi_domain_info *info,
378                                        struct device *dev)
379 {
380         struct msi_desc *desc;
381
382         switch(domain->bus_token) {
383         case DOMAIN_BUS_PCI_MSI:
384         case DOMAIN_BUS_VMD_MSI:
385                 break;
386         default:
387                 return false;
388         }
389
390         if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
391                 return false;
392
393         if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask)
394                 return false;
395
396         /*
397          * Checking the first MSI descriptor is sufficient. MSIX supports
398          * masking and MSI does so when the maskbit is set.
399          */
400         desc = first_msi_entry(dev);
401         return desc->msi_attrib.is_msix || desc->msi_attrib.maskbit;
402 }
403
404 int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
405                             int nvec)
406 {
407         struct msi_domain_info *info = domain->host_data;
408         struct msi_domain_ops *ops = info->ops;
409         struct irq_data *irq_data;
410         struct msi_desc *desc;
411         msi_alloc_info_t arg = { };
412         int i, ret, virq;
413         bool can_reserve;
414
415         ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg);
416         if (ret)
417                 return ret;
418
419         for_each_msi_entry(desc, dev) {
420                 ops->set_desc(&arg, desc);
421
422                 virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
423                                                dev_to_node(dev), &arg, false,
424                                                desc->affinity);
425                 if (virq < 0) {
426                         ret = -ENOSPC;
427                         if (ops->handle_error)
428                                 ret = ops->handle_error(domain, desc, ret);
429                         if (ops->msi_finish)
430                                 ops->msi_finish(&arg, ret);
431                         return ret;
432                 }
433
434                 for (i = 0; i < desc->nvec_used; i++) {
435                         irq_set_msi_desc_off(virq, i, desc);
436                         irq_debugfs_copy_devname(virq + i, dev);
437                 }
438         }
439
440         if (ops->msi_finish)
441                 ops->msi_finish(&arg, 0);
442
443         can_reserve = msi_check_reservation_mode(domain, info, dev);
444
445         /*
446          * This flag is set by the PCI layer as we need to activate
447          * the MSI entries before the PCI layer enables MSI in the
448          * card. Otherwise the card latches a random msi message.
449          */
450         if (!(info->flags & MSI_FLAG_ACTIVATE_EARLY))
451                 goto skip_activate;
452
453         for_each_msi_vector(desc, i, dev) {
454                 if (desc->irq == i) {
455                         virq = desc->irq;
456                         dev_dbg(dev, "irq [%d-%d] for MSI\n",
457                                 virq, virq + desc->nvec_used - 1);
458                 }
459
460                 irq_data = irq_domain_get_irq_data(domain, i);
461                 if (!can_reserve) {
462                         irqd_clr_can_reserve(irq_data);
463                         if (domain->flags & IRQ_DOMAIN_MSI_NOMASK_QUIRK)
464                                 irqd_set_msi_nomask_quirk(irq_data);
465                 }
466                 ret = irq_domain_activate_irq(irq_data, can_reserve);
467                 if (ret)
468                         goto cleanup;
469         }
470
471 skip_activate:
472         /*
473          * If these interrupts use reservation mode, clear the activated bit
474          * so request_irq() will assign the final vector.
475          */
476         if (can_reserve) {
477                 for_each_msi_vector(desc, i, dev) {
478                         irq_data = irq_domain_get_irq_data(domain, i);
479                         irqd_clr_activated(irq_data);
480                 }
481         }
482         return 0;
483
484 cleanup:
485         for_each_msi_vector(desc, i, dev) {
486                 irq_data = irq_domain_get_irq_data(domain, i);
487                 if (irqd_is_activated(irq_data))
488                         irq_domain_deactivate_irq(irq_data);
489         }
490         msi_domain_free_irqs(domain, dev);
491         return ret;
492 }
493
494 /**
495  * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain
496  * @domain:     The domain to allocate from
497  * @dev:        Pointer to device struct of the device for which the interrupts
498  *              are allocated
499  * @nvec:       The number of interrupts to allocate
500  *
501  * Return: %0 on success or an error code.
502  */
503 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
504                           int nvec)
505 {
506         struct msi_domain_info *info = domain->host_data;
507         struct msi_domain_ops *ops = info->ops;
508
509         return ops->domain_alloc_irqs(domain, dev, nvec);
510 }
511
512 void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
513 {
514         struct msi_desc *desc;
515
516         for_each_msi_entry(desc, dev) {
517                 /*
518                  * We might have failed to allocate an MSI early
519                  * enough that there is no IRQ associated to this
520                  * entry. If that's the case, don't do anything.
521                  */
522                 if (desc->irq) {
523                         irq_domain_free_irqs(desc->irq, desc->nvec_used);
524                         desc->irq = 0;
525                 }
526         }
527 }
528
529 /**
530  * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated to @dev
531  * @domain:     The domain to managing the interrupts
532  * @dev:        Pointer to device struct of the device for which the interrupts
533  *              are free
534  */
535 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
536 {
537         struct msi_domain_info *info = domain->host_data;
538         struct msi_domain_ops *ops = info->ops;
539
540         return ops->domain_free_irqs(domain, dev);
541 }
542
543 /**
544  * msi_get_domain_info - Get the MSI interrupt domain info for @domain
545  * @domain:     The interrupt domain to retrieve data from
546  *
547  * Return: the pointer to the msi_domain_info stored in @domain->host_data.
548  */
549 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
550 {
551         return (struct msi_domain_info *)domain->host_data;
552 }
553
554 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */