Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 1 | #include <linux/irq.h> |
| 2 | #include <linux/irqdomain.h> |
| 3 | #include <linux/module.h> |
| 4 | #include <linux/mutex.h> |
| 5 | #include <linux/of.h> |
Grant Likely | 971a0ee | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 6 | #include <linux/of_address.h> |
| 7 | #include <linux/slab.h> |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 8 | |
| 9 | static LIST_HEAD(irq_domain_list); |
| 10 | static DEFINE_MUTEX(irq_domain_mutex); |
| 11 | |
| 12 | /** |
| 13 | * irq_domain_add() - Register an irq_domain |
| 14 | * @domain: ptr to initialized irq_domain structure |
| 15 | * |
Michael Bohan | e6717fd | 2012-01-03 16:37:43 -0800 | [diff] [blame] | 16 | * Adds a irq_domain structure. The irq_domain must at a minimum be |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 17 | * initialized with an ops structure pointer, and either a ->to_irq hook or |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame^] | 18 | * a valid irq_base value. The irq range must be mutually exclusive with |
| 19 | * domains already registered. Everything else is optional. |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 20 | */ |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame^] | 21 | int irq_domain_add(struct irq_domain *domain) |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 22 | { |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame^] | 23 | struct irq_domain *curr; |
| 24 | uint32_t d_highirq = domain->irq_base + domain->nr_irq - 1; |
| 25 | |
| 26 | if (!domain->nr_irq) |
| 27 | return -EINVAL; |
| 28 | |
Michael Bohan | e6717fd | 2012-01-03 16:37:43 -0800 | [diff] [blame] | 29 | mutex_lock(&irq_domain_mutex); |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame^] | 30 | /* insert in ascending order of domain->irq_base */ |
| 31 | list_for_each_entry(curr, &irq_domain_list, list) { |
| 32 | uint32_t c_highirq = curr->irq_base + curr->nr_irq - 1; |
| 33 | if (domain->irq_base < curr->irq_base && |
| 34 | d_highirq < curr->irq_base) { |
| 35 | break; |
| 36 | } |
| 37 | if (d_highirq <= c_highirq) { |
| 38 | mutex_unlock(&irq_domain_mutex); |
| 39 | return -EINVAL; |
| 40 | } |
| 41 | } |
| 42 | list_add_tail(&domain->list, &curr->list); |
Michael Bohan | e6717fd | 2012-01-03 16:37:43 -0800 | [diff] [blame] | 43 | mutex_unlock(&irq_domain_mutex); |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame^] | 44 | |
| 45 | return 0; |
Michael Bohan | e6717fd | 2012-01-03 16:37:43 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
| 49 | * irq_domain_register() - Register an entire irq_domain |
| 50 | * @domain: ptr to initialized irq_domain structure |
| 51 | * |
| 52 | * Registers the entire irq_domain. The irq_domain must at a minimum be |
| 53 | * initialized with an ops structure pointer, and either a ->to_irq hook or |
| 54 | * a valid irq_base value. Everything else is optional. |
| 55 | */ |
| 56 | void irq_domain_register(struct irq_domain *domain) |
| 57 | { |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 58 | struct irq_data *d; |
Rob Herring | 60fe460 | 2011-09-30 10:48:38 -0500 | [diff] [blame] | 59 | int hwirq, irq; |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 60 | |
Rob Herring | 60fe460 | 2011-09-30 10:48:38 -0500 | [diff] [blame] | 61 | irq_domain_for_each_irq(domain, hwirq, irq) { |
| 62 | d = irq_get_irq_data(irq); |
Rob Herring | 36b2c36 | 2011-09-14 11:31:37 -0500 | [diff] [blame] | 63 | if (!d) { |
| 64 | WARN(1, "error: assigning domain to non existant irq_desc"); |
| 65 | return; |
| 66 | } |
| 67 | if (d->domain) { |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 68 | /* things are broken; just report, don't clean up */ |
| 69 | WARN(1, "error: irq_desc already assigned to a domain"); |
| 70 | return; |
| 71 | } |
| 72 | d->domain = domain; |
| 73 | d->hwirq = hwirq; |
| 74 | } |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
Michael Bohan | e6717fd | 2012-01-03 16:37:43 -0800 | [diff] [blame] | 78 | * irq_domain_register_irq() - Register an irq_domain |
| 79 | * @domain: ptr to initialized irq_domain structure |
| 80 | * @hwirq: irq_domain hwirq to register |
| 81 | * |
| 82 | * Registers a specific hwirq within the irq_domain. The irq_domain |
| 83 | * must at a minimum be initialized with an ops structure pointer, and |
| 84 | * either a ->to_irq hook or a valid irq_base value. Everything else is |
| 85 | * optional. |
| 86 | */ |
| 87 | void irq_domain_register_irq(struct irq_domain *domain, int hwirq) |
| 88 | { |
| 89 | struct irq_data *d; |
| 90 | |
| 91 | d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq)); |
| 92 | if (!d) { |
| 93 | WARN(1, "error: assigning domain to non existant irq_desc"); |
| 94 | return; |
| 95 | } |
| 96 | if (d->domain) { |
| 97 | /* things are broken; just report, don't clean up */ |
| 98 | WARN(1, "error: irq_desc already assigned to a domain"); |
| 99 | return; |
| 100 | } |
| 101 | d->domain = domain; |
| 102 | d->hwirq = hwirq; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * irq_domain_del() - Removes a irq_domain from the system |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 107 | * @domain: ptr to registered irq_domain. |
| 108 | */ |
| 109 | void irq_domain_del(struct irq_domain *domain) |
| 110 | { |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 111 | mutex_lock(&irq_domain_mutex); |
| 112 | list_del(&domain->list); |
| 113 | mutex_unlock(&irq_domain_mutex); |
Michael Bohan | e6717fd | 2012-01-03 16:37:43 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * irq_domain_unregister() - Unregister an irq_domain |
| 118 | * @domain: ptr to registered irq_domain. |
| 119 | */ |
| 120 | void irq_domain_unregister(struct irq_domain *domain) |
| 121 | { |
| 122 | struct irq_data *d; |
| 123 | int hwirq, irq; |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 124 | |
| 125 | /* Clear the irq_domain assignments */ |
Rob Herring | 60fe460 | 2011-09-30 10:48:38 -0500 | [diff] [blame] | 126 | irq_domain_for_each_irq(domain, hwirq, irq) { |
| 127 | d = irq_get_irq_data(irq); |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 128 | d->domain = NULL; |
| 129 | } |
| 130 | } |
| 131 | |
Michael Bohan | e6717fd | 2012-01-03 16:37:43 -0800 | [diff] [blame] | 132 | /** |
| 133 | * irq_domain_unregister_irq() - Unregister a hwirq within a irq_domain |
| 134 | * @domain: ptr to registered irq_domain. |
| 135 | * @hwirq: irq_domain hwirq to unregister. |
| 136 | */ |
| 137 | void irq_domain_unregister_irq(struct irq_domain *domain, int hwirq) |
| 138 | { |
| 139 | struct irq_data *d; |
| 140 | |
| 141 | /* Clear the irq_domain assignment */ |
| 142 | d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq)); |
| 143 | d->domain = NULL; |
| 144 | } |
| 145 | |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 146 | #if defined(CONFIG_OF_IRQ) |
| 147 | /** |
| 148 | * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec |
| 149 | * |
| 150 | * Used by the device tree interrupt mapping code to translate a device tree |
| 151 | * interrupt specifier to a valid linux irq number. Returns either a valid |
| 152 | * linux IRQ number or 0. |
| 153 | * |
| 154 | * When the caller no longer need the irq number returned by this function it |
| 155 | * should arrange to call irq_dispose_mapping(). |
| 156 | */ |
| 157 | unsigned int irq_create_of_mapping(struct device_node *controller, |
| 158 | const u32 *intspec, unsigned int intsize) |
| 159 | { |
| 160 | struct irq_domain *domain; |
| 161 | unsigned long hwirq; |
| 162 | unsigned int irq, type; |
| 163 | int rc = -EINVAL; |
| 164 | |
| 165 | /* Find a domain which can translate the irq spec */ |
| 166 | mutex_lock(&irq_domain_mutex); |
| 167 | list_for_each_entry(domain, &irq_domain_list, list) { |
| 168 | if (!domain->ops->dt_translate) |
| 169 | continue; |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame^] | 170 | |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 171 | rc = domain->ops->dt_translate(domain, controller, |
| 172 | intspec, intsize, &hwirq, &type); |
| 173 | if (rc == 0) |
| 174 | break; |
| 175 | } |
| 176 | mutex_unlock(&irq_domain_mutex); |
| 177 | |
| 178 | if (rc != 0) |
| 179 | return 0; |
| 180 | |
| 181 | irq = irq_domain_to_irq(domain, hwirq); |
| 182 | if (type != IRQ_TYPE_NONE) |
| 183 | irq_set_irq_type(irq, type); |
| 184 | pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n", |
| 185 | controller->full_name, (int)hwirq, irq, type); |
| 186 | return irq; |
| 187 | } |
| 188 | EXPORT_SYMBOL_GPL(irq_create_of_mapping); |
| 189 | |
| 190 | /** |
| 191 | * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping() |
| 192 | * @irq: linux irq number to be discarded |
| 193 | * |
| 194 | * Calling this function indicates the caller no longer needs a reference to |
| 195 | * the linux irq number returned by a prior call to irq_create_of_mapping(). |
| 196 | */ |
| 197 | void irq_dispose_mapping(unsigned int irq) |
| 198 | { |
| 199 | /* |
| 200 | * nothing yet; will be filled when support for dynamic allocation of |
| 201 | * irq_descs is added to irq_domain |
| 202 | */ |
| 203 | } |
| 204 | EXPORT_SYMBOL_GPL(irq_dispose_mapping); |
Grant Likely | 971a0ee | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 205 | |
| 206 | int irq_domain_simple_dt_translate(struct irq_domain *d, |
| 207 | struct device_node *controller, |
| 208 | const u32 *intspec, unsigned int intsize, |
| 209 | unsigned long *out_hwirq, unsigned int *out_type) |
| 210 | { |
| 211 | if (d->of_node != controller) |
| 212 | return -EINVAL; |
| 213 | if (intsize < 1) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | *out_hwirq = intspec[0]; |
| 217 | *out_type = IRQ_TYPE_NONE; |
| 218 | if (intsize > 1) |
| 219 | *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | struct irq_domain_ops irq_domain_simple_ops = { |
| 224 | .dt_translate = irq_domain_simple_dt_translate, |
| 225 | }; |
| 226 | EXPORT_SYMBOL_GPL(irq_domain_simple_ops); |
| 227 | |
| 228 | /** |
| 229 | * irq_domain_create_simple() - Set up a 'simple' translation range |
| 230 | */ |
| 231 | void irq_domain_add_simple(struct device_node *controller, int irq_base) |
| 232 | { |
| 233 | struct irq_domain *domain; |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame^] | 234 | int rc; |
Grant Likely | 971a0ee | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 235 | |
| 236 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
| 237 | if (!domain) { |
| 238 | WARN_ON(1); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | domain->irq_base = irq_base; |
| 243 | domain->of_node = of_node_get(controller); |
| 244 | domain->ops = &irq_domain_simple_ops; |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame^] | 245 | rc = irq_domain_add(domain); |
| 246 | if (rc) { |
| 247 | WARN(1, "Unable to create irq domain\n"); |
| 248 | return; |
| 249 | } |
Michael Bohan | e6717fd | 2012-01-03 16:37:43 -0800 | [diff] [blame] | 250 | irq_domain_register(domain); |
Grant Likely | 971a0ee | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 251 | } |
| 252 | EXPORT_SYMBOL_GPL(irq_domain_add_simple); |
| 253 | |
| 254 | void irq_domain_generate_simple(const struct of_device_id *match, |
| 255 | u64 phys_base, unsigned int irq_start) |
| 256 | { |
| 257 | struct device_node *node; |
| 258 | pr_info("looking for phys_base=%llx, irq_start=%i\n", |
| 259 | (unsigned long long) phys_base, (int) irq_start); |
| 260 | node = of_find_matching_node_by_address(NULL, match, phys_base); |
| 261 | if (node) |
| 262 | irq_domain_add_simple(node, irq_start); |
| 263 | else |
| 264 | pr_info("no node found\n"); |
| 265 | } |
| 266 | EXPORT_SYMBOL_GPL(irq_domain_generate_simple); |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 267 | #endif /* CONFIG_OF_IRQ */ |