blob: 8847f277a14ffd5f14ab276626b6e46353300447 [file] [log] [blame]
Thomas Gleixner3795de22010-09-22 17:09:43 +02001/*
2 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
3 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
4 *
5 * This file contains the interrupt descriptor management code
6 *
7 * Detailed information is available in Documentation/DocBook/genericirq
8 *
9 */
10#include <linux/irq.h>
11#include <linux/slab.h>
Paul Gortmakerec53cf22011-09-19 20:33:19 -040012#include <linux/export.h>
Thomas Gleixner3795de22010-09-22 17:09:43 +020013#include <linux/interrupt.h>
14#include <linux/kernel_stat.h>
15#include <linux/radix-tree.h>
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020016#include <linux/bitmap.h>
Marc Zyngier76ba59f2014-08-26 11:03:16 +010017#include <linux/irqdomain.h>
Craig Gallekecb3f392016-09-13 12:14:51 -040018#include <linux/sysfs.h>
Thomas Gleixner3795de22010-09-22 17:09:43 +020019
20#include "internals.h"
21
22/*
23 * lockdep: we want to handle all irq_desc locks as a single lock-class:
24 */
Thomas Gleixner78f90d92010-09-29 17:18:47 +020025static struct lock_class_key irq_desc_lock_class;
Thomas Gleixner3795de22010-09-22 17:09:43 +020026
Thomas Gleixnerfe051432011-05-18 12:53:03 +020027#if defined(CONFIG_SMP)
Thomas Gleixnerfbf19802016-02-03 19:52:23 +010028static int __init irq_affinity_setup(char *str)
29{
30 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
31 cpulist_parse(str, irq_default_affinity);
32 /*
33 * Set at least the boot cpu. We don't want to end up with
34 * bugreports caused by random comandline masks
35 */
36 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
37 return 1;
38}
39__setup("irqaffinity=", irq_affinity_setup);
40
Thomas Gleixner3795de22010-09-22 17:09:43 +020041static void __init init_irq_default_affinity(void)
42{
Thomas Gleixnerfbf19802016-02-03 19:52:23 +010043#ifdef CONFIG_CPUMASK_OFFSTACK
44 if (!irq_default_affinity)
45 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
46#endif
47 if (cpumask_empty(irq_default_affinity))
48 cpumask_setall(irq_default_affinity);
Thomas Gleixner3795de22010-09-22 17:09:43 +020049}
50#else
51static void __init init_irq_default_affinity(void)
52{
53}
54#endif
55
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020056#ifdef CONFIG_SMP
57static int alloc_masks(struct irq_desc *desc, gfp_t gfp, int node)
58{
Jiang Liu9df872f2015-06-03 11:47:50 +080059 if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
60 gfp, node))
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020061 return -ENOMEM;
62
63#ifdef CONFIG_GENERIC_PENDING_IRQ
64 if (!zalloc_cpumask_var_node(&desc->pending_mask, gfp, node)) {
Jiang Liu9df872f2015-06-03 11:47:50 +080065 free_cpumask_var(desc->irq_common_data.affinity);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020066 return -ENOMEM;
67 }
68#endif
69 return 0;
70}
71
Thomas Gleixner45ddcec2016-07-04 17:39:25 +090072static void desc_smp_init(struct irq_desc *desc, int node,
73 const struct cpumask *affinity)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020074{
Thomas Gleixner45ddcec2016-07-04 17:39:25 +090075 if (!affinity)
76 affinity = irq_default_affinity;
77 cpumask_copy(desc->irq_common_data.affinity, affinity);
78
Thomas Gleixnerb7b29332010-09-29 18:46:55 +020079#ifdef CONFIG_GENERIC_PENDING_IRQ
80 cpumask_clear(desc->pending_mask);
81#endif
Jiang Liu449e9ca2015-06-01 16:05:16 +080082#ifdef CONFIG_NUMA
83 desc->irq_common_data.node = node;
84#endif
Thomas Gleixnerb7b29332010-09-29 18:46:55 +020085}
86
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020087#else
88static inline int
89alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; }
Thomas Gleixner45ddcec2016-07-04 17:39:25 +090090static inline void
91desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020092#endif
93
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +020094static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
Thomas Gleixner45ddcec2016-07-04 17:39:25 +090095 const struct cpumask *affinity, struct module *owner)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +020096{
Eric Dumazet6c9ae002011-01-13 15:45:38 -080097 int cpu;
98
Jiang Liuaf7080e2015-06-01 16:05:21 +080099 desc->irq_common_data.handler_data = NULL;
Jiang Liub2377212015-06-01 16:05:43 +0800100 desc->irq_common_data.msi_desc = NULL;
Jiang Liuaf7080e2015-06-01 16:05:21 +0800101
Jiang Liu0d0b4c82015-06-01 16:05:12 +0800102 desc->irq_data.common = &desc->irq_common_data;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200103 desc->irq_data.irq = irq;
104 desc->irq_data.chip = &no_irq_chip;
105 desc->irq_data.chip_data = NULL;
Thomas Gleixnerf9e49892011-02-09 14:54:49 +0100106 irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
Thomas Gleixner801a0e92011-03-27 11:02:49 +0200107 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200108 desc->handle_irq = handle_bad_irq;
109 desc->depth = 1;
Thomas Gleixnerb7b29332010-09-29 18:46:55 +0200110 desc->irq_count = 0;
111 desc->irqs_unhandled = 0;
Thomas Gleixneracb5aef2019-02-08 14:48:03 +0100112 desc->tot_count = 0;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200113 desc->name = NULL;
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200114 desc->owner = owner;
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800115 for_each_possible_cpu(cpu)
116 *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900117 desc_smp_init(desc, node, affinity);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200118}
119
Thomas Gleixner3795de22010-09-22 17:09:43 +0200120int nr_irqs = NR_IRQS;
121EXPORT_SYMBOL_GPL(nr_irqs);
122
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200123static DEFINE_MUTEX(sparse_irq_lock);
Thomas Gleixnerc1ee6262011-02-17 17:45:15 +0100124static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200125
Thomas Gleixner3795de22010-09-22 17:09:43 +0200126#ifdef CONFIG_SPARSE_IRQ
127
Craig Gallekecb3f392016-09-13 12:14:51 -0400128static void irq_kobj_release(struct kobject *kobj);
129
130#ifdef CONFIG_SYSFS
131static struct kobject *irq_kobj_base;
132
133#define IRQ_ATTR_RO(_name) \
134static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
135
136static ssize_t per_cpu_count_show(struct kobject *kobj,
137 struct kobj_attribute *attr, char *buf)
138{
139 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
140 int cpu, irq = desc->irq_data.irq;
141 ssize_t ret = 0;
142 char *p = "";
143
144 for_each_possible_cpu(cpu) {
145 unsigned int c = kstat_irqs_cpu(irq, cpu);
146
147 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
148 p = ",";
149 }
150
151 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
152 return ret;
153}
154IRQ_ATTR_RO(per_cpu_count);
155
156static ssize_t chip_name_show(struct kobject *kobj,
157 struct kobj_attribute *attr, char *buf)
158{
159 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
160 ssize_t ret = 0;
161
162 raw_spin_lock_irq(&desc->lock);
163 if (desc->irq_data.chip && desc->irq_data.chip->name) {
164 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
165 desc->irq_data.chip->name);
166 }
167 raw_spin_unlock_irq(&desc->lock);
168
169 return ret;
170}
171IRQ_ATTR_RO(chip_name);
172
173static ssize_t hwirq_show(struct kobject *kobj,
174 struct kobj_attribute *attr, char *buf)
175{
176 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
177 ssize_t ret = 0;
178
179 raw_spin_lock_irq(&desc->lock);
180 if (desc->irq_data.domain)
181 ret = sprintf(buf, "%d\n", (int)desc->irq_data.hwirq);
182 raw_spin_unlock_irq(&desc->lock);
183
184 return ret;
185}
186IRQ_ATTR_RO(hwirq);
187
188static ssize_t type_show(struct kobject *kobj,
189 struct kobj_attribute *attr, char *buf)
190{
191 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
192 ssize_t ret = 0;
193
194 raw_spin_lock_irq(&desc->lock);
195 ret = sprintf(buf, "%s\n",
196 irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
197 raw_spin_unlock_irq(&desc->lock);
198
199 return ret;
200
201}
202IRQ_ATTR_RO(type);
203
204static ssize_t name_show(struct kobject *kobj,
205 struct kobj_attribute *attr, char *buf)
206{
207 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
208 ssize_t ret = 0;
209
210 raw_spin_lock_irq(&desc->lock);
211 if (desc->name)
212 ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
213 raw_spin_unlock_irq(&desc->lock);
214
215 return ret;
216}
217IRQ_ATTR_RO(name);
218
219static ssize_t actions_show(struct kobject *kobj,
220 struct kobj_attribute *attr, char *buf)
221{
222 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
223 struct irqaction *action;
224 ssize_t ret = 0;
225 char *p = "";
226
227 raw_spin_lock_irq(&desc->lock);
228 for (action = desc->action; action != NULL; action = action->next) {
229 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
230 p, action->name);
231 p = ",";
232 }
233 raw_spin_unlock_irq(&desc->lock);
234
235 if (ret)
236 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
237
238 return ret;
239}
240IRQ_ATTR_RO(actions);
241
242static struct attribute *irq_attrs[] = {
243 &per_cpu_count_attr.attr,
244 &chip_name_attr.attr,
245 &hwirq_attr.attr,
246 &type_attr.attr,
247 &name_attr.attr,
248 &actions_attr.attr,
249 NULL
250};
251
252static struct kobj_type irq_kobj_type = {
253 .release = irq_kobj_release,
254 .sysfs_ops = &kobj_sysfs_ops,
255 .default_attrs = irq_attrs,
256};
257
258static void irq_sysfs_add(int irq, struct irq_desc *desc)
259{
260 if (irq_kobj_base) {
261 /*
262 * Continue even in case of failure as this is nothing
263 * crucial.
264 */
265 if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
266 pr_warn("Failed to add kobject for irq %d\n", irq);
267 }
268}
269
Michael Kelley84219282019-08-01 23:53:53 +0000270static void irq_sysfs_del(struct irq_desc *desc)
271{
272 /*
273 * If irq_sysfs_init() has not yet been invoked (early boot), then
274 * irq_kobj_base is NULL and the descriptor was never added.
275 * kobject_del() complains about a object with no parent, so make
276 * it conditional.
277 */
278 if (irq_kobj_base)
279 kobject_del(&desc->kobj);
280}
281
Craig Gallekecb3f392016-09-13 12:14:51 -0400282static int __init irq_sysfs_init(void)
283{
284 struct irq_desc *desc;
285 int irq;
286
287 /* Prevent concurrent irq alloc/free */
288 irq_lock_sparse();
289
290 irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
291 if (!irq_kobj_base) {
292 irq_unlock_sparse();
293 return -ENOMEM;
294 }
295
296 /* Add the already allocated interrupts */
297 for_each_irq_desc(irq, desc)
298 irq_sysfs_add(irq, desc);
299 irq_unlock_sparse();
300
301 return 0;
302}
303postcore_initcall(irq_sysfs_init);
304
305#else /* !CONFIG_SYSFS */
306
307static struct kobj_type irq_kobj_type = {
308 .release = irq_kobj_release,
309};
310
311static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
Michael Kelley84219282019-08-01 23:53:53 +0000312static void irq_sysfs_del(struct irq_desc *desc) {}
Craig Gallekecb3f392016-09-13 12:14:51 -0400313
314#endif /* CONFIG_SYSFS */
315
Thomas Gleixnerbaa0d232010-10-05 15:14:35 +0200316static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200317
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200318static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
Thomas Gleixner3795de22010-09-22 17:09:43 +0200319{
320 radix_tree_insert(&irq_desc_tree, irq, desc);
321}
322
323struct irq_desc *irq_to_desc(unsigned int irq)
324{
325 return radix_tree_lookup(&irq_desc_tree, irq);
326}
Jiri Kosina3911ff32012-05-13 12:13:15 +0200327EXPORT_SYMBOL(irq_to_desc);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200328
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200329static void delete_irq_desc(unsigned int irq)
330{
331 radix_tree_delete(&irq_desc_tree, irq);
332}
333
334#ifdef CONFIG_SMP
335static void free_masks(struct irq_desc *desc)
336{
337#ifdef CONFIG_GENERIC_PENDING_IRQ
338 free_cpumask_var(desc->pending_mask);
339#endif
Jiang Liu9df872f2015-06-03 11:47:50 +0800340 free_cpumask_var(desc->irq_common_data.affinity);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200341}
342#else
343static inline void free_masks(struct irq_desc *desc) { }
344#endif
345
Thomas Gleixnerc291ee62014-12-11 23:01:41 +0100346void irq_lock_sparse(void)
347{
348 mutex_lock(&sparse_irq_lock);
349}
350
351void irq_unlock_sparse(void)
352{
353 mutex_unlock(&sparse_irq_lock);
354}
355
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900356static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
357 const struct cpumask *affinity,
358 struct module *owner)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200359{
360 struct irq_desc *desc;
Thomas Gleixnerbaa0d232010-10-05 15:14:35 +0200361 gfp_t gfp = GFP_KERNEL;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200362
363 desc = kzalloc_node(sizeof(*desc), gfp, node);
364 if (!desc)
365 return NULL;
366 /* allocate based on nr_cpu_ids */
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800367 desc->kstat_irqs = alloc_percpu(unsigned int);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200368 if (!desc->kstat_irqs)
369 goto err_desc;
370
371 if (alloc_masks(desc, gfp, node))
372 goto err_kstat;
373
374 raw_spin_lock_init(&desc->lock);
375 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
Thomas Gleixner425a5072015-12-13 18:02:22 +0100376 init_rcu_head(&desc->rcu);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200377
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900378 desc_set_defaults(irq, desc, node, affinity, owner);
379 irqd_set(&desc->irq_data, flags);
Craig Gallekecb3f392016-09-13 12:14:51 -0400380 kobject_init(&desc->kobj, &irq_kobj_type);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200381
382 return desc;
383
384err_kstat:
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800385 free_percpu(desc->kstat_irqs);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200386err_desc:
387 kfree(desc);
388 return NULL;
389}
390
Craig Gallekecb3f392016-09-13 12:14:51 -0400391static void irq_kobj_release(struct kobject *kobj)
Thomas Gleixner425a5072015-12-13 18:02:22 +0100392{
Craig Gallekecb3f392016-09-13 12:14:51 -0400393 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
Thomas Gleixner425a5072015-12-13 18:02:22 +0100394
395 free_masks(desc);
396 free_percpu(desc->kstat_irqs);
397 kfree(desc);
398}
399
Craig Gallekecb3f392016-09-13 12:14:51 -0400400static void delayed_free_desc(struct rcu_head *rhp)
401{
402 struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
403
404 kobject_put(&desc->kobj);
405}
406
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200407static void free_desc(unsigned int irq)
408{
409 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200410
Thomas Gleixner13bfe992010-09-30 02:46:07 +0200411 unregister_irq_proc(irq, desc);
412
Thomas Gleixnerc291ee62014-12-11 23:01:41 +0100413 /*
414 * sparse_irq_lock protects also show_interrupts() and
415 * kstat_irq_usr(). Once we deleted the descriptor from the
416 * sparse tree we can free it. Access in proc will fail to
417 * lookup the descriptor.
Craig Gallekecb3f392016-09-13 12:14:51 -0400418 *
419 * The sysfs entry must be serialized against a concurrent
420 * irq_sysfs_init() as well.
Thomas Gleixnerc291ee62014-12-11 23:01:41 +0100421 */
Michael Kelley84219282019-08-01 23:53:53 +0000422 irq_sysfs_del(desc);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200423 delete_irq_desc(irq);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200424
Thomas Gleixner425a5072015-12-13 18:02:22 +0100425 /*
426 * We free the descriptor, masks and stat fields via RCU. That
427 * allows demultiplex interrupts to do rcu based management of
428 * the child interrupts.
429 */
430 call_rcu(&desc->rcu, delayed_free_desc);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200431}
432
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200433static int alloc_descs(unsigned int start, unsigned int cnt, int node,
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900434 const struct cpumask *affinity, struct module *owner)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200435{
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900436 const struct cpumask *mask = NULL;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200437 struct irq_desc *desc;
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900438 unsigned int flags;
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200439 int i;
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900440
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200441 /* Validate affinity mask(s) */
442 if (affinity) {
443 for (i = 0, mask = affinity; i < cnt; i++, mask++) {
444 if (cpumask_empty(mask))
445 return -EINVAL;
446 }
447 }
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900448
449 flags = affinity ? IRQD_AFFINITY_MANAGED : 0;
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200450 mask = NULL;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200451
452 for (i = 0; i < cnt; i++) {
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900453 if (affinity) {
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200454 node = cpu_to_node(cpumask_first(affinity));
455 mask = affinity;
456 affinity++;
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900457 }
458 desc = alloc_desc(start + i, node, flags, mask, owner);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200459 if (!desc)
460 goto err;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200461 irq_insert_desc(start + i, desc);
Craig Gallekecb3f392016-09-13 12:14:51 -0400462 irq_sysfs_add(start + i, desc);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200463 }
Thomas Gleixner3d5960c2017-09-05 10:12:20 +0200464 bitmap_set(allocated_irqs, start, cnt);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200465 return start;
466
467err:
468 for (i--; i >= 0; i--)
469 free_desc(start + i);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200470 return -ENOMEM;
471}
472
Yinghai Lued4dea62011-02-19 11:07:37 -0800473static int irq_expand_nr_irqs(unsigned int nr)
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100474{
Yinghai Lued4dea62011-02-19 11:07:37 -0800475 if (nr > IRQ_BITMAP_BITS)
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100476 return -ENOMEM;
Yinghai Lued4dea62011-02-19 11:07:37 -0800477 nr_irqs = nr;
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100478 return 0;
479}
480
Thomas Gleixner3795de22010-09-22 17:09:43 +0200481int __init early_irq_init(void)
482{
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200483 int i, initcnt, node = first_online_node;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200484 struct irq_desc *desc;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200485
486 init_irq_default_affinity();
487
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200488 /* Let arch update nr_irqs and return the nr of preallocated irqs */
489 initcnt = arch_probe_nr_irqs();
490 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200491
Thomas Gleixnerc1ee6262011-02-17 17:45:15 +0100492 if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
493 nr_irqs = IRQ_BITMAP_BITS;
494
495 if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
496 initcnt = IRQ_BITMAP_BITS;
497
498 if (initcnt > nr_irqs)
499 nr_irqs = initcnt;
500
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200501 for (i = 0; i < initcnt; i++) {
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900502 desc = alloc_desc(i, node, 0, NULL, NULL);
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200503 set_bit(i, allocated_irqs);
504 irq_insert_desc(i, desc);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200505 }
Thomas Gleixner3795de22010-09-22 17:09:43 +0200506 return arch_early_irq_init();
507}
508
Thomas Gleixner3795de22010-09-22 17:09:43 +0200509#else /* !CONFIG_SPARSE_IRQ */
510
511struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
512 [0 ... NR_IRQS-1] = {
Thomas Gleixner3795de22010-09-22 17:09:43 +0200513 .handle_irq = handle_bad_irq,
514 .depth = 1,
515 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
516 }
517};
518
Thomas Gleixner3795de22010-09-22 17:09:43 +0200519int __init early_irq_init(void)
520{
Thomas Gleixneraa99ec02010-09-27 20:02:56 +0200521 int count, i, node = first_online_node;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200522 struct irq_desc *desc;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200523
524 init_irq_default_affinity();
525
526 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
527
528 desc = irq_desc;
529 count = ARRAY_SIZE(irq_desc);
530
531 for (i = 0; i < count; i++) {
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800532 desc[i].kstat_irqs = alloc_percpu(unsigned int);
Linus Walleije7fbad32011-05-31 18:14:39 +0200533 alloc_masks(&desc[i], GFP_KERNEL, node);
534 raw_spin_lock_init(&desc[i].lock);
Thomas Gleixner154cd382010-09-22 15:58:45 +0200535 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900536 desc_set_defaults(i, &desc[i], node, NULL, NULL);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200537 }
538 return arch_early_irq_init();
539}
540
541struct irq_desc *irq_to_desc(unsigned int irq)
542{
543 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
544}
Paul Gortmaker2c45aad2014-02-10 13:39:53 -0500545EXPORT_SYMBOL(irq_to_desc);
Thomas Gleixner3795de22010-09-22 17:09:43 +0200546
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200547static void free_desc(unsigned int irq)
548{
Thomas Gleixnerd8179bc2014-05-07 15:44:23 +0000549 struct irq_desc *desc = irq_to_desc(irq);
550 unsigned long flags;
551
552 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner45ddcec2016-07-04 17:39:25 +0900553 desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
Thomas Gleixnerd8179bc2014-05-07 15:44:23 +0000554 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200555}
556
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200557static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900558 const struct cpumask *affinity,
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200559 struct module *owner)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200560{
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200561 u32 i;
562
563 for (i = 0; i < cnt; i++) {
564 struct irq_desc *desc = irq_to_desc(start + i);
565
566 desc->owner = owner;
567 }
Thomas Gleixner3d5960c2017-09-05 10:12:20 +0200568 bitmap_set(allocated_irqs, start, cnt);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200569 return start;
570}
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100571
Yinghai Lued4dea62011-02-19 11:07:37 -0800572static int irq_expand_nr_irqs(unsigned int nr)
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100573{
574 return -ENOMEM;
575}
576
Thomas Gleixnerf63b6a02014-05-07 15:44:21 +0000577void irq_mark_irq(unsigned int irq)
578{
579 mutex_lock(&sparse_irq_lock);
580 bitmap_set(allocated_irqs, irq, 1);
581 mutex_unlock(&sparse_irq_lock);
582}
583
Thomas Gleixnerc940e012014-05-07 15:44:22 +0000584#ifdef CONFIG_GENERIC_IRQ_LEGACY
585void irq_init_desc(unsigned int irq)
586{
Thomas Gleixnerd8179bc2014-05-07 15:44:23 +0000587 free_desc(irq);
Thomas Gleixnerc940e012014-05-07 15:44:22 +0000588}
589#endif
590
Thomas Gleixner3795de22010-09-22 17:09:43 +0200591#endif /* !CONFIG_SPARSE_IRQ */
592
Thomas Gleixnerfe12bc22011-05-18 12:48:00 +0200593/**
594 * generic_handle_irq - Invoke the handler for a particular irq
595 * @irq: The irq number to handle
596 *
597 */
598int generic_handle_irq(unsigned int irq)
599{
600 struct irq_desc *desc = irq_to_desc(irq);
601
602 if (!desc)
603 return -EINVAL;
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200604 generic_handle_irq_desc(desc);
Thomas Gleixnerfe12bc22011-05-18 12:48:00 +0200605 return 0;
606}
Jonathan Cameronedf76f82011-05-18 10:39:04 +0100607EXPORT_SYMBOL_GPL(generic_handle_irq);
Thomas Gleixnerfe12bc22011-05-18 12:48:00 +0200608
Marc Zyngier76ba59f2014-08-26 11:03:16 +0100609#ifdef CONFIG_HANDLE_DOMAIN_IRQ
610/**
611 * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
612 * @domain: The domain where to perform the lookup
613 * @hwirq: The HW irq number to convert to a logical one
614 * @lookup: Whether to perform the domain lookup or not
615 * @regs: Register file coming from the low-level handling code
616 *
617 * Returns: 0 on success, or -EINVAL if conversion has failed
618 */
619int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
620 bool lookup, struct pt_regs *regs)
621{
622 struct pt_regs *old_regs = set_irq_regs(regs);
623 unsigned int irq = hwirq;
624 int ret = 0;
625
626 irq_enter();
627
628#ifdef CONFIG_IRQ_DOMAIN
629 if (lookup)
630 irq = irq_find_mapping(domain, hwirq);
631#endif
632
633 /*
634 * Some hardware gives randomly wrong interrupts. Rather
635 * than crashing, do something sensible.
636 */
637 if (unlikely(!irq || irq >= nr_irqs)) {
638 ack_bad_irq(irq);
639 ret = -EINVAL;
640 } else {
641 generic_handle_irq(irq);
642 }
643
644 irq_exit();
645 set_irq_regs(old_regs);
646 return ret;
647}
648#endif
649
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200650/* Dynamic interrupt handling */
651
652/**
653 * irq_free_descs - free irq descriptors
654 * @from: Start of descriptor range
655 * @cnt: Number of consecutive irqs to free
656 */
657void irq_free_descs(unsigned int from, unsigned int cnt)
658{
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200659 int i;
660
661 if (from >= nr_irqs || (from + cnt) > nr_irqs)
662 return;
663
Thomas Gleixner3d5960c2017-09-05 10:12:20 +0200664 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200665 for (i = 0; i < cnt; i++)
666 free_desc(from + i);
667
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200668 bitmap_clear(allocated_irqs, from, cnt);
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200669 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200670}
Jonathan Cameronedf76f82011-05-18 10:39:04 +0100671EXPORT_SYMBOL_GPL(irq_free_descs);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200672
673/**
674 * irq_alloc_descs - allocate and initialize a range of irq descriptors
675 * @irq: Allocate for specific irq number if irq >= 0
676 * @from: Start the search from this irq number
677 * @cnt: Number of consecutive irqs to allocate.
678 * @node: Preferred node on which the irq descriptor should be allocated
Randy Dunlapd522a0d2011-08-18 12:19:27 -0700679 * @owner: Owning module (can be NULL)
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200680 * @affinity: Optional pointer to an affinity mask array of size @cnt which
681 * hints where the irq descriptors should be allocated and which
682 * default affinities to use
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200683 *
684 * Returns the first irq number or error code
685 */
686int __ref
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200687__irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900688 struct module *owner, const struct cpumask *affinity)
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200689{
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200690 int start, ret;
691
692 if (!cnt)
693 return -EINVAL;
694
Mark Brownc5182b82011-06-02 18:55:13 +0100695 if (irq >= 0) {
696 if (from > irq)
697 return -EINVAL;
698 from = irq;
Thomas Gleixner62a08ae2014-04-24 09:50:53 +0200699 } else {
700 /*
701 * For interrupts which are freely allocated the
702 * architecture can force a lower bound to the @from
703 * argument. x86 uses this to exclude the GSI space.
704 */
705 from = arch_dynirq_lower_bound(from);
Mark Brownc5182b82011-06-02 18:55:13 +0100706 }
707
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200708 mutex_lock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200709
Yinghai Lued4dea62011-02-19 11:07:37 -0800710 start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
711 from, cnt, 0);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200712 ret = -EEXIST;
713 if (irq >=0 && start != irq)
Thomas Gleixner3d5960c2017-09-05 10:12:20 +0200714 goto unlock;
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200715
Yinghai Lued4dea62011-02-19 11:07:37 -0800716 if (start + cnt > nr_irqs) {
717 ret = irq_expand_nr_irqs(start + cnt);
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100718 if (ret)
Thomas Gleixner3d5960c2017-09-05 10:12:20 +0200719 goto unlock;
Thomas Gleixnere7bcecb2011-02-16 17:12:57 +0100720 }
Thomas Gleixner3d5960c2017-09-05 10:12:20 +0200721 ret = alloc_descs(start, cnt, node, affinity, owner);
722unlock:
Thomas Gleixnera05a9002010-10-08 12:47:53 +0200723 mutex_unlock(&sparse_irq_lock);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200724 return ret;
725}
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200726EXPORT_SYMBOL_GPL(__irq_alloc_descs);
Thomas Gleixner1f5a5b82010-09-27 17:48:26 +0200727
Thomas Gleixner7b6ef122014-05-07 15:44:05 +0000728#ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
729/**
730 * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
731 * @cnt: number of interrupts to allocate
732 * @node: node on which to allocate
733 *
734 * Returns an interrupt number > 0 or 0, if the allocation fails.
735 */
736unsigned int irq_alloc_hwirqs(int cnt, int node)
737{
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900738 int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL, NULL);
Thomas Gleixner7b6ef122014-05-07 15:44:05 +0000739
740 if (irq < 0)
741 return 0;
742
743 for (i = irq; cnt > 0; i++, cnt--) {
744 if (arch_setup_hwirq(i, node))
745 goto err;
746 irq_clear_status_flags(i, _IRQ_NOREQUEST);
747 }
748 return irq;
749
750err:
751 for (i--; i >= irq; i--) {
752 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
753 arch_teardown_hwirq(i);
754 }
755 irq_free_descs(irq, cnt);
756 return 0;
757}
758EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
759
760/**
761 * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
762 * @from: Free from irq number
763 * @cnt: number of interrupts to free
764 *
765 */
766void irq_free_hwirqs(unsigned int from, int cnt)
767{
Keith Busch8844aad2014-06-30 16:24:44 -0600768 int i, j;
Thomas Gleixner7b6ef122014-05-07 15:44:05 +0000769
Keith Busch8844aad2014-06-30 16:24:44 -0600770 for (i = from, j = cnt; j > 0; i++, j--) {
Thomas Gleixner7b6ef122014-05-07 15:44:05 +0000771 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
772 arch_teardown_hwirq(i);
773 }
774 irq_free_descs(from, cnt);
775}
776EXPORT_SYMBOL_GPL(irq_free_hwirqs);
777#endif
778
Thomas Gleixnera98d24b2010-09-30 10:45:07 +0200779/**
780 * irq_get_next_irq - get next allocated irq number
781 * @offset: where to start the search
782 *
783 * Returns next irq number after offset or nr_irqs if none is found.
784 */
785unsigned int irq_get_next_irq(unsigned int offset)
786{
787 return find_next_bit(allocated_irqs, nr_irqs, offset);
788}
789
Thomas Gleixnerd5eb4ad22011-02-12 12:16:16 +0100790struct irq_desc *
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100791__irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
792 unsigned int check)
Thomas Gleixnerd5eb4ad22011-02-12 12:16:16 +0100793{
794 struct irq_desc *desc = irq_to_desc(irq);
795
796 if (desc) {
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100797 if (check & _IRQ_DESC_CHECK) {
798 if ((check & _IRQ_DESC_PERCPU) &&
799 !irq_settings_is_per_cpu_devid(desc))
800 return NULL;
801
802 if (!(check & _IRQ_DESC_PERCPU) &&
803 irq_settings_is_per_cpu_devid(desc))
804 return NULL;
805 }
806
Thomas Gleixnerd5eb4ad22011-02-12 12:16:16 +0100807 if (bus)
808 chip_bus_lock(desc);
809 raw_spin_lock_irqsave(&desc->lock, *flags);
810 }
811 return desc;
812}
813
814void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
815{
816 raw_spin_unlock_irqrestore(&desc->lock, flags);
817 if (bus)
818 chip_bus_sync_unlock(desc);
819}
820
Marc Zyngier222df542016-04-11 09:57:52 +0100821int irq_set_percpu_devid_partition(unsigned int irq,
822 const struct cpumask *affinity)
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100823{
824 struct irq_desc *desc = irq_to_desc(irq);
825
826 if (!desc)
827 return -EINVAL;
828
829 if (desc->percpu_enabled)
830 return -EINVAL;
831
832 desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
833
834 if (!desc->percpu_enabled)
835 return -ENOMEM;
836
Marc Zyngier222df542016-04-11 09:57:52 +0100837 if (affinity)
838 desc->percpu_affinity = affinity;
839 else
840 desc->percpu_affinity = cpu_possible_mask;
841
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100842 irq_set_percpu_devid_flags(irq);
843 return 0;
844}
845
Marc Zyngier222df542016-04-11 09:57:52 +0100846int irq_set_percpu_devid(unsigned int irq)
847{
848 return irq_set_percpu_devid_partition(irq, NULL);
849}
850
851int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
852{
853 struct irq_desc *desc = irq_to_desc(irq);
854
855 if (!desc || !desc->percpu_enabled)
856 return -EINVAL;
857
858 if (affinity)
859 cpumask_copy(affinity, desc->percpu_affinity);
860
861 return 0;
862}
863
Thomas Gleixner792d0012014-02-23 21:40:14 +0000864void kstat_incr_irq_this_cpu(unsigned int irq)
865{
Jiang Liub51bf952015-06-04 12:13:25 +0800866 kstat_incr_irqs_this_cpu(irq_to_desc(irq));
Thomas Gleixner792d0012014-02-23 21:40:14 +0000867}
868
Thomas Gleixnerc291ee62014-12-11 23:01:41 +0100869/**
870 * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
871 * @irq: The interrupt number
872 * @cpu: The cpu number
873 *
874 * Returns the sum of interrupt counts on @cpu since boot for
875 * @irq. The caller must ensure that the interrupt is not removed
876 * concurrently.
877 */
Thomas Gleixner3795de22010-09-22 17:09:43 +0200878unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
879{
880 struct irq_desc *desc = irq_to_desc(irq);
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800881
882 return desc && desc->kstat_irqs ?
883 *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
Thomas Gleixner3795de22010-09-22 17:09:43 +0200884}
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700885
Thomas Gleixnerc291ee62014-12-11 23:01:41 +0100886/**
887 * kstat_irqs - Get the statistics for an interrupt
888 * @irq: The interrupt number
889 *
890 * Returns the sum of interrupt counts on all cpus since boot for
891 * @irq. The caller must ensure that the interrupt is not removed
892 * concurrently.
893 */
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700894unsigned int kstat_irqs(unsigned int irq)
895{
896 struct irq_desc *desc = irq_to_desc(irq);
Nicholas Mc Guire5e9662f2015-05-03 10:48:50 +0200897 unsigned int sum = 0;
Thomas Gleixneracb5aef2019-02-08 14:48:03 +0100898 int cpu;
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700899
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800900 if (!desc || !desc->kstat_irqs)
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700901 return 0;
Thomas Gleixneracb5aef2019-02-08 14:48:03 +0100902 if (!irq_settings_is_per_cpu_devid(desc) &&
903 !irq_settings_is_per_cpu(desc))
904 return desc->tot_count;
905
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700906 for_each_possible_cpu(cpu)
Eric Dumazet6c9ae002011-01-13 15:45:38 -0800907 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
KAMEZAWA Hiroyuki478735e32010-10-27 15:34:15 -0700908 return sum;
909}
Thomas Gleixnerc291ee62014-12-11 23:01:41 +0100910
911/**
912 * kstat_irqs_usr - Get the statistics for an interrupt
913 * @irq: The interrupt number
914 *
915 * Returns the sum of interrupt counts on all cpus since boot for
916 * @irq. Contrary to kstat_irqs() this can be called from any
917 * preemptible context. It's protected against concurrent removal of
918 * an interrupt descriptor when sparse irqs are enabled.
919 */
920unsigned int kstat_irqs_usr(unsigned int irq)
921{
Nicholas Mc Guire7df0b272015-05-03 10:49:11 +0200922 unsigned int sum;
Thomas Gleixnerc291ee62014-12-11 23:01:41 +0100923
924 irq_lock_sparse();
925 sum = kstat_irqs(irq);
926 irq_unlock_sparse();
927 return sum;
928}