152a65ff5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0 2f3cf8bb0SJiang Liu /* 3f3cf8bb0SJiang Liu * Copyright (C) 2014 Intel Corp. 4f3cf8bb0SJiang Liu * Author: Jiang Liu <jiang.liu@linux.intel.com> 5f3cf8bb0SJiang Liu * 6f3cf8bb0SJiang Liu * This file is licensed under GPLv2. 7f3cf8bb0SJiang Liu * 8a359f757SIngo Molnar * This file contains common code to support Message Signaled Interrupts for 9f3cf8bb0SJiang Liu * PCI compatible and non PCI compatible devices. 10f3cf8bb0SJiang Liu */ 11aeeb5965SJiang Liu #include <linux/types.h> 12aeeb5965SJiang Liu #include <linux/device.h> 13f3cf8bb0SJiang Liu #include <linux/irq.h> 14f3cf8bb0SJiang Liu #include <linux/irqdomain.h> 15f3cf8bb0SJiang Liu #include <linux/msi.h> 164e201566SMarc Zyngier #include <linux/slab.h> 17*2f170814SBarry Song #include <linux/pci.h> 18d9109698SJiang Liu 1907557ccbSThomas Gleixner #include "internals.h" 2007557ccbSThomas Gleixner 2128f4b041SThomas Gleixner /** 223b35e7e6SRandy Dunlap * alloc_msi_entry - Allocate an initialized msi_desc 2328f4b041SThomas Gleixner * @dev: Pointer to the device for which this is allocated 2428f4b041SThomas Gleixner * @nvec: The number of vectors used in this entry 2528f4b041SThomas Gleixner * @affinity: Optional pointer to an affinity mask array size of @nvec 2628f4b041SThomas Gleixner * 273b35e7e6SRandy Dunlap * If @affinity is not %NULL then an affinity array[@nvec] is allocated 28bec04037SDou Liyang * and the affinity masks and flags from @affinity are copied. 293b35e7e6SRandy Dunlap * 303b35e7e6SRandy Dunlap * Return: pointer to allocated &msi_desc on success or %NULL on failure 3128f4b041SThomas Gleixner */ 32bec04037SDou Liyang struct msi_desc *alloc_msi_entry(struct device *dev, int nvec, 33bec04037SDou Liyang const struct irq_affinity_desc *affinity) 34aa48b6f7SJiang Liu { 3528f4b041SThomas Gleixner struct msi_desc *desc; 3628f4b041SThomas Gleixner 3728f4b041SThomas Gleixner desc = kzalloc(sizeof(*desc), GFP_KERNEL); 38aa48b6f7SJiang Liu if (!desc) 39aa48b6f7SJiang Liu return NULL; 40aa48b6f7SJiang Liu 41aa48b6f7SJiang Liu INIT_LIST_HEAD(&desc->list); 42aa48b6f7SJiang Liu desc->dev = dev; 4328f4b041SThomas Gleixner desc->nvec_used = nvec; 4428f4b041SThomas Gleixner if (affinity) { 4528f4b041SThomas Gleixner desc->affinity = kmemdup(affinity, 4628f4b041SThomas Gleixner nvec * sizeof(*desc->affinity), GFP_KERNEL); 4728f4b041SThomas Gleixner if (!desc->affinity) { 4828f4b041SThomas Gleixner kfree(desc); 4928f4b041SThomas Gleixner return NULL; 5028f4b041SThomas Gleixner } 5128f4b041SThomas Gleixner } 52aa48b6f7SJiang Liu 53aa48b6f7SJiang Liu return desc; 54aa48b6f7SJiang Liu } 55aa48b6f7SJiang Liu 56aa48b6f7SJiang Liu void free_msi_entry(struct msi_desc *entry) 57aa48b6f7SJiang Liu { 5828f4b041SThomas Gleixner kfree(entry->affinity); 59aa48b6f7SJiang Liu kfree(entry); 60aa48b6f7SJiang Liu } 61aa48b6f7SJiang Liu 6238b6a1cfSJiang Liu void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg) 6338b6a1cfSJiang Liu { 6438b6a1cfSJiang Liu *msg = entry->msg; 6538b6a1cfSJiang Liu } 6638b6a1cfSJiang Liu 6738b6a1cfSJiang Liu void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg) 6838b6a1cfSJiang Liu { 6938b6a1cfSJiang Liu struct msi_desc *entry = irq_get_msi_desc(irq); 7038b6a1cfSJiang Liu 7138b6a1cfSJiang Liu __get_cached_msi_msg(entry, msg); 7238b6a1cfSJiang Liu } 7338b6a1cfSJiang Liu EXPORT_SYMBOL_GPL(get_cached_msi_msg); 7438b6a1cfSJiang Liu 75*2f170814SBarry Song static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr, 76*2f170814SBarry Song char *buf) 77*2f170814SBarry Song { 78*2f170814SBarry Song struct msi_desc *entry; 79*2f170814SBarry Song bool is_msix = false; 80*2f170814SBarry Song unsigned long irq; 81*2f170814SBarry Song int retval; 82*2f170814SBarry Song 83*2f170814SBarry Song retval = kstrtoul(attr->attr.name, 10, &irq); 84*2f170814SBarry Song if (retval) 85*2f170814SBarry Song return retval; 86*2f170814SBarry Song 87*2f170814SBarry Song entry = irq_get_msi_desc(irq); 88*2f170814SBarry Song if (!entry) 89*2f170814SBarry Song return -ENODEV; 90*2f170814SBarry Song 91*2f170814SBarry Song if (dev_is_pci(dev)) 92*2f170814SBarry Song is_msix = entry->msi_attrib.is_msix; 93*2f170814SBarry Song 94*2f170814SBarry Song return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi"); 95*2f170814SBarry Song } 96*2f170814SBarry Song 97*2f170814SBarry Song /** 98*2f170814SBarry Song * msi_populate_sysfs - Populate msi_irqs sysfs entries for devices 99*2f170814SBarry Song * @dev: The device(PCI, platform etc) who will get sysfs entries 100*2f170814SBarry Song * 101*2f170814SBarry Song * Return attribute_group ** so that specific bus MSI can save it to 102*2f170814SBarry Song * somewhere during initilizing msi irqs. If devices has no MSI irq, 103*2f170814SBarry Song * return NULL; if it fails to populate sysfs, return ERR_PTR 104*2f170814SBarry Song */ 105*2f170814SBarry Song const struct attribute_group **msi_populate_sysfs(struct device *dev) 106*2f170814SBarry Song { 107*2f170814SBarry Song const struct attribute_group **msi_irq_groups; 108*2f170814SBarry Song struct attribute **msi_attrs, *msi_attr; 109*2f170814SBarry Song struct device_attribute *msi_dev_attr; 110*2f170814SBarry Song struct attribute_group *msi_irq_group; 111*2f170814SBarry Song struct msi_desc *entry; 112*2f170814SBarry Song int ret = -ENOMEM; 113*2f170814SBarry Song int num_msi = 0; 114*2f170814SBarry Song int count = 0; 115*2f170814SBarry Song int i; 116*2f170814SBarry Song 117*2f170814SBarry Song /* Determine how many msi entries we have */ 118*2f170814SBarry Song for_each_msi_entry(entry, dev) 119*2f170814SBarry Song num_msi += entry->nvec_used; 120*2f170814SBarry Song if (!num_msi) 121*2f170814SBarry Song return NULL; 122*2f170814SBarry Song 123*2f170814SBarry Song /* Dynamically create the MSI attributes for the device */ 124*2f170814SBarry Song msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL); 125*2f170814SBarry Song if (!msi_attrs) 126*2f170814SBarry Song return ERR_PTR(-ENOMEM); 127*2f170814SBarry Song 128*2f170814SBarry Song for_each_msi_entry(entry, dev) { 129*2f170814SBarry Song for (i = 0; i < entry->nvec_used; i++) { 130*2f170814SBarry Song msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL); 131*2f170814SBarry Song if (!msi_dev_attr) 132*2f170814SBarry Song goto error_attrs; 133*2f170814SBarry Song msi_attrs[count] = &msi_dev_attr->attr; 134*2f170814SBarry Song 135*2f170814SBarry Song sysfs_attr_init(&msi_dev_attr->attr); 136*2f170814SBarry Song msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d", 137*2f170814SBarry Song entry->irq + i); 138*2f170814SBarry Song if (!msi_dev_attr->attr.name) 139*2f170814SBarry Song goto error_attrs; 140*2f170814SBarry Song msi_dev_attr->attr.mode = 0444; 141*2f170814SBarry Song msi_dev_attr->show = msi_mode_show; 142*2f170814SBarry Song ++count; 143*2f170814SBarry Song } 144*2f170814SBarry Song } 145*2f170814SBarry Song 146*2f170814SBarry Song msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL); 147*2f170814SBarry Song if (!msi_irq_group) 148*2f170814SBarry Song goto error_attrs; 149*2f170814SBarry Song msi_irq_group->name = "msi_irqs"; 150*2f170814SBarry Song msi_irq_group->attrs = msi_attrs; 151*2f170814SBarry Song 152*2f170814SBarry Song msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL); 153*2f170814SBarry Song if (!msi_irq_groups) 154*2f170814SBarry Song goto error_irq_group; 155*2f170814SBarry Song msi_irq_groups[0] = msi_irq_group; 156*2f170814SBarry Song 157*2f170814SBarry Song ret = sysfs_create_groups(&dev->kobj, msi_irq_groups); 158*2f170814SBarry Song if (ret) 159*2f170814SBarry Song goto error_irq_groups; 160*2f170814SBarry Song 161*2f170814SBarry Song return msi_irq_groups; 162*2f170814SBarry Song 163*2f170814SBarry Song error_irq_groups: 164*2f170814SBarry Song kfree(msi_irq_groups); 165*2f170814SBarry Song error_irq_group: 166*2f170814SBarry Song kfree(msi_irq_group); 167*2f170814SBarry Song error_attrs: 168*2f170814SBarry Song count = 0; 169*2f170814SBarry Song msi_attr = msi_attrs[count]; 170*2f170814SBarry Song while (msi_attr) { 171*2f170814SBarry Song msi_dev_attr = container_of(msi_attr, struct device_attribute, attr); 172*2f170814SBarry Song kfree(msi_attr->name); 173*2f170814SBarry Song kfree(msi_dev_attr); 174*2f170814SBarry Song ++count; 175*2f170814SBarry Song msi_attr = msi_attrs[count]; 176*2f170814SBarry Song } 177*2f170814SBarry Song kfree(msi_attrs); 178*2f170814SBarry Song return ERR_PTR(ret); 179*2f170814SBarry Song } 180*2f170814SBarry Song 181*2f170814SBarry Song /** 182*2f170814SBarry Song * msi_destroy_sysfs - Destroy msi_irqs sysfs entries for devices 183*2f170814SBarry Song * @dev: The device(PCI, platform etc) who will remove sysfs entries 184*2f170814SBarry Song * @msi_irq_groups: attribute_group for device msi_irqs entries 185*2f170814SBarry Song */ 186*2f170814SBarry Song void msi_destroy_sysfs(struct device *dev, const struct attribute_group **msi_irq_groups) 187*2f170814SBarry Song { 188*2f170814SBarry Song struct device_attribute *dev_attr; 189*2f170814SBarry Song struct attribute **msi_attrs; 190*2f170814SBarry Song int count = 0; 191*2f170814SBarry Song 192*2f170814SBarry Song if (msi_irq_groups) { 193*2f170814SBarry Song sysfs_remove_groups(&dev->kobj, msi_irq_groups); 194*2f170814SBarry Song msi_attrs = msi_irq_groups[0]->attrs; 195*2f170814SBarry Song while (msi_attrs[count]) { 196*2f170814SBarry Song dev_attr = container_of(msi_attrs[count], 197*2f170814SBarry Song struct device_attribute, attr); 198*2f170814SBarry Song kfree(dev_attr->attr.name); 199*2f170814SBarry Song kfree(dev_attr); 200*2f170814SBarry Song ++count; 201*2f170814SBarry Song } 202*2f170814SBarry Song kfree(msi_attrs); 203*2f170814SBarry Song kfree(msi_irq_groups[0]); 204*2f170814SBarry Song kfree(msi_irq_groups); 205*2f170814SBarry Song } 206*2f170814SBarry Song } 207*2f170814SBarry Song 208f3cf8bb0SJiang Liu #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 20974faaf7aSThomas Gleixner static inline void irq_chip_write_msi_msg(struct irq_data *data, 21074faaf7aSThomas Gleixner struct msi_msg *msg) 21174faaf7aSThomas Gleixner { 21274faaf7aSThomas Gleixner data->chip->irq_write_msi_msg(data, msg); 21374faaf7aSThomas Gleixner } 21474faaf7aSThomas Gleixner 2150be8153cSMarc Zyngier static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg) 2160be8153cSMarc Zyngier { 2170be8153cSMarc Zyngier struct msi_domain_info *info = domain->host_data; 2180be8153cSMarc Zyngier 2190be8153cSMarc Zyngier /* 2200be8153cSMarc Zyngier * If the MSI provider has messed with the second message and 2210be8153cSMarc Zyngier * not advertized that it is level-capable, signal the breakage. 2220be8153cSMarc Zyngier */ 2230be8153cSMarc Zyngier WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) && 2240be8153cSMarc Zyngier (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) && 2250be8153cSMarc Zyngier (msg[1].address_lo || msg[1].address_hi || msg[1].data)); 2260be8153cSMarc Zyngier } 2270be8153cSMarc Zyngier 228f3cf8bb0SJiang Liu /** 229f3cf8bb0SJiang Liu * msi_domain_set_affinity - Generic affinity setter function for MSI domains 230f3cf8bb0SJiang Liu * @irq_data: The irq data associated to the interrupt 231f3cf8bb0SJiang Liu * @mask: The affinity mask to set 232f3cf8bb0SJiang Liu * @force: Flag to enforce setting (disable online checks) 233f3cf8bb0SJiang Liu * 234f3cf8bb0SJiang Liu * Intended to be used by MSI interrupt controllers which are 235f3cf8bb0SJiang Liu * implemented with hierarchical domains. 2363b35e7e6SRandy Dunlap * 2373b35e7e6SRandy Dunlap * Return: IRQ_SET_MASK_* result code 238f3cf8bb0SJiang Liu */ 239f3cf8bb0SJiang Liu int msi_domain_set_affinity(struct irq_data *irq_data, 240f3cf8bb0SJiang Liu const struct cpumask *mask, bool force) 241f3cf8bb0SJiang Liu { 242f3cf8bb0SJiang Liu struct irq_data *parent = irq_data->parent_data; 2430be8153cSMarc Zyngier struct msi_msg msg[2] = { [1] = { }, }; 244f3cf8bb0SJiang Liu int ret; 245f3cf8bb0SJiang Liu 246f3cf8bb0SJiang Liu ret = parent->chip->irq_set_affinity(parent, mask, force); 247f3cf8bb0SJiang Liu if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) { 2480be8153cSMarc Zyngier BUG_ON(irq_chip_compose_msi_msg(irq_data, msg)); 2490be8153cSMarc Zyngier msi_check_level(irq_data->domain, msg); 2500be8153cSMarc Zyngier irq_chip_write_msi_msg(irq_data, msg); 251f3cf8bb0SJiang Liu } 252f3cf8bb0SJiang Liu 253f3cf8bb0SJiang Liu return ret; 254f3cf8bb0SJiang Liu } 255f3cf8bb0SJiang Liu 25672491643SThomas Gleixner static int msi_domain_activate(struct irq_domain *domain, 25772491643SThomas Gleixner struct irq_data *irq_data, bool early) 258f3cf8bb0SJiang Liu { 2590be8153cSMarc Zyngier struct msi_msg msg[2] = { [1] = { }, }; 260f3cf8bb0SJiang Liu 2610be8153cSMarc Zyngier BUG_ON(irq_chip_compose_msi_msg(irq_data, msg)); 2620be8153cSMarc Zyngier msi_check_level(irq_data->domain, msg); 2630be8153cSMarc Zyngier irq_chip_write_msi_msg(irq_data, msg); 26472491643SThomas Gleixner return 0; 265f3cf8bb0SJiang Liu } 266f3cf8bb0SJiang Liu 267f3cf8bb0SJiang Liu static void msi_domain_deactivate(struct irq_domain *domain, 268f3cf8bb0SJiang Liu struct irq_data *irq_data) 269f3cf8bb0SJiang Liu { 2700be8153cSMarc Zyngier struct msi_msg msg[2]; 271f3cf8bb0SJiang Liu 2720be8153cSMarc Zyngier memset(msg, 0, sizeof(msg)); 2730be8153cSMarc Zyngier irq_chip_write_msi_msg(irq_data, msg); 274f3cf8bb0SJiang Liu } 275f3cf8bb0SJiang Liu 276f3cf8bb0SJiang Liu static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq, 277f3cf8bb0SJiang Liu unsigned int nr_irqs, void *arg) 278f3cf8bb0SJiang Liu { 279f3cf8bb0SJiang Liu struct msi_domain_info *info = domain->host_data; 280f3cf8bb0SJiang Liu struct msi_domain_ops *ops = info->ops; 281f3cf8bb0SJiang Liu irq_hw_number_t hwirq = ops->get_hwirq(info, arg); 282f3cf8bb0SJiang Liu int i, ret; 283f3cf8bb0SJiang Liu 284f3cf8bb0SJiang Liu if (irq_find_mapping(domain, hwirq) > 0) 285f3cf8bb0SJiang Liu return -EEXIST; 286f3cf8bb0SJiang Liu 287bf6f869fSLiu Jiang if (domain->parent) { 288f3cf8bb0SJiang Liu ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg); 289f3cf8bb0SJiang Liu if (ret < 0) 290f3cf8bb0SJiang Liu return ret; 291bf6f869fSLiu Jiang } 292f3cf8bb0SJiang Liu 293f3cf8bb0SJiang Liu for (i = 0; i < nr_irqs; i++) { 294f3cf8bb0SJiang Liu ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg); 295f3cf8bb0SJiang Liu if (ret < 0) { 296f3cf8bb0SJiang Liu if (ops->msi_free) { 297f3cf8bb0SJiang Liu for (i--; i > 0; i--) 298f3cf8bb0SJiang Liu ops->msi_free(domain, info, virq + i); 299f3cf8bb0SJiang Liu } 300f3cf8bb0SJiang Liu irq_domain_free_irqs_top(domain, virq, nr_irqs); 301f3cf8bb0SJiang Liu return ret; 302f3cf8bb0SJiang Liu } 303f3cf8bb0SJiang Liu } 304f3cf8bb0SJiang Liu 305f3cf8bb0SJiang Liu return 0; 306f3cf8bb0SJiang Liu } 307f3cf8bb0SJiang Liu 308f3cf8bb0SJiang Liu static void msi_domain_free(struct irq_domain *domain, unsigned int virq, 309f3cf8bb0SJiang Liu unsigned int nr_irqs) 310f3cf8bb0SJiang Liu { 311f3cf8bb0SJiang Liu struct msi_domain_info *info = domain->host_data; 312f3cf8bb0SJiang Liu int i; 313f3cf8bb0SJiang Liu 314f3cf8bb0SJiang Liu if (info->ops->msi_free) { 315f3cf8bb0SJiang Liu for (i = 0; i < nr_irqs; i++) 316f3cf8bb0SJiang Liu info->ops->msi_free(domain, info, virq + i); 317f3cf8bb0SJiang Liu } 318f3cf8bb0SJiang Liu irq_domain_free_irqs_top(domain, virq, nr_irqs); 319f3cf8bb0SJiang Liu } 320f3cf8bb0SJiang Liu 32101364028SKrzysztof Kozlowski static const struct irq_domain_ops msi_domain_ops = { 322f3cf8bb0SJiang Liu .alloc = msi_domain_alloc, 323f3cf8bb0SJiang Liu .free = msi_domain_free, 324f3cf8bb0SJiang Liu .activate = msi_domain_activate, 325f3cf8bb0SJiang Liu .deactivate = msi_domain_deactivate, 326f3cf8bb0SJiang Liu }; 327f3cf8bb0SJiang Liu 328aeeb5965SJiang Liu static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info, 329aeeb5965SJiang Liu msi_alloc_info_t *arg) 330aeeb5965SJiang Liu { 331aeeb5965SJiang Liu return arg->hwirq; 332aeeb5965SJiang Liu } 333aeeb5965SJiang Liu 334aeeb5965SJiang Liu static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev, 335aeeb5965SJiang Liu int nvec, msi_alloc_info_t *arg) 336aeeb5965SJiang Liu { 337aeeb5965SJiang Liu memset(arg, 0, sizeof(*arg)); 338aeeb5965SJiang Liu return 0; 339aeeb5965SJiang Liu } 340aeeb5965SJiang Liu 341aeeb5965SJiang Liu static void msi_domain_ops_set_desc(msi_alloc_info_t *arg, 342aeeb5965SJiang Liu struct msi_desc *desc) 343aeeb5965SJiang Liu { 344aeeb5965SJiang Liu arg->desc = desc; 345aeeb5965SJiang Liu } 346aeeb5965SJiang Liu 347aeeb5965SJiang Liu static int msi_domain_ops_init(struct irq_domain *domain, 348aeeb5965SJiang Liu struct msi_domain_info *info, 349aeeb5965SJiang Liu unsigned int virq, irq_hw_number_t hwirq, 350aeeb5965SJiang Liu msi_alloc_info_t *arg) 351aeeb5965SJiang Liu { 352aeeb5965SJiang Liu irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip, 353aeeb5965SJiang Liu info->chip_data); 354aeeb5965SJiang Liu if (info->handler && info->handler_name) { 355aeeb5965SJiang Liu __irq_set_handler(virq, info->handler, 0, info->handler_name); 356aeeb5965SJiang Liu if (info->handler_data) 357aeeb5965SJiang Liu irq_set_handler_data(virq, info->handler_data); 358aeeb5965SJiang Liu } 359aeeb5965SJiang Liu return 0; 360aeeb5965SJiang Liu } 361aeeb5965SJiang Liu 362aeeb5965SJiang Liu static int msi_domain_ops_check(struct irq_domain *domain, 363aeeb5965SJiang Liu struct msi_domain_info *info, 364aeeb5965SJiang Liu struct device *dev) 365aeeb5965SJiang Liu { 366aeeb5965SJiang Liu return 0; 367aeeb5965SJiang Liu } 368aeeb5965SJiang Liu 369aeeb5965SJiang Liu static struct msi_domain_ops msi_domain_ops_default = { 370aeeb5965SJiang Liu .get_hwirq = msi_domain_ops_get_hwirq, 371aeeb5965SJiang Liu .msi_init = msi_domain_ops_init, 372aeeb5965SJiang Liu .msi_check = msi_domain_ops_check, 373aeeb5965SJiang Liu .msi_prepare = msi_domain_ops_prepare, 374aeeb5965SJiang Liu .set_desc = msi_domain_ops_set_desc, 37543e9e705SThomas Gleixner .domain_alloc_irqs = __msi_domain_alloc_irqs, 37643e9e705SThomas Gleixner .domain_free_irqs = __msi_domain_free_irqs, 377aeeb5965SJiang Liu }; 378aeeb5965SJiang Liu 379aeeb5965SJiang Liu static void msi_domain_update_dom_ops(struct msi_domain_info *info) 380aeeb5965SJiang Liu { 381aeeb5965SJiang Liu struct msi_domain_ops *ops = info->ops; 382aeeb5965SJiang Liu 383aeeb5965SJiang Liu if (ops == NULL) { 384aeeb5965SJiang Liu info->ops = &msi_domain_ops_default; 385aeeb5965SJiang Liu return; 386aeeb5965SJiang Liu } 387aeeb5965SJiang Liu 38843e9e705SThomas Gleixner if (ops->domain_alloc_irqs == NULL) 38943e9e705SThomas Gleixner ops->domain_alloc_irqs = msi_domain_ops_default.domain_alloc_irqs; 39043e9e705SThomas Gleixner if (ops->domain_free_irqs == NULL) 39143e9e705SThomas Gleixner ops->domain_free_irqs = msi_domain_ops_default.domain_free_irqs; 39243e9e705SThomas Gleixner 39343e9e705SThomas Gleixner if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS)) 39443e9e705SThomas Gleixner return; 39543e9e705SThomas Gleixner 396aeeb5965SJiang Liu if (ops->get_hwirq == NULL) 397aeeb5965SJiang Liu ops->get_hwirq = msi_domain_ops_default.get_hwirq; 398aeeb5965SJiang Liu if (ops->msi_init == NULL) 399aeeb5965SJiang Liu ops->msi_init = msi_domain_ops_default.msi_init; 400aeeb5965SJiang Liu if (ops->msi_check == NULL) 401aeeb5965SJiang Liu ops->msi_check = msi_domain_ops_default.msi_check; 402aeeb5965SJiang Liu if (ops->msi_prepare == NULL) 403aeeb5965SJiang Liu ops->msi_prepare = msi_domain_ops_default.msi_prepare; 404aeeb5965SJiang Liu if (ops->set_desc == NULL) 405aeeb5965SJiang Liu ops->set_desc = msi_domain_ops_default.set_desc; 406aeeb5965SJiang Liu } 407aeeb5965SJiang Liu 408aeeb5965SJiang Liu static void msi_domain_update_chip_ops(struct msi_domain_info *info) 409aeeb5965SJiang Liu { 410aeeb5965SJiang Liu struct irq_chip *chip = info->chip; 411aeeb5965SJiang Liu 4120701c53eSMarc Zyngier BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask); 413aeeb5965SJiang Liu if (!chip->irq_set_affinity) 414aeeb5965SJiang Liu chip->irq_set_affinity = msi_domain_set_affinity; 415aeeb5965SJiang Liu } 416aeeb5965SJiang Liu 417f3cf8bb0SJiang Liu /** 4183b35e7e6SRandy Dunlap * msi_create_irq_domain - Create an MSI interrupt domain 419be5436c8SMarc Zyngier * @fwnode: Optional fwnode of the interrupt controller 420f3cf8bb0SJiang Liu * @info: MSI domain info 421f3cf8bb0SJiang Liu * @parent: Parent irq domain 4223b35e7e6SRandy Dunlap * 4233b35e7e6SRandy Dunlap * Return: pointer to the created &struct irq_domain or %NULL on failure 424f3cf8bb0SJiang Liu */ 425be5436c8SMarc Zyngier struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode, 426f3cf8bb0SJiang Liu struct msi_domain_info *info, 427f3cf8bb0SJiang Liu struct irq_domain *parent) 428f3cf8bb0SJiang Liu { 429a97b852bSMarc Zyngier struct irq_domain *domain; 430a97b852bSMarc Zyngier 431aeeb5965SJiang Liu msi_domain_update_dom_ops(info); 432aeeb5965SJiang Liu if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) 433aeeb5965SJiang Liu msi_domain_update_chip_ops(info); 434f3cf8bb0SJiang Liu 435a97b852bSMarc Zyngier domain = irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0, 43688156f00SEric Auger fwnode, &msi_domain_ops, info); 4370165308aSThomas Gleixner 4380165308aSThomas Gleixner if (domain && !domain->name && info->chip) 439a97b852bSMarc Zyngier domain->name = info->chip->name; 440a97b852bSMarc Zyngier 441a97b852bSMarc Zyngier return domain; 442f3cf8bb0SJiang Liu } 443f3cf8bb0SJiang Liu 444b2eba39bSMarc Zyngier int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev, 445b2eba39bSMarc Zyngier int nvec, msi_alloc_info_t *arg) 446b2eba39bSMarc Zyngier { 447b2eba39bSMarc Zyngier struct msi_domain_info *info = domain->host_data; 448b2eba39bSMarc Zyngier struct msi_domain_ops *ops = info->ops; 449b2eba39bSMarc Zyngier int ret; 450b2eba39bSMarc Zyngier 451b2eba39bSMarc Zyngier ret = ops->msi_check(domain, info, dev); 452b2eba39bSMarc Zyngier if (ret == 0) 453b2eba39bSMarc Zyngier ret = ops->msi_prepare(domain, dev, nvec, arg); 454b2eba39bSMarc Zyngier 455b2eba39bSMarc Zyngier return ret; 456b2eba39bSMarc Zyngier } 457b2eba39bSMarc Zyngier 4582145ac93SMarc Zyngier int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev, 4592145ac93SMarc Zyngier int virq, int nvec, msi_alloc_info_t *arg) 4602145ac93SMarc Zyngier { 4612145ac93SMarc Zyngier struct msi_domain_info *info = domain->host_data; 4622145ac93SMarc Zyngier struct msi_domain_ops *ops = info->ops; 4632145ac93SMarc Zyngier struct msi_desc *desc; 4642145ac93SMarc Zyngier int ret = 0; 4652145ac93SMarc Zyngier 4662145ac93SMarc Zyngier for_each_msi_entry(desc, dev) { 4672145ac93SMarc Zyngier /* Don't even try the multi-MSI brain damage. */ 4682145ac93SMarc Zyngier if (WARN_ON(!desc->irq || desc->nvec_used != 1)) { 4692145ac93SMarc Zyngier ret = -EINVAL; 4702145ac93SMarc Zyngier break; 4712145ac93SMarc Zyngier } 4722145ac93SMarc Zyngier 4732145ac93SMarc Zyngier if (!(desc->irq >= virq && desc->irq < (virq + nvec))) 4742145ac93SMarc Zyngier continue; 4752145ac93SMarc Zyngier 4762145ac93SMarc Zyngier ops->set_desc(arg, desc); 4772145ac93SMarc Zyngier /* Assumes the domain mutex is held! */ 478596a7a1dSJohn Keeping ret = irq_domain_alloc_irqs_hierarchy(domain, desc->irq, 1, 479596a7a1dSJohn Keeping arg); 4802145ac93SMarc Zyngier if (ret) 4812145ac93SMarc Zyngier break; 4822145ac93SMarc Zyngier 483596a7a1dSJohn Keeping irq_set_msi_desc_off(desc->irq, 0, desc); 4842145ac93SMarc Zyngier } 4852145ac93SMarc Zyngier 4862145ac93SMarc Zyngier if (ret) { 4872145ac93SMarc Zyngier /* Mop up the damage */ 4882145ac93SMarc Zyngier for_each_msi_entry(desc, dev) { 4892145ac93SMarc Zyngier if (!(desc->irq >= virq && desc->irq < (virq + nvec))) 4902145ac93SMarc Zyngier continue; 4912145ac93SMarc Zyngier 4922145ac93SMarc Zyngier irq_domain_free_irqs_common(domain, desc->irq, 1); 4932145ac93SMarc Zyngier } 4942145ac93SMarc Zyngier } 4952145ac93SMarc Zyngier 4962145ac93SMarc Zyngier return ret; 4972145ac93SMarc Zyngier } 4982145ac93SMarc Zyngier 499bc976233SThomas Gleixner /* 500bc976233SThomas Gleixner * Carefully check whether the device can use reservation mode. If 501bc976233SThomas Gleixner * reservation mode is enabled then the early activation will assign a 502bc976233SThomas Gleixner * dummy vector to the device. If the PCI/MSI device does not support 503bc976233SThomas Gleixner * masking of the entry then this can result in spurious interrupts when 504bc976233SThomas Gleixner * the device driver is not absolutely careful. But even then a malfunction 505bc976233SThomas Gleixner * of the hardware could result in a spurious interrupt on the dummy vector 506bc976233SThomas Gleixner * and render the device unusable. If the entry can be masked then the core 507bc976233SThomas Gleixner * logic will prevent the spurious interrupt and reservation mode can be 508bc976233SThomas Gleixner * used. For now reservation mode is restricted to PCI/MSI. 509bc976233SThomas Gleixner */ 510bc976233SThomas Gleixner static bool msi_check_reservation_mode(struct irq_domain *domain, 511bc976233SThomas Gleixner struct msi_domain_info *info, 512bc976233SThomas Gleixner struct device *dev) 513da5dd9e8SThomas Gleixner { 514bc976233SThomas Gleixner struct msi_desc *desc; 515bc976233SThomas Gleixner 516c6c9e283SThomas Gleixner switch(domain->bus_token) { 517c6c9e283SThomas Gleixner case DOMAIN_BUS_PCI_MSI: 518c6c9e283SThomas Gleixner case DOMAIN_BUS_VMD_MSI: 519c6c9e283SThomas Gleixner break; 520c6c9e283SThomas Gleixner default: 521bc976233SThomas Gleixner return false; 522c6c9e283SThomas Gleixner } 523bc976233SThomas Gleixner 524da5dd9e8SThomas Gleixner if (!(info->flags & MSI_FLAG_MUST_REACTIVATE)) 525da5dd9e8SThomas Gleixner return false; 526bc976233SThomas Gleixner 527bc976233SThomas Gleixner if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask) 528bc976233SThomas Gleixner return false; 529bc976233SThomas Gleixner 530bc976233SThomas Gleixner /* 531bc976233SThomas Gleixner * Checking the first MSI descriptor is sufficient. MSIX supports 532bc976233SThomas Gleixner * masking and MSI does so when the maskbit is set. 533bc976233SThomas Gleixner */ 534bc976233SThomas Gleixner desc = first_msi_entry(dev); 535bc976233SThomas Gleixner return desc->msi_attrib.is_msix || desc->msi_attrib.maskbit; 536da5dd9e8SThomas Gleixner } 537da5dd9e8SThomas Gleixner 53843e9e705SThomas Gleixner int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, 539d9109698SJiang Liu int nvec) 540d9109698SJiang Liu { 541d9109698SJiang Liu struct msi_domain_info *info = domain->host_data; 542d9109698SJiang Liu struct msi_domain_ops *ops = info->ops; 543da5dd9e8SThomas Gleixner struct irq_data *irq_data; 544d9109698SJiang Liu struct msi_desc *desc; 54506fde695SZenghui Yu msi_alloc_info_t arg = { }; 546b6140914SThomas Gleixner int i, ret, virq; 547da5dd9e8SThomas Gleixner bool can_reserve; 548d9109698SJiang Liu 549b2eba39bSMarc Zyngier ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg); 550d9109698SJiang Liu if (ret) 551d9109698SJiang Liu return ret; 552d9109698SJiang Liu 553d9109698SJiang Liu for_each_msi_entry(desc, dev) { 554d9109698SJiang Liu ops->set_desc(&arg, desc); 555d9109698SJiang Liu 556b6140914SThomas Gleixner virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used, 55706ee6d57SThomas Gleixner dev_to_node(dev), &arg, false, 5580972fa57SThomas Gleixner desc->affinity); 559d9109698SJiang Liu if (virq < 0) { 560d9109698SJiang Liu ret = -ENOSPC; 561d9109698SJiang Liu if (ops->handle_error) 562d9109698SJiang Liu ret = ops->handle_error(domain, desc, ret); 563d9109698SJiang Liu if (ops->msi_finish) 564d9109698SJiang Liu ops->msi_finish(&arg, ret); 565d9109698SJiang Liu return ret; 566d9109698SJiang Liu } 567d9109698SJiang Liu 56807557ccbSThomas Gleixner for (i = 0; i < desc->nvec_used; i++) { 569d9109698SJiang Liu irq_set_msi_desc_off(virq, i, desc); 57007557ccbSThomas Gleixner irq_debugfs_copy_devname(virq + i, dev); 57107557ccbSThomas Gleixner } 572d9109698SJiang Liu } 573d9109698SJiang Liu 574d9109698SJiang Liu if (ops->msi_finish) 575d9109698SJiang Liu ops->msi_finish(&arg, 0); 576d9109698SJiang Liu 577bc976233SThomas Gleixner can_reserve = msi_check_reservation_mode(domain, info, dev); 578da5dd9e8SThomas Gleixner 579f3b0946dSMarc Zyngier /* 580f3b0946dSMarc Zyngier * This flag is set by the PCI layer as we need to activate 581f3b0946dSMarc Zyngier * the MSI entries before the PCI layer enables MSI in the 582f3b0946dSMarc Zyngier * card. Otherwise the card latches a random msi message. 583f3b0946dSMarc Zyngier */ 584da5dd9e8SThomas Gleixner if (!(info->flags & MSI_FLAG_ACTIVATE_EARLY)) 5854c457e8cSMarc Zyngier goto skip_activate; 586f3b0946dSMarc Zyngier 5874c457e8cSMarc Zyngier for_each_msi_vector(desc, i, dev) { 5884c457e8cSMarc Zyngier if (desc->irq == i) { 5894c457e8cSMarc Zyngier virq = desc->irq; 5904c457e8cSMarc Zyngier dev_dbg(dev, "irq [%d-%d] for MSI\n", 5914c457e8cSMarc Zyngier virq, virq + desc->nvec_used - 1); 5924c457e8cSMarc Zyngier } 5934c457e8cSMarc Zyngier 5944c457e8cSMarc Zyngier irq_data = irq_domain_get_irq_data(domain, i); 5956f1a4891SThomas Gleixner if (!can_reserve) { 596bc976233SThomas Gleixner irqd_clr_can_reserve(irq_data); 5976f1a4891SThomas Gleixner if (domain->flags & IRQ_DOMAIN_MSI_NOMASK_QUIRK) 5986f1a4891SThomas Gleixner irqd_set_msi_nomask_quirk(irq_data); 5996f1a4891SThomas Gleixner } 600bc976233SThomas Gleixner ret = irq_domain_activate_irq(irq_data, can_reserve); 601bb9b428aSThomas Gleixner if (ret) 602bb9b428aSThomas Gleixner goto cleanup; 603da5dd9e8SThomas Gleixner } 604da5dd9e8SThomas Gleixner 6054c457e8cSMarc Zyngier skip_activate: 606da5dd9e8SThomas Gleixner /* 607da5dd9e8SThomas Gleixner * If these interrupts use reservation mode, clear the activated bit 608da5dd9e8SThomas Gleixner * so request_irq() will assign the final vector. 609da5dd9e8SThomas Gleixner */ 610da5dd9e8SThomas Gleixner if (can_reserve) { 6114c457e8cSMarc Zyngier for_each_msi_vector(desc, i, dev) { 6124c457e8cSMarc Zyngier irq_data = irq_domain_get_irq_data(domain, i); 61322d0b12fSThomas Gleixner irqd_clr_activated(irq_data); 614f3b0946dSMarc Zyngier } 615d9109698SJiang Liu } 616d9109698SJiang Liu return 0; 617bb9b428aSThomas Gleixner 618bb9b428aSThomas Gleixner cleanup: 6194c457e8cSMarc Zyngier for_each_msi_vector(desc, i, dev) { 6204c457e8cSMarc Zyngier irq_data = irq_domain_get_irq_data(domain, i); 6214c457e8cSMarc Zyngier if (irqd_is_activated(irq_data)) 6224c457e8cSMarc Zyngier irq_domain_deactivate_irq(irq_data); 623bb9b428aSThomas Gleixner } 624bb9b428aSThomas Gleixner msi_domain_free_irqs(domain, dev); 625bb9b428aSThomas Gleixner return ret; 626d9109698SJiang Liu } 627d9109698SJiang Liu 628d9109698SJiang Liu /** 62943e9e705SThomas Gleixner * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain 63043e9e705SThomas Gleixner * @domain: The domain to allocate from 631d9109698SJiang Liu * @dev: Pointer to device struct of the device for which the interrupts 63243e9e705SThomas Gleixner * are allocated 63343e9e705SThomas Gleixner * @nvec: The number of interrupts to allocate 63443e9e705SThomas Gleixner * 6353b35e7e6SRandy Dunlap * Return: %0 on success or an error code. 636d9109698SJiang Liu */ 63743e9e705SThomas Gleixner int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, 63843e9e705SThomas Gleixner int nvec) 63943e9e705SThomas Gleixner { 64043e9e705SThomas Gleixner struct msi_domain_info *info = domain->host_data; 64143e9e705SThomas Gleixner struct msi_domain_ops *ops = info->ops; 64243e9e705SThomas Gleixner 64343e9e705SThomas Gleixner return ops->domain_alloc_irqs(domain, dev, nvec); 64443e9e705SThomas Gleixner } 64543e9e705SThomas Gleixner 64643e9e705SThomas Gleixner void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev) 647d9109698SJiang Liu { 648d9109698SJiang Liu struct msi_desc *desc; 649d9109698SJiang Liu 650d9109698SJiang Liu for_each_msi_entry(desc, dev) { 651fe0c52fcSMarc Zyngier /* 652fe0c52fcSMarc Zyngier * We might have failed to allocate an MSI early 653fe0c52fcSMarc Zyngier * enough that there is no IRQ associated to this 654fe0c52fcSMarc Zyngier * entry. If that's the case, don't do anything. 655fe0c52fcSMarc Zyngier */ 656fe0c52fcSMarc Zyngier if (desc->irq) { 657d9109698SJiang Liu irq_domain_free_irqs(desc->irq, desc->nvec_used); 658d9109698SJiang Liu desc->irq = 0; 659d9109698SJiang Liu } 660d9109698SJiang Liu } 661fe0c52fcSMarc Zyngier } 662d9109698SJiang Liu 663d9109698SJiang Liu /** 6643b35e7e6SRandy Dunlap * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated to @dev 66543e9e705SThomas Gleixner * @domain: The domain to managing the interrupts 66643e9e705SThomas Gleixner * @dev: Pointer to device struct of the device for which the interrupts 66743e9e705SThomas Gleixner * are free 66843e9e705SThomas Gleixner */ 66943e9e705SThomas Gleixner void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev) 67043e9e705SThomas Gleixner { 67143e9e705SThomas Gleixner struct msi_domain_info *info = domain->host_data; 67243e9e705SThomas Gleixner struct msi_domain_ops *ops = info->ops; 67343e9e705SThomas Gleixner 67443e9e705SThomas Gleixner return ops->domain_free_irqs(domain, dev); 67543e9e705SThomas Gleixner } 67643e9e705SThomas Gleixner 67743e9e705SThomas Gleixner /** 678f3cf8bb0SJiang Liu * msi_get_domain_info - Get the MSI interrupt domain info for @domain 679f3cf8bb0SJiang Liu * @domain: The interrupt domain to retrieve data from 680f3cf8bb0SJiang Liu * 6813b35e7e6SRandy Dunlap * Return: the pointer to the msi_domain_info stored in @domain->host_data. 682f3cf8bb0SJiang Liu */ 683f3cf8bb0SJiang Liu struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain) 684f3cf8bb0SJiang Liu { 685f3cf8bb0SJiang Liu return (struct msi_domain_info *)domain->host_data; 686f3cf8bb0SJiang Liu } 687f3cf8bb0SJiang Liu 688f3cf8bb0SJiang Liu #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */ 689