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