blob: d91dcd08113d856600dbbc668ec5e644461a9382 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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>
12#include <linux/export.h>
13#include <linux/interrupt.h>
14#include <linux/kernel_stat.h>
15#include <linux/radix-tree.h>
16#include <linux/bitmap.h>
17
18#include "internals.h"
19
20/*
21 * lockdep: we want to handle all irq_desc locks as a single lock-class:
22 */
23static struct lock_class_key irq_desc_lock_class;
24
25#if defined(CONFIG_SMP)
26static int __init irq_affinity_setup(char *str)
27{
28 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
29 cpulist_parse(str, irq_default_affinity);
30 /*
31 * Set at least the boot cpu. We don't want to end up with
32 * bugreports caused by random comandline masks
33 */
34 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
35 return 1;
36}
37__setup("irqaffinity=", irq_affinity_setup);
38
39static void __init init_irq_default_affinity(void)
40{
41#ifdef CONFIG_CPUMASK_OFFSTACK
42 if (!irq_default_affinity)
43 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
44#endif
45 if (cpumask_empty(irq_default_affinity))
46 cpumask_setall(irq_default_affinity);
47}
48#else
49static void __init init_irq_default_affinity(void)
50{
51}
52#endif
53
54#ifdef CONFIG_SMP
55static int alloc_masks(struct irq_desc *desc, gfp_t gfp, int node)
56{
57 if (!zalloc_cpumask_var_node(&desc->irq_data.affinity, gfp, node))
58 return -ENOMEM;
59
60#ifdef CONFIG_GENERIC_PENDING_IRQ
61 if (!zalloc_cpumask_var_node(&desc->pending_mask, gfp, node)) {
62 free_cpumask_var(desc->irq_data.affinity);
63 return -ENOMEM;
64 }
65#endif
66 return 0;
67}
68
69static void desc_smp_init(struct irq_desc *desc, int node)
70{
71 desc->irq_data.node = node;
72 cpumask_copy(desc->irq_data.affinity, irq_default_affinity);
73#ifdef CONFIG_GENERIC_PENDING_IRQ
74 cpumask_clear(desc->pending_mask);
75#endif
76}
77
78static inline int desc_node(struct irq_desc *desc)
79{
80 return desc->irq_data.node;
81}
82
83#else
84static inline int
85alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; }
86static inline void desc_smp_init(struct irq_desc *desc, int node) { }
87static inline int desc_node(struct irq_desc *desc) { return 0; }
88#endif
89
90static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
91 struct module *owner)
92{
93 int cpu;
94
95 desc->irq_data.irq = irq;
96 desc->irq_data.chip = &no_irq_chip;
97 desc->irq_data.chip_data = NULL;
98 desc->irq_data.handler_data = NULL;
99 desc->irq_data.msi_desc = NULL;
100 irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
101 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
102 desc->handle_irq = handle_bad_irq;
103 desc->depth = 1;
104 desc->irq_count = 0;
105 desc->irqs_unhandled = 0;
106 desc->name = NULL;
107 desc->owner = owner;
108 for_each_possible_cpu(cpu)
109 *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
110 desc_smp_init(desc, node);
111}
112
113int nr_irqs = NR_IRQS;
114EXPORT_SYMBOL_GPL(nr_irqs);
115
116static DEFINE_MUTEX(sparse_irq_lock);
117static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
118
119#ifdef CONFIG_SPARSE_IRQ
120
121static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
122
123static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
124{
125 radix_tree_insert(&irq_desc_tree, irq, desc);
126}
127
128struct irq_desc *irq_to_desc(unsigned int irq)
129{
130 return radix_tree_lookup(&irq_desc_tree, irq);
131}
132EXPORT_SYMBOL(irq_to_desc);
133
134static void delete_irq_desc(unsigned int irq)
135{
136 radix_tree_delete(&irq_desc_tree, irq);
137}
138
139#ifdef CONFIG_SMP
140static void free_masks(struct irq_desc *desc)
141{
142#ifdef CONFIG_GENERIC_PENDING_IRQ
143 free_cpumask_var(desc->pending_mask);
144#endif
145 free_cpumask_var(desc->irq_data.affinity);
146}
147#else
148static inline void free_masks(struct irq_desc *desc) { }
149#endif
150
151void irq_lock_sparse(void)
152{
153 mutex_lock(&sparse_irq_lock);
154}
155
156void irq_unlock_sparse(void)
157{
158 mutex_unlock(&sparse_irq_lock);
159}
160
161static struct irq_desc *alloc_desc(int irq, int node, struct module *owner)
162{
163 struct irq_desc *desc;
164 gfp_t gfp = GFP_KERNEL;
165
166 desc = kzalloc_node(sizeof(*desc), gfp, node);
167 if (!desc)
168 return NULL;
169 /* allocate based on nr_cpu_ids */
170 desc->kstat_irqs = alloc_percpu(unsigned int);
171 if (!desc->kstat_irqs)
172 goto err_desc;
173
174 if (alloc_masks(desc, gfp, node))
175 goto err_kstat;
176
177 raw_spin_lock_init(&desc->lock);
178 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
179
180 desc_set_defaults(irq, desc, node, owner);
181
182 return desc;
183
184err_kstat:
185 free_percpu(desc->kstat_irqs);
186err_desc:
187 kfree(desc);
188 return NULL;
189}
190
191static void free_desc(unsigned int irq)
192{
193 struct irq_desc *desc = irq_to_desc(irq);
194
195 unregister_irq_proc(irq, desc);
196
197 /*
198 * sparse_irq_lock protects also show_interrupts() and
199 * kstat_irq_usr(). Once we deleted the descriptor from the
200 * sparse tree we can free it. Access in proc will fail to
201 * lookup the descriptor.
202 */
203 mutex_lock(&sparse_irq_lock);
204 delete_irq_desc(irq);
205 mutex_unlock(&sparse_irq_lock);
206
207 free_masks(desc);
208 free_percpu(desc->kstat_irqs);
209 kfree(desc);
210}
211
212static int alloc_descs(unsigned int start, unsigned int cnt, int node,
213 struct module *owner)
214{
215 struct irq_desc *desc;
216 int i;
217
218 for (i = 0; i < cnt; i++) {
219 desc = alloc_desc(start + i, node, owner);
220 if (!desc)
221 goto err;
222 mutex_lock(&sparse_irq_lock);
223 irq_insert_desc(start + i, desc);
224 mutex_unlock(&sparse_irq_lock);
225 }
226 return start;
227
228err:
229 for (i--; i >= 0; i--)
230 free_desc(start + i);
231
232 mutex_lock(&sparse_irq_lock);
233 bitmap_clear(allocated_irqs, start, cnt);
234 mutex_unlock(&sparse_irq_lock);
235 return -ENOMEM;
236}
237
238static int irq_expand_nr_irqs(unsigned int nr)
239{
240 if (nr > IRQ_BITMAP_BITS)
241 return -ENOMEM;
242 nr_irqs = nr;
243 return 0;
244}
245
246int __init early_irq_init(void)
247{
248 int i, initcnt, node = first_online_node;
249 struct irq_desc *desc;
250
251 init_irq_default_affinity();
252
253 /* Let arch update nr_irqs and return the nr of preallocated irqs */
254 initcnt = arch_probe_nr_irqs();
255 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
256
257 if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
258 nr_irqs = IRQ_BITMAP_BITS;
259
260 if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
261 initcnt = IRQ_BITMAP_BITS;
262
263 if (initcnt > nr_irqs)
264 nr_irqs = initcnt;
265
266 for (i = 0; i < initcnt; i++) {
267 desc = alloc_desc(i, node, NULL);
268 set_bit(i, allocated_irqs);
269 irq_insert_desc(i, desc);
270 }
271 return arch_early_irq_init();
272}
273
274#else /* !CONFIG_SPARSE_IRQ */
275
276struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
277 [0 ... NR_IRQS-1] = {
278 .handle_irq = handle_bad_irq,
279 .depth = 1,
280 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
281 }
282};
283
284int __init early_irq_init(void)
285{
286 int count, i, node = first_online_node;
287 struct irq_desc *desc;
288
289 init_irq_default_affinity();
290
291 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
292
293 desc = irq_desc;
294 count = ARRAY_SIZE(irq_desc);
295
296 for (i = 0; i < count; i++) {
297 desc[i].kstat_irqs = alloc_percpu(unsigned int);
298 alloc_masks(&desc[i], GFP_KERNEL, node);
299 raw_spin_lock_init(&desc[i].lock);
300 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
301 desc_set_defaults(i, &desc[i], node, NULL);
302 }
303 return arch_early_irq_init();
304}
305
306struct irq_desc *irq_to_desc(unsigned int irq)
307{
308 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
309}
310EXPORT_SYMBOL(irq_to_desc);
311
312static void free_desc(unsigned int irq)
313{
314 dynamic_irq_cleanup(irq);
315}
316
317static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
318 struct module *owner)
319{
320 u32 i;
321
322 for (i = 0; i < cnt; i++) {
323 struct irq_desc *desc = irq_to_desc(start + i);
324
325 desc->owner = owner;
326 }
327 return start;
328}
329
330static int irq_expand_nr_irqs(unsigned int nr)
331{
332 return -ENOMEM;
333}
334
335#endif /* !CONFIG_SPARSE_IRQ */
336
337/**
338 * generic_handle_irq - Invoke the handler for a particular irq
339 * @irq: The irq number to handle
340 *
341 */
342int generic_handle_irq(unsigned int irq)
343{
344 struct irq_desc *desc = irq_to_desc(irq);
345
346 if (!desc)
347 return -EINVAL;
348 generic_handle_irq_desc(irq, desc);
349 return 0;
350}
351EXPORT_SYMBOL_GPL(generic_handle_irq);
352
353/* Dynamic interrupt handling */
354
355/**
356 * irq_free_descs - free irq descriptors
357 * @from: Start of descriptor range
358 * @cnt: Number of consecutive irqs to free
359 */
360void irq_free_descs(unsigned int from, unsigned int cnt)
361{
362 int i;
363
364 if (from >= nr_irqs || (from + cnt) > nr_irqs)
365 return;
366
367 for (i = 0; i < cnt; i++)
368 free_desc(from + i);
369
370 mutex_lock(&sparse_irq_lock);
371 bitmap_clear(allocated_irqs, from, cnt);
372 mutex_unlock(&sparse_irq_lock);
373}
374EXPORT_SYMBOL_GPL(irq_free_descs);
375
376/**
377 * irq_alloc_descs - allocate and initialize a range of irq descriptors
378 * @irq: Allocate for specific irq number if irq >= 0
379 * @from: Start the search from this irq number
380 * @cnt: Number of consecutive irqs to allocate.
381 * @node: Preferred node on which the irq descriptor should be allocated
382 * @owner: Owning module (can be NULL)
383 *
384 * Returns the first irq number or error code
385 */
386int __ref
387__irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
388 struct module *owner)
389{
390 int start, ret;
391
392 if (!cnt)
393 return -EINVAL;
394
395 if (irq >= 0) {
396 if (from > irq)
397 return -EINVAL;
398 from = irq;
399 }
400
401 mutex_lock(&sparse_irq_lock);
402
403 start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
404 from, cnt, 0);
405 ret = -EEXIST;
406 if (irq >=0 && start != irq)
407 goto err;
408
409 if (start + cnt > nr_irqs) {
410 ret = irq_expand_nr_irqs(start + cnt);
411 if (ret)
412 goto err;
413 }
414
415 bitmap_set(allocated_irqs, start, cnt);
416 mutex_unlock(&sparse_irq_lock);
417 return alloc_descs(start, cnt, node, owner);
418
419err:
420 mutex_unlock(&sparse_irq_lock);
421 return ret;
422}
423EXPORT_SYMBOL_GPL(__irq_alloc_descs);
424
425/**
426 * irq_reserve_irqs - mark irqs allocated
427 * @from: mark from irq number
428 * @cnt: number of irqs to mark
429 *
430 * Returns 0 on success or an appropriate error code
431 */
432int irq_reserve_irqs(unsigned int from, unsigned int cnt)
433{
434 unsigned int start;
435 int ret = 0;
436
437 if (!cnt || (from + cnt) > nr_irqs)
438 return -EINVAL;
439
440 mutex_lock(&sparse_irq_lock);
441 start = bitmap_find_next_zero_area(allocated_irqs, nr_irqs, from, cnt, 0);
442 if (start == from)
443 bitmap_set(allocated_irqs, start, cnt);
444 else
445 ret = -EEXIST;
446 mutex_unlock(&sparse_irq_lock);
447 return ret;
448}
449
450/**
451 * irq_get_next_irq - get next allocated irq number
452 * @offset: where to start the search
453 *
454 * Returns next irq number after offset or nr_irqs if none is found.
455 */
456unsigned int irq_get_next_irq(unsigned int offset)
457{
458 return find_next_bit(allocated_irqs, nr_irqs, offset);
459}
460
461struct irq_desc *
462__irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
463 unsigned int check)
464{
465 struct irq_desc *desc = irq_to_desc(irq);
466
467 if (desc) {
468 if (check & _IRQ_DESC_CHECK) {
469 if ((check & _IRQ_DESC_PERCPU) &&
470 !irq_settings_is_per_cpu_devid(desc))
471 return NULL;
472
473 if (!(check & _IRQ_DESC_PERCPU) &&
474 irq_settings_is_per_cpu_devid(desc))
475 return NULL;
476 }
477
478 if (bus)
479 chip_bus_lock(desc);
480 raw_spin_lock_irqsave(&desc->lock, *flags);
481 }
482 return desc;
483}
484
485void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
486{
487 raw_spin_unlock_irqrestore(&desc->lock, flags);
488 if (bus)
489 chip_bus_sync_unlock(desc);
490}
491
492int irq_set_percpu_devid(unsigned int irq)
493{
494 struct irq_desc *desc = irq_to_desc(irq);
495
496 if (!desc)
497 return -EINVAL;
498
499 if (desc->percpu_enabled)
500 return -EINVAL;
501
502 desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
503
504 if (!desc->percpu_enabled)
505 return -ENOMEM;
506
507 irq_set_percpu_devid_flags(irq);
508 return 0;
509}
510
511/**
512 * dynamic_irq_cleanup - cleanup a dynamically allocated irq
513 * @irq: irq number to initialize
514 */
515void dynamic_irq_cleanup(unsigned int irq)
516{
517 struct irq_desc *desc = irq_to_desc(irq);
518 unsigned long flags;
519
520 raw_spin_lock_irqsave(&desc->lock, flags);
521 desc_set_defaults(irq, desc, desc_node(desc), NULL);
522 raw_spin_unlock_irqrestore(&desc->lock, flags);
523}
524
525/**
526 * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
527 * @irq: The interrupt number
528 * @cpu: The cpu number
529 *
530 * Returns the sum of interrupt counts on @cpu since boot for
531 * @irq. The caller must ensure that the interrupt is not removed
532 * concurrently.
533 */
534unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
535{
536 struct irq_desc *desc = irq_to_desc(irq);
537
538 return desc && desc->kstat_irqs ?
539 *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
540}
541
542/**
543 * kstat_irqs - Get the statistics for an interrupt
544 * @irq: The interrupt number
545 *
546 * Returns the sum of interrupt counts on all cpus since boot for
547 * @irq. The caller must ensure that the interrupt is not removed
548 * concurrently.
549 */
550unsigned int kstat_irqs(unsigned int irq)
551{
552 struct irq_desc *desc = irq_to_desc(irq);
553 int cpu;
554 int sum = 0;
555
556 if (!desc || !desc->kstat_irqs)
557 return 0;
558 for_each_possible_cpu(cpu)
559 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
560 return sum;
561}
562
563/**
564 * kstat_irqs_usr - Get the statistics for an interrupt
565 * @irq: The interrupt number
566 *
567 * Returns the sum of interrupt counts on all cpus since boot for
568 * @irq. Contrary to kstat_irqs() this can be called from any
569 * preemptible context. It's protected against concurrent removal of
570 * an interrupt descriptor when sparse irqs are enabled.
571 */
572unsigned int kstat_irqs_usr(unsigned int irq)
573{
574 int sum;
575
576 irq_lock_sparse();
577 sum = kstat_irqs(irq);
578 irq_unlock_sparse();
579 return sum;
580}