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 | |
Michael Bohan | cf68d7a | 2012-01-11 17:53:05 -0800 | [diff] [blame] | 146 | /** |
| 147 | * irq_domain_find_free_range() - Find an available irq range |
| 148 | * @from: lowest logical irq number to request from |
| 149 | * @cnt: number of interrupts to search for |
| 150 | * |
| 151 | * Finds an available logical irq range from the domains specified |
| 152 | * on the system. The from parameter can be used to allocate a range |
| 153 | * at least as great as the specified irq number. |
| 154 | */ |
| 155 | int irq_domain_find_free_range(unsigned int from, unsigned int cnt) |
| 156 | { |
| 157 | struct irq_domain *curr, *prev = NULL; |
| 158 | |
| 159 | if (list_empty(&irq_domain_list)) |
| 160 | return from; |
| 161 | |
| 162 | list_for_each_entry(curr, &irq_domain_list, list) { |
| 163 | if (prev == NULL) { |
| 164 | if ((from + cnt - 1) < curr->irq_base) |
| 165 | return from; |
| 166 | } else { |
| 167 | uint32_t p_next_irq = prev->irq_base + prev->nr_irq; |
| 168 | uint32_t start_irq; |
| 169 | if (from >= curr->irq_base) |
| 170 | continue; |
| 171 | if (from < p_next_irq) |
| 172 | start_irq = p_next_irq; |
| 173 | else |
| 174 | start_irq = from; |
| 175 | if ((curr->irq_base - start_irq) >= cnt) |
| 176 | return p_next_irq; |
| 177 | } |
| 178 | prev = curr; |
| 179 | } |
| 180 | curr = list_entry(curr->list.prev, struct irq_domain, list); |
| 181 | |
| 182 | return from > curr->irq_base + curr->nr_irq ? |
| 183 | from : curr->irq_base + curr->nr_irq; |
| 184 | } |
| 185 | |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 186 | #if defined(CONFIG_OF_IRQ) |
| 187 | /** |
| 188 | * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec |
| 189 | * |
| 190 | * Used by the device tree interrupt mapping code to translate a device tree |
| 191 | * interrupt specifier to a valid linux irq number. Returns either a valid |
| 192 | * linux IRQ number or 0. |
| 193 | * |
| 194 | * When the caller no longer need the irq number returned by this function it |
| 195 | * should arrange to call irq_dispose_mapping(). |
| 196 | */ |
| 197 | unsigned int irq_create_of_mapping(struct device_node *controller, |
| 198 | const u32 *intspec, unsigned int intsize) |
| 199 | { |
| 200 | struct irq_domain *domain; |
| 201 | unsigned long hwirq; |
| 202 | unsigned int irq, type; |
| 203 | int rc = -EINVAL; |
| 204 | |
| 205 | /* Find a domain which can translate the irq spec */ |
| 206 | mutex_lock(&irq_domain_mutex); |
| 207 | list_for_each_entry(domain, &irq_domain_list, list) { |
| 208 | if (!domain->ops->dt_translate) |
| 209 | continue; |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame] | 210 | |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 211 | rc = domain->ops->dt_translate(domain, controller, |
| 212 | intspec, intsize, &hwirq, &type); |
| 213 | if (rc == 0) |
| 214 | break; |
| 215 | } |
| 216 | mutex_unlock(&irq_domain_mutex); |
| 217 | |
| 218 | if (rc != 0) |
| 219 | return 0; |
| 220 | |
| 221 | irq = irq_domain_to_irq(domain, hwirq); |
| 222 | if (type != IRQ_TYPE_NONE) |
| 223 | irq_set_irq_type(irq, type); |
| 224 | pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n", |
| 225 | controller->full_name, (int)hwirq, irq, type); |
| 226 | return irq; |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(irq_create_of_mapping); |
| 229 | |
| 230 | /** |
| 231 | * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping() |
| 232 | * @irq: linux irq number to be discarded |
| 233 | * |
| 234 | * Calling this function indicates the caller no longer needs a reference to |
| 235 | * the linux irq number returned by a prior call to irq_create_of_mapping(). |
| 236 | */ |
| 237 | void irq_dispose_mapping(unsigned int irq) |
| 238 | { |
| 239 | /* |
| 240 | * nothing yet; will be filled when support for dynamic allocation of |
| 241 | * irq_descs is added to irq_domain |
| 242 | */ |
| 243 | } |
| 244 | EXPORT_SYMBOL_GPL(irq_dispose_mapping); |
Grant Likely | 971a0ee | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 245 | |
| 246 | int irq_domain_simple_dt_translate(struct irq_domain *d, |
| 247 | struct device_node *controller, |
| 248 | const u32 *intspec, unsigned int intsize, |
| 249 | unsigned long *out_hwirq, unsigned int *out_type) |
| 250 | { |
| 251 | if (d->of_node != controller) |
| 252 | return -EINVAL; |
| 253 | if (intsize < 1) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | *out_hwirq = intspec[0]; |
| 257 | *out_type = IRQ_TYPE_NONE; |
| 258 | if (intsize > 1) |
| 259 | *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | struct irq_domain_ops irq_domain_simple_ops = { |
| 264 | .dt_translate = irq_domain_simple_dt_translate, |
| 265 | }; |
| 266 | EXPORT_SYMBOL_GPL(irq_domain_simple_ops); |
| 267 | |
| 268 | /** |
| 269 | * irq_domain_create_simple() - Set up a 'simple' translation range |
| 270 | */ |
| 271 | void irq_domain_add_simple(struct device_node *controller, int irq_base) |
| 272 | { |
| 273 | struct irq_domain *domain; |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame] | 274 | int rc; |
Grant Likely | 971a0ee | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 275 | |
| 276 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
| 277 | if (!domain) { |
| 278 | WARN_ON(1); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | domain->irq_base = irq_base; |
| 283 | domain->of_node = of_node_get(controller); |
| 284 | domain->ops = &irq_domain_simple_ops; |
Michael Bohan | 6f4df41 | 2012-01-11 11:45:16 -0800 | [diff] [blame] | 285 | rc = irq_domain_add(domain); |
| 286 | if (rc) { |
| 287 | WARN(1, "Unable to create irq domain\n"); |
| 288 | return; |
| 289 | } |
Michael Bohan | e6717fd | 2012-01-03 16:37:43 -0800 | [diff] [blame] | 290 | irq_domain_register(domain); |
Grant Likely | 971a0ee | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 291 | } |
| 292 | EXPORT_SYMBOL_GPL(irq_domain_add_simple); |
| 293 | |
| 294 | void irq_domain_generate_simple(const struct of_device_id *match, |
| 295 | u64 phys_base, unsigned int irq_start) |
| 296 | { |
| 297 | struct device_node *node; |
| 298 | pr_info("looking for phys_base=%llx, irq_start=%i\n", |
| 299 | (unsigned long long) phys_base, (int) irq_start); |
| 300 | node = of_find_matching_node_by_address(NULL, match, phys_base); |
| 301 | if (node) |
| 302 | irq_domain_add_simple(node, irq_start); |
| 303 | else |
| 304 | pr_info("no node found\n"); |
| 305 | } |
| 306 | EXPORT_SYMBOL_GPL(irq_domain_generate_simple); |
Grant Likely | 6469dfb | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 307 | #endif /* CONFIG_OF_IRQ */ |