Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/irq/msi.c |
| 3 | * |
| 4 | * Copyright (C) 2014 Intel Corp. |
| 5 | * Author: Jiang Liu <jiang.liu@linux.intel.com> |
| 6 | * |
| 7 | * This file is licensed under GPLv2. |
| 8 | * |
| 9 | * This file contains common code to support Message Signalled Interrupt for |
| 10 | * PCI compatible and non PCI compatible devices. |
| 11 | */ |
| 12 | #include <linux/irq.h> |
| 13 | #include <linux/irqdomain.h> |
| 14 | #include <linux/msi.h> |
| 15 | |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame^] | 16 | /* Temparory solution for building, will be removed later */ |
| 17 | #include <linux/pci.h> |
| 18 | |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 19 | #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN |
| 20 | /** |
| 21 | * msi_domain_set_affinity - Generic affinity setter function for MSI domains |
| 22 | * @irq_data: The irq data associated to the interrupt |
| 23 | * @mask: The affinity mask to set |
| 24 | * @force: Flag to enforce setting (disable online checks) |
| 25 | * |
| 26 | * Intended to be used by MSI interrupt controllers which are |
| 27 | * implemented with hierarchical domains. |
| 28 | */ |
| 29 | int msi_domain_set_affinity(struct irq_data *irq_data, |
| 30 | const struct cpumask *mask, bool force) |
| 31 | { |
| 32 | struct irq_data *parent = irq_data->parent_data; |
| 33 | struct msi_msg msg; |
| 34 | int ret; |
| 35 | |
| 36 | ret = parent->chip->irq_set_affinity(parent, mask, force); |
| 37 | if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) { |
| 38 | BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg)); |
| 39 | irq_chip_write_msi_msg(irq_data, &msg); |
| 40 | } |
| 41 | |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | static void msi_domain_activate(struct irq_domain *domain, |
| 46 | struct irq_data *irq_data) |
| 47 | { |
| 48 | struct msi_msg msg; |
| 49 | |
| 50 | BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg)); |
| 51 | irq_chip_write_msi_msg(irq_data, &msg); |
| 52 | } |
| 53 | |
| 54 | static void msi_domain_deactivate(struct irq_domain *domain, |
| 55 | struct irq_data *irq_data) |
| 56 | { |
| 57 | struct msi_msg msg; |
| 58 | |
| 59 | memset(&msg, 0, sizeof(msg)); |
| 60 | irq_chip_write_msi_msg(irq_data, &msg); |
| 61 | } |
| 62 | |
| 63 | static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 64 | unsigned int nr_irqs, void *arg) |
| 65 | { |
| 66 | struct msi_domain_info *info = domain->host_data; |
| 67 | struct msi_domain_ops *ops = info->ops; |
| 68 | irq_hw_number_t hwirq = ops->get_hwirq(info, arg); |
| 69 | int i, ret; |
| 70 | |
| 71 | if (irq_find_mapping(domain, hwirq) > 0) |
| 72 | return -EEXIST; |
| 73 | |
| 74 | ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg); |
| 75 | if (ret < 0) |
| 76 | return ret; |
| 77 | |
| 78 | for (i = 0; i < nr_irqs; i++) { |
| 79 | ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg); |
| 80 | if (ret < 0) { |
| 81 | if (ops->msi_free) { |
| 82 | for (i--; i > 0; i--) |
| 83 | ops->msi_free(domain, info, virq + i); |
| 84 | } |
| 85 | irq_domain_free_irqs_top(domain, virq, nr_irqs); |
| 86 | return ret; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static void msi_domain_free(struct irq_domain *domain, unsigned int virq, |
| 94 | unsigned int nr_irqs) |
| 95 | { |
| 96 | struct msi_domain_info *info = domain->host_data; |
| 97 | int i; |
| 98 | |
| 99 | if (info->ops->msi_free) { |
| 100 | for (i = 0; i < nr_irqs; i++) |
| 101 | info->ops->msi_free(domain, info, virq + i); |
| 102 | } |
| 103 | irq_domain_free_irqs_top(domain, virq, nr_irqs); |
| 104 | } |
| 105 | |
| 106 | static struct irq_domain_ops msi_domain_ops = { |
| 107 | .alloc = msi_domain_alloc, |
| 108 | .free = msi_domain_free, |
| 109 | .activate = msi_domain_activate, |
| 110 | .deactivate = msi_domain_deactivate, |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * msi_create_irq_domain - Create a MSI interrupt domain |
| 115 | * @of_node: Optional device-tree node of the interrupt controller |
| 116 | * @info: MSI domain info |
| 117 | * @parent: Parent irq domain |
| 118 | */ |
| 119 | struct irq_domain *msi_create_irq_domain(struct device_node *of_node, |
| 120 | struct msi_domain_info *info, |
| 121 | struct irq_domain *parent) |
| 122 | { |
| 123 | struct irq_domain *domain; |
| 124 | |
| 125 | domain = irq_domain_add_tree(of_node, &msi_domain_ops, info); |
| 126 | if (domain) |
| 127 | domain->parent = parent; |
| 128 | |
| 129 | return domain; |
| 130 | } |
| 131 | |
| 132 | /** |
Jiang Liu | d910969 | 2014-11-15 22:24:04 +0800 | [diff] [blame^] | 133 | * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain |
| 134 | * @domain: The domain to allocate from |
| 135 | * @dev: Pointer to device struct of the device for which the interrupts |
| 136 | * are allocated |
| 137 | * @nvec: The number of interrupts to allocate |
| 138 | * |
| 139 | * Returns 0 on success or an error code. |
| 140 | */ |
| 141 | int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, |
| 142 | int nvec) |
| 143 | { |
| 144 | struct msi_domain_info *info = domain->host_data; |
| 145 | struct msi_domain_ops *ops = info->ops; |
| 146 | msi_alloc_info_t arg; |
| 147 | struct msi_desc *desc; |
| 148 | int i, ret, virq = -1; |
| 149 | |
| 150 | ret = ops->msi_check(domain, info, dev); |
| 151 | if (ret == 0) |
| 152 | ret = ops->msi_prepare(domain, dev, nvec, &arg); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | for_each_msi_entry(desc, dev) { |
| 157 | ops->set_desc(&arg, desc); |
| 158 | |
| 159 | virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used, |
| 160 | dev_to_node(dev), &arg, false); |
| 161 | if (virq < 0) { |
| 162 | ret = -ENOSPC; |
| 163 | if (ops->handle_error) |
| 164 | ret = ops->handle_error(domain, desc, ret); |
| 165 | if (ops->msi_finish) |
| 166 | ops->msi_finish(&arg, ret); |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | for (i = 0; i < desc->nvec_used; i++) |
| 171 | irq_set_msi_desc_off(virq, i, desc); |
| 172 | } |
| 173 | |
| 174 | if (ops->msi_finish) |
| 175 | ops->msi_finish(&arg, 0); |
| 176 | |
| 177 | for_each_msi_entry(desc, dev) { |
| 178 | if (desc->nvec_used == 1) |
| 179 | dev_dbg(dev, "irq %d for MSI\n", virq); |
| 180 | else |
| 181 | dev_dbg(dev, "irq [%d-%d] for MSI\n", |
| 182 | virq, virq + desc->nvec_used - 1); |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated tp @dev |
| 190 | * @domain: The domain to managing the interrupts |
| 191 | * @dev: Pointer to device struct of the device for which the interrupts |
| 192 | * are free |
| 193 | */ |
| 194 | void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev) |
| 195 | { |
| 196 | struct msi_desc *desc; |
| 197 | |
| 198 | for_each_msi_entry(desc, dev) { |
| 199 | irq_domain_free_irqs(desc->irq, desc->nvec_used); |
| 200 | desc->irq = 0; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
Jiang Liu | f3cf8bb | 2014-11-12 11:39:03 +0100 | [diff] [blame] | 205 | * msi_get_domain_info - Get the MSI interrupt domain info for @domain |
| 206 | * @domain: The interrupt domain to retrieve data from |
| 207 | * |
| 208 | * Returns the pointer to the msi_domain_info stored in |
| 209 | * @domain->host_data. |
| 210 | */ |
| 211 | struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain) |
| 212 | { |
| 213 | return (struct msi_domain_info *)domain->host_data; |
| 214 | } |
| 215 | |
| 216 | #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */ |