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 | * |
Ingo Molnar | a359f75 | 2021-03-22 04:21:30 +0100 | [diff] [blame] | 8 | * This file contains common code to support Message Signaled Interrupts for |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 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> |
Barry Song | 2f17081 | 2021-08-13 15:56:27 +1200 | [diff] [blame] | 17 | #include <linux/pci.h> |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 18 | |
Thomas Gleixner | 07557cc | 2017-09-13 23:29:05 +0200 | [diff] [blame] | 19 | #include "internals.h" |
| 20 | |
Thomas Gleixner | 28f4b04 | 2016-09-14 16:18:47 +0200 | [diff] [blame] | 21 | /** |
Randy Dunlap | 3b35e7e | 2021-08-10 16:48:35 -0700 | [diff] [blame] | 22 | * alloc_msi_entry - Allocate an initialized msi_desc |
Thomas Gleixner | 28f4b04 | 2016-09-14 16:18:47 +0200 | [diff] [blame] | 23 | * @dev: Pointer to the device for which this is allocated |
| 24 | * @nvec: The number of vectors used in this entry |
| 25 | * @affinity: Optional pointer to an affinity mask array size of @nvec |
| 26 | * |
Randy Dunlap | 3b35e7e | 2021-08-10 16:48:35 -0700 | [diff] [blame] | 27 | * If @affinity is not %NULL then an affinity array[@nvec] is allocated |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 28 | * and the affinity masks and flags from @affinity are copied. |
Randy Dunlap | 3b35e7e | 2021-08-10 16:48:35 -0700 | [diff] [blame] | 29 | * |
| 30 | * Return: pointer to allocated &msi_desc on success or %NULL on failure |
Thomas Gleixner | 28f4b04 | 2016-09-14 16:18:47 +0200 | [diff] [blame] | 31 | */ |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 32 | struct msi_desc *alloc_msi_entry(struct device *dev, int nvec, |
| 33 | const struct irq_affinity_desc *affinity) |
Jiang Liu | aa48b6f | 2015-07-09 16:00:47 +0800 | [diff] [blame] | 34 | { |
Thomas Gleixner | 28f4b04 | 2016-09-14 16:18:47 +0200 | [diff] [blame] | 35 | struct msi_desc *desc; |
| 36 | |
| 37 | desc = kzalloc(sizeof(*desc), GFP_KERNEL); |
Jiang Liu | aa48b6f | 2015-07-09 16:00:47 +0800 | [diff] [blame] | 38 | if (!desc) |
| 39 | return NULL; |
| 40 | |
| 41 | INIT_LIST_HEAD(&desc->list); |
| 42 | desc->dev = dev; |
Thomas Gleixner | 28f4b04 | 2016-09-14 16:18:47 +0200 | [diff] [blame] | 43 | desc->nvec_used = nvec; |
| 44 | if (affinity) { |
| 45 | desc->affinity = kmemdup(affinity, |
| 46 | nvec * sizeof(*desc->affinity), GFP_KERNEL); |
| 47 | if (!desc->affinity) { |
| 48 | kfree(desc); |
| 49 | return NULL; |
| 50 | } |
| 51 | } |
Jiang Liu | aa48b6f | 2015-07-09 16:00:47 +0800 | [diff] [blame] | 52 | |
| 53 | return desc; |
| 54 | } |
| 55 | |
| 56 | void free_msi_entry(struct msi_desc *entry) |
| 57 | { |
Thomas Gleixner | 28f4b04 | 2016-09-14 16:18:47 +0200 | [diff] [blame] | 58 | kfree(entry->affinity); |
Jiang Liu | aa48b6f | 2015-07-09 16:00:47 +0800 | [diff] [blame] | 59 | kfree(entry); |
| 60 | } |
| 61 | |
Jiang Liu | 38b6a1c | 2014-11-12 12:11:25 +0100 | [diff] [blame] | 62 | void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
| 63 | { |
| 64 | *msg = entry->msg; |
| 65 | } |
| 66 | |
| 67 | void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg) |
| 68 | { |
| 69 | struct msi_desc *entry = irq_get_msi_desc(irq); |
| 70 | |
| 71 | __get_cached_msi_msg(entry, msg); |
| 72 | } |
| 73 | EXPORT_SYMBOL_GPL(get_cached_msi_msg); |
| 74 | |
Thomas Gleixner | 1197528 | 2021-12-06 23:27:28 +0100 | [diff] [blame^] | 75 | #ifdef CONFIG_SYSFS |
Barry Song | 2f17081 | 2021-08-13 15:56:27 +1200 | [diff] [blame] | 76 | static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr, |
| 77 | char *buf) |
| 78 | { |
| 79 | struct msi_desc *entry; |
| 80 | bool is_msix = false; |
| 81 | unsigned long irq; |
| 82 | int retval; |
| 83 | |
| 84 | retval = kstrtoul(attr->attr.name, 10, &irq); |
| 85 | if (retval) |
| 86 | return retval; |
| 87 | |
| 88 | entry = irq_get_msi_desc(irq); |
| 89 | if (!entry) |
| 90 | return -ENODEV; |
| 91 | |
| 92 | if (dev_is_pci(dev)) |
| 93 | is_msix = entry->msi_attrib.is_msix; |
| 94 | |
| 95 | return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi"); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * msi_populate_sysfs - Populate msi_irqs sysfs entries for devices |
| 100 | * @dev: The device(PCI, platform etc) who will get sysfs entries |
| 101 | * |
| 102 | * Return attribute_group ** so that specific bus MSI can save it to |
| 103 | * somewhere during initilizing msi irqs. If devices has no MSI irq, |
| 104 | * return NULL; if it fails to populate sysfs, return ERR_PTR |
| 105 | */ |
| 106 | const struct attribute_group **msi_populate_sysfs(struct device *dev) |
| 107 | { |
| 108 | const struct attribute_group **msi_irq_groups; |
| 109 | struct attribute **msi_attrs, *msi_attr; |
| 110 | struct device_attribute *msi_dev_attr; |
| 111 | struct attribute_group *msi_irq_group; |
| 112 | struct msi_desc *entry; |
| 113 | int ret = -ENOMEM; |
| 114 | int num_msi = 0; |
| 115 | int count = 0; |
| 116 | int i; |
| 117 | |
| 118 | /* Determine how many msi entries we have */ |
| 119 | for_each_msi_entry(entry, dev) |
| 120 | num_msi += entry->nvec_used; |
| 121 | if (!num_msi) |
| 122 | return NULL; |
| 123 | |
| 124 | /* Dynamically create the MSI attributes for the device */ |
| 125 | msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL); |
| 126 | if (!msi_attrs) |
| 127 | return ERR_PTR(-ENOMEM); |
| 128 | |
| 129 | for_each_msi_entry(entry, dev) { |
| 130 | for (i = 0; i < entry->nvec_used; i++) { |
| 131 | msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL); |
| 132 | if (!msi_dev_attr) |
| 133 | goto error_attrs; |
| 134 | msi_attrs[count] = &msi_dev_attr->attr; |
| 135 | |
| 136 | sysfs_attr_init(&msi_dev_attr->attr); |
| 137 | msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d", |
| 138 | entry->irq + i); |
| 139 | if (!msi_dev_attr->attr.name) |
| 140 | goto error_attrs; |
| 141 | msi_dev_attr->attr.mode = 0444; |
| 142 | msi_dev_attr->show = msi_mode_show; |
| 143 | ++count; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL); |
| 148 | if (!msi_irq_group) |
| 149 | goto error_attrs; |
| 150 | msi_irq_group->name = "msi_irqs"; |
| 151 | msi_irq_group->attrs = msi_attrs; |
| 152 | |
| 153 | msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL); |
| 154 | if (!msi_irq_groups) |
| 155 | goto error_irq_group; |
| 156 | msi_irq_groups[0] = msi_irq_group; |
| 157 | |
| 158 | ret = sysfs_create_groups(&dev->kobj, msi_irq_groups); |
| 159 | if (ret) |
| 160 | goto error_irq_groups; |
| 161 | |
| 162 | return msi_irq_groups; |
| 163 | |
| 164 | error_irq_groups: |
| 165 | kfree(msi_irq_groups); |
| 166 | error_irq_group: |
| 167 | kfree(msi_irq_group); |
| 168 | error_attrs: |
| 169 | count = 0; |
| 170 | msi_attr = msi_attrs[count]; |
| 171 | while (msi_attr) { |
| 172 | msi_dev_attr = container_of(msi_attr, struct device_attribute, attr); |
| 173 | kfree(msi_attr->name); |
| 174 | kfree(msi_dev_attr); |
| 175 | ++count; |
| 176 | msi_attr = msi_attrs[count]; |
| 177 | } |
| 178 | kfree(msi_attrs); |
| 179 | return ERR_PTR(ret); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * msi_destroy_sysfs - Destroy msi_irqs sysfs entries for devices |
| 184 | * @dev: The device(PCI, platform etc) who will remove sysfs entries |
| 185 | * @msi_irq_groups: attribute_group for device msi_irqs entries |
| 186 | */ |
| 187 | void msi_destroy_sysfs(struct device *dev, const struct attribute_group **msi_irq_groups) |
| 188 | { |
| 189 | struct device_attribute *dev_attr; |
| 190 | struct attribute **msi_attrs; |
| 191 | int count = 0; |
| 192 | |
| 193 | if (msi_irq_groups) { |
| 194 | sysfs_remove_groups(&dev->kobj, msi_irq_groups); |
| 195 | msi_attrs = msi_irq_groups[0]->attrs; |
| 196 | while (msi_attrs[count]) { |
| 197 | dev_attr = container_of(msi_attrs[count], |
| 198 | struct device_attribute, attr); |
| 199 | kfree(dev_attr->attr.name); |
| 200 | kfree(dev_attr); |
| 201 | ++count; |
| 202 | } |
| 203 | kfree(msi_attrs); |
| 204 | kfree(msi_irq_groups[0]); |
| 205 | kfree(msi_irq_groups); |
| 206 | } |
| 207 | } |
Thomas Gleixner | 1197528 | 2021-12-06 23:27:28 +0100 | [diff] [blame^] | 208 | #endif |
Barry Song | 2f17081 | 2021-08-13 15:56:27 +1200 | [diff] [blame] | 209 | |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 210 | #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN |
Thomas Gleixner | 74faaf7 | 2014-12-06 21:20:20 +0100 | [diff] [blame] | 211 | static inline void irq_chip_write_msi_msg(struct irq_data *data, |
| 212 | struct msi_msg *msg) |
| 213 | { |
| 214 | data->chip->irq_write_msi_msg(data, msg); |
| 215 | } |
| 216 | |
Marc Zyngier | 0be8153 | 2018-05-08 13:14:30 +0100 | [diff] [blame] | 217 | static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg) |
| 218 | { |
| 219 | struct msi_domain_info *info = domain->host_data; |
| 220 | |
| 221 | /* |
| 222 | * If the MSI provider has messed with the second message and |
| 223 | * not advertized that it is level-capable, signal the breakage. |
| 224 | */ |
| 225 | WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) && |
| 226 | (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) && |
| 227 | (msg[1].address_lo || msg[1].address_hi || msg[1].data)); |
| 228 | } |
| 229 | |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 230 | /** |
| 231 | * msi_domain_set_affinity - Generic affinity setter function for MSI domains |
| 232 | * @irq_data: The irq data associated to the interrupt |
| 233 | * @mask: The affinity mask to set |
| 234 | * @force: Flag to enforce setting (disable online checks) |
| 235 | * |
| 236 | * Intended to be used by MSI interrupt controllers which are |
| 237 | * implemented with hierarchical domains. |
Randy Dunlap | 3b35e7e | 2021-08-10 16:48:35 -0700 | [diff] [blame] | 238 | * |
| 239 | * Return: IRQ_SET_MASK_* result code |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 240 | */ |
| 241 | int msi_domain_set_affinity(struct irq_data *irq_data, |
| 242 | const struct cpumask *mask, bool force) |
| 243 | { |
| 244 | struct irq_data *parent = irq_data->parent_data; |
Marc Zyngier | 0be8153 | 2018-05-08 13:14:30 +0100 | [diff] [blame] | 245 | struct msi_msg msg[2] = { [1] = { }, }; |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 246 | int ret; |
| 247 | |
| 248 | ret = parent->chip->irq_set_affinity(parent, mask, force); |
| 249 | if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) { |
Marc Zyngier | 0be8153 | 2018-05-08 13:14:30 +0100 | [diff] [blame] | 250 | BUG_ON(irq_chip_compose_msi_msg(irq_data, msg)); |
| 251 | msi_check_level(irq_data->domain, msg); |
| 252 | irq_chip_write_msi_msg(irq_data, msg); |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | return ret; |
| 256 | } |
| 257 | |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 258 | static int msi_domain_activate(struct irq_domain *domain, |
| 259 | struct irq_data *irq_data, bool early) |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 260 | { |
Marc Zyngier | 0be8153 | 2018-05-08 13:14:30 +0100 | [diff] [blame] | 261 | struct msi_msg msg[2] = { [1] = { }, }; |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 262 | |
Marc Zyngier | 0be8153 | 2018-05-08 13:14:30 +0100 | [diff] [blame] | 263 | BUG_ON(irq_chip_compose_msi_msg(irq_data, msg)); |
| 264 | msi_check_level(irq_data->domain, msg); |
| 265 | irq_chip_write_msi_msg(irq_data, msg); |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 266 | return 0; |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void msi_domain_deactivate(struct irq_domain *domain, |
| 270 | struct irq_data *irq_data) |
| 271 | { |
Marc Zyngier | 0be8153 | 2018-05-08 13:14:30 +0100 | [diff] [blame] | 272 | struct msi_msg msg[2]; |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 273 | |
Marc Zyngier | 0be8153 | 2018-05-08 13:14:30 +0100 | [diff] [blame] | 274 | memset(msg, 0, sizeof(msg)); |
| 275 | irq_chip_write_msi_msg(irq_data, msg); |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 279 | unsigned int nr_irqs, void *arg) |
| 280 | { |
| 281 | struct msi_domain_info *info = domain->host_data; |
| 282 | struct msi_domain_ops *ops = info->ops; |
| 283 | irq_hw_number_t hwirq = ops->get_hwirq(info, arg); |
| 284 | int i, ret; |
| 285 | |
| 286 | if (irq_find_mapping(domain, hwirq) > 0) |
| 287 | return -EEXIST; |
| 288 | |
Liu Jiang | bf6f869 | 2016-01-12 13:18:06 -0700 | [diff] [blame] | 289 | if (domain->parent) { |
| 290 | ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg); |
| 291 | if (ret < 0) |
| 292 | return ret; |
| 293 | } |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 294 | |
| 295 | for (i = 0; i < nr_irqs; i++) { |
| 296 | ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg); |
| 297 | if (ret < 0) { |
| 298 | if (ops->msi_free) { |
| 299 | for (i--; i > 0; i--) |
| 300 | ops->msi_free(domain, info, virq + i); |
| 301 | } |
| 302 | irq_domain_free_irqs_top(domain, virq, nr_irqs); |
| 303 | return ret; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static void msi_domain_free(struct irq_domain *domain, unsigned int virq, |
| 311 | unsigned int nr_irqs) |
| 312 | { |
| 313 | struct msi_domain_info *info = domain->host_data; |
| 314 | int i; |
| 315 | |
| 316 | if (info->ops->msi_free) { |
| 317 | for (i = 0; i < nr_irqs; i++) |
| 318 | info->ops->msi_free(domain, info, virq + i); |
| 319 | } |
| 320 | irq_domain_free_irqs_top(domain, virq, nr_irqs); |
| 321 | } |
| 322 | |
Krzysztof Kozlowski | 0136402 | 2015-04-27 21:54:23 +0900 | [diff] [blame] | 323 | static const struct irq_domain_ops msi_domain_ops = { |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 324 | .alloc = msi_domain_alloc, |
| 325 | .free = msi_domain_free, |
| 326 | .activate = msi_domain_activate, |
| 327 | .deactivate = msi_domain_deactivate, |
| 328 | }; |
| 329 | |
Jiang Liu | aeeb596 | 2014-11-15 22:24:05 +0800 | [diff] [blame] | 330 | static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info, |
| 331 | msi_alloc_info_t *arg) |
| 332 | { |
| 333 | return arg->hwirq; |
| 334 | } |
| 335 | |
| 336 | static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev, |
| 337 | int nvec, msi_alloc_info_t *arg) |
| 338 | { |
| 339 | memset(arg, 0, sizeof(*arg)); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static void msi_domain_ops_set_desc(msi_alloc_info_t *arg, |
| 344 | struct msi_desc *desc) |
| 345 | { |
| 346 | arg->desc = desc; |
| 347 | } |
Jiang Liu | aeeb596 | 2014-11-15 22:24:05 +0800 | [diff] [blame] | 348 | |
| 349 | static int msi_domain_ops_init(struct irq_domain *domain, |
| 350 | struct msi_domain_info *info, |
| 351 | unsigned int virq, irq_hw_number_t hwirq, |
| 352 | msi_alloc_info_t *arg) |
| 353 | { |
| 354 | irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip, |
| 355 | info->chip_data); |
| 356 | if (info->handler && info->handler_name) { |
| 357 | __irq_set_handler(virq, info->handler, 0, info->handler_name); |
| 358 | if (info->handler_data) |
| 359 | irq_set_handler_data(virq, info->handler_data); |
| 360 | } |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int msi_domain_ops_check(struct irq_domain *domain, |
| 365 | struct msi_domain_info *info, |
| 366 | struct device *dev) |
| 367 | { |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static struct msi_domain_ops msi_domain_ops_default = { |
Thomas Gleixner | 43e9e70 | 2020-08-26 13:16:57 +0200 | [diff] [blame] | 372 | .get_hwirq = msi_domain_ops_get_hwirq, |
| 373 | .msi_init = msi_domain_ops_init, |
| 374 | .msi_check = msi_domain_ops_check, |
| 375 | .msi_prepare = msi_domain_ops_prepare, |
| 376 | .set_desc = msi_domain_ops_set_desc, |
| 377 | .domain_alloc_irqs = __msi_domain_alloc_irqs, |
| 378 | .domain_free_irqs = __msi_domain_free_irqs, |
Jiang Liu | aeeb596 | 2014-11-15 22:24:05 +0800 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | static void msi_domain_update_dom_ops(struct msi_domain_info *info) |
| 382 | { |
| 383 | struct msi_domain_ops *ops = info->ops; |
| 384 | |
| 385 | if (ops == NULL) { |
| 386 | info->ops = &msi_domain_ops_default; |
| 387 | return; |
| 388 | } |
| 389 | |
Thomas Gleixner | 43e9e70 | 2020-08-26 13:16:57 +0200 | [diff] [blame] | 390 | if (ops->domain_alloc_irqs == NULL) |
| 391 | ops->domain_alloc_irqs = msi_domain_ops_default.domain_alloc_irqs; |
| 392 | if (ops->domain_free_irqs == NULL) |
| 393 | ops->domain_free_irqs = msi_domain_ops_default.domain_free_irqs; |
| 394 | |
| 395 | if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS)) |
| 396 | return; |
| 397 | |
Jiang Liu | aeeb596 | 2014-11-15 22:24:05 +0800 | [diff] [blame] | 398 | if (ops->get_hwirq == NULL) |
| 399 | ops->get_hwirq = msi_domain_ops_default.get_hwirq; |
| 400 | if (ops->msi_init == NULL) |
| 401 | ops->msi_init = msi_domain_ops_default.msi_init; |
| 402 | if (ops->msi_check == NULL) |
| 403 | ops->msi_check = msi_domain_ops_default.msi_check; |
| 404 | if (ops->msi_prepare == NULL) |
| 405 | ops->msi_prepare = msi_domain_ops_default.msi_prepare; |
| 406 | if (ops->set_desc == NULL) |
| 407 | ops->set_desc = msi_domain_ops_default.set_desc; |
| 408 | } |
| 409 | |
| 410 | static void msi_domain_update_chip_ops(struct msi_domain_info *info) |
| 411 | { |
| 412 | struct irq_chip *chip = info->chip; |
| 413 | |
Marc Zyngier | 0701c53 | 2015-10-13 19:14:45 +0100 | [diff] [blame] | 414 | BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask); |
Jiang Liu | aeeb596 | 2014-11-15 22:24:05 +0800 | [diff] [blame] | 415 | if (!chip->irq_set_affinity) |
| 416 | chip->irq_set_affinity = msi_domain_set_affinity; |
| 417 | } |
| 418 | |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 419 | /** |
Randy Dunlap | 3b35e7e | 2021-08-10 16:48:35 -0700 | [diff] [blame] | 420 | * msi_create_irq_domain - Create an MSI interrupt domain |
Marc Zyngier | be5436c | 2015-10-13 12:51:44 +0100 | [diff] [blame] | 421 | * @fwnode: Optional fwnode of the interrupt controller |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 422 | * @info: MSI domain info |
| 423 | * @parent: Parent irq domain |
Randy Dunlap | 3b35e7e | 2021-08-10 16:48:35 -0700 | [diff] [blame] | 424 | * |
| 425 | * Return: pointer to the created &struct irq_domain or %NULL on failure |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 426 | */ |
Marc Zyngier | be5436c | 2015-10-13 12:51:44 +0100 | [diff] [blame] | 427 | struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode, |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 428 | struct msi_domain_info *info, |
| 429 | struct irq_domain *parent) |
| 430 | { |
Marc Zyngier | a97b852 | 2017-05-12 12:55:37 +0100 | [diff] [blame] | 431 | struct irq_domain *domain; |
| 432 | |
Thomas Gleixner | 43e9e70 | 2020-08-26 13:16:57 +0200 | [diff] [blame] | 433 | msi_domain_update_dom_ops(info); |
Jiang Liu | aeeb596 | 2014-11-15 22:24:05 +0800 | [diff] [blame] | 434 | if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) |
| 435 | msi_domain_update_chip_ops(info); |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 436 | |
Marc Zyngier | a97b852 | 2017-05-12 12:55:37 +0100 | [diff] [blame] | 437 | domain = irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0, |
| 438 | fwnode, &msi_domain_ops, info); |
Thomas Gleixner | 0165308 | 2017-06-20 01:37:04 +0200 | [diff] [blame] | 439 | |
| 440 | if (domain && !domain->name && info->chip) |
Marc Zyngier | a97b852 | 2017-05-12 12:55:37 +0100 | [diff] [blame] | 441 | domain->name = info->chip->name; |
| 442 | |
| 443 | return domain; |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Marc Zyngier | b2eba39 | 2015-11-23 08:26:05 +0000 | [diff] [blame] | 446 | int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev, |
| 447 | int nvec, msi_alloc_info_t *arg) |
| 448 | { |
| 449 | struct msi_domain_info *info = domain->host_data; |
| 450 | struct msi_domain_ops *ops = info->ops; |
| 451 | int ret; |
| 452 | |
| 453 | ret = ops->msi_check(domain, info, dev); |
| 454 | if (ret == 0) |
| 455 | ret = ops->msi_prepare(domain, dev, nvec, arg); |
| 456 | |
| 457 | return ret; |
| 458 | } |
| 459 | |
Marc Zyngier | 2145ac9 | 2015-11-23 08:26:06 +0000 | [diff] [blame] | 460 | int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev, |
| 461 | int virq, int nvec, msi_alloc_info_t *arg) |
| 462 | { |
| 463 | struct msi_domain_info *info = domain->host_data; |
| 464 | struct msi_domain_ops *ops = info->ops; |
| 465 | struct msi_desc *desc; |
| 466 | int ret = 0; |
| 467 | |
| 468 | for_each_msi_entry(desc, dev) { |
| 469 | /* Don't even try the multi-MSI brain damage. */ |
| 470 | if (WARN_ON(!desc->irq || desc->nvec_used != 1)) { |
| 471 | ret = -EINVAL; |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | if (!(desc->irq >= virq && desc->irq < (virq + nvec))) |
| 476 | continue; |
| 477 | |
| 478 | ops->set_desc(arg, desc); |
| 479 | /* Assumes the domain mutex is held! */ |
John Keeping | 596a7a1 | 2017-09-06 10:35:40 +0100 | [diff] [blame] | 480 | ret = irq_domain_alloc_irqs_hierarchy(domain, desc->irq, 1, |
| 481 | arg); |
Marc Zyngier | 2145ac9 | 2015-11-23 08:26:06 +0000 | [diff] [blame] | 482 | if (ret) |
| 483 | break; |
| 484 | |
John Keeping | 596a7a1 | 2017-09-06 10:35:40 +0100 | [diff] [blame] | 485 | irq_set_msi_desc_off(desc->irq, 0, desc); |
Marc Zyngier | 2145ac9 | 2015-11-23 08:26:06 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | if (ret) { |
| 489 | /* Mop up the damage */ |
| 490 | for_each_msi_entry(desc, dev) { |
| 491 | if (!(desc->irq >= virq && desc->irq < (virq + nvec))) |
| 492 | continue; |
| 493 | |
| 494 | irq_domain_free_irqs_common(domain, desc->irq, 1); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
Thomas Gleixner | bc97623 | 2017-12-29 10:47:22 +0100 | [diff] [blame] | 501 | /* |
| 502 | * Carefully check whether the device can use reservation mode. If |
| 503 | * reservation mode is enabled then the early activation will assign a |
| 504 | * dummy vector to the device. If the PCI/MSI device does not support |
| 505 | * masking of the entry then this can result in spurious interrupts when |
| 506 | * the device driver is not absolutely careful. But even then a malfunction |
| 507 | * of the hardware could result in a spurious interrupt on the dummy vector |
| 508 | * and render the device unusable. If the entry can be masked then the core |
| 509 | * logic will prevent the spurious interrupt and reservation mode can be |
| 510 | * used. For now reservation mode is restricted to PCI/MSI. |
| 511 | */ |
| 512 | static bool msi_check_reservation_mode(struct irq_domain *domain, |
| 513 | struct msi_domain_info *info, |
| 514 | struct device *dev) |
Thomas Gleixner | da5dd9e | 2017-12-29 10:42:10 +0100 | [diff] [blame] | 515 | { |
Thomas Gleixner | bc97623 | 2017-12-29 10:47:22 +0100 | [diff] [blame] | 516 | struct msi_desc *desc; |
| 517 | |
Thomas Gleixner | c6c9e283 | 2020-08-26 13:16:51 +0200 | [diff] [blame] | 518 | switch(domain->bus_token) { |
| 519 | case DOMAIN_BUS_PCI_MSI: |
| 520 | case DOMAIN_BUS_VMD_MSI: |
| 521 | break; |
| 522 | default: |
Thomas Gleixner | bc97623 | 2017-12-29 10:47:22 +0100 | [diff] [blame] | 523 | return false; |
Thomas Gleixner | c6c9e283 | 2020-08-26 13:16:51 +0200 | [diff] [blame] | 524 | } |
Thomas Gleixner | bc97623 | 2017-12-29 10:47:22 +0100 | [diff] [blame] | 525 | |
Thomas Gleixner | da5dd9e | 2017-12-29 10:42:10 +0100 | [diff] [blame] | 526 | if (!(info->flags & MSI_FLAG_MUST_REACTIVATE)) |
| 527 | return false; |
Thomas Gleixner | bc97623 | 2017-12-29 10:47:22 +0100 | [diff] [blame] | 528 | |
| 529 | if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask) |
| 530 | return false; |
| 531 | |
| 532 | /* |
| 533 | * Checking the first MSI descriptor is sufficient. MSIX supports |
Thomas Gleixner | 9c8e9c9 | 2021-11-04 00:27:29 +0100 | [diff] [blame] | 534 | * masking and MSI does so when the can_mask attribute is set. |
Thomas Gleixner | bc97623 | 2017-12-29 10:47:22 +0100 | [diff] [blame] | 535 | */ |
| 536 | desc = first_msi_entry(dev); |
Thomas Gleixner | 9c8e9c9 | 2021-11-04 00:27:29 +0100 | [diff] [blame] | 537 | return desc->msi_attrib.is_msix || desc->msi_attrib.can_mask; |
Thomas Gleixner | da5dd9e | 2017-12-29 10:42:10 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Thomas Gleixner | 43e9e70 | 2020-08-26 13:16:57 +0200 | [diff] [blame] | 540 | int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, |
| 541 | int nvec) |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 542 | { |
| 543 | struct msi_domain_info *info = domain->host_data; |
| 544 | struct msi_domain_ops *ops = info->ops; |
Thomas Gleixner | da5dd9e | 2017-12-29 10:42:10 +0100 | [diff] [blame] | 545 | struct irq_data *irq_data; |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 546 | struct msi_desc *desc; |
Zenghui Yu | 06fde69 | 2020-12-18 14:00:39 +0800 | [diff] [blame] | 547 | msi_alloc_info_t arg = { }; |
Thomas Gleixner | b614091 | 2016-07-04 17:39:22 +0900 | [diff] [blame] | 548 | int i, ret, virq; |
Thomas Gleixner | da5dd9e | 2017-12-29 10:42:10 +0100 | [diff] [blame] | 549 | bool can_reserve; |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 550 | |
Marc Zyngier | b2eba39 | 2015-11-23 08:26:05 +0000 | [diff] [blame] | 551 | ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg); |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 552 | if (ret) |
| 553 | return ret; |
| 554 | |
| 555 | for_each_msi_entry(desc, dev) { |
| 556 | ops->set_desc(&arg, desc); |
| 557 | |
Thomas Gleixner | b614091 | 2016-07-04 17:39:22 +0900 | [diff] [blame] | 558 | virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used, |
Thomas Gleixner | 06ee6d5 | 2016-07-04 17:39:24 +0900 | [diff] [blame] | 559 | dev_to_node(dev), &arg, false, |
Thomas Gleixner | 0972fa5 | 2016-07-04 17:39:26 +0900 | [diff] [blame] | 560 | desc->affinity); |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 561 | if (virq < 0) { |
| 562 | ret = -ENOSPC; |
| 563 | if (ops->handle_error) |
| 564 | ret = ops->handle_error(domain, desc, ret); |
| 565 | if (ops->msi_finish) |
| 566 | ops->msi_finish(&arg, ret); |
| 567 | return ret; |
| 568 | } |
| 569 | |
Thomas Gleixner | 07557cc | 2017-09-13 23:29:05 +0200 | [diff] [blame] | 570 | for (i = 0; i < desc->nvec_used; i++) { |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 571 | irq_set_msi_desc_off(virq, i, desc); |
Thomas Gleixner | 07557cc | 2017-09-13 23:29:05 +0200 | [diff] [blame] | 572 | irq_debugfs_copy_devname(virq + i, dev); |
| 573 | } |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | if (ops->msi_finish) |
| 577 | ops->msi_finish(&arg, 0); |
| 578 | |
Thomas Gleixner | bc97623 | 2017-12-29 10:47:22 +0100 | [diff] [blame] | 579 | can_reserve = msi_check_reservation_mode(domain, info, dev); |
Thomas Gleixner | da5dd9e | 2017-12-29 10:42:10 +0100 | [diff] [blame] | 580 | |
Marc Zyngier | 4c457e8 | 2021-01-23 12:27:59 +0000 | [diff] [blame] | 581 | /* |
| 582 | * This flag is set by the PCI layer as we need to activate |
| 583 | * the MSI entries before the PCI layer enables MSI in the |
| 584 | * card. Otherwise the card latches a random msi message. |
| 585 | */ |
| 586 | if (!(info->flags & MSI_FLAG_ACTIVATE_EARLY)) |
| 587 | goto skip_activate; |
| 588 | |
| 589 | for_each_msi_vector(desc, i, dev) { |
| 590 | if (desc->irq == i) { |
| 591 | virq = desc->irq; |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 592 | dev_dbg(dev, "irq [%d-%d] for MSI\n", |
| 593 | virq, virq + desc->nvec_used - 1); |
Marc Zyngier | 4c457e8 | 2021-01-23 12:27:59 +0000 | [diff] [blame] | 594 | } |
Marc Zyngier | f3b0946 | 2016-07-13 17:18:33 +0100 | [diff] [blame] | 595 | |
Marc Zyngier | 4c457e8 | 2021-01-23 12:27:59 +0000 | [diff] [blame] | 596 | irq_data = irq_domain_get_irq_data(domain, i); |
Thomas Gleixner | 6f1a489 | 2020-01-31 15:26:52 +0100 | [diff] [blame] | 597 | if (!can_reserve) { |
Thomas Gleixner | bc97623 | 2017-12-29 10:47:22 +0100 | [diff] [blame] | 598 | irqd_clr_can_reserve(irq_data); |
Thomas Gleixner | 6f1a489 | 2020-01-31 15:26:52 +0100 | [diff] [blame] | 599 | if (domain->flags & IRQ_DOMAIN_MSI_NOMASK_QUIRK) |
| 600 | irqd_set_msi_nomask_quirk(irq_data); |
| 601 | } |
Thomas Gleixner | bc97623 | 2017-12-29 10:47:22 +0100 | [diff] [blame] | 602 | ret = irq_domain_activate_irq(irq_data, can_reserve); |
Thomas Gleixner | da5dd9e | 2017-12-29 10:42:10 +0100 | [diff] [blame] | 603 | if (ret) |
| 604 | goto cleanup; |
| 605 | } |
| 606 | |
Marc Zyngier | 4c457e8 | 2021-01-23 12:27:59 +0000 | [diff] [blame] | 607 | skip_activate: |
Thomas Gleixner | da5dd9e | 2017-12-29 10:42:10 +0100 | [diff] [blame] | 608 | /* |
| 609 | * If these interrupts use reservation mode, clear the activated bit |
| 610 | * so request_irq() will assign the final vector. |
| 611 | */ |
| 612 | if (can_reserve) { |
Marc Zyngier | 4c457e8 | 2021-01-23 12:27:59 +0000 | [diff] [blame] | 613 | for_each_msi_vector(desc, i, dev) { |
| 614 | irq_data = irq_domain_get_irq_data(domain, i); |
Thomas Gleixner | da5dd9e | 2017-12-29 10:42:10 +0100 | [diff] [blame] | 615 | irqd_clr_activated(irq_data); |
Marc Zyngier | f3b0946 | 2016-07-13 17:18:33 +0100 | [diff] [blame] | 616 | } |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 617 | } |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 618 | return 0; |
Thomas Gleixner | bb9b428 | 2017-09-13 23:29:11 +0200 | [diff] [blame] | 619 | |
| 620 | cleanup: |
Thomas Gleixner | bb9b428 | 2017-09-13 23:29:11 +0200 | [diff] [blame] | 621 | msi_domain_free_irqs(domain, dev); |
| 622 | return ret; |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /** |
Thomas Gleixner | 43e9e70 | 2020-08-26 13:16:57 +0200 | [diff] [blame] | 626 | * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain |
| 627 | * @domain: The domain to allocate from |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 628 | * @dev: Pointer to device struct of the device for which the interrupts |
Thomas Gleixner | 43e9e70 | 2020-08-26 13:16:57 +0200 | [diff] [blame] | 629 | * are allocated |
| 630 | * @nvec: The number of interrupts to allocate |
| 631 | * |
Randy Dunlap | 3b35e7e | 2021-08-10 16:48:35 -0700 | [diff] [blame] | 632 | * Return: %0 on success or an error code. |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 633 | */ |
Thomas Gleixner | 43e9e70 | 2020-08-26 13:16:57 +0200 | [diff] [blame] | 634 | int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, |
| 635 | int nvec) |
| 636 | { |
| 637 | struct msi_domain_info *info = domain->host_data; |
| 638 | struct msi_domain_ops *ops = info->ops; |
| 639 | |
| 640 | return ops->domain_alloc_irqs(domain, dev, nvec); |
| 641 | } |
| 642 | |
| 643 | void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev) |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 644 | { |
Bixuan Cui | dbbc935 | 2021-05-18 11:31:17 +0800 | [diff] [blame] | 645 | struct irq_data *irq_data; |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 646 | struct msi_desc *desc; |
Bixuan Cui | dbbc935 | 2021-05-18 11:31:17 +0800 | [diff] [blame] | 647 | int i; |
| 648 | |
| 649 | for_each_msi_vector(desc, i, dev) { |
| 650 | irq_data = irq_domain_get_irq_data(domain, i); |
| 651 | if (irqd_is_activated(irq_data)) |
| 652 | irq_domain_deactivate_irq(irq_data); |
| 653 | } |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 654 | |
| 655 | for_each_msi_entry(desc, dev) { |
Marc Zyngier | fe0c52f | 2015-01-26 19:10:19 +0000 | [diff] [blame] | 656 | /* |
| 657 | * We might have failed to allocate an MSI early |
| 658 | * enough that there is no IRQ associated to this |
| 659 | * entry. If that's the case, don't do anything. |
| 660 | */ |
| 661 | if (desc->irq) { |
| 662 | irq_domain_free_irqs(desc->irq, desc->nvec_used); |
| 663 | desc->irq = 0; |
| 664 | } |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
| 668 | /** |
Randy Dunlap | 3b35e7e | 2021-08-10 16:48:35 -0700 | [diff] [blame] | 669 | * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated to @dev |
Thomas Gleixner | 43e9e70 | 2020-08-26 13:16:57 +0200 | [diff] [blame] | 670 | * @domain: The domain to managing the interrupts |
| 671 | * @dev: Pointer to device struct of the device for which the interrupts |
| 672 | * are free |
| 673 | */ |
| 674 | void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev) |
| 675 | { |
| 676 | struct msi_domain_info *info = domain->host_data; |
| 677 | struct msi_domain_ops *ops = info->ops; |
| 678 | |
| 679 | return ops->domain_free_irqs(domain, dev); |
| 680 | } |
| 681 | |
| 682 | /** |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 683 | * msi_get_domain_info - Get the MSI interrupt domain info for @domain |
| 684 | * @domain: The interrupt domain to retrieve data from |
| 685 | * |
Randy Dunlap | 3b35e7e | 2021-08-10 16:48:35 -0700 | [diff] [blame] | 686 | * Return: the pointer to the msi_domain_info stored in @domain->host_data. |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 687 | */ |
| 688 | struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain) |
| 689 | { |
| 690 | return (struct msi_domain_info *)domain->host_data; |
| 691 | } |
| 692 | |
| 693 | #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */ |